Haskell Hierarchical Libraries (collections package)ContentsIndex
Data.COrdering
Portabilityportable
Stabilitystable
Maintainerhttp://homepages.nildram.co.uk/~ahey/em.png
Contents
Types
Useful combinators
Misc.
For combining "equal" values with a user supplied function.
Description

This module defines a useful variant of the Prelude Ordering data type.

Typically this data type is used as the result of a "combining comparison" which combines values that are deemed to be equal (somehow). Note that the functions defined here adhere to the same ordering convention as the overloaded compare (from the Ord class). That is..

 a `compare` b -> LT (or Lt) implies a < b   
 a `compare` b -> GT (or Gt) implies a > b   
 

The combinators exported from this module have a "CC" suffix if they return a combining comparison (most of them) and a "C" suffix if they return an ordinary comparison. All the combinators defined here are INLINEd, in the hope that the compiler can avoid the overhead of using HOFs for frequently used comparisons (dunno if this does any good though :-)

Synopsis
data COrdering a
= Lt
| Eq a
| Gt
unitCC :: Ord a => a -> a -> COrdering ()
unitByCC :: (a -> b -> Ordering) -> a -> b -> COrdering ()
fstCC :: Ord a => a -> a -> COrdering a
fstByCC :: (a -> b -> Ordering) -> a -> b -> COrdering a
sndCC :: Ord a => a -> a -> COrdering a
sndByCC :: (a -> b -> Ordering) -> a -> b -> COrdering b
flipC :: (a -> b -> Ordering) -> b -> a -> Ordering
flipCC :: (a -> b -> COrdering c) -> b -> a -> COrdering c
withCC :: Ord a => (a -> a -> b) -> a -> a -> COrdering b
withCC' :: Ord a => (a -> a -> b) -> a -> a -> COrdering b
withByCC :: (a -> b -> Ordering) -> (a -> b -> c) -> a -> b -> COrdering c
withByCC' :: (a -> b -> Ordering) -> (a -> b -> c) -> a -> b -> COrdering c
Types
data COrdering a
Result of a combining comparison.
Constructors
Lt
Eq a
Gt
show/hide Instances
Typeable1 COrdering
Eq a => Eq (COrdering a)
Ord a => Ord (COrdering a)
Read a => Read (COrdering a)
Show a => Show (COrdering a)
Typeable e => Typeable (COrdering e)
Useful combinators
Misc.
unitCC :: Ord a => a -> a -> COrdering ()

A combining comparison for an instance of Ord which returns unit () where appropriate.

unitCC a b = case compare a b of LT -> Lt
                                 EQ -> Eq ()
                                 GT -> Gt
unitByCC :: (a -> b -> Ordering) -> a -> b -> COrdering ()

Create a combining comparison from an ordinary comparison by returning unit () where appropriate.

unitByCC cmp a b = case cmp a b of LT -> Lt
                                   EQ -> Eq ()
                                   GT -> Gt
fstCC :: Ord a => a -> a -> COrdering a

A combining comparison for an instance of Ord which keeps the first argument if they are deemed equal. The second argument is discarded in this case.

fstCC a a' = case compare a a' of LT -> Lt
                                  EQ -> Eq a
                                  GT -> Gt
fstByCC :: (a -> b -> Ordering) -> a -> b -> COrdering a

Create a combining comparison from an ordinary comparison by keeping the first argument if they are deemed equal. The second argument is discarded in this case.

fstByCC cmp a b = case cmp a b of LT -> Lt
                                  EQ -> Eq a
                                  GT -> Gt
sndCC :: Ord a => a -> a -> COrdering a

A combining comparison for an instance of Ord which keeps the second argument if they are deemed equal. The first argument is discarded in this case.

sndCC a a' = case compare a a' of LT -> Lt
                                  EQ -> Eq a'
                                  GT -> Gt
sndByCC :: (a -> b -> Ordering) -> a -> b -> COrdering b

Create a combining comparison from an ordinary comparison by keeping the second argument if they are deemed equal. The first argument is discarded in this case.

sndByCC cmp a b = case cmp a b of LT -> Lt
                                  EQ -> Eq b
                                  GT -> Gt
flipC :: (a -> b -> Ordering) -> b -> a -> Ordering

Converts a comparison to one which takes arguments in flipped order, but preserves the ordering that would be given by the "unflipped" version (disregarding type issues). So it's not the same as using the prelude flip (which would reverse the ordering too).

flipC cmp b a = case cmp a b of LT -> GT
                                EQ -> EQ
                                GT -> LT
flipCC :: (a -> b -> COrdering c) -> b -> a -> COrdering c

Converts a combining comparison to one which takes arguments in flipped order, but preserves the ordering that would be given by the "unflipped" version (disregarding type issues). So it's not the same as using the prelude flip (which would reverse the ordering too).

flipCC cmp b a = case cmp a b of Lt       -> Gt
                                 e@(Eq _) -> e
                                 Gt       -> Lt
For combining "equal" values with a user supplied function.
withCC :: Ord a => (a -> a -> b) -> a -> a -> COrdering b

Create a combining comparison using the supplied combining function, which is applied if compare returns EQ. See withCC' for a stricter version of this function.

withCC f a a' = case compare a a' of LT -> Lt
                                     EQ -> Eq (f a a')
                                     GT -> Gt
withCC' :: Ord a => (a -> a -> b) -> a -> a -> COrdering b

Same as withCC, except the combining function is applied strictly.

withCC' f a a' = case compare a a' of LT -> Lt
                                      EQ -> let b = f a a' in b `seq` Eq b
                                      GT -> Gt
withByCC :: (a -> b -> Ordering) -> (a -> b -> c) -> a -> b -> COrdering c

Create a combining comparison using the supplied comparison and combining function, which is applied if the comparison returns EQ. See withByCC' for a stricter version of this function.

withByCC cmp f a b = case cmp a b of LT -> Lt
                                     EQ -> Eq (f a b)
                                     GT -> Gt
withByCC' :: (a -> b -> Ordering) -> (a -> b -> c) -> a -> b -> COrdering c

Same as withByCC, except the combining function is applied strictly.

withByCC' cmp f a b = case cmp a b of LT -> Lt
                                      EQ -> let c = f a b in c `seq` Eq c
                                      GT -> Gt
Produced by Haddock version 0.7