/* license: GNU Lesser General Public License Version 2.1 or later */ /* ------------------------------------------------------------------------- */ namespace tc { namespace lettify { func replace-args term, list term, list term, (func term -> term) -> term. replace-args Hd [] L K R :- std.rev L L1, K (app [Hd|L1]) R. replace-args Hd [X|Xs] L K (let `_` Ty X T') :- coq.typecheck X Ty ok, pi x\ replace-args Hd Xs [x|L] K (T' x). func replace-top-level list term, term, list term -> term. replace-top-level [] Hd L (app [Hd | L']) :- std.rev L L'. replace-top-level [app[X|Xs]|As] Hd L R :- replace-args X Xs [] (x\ replace-top-level As Hd [x|L]) R. replace-top-level [(fun _ _ _ as A)|As] Hd L (let `_` Ty A T') :- coq.typecheck A Ty ok, pi x\ replace-top-level As Hd [x|L] (T' x). func main term -> term. main (prod N Ty Bo) (prod N Ty Bo') :- !, pi x\ main (Bo x) (Bo' x). main (app [X|Xs]) T :- is-instance-term X, !, replace-top-level Xs X [] T. main T T. } % return if a gref is an existsing instance % TODO: this could be replaced with an API % coq.TC.get-class-of-inst i:gref, o:gref func existing-instance gref ->. existing-instance InstGR :- coq.env.typeof InstGR InstTy, get-TC-of-inst-type InstTy ClassGR, coq.TC.class? ClassGR, coq.TC.db-for ClassGR DB, std.mem! DB (tc-instance InstGR _). func under-p-global term -> gref. under-p-global (global GR) GR. under-p-global (pglobal GR _) GR. func class-gref term -> gref. class-gref T ClassGR :- under-p-global T ClassGR, coq.TC.class? ClassGR. % returns the TC from the type of an instance % TODO: This could be replaced with an api % coq.TC.get-class-of-inst i:gref, o:gref func get-TC-of-inst-type term -> gref. get-TC-of-inst-type (prod _ _ A) ClassGR:- !, pi x\ get-TC-of-inst-type (A x) ClassGR. get-TC-of-inst-type (let _ _ _ A) ClassGR:- !, pi x\ get-TC-of-inst-type (A x) ClassGR. get-TC-of-inst-type T ClassGR :- coq.safe-dest-app T HD _, not (var HD), class-gref HD ClassGR. func instances-of-current-section list gref ->. :name "MySectionEndHook" instances-of-current-section InstsFiltered :- coq.env.current-section-path SectionPath, std.findall (tc.instance SectionPath _ _ _) Insts, coq.env.section-variables SectionVars, std.map-filter Insts (x\r\ sigma X\ tc.instance _ r _ _ = x, const X = r, not(std.mem SectionVars X)) InstsFiltered. func is-instance-gr gref ->. is-instance-gr GR :- coq.env.typeof GR Ty, is-instance-term Ty. func is-instance-term term ->. is-instance-term T :- get-TC-of-inst-type T TC, coq.TC.class? TC. % adds a clause to the tc.db DB at the passed grafting func add-tc-db id, grafting, prop ->. add-tc-db _ _ Clause :- not (ground_term Clause), coq.error "[TC] anomaly: open rule:" Clause, !. add-tc-db ClauseName Graft Clause :- coq.elpi.accumulate _ "tc.db" (clause ClauseName Graft Clause), !. add-tc-db _ _ Clause :- coq.error "cannot add " PR " to tc.db". % takes a tc-instance and return the gref of the inst func inst->gref tc-instance -> gref. inst->gref (tc-instance Res _) Res. % returns all the instances of the passed ClassName func get-inst-by-tc-name gref -> list gref. get-inst-by-tc-name TC GRL:- coq.TC.db-for TC Inst, std.map Inst inst->gref GRL', std.rev GRL' GRL. func app-has-class term ->. app-has-class T :- get-TC-of-inst-type T Hd, coq.TC.class? Hd. % TC preds are on the form tc-[PATH_TO_TC].tc-[TC-Name] func gref->pred-name gref -> string. gref->pred-name Gr S :- if (tc.is-option-active tc.oTC-clauseNameShortName) (Path = "") (coq.gref->path Gr [Hd | Tl], if (Hd = "Coq") (Hd' = "Corelib") (Hd' = Hd), std.string.concat "." [Hd'|Tl] Path', Path is Path' ^ ".tc-"), % CAVEAT : Non-ascii caractars can't be part of a pred % name, we replace ö with o rex.replace "ö" "o" {coq.gref->id Gr} GrStr, S is "tc-" ^ Path ^ GrStr. func get-mode gref -> list string. get-mode ClassGR M :- tc.class ClassGR _ _ M, !. get-mode ClassGR _ :- coq.error "[TC]" ClassGR "is an unknown class". /* [make-tc.aux B Sol Head Body Rule] builds the rule with the given Head and body paying attention to the positivity of the clause Note: if the Rule being constructed is negative (B = ff), then Rules returns a solution Sol used inside the proof. If the solution is already given, we do not run the premise. This would ask Sol to be ground (ground_term S). Here, for performance issues, we simply check that the solution is not a flexible term */ :index (1 _ _ 1) func make-tc.aux bool, term, prop, list prop -> prop. make-tc.aux tt _ Head [] Head :- !. make-tc.aux ff Sol Head [] P :- !, P = if (var Sol) Head true. make-tc.aux tt _ Head Body (Head :- Body) :- !. make-tc.aux ff Sol Head Body P :- !, P = if (var Sol) (Body => Head) true. func make-tc term, term, list prop, bool -> prop. make-tc Goal Sol RuleBody IsPositive Rule :- coq.safe-dest-app Goal Class Args, get-TC-of-inst-type Class ClassGR, gref->pred-name ClassGR ClassStr, std.append Args [Sol] ArgsSol, coq.elpi.predicate ClassStr ArgsSol RuleHead, make-tc.aux IsPositive Sol RuleHead RuleBody Rule. % returns the priority of an instance from the gref of an instance func get-inst-prio gref -> int. get-inst-prio InstGR Prio :- coq.env.typeof InstGR InstTy, get-TC-of-inst-type InstTy ClassGR, coq.TC.get-inst-prio ClassGR InstGR Prio. func get-full-path gref -> string. get-full-path Gr Res' :- coq.gref->string Gr Path, coq.env.current-section-path SectionPath, std.fold SectionPath "" (e\acc\r\ r is acc ^ "." ^ e) Res, Res' is Res ^ Path. func constant->redflag prop -> coq.redflag. constant->redflag (tc.unfold-constant C) (coq.redflags.const C). func normalize-ty term -> term. :name "normalize-ty" normalize-ty G1 G2 :- std.findall (tc.unfold-constant _) UnfoldFindall, std.map UnfoldFindall constant->redflag UnfoldRF, coq.redflags.add coq.redflags.nored [coq.redflags.beta, coq.redflags.delta | UnfoldRF] RF, @redflags! RF => coq.reduction.lazy.norm G1 G2. func remove-clause string, prop, list prop ->. remove-clause ClauseName P Locality :- %Locality => add-tc-db _ (remove ClauseName) P. Locality => add-tc-db _ (replace ClauseName) (P :- fail, !). % [section-var->decl.aux L R] auxiliary function for `section-var->decl` func section-var->decl.aux list constant -> list prop. section-var->decl.aux [] []. section-var->decl.aux [X|XS] [(decl (global (const X)) _ Ty)|YS] :- coq.env.typeof (const X) Ty, section-var->decl.aux XS YS. % [section-var->decl L] decl representing seciton variables with their types func section-var->decl -> list prop. section-var->decl L :- section-var->decl.aux {coq.env.section-variables} L. func time-is-active (func (list string) ->) ->. :name "time-is-active" time-is-active _ :- coq.option.get ["Time", "TC", "Bench"] (coq.option.bool tt), !. time-is-active Opt :- tc.is-option-active Opt. func time-pp string, float ->. time-pp Msg Time :- !, coq.debug "[TC] - Time of" Msg "is" Time. pred give-res i:prop, o:prop. give-res P true :- P. give-res _ fail. pred time-res i:prop, o:float, o:prop. time-res P T Res :- std.time (give-res P Res) T. func build-msg prop, string -> string. build-msg true S S :- !. build-msg fail S S' :- S' is S ^ " fail". pred time-it i:(list string -> prop), i:prop, i:string. time-it Opt P Msg :- time-is-active Opt, !, time-res P Time Res, build-msg Res Msg Msg', time-pp Msg' Time, Res. time-it _ P _ :- P. kind range-arity type. type r-ar nat -> nat -> range-arity. type prod-range term -> % The current qunatified uvar range-arity -> % Its minimum and maximal application term. type maybe-eta-tm term -> % The current precompiled subterm list term -> % The list of FV in the precomp subterm term. type maybe-llam-tm term -> % The current precompiled subterm: shape is app[app[X,PF],NPF] list term -> % The eta-expanded version of X, from X^{len(PF)} to X^{len(PF)+len(NPF)} term. }