[Haskell-cafe] Circular enums
Ben Butler-Cole
ben at bridesmere.com
Sat Feb 2 18:47:39 EST 2008
[Resend with formatting maybe fixed.]
Hello
I'm trying to define functions that allow you to traverse a bounded enumeration, "wrapping" at the start and the end.
My implementation looks like this:
next, prev :: (Enum a, Bounded a) => a -> a
next = turn 1
prev = turn (-1)
turn :: (Enum a, Bounded a) => Int -> a -> a
turn n e = toEnum (add (fromEnum (maxBound::a) + 1) (fromEnum e) n)
where
add mod x y = (x + y + mod) `rem` mod
Which fails to type check under GHC with this error:
No instance for (Bounded a)
arising from use of `maxBound' at Hbot.hs:6:34-41
Probable fix: add (Bounded a) to the expected type of an expression
In the expression: maxBound :: a
In the first argument of `fromEnum', namely `(maxBound :: a)'
In the first argument of `(+)', namely `fromEnum (maxBound :: a)'
My (clearly flawed) understanding of the signature I've specified for 'turn' means *exactly* that a is Bounded.
Can anyone enlighten me as to where my understanding is going awry and how (whether) I can achieve what I'm trying to do.
Thank you
Ben
More information about the Haskell-Cafe
mailing list