Debian Codegraph @

apps/derive/elpi/projK.elpi

raw
1 /* Derive a function "projnK t -> x" iif t is "K ..x.." */
2 /* license: GNU Lesser General Public License Version 2.1 or later */
3 /* ------------------------------------------------------------------------- */
4
5 shorten std.{append, any->string, zip, map, map2, nth, do!, assert!, flatten}.
6
7 % TODO: instead of using a context and integers one could
8 % generate the product for the projected argument and use via subst-prod
9 % rather than using copy like crazy
10
11 namespace derive.projK {
12
13 :index (1)
14 func exT-close list int, list (pair term term), list term, term, term -> term, term.
15 exT-close [] _ _ T Ty T Ty1 :- copy Ty Ty1.
16 exT-close [N|Mask] Ctx Args T Ty {{ @existT lp:S lp:P lp:A lp:R1 }}
17 {{ @sigT lp:S lp:P }} :-
18 nth N Ctx (pr X TX),
19 copy TX S,
20 P = fun {coq.name-suffix `i` N} S B,
21 nth N Args A,
22 (pi x\ (copy X x :- !) => exT-close Mask Ctx Args T Ty (R x) (B x)),
23 R1 = R A.
24
25 :index (1)
26 func sigT-close list int, list (pair term term), term -> term.
27 sigT-close [] _ Ty Ty1 :- copy Ty Ty1.
28 sigT-close [N|Mask] Ctx Ty {{ @sigT lp:S lp:P }} :-
29 nth N Ctx (pr X TX),
30 copy TX S,
31 P = fun {coq.name-suffix `i` N} S B,
32 pi x\ (copy X x :- !) => sigT-close Mask Ctx Ty (B x).
33
34 func body-branch int, constructor, term, term, list term, list term -> term.
35 body-branch J K K1 _ VS TS R :-
36 default-output J Mask DfltCtx Dflt DfltTy, !,
37 if (coq.safe-dest-app K1 K1HD _, coq.env.global (indc K) K1HD)
38 (nth J VS X, nth J TS XT, zip VS TS Ctx, Args = VS)
39 (X = Dflt, XT = DfltTy, Ctx = DfltCtx, map DfltCtx fst Args),
40 exT-close Mask Ctx Args X XT R _RT.
41
42 func body-ty int, term, list term, list term -> term.
43 body-ty J _ _ _ SigT :-
44 default-output J Mask Args _ T, !,
45 sigT-close Mask Args T SigT.
46
47 :index (1)
48 func occurs-list list (pair term term), int, term -> list int.
49 occurs-list [] _ _ [].
50 occurs-list [pr X _|XS] I T [I|IS] :- occurs X T, !,
51 J is I + 1, occurs-list XS J T IS.
52 occurs-list [_|XS] I T IS :-
53 J is I + 1, occurs-list XS J T IS.
54
55 func mask-for term, list (pair term term) -> list int.
56 mask-for T Args Mask :- occurs-list Args 0 T Mask.
57
58
59 pred default-output i:int, % gather infos to generate the match
60 o:list int, % mask: the position of the arguments that occur in the type
61 % of the projected one
62 o:list (pair term term), % a context (term,type) of default values
63 o:term, % the default value for the projected argument
64 o:term. % its type
65
66 func body-default % makes lambdas for all default arguments + the projected
67 term, % constructor type (begin processed)
68 int, % current argument
69 int, % argument to project
70 term, % inductive type applied to parameters and indexes
71 list (pair term term), % variables for default arguments
72 constructor % constructor to project
73 -> term.
74
75 body-default (prod N T F) J OJ IT Args K (fun N T B) :- !,
76 mask-for T Args Mask,
77 J1 is J + 1,
78 @pi-decl N T x\ sigma Def\
79 if (J = OJ)
80 (Def = [default-output J Mask Args x T])
81 (Def = []),
82 Def ==> body-default (F x) J1 OJ IT {append Args [pr x T]} K (B x).
83
84 body-default X J OJ IT Args K F :- whd1 X X1, !,
85 body-default X1 J OJ IT Args K F.
86
87 body-default _ _ J IT _ K (fun `i` IT B) :- !,
88 @pi-decl `i` IT i\ coq.build-match i IT (body-ty J) (body-branch J K) (B i).
89
90 func body-param % makes lambdas for all parameters and indexes
91 term, % arity of the inductive type
92 term, % type of the constructor to project
93 term, % inductive type applied to the arity processed so far
94 int, % number of Parameters left to process
95 int, % argument to project
96 constructor % constructor to project
97 -> term.
98
99 body-param (sort _) KTy IT 0 J K R :- !,
100 body-default KTy 0 J IT [] K R.
101
102 body-param (prod N T F) KTy IT 0 J K (fun N T B) :- !,
103 @pi-decl N T x\ body-param (F x) KTy {coq.mk-app IT [x]} 0 J K (B x).
104
105 body-param (prod N T F) (prod _ _ FK) IT Pno J K (fun N T B) :- !,
106 Pno1 is Pno - 1,
107 @pi-decl N T x\ body-param (F x) (FK x) {coq.mk-app IT [x]} Pno1 J K (B x).
108
109 body-param (prod _ _ _ as X) KTy IT N J K R :- whd1 KTy KTy1, !,
110 body-param X KTy1 IT N J K R.
111 body-param X (prod _ _ _ as KTy) IT N J K R :- whd1 X X1, !,
112 body-param X1 KTy IT N J K R.
113 body-param X KTy IT N J K R :- whd1 X X1, whd1 KTy KTy1, !,
114 body-param X1 KTy1 IT N J K R.
115
116 % ---------------------------------------------------------------------
117
118 func allK-projs string, int, int, int, term, term, constructor, term -> list prop.
119
120 allK-projs _ J J _ _ _ _ _ [] :- !.
121
122 allK-projs Prefix J JN Paramsno Arity ITy K KTy Clauses :- do! [
123 (@dropunivs! => coq.elaborate-skeleton ITy _ IT ok),
124 body-param Arity KTy IT Paramsno J K RSkel,
125 J1 is J + 1,
126 Name is Prefix ^ {coq.gref->id (indc K)} ^ {any->string J1},
127 if (coq.elaborate-skeleton RSkel TyR R ok)
128 ((@dropunivs! => coq.env.add-const Name R TyR _ P),
129 Clause = (projK-db K J1 (global (const P)) :- !),
130 coq.elpi.accumulate _ "derive.projK.db" (clause _ (before "projK-db:fail") Clause),
131 Clauses = [Clause|ClausesRest])
132 (coq.say "skip" Name R, Clauses = ClausesRest),
133 allK-projs Prefix J1 JN Paramsno Arity IT K KTy ClausesRest
134 ].
135
136 func for-K string, int, term, term, constructor, term -> list prop.
137 for-K Prefix Paramsno Arity IT K KT Clauses :- do! [
138 coq.count-prods KT N,
139 Argsno is N - Paramsno,
140 allK-projs Prefix 0 Argsno Paramsno Arity IT K KT Clauses
141 ].
142
143 func main inductive, string -> list prop.
144 main GR Prefix Clauses :- do! [
145 coq.env.global (indt GR) T,
146 coq.env.indt GR _ Paramsno _ Arity Kn Kt,
147 map2 Kn Kt (for-K Prefix Paramsno Arity T) ClausesList,
148 flatten ClausesList Clauses
149 ].
150
151 }