
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

On Feb 2, 2008, at 18:41 , Ben Butler-Cole wrote:
No instance for (Bounded a) arising from use of `maxBound' at Hbot.hs:6:34-41 (...) My (clearly flawed) understanding of the signature I've specified for 'turn' means *exactly* that a is Bounded.
The problem is that the scope of a is the type signature; it does *not* extend to the definition. You can try omitting the type on maxBound and minBound, or you can enable the scoped type variables extension ( http://www.haskell.org/ ghc/docs/latest/html/users_guide/other-type-extensions.html#scoped- type-variables ). -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
participants (2)
-
Ben Butler-Cole
-
Brandon S. Allbery KF8NH