Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Type-class instance for any type that has an instance of
      another type (Arlen Cuss)
   2. Re:  Type-class instance for any type that has an instance of
      another type (Arlen Cuss)
   3. Re:  Type-class instance for any type that has an instance of
      another type (Brent Yorgey)
   4.  Handling Maybes (Christopher Howard)
   5. Re:  Handling Maybes (Erik de Castro Lopo)
   6. Re:  Handling Maybes (Elvio Toccalino)


----------------------------------------------------------------------

Message: 1
Date: Tue, 24 May 2011 20:54:05 +1000
From: Arlen Cuss <[email protected]>
Subject: [Haskell-beginners] Type-class instance for any type that has
        an instance of another type
To: [email protected]
Message-ID: <1306234445.2526.7.camel@asu>
Content-Type: text/plain; charset="utf-8"

Hi all,

I may be a liiittle in over my head. I'm trying to make a type-class for
a type that can be "randomly generated". The class looks like this:

> class RandomlyGeneratable a where
>   rgen :: IO a

So far so good. Now, I'd love to be able to define an instance of this
on all types that have Enum, because the definition is not hard:

> instance Enum a => RandomlyGeneratable a where
>   rgen = fmap (opts !!) $ randomRIO (0,pred (length opts))
>     where opts = enumFrom $ (toEnum 0) :: a

Unfortunately I'm wording this incorrectly, as the "instance" line
produces:

    Illegal instance declaration for `RandomlyGeneratable a'
      (All instance types must be of the form (T a1 ... an)
       where a1 ... an are *distinct type variables*,
       and each type variable appears at most once in the instance head.
       Use -XFlexibleInstances if you want to disable this.)
    In the instance declaration for `RandomlyGeneratable a'

Could someone point out my error? Am I trying to do something on the
edge of possibility?

Thanks!

Arlen
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110524/b6ee3339/attachment-0001.pgp>

------------------------------

Message: 2
Date: Tue, 24 May 2011 21:13:25 +1000
From: Arlen Cuss <[email protected]>
Subject: Re: [Haskell-beginners] Type-class instance for any type that
        has an instance of another type
To: [email protected]
Message-ID: <1306235605.2526.11.camel@asu>
Content-Type: text/plain; charset="utf-8"

>     Illegal instance declaration for `RandomlyGeneratable a'
>       (All instance types must be of the form (T a1 ... an)
>        where a1 ... an are *distinct type variables*,
>        and each type variable appears at most once in the instance head.
>        Use -XFlexibleInstances if you want to disable this.)
>     In the instance declaration for `RandomlyGeneratable a'

"Solved" with the somewhat apocryphal:

> newtype ByEnum e = ByEnum { unByEnum :: e } 
>
> instance Enum k => RG (ByEnum k) where
>   rgen = fmap (ByEnum . (opts !!)) $ randomRIO (0,pred (length opts))
>     where opts = enumFrom $ toEnum 0

Then I can say:

> r <- fmap unByEnum rgen

and get my answer. I think I better understand what's going on. Here's
my reference:

http://groups.google.com/group/fa.haskell/browse_thread/thread/113d5834fddfa71a/0da43166c7c629cd?#0da43166c7c629cd

Cheers!

Arlen
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110524/cd7e4727/attachment-0001.pgp>

------------------------------

Message: 3
Date: Tue, 24 May 2011 08:58:14 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Type-class instance for any type that
        has an instance of another type
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Tue, May 24, 2011 at 08:54:05PM +1000, Arlen Cuss wrote:
> Hi all,
> 
> I may be a liiittle in over my head. I'm trying to make a type-class for
> a type that can be "randomly generated". The class looks like this:
> 
> > class RandomlyGeneratable a where
> >   rgen :: IO a
> 
> So far so good. Now, I'd love to be able to define an instance of this
> on all types that have Enum, because the definition is not hard:
> 
> > instance Enum a => RandomlyGeneratable a where
> >   rgen = fmap (opts !!) $ randomRIO (0,pred (length opts))
> >     where opts = enumFrom $ (toEnum 0) :: a
> 
> Unfortunately I'm wording this incorrectly, as the "instance" line
> produces:
> 
>     Illegal instance declaration for `RandomlyGeneratable a'
>       (All instance types must be of the form (T a1 ... an)
>        where a1 ... an are *distinct type variables*,
>        and each type variable appears at most once in the instance head.
>        Use -XFlexibleInstances if you want to disable this.)
>     In the instance declaration for `RandomlyGeneratable a'

Actually there wasn't much wrong with your syntax here, it's just that
the basic Haskell standard is quite rigid in terms of what sorts of
instances are accepted.  If you turned on the flag -XFlexibleInstances
(which is quite uncontroversial and widely supported) as the error
message suggests, it would work fine.

However, with this instance you would not able to make any other
RandomlyGeneratable instances -- essentially the instance says "any
type a is an instance of RandomlyGeneratable, and it is an error if
the type a is not an instance of Enum".  So for that reason the
newytpe approach (what you wrote in your second email) is to be
preferred.

By the way, such a class already exists -- it is called Random [1].
But if you are just doing this as a learning exercise, then go right
ahead.

-Brent

[1] 
http://hackage.haskell.org/packages/archive/random/latest/doc/html/System-Random.html#t:Random



------------------------------

Message: 4
Date: Tue, 24 May 2011 19:09:05 -0800
From: Christopher Howard <[email protected]>
Subject: [Haskell-beginners] Handling Maybes
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Since I started into Haskell, I frequently run into a scenario like
this: some function returns a Maybe value. If it is a Nothing, I want to
do one thing, but if it isn't, I want to do something with the value
encapsulated by the Maybe. But before I can use that value (say, to
print it) I've got to pull it out of the Maybe in a way that satisfies
the compiler. So one option is to use a separate function with pattern
matching, for example:

handleMaybeVal Nothing = do someAction
                            someOtherActionEtc
handleMaybeVal (Just a) = putStrLn ("The string in reverse is " ++
                                   (reverse a))

Or I can use the maybe function with a conditional

main = do val <- someIOFunc -- val is a Maybe String
          if val == Nothing
            then do someAction
              someOtherActionEtc
            else putStrLn ("The string in reverse is " ++
                          ((reverse . unpack) val))
  where unpack a = maybe
                     (error "unreachable error")
                     (\b -> b) a

The second option I like better, for some reason, but it is a bit
dangerous, as I have to double check my code to make sure the default
value passed to "maybe" cannot possibly be evaluated. (Alternatively, I
could pass in a regular default value, like a blank string, though that
is actually more dangerous in my mind because a bad value might be used
silently.)

Anyway: how do you pros go about handling situations like these?

-- 
frigidcode.com
theologia.indicium.us



------------------------------

Message: 5
Date: Wed, 25 May 2011 13:15:58 +1000
From: Erik de Castro Lopo <[email protected]>
Subject: Re: [Haskell-beginners] Handling Maybes
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Christopher Howard wrote:

> Since I started into Haskell, I frequently run into a scenario like
> this: some function returns a Maybe value. If it is a Nothing, I want to
> do one thing, but if it isn't, I want to do something with the value
> encapsulated by the Maybe. But before I can use that value (say, to
> print it) I've got to pull it out of the Maybe in a way that satisfies
> the compiler. So one option is to use a separate function with pattern
> matching, for example:
> 
> handleMaybeVal Nothing = do someAction
>                             someOtherActionEtc
> handleMaybeVal (Just a) = putStrLn ("The string in reverse is " ++
>                                    (reverse a))
> 
> Or I can use the maybe function with a conditional
> 
> main = do val <- someIOFunc -- val is a Maybe String
>           if val == Nothing
>             then do someAction
>               someOtherActionEtc
>             else putStrLn ("The string in reverse is " ++
>                           ((reverse . unpack) val))
>   where unpack a = maybe
>                      (error "unreachable error")
>                      (\b -> b) a
> 
> The second option I like better, for some reason, but it is a bit
> dangerous, as I have to double check my code to make sure the default
> value passed to "maybe" cannot possibly be evaluated. (Alternatively, I
> could pass in a regular default value, like a blank string, though that
> is actually more dangerous in my mind because a bad value might be used
> silently.)
> 
> Anyway: how do you pros go about handling situations like these?

I usually prefer an explicit pattern match like this:

main = do val <- someIOFunc -- val is a Maybe String
          case val of
            Nothing -> do someAction
                          someOtherActionEtc
            Just s  -> putStrLn ("The string in reverse is " ++ (reverse s))


HTH,
Erik
-- 
----------------------------------------------------------------------
Erik de Castro Lopo
http://www.mega-nerd.com/



------------------------------

Message: 6
Date: Wed, 25 May 2011 00:21:51 -0300
From: Elvio Toccalino <[email protected]>
Subject: Re: [Haskell-beginners] Handling Maybes
To: Christopher Howard <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID: <1306293714.1606.7.camel@el-fierro>
Content-Type: text/plain; charset="UTF-8"

Your second option may or may not be dangerous, but it sure looks
tricky. I wouldn't use 'maybe' that way.
How about this?

main = do val <- someIOFunc -- val is a Maybe String
          maybe (someAction >> someOtherActionEtc)
                (\theValue -> putStrLn ("the string in reverse is " ++
(reverse theValue))
                val

If you like point-free definitions:

main = do val <- someIOFunc -- val is a Maybe String
          maybe (someAction >> someOtherActionEtc)
                (putStrLn . ("the string in reverse is " ++) . reverse)
                val

The difference is that my use of 'maybe' involves giving it actions, and
not just pure functions.

On Tue, 2011-05-24 at 19:09 -0800, Christopher Howard wrote:
> Since I started into Haskell, I frequently run into a scenario like
> this: some function returns a Maybe value. If it is a Nothing, I want to
> do one thing, but if it isn't, I want to do something with the value
> encapsulated by the Maybe. But before I can use that value (say, to
> print it) I've got to pull it out of the Maybe in a way that satisfies
> the compiler. So one option is to use a separate function with pattern
> matching, for example:
> 
> handleMaybeVal Nothing = do someAction
>                             someOtherActionEtc
> handleMaybeVal (Just a) = putStrLn ("The string in reverse is " ++
>                                    (reverse a))
> 
> Or I can use the maybe function with a conditional
> 
> main = do val <- someIOFunc -- val is a Maybe String
>           if val == Nothing
>             then do someAction
>               someOtherActionEtc
>             else putStrLn ("The string in reverse is " ++
>                           ((reverse . unpack) val))
>   where unpack a = maybe
>                      (error "unreachable error")
>                      (\b -> b) a
> 
> The second option I like better, for some reason, but it is a bit
> dangerous, as I have to double check my code to make sure the default
> value passed to "maybe" cannot possibly be evaluated. (Alternatively, I
> could pass in a regular default value, like a blank string, though that
> is actually more dangerous in my mind because a bad value might be used
> silently.)
> 
> Anyway: how do you pros go about handling situations like these?
> 





------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 35, Issue 47
*****************************************

Reply via email to