Permalink
Browse files

Added Generic.Data.Ord module.

  • Loading branch information...
1 parent 659f265 commit 01fac77a4eda466011987fa1340c953480871424 @tomlokhorst committed Jan 21, 2010
Showing with 43 additions and 0 deletions.
  1. +3 −0 src/Generic/Data/Bool.hs
  2. +2 −0 src/Generic/Data/Eq.hs
  3. +38 −0 src/Generic/Data/Ord.hs
View
@@ -4,6 +4,9 @@ module Generic.Data.Bool where
import Prelude ()
+infixr 3 &&
+infixr 2 ||
+
data Bool
class BoolC j where
false :: j Bool
View
@@ -5,6 +5,8 @@ module Generic.Data.Eq where
import Prelude ()
import Generic.Data.Bool
+infix 4 ==, /=
+
class Eq j a where
(==) :: j a -> j a -> j Bool
(/=) :: j a -> j a -> j Bool
View
@@ -0,0 +1,38 @@
+{-# LANGUAGE EmptyDataDecls #-}
+
+module Generic.Data.Ord where
+
+import Prelude ()
+import Generic.Data.Bool
+import Generic.Data.Eq
+
+infix 4 <, <=, >=, >
+
+class (Eq j a) => Ord j a where
+ compare :: (BoolC j, OrderingC j) => j a -> j a -> j Ordering
+ (<), (<=), (>), (>=) :: (BoolC j, OrderingC j) => j a -> j a -> j Bool
+ max, min :: (BoolC j, OrderingC j) => j a -> j a -> j a
+
+ compare x y = if' (x == y)
+ eq
+ (if' (x <= y) lt gt)
+
+ x < y = ordering true false false (compare x y)
+ x <= y = ordering true true false (compare x y)
+ x > y = ordering false false true (compare x y)
+ x >= y = ordering false true true (compare x y)
+
+ max x y = if' (x <= y) y x
+ min x y = if' (x <= y) x y
+
+data Ordering
+class OrderingC j where
+ lt :: j Ordering
+ eq :: j Ordering
+ gt :: j Ordering
+ ordering :: j a -> j a -> j a -> j Ordering -> j a
+
+comparing :: (Ord j a, BoolC j, OrderingC j)
+ => (b -> j a) -> b -> b -> j Ordering
+comparing p x y = compare (p x) (p y)
+

0 comments on commit 01fac77

Please sign in to comment.