Haskell 2: Enum Classes

Ashley Yakeley ashley@semantic.org
Wed, 24 Oct 2001 18:14:47 -0700


OK, I'm from the school of thought that prefers everything to have a 
precise meaning, even if it means "multiplying entities". The fact is, in 
Haskell 98 the 'Enum' class does extensive multiple duty.

--
data Type a = Type

class Enum a where
     predMaybe,succMaybe :: a -> Maybe a

class (Enum a) => BoundedStartEnum a where
     nthValueFromStart :: Integer -> a
     valueNthFromStart :: a -> Integer

class (Enum a) => BoundedEndEnum a where
     nthValueFromEnd :: Integer -> a
     valueNthFromEnd :: a -> Integer

class (BoundedStartEnum a,BoundedEndEnum a) => FiniteEnum a where
     predWrap,succWrap :: a -> a
     nValues :: Type a -> Integer

class (Enum a) => UnboundedStartEnum a where
     pred :: a -> a

class (Enum a) => UnboundedEndEnum a where
     succ :: a -> a
--

Of course, Float, Double and Rational would not be instances of any of 
these classes. If you need 'add one', 'subtract one' functions, those go 
with numeric classes, which many Enum types would not be instances of. 
For instance, the letter 'q' is the successor of the letter 'p', but that 
does not mean that "'q' = 'p' + 1" is meaningful.

-- 
Ashley Yakeley, Seattle WA