Portability | portable |
---|---|
Stability | experimental |
Maintainer | paul@cogito.org.uk |
Data.Ranged.Boundaries
Description
- class Ord a => DiscreteOrdered a where
- adjacent :: a -> a -> Bool
- adjacentBelow :: a -> Maybe a
- enumAdjacent :: (Ord a, Enum a) => a -> a -> Bool
- boundedAdjacent :: (Ord a, Enum a) => a -> a -> Bool
- boundedBelow :: (Eq a, Enum a, Bounded a) => a -> Maybe a
- data Boundary a
- = BoundaryAbove a
- | BoundaryBelow a
- | BoundaryAboveAll
- | BoundaryBelowAll
- above :: Ord v => Boundary v -> v -> Bool
- (/>/) :: Ord v => v -> Boundary v -> Bool
Documentation
class Ord a => DiscreteOrdered a whereSource
Distinguish between dense and sparse ordered types. A dense type is
one in which any two values v1 < v2
have a third value v3
such that
v1 < v3 < v2
.
In theory the floating types are dense, although in practice they can only have finitely many values. This class treats them as dense.
Tuples up to 4 members are declared as instances. Larger tuples may be added if necessary.
Most values of sparse types have an adjacentBelow
, such that, for all x:
case adjacentBelow x of Just x1 -> adjacent x1 x Nothing -> True
The exception is for bounded types when x == lowerBound
. For dense types
adjacentBelow
always returns Nothing
.
This approach was suggested by Ben Rudiak-Gould on comp.lang.functional.
enumAdjacent :: (Ord a, Enum a) => a -> a -> BoolSource
Check adjacency for sparse enumerated types (i.e. where there
is no value between x
and succ x
).
boundedAdjacent :: (Ord a, Enum a) => a -> a -> BoolSource
Check adjacency, allowing for case where x = maxBound. Use as the definition of adjacent for bounded enumerated types such as Int and Char.
boundedBelow :: (Eq a, Enum a, Bounded a) => a -> Maybe aSource
The usual implementation of adjacentBelow
for bounded enumerated types.
A Boundary is a division of an ordered type into values above and below the boundary. No value can sit on a boundary.
Known bug: for Bounded types
BoundaryAbove maxBound < BoundaryAboveAll
BoundaryBelow minBound > BoundaryBelowAll
This is incorrect because there are no possible values in between the left and right sides of these inequalities.
Constructors
BoundaryAbove a | The argument is the highest value below the boundary. |
BoundaryBelow a | The argument is the lowest value above the boundary. |
BoundaryAboveAll | The boundary above all values. |
BoundaryBelowAll | The boundary below all values. |