0 | module Control.Relation.Trichotomy
2 | import Control.Relation.ReflexiveClosure
15 | data Trichotomy : (lt : a -> a -> Type) -> (a -> a -> Type) where
16 | LT : {0 lt : a -> a -> Type}
18 | -> (0 e : Not (v === w))
19 | -> (0 g : Not (lt w v))
20 | -> Trichotomy lt v w
22 | EQ : {0 lt : a -> a -> Type}
23 | -> (0 l : Not (lt v w))
25 | -> (0 g : Not (lt w v))
26 | -> Trichotomy lt v w
28 | GT : {0 lt : a -> a -> Type}
29 | -> (0 l : Not (lt v w))
30 | -> (0 e : Not (v === w))
32 | -> Trichotomy lt v w
36 | conIndex : Trichotomy lt m n -> Bits8
37 | conIndex (LT _ _ _) = 0
38 | conIndex (EQ _ _ _) = 1
39 | conIndex (GT _ _ _) = 2
41 | public export %inline
42 | Eq (Trichotomy lt m n) where
43 | x == y = conIndex x == conIndex y
46 | Ord (Trichotomy lt m n) where
47 | compare x y = compare (conIndex x) (conIndex y)
50 | Show (Trichotomy lt m n) where
51 | show (LT _ _ _) = "LT"
52 | show (EQ _ _ _) = "EQ"
53 | show (GT _ _ _) = "GT"
56 | toOrdering : Trichotomy lt m n -> Ordering
57 | toOrdering (LT _ _ _) = LT
58 | toOrdering (EQ _ _ _) = EQ
59 | toOrdering (GT _ _ _) = GT
62 | swap : Trichotomy lt m n -> Trichotomy lt n m
63 | swap (LT l e g) = GT g (\x => e $
sym x) l
64 | swap (EQ l e g) = EQ g (sym e) l
65 | swap (GT l e g) = LT g (\x => e $
sym x) l
72 | interface Trichotomous (0 a : Type) (0 rel : a -> a -> Type) | rel where
73 | constructor MkTrichotomous
74 | trichotomy : (m,n : a) -> Trichotomy rel m n
77 | interface Transitive a rel => Trichotomous a rel => Strict a rel where
78 | constructor MkStrict
81 | Trichotomous a rel => Antisymmetric a rel where
82 | antisymmetric q p = case trichotomy x y of
83 | LT _ _ g => void $
g p
84 | EQ f _ _ => void $
f q
85 | GT f _ _ => void $
f q
88 | 0 irreflexive : {x : _} -> Trichotomous a rel => rel x x -> Void
89 | irreflexive lt = case trichotomy x x of
95 | Trichotomous a rel => Antisymmetric a (ReflexiveClosure rel) where
96 | antisymmetric _ Refl = Refl
97 | antisymmetric Refl _ = Refl
98 | antisymmetric (Rel q) (Rel p) = case trichotomy x y of
99 | LT _ _ g => void $
g p
100 | EQ f _ _ => void $
f q
101 | GT f _ _ => void $
f q
109 | 0 Not_LT_and_GT : Trichotomous a lt => lt m n -> Not (lt n m)
110 | Not_LT_and_GT isLT isGT = case trichotomy m n of
117 | 0 Not_LT_and_EQ : Trichotomous a lt => lt m n -> Not (m === n)
118 | Not_LT_and_EQ isLT isEQ = case trichotomy m n of
125 | 0 Not_LT_and_GTE : Trichotomous a lt => lt m n -> Not (ReflexiveClosure lt n m)
126 | Not_LT_and_GTE l (Rel prf) = Not_LT_and_GT l prf
127 | Not_LT_and_GTE l Refl = Not_LT_and_EQ l Refl