/* Binary parametricity translation */ /* license: GNU Lesser General Public License Version 2.1 or later */ /* ------------------------------------------------------------------------- */ % Author: Cyril Cohen shorten std.{forall, forall2, do!, rev, map2, map}. :before "subst-fun:fail" coq.subst-fun XS T TXS :- !, coq.mk-app T XS TXS. % this is outside the namespace since the predicate is also the db-one param (sort prop as P) P (fun `s` P x\ fun `s` P y\ prod `s1` x _\ prod `s2` y _\ P) :- !. param (sort _ as P) P (fun `u` (sort (typ U)) x\ fun `v` (sort (typ V)) y\ prod `s1` x _\ prod `s2` y _\ P) :- !, coq.univ.new U, coq.univ.new V. param (fun N T B) (fun N T1 B1) (fun N1 T x\ fun N2 T1 x1\ fun NR (TRsubst x x1) xR\ BR x x1 xR) :- !, do! [ derive.param2.names12R N N1 N2 NR, param T T1 TR, (@pi-decl N T x\ @pi-decl N1 T1 x1\ @pi-decl NR TR xR\ param x x1 xR => param (B x) (B1 x1) (BR x x1 xR)), (TRsubst = x\ x1\ {coq.subst-fun [x,x1] TR}) ]. param (prod N T P as Prod) Prod1 ProdR :- !, do! [ param T T1 TR, (@pi-decl N T x\ @pi-decl N T1 x1\ @pi-decl N TR xR\ param x x1 xR => param (P x) (P1 x1) (PR x x1 xR)), Prod1 = prod N T1 P1, derive.param2.names12R N N1 N2 NR, ProdR = fun `f` Prod f\ fun `g` Prod1 g\ prod N1 T x\ prod N2 T1 x1\ prod NR {coq.subst-fun [x,x1] TR} xR\ {coq.subst-fun [{coq.mk-app f [x]}, {coq.mk-app g [x1]}] (PR x x1 xR)} ]. % Primitive projections are applied directly to the record instance, % without mentioning the record's parameters. The application node can % have more arguments if the record field is a function, in which case % the first argument should be treated specially, but then the rest % should be processed like a regular application param (app [primitive (proj Proj NoArgs), X | Xs]) (app [PP1, X1 | Xs1]) (app [PPR, XR | XsR]) :- !, % Can't use `as PP` to bind `PP` in the clause head, since param is declared % :index(3) and `PP` occurs at depth exactly 3, see % https://github.com/LPCIC/elpi/issues/410 % This is fixed in elpi >3.7, as of yet unreleased PP = primitive (proj Proj NoArgs), param PP PP1 PPR, param X X1 XR, derive.param2.param-args Xs Xs1 XsR. param (app [A|Bs]) (app [A1|Bs1]) ARBsR :- !, do! [ param A A1 AR, derive.param2.param-args Bs Bs1 BsR, coq.mk-app AR BsR ARBsR ]. param (let N T V B) Let1 LetR :- !, do! [ param T T1 TR, param V V1 VR, (@pi-decl N T x\ @pi-decl N T1 x1\ @pi-decl N TR xR\ param x x1 xR => param (B x) (B1 x1) (BR x x1 xR)), Let1 = let N T1 V1 B1, derive.param2.names12R N N1 N2 NR, LetR = let N1 T V x\ let N2 T1 V1 x1\ let NR {coq.mk-app TR [x,x1]} VR xR\ BR x x1 xR ]. param (match T P Bs) M1 MR :- !, do! [ param T T1 TR, derive.param2.param-match P P1 PRM, param T T1 TR => derive.param2.map-param Bs Bs1 BsR, M1 = match T1 P1 Bs1, MR = match TR (PRM (x\ match x P Bs) (x\ match x P1 Bs1)) BsR ]. param (fix N Rno T F as Fix) Fix1 FixR :- !, do! [ RnoR is 3 * Rno + 2, RnoR1 is RnoR + 1, param T T1 TR, (@pi-decl N T x\ @pi-decl N1 T1 x1\ @pi-decl N TR xR\ param x x1 xR => param (F x) (F1 x1) (FR x x1 xR)), Fix1 = fix N Rno T1 F1, (TRsubst = f\ f1\ {coq.subst-fun [f, f1] TR}), (pi f f1 xR\ FixBody f f1 xR = let N (TRsubst (F f) (F1 f1)) (FR f f1 xR) fr\ {paramX.mk-trivial-match RnoR (TRsubst f f1) [] fr}), (pi f f1 xR\ coq.mk-eta RnoR1 (TRsubst f f1) (FixBody f f1 xR) (EtaFixBody f f1 xR)), derive.param2.names12R N N1 N2 NR, FixR = (let N1 T Fix f\ let N2 T1 Fix1 f1\ fix NR RnoR (TRsubst f f1) xR\ EtaFixBody f f1 xR) ]. namespace derive.param2 { func names12R name -> name, name, name. names12R N N1 N2 NR :- !, coq.name-suffix N 1 N1, coq.name-suffix N 2 N2, coq.name-suffix N "_R" NR. func id12R id -> id, id, id. id12R N N1 N2 NR :- !, N1 is N ^ "1", N2 is N ^ "2", NR is N ^ "_R". func param-args -> list term, list term, list term. param-args [] [] [] :- !. param-args [X|Xs] [X1|Xs1] [X,X1,XR|XsR] :- !, param X X1 XR, !, param-args Xs Xs1 XsR, !. func map-param -> list term, list term, list term. map-param [] [] [] :- !. map-param [X|Xs] [X1|Xs1] [XR|XsR]:- !, param X X1 XR, !, map-param Xs Xs1 XsR, !. % helpers for match return type func param-match term -> term, ((term -> term) -> (term -> term) -> term). param-match (fun N T B) P1 PRM :- pi x\ not (B x = fun _ _ _), !, param T T1 TR, !, (pi x x1 xR\ (param x x1 xR :- !) => param (B x) (B1 x1) (BR x x1 xR)), !, P1 = fun N T1 B1, derive.param2.names12R N N1 N2 NR, (pi z z1\ PRM z z1 = fun N1 T x\ fun N2 T1 x1\ fun NR {coq.subst-fun [x,x1] TR} xR\ {coq.mk-app (BR x x1 xR) [z x, z1 x1]}). param-match (fun N T B) P1 PRM :- param T T1 TR, !, (pi x x1 xR\ (param x x1 xR :- !) => param-match (B x) (B1 x1) (BR x x1 xR)), !, P1 = fun N T1 B1, derive.param2.names12R N N1 N2 NR, (pi z z1\ PRM z z1 = fun N1 T x\ fun N2 T1 x1\ fun NR {coq.subst-fun [x,x1] TR} xR\ BR x x1 xR z z1). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % translation of inductive declarations % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % helper to improve name hints func rename term, list (option id) -> term. rename (prod _ T P) [some Ni|LN] (prod Nn T P') :- !, pi x\ rename (P x) LN (P' x), coq.id->name Ni Nn. rename (prod N T P) [none|LN] (prod N T P') :- !, pi x\ rename (P x) LN (P' x). rename T _ T :- !. % Renames only the non-relevant names in the final sort, % while ignoring the new names for the relevant parameter names. func rename-arity-sort arity, list (option id) -> arity. rename-arity-sort (parameter N Expl T P) [_|LN] (parameter N Expl T P') :- !, pi x\ rename-arity-sort (P x) LN (P' x). rename-arity-sort (arity T) LN (arity T') :- !, rename T LN T'. rename-arity-sort A _ A :- !. % [param-arity A T1 T2 AR] returns in AR the translation of A applied to the terms % T1 and T2, which are expected to have the types P and P', respectively, P and P' % being the arities A and A' turned into types. func param-arity arity, % Arity (a : A).. | P a.. term, % Term T1 : forall (a : A).., P a.. term -> % Term T2 : forall (a' : A').., P' a'.. arity. % Arity [[ (a : A).. ]] | [[ P a.. ]] (T1 a..) (T2 a'..) param-arity A1 Hd1 Hd2 AR :- param-arity.aux A1 Hd1 Hd2 [] [] AR. func param-arity.aux arity, term, term, list term, list term -> arity. param-arity.aux (parameter N _ Ty1 Rest1) Hd1 Hd2 RevParams1 RevParams2 % Ignore the explicitness set in the untranslated arity. Reusing it leads % to inference failures, so let the user pick their preferred explicitness with % either Arguments or Set Implicit Arguments. (parameter N1 explicit Ty1 x1\ parameter N2 explicit Ty2 x2\ parameter NR explicit (TyRsubst x1 x2) xR\ RestR x1 x2 xR) :- id12R N N1 N2 NR, param Ty1 Ty2 TyR, (pi x x'\ coq.subst-fun [x, x'] TyR (TyRsubst x x')), do! [ @pi-parameter N1 Ty1 x1\ @pi-parameter N2 Ty2 x2\ @pi-parameter NR (TyRsubst x1 x2) xR\ param x1 x2 xR => param-arity.aux (Rest1 x1) Hd1 Hd2 [x1|RevParams1] [x2|RevParams2] (RestR x1 x2 xR) ]. param-arity.aux (arity T1) Hd1 Hd2 RevParams1 RevParams2 (arity TRSubst) :- param T1 _ TR, coq.mk-app Hd1 {rev RevParams1} Ty1, coq.mk-app Hd2 {rev RevParams2} Ty2, coq.subst-fun [Ty1, Ty2] TR TRSubst. % [param-indt-decl I S NR DeclR] accepts a name of an inductive I and a % string S, and returns the representation of the translated inductive DeclR and % its name NR. % The name of the inductive and constructors is generated by appending % S and ensuring freshness in the current context. func param-indt-decl inductive, % Name of the inductive string -> % Suffix to add to the name string, % Name of the translated inductive indt-decl. % Description of the translated inductive param-indt-decl I Suffix NR DeclR :- coq.env.indt-decl I Decl, % Issue: consider the humble Inductive Foo (A : Type) := foo : Foo A -> Foo A. % The arity of foo we get is (f\ "f -> f"), which we can choose to % instantiate either with "Foo", or separately with "Foo A" and "Foo A'". % If we do the translation for Foo, we try to translate (foo : Foo -> Foo), % which produces % (foo_R : ∀ (f : Foo) (f' : Foo), Foo_R f f' -> Foo_R (foo f) (foo f')) % and that's ill-typed, because Foo takes a type parameter, so it should be % (foo_R : ∀ (f : Foo A) (f' : Foo A'), ...). % On the other hand, if we do the translation for Hd1 and Hd2 and we translate % (foo : Foo A -> Foo A), we get % (foo_R : ∀ (f : Foo A) (f' : Foo A'), % Foo_R A A' A_R f f' -> Foo_R A A' A_R (foo f) (foo' f')). % Unfortunately, this is rejected as well, since A A' A_R are % uniform parameters, so the rocq-elpi encoding enforces that the user % treat Foo_R as already being pre-applied. What rocq-elpi expects is % something in the middle, namely % (foo_R : ∀ (f : Foo A) (f' : Foo A'), % Foo_R f f' -> Foo_R (foo f) (foo' f')). % We can avoid the issue by first converting all parameters to non-uniform % ones, since then passing the unapplied inductives everywhere still produces % well-typed arities. % In principle records don't have this problem, since their declarations only % specify the types of fields, and records can't be recursive, so the types % of fields don't ever need to refer to the record type itself. We do, however, % want to still "clean up" records without primitive projections, since those % are expected to be translated as if they were inductives, and this conversion % happens inside the translation to non-uniformly parameterized inductives. if (coq.env.record? I tt) (CleanDecl = Decl) (coq.indt-unif->indt-nonunif Decl CleanDecl), param-indt-decl.aux I Suffix [] [] CleanDecl NR DeclR. func param-indt-decl.aux inductive, string, list term, % Reversed list of "unprimed" parameters accumulated so far list term, % Reversed list of "primed" parameters accumulated so far indt-decl -> string, indt-decl. param-indt-decl.aux I Suffix RevParams1 RevParams2 % Ignore the explicitness set in the untranslated inductive. Reusing them leads % to inference failures, so let the user pick their preferred explicitness with % either Arguments or Set Implicit Arguments. (parameter N _ Ty Rest) NewName (parameter N1 explicit Ty x\ parameter N2 explicit Ty' x'\ parameter NR explicit (TyRsubst x x') xR\ RestR x x' xR) :- id12R N N1 N2 NR, param Ty Ty' TyR, (pi x x'\ coq.subst-fun [x,x'] TyR (TyRsubst x x')), do! [ @pi-parameter N1 Ty x\ @pi-parameter N2 Ty' x'\ @pi-parameter NR TyR xR\ param x x' xR => param-indt-decl.aux I Suffix [x|RevParams1] [x'|RevParams2] (Rest x) NewName (RestR x x' xR) ]. param-indt-decl.aux I Suffix RevParams1 RevParams2 (inductive N Ind IndexA Ks) NewName (inductive NewName Ind IndexAR (KRs HdTy)) :- coq.ensure-fresh-global-id {calc (N ^ Suffix)} NewName, coq.env.global (indt I) HdTy, rev RevParams1 Params1, rev RevParams2 Params2, param-arity IndexA {coq.mk-app HdTy Params1} {coq.mk-app HdTy Params2} IndexAR, % The indt-decl we get on the input doesn't give us the constructor names % we can actually use to form terms, so we need to fetch those separately coq.env.indt I _ _ _ _ Cs _, % Create a fresh variable for both the untranslated and translated inductives. % The constructors Ks already treat the untranslated inductive as an opaque % term. The alternative is to use HdTy directly instead of ty, but then the % local param rule would technically conflict with the default rule for (p)global, % so the "proper" local assumption would be on the param.gref predicate, % but we don't have a gref for the translated inductive yet. pi ty tyR\ param ty ty tyR =!=> map2 Cs (Ks ty) (param-indc-decl Suffix Params1 Params2) (KRs ty tyR). param-indt-decl.aux I Suffix RevParams1 RevParams2 (record Name Ty Kname Fields) FNameR (parameter C1 explicit Ty1 x \ parameter C2 explicit Ty2 x'\ record FNameR Ty FKnameR (FieldsR x x')) :- coq.env.global (indt I) HdTy, id12R Name C1 C2 _, coq.ensure-fresh-global-id {calc (Name ^ Suffix)} FNameR, coq.ensure-fresh-global-id {calc (Kname ^ Suffix)} FKnameR, coq.mk-app HdTy {rev RevParams1} Ty1, coq.mk-app HdTy {rev RevParams2} Ty2, % The input declaration does not carry the projection names which we need % to construct terms out of them. We know this record has primitive projections, % since records without primitive projections are treated as inductives. coq.env.primitive-projections I Projs, @pi-parameter C1 Ty1 x\ @pi-parameter C2 Ty2 x'\ param-prim-record-decl Fields Suffix Projs x x' (FieldsR x x'). func param-indc-decl string, % Suffix to add to the name list term, % "Unprimed" parameter instantiation of the inductive list term, % "Primed" parameter instantiation of the inductive constructor, % Name of the constructor being translated indc-decl -> % Declaration of the constructor being translated indc-decl. % Translated constructor param-indc-decl Suffix Params1 Params2 CHd (constructor N A) (constructor NR AR) :- coq.env.global (indc CHd) CT, % If the user changed argument names of the untranslated constructor, % we want to use those for pretty-printing hints in the translation. % We need to be careful, however, because when adding the inductive, % the names of non-uniform parameters in the inductive aritiy and in the % constructor arity need to match. We deal with this by ignoring the name % hints for the relevant names in rename-arity-sort. coq.arguments.name (indc CHd) Names, coq.ensure-fresh-global-id {calc (N ^ Suffix)} NR, param-arity {rename-arity-sort A Names} {coq.mk-app CT Params1} {coq.mk-app CT Params2} AR. func param-prim-record-decl record-decl, % Untranslated record declaration string, % Suffix to add to the record, constructor, and projections' names list % List of projection names of the untranslated record (option (pair projection int)), % Records with unnameable projections are not supported term, % "Unprimed" instance of the untranslated record term -> % "Primed" instance of the untranslated record record-decl. param-prim-record-decl end-record _ _ _ _ end-record. param-prim-record-decl (field Attrs Id Ty Rest) Suffix [OProj|Projs] X X' (field Attrs IdR TyRsubst (RestR Proj1 Proj2)) :- (OProj = some (pr Proj NoParams) ; std.fatal-error-w-data "Cannot translate records with unnameable projections" Id), !, coq.env.fresh-global-id {calc (Id ^ Suffix)} IdR, param Ty _Ty' TyR, Proj1 = app [primitive (proj Proj NoParams), X], Proj2 = app [primitive (proj Proj NoParams), X'], coq.subst-fun [Proj1, Proj2] TyR TyRsubst, % Similarly to the inductive case, instantiate the field continutations % with fresh elpi variables, since we prefer to avoid locally assuming % a translation of a complex term like a fully applied projection. pi proj1 proj2 projR\ param proj1 proj2 projR =!=> param-prim-record-decl (Rest proj1) Suffix Projs X X' (RestR proj1 proj2 projR). % [param-record-constr C N Ty CR Bo TyB] takes the constructor names of % a record and its translation, the number of parameters of the untranslated % record type, and the type of the untranslated constructor, and returns % a parametricity shim for the constructor, and its type. % % Generating a shim is necessary because the type of the constructor of % the translated record type does not have the type of a parametricity % translation of the original constructor: the type of mkFoo in % Record Foo (A : TyA) := mkFoo { fld : TyF } % is (forall A : TyA, TyF -> Foo A). The translation of Foo, however, is % Record Foo_R (A A' A_R) (f : Foo A) (f' : Foo A') := % mkFoo_R { fld_R : TyF_R f.(fld) f'.(fld) } % where mkFoo_R has the type % (forall A A' A_R f f', TyF_R f.(fld) f'.(fld) -> Foo_R A A' A_R f f'). % Meanwhile the expected type is a function type which has a triplet of arguments % for every field in the original record, and whose codomain ends in the translated % record type instantiated at instances constructed from the arguments, here % (forall A A' A_R fld fld', % TyF_R fld fld' -> Foo_R A A' A_R (mkFoo A fld) (mkFoo A' fld')). % The shim generated by this predicate does this transformation by wrapping % the appropriate arguments in constructor calls before forwarding them to % the translated constructor. func param-record-constr constructor, % Name of the untranslated constructor int, % Number of parameters term, % Type of the original constructor constructor -> % Name of the constructor of the translation term, % Body of the new compatibility constant term. % And its type param-record-constr C NParams Ty CR Bo TyR :- % Eta-expand the parameters of the constructor first; % This allows us to treat (ctor A0 ... AN) as a single unit to be % translated. It needs special treatment, because the arguments % which come after correspond to the record fields, which need to be % collected separately and wrapped in a constructor call when forwarding % to the translated constructor. % The expansion is entirely type-driven, and we abstract over the concrete % constructor term to enforce that. (pi ctor\ coq.mk-eta NParams Ty ctor (Exp ctor)), % Note: this still doesn't feel great, since we don't control mk-eta from here % and then we're matching on a non-atomic term (app [ctor | xs]). Maybe % we can locally assume a new term constructor similar to app and make our own % mk-eta which uses that instead of app? With that we could also remove the % second local assumption below. % Register the translation of (ctor A0 .. AN) to be a function taking % fld1 -> fld1' -> fld1R .. 's to the constructor call % ctorR A0 A0' A0R .. AN AN' ANR % (ctor A0 .. AN fld1 ..) % (ctor A0' .. AN' fld1' ..) % fld1R .. (pi ctor ctorR\ [(pi xs C x1s C1 OutR xRs Tele AppliedCR\ param (app [ctor | xs]) (app [ctor | x1s]) OutR :- !, do! [ param-args xs x1s xRs, % Substitute parameters to the constructor type, leaving just % the arguments corresponding to fields coq.subst-prod xs Ty Tele, % Pre-apply the translation of parameters coq.mk-app ctorR xRs AppliedCR, % Construct the output term by wrapping the rest of the arguments % in (ctor ..xs) and (ctor x1s..) calls param-record-constr.aux Tele (app [ctor | xs]) (app [ctor | x1s]) AppliedCR [] [] [] OutR ]), % If the record didn't have any parameters, then there was no eta % expansion, and the constructors are just ctor instead of (ctor) (pi OutR\ param ctor ctor OutR :- !, param-record-constr.aux Ty ctor ctor ctorR [] [] [] OutR)] => % Apply the above translation, together with the usual mechanism % for translating functions when translating parameters param (Exp ctor) _ (BoSubst ctor ctorR)), % Instantiate the body of the compatibility constant with concrete constructor terms coq.env.global (indc C) T, coq.env.global (indc CR) TR, Bo = BoSubst T TR, param Ty _ TyRUnsubst, coq.subst-fun [T, T] TyRUnsubst TyR, % Sanity check that the type of the shim has the type expected of a translation std.assert-ok! (coq.typecheck Bo TyR) "param-record-constr: Ill-typed translated constructor shim". func param-record-constr.aux term, % Rest of the constructor telescope term, % Untranslated constructor applied to "unprimed" parameters term, % Untranslated constructor applied to "primed" parameters term, % Constructor of the translated record applied to parameters list term, % Reverse "unprimed" arguments corresponding to % fields of the untranslated record list term, % Reverse "primed" arguments list term -> % Reverse arguments corresponding to fields of the translated record term. % Call to the translated constructor param-record-constr.aux (prod N T1 B) C1 C2 CR RevArgs1 RevArgs2 RevArgsR (fun N1 T1 x1\ fun N2 T2 x2\ fun NR (TRsubst x1 x2) (E x1 x2)) :- !, names12R N N1 N2 NR, param T1 T2 TR, (pi x1 x2\ coq.subst-fun [x1, x2] TR (TRsubst x1 x2)), (@pi-decl N1 T1 x1\ @pi-decl N2 T2 x2\ @pi-decl NR (TRsubst x1 x2) xR\ param x1 x2 xR =!=> param-record-constr.aux (B x1) C1 C2 CR [x1 | RevArgs1] [x2 | RevArgs2] [xR | RevArgsR] (E x1 x2 xR)). param-record-constr.aux _ C1 C2 CR RevArgs1 RevArgs2 RevArgsR Call :- rev RevArgs1 Args1, rev RevArgs2 Args2, rev RevArgsR ArgsR, coq.mk-app C1 Args1 CCall1, coq.mk-app C2 Args2 CCall2, coq.mk-app CR [CCall1, CCall2 | ArgsR] Call. %%%%%%%%%%%%%%%%%%%%%%%%%%%% % Class storage functions: % %%%%%%%%%%%%%%%%%%%%%%%%%%%% func store-param string, term, term, term. store-param N X X1 XR :- !, Nparam is "param_" ^ N, Args = [_, _, _, X, X1, XR], T1 = app [{{ lib:@param2.store_param }}|Args], std.assert-ok! (coq.typecheck T1 T2) "store-param: T1 illtyped", coq.ensure-fresh-global-id Nparam FNparam, coq.env.add-const FNparam T1 T2 _ C, @global! => coq.TC.declare-instance (const C) 0. func store-param-indc string, constructor, constructor. store-param-indc Suffix K KR :- store-param {calc ({coq.gref->id (indc K)} ^ Suffix)} {coq.env.global (indc K)} {coq.env.global (indc K)} {coq.env.global (indc KR)}. %%%%%%%%%%%%%%%%%%%%%%% % toplevel predicates % %%%%%%%%%%%%%%%%%%%%%%% :index (1) func dispatch gref, string -> list prop. dispatch ((const GR) as C) Suffix Clauses :- do! [ % We need to check both projection and primitive-projection: a non-primitive % projection is still a projection, and a manual wrapper of a primitive projection % is succesfully checked as a compatibility constant. We want both of these cases % to be successfully translated, so we want to only rule out manual translations % of generated compatibility constants, which already have registered translations. (not (coq.env.projection? GR _, coq.env.primitive-projection? _ GR _) ; coq.error "derive.param2: cannot translate a projection"), coq.env.global C Term, NameR is {coq.gref->id C} ^ Suffix, coq.env.const GR OBo Ty, (OBo = (some X) ; std.fatal-error-w-data "Cannot translate local constant without body" GR), param Ty _ TyR, coq.mk-app TyR [Term, Term] TyRTermTerm, % coq.typecheck is needed to add universe constraints std.assert-ok! (coq.typecheck TyRTermTerm _) "param2: illtyped param type", param X _ XR, % apparently calling the type checker with the expected type is weaker in this case std.assert-ok! (coq.typecheck XR XRTy) "param2: illtyped constant", std.assert-ok! (coq.unify-leq XRTy TyRTermTerm) "param2: constant does not have the right type", coq.ensure-fresh-global-id NameR FNameR, coq.env.add-const FNameR XR TyRTermTerm _ TermR, store-param NameR Term Term {coq.env.global (const TermR)}, C1 = (param.gref C C (const TermR) :- !), coq.elpi.accumulate _ "derive.param2.db" (clause _ (before "param:fail") C1), coq.elpi.accumulate _ "derive.param2.db" (clause _ _ (param-done C)), Clauses = [param-done C, C1] ]. dispatch (indt I as GR) Suffix Clauses :- % Records with primitive projections get translated into records, % as opposed to records without primitive projections, which are % just glorified single-constructor inductives coq.env.record? I tt, !, do! [ param-indt-decl I Suffix NameR RecordDeclR, std.assert-ok! (coq.typecheck-indt-decl RecordDeclR) "derive.param2 generates illtyped record type", (@primitive! => coq.env.add-indt RecordDeclR IR), GRR = indt IR, coq.env.global GR Ind, coq.env.global GRR IndR, store-param NameR Ind Ind IndR, TypeClause = (param.gref GR GR GRR :- !), % All projections are guaranteed to be nameable, otherwise % generating the record declaration would have failed by now coq.env.projections I OProjs, map OProjs (p\ r\ (some r) = p) Projs, coq.env.projections IR OProjsR, map OProjsR (p\ r\ (some r) = p) ProjsR, % Primitive projections are different terms, % so we need to register their translations separately coq.env.primitive-projections I OPProjs, coq.env.primitive-projections IR OPProjsR, map OPProjs (p\ r\ sigma pproj args\ (some (pr pproj args)) = p, r = proj pproj args) PProjs, map OPProjsR (p\ r\ sigma pproj args\ (some (pr pproj args)) = p, r = proj pproj args) PProjsR, % store projection translations as witnesses forall2 Projs ProjsR (p\ pR\ sigma n fulln gp gpR \ coq.gref->id (const p) n, coq.gref->id (const pR) fulln, coq.env.global (const p) gp, coq.env.global (const pR) gpR, store-param fulln gp gp gpR), % projection translation clauses map2 Projs ProjsR (p\ pR\ r\ r = (param.gref (const p) (const p) (const pR) :- !)) ProjectionClauses, map2 PProjs PProjsR (p\ pR\ r\ r = (param (primitive p) (primitive p) (primitive pR) :- !)) PProjectionClauses, MostClauses = [TypeClause | {std.append PProjectionClauses ProjectionClauses}], coq.env.indt I _ NParams _ _ [C] [CTy], coq.env.indt IR _ _ _ _ [CR] _, % Generating the constructor shim needs to know about registered % translations (MostClauses =!=> param-record-constr C NParams CTy CR ShimCR ShimCRTy), coq.env.add-const {coq.env.fresh-global-id {calc ("shim_" ^ {coq.gref->id (indc CR)})}} ShimCR ShimCRTy @transparent! Shim, store-param {coq.gref->id (indc CR)} {coq.env.global (indc C)} {coq.env.global (indc C)} {coq.env.global (const Shim)}, ConstrClause = (param.gref (indc C) (indc C) (const Shim) :- !), Clauses = [param-done GR, ConstrClause | MostClauses], forall Clauses (c\ coq.elpi.accumulate _ "derive.param2.db" (clause _ (before "param:fail") c)), ]. dispatch (indt I as GR) Suffix Clauses :- !, param-indt-decl I Suffix NameR DeclR, std.assert-ok! (coq.typecheck-indt-decl DeclR) "derive.param2 generates illtyped inductive type", coq.env.add-indt DeclR IR, coq.env.global GR Ind, store-param NameR Ind Ind {coq.env.global (indt IR)}, coq.env.indt I _ _ _ _ Knames _, coq.env.indt IR _ _ _ _ KnamesR _, forall2 Knames KnamesR (store-param-indc Suffix), C1 = (param.gref GR GR (indt IR) :- !), coq.elpi.accumulate _ "derive.param2.db" (clause _ (before "param:fail") C1), map2 Knames KnamesR (a\ b\ r\ r = (param.gref (indc a) (indc a) (indc b) :- !)) CK, forall CK (c\ coq.elpi.accumulate _ "derive.param2.db" (clause _ (before "param:fail") c)), CD = (param-done GR :- !), coq.elpi.accumulate _ "derive.param2.db" (clause _ _ CD), Clauses = [CD, C1|CK]. dispatch (indc _) _ _ :- coq.error "derive.param2: cannot translate a constructor". func main gref, string -> list prop. main T _ Clauses :- dispatch T "_R" Clauses. func main_register gref, gref. main_register I R :- C1 = (param.gref I I R :- !), coq.elpi.accumulate _ "derive.param2.db" (clause _ (before "param:fail") C1), coq.elpi.accumulate _ "derive.param2.db" (clause _ _ (param-done I)). } /* %%%%%%%%%%%%%%%%%%%%% % Tactic entrypoint % %%%%%%%%%%%%%%%%%%%%% % We disable coq-refiner :before "refiner-assign-evar" evar _ _ _ :- !. pred ctx->TC i:(list prop), o:(list (pair term term)). ctx->TC [] [] :- !. ctx->TC [decl X _ Ty |Xs] [pr X Ty|Is] :- !, ctx->TC Xs Is. ctx->TC [def X _ _ _ Ty |Xs] [pr X Ty|Is] :- !, ctx->TC Xs Is. solve _ [goal Ctx Ev (app[{{@param}}, T, TR, X, XR]) _] _ :- !, coq.sigma.print, coq.say "goal->TC" {ctx->TC Ctx}, coq.say "searching param for" X, (param T _ TR), (param X _ XR), Ev = app [{{@Param}}, T, TR, X, XR], coq.typecheck Ev Ty ok, coq.say "Ty=" Ty. */