| 1 |
|
| 2 |
From elpi Require Import tc. |
| 3 |
|
| 4 |
Elpi Accumulate tc.db lp:{{ |
| 5 |
pred get-pattern-fragment i:term, o:list term. |
| 6 |
|
| 7 |
pred get-inout i:argument_mode, i:term, o:list term. |
| 8 |
% TODO: the second arg may not be an (app L) |
| 9 |
get-inout AMode (app [global GR | L]) Res :- |
| 10 |
std.drop-last 1 {tc-mode GR} Modes, |
| 11 |
std.map2-filter L Modes (t\m\r\ pr AMode _ = m, r = t) Res. |
| 12 |
get-inout _ _ []. |
| 13 |
|
| 14 |
pred input-must-have-predecessor i:term, i:term, i:list term, i:list term. |
| 15 |
input-must-have-predecessor _ _ [] _ :- !. |
| 16 |
input-must-have-predecessor Instance Premise [Mode | Modes] Premises :- |
| 17 |
std.exists Premises (p\ sigma MOut\ |
| 18 |
get-inout out p MOut, std.mem MOut Mode), |
| 19 |
input-must-have-predecessor Instance Premise Modes Premises. |
| 20 |
input-must-have-predecessor Instance Premise [Mode | _] _ :- |
| 21 |
coq.error "Input mode" Mode "of" |
| 22 |
Premise "cannot be inferred from the other premises of the instance" |
| 23 |
Instance. |
| 24 |
|
| 25 |
|
| 26 |
% CurrentType is the type of the current instance to get its input variables, |
| 27 |
% These variables should not create edges in the graph |
| 28 |
pred sort-hypothesis i:term, i:term, i:list term, o:list int. |
| 29 |
sort-hypothesis Instance (app [_ | InputCurrentType]) L NL :- |
| 30 |
std.map-i L (i\x\r\ r = pr x i) LookupList, |
| 31 |
std.map L (premise\r\ sigma M M'\ get-inout in premise M, |
| 32 |
std.filter M (x\ not (std.mem InputCurrentType x)) M', |
| 33 |
input-must-have-predecessor Instance premise M' L, |
| 34 |
r = pr premise M') InputModes, |
| 35 |
% foreach goal, we associate those goals having a dependency on it, |
| 36 |
% in particular a goal G2 depends on G1 if a variable V is in |
| 37 |
% output mode for G1 and in input mode for G2 (the dependency graph will |
| 38 |
% and edge going from G1 to G2 : G1 -> G2) |
| 39 |
std.map L (x\r\ sigma Output Deps\ % O(N^3 * check of occurs) |
| 40 |
% the list of variable in input of the current goal G |
| 41 |
get-inout out x Output, |
| 42 |
% for each output modes of all goals, we keep those having an output |
| 43 |
% which exists in the input variable of G |
| 44 |
std.map-filter InputModes (x\r\ |
| 45 |
sigma Fst Snd\ pr Fst Snd = x, |
| 46 |
std.exists Output (v\ std.exists Snd (v1\ occurs v v1)), r = Fst) Deps, % O(N^2) |
| 47 |
sigma Output2Nb Deps2Nb\ |
| 48 |
std.lookup! LookupList x Output2Nb, |
| 49 |
std.map Deps (std.lookup! LookupList) Deps2Nb, |
| 50 |
r = pr Output2Nb Deps2Nb) Graph, |
| 51 |
coq.toposort Graph NL. |
| 52 |
|
| 53 |
pred sort-and-compile-premises i:term, i:term, i:list term, i:list term, i:prop, o:list prop. |
| 54 |
sort-and-compile-premises Instance CurrentType Types Vars IsPositive Premises :- |
| 55 |
sort-hypothesis Instance CurrentType Types TypesSortedIndexes, % O (n^3) |
| 56 |
% std.map-i Types (i\e\r\ r = i) TypesSortedIndexes, |
| 57 |
std.map TypesSortedIndexes (x\r\ std.nth x Vars r) SortedVars, % O (n^2) |
| 58 |
std.map TypesSortedIndexes (x\r\ std.nth x Types r) SortedTypes, % O (n^2) |
| 59 |
std.map2-filter SortedTypes SortedVars (t\v\r\ |
| 60 |
compile-aux1 t v [] [] [] (not IsPositive) false r _) Premises. |
| 61 |
|
| 62 |
pred compile-aux1 i:term, i:term, i:list term, i:list univ, i:list term, i:prop, i:prop, o:prop, o:bool. |
| 63 |
:name "compiler-aux:start" |
| 64 |
compile-aux1 Ty I [] [X | XS] [] IsPositive IsHead (pi x\ C x) IsLeaf :- !, |
| 65 |
pi x\ copy (sort (typ X)) (sort (typ x)) => copy Ty (Ty1 x), |
| 66 |
compile-aux1 (Ty1 x) I [] XS [] IsPositive IsHead (C x) IsLeaf. |
| 67 |
compile-aux1 (prod N T F) I ListVar [] Types IsPositive IsHead Clause ff :- !, |
| 68 |
(if IsPositive (Clause = pi x\ C x) (Clause = (pi x\ decl x N T => C x))), |
| 69 |
pi p\ sigma Type\ |
| 70 |
if (app-has-class T, not (occurs p (F p))) (Type = T) (Type = app []), |
| 71 |
compile-aux1 (F p) I [p | ListVar] [] [Type | Types] IsPositive IsHead (C p) _. |
| 72 |
:if "simple-compiler" |
| 73 |
% TODO: here we don't do pattern fragment unification |
| 74 |
compile-aux1 Ty I ListVar [] Types IsPositive IsHead Clause tt :- !, |
| 75 |
sort-and-compile-premises I Ty Types ListVar IsPositive Premises, |
| 76 |
coq.mk-app I {std.rev ListVar} AppInst, |
| 77 |
tc.make-tc IsHead Ty AppInst Premises Clause. |
| 78 |
compile-aux1 Ty I ListVar [] Types IsPositive IsHead Clause tt :- !, |
| 79 |
sort-and-compile-premises I Ty Types ListVar IsPositive Premises, |
| 80 |
coq.mk-app I {std.rev ListVar} AppInst, |
| 81 |
std.append {get-pattern-fragment Ty} {get-pattern-fragment AppInst} Term-to-be-fixed, |
| 82 |
std.fold Term-to-be-fixed 0 (e\acc\r\ sigma L X\ e = app X, std.length X L, r is acc + L - 1) Len, |
| 83 |
if (IsPositive) (IsPositiveBool = tt) (IsPositiveBool = ff), |
| 84 |
remove-ho-unification IsHead IsPositiveBool Len Ty AppInst Premises Term-to-be-fixed [] [] [] [] [] Clause. |
| 85 |
|
| 86 |
:after "firstHook" |
| 87 |
compile-aux Ty Inst _Premises _VarAcc UnivL IsPositive IsHead Clause NoPremises :- !, |
| 88 |
compile-aux1 Ty Inst [] UnivL [] (IsPositive = tt, true; false) IsHead Clause NoPremises. |
| 89 |
}}. |