"Cyclic" instance of Enum

Hi, I've created an instance of Enum that repeats the same elements when it gets to the end: module Music.Note.Name where data Name = C | D | E | F | G | A | B deriving (Show, Eq) instance Enum Name where toEnum 0 = C toEnum 1 = D toEnum 2 = E toEnum 3 = F toEnum 4 = G toEnum 5 = A toEnum 6 = B toEnum i = toEnum $ i `mod` 7 fromEnum C = 0 fromEnum D = 1 fromEnum E = 2 fromEnum F = 3 fromEnum G = 4 fromEnum A = 5 fromEnum B = 6 enumFromTo x y = map toEnum [a .. b'] where a = fromEnum x b = fromEnum y b' = if a <= b then b else b + 7 enumFromThen x1 x2 = error "enumFromThen not supported for Music.Note.Name" enumFromThenTo x1 x2 y = error "enumFromThenTo not supported for Music.Note.Name" Does this violate any implicit rules that an Enum instance should follow? Is there a better way to do this? Thanks, Patrick -- ===================== Patrick LeBoutillier Rosemère, Québec, Canada

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
participants (2)
-
Daniel Fischer
-
Patrick LeBoutillier