Ticket #109 (new task)

Opened 6 years ago

split Enum into two typeclasses

Reported by: guest Owned by: none
Milestone: Keywords:
Cc: Section:
State:

Description

The current uses of Enum are somewhat confusing -- this is partially because of the name, but partially because two seperate interfaces are glued together. One provides Enumeration of all possible values and conversion to integers for binary storage, and the other provides sequences of values (usually arithmetic) (which [a..b], etc desugar to).

class Enum a where
    succ, pred :: a -> a
    toEnum     :: Int -> a
    fromEnum   :: a -> Int

instance Enum Integer
instance Enum Int
instance Enum Bool
instance Enum Ordering
instance Enum Word...
class ArithmeticSequence a where
    stepFrom        :: a -> [a]           -- [n..]
    stepFromThen    :: a -> a -> [a]      -- [n, n'..]
    stepFromTo      :: a -> a -> [a]      -- [n..m]
    stepFromThenTo  :: a -> a -> a -> [a] -- [n,n'..m]

instance ArithmeticSequence Integer
instance ArithmeticSequence Int
instance ArithmeticSequence Bool
instance ArithmeticSequence Ordering
instance ArithmeticSequence Float
instance ArithmeticSequence Double
instance ArithmeticSequence Rational
instance ArithmeticSequence Word...
...

Steppable may be a preferrable name.

pros:

  • Clearly divides two seperate uses, while keeping functionality.
  • Can re-introduce relationship between Ix and Enum?

cons:

  • Yet another typeclass.
  • Slightly misleading name, as non-arithmetic structures _should_ be supported. Also a bit long.
  • Automatically supporting such isn't trivial if they're not in Enum, though.
Note: See TracTickets for help on using tickets.