0 | module Data.Prim.Bits64
2 | import public Algebra.Ring
3 | import public Control.Order
4 | import public Control.Relation
5 | import public Control.Relation.ReflexiveClosure
6 | import public Control.Relation.Trichotomy
7 | import public Control.WellFounded
8 | import public Data.Maybe0
12 | unsafeRefl : a === b
13 | unsafeRefl = believe_me (Builtin.Refl {x = a})
17 | data (<) : (m,n : Bits64) -> Type where
18 | LT : {0 m,n : Bits64} -> (0 prf : (m < n) === True) -> m < n
26 | unerase : (0 p : m < n) -> m < n
27 | unerase (LT p) = LT p
33 | 0 mkLT : (0 prf : (m < n) === True) -> m < n
40 | 0 runLT : m < n -> (m < n) === True
41 | runLT (LT prf) = prf
46 | strictLT : (0 p : m < n) -> Lazy c -> c
47 | strictLT (LT prf) x = x
55 | 0 (>) : (m,n : Bits64) -> Type
60 | 0 (<=) : (m,n : Bits64) -> Type
61 | (<=) = ReflexiveClosure (<)
67 | 0 ltNotEQ : m < n -> Not (m === n)
68 | ltNotEQ x = strictLT x $
assert_total (idris_crash "IMPOSSIBLE: LT and EQ")
70 | 0 ltNotGT : m < n -> Not (n < m)
71 | ltNotGT x = strictLT x $
assert_total (idris_crash "IMPOSSIBLE: LT and GT")
73 | 0 eqNotLT : m === n -> Not (m < n)
74 | eqNotLT = flip ltNotEQ
76 | public export %inline
77 | lt : (x,y : Bits64) -> Maybe0 (x < y)
78 | lt x y = case prim__lt_Bits64 x y of
80 | _ => Just0 (mkLT unsafeRefl)
82 | public export %inline
83 | lte : (x,y : Bits64) -> Maybe0 (x <= y)
84 | lte x y = case prim__lte_Bits64 x y of
86 | _ => Just0 (if x < y then (Rel $
mkLT unsafeRefl) else (fromEq unsafeRefl))
89 | comp : (m,n : Bits64) -> Trichotomy (<) m n
90 | comp m n = case prim__lt_Bits64 m n of
91 | 0 => case prim__eq_Bits64 m n of
92 | 0 => GT (ltNotGT $
LT unsafeRefl) (ltNotEQ $
LT unsafeRefl) (LT unsafeRefl)
93 | x => EQ (eqNotLT unsafeRefl) (unsafeRefl) (eqNotLT unsafeRefl)
94 | x => LT (LT unsafeRefl) (ltNotEQ $
LT unsafeRefl) (ltNotGT $
LT unsafeRefl)
101 | Transitive Bits64 (<) where
102 | transitive _ _ = LT unsafeRefl
105 | Trichotomous Bits64 (<) where
106 | trichotomy m n = comp m n
120 | MaxBits64 = 0xffff_ffff_ffff_ffff
124 | 0 GTE_MinBits64 : (m : Bits64) -> MinBits64 <= m
125 | GTE_MinBits64 m = case comp MinBits64 m of
127 | EQ f x g => fromEq x
128 | GT f g x => assert_total $
idris_crash "IMPOSSIBLE: Bits64 smaller than 0"
132 | 0 Not_LT_MinBits64 : m < 0 -> Void
133 | Not_LT_MinBits64 p = Not_LT_and_GTE p (GTE_MinBits64 m)
137 | 0 LTE_MaxBits64 : (m : Bits64) -> m <= MaxBits64
138 | LTE_MaxBits64 m = case comp m MaxBits64 of
140 | EQ f x g => fromEq x
141 | GT f g x => assert_total
142 | $
idris_crash "IMPOSSIBLE: Bits64 greater than \{show MaxBits64}"
146 | 0 Not_GT_MaxBits64 : m > MaxBits64 -> Void
147 | Not_GT_MaxBits64 p = Not_LT_and_GTE p (LTE_MaxBits64 m)
152 | accessLT : (m : Bits64) -> Accessible (<) m
153 | accessLT m = Access $
\n,lt => accessLT (assert_smaller m n)
155 | namespace WellFounded
158 | [LT] WellFounded Bits64 (<) where
159 | wellFounded = accessLT
164 | accessGT : (m : Bits64) -> Accessible (>) m
165 | accessGT m = Access $
\n,gt => accessGT (assert_smaller m n)
169 | [GT] WellFounded Bits64 (>) where
170 | wellFounded = accessGT