Debian Codegraph @

apps/tc/elpi/rewrite_forward.elpi

raw
1 % /* license: GNU Lesser General Public License Version 2.1 or later */
2 % /* ------------------------------------------------------------------------- */
3
4 % namespace tc {
5
6 % pred forward i:term, o:term, o:list (pair (list term) term).
7
8 % % Auxiliary function for rewrite-forward
9 % pred rewrite-forward->list i:term, i:name, i:prop, o:list prop.
10 % rewrite-forward->list P N (forward _ Lemma RewL) L :-
11 % coq.mk-app Lemma [P] LemmaApp,
12 % % coq.typecheck LemmaApp T ok,
13 % % coq.say T,
14 % std.map RewL (x\r\ sigma ProjL Ty Pr\
15 % pr ProjL Ty = x,
16 % make-proj-app ProjL LemmaApp Pr,
17 % r = decl Pr N Ty) L.
18
19 % % Takes a decl from the context and returns the list of its atomic
20 % % representations by looking in the forward clauses
21 % pred rewrite-forward i:prop, o:list prop.
22 % rewrite-forward (decl P N Ty) L :-
23 % std.findall (forward Ty _ _) FwdL,
24 % std.map FwdL (rewrite-forward->list P N) RewFdw,
25 % std.flatten RewFdw L.
26 % rewrite-forward _ [].
27
28 % % Takes a list of projections ([proj1|proj2]*) and a term T
29 % % and returns the coq's term (projX (projY (... (projZ T))))
30 % % Note that app [Proj, _, _, Rest] has two holes for the types
31 % % of the left and right side of Rest
32 % pred make-proj-app i:list term, i:term, o:term.
33 % make-proj-app [Proj | Projs] T (app [Proj, L, R, Rest]) :-
34 % make-proj-app Projs T Rest,
35 % % TODO: here we do a naive typecheck to get the types L and R of Rest,
36 % % it can be done in a faster way
37 % coq.typecheck Rest {{and lp:L lp:R}} ok.
38 % make-proj-app [] T T.
39
40 % % Takes a conjunction C of terms and []. It returns a list of pair:
41 % % The paths to the conjunct c in C and the path to reach it in C
42 % pred rec-split-and i:term, i:(list term), o:list (pair (list term) term).
43 % rec-split-and {{lp:A /\ lp:B}} DL L :-
44 % LEFT = [{{proj1}} | DL],
45 % RIGHT = [{{proj2}} | DL],
46 % rec-split-and A LEFT AL,
47 % rec-split-and B RIGHT BL,
48 % std.append AL BL L.
49 % rec-split-and A P [pr P A].
50
51 % % It takes a rewriting-lemma and abstract it into elpi in a forward
52 % % clause of type forward. The base case wants a ∀(x : T).f x, since
53 % % we want to keep trace of the type T of x.
54 % pred compile-rewrite i:term, i:term, i:list term, o:prop.
55 % compile-rewrite (prod _ Ty ((x\ app _) as Bo)) Lemma L (pi x\ forward Ty LemmaApp (TL x)) :-
56 % pi x\
57 % coq.mk-app Lemma {std.rev L} LemmaApp,
58 % rec-split-and (Bo x) [] (TL x).
59 % compile-rewrite (prod _ _ Bo) Lemma L (pi x\ C x) :-
60 % pi x\ compile-rewrite (Bo x) Lemma [x | L] (C x).
61
62 % % Takes a string (the name of a rewriting-lemma),
63 % % compiles and adds it as a forward clause in tc.db
64 % pred add-lemma->forward i:string.
65 % add-lemma->forward Lemma :-
66 % coq.locate Lemma Gr,
67 % coq.env.typeof Gr Type,
68 % compile-rewrite Type (global Gr) [] Cl,
69 % coq.elpi.accumulate _ "tc.db" (clause Lemma _ Cl).
70
71 % % TODO: @FissoreD @gares should make a set in output?
72 % pred rewrite-dep i:list prop, o:list prop.
73 % rewrite-dep [] [].
74 % rewrite-dep [A | B] L :-
75 % rewrite-forward A NewA, not (NewA = []),
76 % std.append NewA B ToTreat,
77 % rewrite-dep ToTreat L.
78 % rewrite-dep [A | TL] [A | L] :- rewrite-dep TL L.
79
80 % }