
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