[Haskell-cafe] Why does Enum succ and pred functions throw exception

wren ng thornton wren at freegeek.org
Mon Jun 25 06:13:44 CEST 2012

On 6/21/12 1:15 PM, Brandon Allbery wrote:
> On Thu, Jun 21, 2012 at 5:11 AM, Rouan van Dalen<rvdalen at yahoo.co.uk>wrote:
>> Can anyone shed some light on why the succ and pred functions of the Enum
>> typeclass throw
>> exceptions if we go over the upper or lower boundary, and not return Maybe
>> a?
> Enum and Bounded have a complicated and arguably incorrect[1] relationship.
>   The whole thing should be overhauled,

Indeed. In light of the exception-throwing specification, it's not 
trivial to define a safe enumFromDownTo function satisfying the 

     enumFromDownTo x y == reverse (enumFromTo y x)

with all the properties we'd like to have (e.g., list fusion). 
Essentially, we need access to whatever predicate decides to throw an 
error in the pred function, in order to use it to return the empty list 
when appropriate. The predicate can be reconstructed from Ord or from 
Bounded and Eq but both of those constructions make non-trivial 
assumptions about how the classes interact.

Hence, the Enum class in incomplete, in addition to any correctness 
problems interacting with other classes.

Live well,

More information about the Haskell-Cafe mailing list