wren romano (
winterkoninkje) wrote2013-05-29 07:53 pm
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Entry tags:
ANN: prelude-safeenum
prelude-safeenum 0.1.0
The prelude-safeenum package offers a safe alternative to the Prelude's Enum
class in order to render it safe. While we're at it, we also generalize the notion of enumeration to support types which can only be enumerated in one direction.
Description
The prelude-safeenum package offers an alternative to the notion of enumeration provided by the Prelude. For now it is just a package, but the eventual goal is to be incorporated into haskell prime. Some salient characteristics of the new type-class hierarchy are:
- Removes partial functions
- The Haskell Language Report section 6.3.4 defines
pred
,succ
,fromEnum
, andtoEnum
to be partial functions when the type isBounded
, but this is unacceptable. The new classes remove this problem by correcting the type signatures for these functions. - Generalizes the notion of enumeration
- Rather than requiring that the type is linearly enumerable, we distinguish between forward enumeration (which allows for multiple predecessors) and backward enumeration (which allows for multiple successors).
- Adds new functions:
enumDownFrom
,enumDownFromTo
- One of the big problems with the partiality of
pred
is that there is no safe way to enumerate downwards since in the border caseenumFromThen x (pred x)
will throw an error rather than evaluating to[x]
as desired. These new functions remove this problem. - Removes the requirement...
- ...that the enumeration order coincides with the
Ord
ordering (if one exists). Though, of course, it's advisable to keep them in sync if possible, for your sanity. - Ensures that the notion of enumeration is well-defined
- This much-needed rigor clarifies the meaning of enumeration. In addition, it rules out instances for
Float
andDouble
which are highly problematic and often confuse newcomers to Haskell. Unfortunately, this rigor does render the instance forRatio
problematic. However,Ratio
instances can be provided so long as the base type is enumerable (andIntegral
, naturally); but they must be done in an obscure order that does not coincide withOrd
. - The obscure order required for well-defined enumeration of
Ratio
is provided.
Links
- Homepage: http://code.haskell.org/~wren/
- Hackage: http://hackage.haskell.org/package/prelude-safeenum
- Darcs: http://community.haskell.org/~wren/prelude-safeenum
- Haddock: Darcs version