[Haskell-beginners] Type-class instance for any type that has an instance of another type

Arlen Cuss celtic at sairyx.org
Tue May 24 12:54:05 CEST 2011


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.pgp>


More information about the Beginners mailing list