[Haskell-beginners] "Cyclic" instance of Enum

Daniel Fischer daniel.is.fischer at googlemail.com
Tue Feb 1 20:43:01 CET 2011


On Tuesday 01 February 2011 20:14:05, Patrick LeBoutillier wrote:
> Hi,
>
> I've created an instance of Enum that repeats the same elements when
> it gets to the end:
>


>
> Does this violate any implicit rules that an Enum instance should
> follow?

If you make that type also an instance of Bounded. Says the report:

>  For any type that is an instance of class Bounded as well as Enum, the
> following should hold:
>
>     * The calls succ maxBound and pred minBound should result in a
> runtime error. * fromEnum and toEnum should give a runtime error if the
> result value is not representable in the result type. For example,
> toEnum 7 :: Bool is an error. * enumFrom and enumFromThen should be
> defined with an implicit bound, thus: enumFrom     x   = enumFromTo    
> x maxBound
>         enumFromThen x y = enumFromThenTo x y bound
>           where
>             bound | fromEnum y >= fromEnum x = maxBound
>
>                   | otherwise                = minBound

but otherwise, it's okay.

> Is there a better way to do this?

None obvious.

Cheers,
Daniel




More information about the Beginners mailing list