Debian Codegraph @

apps/tc/elpi/WIP/force_llam.elpi

raw
1 % % This is an effort for forcing llam links when used as input variables
2 % % in a premise call. However, this brings several issues to find
3 % % the right variable to activate the right link.s
4 % namespace force-llam {
5 % pred is-uvar-destruct i:term, o:term.
6 % is-uvar-destruct T R :- name T Hd _, name-pair R Hd _, !.
7 % is-uvar-destruct T T :- is-uvar T.
8
9 % % At compile time, given a premise p with a flexible argument X.
10 % % If X is expected to be in input mode, we add the auxiliary clause
11 % % `solve-llam-t X`, so that, if any suspended
12 % % llam link on X exists, then it is forced before solving p
13 % pred modes i:list string, i:list term, o:list prop.
14 % modes ["o"] [] [] :- !.
15 % modes ["+" | Ms] [X | Xs] [P | Ps] :- is-uvar-destruct X R, !,
16 % P = tc.link.solve-llam-t R, modes Ms Xs Ps.
17 % modes ["!" | Ms] [X | Xs] [P | Ps] :- is-uvar-destruct X R, !,
18 % P = tc.link.solve-llam-t R, modes Ms Xs Ps.
19 % modes [_ | Ms] [_ | Xs] Ps :-
20 % modes Ms Xs Ps.
21
22 % % The following rule represents a try to force llam links when input of
23 % % of other premises.
24 % compile-conclusion ff Goal Proof HOPremisesIn HOPremisesOut Premises Clause :-
25 % coq.safe-dest-app Goal Class Args,
26 % tc.get-mode {tc.get-TC-of-inst-type Class} Modes,
27 % force-llam.modes Modes Args ForceLlam,
28 % tc.make-tc Goal Proof Premises ff Clause1,
29 % Prems = [HOPremisesIn, ForceLlam, [Clause1], HOPremisesOut],
30 % std.flatten Prems AllPremises,
31 % Clause = do AllPremises.
32
33 % % Scope Var Args Rhs
34 % pred progress-llam-refine i:list term, i:term, i:list term, o:term.
35 % progress-llam-refine S V [A|As] R :- name A, not (std.mem! S A), !,
36 % % prune (V' A) [A|S],
37 % eta V (fun _ _ (x\ V' x)),
38 % progress-llam-refine [A|S] (V' A) As R.
39 % progress-llam-refine _ V [] V.
40 % progress-llam-refine _ V As (app [V|As]).
41
42 % pred split-pf i:list term, i:list term, o:list term, o:list term.
43 % split-pf [] _ [] [] :- !.
44 % split-pf [X|Xs] Old [X|Ys] L :- name X, not (std.mem! Old X), !, split-pf Xs [X|Old] Ys L.
45 % split-pf Xs _ [] Xs.
46
47 % pred lam->fun i:term, i:list term, i:any.
48 % lam->fun T [] R :- !, std.unsafe-cast R R', copy T T', R' = T'.
49 % lam->fun Hd [H|L] R :- !,
50 % pi x\ (copy H x :- !) => lam->fun Hd L (R' x),
51 % std.unsafe-cast R Rx, Rx = R'.
52
53 % pred unify-align i:term, i:term, i:list term, i:list term.
54 % unify-align (app L) Hd PF NPF :-
55 % Len is {std.length L} - {std.length NPF}, Len >= 0,
56 % std.split-at Len L L' NPF,
57 % Z = app L',
58 % coq.mk-app Z NPF TT,
59 % lam->fun TT {std.append PF NPF} Hd.
60
61 % pred unify-const i:term, i:term, i:list term.
62 % unify-const N R [] :- !, copy N X, R = X.
63 % unify-const N R [A|As] :- not found.aux, A == N, !,
64 % pi x\ found.aux => (copy N x :- !) => unify-const N (R' x) As,
65 % std.unsafe-cast R Rx, Rx = R'.
66 % unify-const N R [_|As] :-
67 % pi x\ unify-const N (R' x) As,
68 % std.unsafe-cast R Rx, Rx = R'.
69
70 % pred unify-heuristics i:term, i:term.
71 % unify-heuristics A T :- tc.unify-eq A T.
72 % % unify-heuristics (app _ as A) (fun _ _ B) :- !,
73 % % coq.error "A",
74 % % eta-expand A (fun _ _ A'), pi x\ unify-heuristics (A' x) (B x).
75 % % unify-heuristics (app _ as A) B :- var B Hd Tl, !,
76 % % split-pf Tl [] PF NPF,
77 % % unify-align A Hd PF NPF.
78 % % unify-heuristics A B :- name A, var B Hd Tl, !, % TODO: also const
79 % % unify-const A Hd Tl.
80 % % unify-heuristics (app L) (app[X|L']) :-
81 % % var X, !,
82 % % Len is {std.length L} - {std.length L'}, Len > 0,
83 % % std.split-at Len L Hd L',
84 % % if (Hd = [X]) true (X = app Hd).
85 % % unify-heuristics (app L) (app L') :-
86 % % std.forall2 L L' unify-heuristics.
87 % % % The following rule leaves elpi uvars outside PF in coq
88 % % % unify-heuristics A B :- A = B.
89 % % % The following seems to solve the problem of the previous rule
90 % % % TODO: I don't understand why the following rule cant be written as:
91 % % % unify-heuristics A B :- var B Hb Lb, A = app[Hb|Lb]
92 % % % without breaking test CoqUvar2
93 % % unify-heuristics (uvar Ha []) (uvar Hb Lb) :- !,
94 % % Ha = app[Hb|Lb].
95 % % unify-heuristics (fun _ _ Bo) (uvar Hb_ Lb1) :- not (distinct_names Lb1), !,
96 % % Lb1 = [_ | Lb],
97 % % std.spy-do![prune Z Lb,
98 % % % std.unsafe-cast Hb Hb1,
99 % % (pi x\ unify-heuristics (Bo x) Z),
100 % % Hb_ =
101 % % coq.error "TODO" Z].
102 % % unify-heuristics A B :- A = B.
103
104 % pred llam-aux i:term, i:term.
105 % llam-aux A (uvar _ S as T) :- distinct_names S, !, A = T. % Here, both A and T are in PF
106 % llam-aux A (uvar _ _ as T) :- !, A = T.
107 % llam-aux A (app [H|L] as T) :- var A, var H, !, get-vars T Vars, declare_constraint (llam A (app [H|L])) [_,A|Vars].
108 % llam-aux A (app [H|L] as T) :- var H, !, get-vars T Vars, declare_constraint (llam A (app [H|L])) [_|Vars].
109 % llam-aux A (app [H|L]) :- coq.mk-app H L T, !, unify-heuristics A T.
110
111 % llam L (app [H|As]) :- var H _ S, !,
112 % progress-llam-refine S H As Rhs,
113 % llam-aux L Rhs.
114 % llam L Rhs :- llam-aux L Rhs.
115
116 % pred solve-llam-t-cond i:term, i:term.
117 % :name "solve-llam-t-cond"
118 % solve-llam-t-cond (uvar A _) (app [uvar B _ | _]) :- A = B.
119
120 % % Aims to force a llam link suspended on the given variable
121 % pred solve-llam-t o:term.
122 % solve-llam-t X :- var X, !, declare_constraint (solve-llam-t X) [X].
123 % solve-llam-t _.
124
125 % constraint solve-llam solve-llam-t llam {
126 % rule \ (solve-llam-t X) (llam A B) | (solve-llam-t-cond X B) <=> (unify-heuristics A B).
127 % rule solve-llam \ (llam A B) <=> (unify-heuristics A B).
128 % rule \ solve-llam.
129 % }
130 % }
131