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
55 | HDecEq (Index n) where
56 | hdecEq (I x) (I y) =
58 | Nothing0 => Nothing0
59 | Just0 prf => Just0 $
believe_me (Builtin.Refl {x})
76 | interface Finite t => Enum (0 t : Type) (0 n : Bits32) | t where
78 | toIndex : t -> Index n
80 | 0 toIndexInjective : (x,y : t) -> toIndex x === toIndex y -> x === y
82 | 0 valuesComplete : (x : t) -> Elem x Finite.values
85 | enumToFin : Enum t n => t -> Fin (cast n)
86 | enumToFin = toFin . toIndex
89 | enumToBits32 : Enum t n => t -> Bits32
90 | enumToBits32 = cast . toIndex
93 | enumToInteger : Enum t n => t -> Integer
94 | enumToInteger = cast . toIndex
101 | toIndexInjective False False Refl = Refl
102 | toIndexInjective True True Refl = Refl
104 | valuesComplete False = Here
105 | valuesComplete True = There Here
108 | Enum Ordering 3 where
113 | toIndexInjective LT LT Refl = Refl
114 | toIndexInjective EQ EQ Refl = Refl
115 | toIndexInjective GT GT Refl = Refl
117 | valuesComplete LT = Here
118 | valuesComplete EQ = There Here
119 | valuesComplete GT = There $
There Here
122 | {n : _} -> Finite (Index n) where
123 | values = mapMaybe tryIndex [0..n]
127 | vcomplete : {n : _} -> (i : Index n) -> Elem i Finite.values
128 | vcomplete i = go values
130 | go : (is : List (Index n)) -> Elem i is
131 | go [] = assert_total $
idris_crash "vcomplete end of list"
134 | Nothing0 => There $
go xs
135 | Just0 prf => rewrite prf in Here
138 | {n : _} -> Enum (Index n) n where
140 | toIndexInjective (I x) (I x) Refl = Refl
141 | valuesComplete x = vcomplete x
149 | inList : HDecEq i => (v : i) -> List i -> Bool
150 | inList v [] = False
151 | inList v (x :: xs) =
153 | Nothing0 => inList v xs
157 | 0 inListImpliesElem : HDecEq i => (v : i) -> inList v is === True -> Elem v is
158 | inListImpliesElem v prf {is = []} = absurd prf
159 | inListImpliesElem v prf {is = x::xs} with (hdecEq v x)
160 | _ | Nothing0 = There $
inListImpliesElem v prf {is = xs}
161 | _ | Just0 p = rewrite p in Here
175 | interface TEnum (0 t : k -> Type) (0 n : Bits32) | t where
176 | constructor MkTEnum
177 | toIndexT : t a -> Index n
179 | 0 toIndexInjectiveT : (x : t a) -> (y : t b) -> toIndexT x === toIndexT y -> x ~=~ y
182 | tenumToFin : TEnum t n => t a -> Fin (cast n)
183 | tenumToFin = toFin . toIndexT
186 | tenumToBits32 : TEnum t n => t a -> Bits32
187 | tenumToBits32 = cast . toIndexT
190 | tenumToInteger : TEnum t n => t a -> Integer
191 | tenumToInteger = cast . toIndexT