% % This is an effort for forcing llam links when used as input variables % % in a premise call. However, this brings several issues to find % % the right variable to activate the right link.s % namespace force-llam { % pred is-uvar-destruct i:term, o:term. % is-uvar-destruct T R :- name T Hd _, name-pair R Hd _, !. % is-uvar-destruct T T :- is-uvar T. % % At compile time, given a premise p with a flexible argument X. % % If X is expected to be in input mode, we add the auxiliary clause % % `solve-llam-t X`, so that, if any suspended % % llam link on X exists, then it is forced before solving p % pred modes i:list string, i:list term, o:list prop. % modes ["o"] [] [] :- !. % modes ["+" | Ms] [X | Xs] [P | Ps] :- is-uvar-destruct X R, !, % P = tc.link.solve-llam-t R, modes Ms Xs Ps. % modes ["!" | Ms] [X | Xs] [P | Ps] :- is-uvar-destruct X R, !, % P = tc.link.solve-llam-t R, modes Ms Xs Ps. % modes [_ | Ms] [_ | Xs] Ps :- % modes Ms Xs Ps. % % The following rule represents a try to force llam links when input of % % of other premises. % compile-conclusion ff Goal Proof HOPremisesIn HOPremisesOut Premises Clause :- % coq.safe-dest-app Goal Class Args, % tc.get-mode {tc.get-TC-of-inst-type Class} Modes, % force-llam.modes Modes Args ForceLlam, % tc.make-tc Goal Proof Premises ff Clause1, % Prems = [HOPremisesIn, ForceLlam, [Clause1], HOPremisesOut], % std.flatten Prems AllPremises, % Clause = do AllPremises. % % Scope Var Args Rhs % pred progress-llam-refine i:list term, i:term, i:list term, o:term. % progress-llam-refine S V [A|As] R :- name A, not (std.mem! S A), !, % % prune (V' A) [A|S], % eta V (fun _ _ (x\ V' x)), % progress-llam-refine [A|S] (V' A) As R. % progress-llam-refine _ V [] V. % progress-llam-refine _ V As (app [V|As]). % pred split-pf i:list term, i:list term, o:list term, o:list term. % split-pf [] _ [] [] :- !. % split-pf [X|Xs] Old [X|Ys] L :- name X, not (std.mem! Old X), !, split-pf Xs [X|Old] Ys L. % split-pf Xs _ [] Xs. % pred lam->fun i:term, i:list term, i:any. % lam->fun T [] R :- !, std.unsafe-cast R R', copy T T', R' = T'. % lam->fun Hd [H|L] R :- !, % pi x\ (copy H x :- !) => lam->fun Hd L (R' x), % std.unsafe-cast R Rx, Rx = R'. % pred unify-align i:term, i:term, i:list term, i:list term. % unify-align (app L) Hd PF NPF :- % Len is {std.length L} - {std.length NPF}, Len >= 0, % std.split-at Len L L' NPF, % Z = app L', % coq.mk-app Z NPF TT, % lam->fun TT {std.append PF NPF} Hd. % pred unify-const i:term, i:term, i:list term. % unify-const N R [] :- !, copy N X, R = X. % unify-const N R [A|As] :- not found.aux, A == N, !, % pi x\ found.aux => (copy N x :- !) => unify-const N (R' x) As, % std.unsafe-cast R Rx, Rx = R'. % unify-const N R [_|As] :- % pi x\ unify-const N (R' x) As, % std.unsafe-cast R Rx, Rx = R'. % pred unify-heuristics i:term, i:term. % unify-heuristics A T :- tc.unify-eq A T. % % unify-heuristics (app _ as A) (fun _ _ B) :- !, % % coq.error "A", % % eta-expand A (fun _ _ A'), pi x\ unify-heuristics (A' x) (B x). % % unify-heuristics (app _ as A) B :- var B Hd Tl, !, % % split-pf Tl [] PF NPF, % % unify-align A Hd PF NPF. % % unify-heuristics A B :- name A, var B Hd Tl, !, % TODO: also const % % unify-const A Hd Tl. % % unify-heuristics (app L) (app[X|L']) :- % % var X, !, % % Len is {std.length L} - {std.length L'}, Len > 0, % % std.split-at Len L Hd L', % % if (Hd = [X]) true (X = app Hd). % % unify-heuristics (app L) (app L') :- % % std.forall2 L L' unify-heuristics. % % % The following rule leaves elpi uvars outside PF in coq % % % unify-heuristics A B :- A = B. % % % The following seems to solve the problem of the previous rule % % % TODO: I don't understand why the following rule cant be written as: % % % unify-heuristics A B :- var B Hb Lb, A = app[Hb|Lb] % % % without breaking test CoqUvar2 % % unify-heuristics (uvar Ha []) (uvar Hb Lb) :- !, % % Ha = app[Hb|Lb]. % % unify-heuristics (fun _ _ Bo) (uvar Hb_ Lb1) :- not (distinct_names Lb1), !, % % Lb1 = [_ | Lb], % % std.spy-do![prune Z Lb, % % % std.unsafe-cast Hb Hb1, % % (pi x\ unify-heuristics (Bo x) Z), % % Hb_ = % % coq.error "TODO" Z]. % % unify-heuristics A B :- A = B. % pred llam-aux i:term, i:term. % llam-aux A (uvar _ S as T) :- distinct_names S, !, A = T. % Here, both A and T are in PF % llam-aux A (uvar _ _ as T) :- !, A = T. % 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]. % llam-aux A (app [H|L] as T) :- var H, !, get-vars T Vars, declare_constraint (llam A (app [H|L])) [_|Vars]. % llam-aux A (app [H|L]) :- coq.mk-app H L T, !, unify-heuristics A T. % llam L (app [H|As]) :- var H _ S, !, % progress-llam-refine S H As Rhs, % llam-aux L Rhs. % llam L Rhs :- llam-aux L Rhs. % pred solve-llam-t-cond i:term, i:term. % :name "solve-llam-t-cond" % solve-llam-t-cond (uvar A _) (app [uvar B _ | _]) :- A = B. % % Aims to force a llam link suspended on the given variable % pred solve-llam-t o:term. % solve-llam-t X :- var X, !, declare_constraint (solve-llam-t X) [X]. % solve-llam-t _. % constraint solve-llam solve-llam-t llam { % rule \ (solve-llam-t X) (llam A B) | (solve-llam-t-cond X B) <=> (unify-heuristics A B). % rule solve-llam \ (llam A B) <=> (unify-heuristics A B). % rule \ solve-llam. % } % }