Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Synopsis
Constraint kinds
type Order a = (Eq a, Preorder a) Source #
An order on a.
Note: ideally this would be a subclass of Preorder.
We instead use a constraint kind in order to retain compatibility with the downstream users of Eq.
type Total a = (Ord a, Preorder a) Source #
A total order on a.
Note: ideally this would be a subclass of Order, without instances for Float, Double, Rational, etc.
We instead use a constraint kind in order to retain compatibility with the downstream users of Ord.
Preorders
class Preorder a where Source #
A preorder on a.
A preorder relation <~
must satisfy the following two axioms:
\( \forall x: x \leq x \) (reflexivity)
\( \forall a, b, c: ((a \leq b) \wedge (b \leq c)) \Rightarrow (a \leq c) \) (transitivity)
Given a preorder on a one may define an equivalence relation ~~
such that
a ~~ b if and only if a <~ b and b <~ a.
If no partion induced by ~~
contains more than a single element, then a
is a partial order and we may define an Eq
instance such that the
following holds:
x==
y = x~~
y x<=
y = x<
y||
x~~
y
Minimal complete definition: either pcompare
or <~
. Using pcompare
can
be more efficient for complex types.
(<~) :: a -> a -> Bool infix 4 Source #
A non-strict preorder order relation on a.
Is x less than or equal to y?
<~
is reflexive, anti-symmetric, and transitive.
x <~ y = x < y || x ~~ y x <~ y = maybe False (<~ EQ) (pcompare x y)
for all x, y in a.
(>~) :: a -> a -> Bool infix 4 Source #
A converse non-strict preorder relation on a.
Is x greater than or equal to y?
>~
is reflexive, anti-symmetric, and transitive.
x >~ y = x > y || x ~~ y x >~ y = maybe False (>~ EQ) (pcompare x y)
for all x, y in a.
(?~) :: a -> a -> Bool infix 4 Source #
An equivalence relation on a.
Are x and y comparable?
?~
is reflexive, symmetric, and transitive.
If a implements Ord
then we should have x ?~ y = True
.
(~~) :: a -> a -> Bool infix 4 Source #
An equivalence relation on a.
Are x and y equivalent?
~~
is reflexive, symmetric, and transitive.
x ~~ y = x <~ y && y <~ x x ~~ y = maybe False (~~ EQ) (pcompare x y)
Use this as a lawful substitute for ==
when comparing
floats, doubles, or rationals.
(/~) :: a -> a -> Bool infix 4 Source #
Negation of ~~
.
Are x and y not equivalent?
plt :: a -> a -> Bool infix 4 Source #
A strict preorder relation on a.
Is x less than y?
plt
is irreflexive, asymmetric, and transitive.
x `plt` y = x <~ y && not (y <~ x) x `plt` y = maybe False (< EQ) (pcompare x y)
When <~
is antisymmetric then a is a partial
order and we have:
x `plt` y = x <~ y && x /~ y
for all x, y in a.
pgt :: a -> a -> Bool infix 4 Source #
A converse strict preorder relation on a.
Is x greater than y?
pgt
is irreflexive, asymmetric, and transitive.
x `pgt` y = x >~ y && not (y >~ x) x `pgt` y = maybe False (> EQ) (pcompare x y)
When <~
is antisymmetric then a is a partial
order and we have:
x `pgt` y = x >~ y && x /~ y
for all x, y in a.
similar :: a -> a -> Bool Source #
A similarity relation on a.
Are x and y either equivalent or incomparable?
similar
is reflexive and symmetric, but not necessarily transitive.
Note this is only equivalent to ==
in a total order:
similar (0/0 :: Float) 5 = True
If a implements Ord
then we should have (
.~~
) = similar
= (==
)
pmax :: a -> a -> Maybe a infix 4 Source #
A partial version of max
.
Returns the left-hand argument in the case of equality.
pmin :: a -> a -> Maybe a infix 4 Source #
A partial version of min
.
Returns the left-hand argument in the case of equality.
pcompare :: a -> a -> Maybe Ordering infix 4 Source #
A partial version of compare
.
x < y = maybe False (< EQ) $ pcompare x y x > y = maybe False (> EQ) $ pcompare x y x <~ y = maybe False (<~ EQ) $ pcompare x y x >~ y = maybe False (>~ EQ) $ pcompare x y x ~~ y = maybe False (~~ EQ) $ pcompare x y x ?~ y = maybe False (const True) $ pcompare x y similar x y = maybe True (~~ EQ) $ pcompare x y
If a implements Ord
then we should have
.pcompare
x y = Just
$
compare
x y
Instances
pcomparing :: Preorder a => (b -> a) -> b -> b -> Maybe Ordering Source #
A partial version of comparing
.
pcomparing p x y = pcompare (p x) (p y)
The partial application pcomparing f induces a lawful preorder for any total function f.
DerivingVia
Instances
Functor Base Source # | |
Applicative Base Source # | |
Eq a => Eq (Base a) Source # | |
Ord a => Ord (Base a) Source # | |
Show a => Show (Base a) Source # | |
Ord a => Preorder (Base a) Source # | |
Defined in Data.Order (<~) :: Base a -> Base a -> Bool Source # (>~) :: Base a -> Base a -> Bool Source # (?~) :: Base a -> Base a -> Bool Source # (~~) :: Base a -> Base a -> Bool Source # (/~) :: Base a -> Base a -> Bool Source # plt :: Base a -> Base a -> Bool Source # pgt :: Base a -> Base a -> Bool Source # similar :: Base a -> Base a -> Bool Source # pmax :: Base a -> Base a -> Maybe (Base a) Source # |
Instances
Functor N5 Source # | |
Applicative N5 Source # | |
Eq a => Eq (N5 a) Source # | |
Show a => Show (N5 a) Source # | |
(Ord a, Fractional a) => Preorder (N5 a) Source # | |
Defined in Data.Order (<~) :: N5 a -> N5 a -> Bool Source # (>~) :: N5 a -> N5 a -> Bool Source # (?~) :: N5 a -> N5 a -> Bool Source # (~~) :: N5 a -> N5 a -> Bool Source # (/~) :: N5 a -> N5 a -> Bool Source # plt :: N5 a -> N5 a -> Bool Source # pgt :: N5 a -> N5 a -> Bool Source # similar :: N5 a -> N5 a -> Bool Source # pmax :: N5 a -> N5 a -> Maybe (N5 a) Source # |
Re-exports
Instances
The Down
type allows you to reverse sort order conveniently. A value of type
contains a value of type Down
aa
(represented as
).
If Down
aa
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: Ord
then sortWith by
Down
x
Since: base-4.6.0.0
Instances
Monad Down | Since: base-4.11.0.0 |
Functor Down | Since: base-4.11.0.0 |
Applicative Down | Since: base-4.11.0.0 |
Foldable Down | Since: base-4.12.0.0 |
Defined in Data.Foldable fold :: Monoid m => Down m -> m # foldMap :: Monoid m => (a -> m) -> Down a -> m # foldMap' :: Monoid m => (a -> m) -> Down a -> m # foldr :: (a -> b -> b) -> b -> Down a -> b # foldr' :: (a -> b -> b) -> b -> Down a -> b # foldl :: (b -> a -> b) -> b -> Down a -> b # foldl' :: (b -> a -> b) -> b -> Down a -> b # foldr1 :: (a -> a -> a) -> Down a -> a # foldl1 :: (a -> a -> a) -> Down a -> a # elem :: Eq a => a -> Down a -> Bool # maximum :: Ord a => Down a -> a # | |
Traversable Down | Since: base-4.12.0.0 |
Eq1 Down | Since: base-4.12.0.0 |
Ord1 Down | Since: base-4.12.0.0 |
Defined in Data.Functor.Classes | |
Read1 Down | Since: base-4.12.0.0 |
Defined in Data.Functor.Classes | |
Show1 Down | Since: base-4.12.0.0 |
Bounded a => Bounded (Down a) | Since: base-4.14.0.0 |
Enum a => Enum (Down a) | Since: base-4.14.0.0 |
Defined in Data.Ord | |
Eq a => Eq (Down a) | Since: base-4.6.0.0 |
Floating a => Floating (Down a) | Since: base-4.14.0.0 |
Fractional a => Fractional (Down a) | Since: base-4.14.0.0 |
Integral a => Integral (Down a) | Since: base-4.14.0.0 |
Num a => Num (Down a) | Since: base-4.11.0.0 |
Ord a => Ord (Down a) | Since: base-4.6.0.0 |
Read a => Read (Down a) | This instance would be equivalent to the derived instances of the
Since: base-4.7.0.0 |
Real a => Real (Down a) | Since: base-4.14.0.0 |
Defined in Data.Ord toRational :: Down a -> Rational # | |
RealFloat a => RealFloat (Down a) | Since: base-4.14.0.0 |
Defined in Data.Ord floatRadix :: Down a -> Integer # floatDigits :: Down a -> Int # floatRange :: Down a -> (Int, Int) # decodeFloat :: Down a -> (Integer, Int) # encodeFloat :: Integer -> Int -> Down a # significand :: Down a -> Down a # scaleFloat :: Int -> Down a -> Down a # isInfinite :: Down a -> Bool # isDenormalized :: Down a -> Bool # isNegativeZero :: Down a -> Bool # | |
RealFrac a => RealFrac (Down a) | Since: base-4.14.0.0 |
Show a => Show (Down a) | This instance would be equivalent to the derived instances of the
Since: base-4.7.0.0 |
Ix a => Ix (Down a) | Since: base-4.14.0.0 |
Generic (Down a) | Since: base-4.12.0.0 |
Semigroup a => Semigroup (Down a) | Since: base-4.11.0.0 |
Monoid a => Monoid (Down a) | Since: base-4.11.0.0 |
Storable a => Storable (Down a) | Since: base-4.14.0.0 |
Bits a => Bits (Down a) | Since: base-4.14.0.0 |
Defined in Data.Ord (.&.) :: Down a -> Down a -> Down a # (.|.) :: Down a -> Down a -> Down a # xor :: Down a -> Down a -> Down a # complement :: Down a -> Down a # shift :: Down a -> Int -> Down a # rotate :: Down a -> Int -> Down a # setBit :: Down a -> Int -> Down a # clearBit :: Down a -> Int -> Down a # complementBit :: Down a -> Int -> Down a # testBit :: Down a -> Int -> Bool # bitSizeMaybe :: Down a -> Maybe Int # shiftL :: Down a -> Int -> Down a # unsafeShiftL :: Down a -> Int -> Down a # shiftR :: Down a -> Int -> Down a # unsafeShiftR :: Down a -> Int -> Down a # rotateL :: Down a -> Int -> Down a # | |
FiniteBits a => FiniteBits (Down a) | Since: base-4.14.0.0 |
Defined in Data.Ord finiteBitSize :: Down a -> Int # countLeadingZeros :: Down a -> Int # countTrailingZeros :: Down a -> Int # | |
Preorder a => Preorder (Down a) Source # | |
Defined in Data.Order (<~) :: Down a -> Down a -> Bool Source # (>~) :: Down a -> Down a -> Bool Source # (?~) :: Down a -> Down a -> Bool Source # (~~) :: Down a -> Down a -> Bool Source # (/~) :: Down a -> Down a -> Bool Source # plt :: Down a -> Down a -> Bool Source # pgt :: Down a -> Down a -> Bool Source # similar :: Down a -> Down a -> Bool Source # pmax :: Down a -> Down a -> Maybe (Down a) Source # | |
Generic1 Down | Since: base-4.12.0.0 |
type Rep (Down a) | |
Defined in GHC.Generics | |
type Rep1 Down | |
Defined in GHC.Generics |