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

Arlen Cuss celtic at sairyx.org
Tue May 24 13:13:25 CEST 2011


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


More information about the Beginners mailing list