emgm-0.4: Extensible and Modular Generics for the Masses

Portabilitynon-portable
Stabilityexperimental
Maintainergenerics@haskell.org

Generics.EMGM.Functions.Compare

Contents

Description

Summary: Generic functions for comparing two values in different ways.

The fundamental function here is compare, a function that returns the Ordering of two values (less than, equal to, or greater than). It uses the same lexicographical ordering as deriving Ord (e.g. left alternative of a sum is less than the right alternative, the first component of a product is compared first while the second is only compared if the first is equal, etc.).

All of the remaining functions are simply derived (in the most obvious way) from compare. All of these functions are equivalent to methods in the Eq and Ord type classes. The difference with using this approach vs. deriving (Eq, Ord) is that you can write ad-hoc cases for certain datatypes while most of the functionality is handled generically.

Synopsis

Compare

compare is equivalent to the function of the same name when deriving Ord. All other comparison functions in this module are derived from compare.

newtype Compare a Source

The type of a generic function that takes two values of the same type and returns an Ordering.

Constructors

Compare 

Fields

selCompare :: a -> a -> Ordering
 

Instances

compare :: Rep Compare a => a -> a -> OrderingSource

Compare two values and return an Ordering (i.e. LT, GT, or EQ). This is implemented exactly as if the datatype was deriving Ord.

Equality, inequality

These functions are equivalent to (==) and (/=) when deriving Eq.

eq :: Rep Compare a => a -> a -> BoolSource

Equal to. Returns x == y.

neq :: Rep Compare a => a -> a -> BoolSource

Not equal to. Returns x /= y.

Less than, greater than

These functions are equivalent to (<), (<=), (>), and (>=) when deriving Ord.

lt :: Rep Compare a => a -> a -> BoolSource

Less than. Returns x < y.

lteq :: Rep Compare a => a -> a -> BoolSource

Less than or equal to. Returns x <= y.

gt :: Rep Compare a => a -> a -> BoolSource

Greater than. Returns x > y.

gteq :: Rep Compare a => a -> a -> BoolSource

Greater than or equal to. Returns x >= y.

Minimum and maximum

These functions are equivalent to functions of the same name when deriving Ord.

min :: Rep Compare a => a -> a -> aSource

The minimum of two values.

max :: Rep Compare a => a -> a -> aSource

The maximum of two values.