|
| 1 | +namespace tc { |
| 2 | + shorten tc.{r-ar, range-arity}. |
| 3 | + |
| 4 | + namespace compile { |
| 5 | + namespace goall { |
| 6 | + :index (_ _ 1) |
| 7 | + pred may-contract-to i:list term, i:term, i:term. |
| 8 | + may-contract-to _ N N :- !. |
| 9 | + % TODO: here we should do var V _ Scope and use scope: N can be in Scope but not in S |
| 10 | + may-contract-to L N (app [V|S]) :- var V, !, |
| 11 | + std.forall [N|L] (x\ exists! S (may-contract-to [] x)). |
| 12 | + may-contract-to L N (app [N|A]) :- |
| 13 | + std.length A {std.length L}, |
| 14 | + std.forall2 {std.rev L} A (may-contract-to []). |
| 15 | + may-contract-to L N (fun _ _ B) :- |
| 16 | + pi x\ may-contract-to [x|L] N (B x). |
| 17 | + |
| 18 | + :index (_ 1) |
| 19 | + pred occurs-rigidly i:term, i:term. |
| 20 | + occurs-rigidly N N :- name N, !. |
| 21 | + occurs-rigidly _ (app [N|_]) :- var N, !, fail. |
| 22 | + occurs-rigidly N (app A) :- exists! A (occurs-rigidly N). |
| 23 | + occurs-rigidly N (fun _ _ B) :- pi x\ occurs-rigidly N (B x). |
| 24 | + |
| 25 | + pred maybe-eta-aux i:term, i:list term. |
| 26 | + % TODO: here we should do var V _ Scope and use Scope: an elt in L can appear in Scope |
| 27 | + maybe-eta-aux (app[V|S]) L :- var V, !, |
| 28 | + std.forall L (x\ exists! S (y\ may-contract-to [] x y)). |
| 29 | + maybe-eta-aux (app [_|A]) L :- |
| 30 | + SplitLen is {std.length A} - {std.length L}, |
| 31 | + split-at-not-fatal SplitLen A HD TL, |
| 32 | + std.forall L (x\ not (exists! HD (occurs-rigidly x))), |
| 33 | + std.forall2 {std.rev L} TL (may-contract-to []). |
| 34 | + maybe-eta-aux (fun _ _ B) L :- |
| 35 | + pi x\ maybe-eta-aux (B x) [x|L]. |
| 36 | + |
| 37 | + pred maybe-eta i:term. |
| 38 | + maybe-eta (fun _ _ B) :- pi x\ maybe-eta-aux (B x) [x]. |
| 39 | + |
| 40 | + pred split-pf i:list term, i:list term, o:list term, o:list term. |
| 41 | + split-pf [] _ [] [] :- !. |
| 42 | + split-pf [X|Xs] Old [X|Ys] L :- name X, not (std.mem! Old X), !, split-pf Xs [X|Old] Ys L. |
| 43 | + split-pf Xs _ [] Xs. |
| 44 | + |
| 45 | + pred used i:term, i:term. |
| 46 | + used X (uvar _ S) :- std.mem! S X, !. |
| 47 | + used X (fun _ _ Bo) :- pi x\ used X (Bo x). |
| 48 | + |
| 49 | + pred close-term-prune-safe-fun i:(term -> list term), i:term, o:list term. |
| 50 | + close-term-prune-safe-fun (x\ []) _ [] :- !. |
| 51 | + close-term-prune-safe-fun (x\ [X x | Xs x]) Ty [fun _ _ X | Xs'] :- |
| 52 | + pi x\ used x (X x), !, close-term-prune-safe-fun Xs Ty Xs'. |
| 53 | + close-term-prune-safe-fun (x\ [X | Xs x]) Ty [X | Xs'] :- |
| 54 | + close-term-prune-safe-fun Xs Ty Xs'. |
| 55 | + |
| 56 | + pred compile-full-aux-close i:(term -> term), i:term, i:list prop, o:(term -> term), o:list prop. |
| 57 | + compile-full-aux-close Bo Ty L Bo' L' :- |
| 58 | + (pi x\ compile-full-aux (Bo x) [] (Bo' x) (BoL x)), |
| 59 | + % TODO: maybe attach to the links the list of used binders, to simply make this check? |
| 60 | + % Maybe L' is a pair list links and list binders per link, this way we can easily prune |
| 61 | + close-term-no-prune-ty BoL Ty BoL', |
| 62 | + std.append L BoL' L'. |
| 63 | + |
| 64 | + pred compile-full-aux i:term, i:list prop, o:term, o:list prop. |
| 65 | + compile-full-aux (global _ as G) L G L :- !. |
| 66 | + compile-full-aux (pglobal _ _ as G) L G L :- !. |
| 67 | + compile-full-aux (sort _ as G) L G L :- !. |
| 68 | + |
| 69 | + % Link for coercion |
| 70 | + compile-full-aux (app [HD|_] as G) L G' [tc.link.cs G' G | L] :- tc.coercion-unify HD, !. |
| 71 | + |
| 72 | + % Link for canonical structure |
| 73 | + compile-full-aux (app [global (const C) | _] as G) L G' [tc.link.cs G' G | L] :- coq.env.projection? C _, !. |
| 74 | + |
| 75 | + % Link for primitive projection |
| 76 | + compile-full-aux (app [primitive (proj P _) | _] as G) L G' [tc.link.cs G' G | L] :- coq.env.primitive-projection? P _, !. |
| 77 | + |
| 78 | + % Link for eta-redex |
| 79 | + compile-full-aux (fun Name Ty Bo as G) L G' [tc.link.eta G' (fun Name Ty' Bo') | L'] :- maybe-eta G, !, |
| 80 | + compile-full-aux Ty L Ty' LTy, |
| 81 | + compile-full-aux-close Bo Ty LTy Bo' L'. |
| 82 | + |
| 83 | + compile-full-aux (fun Name Ty Bo) L (fun Name Ty' Bo') L' :- !, |
| 84 | + compile-full-aux Ty L Ty' LTy, |
| 85 | + compile-full-aux-close Bo Ty LTy Bo' L'. |
| 86 | + |
| 87 | + compile-full-aux (prod Name Ty Bo) L (prod Name Ty' Bo') L' :- !, |
| 88 | + compile-full-aux Ty L Ty' LTy, |
| 89 | + compile-full-aux-close Bo Ty LTy Bo' L'. |
| 90 | + |
| 91 | + % NOTE: when compiling t = (app [X, x, y]) and [x,y] are distinct_names, |
| 92 | + % then the coq variable has not [x,y] in scope, it is applied to |
| 93 | + % them. Note also that t is a problematic term that cannot be |
| 94 | + % exposed to the else unification algorithm. |
| 95 | + % The solution is to build the following links: |
| 96 | + % X =η (λa.Y a) |
| 97 | + % a |- Y a =η (λb.Z a b) |
| 98 | + % and expose a variable W at toplevel having Z has head and [x, y] |
| 99 | + % as scope |
| 100 | + |
| 101 | + % NOTE: when compiling t = (app [X, a, x]) where a is a constant and x a |
| 102 | + % name, we build a llam link. |
| 103 | + % The link will be: T x =L X a x |
| 104 | + % the exposed variable in the term will be T x |
| 105 | + |
| 106 | + % NOTE: here a combination of eta and llam: |
| 107 | + % let t = (app [X x y, z, c, w, d]) where X is a coq var with x and |
| 108 | + % y in scope, z and w are names and c, d are constants. |
| 109 | + % In this case, the links should be: |
| 110 | + % X x y =η (λa.Y x y a) |
| 111 | + % a |- Z x y a w =L (app[Y x y a, c w d]) |
| 112 | + % The returned variable is Z x y a w |
| 113 | + |
| 114 | + pred free-names i:term, o:list term. |
| 115 | + free-names T L :- |
| 116 | + names N, |
| 117 | + (pi T L\ fold-map T L T [T|L] :- name T, std.mem! N T, !) => fold-map T [] _ L', |
| 118 | + undup {std.rev L'} L. |
| 119 | + |
| 120 | + :index(_ _ _ _ 3) |
| 121 | + % TODO: the argument Ty is unused, i.e. the binders of the eta links have no type... |
| 122 | + % LHS Scope Ty Names PF NPF OLDLINKS NEW_VAR |
| 123 | + pred build-eta-llam-links i:term, i:list term, o:term, i:list term, i:list term, i:list term, i:list prop, o:term, o:list prop. |
| 124 | + build-eta-llam-links LHS _ _ Names [] NPF OL HD [tc.link.llam T (app [LHS | NPF]) | OL] :- !, |
| 125 | + std.assert! (not (NPF = [])) "[TC] NPF List should not be empty", |
| 126 | + prune T Names, |
| 127 | + var T HD _. |
| 128 | + build-eta-llam-links LHS SC _Ty _ [X] [] OL HD [tc.link.eta LHS (fun _ _ (x\ V x)) | OL] :- !, |
| 129 | + prune V SC, var (V X) HD _. % TODO... |
| 130 | + build-eta-llam-links LHS SC _Ty Names [X|XS] NPF OL HD [tc.link.eta LHS (fun _ _ (x\ LHS' x)) | L] :- !, |
| 131 | + % TODO: unfold Ty if needed... |
| 132 | + std.append SC [X] SC', |
| 133 | + prune LHS' SC, build-eta-llam-links (LHS' X) SC' _ Names XS NPF OL HD L. |
| 134 | + |
| 135 | + % Link for beta-redex (Uvar in PF) |
| 136 | + % BUILD CHAIN OF LINKS-ETA from X to V... |
| 137 | + % TODO: to avoid too many chain for the same var, maybe pass a list into the fold |
| 138 | + compile-full-aux (app [(uvar _ Scope as V) | S] as T) L G' L'' :- !, |
| 139 | + % By construction the scope of a uvar is a list of distinct name |
| 140 | + std.fold-map S L compile-full-aux S' L', % LS = Links built for S |
| 141 | + % std.assert-ok! (coq.typecheck V Ty) "Not typecheckable variable", |
| 142 | + split-pf S' Scope PF NPF, |
| 143 | + free-names T Names, |
| 144 | + build-eta-llam-links V Scope _ Names PF NPF L' G'' L'', |
| 145 | + prune G' Names, |
| 146 | + var G' G'' Names. |
| 147 | + |
| 148 | + compile-full-aux (app L) A (app L1) A1 :- !, std.fold-map L A compile-full-aux L1 A1. |
| 149 | + |
| 150 | + compile-full-aux (let N T B F) A (let N T1 B1 F1) A3 :- !, |
| 151 | + compile-full-aux T A T1 A1, compile-full-aux B A1 B1 A2, |
| 152 | + compile-full-aux-close F T A2 F1 A3. |
| 153 | + |
| 154 | + compile-full-aux (fix N Rno Ty F) A (fix N Rno Ty1 F1) A2 :- !, |
| 155 | + compile-full-aux Ty A Ty1 A1, |
| 156 | + compile-full-aux-close F Ty A1 F1 A2. |
| 157 | + |
| 158 | + compile-full-aux (match T Rty B) A (match T1 Rty1 B1) A3 :- !, |
| 159 | + compile-full-aux T A T1 A1, |
| 160 | + compile-full-aux Rty A1 Rty1 A2, |
| 161 | + std.fold-map B A2 compile-full-aux B1 A3. |
| 162 | + |
| 163 | + compile-full-aux (uvar _ _ as X) A X A :- !. |
| 164 | + compile-full-aux X A X A :- name X, !. |
| 165 | + |
| 166 | + compile-full-aux A B _ _ :- coq.error "Fail to compile-full-aux" A B. |
| 167 | + |
| 168 | + % takes a term t and returns a term t' and a list of links. |
| 169 | + pred compile-full i:term, o:term, o:list prop. |
| 170 | + compile-full Goal Goal' Links :- compile-full-aux Goal [] Goal' Links. |
| 171 | + } |
| 172 | + |
| 173 | + pred goal' i:term, o:term, o:list prop. |
| 174 | + :name "compile-goal'" |
| 175 | + goal' Goal Goal' Links :- |
| 176 | + goall.compile-full-aux Goal [] Goal' Links. |
| 177 | + } |
| 178 | +} |
0 commit comments