[Haskell-cafe] deriving instances of Enum for tuples of bounded, enumerable types (useful for generating QuickCheck.Arbitrary instances for collection of collection types)

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Sat Mar 8 22:23:02 EST 2008

On Mar 8, 2008, at 22:06 , Thomas Hartman wrote:

> A minor issue: I had a question if I could make the type signatures
> for Enum instances less  verbose by doing something like type
> BoundedEnum = (Bounded a, Enum a) => a... I tried, and commented out
> my attempt as it wouldn't type check. Guidance appreciated.

Nope.  Uses of "type" declarations are self-contained; if you do

   type BoundedEnum = (Bounded a, Enum a) => a

   myFunc :: BoundedEnum -> BoundedEnum -> BoundedEnum

each instance of BoundedEnum in the declaration of BoundedEnum refers  
to a distinct a.  More technically, the declaration expands to:

   myFunc :: (forall a. (Bounded a, Enum a) => a) ->
             (forall a. (Bounded a, Enum a) => a) ->
             (forall a. (Bounded a, Enum a) => a)

which is a rather useless declaration (I think the only inhabitant of  
that type is _|_).  And yes, this is annoying.

I think you might be able to do this as a typeclass instead, at the  
expense of having to insert an instance declaration for each type.   
(You will have to use an extension if you want to declare instances  
for types such as Int.  I think.)

   class (Bounded a, Enum a) => BoundedEnum a where -- empty

   instance BoundedEnum MyType a where
   instance BoundedEnum Int where -- requires FlexibleInstances?

brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH

More information about the Haskell-Cafe mailing list