1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
{-# LANGUAGE NoImplicitPrelude #-}
module Data.Poset where
-- This also provides Ord, but with a different backing.
import Data.Eq
data Ordering = LT | EQ | GT | NC
infix 4 <, <=, >, >=
infix 4 <==>, </=> -- I presume these are the same...
class Eq a => Poset a where
compare :: a -> a -> Ordering
-- comparable
(<==>) :: a -> a -> Bool
-- not comparable
(</=>) :: a -> a -> Bool
(<) :: a -> a -> Bool
(<=) :: a -> a -> Bool
(>=) :: a -> a -> Bool
(>) :: a -> a -> Bool
compare x y | x < y = LT
| x == y = EQ
| x > y = GT
| otherwise = NC
x </=> y = not (x <==> y)
x <==> y = not (x </=> y)
x < y = x<==>y && not (x >= y)
x > y = x<==>y && not (x <= y)
x >= y = x > y || x == y
x <= y = x < y || x == y
-- Ignore NaNs
class Poset a => Sortable a where
sortBy :: (a -> a -> Ordering) -> [a] -> [a]
isOrdered :: a -> Bool
max :: a -> a -> a
min :: a -> a -> a
-- Alternative Ord implementation that does not include Double
class Sortable a => Ord a
-- sort :: Sortable a => [a] -> [a]
-- comparing :: Poset b => (a -> b) -> a -> a -> Ordering
foreign import bpcall "Prelude:" lessthan_char :: Char -> Char -> Bool
foreign import bpcall "Prelude:" lessthan_int :: Int -> Int -> Bool
foreign import bpcall "Prelude:" lessthan_integer :: Integer -> Integer -> Bool
foreign import bpcall "Prelude:" lessthan_double :: Double -> Double -> Bool
instance Poset Char where
x <==> y = True
(<) = lessthan_char
instance Poset Int where
x <==> y = True
(<) = lessthan_int
instance Poset Integer where
x <==> y = True
(<) = lessthan_integer
instance Poset Double where
x <==> y = True
(<) = lessthan_double
|