0 | module Data.Enum
  1 |
  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
  7 |
  8 | %default total
  9 | %language ElabReflection
 10 |
 11 | --------------------------------------------------------------------------------
 12 | -- Primitive Indices
 13 | --------------------------------------------------------------------------------
 14 |
 15 | public export
 16 | record Index (n : Bits32) where
 17 |   constructor I
 18 |   val : Bits32
 19 |   {auto 0 prf : val < n}
 20 |
 21 | %runElab deriveIndexed "Index" [Show,Eq,Ord]
 22 |
 23 | export
 24 | tryIndex : {r : _} -> Bits32 -> Maybe (Index r)
 25 | tryIndex n =
 26 |   case lt n r of
 27 |     Nothing0 => Nothing
 28 |     Just0 v  => Just (I n)
 29 |
 30 | public export
 31 | fromInteger : (n : Integer) -> (0 p : cast n < r) => Index r
 32 | fromInteger n = I (cast n)
 33 |
 34 | public export
 35 | Zero : (0 prf : 0 < n) => Index n
 36 | Zero = I 0
 37 |
 38 | export
 39 | 0 ltProof : (v : Bits32) -> (0 prf : v < n) => lt (cast v) (cast n) === True
 40 | ltProof v = believe_me $ Builtin.Refl {x = True}
 41 |
 42 | export %inline
 43 | toFin : Index n -> Fin (cast n)
 44 | toFin (I v) = bits32ToFin v n
 45 |
 46 | export %inline
 47 | Cast (Index n) Integer where
 48 |   cast (I v) = cast v
 49 |
 50 | export %inline
 51 | Cast (Index n) Bits32 where
 52 |   cast (I v) = v
 53 |
 54 | export
 55 | HDecEq (Index n) where
 56 |   hdecEq (I x) (I y) =
 57 |     case hdecEq x y of
 58 |       Nothing0  => Nothing0
 59 |       Just0 prf => Just0 $ believe_me (Builtin.Refl {x})
 60 |
 61 | --------------------------------------------------------------------------------
 62 | -- Enum Interface
 63 | --------------------------------------------------------------------------------
 64 |
 65 | ||| Verified enumerations.
 66 | |||
 67 | ||| Provides a function for converting a value of the given type to an
 68 | ||| `Index n` of the given size.
 69 | |||
 70 | ||| In addition, proves that `Finite.value` holds every value there is and
 71 | ||| that `toIndex` is injective.
 72 | |||
 73 | ||| With these proofs we can use a value of type `t` as an index into a
 74 | ||| (potentially dependent) array.
 75 | public export
 76 | interface Finite t => Enum (0 t : Type) (0 n : Bits32) | t where
 77 |   constructor MkEnum
 78 |   toIndex : t -> Index n
 79 |
 80 |   0 toIndexInjective : (x,y : t) -> toIndex x === toIndex y -> x === y
 81 |
 82 |   0 valuesComplete : (x : t) -> Elem x Finite.values
 83 |
 84 | export %inline
 85 | enumToFin : Enum t n => t -> Fin (cast n)
 86 | enumToFin = toFin . toIndex
 87 |
 88 | export %inline
 89 | enumToBits32 : Enum t n => t -> Bits32
 90 | enumToBits32 = cast . toIndex
 91 |
 92 | export %inline
 93 | enumToInteger : Enum t n => t -> Integer
 94 | enumToInteger = cast . toIndex
 95 |
 96 | export
 97 | Enum Bool 2 where
 98 |   toIndex False = 0
 99 |   toIndex True  = 1
100 |
101 |   toIndexInjective False False Refl = Refl
102 |   toIndexInjective True True Refl = Refl
103 |
104 |   valuesComplete False = Here
105 |   valuesComplete True = There Here
106 |
107 | export
108 | Enum Ordering 3 where
109 |   toIndex LT = 0
110 |   toIndex EQ = 1
111 |   toIndex GT = 2
112 |
113 |   toIndexInjective LT LT Refl = Refl
114 |   toIndexInjective EQ EQ Refl = Refl
115 |   toIndexInjective GT GT Refl = Refl
116 |
117 |   valuesComplete LT = Here
118 |   valuesComplete EQ = There Here
119 |   valuesComplete GT = There $ There Here
120 |
121 | export
122 | {n : _} -> Finite (Index n) where
123 |   values = mapMaybe tryIndex [0..n]
124 |
125 | ||| Proof that every `Index n` is in the list of values.
126 | export
127 | vcomplete : {n : _} -> (i : Index n) -> Elem i Finite.values
128 | vcomplete i = go values
129 |   where
130 |     go : (is : List (Index n)) -> Elem i is
131 |     go [] = assert_total $ idris_crash "vcomplete end of list"
132 |     go (x::xs) =
133 |       case hdecEq i x of
134 |         Nothing0  => There $ go xs
135 |         Just0 prf => rewrite prf in Here
136 |
137 | export %inline
138 | {n : _} -> Enum (Index n) n where
139 |   toIndex x = x
140 |   toIndexInjective (I x) (I x) Refl = Refl
141 |   valuesComplete x = vcomplete x
142 |
143 |
144 | --------------------------------------------------------------------------------
145 | -- Utilities
146 | --------------------------------------------------------------------------------
147 |
148 | public export
149 | inList : HDecEq i => (v : i) -> List i -> Bool
150 | inList v []        = False
151 | inList v (x :: xs) =
152 |   case hdecEq v x of
153 |     Nothing0 => inList v xs
154 |     Just0 _  => True
155 |
156 | export
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
162 |
163 | --------------------------------------------------------------------------------
164 | -- TEnum Interface
165 | --------------------------------------------------------------------------------
166 |
167 | ||| Verified tagged enumerations.
168 | |||
169 | ||| Provides a function for converting a value of the given type to an
170 | ||| `Index n` of the given size plus a proof that this function is injective.
171 | |||
172 | ||| With this we can use a value of type `t a` as an index into a
173 | ||| dependent array.
174 | public export
175 | interface TEnum (0 t : k -> Type) (0 n : Bits32) | t where
176 |   constructor MkTEnum
177 |   toIndexT : t a -> Index n
178 |
179 |   0 toIndexInjectiveT : (x : t a) -> (y : t b) -> toIndexT x === toIndexT y -> x ~=~ y
180 |
181 | export %inline
182 | tenumToFin : TEnum t n => t a -> Fin (cast n)
183 | tenumToFin = toFin . toIndexT
184 |
185 | export %inline
186 | tenumToBits32 : TEnum t n => t a -> Bits32
187 | tenumToBits32 = cast . toIndexT
188 |
189 | export %inline
190 | tenumToInteger : TEnum t n => t a -> Integer
191 | tenumToInteger = cast . toIndexT
192 |