| Copyright | (c) The University of Glasgow 2005 |
|---|---|
| License | BSD-style (see the file libraries/base/LICENSE) |
| Maintainer | libraries@haskell.org |
| Stability | stable |
| Portability | portable |
| Safe Haskell | Safe |
| Language | Haskell2010 |
Data.Ord
Description
Orderings
Documentation
class Eq a => Ord a where Source #
The Ord class is used for totally ordered datatypes.
Instances of Ord can be derived for any user-defined datatype whose
constituent types are in Ord. The declared order of the constructors in
the data declaration determines the ordering in derived Ord instances. The
Ordering datatype allows a single comparison to determine the precise
ordering of two objects.
Ord, as defined by the Haskell report, implements a total order and has the
following properties:
- Comparability
x <= y || y <= x=True- Transitivity
- if
x <= y && y <= z=True, thenx <= z=True - Reflexivity
x <= x=True- Antisymmetry
- if
x <= y && y <= x=True, thenx == y=True
The following operator interactions are expected to hold:
x >= y=y <= xx < y=x <= y && x /= yx > y=y < xx < y=compare x y == LTx > y=compare x y == GTx == y=compare x y == EQmin x y == if x <= y then x else y=Truemax x y == if x >= y then x else y=True
Note that (7.) and (8.) do not require min and max to return either of
their arguments. The result is merely required to equal one of the
arguments in terms of (==). Users who expect a stronger guarantee are advised
to write their own min and/or max functions.
The nuance of the above distinction is not always fully internalized by
developers, and in the past (tracing back to the Haskell 1.4 Report) the
specification for Ord asserted the stronger property that (min x y, max x
y) = (x, y) or (y, x), or in other words, that min and max will
return one of their arguments, using argument order as the tie-breaker if
the arguments are equal by comparison. A few list and
Foldable functions have behavior that is best understood
with this assumption in mind: all variations of minimumBy and maximumBy
(which can't use min and max in their implementations) are written such
that minimumBy and comparemaximumBy are respectively
equivalent to compareminimum and maximum (which do use min and max) only if
min and max adhere to this tie-breaking convention. Otherwise, if there
are multiple least or largest elements in a container, minimum and
maximum may not return the same one that minimumBy and
comparemaximumBy do (though they should return something that is
equal). (This is relevant for types with non-extensional equality, like
compareArg, but also in cases where the precise reference held
matters for memory-management reasons.) Unless there is a reason to deviate,
it is less confusing for implementors of Ord to respect this same
convention (as the default definitions of min and max do).
Minimal complete definition: either compare or <=.
Using compare can be more efficient for complex types.
Methods
compare :: a -> a -> Ordering Source #
(<) :: a -> a -> Bool infix 4 Source #
(<=) :: a -> a -> Bool infix 4 Source #
(>) :: a -> a -> Bool infix 4 Source #
Instances
Instances
| Monoid Ordering Source # | Since: base-2.1 |
| Semigroup Ordering Source # | Since: base-4.9.0.0 |
| Data Ordering Source # | Since: base-4.0.0.0 |
Defined in GHC.Internal.Data.Data Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Ordering -> c Ordering Source # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c Ordering Source # toConstr :: Ordering -> Constr Source # dataTypeOf :: Ordering -> DataType Source # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c Ordering) Source # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Ordering) Source # gmapT :: (forall b. Data b => b -> b) -> Ordering -> Ordering Source # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Ordering -> r Source # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Ordering -> r Source # gmapQ :: (forall d. Data d => d -> u) -> Ordering -> [u] Source # gmapQi :: Int -> (forall d. Data d => d -> u) -> Ordering -> u Source # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Ordering -> m Ordering Source # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Ordering -> m Ordering Source # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Ordering -> m Ordering Source # | |
| Bounded Ordering Source # | Since: base-2.1 |
| Enum Ordering Source # | Since: base-2.1 |
Defined in GHC.Internal.Enum Methods succ :: Ordering -> Ordering Source # pred :: Ordering -> Ordering Source # toEnum :: Int -> Ordering Source # fromEnum :: Ordering -> Int Source # enumFrom :: Ordering -> [Ordering] Source # enumFromThen :: Ordering -> Ordering -> [Ordering] Source # enumFromTo :: Ordering -> Ordering -> [Ordering] Source # enumFromThenTo :: Ordering -> Ordering -> Ordering -> [Ordering] Source # | |
| Generic Ordering Source # | |
Defined in GHC.Internal.Generics | |
| Ix Ordering Source # | Since: base-2.1 |
Defined in GHC.Internal.Ix | |
| Read Ordering Source # | Since: base-2.1 |
| Show Ordering Source # | Since: base-2.1 |
| Eq Ordering Source # | |
| Ord Ordering Source # | |
Defined in GHC.Classes | |
| type Rep Ordering Source # | Since: base-4.6.0.0 |
The Down type allows you to reverse sort order conveniently. A value of type
contains a value of type Down aa (represented as ).Down a
If a has an instance associated with it then comparing two
values thus wrapped will give you the opposite of their normal sort order.
This is particularly useful when sorting in generalised list comprehensions,
as in: Ordthen sortWith by .Down x
>>>compare True FalseGT
>>>compare (Down True) (Down False)LT
If a has a instance then the wrapped instance also respects
the reversed ordering by exchanging the values of Bounded and
minBound.maxBound
>>>minBound :: Int-9223372036854775808
>>>minBound :: Down IntDown 9223372036854775807
All other instances of behave as they do for Down aa.
Since: base-4.6.0.0
Instances
| Foldable1 Down Source # | Since: base-4.18.0.0 | ||||
Defined in Data.Foldable1 Methods fold1 :: Semigroup m => Down m -> m Source # foldMap1 :: Semigroup m => (a -> m) -> Down a -> m Source # foldMap1' :: Semigroup m => (a -> m) -> Down a -> m Source # toNonEmpty :: Down a -> NonEmpty a Source # maximum :: Ord a => Down a -> a Source # minimum :: Ord a => Down a -> a Source # foldrMap1 :: (a -> b) -> (a -> b -> b) -> Down a -> b Source # foldlMap1' :: (a -> b) -> (b -> a -> b) -> Down a -> b Source # foldlMap1 :: (a -> b) -> (b -> a -> b) -> Down a -> b Source # foldrMap1' :: (a -> b) -> (a -> b -> b) -> Down a -> b Source # | |||||
| Eq1 Down Source # | Since: base-4.12.0.0 | ||||
| Ord1 Down Source # | Since: base-4.12.0.0 | ||||
Defined in Data.Functor.Classes | |||||
| Read1 Down Source # | Since: base-4.12.0.0 | ||||
Defined in Data.Functor.Classes Methods liftReadsPrec :: (Int -> ReadS a) -> ReadS [a] -> Int -> ReadS (Down a) Source # liftReadList :: (Int -> ReadS a) -> ReadS [a] -> ReadS [Down a] Source # liftReadPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec (Down a) Source # liftReadListPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec [Down a] Source # | |||||
| Show1 Down Source # | Since: base-4.12.0.0 | ||||
| Applicative Down Source # | Since: base-4.11.0.0 | ||||
| Functor Down Source # | Since: base-4.11.0.0 | ||||
| Monad Down Source # | Since: base-4.11.0.0 | ||||
| MonadFix Down Source # | Since: base-4.12.0.0 | ||||
| MonadZip Down Source # | Since: ghc-internal-4.12.0.0 | ||||
| Foldable Down Source # | Since: base-4.12.0.0 | ||||
Defined in GHC.Internal.Data.Foldable Methods fold :: Monoid m => Down m -> m Source # foldMap :: Monoid m => (a -> m) -> Down a -> m Source # foldMap' :: Monoid m => (a -> m) -> Down a -> m Source # foldr :: (a -> b -> b) -> b -> Down a -> b Source # foldr' :: (a -> b -> b) -> b -> Down a -> b Source # foldl :: (b -> a -> b) -> b -> Down a -> b Source # foldl' :: (b -> a -> b) -> b -> Down a -> b Source # foldr1 :: (a -> a -> a) -> Down a -> a Source # foldl1 :: (a -> a -> a) -> Down a -> a Source # toList :: Down a -> [a] Source # null :: Down a -> Bool Source # length :: Down a -> Int Source # elem :: Eq a => a -> Down a -> Bool Source # maximum :: Ord a => Down a -> a Source # minimum :: Ord a => Down a -> a Source # | |||||
| Traversable Down Source # | Since: base-4.12.0.0 | ||||
Defined in GHC.Internal.Data.Traversable | |||||
| Generic1 Down Source # | |||||
Defined in GHC.Internal.Generics Associated Types
| |||||
| Monoid a => Monoid (Down a) Source # | Since: base-4.11.0.0 | ||||
| Semigroup a => Semigroup (Down a) Source # | Since: base-4.11.0.0 | ||||
| Bits a => Bits (Down a) Source # | Since: base-4.14.0.0 | ||||
Defined in GHC.Internal.Data.Ord Methods (.&.) :: Down a -> Down a -> Down a Source # (.|.) :: Down a -> Down a -> Down a Source # xor :: Down a -> Down a -> Down a Source # complement :: Down a -> Down a Source # shift :: Down a -> Int -> Down a Source # rotate :: Down a -> Int -> Down a Source # setBit :: Down a -> Int -> Down a Source # clearBit :: Down a -> Int -> Down a Source # complementBit :: Down a -> Int -> Down a Source # testBit :: Down a -> Int -> Bool Source # bitSizeMaybe :: Down a -> Maybe Int Source # bitSize :: Down a -> Int Source # isSigned :: Down a -> Bool Source # shiftL :: Down a -> Int -> Down a Source # unsafeShiftL :: Down a -> Int -> Down a Source # shiftR :: Down a -> Int -> Down a Source # unsafeShiftR :: Down a -> Int -> Down a Source # rotateL :: Down a -> Int -> Down a Source # | |||||
| FiniteBits a => FiniteBits (Down a) Source # | Since: base-4.14.0.0 | ||||
Defined in GHC.Internal.Data.Ord Methods finiteBitSize :: Down a -> Int Source # countLeadingZeros :: Down a -> Int Source # countTrailingZeros :: Down a -> Int Source # | |||||
| Data a => Data (Down a) Source # | Since: base-4.12.0.0 | ||||
Defined in GHC.Internal.Data.Data Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Down a -> c (Down a) Source # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Down a) Source # toConstr :: Down a -> Constr Source # dataTypeOf :: Down a -> DataType Source # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Down a)) Source # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Down a)) Source # gmapT :: (forall b. Data b => b -> b) -> Down a -> Down a Source # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Down a -> r Source # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Down a -> r Source # gmapQ :: (forall d. Data d => d -> u) -> Down a -> [u] Source # gmapQi :: Int -> (forall d. Data d => d -> u) -> Down a -> u Source # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Down a -> m (Down a) Source # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Down a -> m (Down a) Source # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Down a -> m (Down a) Source # | |||||
| Bounded a => Bounded (Down a) Source # | Swaps Since: base-4.14.0.0 | ||||
| (Enum a, Bounded a, Eq a) => Enum (Down a) Source # | Swaps Since: base-4.18.0.0 | ||||
Defined in GHC.Internal.Data.Ord Methods succ :: Down a -> Down a Source # pred :: Down a -> Down a Source # toEnum :: Int -> Down a Source # fromEnum :: Down a -> Int Source # enumFrom :: Down a -> [Down a] Source # enumFromThen :: Down a -> Down a -> [Down a] Source # enumFromTo :: Down a -> Down a -> [Down a] Source # enumFromThenTo :: Down a -> Down a -> Down a -> [Down a] Source # | |||||
| Floating a => Floating (Down a) Source # | Since: base-4.14.0.0 | ||||
Defined in GHC.Internal.Data.Ord Methods exp :: Down a -> Down a Source # log :: Down a -> Down a Source # sqrt :: Down a -> Down a Source # (**) :: Down a -> Down a -> Down a Source # logBase :: Down a -> Down a -> Down a Source # sin :: Down a -> Down a Source # cos :: Down a -> Down a Source # tan :: Down a -> Down a Source # asin :: Down a -> Down a Source # acos :: Down a -> Down a Source # atan :: Down a -> Down a Source # sinh :: Down a -> Down a Source # cosh :: Down a -> Down a Source # tanh :: Down a -> Down a Source # asinh :: Down a -> Down a Source # acosh :: Down a -> Down a Source # atanh :: Down a -> Down a Source # log1p :: Down a -> Down a Source # expm1 :: Down a -> Down a Source # | |||||
| RealFloat a => RealFloat (Down a) Source # | Since: base-4.14.0.0 | ||||
Defined in GHC.Internal.Data.Ord Methods floatRadix :: Down a -> Integer Source # floatDigits :: Down a -> Int Source # floatRange :: Down a -> (Int, Int) Source # decodeFloat :: Down a -> (Integer, Int) Source # encodeFloat :: Integer -> Int -> Down a Source # exponent :: Down a -> Int Source # significand :: Down a -> Down a Source # scaleFloat :: Int -> Down a -> Down a Source # isNaN :: Down a -> Bool Source # isInfinite :: Down a -> Bool Source # isDenormalized :: Down a -> Bool Source # isNegativeZero :: Down a -> Bool Source # | |||||
| Storable a => Storable (Down a) Source # | Since: base-4.14.0.0 | ||||
Defined in GHC.Internal.Data.Ord Methods sizeOf :: Down a -> Int Source # alignment :: Down a -> Int Source # peekElemOff :: Ptr (Down a) -> Int -> IO (Down a) Source # pokeElemOff :: Ptr (Down a) -> Int -> Down a -> IO () Source # peekByteOff :: Ptr b -> Int -> IO (Down a) Source # pokeByteOff :: Ptr b -> Int -> Down a -> IO () Source # | |||||
| Generic (Down a) Source # | |||||
Defined in GHC.Internal.Generics Associated Types
| |||||
| Ix a => Ix (Down a) Source # | Since: base-4.14.0.0 | ||||
| Num a => Num (Down a) Source # | Since: base-4.11.0.0 | ||||
Defined in GHC.Internal.Data.Ord | |||||
| Read a => Read (Down a) Source # | This instance would be equivalent to the derived instances of the
Since: base-4.7.0.0 | ||||
| Fractional a => Fractional (Down a) Source # | Since: base-4.14.0.0 | ||||
| Real a => Real (Down a) Source # | Since: base-4.14.0.0 | ||||
Defined in GHC.Internal.Data.Ord Methods toRational :: Down a -> Rational Source # | |||||
| RealFrac a => RealFrac (Down a) Source # | Since: base-4.14.0.0 | ||||
| Show a => Show (Down a) Source # | This instance would be equivalent to the derived instances of the
Since: base-4.7.0.0 | ||||
| Eq a => Eq (Down a) Source # | Since: base-4.6.0.0 | ||||
| Ord a => Ord (Down a) Source # | Since: base-4.6.0.0 | ||||
| type Rep1 Down Source # | Since: base-4.12.0.0 | ||||
Defined in GHC.Internal.Generics | |||||
| type Rep (Down a) Source # | Since: base-4.12.0.0 | ||||
Defined in GHC.Internal.Generics | |||||
comparing :: Ord a => (b -> a) -> b -> b -> Ordering Source #
comparing p x y = compare (p x) (p y)
Useful combinator for use in conjunction with the xxxBy family
of functions from Data.List, for example:
... sortBy (comparing fst) ...
clamp :: Ord a => (a, a) -> a -> a Source #
clamp (low, high) a = min high (max a low)
Function for ensuring the value a is within the inclusive bounds given by
low and high. If it is, a is returned unchanged. The result
is otherwise low if a <= low, or high if high <= a.
When clamp is used at Double and Float, it has NaN propagating semantics in
its second argument. That is, clamp (l,h) NaN = NaN, but clamp (NaN, NaN)
x = x.
>>>clamp (0, 10) 22
>>>clamp ('a', 'm') 'x''m'
Since: base-4.16.0.0