| 1 |
/* Hide indexes using non-uniform parameters */ |
| 2 |
/* license: GNU Lesser General Public License Version 2.1 or later */ |
| 3 |
/* ------------------------------------------------------------------------- */ |
| 4 |
|
| 5 |
shorten std.{assert!, do!, map-i, map}. |
| 6 |
|
| 7 |
namespace derive.invert { |
| 8 |
|
| 9 |
func the-inductive term -> int. |
| 10 |
func the-nup-for-idx int -> term. |
| 11 |
func the-suffix -> string. |
| 12 |
|
| 13 |
func invert indt-decl -> indt-decl. |
| 14 |
invert (parameter ID Imp Ty In) (parameter ID Imp Ty Out) :- |
| 15 |
@pi-parameter ID Ty p\ invert (In p) (Out p). |
| 16 |
invert (inductive ID IsInd Arity Ks) (inductive ID1 IsInd Arity1 Ks1) :- |
| 17 |
ID1 is ID ^ {the-suffix}, |
| 18 |
coq.arity->nparams Arity Nup, |
| 19 |
trivial-arity Arity 0 Arity1, |
| 20 |
coq.arity->term Arity ITy, |
| 21 |
@pi-parameter ID ITy i\ the-inductive i Nup => std.map (Ks i) (invert-c Nup Arity1) (Ks1 i). |
| 22 |
|
| 23 |
func invert-c int, arity, indc-decl -> indc-decl. |
| 24 |
invert-c Nup ArityI (constructor ID ArityK) (constructor ID1 ArityK1) :- |
| 25 |
ID1 is ID ^ {the-suffix}, |
| 26 |
invert-c-params Nup ArityI ArityK ArityK1. |
| 27 |
|
| 28 |
func invert-c-params int, arity, arity -> arity. |
| 29 |
invert-c-params Nup (parameter ID Imp Ty In) (parameter _ _ _ In1) (parameter ID Imp Ty Out):- Nup > 0, !, |
| 30 |
Nup1 is Nup - 1, |
| 31 |
@pi-parameter ID Ty p\ invert-c-params Nup1 (In p) (In1 p) (Out p). |
| 32 |
invert-c-params 0 Arity KArity Out :- invert-c-params-idx 0 Arity KArity Out. |
| 33 |
|
| 34 |
func invert-c-params-idx int, arity, arity -> arity. |
| 35 |
invert-c-params-idx Idx (parameter ID Imp Ty In) KArity (parameter ID Imp Ty Out) :- |
| 36 |
Idx1 is Idx + 1, |
| 37 |
@pi-parameter ID Ty p\ the-nup-for-idx Idx p =!=> invert-c-params-idx Idx1 (In p) KArity (Out p). |
| 38 |
invert-c-params-idx _ (arity _) KArity KArity1 :- |
| 39 |
invert-c-arity KArity KArity1. |
| 40 |
|
| 41 |
func invert-c-arity arity -> arity. |
| 42 |
invert-c-arity (parameter ID Imp Ty In) (parameter ID Imp Ty Out) :- |
| 43 |
@pi-parameter ID Ty p\ invert-c-arity (In p) (Out p). |
| 44 |
invert-c-arity (arity T) (arity T1) :- invert-c-ty T T1. |
| 45 |
|
| 46 |
func invert-c-ty term -> term. |
| 47 |
invert-c-ty (prod N S T) (prod N S T1) :- !, |
| 48 |
@pi-decl N S x\ invert-c-ty (T x) (T1 x). |
| 49 |
invert-c-ty I I :- the-inductive I _, !. |
| 50 |
invert-c-ty (app[I|Args]) Out :- the-inductive I Nup, !, |
| 51 |
std.split-at Nup Args NUArgs Indexes, |
| 52 |
invert-c-ty-eq Indexes 0 (app[I|NUArgs]) Out. |
| 53 |
|
| 54 |
func invert-c-ty-eq list term, int, term -> term. |
| 55 |
invert-c-ty-eq [] _ X X. |
| 56 |
invert-c-ty-eq [I|Idxs] N Acc {{ lp:V = lp:I -> lp:Out }} :- |
| 57 |
std.assert! (the-nup-for-idx N V) "no variable to equate to index expression", |
| 58 |
M is N + 1, |
| 59 |
invert-c-ty-eq Idxs M {coq.mk-app Acc [V]} Out. |
| 60 |
|
| 61 |
func trivial-arity arity, int -> arity. |
| 62 |
trivial-arity (parameter ID Imp Ty In) Ni (parameter ID Imp Ty Out) :- |
| 63 |
@pi-parameter ID Ty p\ trivial-arity (In p) Ni (Out p). |
| 64 |
trivial-arity (arity (prod Name Ty In)) Ni (parameter ID explicit Ty Out) :- !, |
| 65 |
ID is "idx" ^ {term_to_string Ni}, |
| 66 |
Ni1 is Ni + 1, |
| 67 |
@pi-decl Name Ty p\ trivial-arity (arity (In p)) Ni1 (Out p). |
| 68 |
trivial-arity (arity T) _ (arity T). |
| 69 |
|
| 70 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 71 |
|
| 72 |
func main inductive, string -> list prop. |
| 73 |
main GR Suffix Clauses :- do! [ |
| 74 |
coq.env.indt-decl GR Decl, |
| 75 |
the-suffix Suffix => invert Decl Decl1, |
| 76 |
std.assert-ok! (coq.typecheck-indt-decl Decl1) "derive.invert generates illtyped term", |
| 77 |
coq.env.add-indt Decl1 Inv, |
| 78 |
coq.env.indt GR _ _ _ _ Ks _, |
| 79 |
coq.env.indt Inv _ _ _ _ KIs _, |
| 80 |
|
| 81 |
Clauses = [invert-db (indt GR) (indt Inv) | {std.map2 Ks KIs mk-k-clause}], |
| 82 |
std.forall Clauses (c\coq.elpi.accumulate _ "derive.invert.db" (clause _ _ c)) |
| 83 |
]. |
| 84 |
|
| 85 |
func mk-k-clause constructor, constructor -> prop. |
| 86 |
mk-k-clause K1 K2 (invert-db (indc K1) (indc K2)). |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
% vim: set spelllang=: |