namespace tc { shorten tc.{r-ar, range-arity}. namespace compile { namespace instance { func is-name term ->. func is-uvar term ->. % [name-pair H0 Hn Ar] % the variable H0 (applied 0 time) is associated to the variable Hn which is % applied Ar times. pred name-pair o:term, o:term, o:nat. namespace decompile { func decompile-term-aux term, pair (list term) (list prop) -> term, pair (list term) (list prop). decompile-term-aux X A Y A :- name X, !, X = Y, !. % avoid loading "decompile-term-aux x A x A" at binders decompile-term-aux (global _ as C) A C A :- !. decompile-term-aux (pglobal _ _ as T) L T' L :- !, copy T T', !. decompile-term-aux (sort _ as T) L T' L :- !, copy T T', !. decompile-term-aux (uvar as X) L X L :- !. decompile-term-aux (primitive _ as P) L P L :- !. decompile-term-aux (tc.maybe-eta-tm T S) (pr [X|XS] L1) Y (pr XS' [NL | L2]) :- !, name Y X S, decompile-term-aux T (pr XS L1) T' (pr XS' L2), NL = tc.link.eta Y T'. decompile-term-aux (tc.prod-range T _) A T' A' :- !, decompile-term-aux T A T' A'. % Maybe-llam when H is a coq unif variable quantified in the instance type % In the following instance, X is a HO variable applied to a constant (not a name) % Instance i : forall (X : T1 -> T2) (a : T1), c (X a). decompile-term-aux (tc.maybe-llam-tm (app[app[H | PF] | NPF]) S) (pr [X|XS] L1) Y (pr XS' [NL | L2]) :- not (var H), !, % is-uvar H, holds name Y X S, length-nat PF Len, std.assert!(name-pair H V Len) "[TC] fail to find name-pair", name Hd V PF, std.fold-map NPF (pr XS L1) decompile-term-aux Tl (pr XS' L2), NL = tc.link.llam Y (app [Hd|Tl]). % Maybe-llam when H is a hole appearing in the shelved goals % This happens when the instance to be compiled comes from the context % Example: Goal exists (X : T1 -> T2), forall a, c (X a) -> ... % intros; eexists. (* In the context we have the instance `H: c (?X a)` *) decompile-term-aux (tc.maybe-llam-tm (app[app[H | PF] | NPF]) S) A Z (pr XS' [NL | L3]) :- !, var H _ Scope, !, std.append Scope S S', prune Z S', tc.compile.goal.make-pairs [T] Pairs, % We build on the fly the eta-links for T (Pairs => (tc.compile.goal.build-eta-links-of-vars [T] P, tc.compile.goal.get-uva-pair-arity T PF Y)), std.fold-map NPF A decompile-term-aux Tl (pr XS' L2), std.append P L2 L3, NL = tc.link.llam Z (app [Y|Tl]). decompile-term-aux (fun Name Ty Bo) (pr XS L) (fun Name Ty' Bo') (pr XS2 L3) :- !, (pi x\ is-name x => decompile-term-aux (Bo x) (pr XS []) (Bo' x) (pr XS1 (L1x x))), close-term-no-prune-ty L1x Ty L1, decompile-term-aux Ty (pr XS1 L) Ty' (pr XS2 L2), std.append L1 L2 L3. decompile-term-aux (prod Name Ty Bo) (pr XS L) (prod Name Ty' Bo') (pr XS2 L3) :- !, (pi x\ is-name x => decompile-term-aux (Bo x) (pr XS []) (Bo' x) (pr XS1 (L1x x))), close-term-no-prune-ty L1x Ty L1, decompile-term-aux Ty (pr XS1 L) Ty' (pr XS2 L2), std.append L1 L2 L3. % HO var when H is a quantified variable in the instance type being in PF % Example: Instance i: forall (X : T1 -> T2), (forall a, c1 (X a)) -> c2. % Note: X is the HO var taken into account which is applied to the list of % distinct_names [a] decompile-term-aux (app [H|L]) N R N :- is-uvar H, std.forall L is-name, % Not needed, since precompile does this check distinct_names L, !, % Not needed, since precompile does this check length-nat L Len, std.assert! (name-pair H V Len) "[TC] name-pair not found", name R V L. % HO var when H is a hole appearing in the shelved goals decompile-term-aux (app [T|L]) (pr A B) Z (pr A B') :- var T _ Scope, std.forall L is-name, % Not needed, since decompile for llam leaves only PF distinct_names L, !, % Not needed, since decompile for llam leaves only PF std.append Scope L Scope', prune Z Scope', tc.compile.goal.make-pairs [T] Pairs, % We build on the fly the eta-links for `T` (Pairs => (tc.compile.goal.build-eta-links-of-vars [T] P, tc.compile.goal.get-uva-pair-arity T L Y)), var Z Y Scope', std.append P B B'. decompile-term-aux (app L) PR (app L') PR' :- !, std.fold-map L PR decompile-term-aux L' PR'. decompile-term-aux (let N T Ty Bo) PR (let N T' Ty' Bo') PR3 :- !, decompile-term-aux T PR T' PR1, decompile-term-aux Ty PR1 Ty' PR2, (pi x\ is-name x => decompile-term-aux (Bo x) PR2 (Bo' x) PR3). decompile-term-aux A B _ _ :- coq.error "[TC] cannot decompile-term-aux of" A B. :functional pred decompile-term i:list term, o:list term, i:term, o:term, o:list prop. decompile-term L L' T R Links :- decompile-term-aux T (pr L []) R (pr L' Links). } % TODO: also replace (sort (typ X)) and (pglobal _ X) with holes in the place of X func clean-term term -> term. clean-term A B :- (pi t s r \ copy (tc.maybe-eta-tm t s) r :- !, copy t r, !) => (pi t s r \ copy (tc.prod-range t s) r :- !, copy t r, !) => (pi t s r \ copy (tc.maybe-llam-tm t s) r :- !, copy t r, !) => std.assert! (copy A B) "[TC] clean-term error". func main nat, % the number of problematic terms term, % the type of the instance term, % the global gref of the instance list univ, % the list of univ variable to be replaced with elpi fresh vars list univ-instance % the list of univ-instance to be replaced with elpi fresh vars -> prop. % the compiled clause for the instance main N Ty ProofHd [] [] Clause :- add-pi-problematic-terms N [] Ty ProofHd Clause. main N Ty ProofHd [Univ | UnivL] UnivInstL (pi x\ Clause x) :- !, pi x\ (copy (sort (typ Univ)) (sort (typ x)) :- !) => main N Ty ProofHd UnivL UnivInstL (Clause x). main N Ty ProofHd [] [UnivInst | UnivInstL] (pi x\ Clause x) :- !, pi x\ (copy (pglobal A UnivInst) (pglobal A x) :- !) => main N Ty ProofHd [] UnivInstL (Clause x). % Start to charge the right number of pi for the resulting clause: % This number is equal to the number of problematic terms + number of subterms with shape `sort _` and `pglobal _ _` func add-pi-problematic-terms nat, % the number of pi to quantify list term, % the list of quantified pi term, % the fuel of the compilation (the type of the instance) term % the global gref of the current instance -> prop. % the compiled clause for the instance add-pi-problematic-terms z L Ty ProofHd Clause :- compile-ty L _ ProofHd tt Ty [] [] Clause. add-pi-problematic-terms (s N) L Ty ProofHd (pi x\ Clause x) :- pi x\ is-uvar x => add-pi-problematic-terms N [x|L] Ty ProofHd (Clause x). % Builds a eta link between the varibale A whose type _must_ be of type `prod` % A is linked with B : A =_eta (fun (x : Ty) => B_x) func make-eta-link-aux term, % A : The variable to eta-expand term, % prod _ Ty Bo : The type of A pair term name, % pr B Bn : The eta-expanded version of B with its name (they are fresh names) list term % L : The list of name in the scope of A and B -> prop, % Link : The new eta-link term, % Ty' : The cleaned version of the binder in Ty (term -> term). % Bo : the body of the type of A make-eta-link-aux A (prod _ Ty Bo) (pr B Name) L Link Ty' Bo :- !, clean-term Ty Ty', name A' A {std.rev L}, Link = tc.link.eta A' (fun Name Ty' B'), pi x\ sigma L'\ std.rev [x|L] L', name (B' x) B L'. % Going under prod-range make-eta-link-aux A (tc.prod-range Prod _) BN L Link Ty' Bo :- !, make-eta-link-aux A Prod BN L Link Ty' Bo. % The type of a higher order variable can be hidden behind a definition % In this case we unfold this definition to get the prod constructor make-eta-link-aux A T BN L Link Ty' Bo :- coq.safe-dest-app T Hd Ag, (@redflags! coq.redflags.delta => coq.reduction.lazy.whd Hd Hd'), not (Hd = Hd'), !, coq.mk-app Hd' Ag TT', make-eta-link-aux A TT' BN L Link Ty' Bo. make-eta-link-aux _ T _ _ _ _ _ :- coq.error "[TC] make-eta-link-aux of" T. % Create spine of eta-links :index (_ _ 1) func make-eta-link term, term, list (pair term name), list term, list prop -> prop. make-eta-link P PTy [Hd] L Links (do [Link1|Links]) :- !, make-eta-link-aux P PTy Hd L Link1 _ _. make-eta-link P PTy [(pr B _ as Hd)|Tl] L Links (pi x\ decl x `x` PTy' => Res x) :- !, make-eta-link-aux P PTy Hd L Link1 PTy' Bo, pi x\ make-eta-link B (Bo x) Tl [x|L] [Link1|Links] (Res x). make-eta-link P PTy _ _ _ _ :- coq.error "[TC] make-eta-link error : empty list of pairs" P PTy. % Accumulates pi for eta-links :index (_ 1) func add-link-eta-dedup (func list prop -> prop), range-arity, term, term, (list (pair term name)), list prop -> prop. % Base case when the variable is always used at same arity add-link-eta-dedup F (r-ar _ z) _ _ [] PremR Clause :- !, F PremR Clause. add-link-eta-dedup F (r-ar _ z) P Pty Acc PremR Clause :- !, make-eta-link P Pty Acc [] [] LinkEtaDedup, F [LinkEtaDedup|PremR] Clause. add-link-eta-dedup F (r-ar M (s N)) P PTy Acc PremR (pi x y\ Clause x y) :- !, pi x y\ name-pair P x (s N) => is-uvar x => add-link-eta-dedup F (r-ar M N) P PTy [pr x y|Acc] PremR (Clause x y). add-link-eta-dedup _ Ar P PTy _ _ _ :- coq.error "[TC] add-link-eta-dedup error" Ar P PTy. :functional pred compile-premise i:list term, o:list term, i:term, i:term, i:term, i:bool, i:term, i:list term, i:list prop, o:prop. compile-premise L L2 P PTy ProofHd IsPositive ITy ProofTlR PremR Clause :- ((pi a b c\ tc.get-TC-of-inst-type (tc.prod-range a c) b :- !, tc.get-TC-of-inst-type a b) => tc.get-TC-of-inst-type PTy TC), !, compile-ty L L1 P {neg IsPositive} PTy [] [] NewPrem, if (tc.class TC _ tc.deterministic _) (NewPrem' = std.once NewPrem) (NewPrem' = NewPrem), !, compile-ty L1 L2 ProofHd IsPositive ITy ProofTlR [NewPrem' | PremR] Clause. compile-premise L L1 _ _ ProofHd IsPositive ITy ProofTlR PremR Clause :- compile-ty L L1 ProofHd IsPositive ITy ProofTlR PremR Clause. :index (_ _ _ _ 1) func compile-ty list term, list term, term, bool, term, list term, list prop -> prop. compile-ty L L1 ProofHd IsPositive (tc.prod-range (prod N Ty Bo) Arity) ProofTlR PremR Clause :- !, std.do![ if (IsPositive = tt) (Clause = (pi x\ C x), E = is-uvar) (clean-term Ty Ty', Clause = (pi x\ decl x N Ty' => C x), E = is-name), pi p\ sigma F\ F = compile-premise L L1 p Ty ProofHd IsPositive (Bo p) [p|ProofTlR], decl p N Ty' => name-pair p p z => E p => add-link-eta-dedup F Arity p Ty [] PremR (C p) ]. compile-ty L L1 ProofHd IsPositive (let _ Ty T Bo) ProofTlR PremR Clause :- !, if (IsPositive = tt) (Clause = (pi x\ C x), E = is-uvar) (clean-term Ty Ty', Clause = (pi x\ decl x N Ty' => C x), E = is-name), !, pi p\ sigma F NewPrem\ (decl p N Ty' :- !) => (E p :- !) => ( NewPrem = tc.link.unif-eq T p, compile-premise L L1 p Ty ProofHd IsPositive (Bo p) ProofTlR [NewPrem | PremR] (C p) ). compile-ty L L2 ProofHd IsPositive Goal ProofTlR PremR Clause :- std.do![ coq.mk-app ProofHd {std.rev ProofTlR} Proof, decompile.decompile-term L L1 Proof Proof' Prem1, decompile.decompile-term L1 L2 Goal Goal' Prem2, compile-conclusion IsPositive Goal' Proof' Prem2 Prem1 {std.rev PremR} Clause ]. func compile-conclusion bool, % tt if the term is in positive position term, % the goal (invariant: it is a constant or a application) term, % the proof list prop, % the list of HOPremises in input mode list prop, % the list of HOPremises in output mode list prop % the premises -> prop. % the compiled clause for the instance compile-conclusion tt Goal Proof HOPremisesIn HOPremisesOut Premises Clause :- std.append {std.append HOPremisesIn Premises} HOPremisesOut AllPremises, tc.make-tc Goal Proof AllPremises tt Clause. compile-conclusion ff Goal Proof HOPremisesIn HOPremisesOut Premises Clause :- tc.make-tc Goal Proof Premises ff Clause1, Clause = (do HOPremisesIn, Clause1, do HOPremisesOut). func context goal-ctx -> list prop. context [] []. context [X | Xs] [Clause | ResTl] :- (decl Var _ Ty = X; def Var _ Ty _ = X), tc.is-instance-term Ty, !, std.assert! (compile.instance Ty Var Clause) "[TC] cannot compile instance of context", context Xs ResTl. context [_ | Tl] L :- context Tl L. } % build a list of Clauses of type tc to be temporarly added to the % database, used in theorems having assumptions. func context goal-ctx -> list prop. :name "tc-compile-context" context Ctx Clauses :- std.assert! (instance.context Ctx Clauses) "[TC] cannot compile context". func instance term, term -> prop. instance Ty ProofHd Clause :- tc.time-it tc.oTC-time-compile-instance ( tc.normalize-ty Ty Ty', tc.precomp.instance Ty' Ty'' N UnivConst UnivInst, instance.main N Ty'' ProofHd UnivConst UnivInst Clause ) "Compile Instance", !. func instance-gr gref -> prop. % If the instance is polymorphic, we wrap its gref into the pglobal constructor instance-gr InstGR (pi x\ Clause x) :- coq.env.univpoly? InstGR _, !, coq.env.typeof InstGR Ty, pi x\ tc.compile.instance Ty (pglobal InstGR x) (Clause x). instance-gr InstGR Clause :- coq.env.typeof InstGR Ty, tc.compile.instance Ty (global InstGR) Clause. namespace goal { % [uvar-pair V1 Ty V2] List of uvar for link-eta-dedup % V1 has arity n and V2 has arity n+1 % If V1 has type A -> B, then A = Ty pred uvar-pair i:term, o:term, o:term. % Type Var Cnt uvar-pair-list func make-pairs-aux term, term -> list prop. make-pairs-aux (prod _ Ty Bo) V [pi x\ uvar-pair x Ty X' :- x == V, ! | L] :- !, pi x\ make-pairs-aux (Bo x) X' L. make-pairs-aux _ _ []. func make-pairs list term -> list prop. make-pairs [] [] :- !. make-pairs [X|Xs] L :- !, coq.typecheck X Ty ok, make-pairs-aux Ty X L', make-pairs Xs L'', std.append L' L'' L. func get-uva-pair-arity term, list term -> term. get-uva-pair-arity X [] X :- !. get-uva-pair-arity X [_|L] Z :- uvar-pair X _ Y, !, get-uva-pair-arity Y L Z. func decompile-problematic-term term, list prop -> term, list prop. decompile-problematic-term (tc.maybe-eta-tm T S) L V [tc.link.eta V T' | L2] :- prune V S, !, fold-map T L T' L2. decompile-problematic-term (tc.prod-range T _) A T' A' :- !, fold-map T A T' A'. decompile-problematic-term (tc.maybe-llam-tm (app [app[H|S] | NPF]) Sc) L Z [NL|L'] :- !, prune Z Sc, get-uva-pair-arity H S Y, std.fold-map NPF L fold-map Tl L', NL = tc.link.llam Z (app[Y | Tl]). % TODO: complete this fold decompile-problematic-term (app[X|S]) L Z L :- var X _ Scope, std.append Scope S Scope', distinct_names Scope', !, get-uva-pair-arity X S Y, prune Z Scope', var Z Y Scope'. decompile-problematic-term A L A L :- var A, !. decompile-problematic-term (fun N Ty Bo) L (fun N Ty' Bo') L3 :- fold-map Ty L Ty' L1, (pi x\ fold-map (Bo x) [] (Bo' x) (Lx x)), close-term-no-prune-ty Lx Ty' L2, std.append L1 L2 L3. decompile-problematic-term (prod N Ty Bo) L (prod N Ty' Bo') L3 :- (pi x\ fold-map (Bo x) [] (Bo' x) (Lx x)), close-term-no-prune-ty Lx Ty L1, fold-map Ty L Ty' L2, std.append L2 L1 L3. func compile term, list prop -> term, list prop. compile T L T' L' :- (pi t l t' l'\ fold-map t l t' l' :- decompile-problematic-term t l t' l', !) => fold-map T L T' L'. % Uva Binders LinkEta func build-eta-links-of-vars-aux term, list term -> list prop. build-eta-links-of-vars-aux Old L [Hd | Xs] :- uvar-pair Old Ty Next, !, prune OldScope L, prune Name L, var OldScope Old L, Hd = tc.link.eta OldScope (fun Name Ty (x\ NextScope x)), pi x\ sigma L'\ std.append L [x] L', prune (NextScope x) L', var (NextScope x) Next L', build-eta-links-of-vars-aux Next L' (Ys x), !, sigma Closed\ (close-term-no-prune-ty Ys Ty Closed), Xs = Closed. build-eta-links-of-vars-aux _ _ []. func build-eta-links-of-vars list term -> list prop. build-eta-links-of-vars [] []. build-eta-links-of-vars [V|Vars] L :- var V Hd S, build-eta-links-of-vars-aux Hd S L', build-eta-links-of-vars Vars L'', std.append L' L'' L. } % Goal Goal' Links func goal term -> term, list prop. :name "compile-goal" goal Goal Goal' Links :- tc.precomp.goal Goal GoalPrecomp Vars, !, goal.make-pairs Vars Pairs, Pairs => ( std.assert!(goal.build-eta-links-of-vars Vars EtaLinks) "[TC] cannot build eta-links", std.assert!(goal.compile GoalPrecomp EtaLinks Goal' Links) "[TC] cannot compile goal" ). } }