lude

Haskell prelude alternative
git clone git://git.rr3.xyz/lude | git clone gits://git.rr3.xyz/lude
Log | Files | Refs | README | LICENSE

Partial.hs (2128B)


      1 module Lude.Order.Partial (
      2     Ordering (..),
      3     (==),
      4     (/=),
      5     (≠),
      6     --
      7     PartiallyOrdered (..),
      8     (<),
      9     (<=),
     10     (≤),
     11     (>=),
     12     (≥),
     13     (>),
     14 ) where
     15 
     16 import Data.Coerce
     17 import Numeric.Natural qualified as Base
     18 import Prelude qualified as Base
     19 
     20 import Lude.Bool
     21 import Lude.Equivalence
     22 import Lude.Function
     23 import Lude.Haskell
     24 import Lude.Maybe
     25 import Lude.Order.Ordering
     26 
     27 --------------------------------------------------------------------------------
     28 
     29 type PartiallyOrdered :: forall {r :: Repr}. Type# r -> Constraint
     30 class (Equivalential a) => PartiallyOrdered (a :: Type# r) where
     31     {-# MINIMAL pcmp #-}
     32 
     33     pcmp :: a -> a -> Maybe Ordering
     34 
     35     lt :: a -> a -> Bool
     36     default lt :: (Functional# r) => a -> a -> Bool
     37     lt = lam2# \x y -> case app2# pcmp x y of Just LT -> True; _ -> False
     38 
     39     le :: a -> a -> Bool
     40     default le :: (Functional# r) => a -> a -> Bool
     41     le = lam2# \x y -> case app2# pcmp x y of Just GT -> False; _ -> True
     42 
     43     ge :: a -> a -> Bool
     44     default ge :: (Functional# r) => a -> a -> Bool
     45     ge = lam2# \x y -> case app2# pcmp x y of Just LT -> False; _ -> True
     46 
     47     gt :: a -> a -> Bool
     48     default gt :: (Functional# r) => a -> a -> Bool
     49     gt = lam2# \x y -> case app2# pcmp x y of Just GT -> True; _ -> False
     50 
     51 (<), (<=), (≤), (>=), (≥), (>) :: forall {r :: Repr} (a :: Type# r). (PartiallyOrdered a) => a -> a -> Bool
     52 (<) = lt
     53 (<=) = le
     54 (≤) = le
     55 (>=) = ge
     56 (≥) = ge
     57 (>) = gt
     58 infix 4 <, <=, ≤, >=, ≥, >
     59 
     60 --------------------------------------------------------------------------------
     61 -- Generics
     62 
     63 -- TODO
     64 
     65 --------------------------------------------------------------------------------
     66 -- Interop
     67 
     68 instance (Base.Ord a) => PartiallyOrdered (BaseInterop a) where
     69     pcmp = Just .* coerce Base.compare
     70     lt = coerce (Base.<)
     71     le = coerce (Base.<=)
     72     ge = coerce (Base.>=)
     73     gt = coerce (Base.>)
     74 
     75 deriving via BaseInterop Base.Natural instance PartiallyOrdered Base.Natural
     76 deriving via BaseInterop Base.Integer instance PartiallyOrdered Base.Integer
     77 deriving via BaseInterop Base.Rational instance PartiallyOrdered Base.Rational