namespace tc { shorten tc.{r-ar, range-arity}. namespace precomp { namespace instance { % Tells if the current name is a bound variables pred is-name o:term. % Tells if the current name stands for a uvar pred is-uvar o:term. :index (_ _ 1) func may-contract-to list term, term, term ->. may-contract-to _ N N :- !. may-contract-to L N (app [V|S]) :- var V, !, std.forall [N|L] (x\ std.exists! S (may-contract-to [] x)). may-contract-to L N (app [V|S]) :- is-uvar V, !, std.forall [N|L] (x\ std.exists! S (may-contract-to [] x)). may-contract-to L N (app [N|A]) :- std.length A {std.length L}, std.forall2 {std.rev L} A (may-contract-to []). may-contract-to L N (fun _ _ B) :- pi x\ may-contract-to [x|L] N (B x). :index (_ 1) func occurs-rigidly term, term ->. occurs-rigidly N N :- name N, !. occurs-rigidly _ (app [N|_]) :- is-uvar N, !, fail. occurs-rigidly _ (app [N|_]) :- var N, !, fail. occurs-rigidly N (app A) :- std.exists! A (occurs-rigidly N). occurs-rigidly N (fun _ _ B) :- pi x\ occurs-rigidly N (B x). func maybe-eta-aux term, list term ->. maybe-eta-aux (app[V|S]) L :- is-uvar V, !, std.forall L (x\ std.exists! S (y\ may-contract-to [] x y)). maybe-eta-aux (app[V|S]) L :- var V, !, std.forall L (x\ std.exists! S (y\ may-contract-to [] x y)). maybe-eta-aux (app [_|A]) L :- SplitLen is {std.length A} - {std.length L}, split-at-not-fatal SplitLen A HD TL, std.forall L (x\ not (std.exists! HD (occurs-rigidly x))), std.forall2 {std.rev L} TL (may-contract-to []). maybe-eta-aux (fun _ _ B) L :- pi x\ maybe-eta-aux (B x) [x|L]. func maybe-eta term ->. maybe-eta (fun _ _ B) :- pi x\ maybe-eta-aux (B x) [x]. func free-var -> list term. free-var L :- std.findall (is-name _) T, std.map T (x\y\ x = is-name y) L. func split-pf list term, list term -> list term, list term. split-pf [] _ [] [] :- !. split-pf [X|Xs] Old [X|Ys] L :- is-name X, not (std.mem! Old X), !, split-pf Xs [X|Old] Ys L. split-pf Xs _ [] Xs. kind positivity type. type is_pos positivity. type is_neg positivity. type is_neg_fix positivity. :index (1 _) func neg positivity -> positivity. neg is_pos is_neg :- !. neg is_neg is_pos :- !. neg is_neg_fix is_neg_fix :- !. macro @max-min :- r-ar inf z. func min-max-nat range-arity, range-arity -> range-arity. min-max-nat (r-ar A B) (r-ar A' B') (r-ar A'' B'') :- !, min-nat A A' A'', max-nat B B' B''. % TODO: this is incomplete: it lacks of some term constructors :index (_ 1) func get-range-arity-aux term, term -> range-arity. get-range-arity-aux N N (r-ar z z) :- !. get-range-arity-aux _ N @max-min :- name N, !. get-range-arity-aux T (app [T|L]) R :- !, length-nat L Len, std.fold L (r-ar Len Len) (x\y\w\ sigma M\ get-range-arity-aux T x M, min-max-nat y M w) R. get-range-arity-aux T (app [_|L]) R :- !, std.fold L @max-min (x\y\w\ sigma M\ get-range-arity-aux T x M, min-max-nat y M w) R. get-range-arity-aux T (fun _ Ty B) R2 :- !, get-range-arity-aux T Ty R, (pi x\ get-range-arity-aux T (B x) R1), min-max-nat R R1 R2. get-range-arity-aux T (prod _ Ty B) R2 :- !, get-range-arity-aux T Ty R, (pi x\ get-range-arity-aux T (B x) R1), min-max-nat R R1 R2. get-range-arity-aux _ (global _) @max-min :- !. get-range-arity-aux _ uvar @max-min :- !. get-range-arity-aux _ (sort _) @max-min :- !. get-range-arity-aux _ (pglobal _ _) @max-min :- !. get-range-arity-aux X (let _ T Ty B) R4 :- !, get-range-arity-aux X Ty R1, get-range-arity-aux X T R2, (pi x\ get-range-arity-aux T (B x) R3), min-max-nat R1 R2 R12, min-max-nat R12 R3 R4. get-range-arity-aux A B _ :- coq.error "Count maximal arity failure" A B. func get-range-arity term, term, term -> range-arity. get-range-arity _ Ty _ (r-ar z N) :- tc.get-TC-of-inst-type Ty _, !, count-prod Ty N. get-range-arity B _ T N :- !, get-range-arity-aux B T N. :index (_ 1) func precompile-aux positivity, term, nat -> term, nat. precompile-aux _ X A Y A :- name X, !, X = Y, !. % avoid loading "precompile-aux x A x A" at binders precompile-aux _ (global _ as C) A C A :- !. precompile-aux _ (pglobal _ _ as C) A C A :- !. precompile-aux _ (sort _ as C) A C A :- !. % Detect maybe-eta term % TODO: should I precompile also the type of the fun and put it in the output term precompile-aux _ (fun Name Ty B as T) N (tc.maybe-eta-tm (fun Name Ty B') Scope) (s M) :- maybe-eta T, !, free-var Scope, precompile-aux is_neg_fix Ty N _ N', (pi x\ is-name x => precompile-aux is_neg_fix (B x) N' (B' x) M). precompile-aux _ (app [X|XS]) N (tc.maybe-llam-tm (app [app[X | PF] | NPF1]) Scope) (s M) :- if (is-uvar X) (Sc = []) (var X _ Sc), split-pf XS Sc PF NPF, not (NPF = []), !, % else XS is a list of distinct names, i.e. `app [X|XS]` is in PF free-var Scope, std.fold-map NPF N (precompile-aux is_neg_fix) NPF1 M. % Charge if we work with unification variable or local name % And returns the subterms is a prod-range precompile-aux IsP (prod Name Ty B) N (tc.prod-range (prod Name Ty' B') MaxAr) P :- !, std.assert! (pi x\ get-range-arity x Ty (B x) MaxAr) "[TC] get-range-arity should not fail", if (IsP = is_pos) (C = x\ is-uvar x) (C = x\ is-name x), std.assert! (pi x\ C x => precompile-aux IsP (B x) N (B' x) M) "[TC] should not fail", precompile-aux {neg IsP} Ty M Ty' P. % Working with fun precompile-aux _ (fun N T F) A (fun N T1 F1) A2 :- !, precompile-aux is_neg_fix T A T1 A1, pi x\ is-name x => precompile-aux is_neg_fix (F x) A1 (F1 x) A2. precompile-aux _ (app L) A (app L1) A1 :- !, std.fold-map L A (precompile-aux is_neg_fix) L1 A1. precompile-aux _ X A X A :- var X, !. precompile-aux _ (let N T Ty Bo) A (let N T' Ty' Bo') A3 :- !, precompile-aux is_neg_fix T A T' A1, precompile-aux is_neg_fix Ty A1 Ty' A2, pi x\ is-name x => precompile-aux is_neg_fix (Bo x) A2 (Bo' x) A3. % TODO: what about the following constructors? % precompile-aux IsP (let N T B F) A (let N T1 B1 F1) A3 :- !, % precompile-aux IsP T A T1 A1, precompile-aux IsP B A1 B1 A2, pi x\ is-name x => precompile-aux IsP (F x) A2 (F1 x) A3. % precompile-aux IsP (fix N Rno Ty F) A (fix N Rno Ty1 F1) A2 :- !, % precompile-aux IsP Ty A Ty1 A1, pi x\ is-name x => precompile-aux IsP (F x) A1 (F1 x) A2. % precompile-aux IsP (match T Rty B) A (match T1 Rty1 B1) A3 :- !, % precompile-aux IsP T A T1 A1, precompile-aux IsP Rty A1 Rty1 A2, std.fold-map B A2 (precompile-aux IsP) B1 A3. precompile-aux _ (primitive _ as C) A C A :- !. % precompile-aux IsP (uvar M L as X) A W A1 :- var X, !, std.fold-map L A (precompile-aux IsP) L1 A1, coq.mk-app-uvar M L1 W. % % when used in CHR rules % precompile-aux IsP (uvar X L) A (uvar X L1) A1 :- std.fold-map L A (precompile-aux IsP) L1 A1. func get-univ-instances term -> list univ-instance. get-univ-instances T L :- (pi x L\ fold-map (pglobal _ x) L _ [x | L] :- !) => fold-map T [] _ L, !. func get-univ term -> list univ. get-univ T L :- coq.univ.variable.set.elements {coq.univ.variable.of-term T} Vars, std.map Vars (x\r\ coq.univ.variable r x) L. } /* [tc.precomp.instance T T' N] Returns T' N from T, where: T' is obtained by the replacement of - all maybe-eta term `t1` with (tc.maybe-eta-tm `t1` `s`) where `s` = FV(`t1`) ==> This helps knowing if a subterm should be replaced with a `eta-link` - all `prod _ Ty (x\ Bo x)` with (tc.prod-range (prod _ Ty (x\ Bo x)) N), where N is represent the "maximal" application of `x` in `Bo` for example: let Ty = {{Type -> Type -> Type -> Type -> Type}}, and Bo = x\ c1 (x nat bool) (x nat) (x nat nat bool) the term `prod _ Ty Bo` is replaced with (tc.prod-range (prod _ T Bo) 3) since x is applied at most 3 times in Bo ==> This helps charging the right number of `eta-link` for map-deduplication rule N is the number of problematic terms in T */ func instance term -> term, nat, list univ, list univ-instance. instance T T' N UnivConstL UnivInstL :- tc.precomp.instance.get-univ T UnivConstL, tc.precomp.instance.get-univ-instances T UnivInstL, std.assert!(instance.precompile-aux instance.is_pos T z T' N) "[TC] cannot precompile instance". namespace goal { :index (_ _ 1) func may-contract-to list term, term, term ->. may-contract-to _ N N :- !. % TODO: here we should do var V _ Scope and use scope: N can be in Scope but not in S may-contract-to L N (app [V|S]) :- var V, !, std.forall [N|L] (x\ std.exists! S (may-contract-to [] x)). may-contract-to L N (app [N|A]) :- std.length A {std.length L}, std.forall2 {std.rev L} A (may-contract-to []). may-contract-to L N (fun _ _ B) :- pi x\ may-contract-to [x|L] N (B x). :index (_ 1) func occurs-rigidly term, term ->. occurs-rigidly N N :- name N, !. occurs-rigidly _ (app [N|_]) :- var N, !, fail. occurs-rigidly N (app A) :- std.exists! A (occurs-rigidly N). occurs-rigidly N (fun _ _ B) :- pi x\ occurs-rigidly N (B x). func maybe-eta-aux term, list term ->. % TODO: here we should do var V _ Scope and use Scope: an elt in L can appear in Scope maybe-eta-aux (app[V|S]) L :- var V, !, std.forall L (x\ std.exists! S (y\ may-contract-to [] x y)). maybe-eta-aux (app [_|A]) L :- SplitLen is {std.length A} - {std.length L}, split-at-not-fatal SplitLen A HD TL, std.forall L (x\ not (std.exists! HD (occurs-rigidly x))), std.forall2 {std.rev L} TL (may-contract-to []). maybe-eta-aux (fun _ _ B) L :- pi x\ maybe-eta-aux (B x) [x|L]. func maybe-eta term ->. maybe-eta (fun _ _ B) :- pi x\ maybe-eta-aux (B x) [x]. func split-pf list term, list term -> list term, 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. func precompile-aux term, list term -> term, list term. precompile-aux X A Y A :- name X, !, X = Y, !. % avoid loading "precompile-aux x A x A" at binders precompile-aux (global _ as C) A C A :- !. precompile-aux (pglobal _ _ as C) A C A :- !. precompile-aux (sort _ as C) A C A :- !. % Detect maybe-eta term precompile-aux (fun Name Ty B as T) N (tc.maybe-eta-tm (fun Name Ty' B') Scope) M :- maybe-eta T, !, names Scope, (pi x\ precompile-aux (B x) N (B' x) M'), precompile-aux Ty M' Ty' M. % Detect maybe-beta term precompile-aux (app [X|XS]) N (tc.maybe-llam-tm (app [app[X | PF] | NPF1]) Scope1) [X|M] :- var X _ Scope, split-pf XS Scope PF NPF, not (NPF = []), !, % else XS is a list of distinct names, i.e. `app [X|XS]` is in PF names Scope1, std.fold-map NPF N precompile-aux NPF1 M. % In the goal there are precompile-aux (prod Name Ty B) N (tc.prod-range (prod Name Ty' B') (r-ar z MaxAr)) P :- !, count-prod Ty MaxAr, std.assert! (pi x\ precompile-aux (B x) N (B' x) M) "[TC] should not fail", precompile-aux Ty M Ty' P. % Working with fun precompile-aux (fun N T F) A (fun N T F1) A2 :- !, A = A1, /*precompile-aux IsP T A T1 A1,*/ pi x\ precompile-aux (F x) A1 (F1 x) A2. precompile-aux (app L) A (app L1) A1 :- !, std.fold-map L A precompile-aux L1 A1. precompile-aux (let N T B F) A (let N T1 B1 F1) A3 :- !, precompile-aux T A T1 A1, precompile-aux B A1 B1 A2, pi x\ precompile-aux (F x) A2 (F1 x) A3. precompile-aux (fix N Rno Ty F) A (fix N Rno Ty1 F1) A2 :- !, precompile-aux Ty A Ty1 A1, pi x\ precompile-aux (F x) A1 (F1 x) A2. precompile-aux (match T Rty B) A (match T1 Rty1 B1) A3 :- !, precompile-aux T A T1 A1, precompile-aux Rty A1 Rty1 A2, std.fold-map B A2 precompile-aux B1 A3. precompile-aux (primitive _ as C) A C A :- !. precompile-aux X A X [X|A] :- var X, !. } func goal term -> term, list term. goal T T' Vars' :- std.assert!(goal.precompile-aux T [] T' Vars) "[TC] cannot precompile goal", undup-same Vars Vars'. } }