0 | module Data.Prim.Bits16
3 | import public Algebra.Ring
4 | import public Control.Order
5 | import public Control.Relation
6 | import public Control.Relation.ReflexiveClosure
7 | import public Control.Relation.Trichotomy
8 | import public Control.WellFounded
9 | import public Data.Maybe0
13 | unsafeRefl : a === b
14 | unsafeRefl = believe_me (Builtin.Refl {x = a})
18 | data (<) : (m,n : Bits16) -> Type where
19 | LT : {0 m,n : Bits16} -> (0 prf : (m < n) === True) -> m < n
27 | unerase : (0 p : m < n) -> m < n
28 | unerase (LT p) = LT p
34 | 0 mkLT : (0 prf : (m < n) === True) -> m < n
41 | 0 runLT : m < n -> (m < n) === True
42 | runLT (LT prf) = prf
47 | strictLT : (0 p : m < n) -> Lazy c -> c
48 | strictLT (LT prf) x = x
56 | 0 (>) : (m,n : Bits16) -> Type
61 | 0 (<=) : (m,n : Bits16) -> Type
62 | (<=) = ReflexiveClosure (<)
68 | 0 ltNotEQ : m < n -> Not (m === n)
69 | ltNotEQ x = strictLT x $
assert_total (idris_crash "IMPOSSIBLE: LT and EQ")
71 | 0 ltNotGT : m < n -> Not (n < m)
72 | ltNotGT x = strictLT x $
assert_total (idris_crash "IMPOSSIBLE: LT and GT")
74 | 0 eqNotLT : m === n -> Not (m < n)
75 | eqNotLT = flip ltNotEQ
77 | public export %inline
78 | lt : (x,y : Bits16) -> Maybe0 (x < y)
79 | lt x y = case prim__lt_Bits16 x y of
81 | _ => Just0 (mkLT unsafeRefl)
83 | public export %inline
84 | lte : (x,y : Bits16) -> Maybe0 (x <= y)
85 | lte x y = case prim__lte_Bits16 x y of
87 | _ => Just0 (if x < y then (Rel $
mkLT unsafeRefl) else (fromEq unsafeRefl))
90 | comp : (m,n : Bits16) -> Trichotomy (<) m n
91 | comp m n = case prim__lt_Bits16 m n of
92 | 0 => case prim__eq_Bits16 m n of
93 | 0 => GT (ltNotGT $
LT unsafeRefl) (ltNotEQ $
LT unsafeRefl) (LT unsafeRefl)
94 | x => EQ (eqNotLT unsafeRefl) (unsafeRefl) (eqNotLT unsafeRefl)
95 | x => LT (LT unsafeRefl) (ltNotEQ $
LT unsafeRefl) (ltNotGT $
LT unsafeRefl)
102 | Transitive Bits16 (<) where
103 | transitive _ _ = LT unsafeRefl
106 | Trichotomous Bits16 (<) where
107 | trichotomy m n = comp m n
125 | 0 GTE_MinBits16 : (m : Bits16) -> MinBits16 <= m
126 | GTE_MinBits16 m = case comp MinBits16 m of
128 | EQ f x g => fromEq x
129 | GT f g x => assert_total $
idris_crash "IMPOSSIBLE: Bits16 smaller than 0"
133 | 0 Not_LT_MinBits16 : m < 0 -> Void
134 | Not_LT_MinBits16 p = Not_LT_and_GTE p (GTE_MinBits16 m)
138 | 0 LTE_MaxBits16 : (m : Bits16) -> m <= MaxBits16
139 | LTE_MaxBits16 m = case comp m MaxBits16 of
141 | EQ f x g => fromEq x
142 | GT f g x => assert_total
143 | $
idris_crash "IMPOSSIBLE: Bits16 greater than \{show MaxBits16}"
147 | 0 Not_GT_MaxBits16 : m > MaxBits16 -> Void
148 | Not_GT_MaxBits16 p = Not_LT_and_GTE p (LTE_MaxBits16 m)
153 | accessLT : (m : Bits16) -> Accessible (<) m
154 | accessLT m = Access $
\n,lt => accessLT (assert_smaller m n)
156 | namespace WellFounded
159 | [LT] WellFounded Bits16 (<) where
160 | wellFounded = accessLT
165 | accessGT : (m : Bits16) -> Accessible (>) m
166 | accessGT m = Access $
\n,gt => accessGT (assert_smaller m n)
170 | [GT] WellFounded Bits16 (>) where
171 | wellFounded = accessGT
178 | 0 ltImpliesNatLT : (x,y : Bits16) -> x < y => Nat.lt (cast x) (cast y) === True
179 | ltImpliesNatLT x y = unsafeRefl
182 | 0 lteImpliesNatLT : (x,y : Bits16) -> x <= y => Nat.lt (cast x) (S $
cast y) === True
183 | lteImpliesNatLT x y = unsafeRefl
186 | 0 gtImpliesNatGT : (x,y : Bits16) -> x > y => Nat.gt (cast x) (cast y) === True
187 | gtImpliesNatGT x y = ltImpliesNatLT y x
190 | bits16ToFin : (x : Bits16) -> (0 y : Bits16) -> (0 lt : x < y) => Fin (cast y)
191 | bits16ToFin x y = natToFinLT (cast x) @{ltReflectsLT _ _ $
ltImpliesNatLT x y}
194 | bits16ToFinLTE : (x : Bits16) -> (0 y : Bits16) -> (0 lte : x <= y) => Fin (S $
cast y)
195 | bits16ToFinLTE x y = natToFinLT (cast x) @{ltReflectsLT _ _ $
lteImpliesNatLT x y}
198 | Cast Bits16 (Fin 0x10000) where
199 | cast x = bits16ToFinLTE x 0xffff @{LTE_MaxBits16 x}