Debian Codegraph @

apps/derive/elpi/param1_functor.elpi

raw
1 /* map over a container */
2 /* license: GNU Lesser General Public License Version 2.1 or later */
3 /* ------------------------------------------------------------------------- */
4 shorten std.{assert!, do!, length, split-at, drop-last, rev, append}.
5
6 param1-functor-db (app[global (indt GR1)|A1]) (app[global (indt GR2)|A2]) R :-
7 coq.env.indt GR1 _ Lno1 _ _ _ _,
8 coq.env.indt GR2 _ Lno2 _ _ _ _,
9 {length A1} > Lno1, {length A2} > Lno2,
10 split-at Lno1 A1 Args1 Points1,
11 split-at Lno2 A2 Args2 Points2,
12 Points1 = Points2, !,
13 param1-functor-db {coq.mk-app (global (indt GR1)) Args1} {coq.mk-app (global (indt GR2)) Args2} F,
14 coq.mk-app F Points1 R.
15
16 namespace derive.param1.functor {
17
18 pred skip i:int. % position of a parameter that has to be skipped
19
20 % Building the body %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21 func self -> term.
22
23 :functional
24 pred bo-idx
25 i:term, % inductive arity (input)
26 i:term, % inductive type (input) applied to params & idx handled so far
27 i:term, % inductive type (output) applied to params & idx handled so far
28 i:int, % current index no
29 o:int, % Recno
30 i:list term, % rev list of (output) parameters
31 o:term, % body
32 o:term. % type
33
34 bo-idx (prod _ S1 T1) Ity1 Ity2 N M Ps (fun `x` S1 Bo) (prod `x` S1 Ty) :- !,
35 pi x\ sigma Ity1x Ity2x\
36 coq.mk-app Ity1 [x] Ity1x,
37 coq.mk-app Ity2 [x] Ity2x,
38 N1 is N + 1,
39 decl x `x` S1 => bo-idx (T1 x) Ity1x Ity2x N1 M Ps (Bo x) (Ty x).
40
41 bo-idx (sort _) Ity1 Ity2 N N Ps (fun `x` Ity1 Bo) (prod `_` Ity1 _\ Ity2) :- !,
42 @pi-decl `x` Ity1 x\
43 coq.build-match x Ity1 (bo-idx-rty Ps Ity2) (bo-k-args Ps) (Bo x).
44
45 bo-idx X Ity1 Ity2 N M Ps R1 R2 :- whd1 X X1, !,
46 bo-idx X1 Ity1 Ity2 N M Ps R1 R2.
47
48 func bo-idx-rty list term, term, term, list term, list term -> term.
49 bo-idx-rty Ps ItyArgs _ Vs _ R :-
50 rev Vs [_|IdxsRev],
51 rev IdxsRev Idxs,
52 coq.safe-dest-app ItyArgs HD _,
53 coq.mk-app HD {append {rev Ps} Idxs} R.
54
55 func bo-k-args list term, term, term, list term, list term -> term.
56 bo-k-args ParamsRev K _ Args Tys R :-
57 rev ParamsRev Params,
58 coq.safe-dest-app K (global (indc GR)) _,
59 coq.env.typeof (indc GR) T, coq.subst-prod Params T KT,
60 (bo-k-args.aux {coq.mk-app (global (indc GR)) Params} Args Tys KT R).
61
62 func bo-k-args.aux term, list term, list term, term -> term.
63 bo-k-args.aux K [A,PA|Args] [_,TPA|Types] {{ forall x (px : lp:(TPB x)), lp:(Ty x px) }} R :-
64 not(same_term TPA (TPB A)), param1-functor-db TPA (TPB A) F, !,
65 coq.mk-app K [A, {{ lp:F lp:PA }}] KAPFA,
66 bo-k-args.aux KAPFA Args Types (Ty A KAPFA) R.
67
68 bo-k-args.aux K [A,PA|Args] [_,_|Types] {{ forall x px, lp:(Ty x px) }} R :-
69 coq.mk-app K [A,PA] KAPA,
70 bo-k-args.aux KAPA Args Types (Ty A KAPA) R.
71
72 bo-k-args.aux R [] [] _ R.
73
74 % Take in input a mapping function for each parameter (not to be skipped)
75 % and then do the fixpoint
76
77 func bo-params
78 int, % current parameter
79 int, % number of parameters
80 term, % inductive type (input) applied to parameters handled so far
81 term, % inductive type (output) applied to parameters handled so far
82 term, % inductive arity (input)
83 term, % inductive arity (output)
84 list term % output parameters so far
85 -> term. % map function
86
87 func mk-rec-clause term, term, term, term -> prop.
88 mk-rec-clause (prod _ _ x\prod _ _ (P x)) T1 T2 F (pi x px\C x px) :- !,
89 pi x px\ mk-rec-clause (P x px) {coq.mk-app T1 [x,px]} {coq.mk-app T2 [x,px]} {coq.mk-app F [x,px]} (C x px).
90 mk-rec-clause (prod _ _ _\sort _) T1 T2 F (param1-functor-db T1 T2 F).
91
92 func mk-rec-clause-app term, term, term, term -> prop.
93 mk-rec-clause-app (prod _ _ x\prod _ _ (P x)) T1 T2 F (pi x px py\C x px py) :- !,
94 pi x px py\ mk-rec-clause-app (P x px) {coq.mk-app T1 [x,px]} {coq.mk-app T2 [x,py]} {coq.mk-app F [x,px]} (C x px py).
95 mk-rec-clause-app (prod _ _ _\sort S) T1 T2 F (pi x\C x) :-
96 pi x \ mk-rec-clause-app (sort S) {coq.mk-app T1 [x]} {coq.mk-app T2 [x]} {coq.mk-app F [x]} (C x).
97 mk-rec-clause-app (sort _) T1 T2 F (param1-functor-db T1 T2 F).
98
99 bo-params Lno Lno Ity1 Ity2 A1 _ Ps (fix `f` Recno Fty Bo) :- !,
100 @pi-decl `rec` Fty f\
101 mk-rec-clause A1 Ity1 Ity2 f (C f),
102 mk-rec-clause-app A1 Ity1 Ity2 f (D f),
103 (D f) =!=> (C f) =!=> bo-idx A1 Ity1 Ity2 0 Recno Ps (Bo f) Fty.
104
105 bo-params N Lno Ity1 Ity2 {{ forall (a : lp:T1) (p : a -> Type), lp:(Rty1 a p) }}
106 {{ forall (a : _) (p : a -> Type), lp:(Rty2 a p) }}
107 Ps R :- whd T1 [] {{ Type }} [], !,
108 N1 is N + 2,
109 (pi a b f \ mk-map-ty a {{ lp:a -> Type }} b {{ lp:b -> Type }} (FAB a b) f _ (Clause a b f)),
110 R = {{ fun (a : Type) (pa pb : a -> Type) (f : forall x : a, pa x -> pb x) => lp:(Bo a pa pb f) }},
111 pi a pa pb f\ sigma Ity1A Ity2A \
112 coq.mk-app Ity1 [a,pa] Ity1A,
113 coq.mk-app Ity2 [a,pb] Ity2A,
114 Clause pa pb f =>
115 decl a `a` {{ Type }} =>
116 decl pa `pa` {{ lp:a -> Type }} =>
117 decl pb `pb` {{ lp:a -> Type }} =>
118 decl f `f` (FAB pa pb) =>
119 bo-params N1 Lno Ity1A Ity2A (Rty1 a pa) (Rty2 a pb) [pb,a|Ps] (Bo a pa pb f).
120
121 bo-params N Lno Ity1 Ity2 (prod A Sty1 Rty1) (prod _ _ Rty2) Ps R :-
122 N1 is N + 1,
123 R = (fun A Sty1 a\ Bo a),
124 pi a\ sigma Ity1A Ity2A \
125 coq.mk-app Ity1 [a] Ity1A,
126 coq.mk-app Ity2 [a] Ity2A,
127 decl a A Sty1 => bo-params N1 Lno Ity1A Ity2A (Rty1 a) (Rty2 a) [a|Ps] (Bo a).
128
129 func map-pi any -> list prop.
130 map-pi (x\ []) [] :- !.
131 map-pi (x\ [X x| XS x]) [pi x\ X x | YS] :- map-pi XS YS.
132
133 :functional
134 pred mk-map-ty
135 i:term, % input variable
136 i:term, % and its type
137 i:term, % output variable
138 i:term, % an its type
139 o:term, % type of a mapping function from input to output
140 i:term, % map function (having the type above)
141 o:int, % arity of the predicate
142 o:list prop. % param1-functor-db clause for map function
143 mk-map-ty A (prod _ SA T1) B (prod _ SB T2) (prod `x` SA x\ R x) F N C1 :-
144 param1-functor-db SA SB Fa, !,
145 (pi x\ sigma Ax Fx BFx \
146 coq.mk-app A [x] Ax, coq.mk-app Fa [x] Fx, coq.mk-app B [Fx] BFx,
147 mk-map-ty Ax (T1 x) BFx (T2 BFx) (R x) {coq.mk-app F [x]} M (C x),
148 N is M + 1),
149 map-pi C C1.
150 mk-map-ty A (prod _ S1 T1) B (prod _ _ T2) (prod `x` S1 x\ R x) F N C1 :- !,
151 (pi x\ sigma Ax Bx \ coq.mk-app A [x] Ax, coq.mk-app B [x] Bx,
152 mk-map-ty Ax (T1 x) Bx (T2 x) (R x) {coq.mk-app F [x]} M (C x),
153 N is M + 1),
154 map-pi C C1.
155 mk-map-ty (app[X|XS] as A) _ (app[Y|YS] as B) _ (prod `x` A _\ B) (app [G|GS] as F) 0 [param1-functor-db PLA PLB PLF,param1-functor-db A B F] :- !,
156 drop-last 1 XS LA,
157 drop-last 1 YS LB,
158 drop-last 1 GS LF,
159 coq.mk-app X LA PLA,
160 coq.mk-app Y LB PLB,
161 coq.mk-app G LF PLF.
162 mk-map-ty A _ B _ (prod `x` A _\ B) F 0 [param1-functor-db A B F].
163
164 % Build a clause %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165 :functional
166 pred mk-clause
167 i:int, % current parameter
168 i:int, % number of parameters
169 i:term, % inductive type (input)
170 i:term, % inductive type (output)
171 i:term, % arity of the inductive
172 i:list (func), % premises of the clause
173 i:term, % map function
174 o:prop,
175 i:inductive,
176 i:list bool,
177 o:prop. % clause for param1-functor-db
178 mk-clause N N Ity1 Ity2 _ Todo Map (param1-functor-db Ity1 Ity2 Map :- [!|Todo]) I Mask (param1-functor-for I F MaskRev) :- !, coq.safe-dest-app Map (global F) _, std.rev Mask MaskRev.
179 mk-clause N Lno Ity1 Ity2 {{ forall (a : lp:Ty) (pa : a -> Type), lp:(T a pa) }} Todo Map (pi x px py f\ C x px py f) I M CF :- whd Ty [] {{ Type }} [], !,
180 N1 is N + 2,
181 pi x px py f\ sigma Ity1x Ity2y Mapf\
182 coq.mk-app Ity1 [x,px] Ity1x,
183 coq.mk-app Ity2 [x,py] Ity2y,
184 coq.mk-app Map [x,px,py,f] Mapf,
185 mk-clause N1 Lno Ity1x Ity2y (T x px) [param1-functor-db px py f|Todo] Mapf (C x px py f) I [tt,ff|M] CF.
186 mk-clause N Lno Ity1 Ity2 (prod _ _ T) Todo Map (pi x\ C x) I M CF :- !,
187 N1 is N + 1,
188 pi x\ sigma Ity1x Ity2x Mapf\
189 coq.mk-app Ity1 [x] Ity1x,
190 coq.mk-app Ity2 [x] Ity2x,
191 coq.mk-app Map [x] Mapf,
192 mk-clause N1 Lno Ity1x Ity2x (T x) Todo Mapf (C x) I [ff|M] CF.
193 mk-clause N Lno Ity1 Ity2 X Todo Map C I M CF :- whd1 X X1, !,
194 mk-clause N Lno Ity1 Ity2 X1 Todo Map C I M CF.
195
196 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
197 func main gref, gref, string -> list prop.
198 main _ (indt GR) Suffix C :- do! [
199 coq.env.indt GR _Ind Lno Luno Arity _ _,
200 assert! (Lno = Luno) "derive.param1.functor: Non-uniform parameters not supported",
201
202 % generate map and add to the env
203 T = global (indt GR),
204 bo-params 0 Lno T T Arity Arity [] R,
205 std.assert-ok! (coq.typecheck R Rty) "derive.param1_functor generates illtyped term",
206
207 Name is {coq.gref->id (indt GR)} ^ Suffix,
208 coq.env.add-const Name R Rty @transparent! Funct,
209
210 % generate clause and add to the db
211 mk-clause 0 Lno T T Arity [] (global (const Funct)) Clause1 GR [] Clause2,
212 C = [Clause1, Clause2],
213 std.forall C (x\coq.elpi.accumulate _ "derive.param1.functor.db" (clause _ _ x)),
214 ].
215
216 }