0 | module Derive.Enum
  1 |
  2 | import public Data.Enum
  3 | import public Derive.Finite
  4 | import Language.Reflection.Util
  5 |
  6 | %default total
  7 |
  8 | --------------------------------------------------------------------------------
  9 | --          Claims
 10 | --------------------------------------------------------------------------------
 11 |
 12 | export
 13 | conIndexLtName : Named a => a -> Name
 14 | conIndexLtName v = funName v "conIndexLt"
 15 |
 16 | export
 17 | toIndexName : Named a => a -> Name
 18 | toIndexName v = funName v "toIndex"
 19 |
 20 | export
 21 | toIndexInjectiveName : Named a => a -> Name
 22 | toIndexInjectiveName v = funName v "toIndexInjective"
 23 |
 24 | export
 25 | valuesCompleteName : Named a => a -> Name
 26 | valuesCompleteName v = funName v "valuesComplete"
 27 |
 28 | ||| Top-level function declaration of a proof that all constructor indexes
 29 | ||| are less than the total number of constructors.
 30 | export
 31 | conIndexLtClaim : Visibility -> (cifun, fun : Name) -> (p : TypeInfo) -> Decl
 32 | conIndexLtClaim vis cifun fun p =
 33 |   let civ := var cifun
 34 |       tot := primVal (B32 $ cast $ length p.cons)
 35 |       arg := MkArg MW ExplicitArg (Just "v") p.applied
 36 |       tpe := piAll `(cast {to = Bits32} (~(civ) v) < ~(tot)) (p.implicits ++ [arg])
 37 |    in claim M0 vis [] fun tpe
 38 |
 39 | ||| Top-level function declaration of a proof that all the `conIndexXY`
 40 | ||| function is injective.
 41 | export
 42 | toIndexInjectiveClaim : Visibility -> (tifun, fun : Name) -> (p : TypeInfo) -> Decl
 43 | toIndexInjectiveClaim vis tifun fun p =
 44 |   let civ := var tifun
 45 |       a1  := MkArg MW ExplicitArg (Just "x") p.applied
 46 |       a2  := MkArg MW ExplicitArg (Just "y") p.applied
 47 |       prf := MkArg MW ExplicitArg (Just "prf") `(~(civ) x === ~(civ) y)
 48 |       tpe := piAll `(x === y) (p.implicits ++ [a1,a2,prf])
 49 |    in claim M0 vis [] fun tpe
 50 |
 51 | argType : Vect n Arg -> TTImp
 52 | argType []        = `(_)
 53 | argType [a]       = a.type
 54 | argType (_ :: xs) = argType xs
 55 |
 56 | ||| Top-level function declaration of a proof that all the `conIndexXY`
 57 | ||| function is injective.
 58 | export
 59 | toIndexInjectiveClaimT : Visibility -> (tifun, fun : Name) -> (p : TypeInfo) -> Decl
 60 | toIndexInjectiveClaimT vis tifun fun p =
 61 |   let civ := var tifun
 62 |       vp  := var p.name
 63 |       at  := argType p.args
 64 |       t1  := MkArg M0 ImplicitArg (Just "t1") at
 65 |       t2  := MkArg M0 ImplicitArg (Just "t2") at
 66 |       a1  := MkArg MW ExplicitArg (Just "x") `(~(vp) t1)
 67 |       a2  := MkArg MW ExplicitArg (Just "y") `(~(vp) t2)
 68 |       prf := MkArg MW ExplicitArg (Just "prf") `(~(civ) x = ~(civ) y)
 69 |       tpe := piAll `(x = y) [t1,t2,a1,a2,prf]
 70 |    in claim M0 vis [] fun tpe
 71 |
 72 | ||| Top-level function declaration of a proof that every value is
 73 | ||| indeed included in `Data.Finite.values`.
 74 | export
 75 | valuesCompleteClaim : Visibility -> (fun : Name) -> (p : TypeInfo) -> Decl
 76 | valuesCompleteClaim vis fun p =
 77 |   let arg := MkArg MW ExplicitArg (Just "v") p.applied
 78 |       tpe := piAll `(Data.List.Elem.Elem v Data.Finite.values) (p.implicits ++ [arg])
 79 |    in claim M0 vis [] fun tpe
 80 |
 81 | ||| Top-level function declaration for a conversion of a data constructor
 82 | ||| to a value of type `Index n`, where `n` is the number of data constructors
 83 | ||| of the type.
 84 | export
 85 | toIndexClaim : Visibility -> (fun : Name) -> (p : TypeInfo) -> Decl
 86 | toIndexClaim vis fun p =
 87 |   let tot := primVal (B32 $ cast $ length p.cons)
 88 |       arg := MkArg MW ExplicitArg (Just "v") p.applied
 89 |       tpe := piAll `(Index ~(tot)) (p.implicits ++ [arg])
 90 |    in simpleClaim vis fun tpe
 91 |
 92 | ||| Top-level `Enum` implementation declaration
 93 | export
 94 | enumClaim : Visibility -> (fun : Name) -> (p : TypeInfo) -> Decl
 95 | enumClaim vis fun p =
 96 |   let tot := primVal (B32 $ cast $ length p.cons)
 97 |    in implClaimVis vis fun `(Enum ~(p.applied) ~(tot))
 98 |
 99 | ||| Top-level `Enum` implementation declaration
100 | export
101 | tenumClaim : Visibility -> (fun : Name) -> (p : TypeInfo) -> Decl
102 | tenumClaim vis fun p =
103 |   let tot := primVal (B32 $ cast $ length p.cons)
104 |    in implClaimVis vis fun `(TEnum ~(var p.name) ~(tot))
105 |
106 | --------------------------------------------------------------------------------
107 | --          Definitions
108 | --------------------------------------------------------------------------------
109 |
110 | export
111 | conIndexLtDef : (fun : Name) -> TypeInfo -> Decl
112 | conIndexLtDef f p = def f $ map cclause p.cons
113 |   where
114 |     cclause : Con p.arty p.args -> Clause
115 |     cclause c = patClause (var f `app` bindAny c) `(Data.Prim.Bits32.mkLT Refl)
116 |
117 | export
118 | toIndexDef : (fun, cif, ltp : Name) -> Decl
119 | toIndexDef f cif ltp =
120 |  let rhs := `(I (cast {to = Bits32} (~(var cif) v)) @{~(var ltp) v})
121 |   in def f [patClause (var f `app` var "v") rhs]
122 |
123 | export
124 | toIndexInjectiveDef : (fun : Name) -> TypeInfo -> Decl
125 | toIndexInjectiveDef f p = def f $ map cclause [| MkPair p.cons p.cons |]
126 |   where
127 |     cclause : (Con x y,Con x y) -> Clause
128 |     cclause (c1,c2) =
129 |      let t1 := var c1.name
130 |          t2 := var c2.name
131 |       in case c1.name == c2.name of
132 |            True => patClause (appAll f [t1,t2,`(Refl)]) `(Refl)
133 |            False => impossibleClause (appAll f [t1,t2,`(Refl)])
134 |
135 | export
136 | valuesCompleteDef : (fun : Name) -> TypeInfo -> Decl
137 | valuesCompleteDef f p = def f (clauses [<] `(Here) p.cons)
138 |   where
139 |     clauses : SnocList Clause -> TTImp -> List (Con p.arty p.args) -> List Clause
140 |     clauses sc prf []        = sc <>> []
141 |     clauses sc prf (x :: xs) =
142 |      let c := patClause (var f `app` bindAny x) prf
143 |       in clauses (sc:<c) `(There ~(prf)) xs
144 |
145 | export
146 | enumDef : (f, ti, inj, comp : Name) -> Decl
147 | enumDef f ti inj comp =
148 |   def f [patClause (var f) `(MkEnum ~(var ti) ~(var inj) ~(var comp))]
149 |
150 | export
151 | tenumDef : (f, ti, inj : Name) -> Decl
152 | tenumDef f ti inj =
153 |   def f [patClause (var f) `(MkTEnum ~(var ti) ~(var inj))]
154 |
155 | --------------------------------------------------------------------------------
156 | --          Deriving
157 | --------------------------------------------------------------------------------
158 |
159 | ||| Generates a proof that the constructor index returned by `conIndexXY` is
160 | ||| strictly less than the number of constructors.
161 | export
162 | ConIndexLtVis : Visibility -> List Name -> ParamTypeInfo -> Res (List TopLevel)
163 | ConIndexLtVis vis nms p =
164 |   let ci   := conIndexName p
165 |       fun  := conIndexLtName p
166 |    in Right
167 |         [ TL (conIndexLtClaim vis ci fun p.info) (conIndexLtDef fun p.info)
168 |         ]
169 |
170 | ||| Alias for `ConIndexLtVis Export`
171 | export %inline
172 | ConIndexLt : List Name -> ParamTypeInfo -> Res (List TopLevel)
173 | ConIndexLt = ConIndexLtVis Export
174 |
175 | ||| Generates a conversion of data constructors to values of type `Index n`,
176 | ||| where `n` is the number of data constructors of the given type.
177 | |||
178 | ||| This includes `ConIndexLtVis`
179 | export
180 | ToIndexVis : Visibility -> List Name -> ParamTypeInfo -> Res (List TopLevel)
181 | ToIndexVis vis nms p =
182 |   let fun  := toIndexName p
183 |       cif  := conIndexName p
184 |       ltp  := conIndexLtName p
185 |    in sequenceJoin
186 |         [ ConIndexLtVis vis nms p
187 |         , Right [TL (toIndexClaim vis fun p.info) (toIndexDef fun cif ltp)]
188 |         ]
189 |
190 | ||| Alias for `ToIndexVis Export`
191 | export %inline
192 | ToIndex : List Name -> ParamTypeInfo -> Res (List TopLevel)
193 | ToIndex = ToIndexVis Export
194 |
195 | ||| Generates a proof that the `toIndexXY` function is injective.
196 | export
197 | ToIndexInjectiveVis : Visibility -> List Name -> ParamTypeInfo -> Res (List TopLevel)
198 | ToIndexInjectiveVis vis nms p =
199 |   let ti   := toIndexName p
200 |       fun  := toIndexInjectiveName p
201 |    in Right
202 |         [ TL (toIndexInjectiveClaim vis ti fun p.info) (toIndexInjectiveDef fun p.info)
203 |         ]
204 |
205 | ||| Alias for `ToIndexInjectiveVis Export`
206 | export %inline
207 | ToIndexInjective : List Name -> ParamTypeInfo -> Res (List TopLevel)
208 | ToIndexInjective = ToIndexInjectiveVis Export
209 |
210 | ||| Generates a proof that the `Data.Finite.values` indeed contains every
211 | ||| possible value. This currently only works for enum types.
212 | export
213 | ValuesCompleteVis : Visibility -> List Name -> ParamTypeInfo -> Res (List TopLevel)
214 | ValuesCompleteVis vis nms p =
215 |   let fun := valuesCompleteName p
216 |    in Right
217 |         [ TL (valuesCompleteClaim vis fun p.info) (valuesCompleteDef fun p.info)
218 |         ]
219 |
220 | ||| Alias for `ValuesCompleteVis Export`
221 | export %inline
222 | ValuesComplete : List Name -> ParamTypeInfo -> Res (List TopLevel)
223 | ValuesComplete = ValuesCompleteVis Export
224 |
225 | ||| Derives interfaces `Eq`, `Ord`, `Finite`, and `Enum` plus utility
226 | ||| functions with the relevant proofs for the given type.
227 | |||
228 | ||| Erased proofs are generated at `export` visibility.
229 | export
230 | EnumVis : Visibility -> List Name -> ParamTypeInfo -> Res (List TopLevel)
231 | EnumVis vis nms p =
232 |   let fun := implName p "Enum"
233 |       ti  := toIndexName p
234 |       inj := toIndexInjectiveName p
235 |       cmp := valuesCompleteName p
236 |    in sequenceJoin
237 |         [ EqVis vis nms p
238 |         , OrdVis vis nms p
239 |         , FiniteVis vis nms p
240 |         , ToIndex nms p
241 |         , ToIndexInjective nms p
242 |         , ValuesComplete nms p
243 |         , Right [TL (enumClaim vis fun p.info) (enumDef fun ti inj cmp)]
244 |         ]
245 |
246 | ||| Alias for `EnumVis Public`
247 | export %inline
248 | Enum : List Name -> ParamTypeInfo -> Res (List TopLevel)
249 | Enum = EnumVis Public
250 |
251 | ||| Generates a proof that the `toIndexXY` function is injective.
252 | export
253 | ToIndexInjectiveTVis : Visibility -> List Name -> ParamTypeInfo -> Res (List TopLevel)
254 | ToIndexInjectiveTVis vis nms p =
255 |   let ti   := toIndexName p
256 |       fun  := toIndexInjectiveName p
257 |    in Right
258 |         [ TL (toIndexInjectiveClaimT vis ti fun p.info) (toIndexInjectiveDef fun p.info)
259 |         ]
260 |
261 | ||| Alias for `ToIndexInjectiveTVis Export`
262 | export %inline
263 | ToIndexInjectiveT : List Name -> ParamTypeInfo -> Res (List TopLevel)
264 | ToIndexInjectiveT = ToIndexInjectiveTVis Export
265 |
266 | ||| Derives interfaces `TEnum` plus utility
267 | ||| functions with the relevant proofs for the given `Type -> Type`.
268 | |||
269 | ||| Erased proofs are generated at `export` visibility.
270 | export
271 | TEnumVis : Visibility -> List Name -> ParamTypeInfo -> Res (List TopLevel)
272 | TEnumVis vis nms p =
273 |   let fun := implName p "TEnum"
274 |       ti  := toIndexName p
275 |       inj := toIndexInjectiveName p
276 |    in sequenceJoin
277 |         [ ConIndex nms p
278 |         , ToIndex nms p
279 |         , ToIndexInjectiveT nms p
280 |         , Right [TL (tenumClaim vis fun p.info) (tenumDef fun ti inj)]
281 |         ]
282 |
283 | ||| Alias for `TEnumVis Public`
284 | export %inline
285 | TEnum : List Name -> ParamTypeInfo -> Res (List TopLevel)
286 | TEnum = TEnumVis Public
287 |