2 | import Derive.Prelude
3 | import public Data.Finite
4 | import public Data.List.Elem
5 | import public Data.Prim.Bits32
6 | import public Decidable.HDecEq
9 | %language ElabReflection
16 | record Index (n : Bits32) where
19 | {auto 0 prf : val < n}
21 | %runElab deriveIndexed "Index" [Show,Eq,Ord]
24 | tryIndex : {r : _} -> Bits32 -> Maybe (Index r)
28 | Just0 v => Just (I n)
31 | fromInteger : (n : Integer) -> (0 p : cast n < r) => Index r
32 | fromInteger n = I (cast n)
35 | Zero : (0 prf : 0 < n) => Index n
39 | 0 ltProof : (v : Bits32) -> (0 prf : v < n) => lt (cast v) (cast n) === True
40 | ltProof v = believe_me $
Builtin.Refl {x = True}
43 | toFin : Index n -> Fin (cast n)
44 | toFin (I v) = bits32ToFin v n
47 | Cast (Index n) Integer where
51 | Cast (Index n) Bits32 where
69 | interface Finite t => Enum (0 t : Type) (0 n : Bits32) | t where
71 | toIndex : t -> Index n
73 | 0 toIndexInjective : (x,y : t) -> toIndex x === toIndex y -> x === y
75 | 0 valuesComplete : (x : t) -> Elem x Finite.values
78 | enumToFin : Enum t n => t -> Fin (cast n)
79 | enumToFin = toFin . toIndex
82 | enumToBits32 : Enum t n => t -> Bits32
83 | enumToBits32 = cast . toIndex
86 | enumToInteger : Enum t n => t -> Integer
87 | enumToInteger = cast . toIndex
94 | toIndexInjective False False Refl = Refl
95 | toIndexInjective True True Refl = Refl
97 | valuesComplete False = Here
98 | valuesComplete True = There Here
101 | Enum Ordering 3 where
106 | toIndexInjective LT LT Refl = Refl
107 | toIndexInjective EQ EQ Refl = Refl
108 | toIndexInjective GT GT Refl = Refl
110 | valuesComplete LT = Here
111 | valuesComplete EQ = There Here
112 | valuesComplete GT = There $
There Here
119 | inList : HDecEq i => (v : i) -> List i -> Bool
120 | inList v [] = False
121 | inList v (x :: xs) =
123 | Nothing0 => inList v xs
127 | 0 inListImpliesElem : HDecEq i => (v : i) -> inList v is === True -> Elem v is
128 | inListImpliesElem v prf {is = []} = absurd prf
129 | inListImpliesElem v prf {is = x::xs} with (hdecEq v x)
130 | _ | Nothing0 = There $
inListImpliesElem v prf {is = xs}
131 | _ | Just0 p = rewrite p in Here