dmwit / universe

Classes for types where we know all the values
BSD 3-Clause "New" or "Revised" License
37 stars 17 forks source link

Instance for Bounded Enum #19

Closed jachymb closed 8 years ago

jachymb commented 8 years ago

Could we have instance for Enum a, Bounded a where universe = [minBound..maxBound]?

phadej commented 8 years ago

There is universeDef in universe-base, which let you define:

instance Universe Foo where universe = universeDef
instance Finite Foo

-- or even on recent enough GHC (with DefaultSignatures)
instance Universe Foo
instance Finite Foo

We could add BoundedEnum though:

newtype BoundedEnum a = BoundedEnum { getBoundedEnum :: a }

instance (Bounded a, Enum a) => Universe (BoundedEnum a) where universe = universeDef
instance (Bounded a, Enum a) => Finite (BoundedEnum a)

what @dmwit thinks about that?

dmwit commented 8 years ago

Right, universeDef (or using the default, for GHCs that support it) is the intended way to use [minBound .. maxBound]. Additionally with DeriveAnyClass turned on you ought to be able to write, for example:

data Suit = Clubs | Diamonds | Spades | Hearts deriving (Bounded, Enum, Universe, Finite)

I'd be perfectly happy with adding a newtype if @jachymb plans to use it, but probably the above mechanisms are already convenient enough...?

jachymb commented 8 years ago

It seems enough, I did not know it. Thank you