From 68f008466b799ab1fa020b6472b1eb00725482ec Mon Sep 17 00:00:00 2001 From: lucie <135951230+lweqx@users.noreply.github.com> Date: Thu, 1 Oct 2026 10:47:38 +0200 Subject: [PATCH 1/5] Rename `fake-term` to `fake-aterm` --- elpi/aterm.elpi | 34 +++++++++++++++++----------------- elpi/vernac.elpi | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/elpi/aterm.elpi b/elpi/aterm.elpi index f2ae708..817b076 100644 --- a/elpi/aterm.elpi +++ b/elpi/aterm.elpi @@ -300,8 +300,8 @@ namespace aterm.util { coq.env.global G TG. - func fake-term->aterm term -> aterm. - fake-term->aterm (app [PType|L]) (asort (typ U) Cl) :- + func fake-aterm->aterm term -> aterm. + fake-aterm->aterm (app [PType|L]) (asort (typ U) Cl) :- trocq.data.ptype PType, !, L = [TM, TN], trocq.data.term->map-class TM M, @@ -310,10 +310,10 @@ namespace aterm.util { (Cl = _) (Cl = pc M N), coq.univ.new U. - fake-term->aterm (app [PTriple|L]) Out :- + fake-aterm->aterm (app [PTriple|L]) Out :- trocq.data.ptriple PTriple, L = [_,_,_,X,X',RX|L'], !, - std.map L' fake-term->aterm AL', + std.map L' fake-aterm->aterm AL', % XXX Reactivate this, but think about it first %coq.elaborate-skeleton RX (prod `_` X _\ prod `_` X' _\ sort _) RXR Diag, %(Diag = ok, ! ; trocq.error {coq.term->string RX} "is not a relation between" {coq.term->string X} "and" {coq.term->string X'} Diag), !, @@ -323,29 +323,29 @@ namespace aterm.util { (if (X = pglobal G U) (OutT = apglobal G U X' RXR) (OutT = atriple X X' RXR)), - std.map L' fake-term->aterm AL', + std.map L' fake-aterm->aterm AL', aterm.util.mk-app OutT AL' Out. - fake-term->aterm (sort S) (asort S Cl) :- !, + fake-aterm->aterm (sort S) (asort S Cl) :- !, prune Cl []. %XXX Check wether those prunes will cause any trouble. - fake-term->aterm (global G) (aglobal G G' RG) :- !, + fake-aterm->aterm (global G) (aglobal G G' RG) :- !, prune G' [], prune RG []. - fake-term->aterm (pglobal G U) (apglobal G U G' RG) :- !, + fake-aterm->aterm (pglobal G U) (apglobal G U G' RG) :- !, prune G' [], prune RG []. - fake-term->aterm (fun Name T F) (afun Name AT AF) :- !, - fake-term->aterm T AT, + fake-aterm->aterm (fun Name T F) (afun Name AT AF) :- !, + fake-aterm->aterm T AT, @pi-adecl Name AT (ay\ y\ _\ _\ - fake-term->aterm (F y) (AF ay) + fake-aterm->aterm (F y) (AF ay) ). - fake-term->aterm (prod Name T F) (aprod Name AT AF) :- !, - fake-term->aterm T AT, + fake-aterm->aterm (prod Name T F) (aprod Name AT AF) :- !, + fake-aterm->aterm T AT, @pi-adecl Name AT (ay\ y\ _\ _\ - fake-term->aterm (F y) (AF ay) + fake-aterm->aterm (F y) (AF ay) ). - fake-term->aterm (app L) (aapp AL) :- !, - std.map L fake-term->aterm AL. - fake-term->aterm X AX :- adecl AX _ X _ _ _, !. + fake-aterm->aterm (app L) (aapp AL) :- !, + std.map L fake-aterm->aterm AL. + fake-aterm->aterm X AX :- adecl AX _ X _ _ _, !. % From a full aterm in the database, instantiates a aterm with holes func equals-ignore aterm -> aterm. diff --git a/elpi/vernac.elpi b/elpi/vernac.elpi index 1a6136f..84f7c25 100644 --- a/elpi/vernac.elpi +++ b/elpi/vernac.elpi @@ -76,7 +76,7 @@ func vernac.infer-aterm term -> aterm. vernac.infer-aterm uvar _AT :- !. vernac.infer-aterm T AT :- (coq.elaborate-skeleton T _ ET ok, - aterm.util.fake-term->aterm ET AT + aterm.util.fake-aterm->aterm ET AT ; trocq.error "Could not make an annotated term out of term" {coq.term->string T}), !, logging.trace (coq.say "[infer-aterm] Inferred annotated term" AT). From 8b25b80384e712c5b372d8c558fb823dae6147c9 Mon Sep 17 00:00:00 2001 From: lucie <135951230+lweqx@users.noreply.github.com> Date: Thu, 1 Oct 2026 15:10:35 +0200 Subject: [PATCH 2/5] Reorganise fake aterms into elpi/fake-aterm.elpi --- elpi/aterm.elpi | 65 --------------------------- elpi/data.elpi | 12 ----- elpi/fake-aterm.elpi | 102 +++++++++++++++++++++++++++++++++++++++++++ elpi/vernac.elpi | 4 +- 4 files changed, 104 insertions(+), 79 deletions(-) create mode 100644 elpi/fake-aterm.elpi diff --git a/elpi/aterm.elpi b/elpi/aterm.elpi index 817b076..653b81e 100644 --- a/elpi/aterm.elpi +++ b/elpi/aterm.elpi @@ -299,54 +299,6 @@ namespace aterm.util { annotate-legacy (pglobal G U) [] none (apglobal G U TG TG) :- coq.env.global G TG. - - func fake-aterm->aterm term -> aterm. - fake-aterm->aterm (app [PType|L]) (asort (typ U) Cl) :- - trocq.data.ptype PType, !, - L = [TM, TN], - trocq.data.term->map-class TM M, - trocq.data.term->map-class TN N, - if (var M, var N) - (Cl = _) - (Cl = pc M N), - coq.univ.new U. - fake-aterm->aterm (app [PTriple|L]) Out :- - trocq.data.ptriple PTriple, - L = [_,_,_,X,X',RX|L'], !, - std.map L' fake-aterm->aterm AL', - % XXX Reactivate this, but think about it first - %coq.elaborate-skeleton RX (prod `_` X _\ prod `_` X' _\ sort _) RXR Diag, - %(Diag = ok, ! ; trocq.error {coq.term->string RX} "is not a relation between" {coq.term->string X} "and" {coq.term->string X'} Diag), !, - RXR = RX, - if (X = global G) - (OutT = aglobal G X' RXR) - (if (X = pglobal G U) - (OutT = apglobal G U X' RXR) - (OutT = atriple X X' RXR)), - std.map L' fake-aterm->aterm AL', - aterm.util.mk-app OutT AL' Out. - fake-aterm->aterm (sort S) (asort S Cl) :- !, - prune Cl []. %XXX Check wether those prunes will cause any trouble. - fake-aterm->aterm (global G) (aglobal G G' RG) :- !, - prune G' [], - prune RG []. - fake-aterm->aterm (pglobal G U) (apglobal G U G' RG) :- !, - prune G' [], - prune RG []. - fake-aterm->aterm (fun Name T F) (afun Name AT AF) :- !, - fake-aterm->aterm T AT, - @pi-adecl Name AT (ay\ y\ _\ _\ - fake-aterm->aterm (F y) (AF ay) - ). - fake-aterm->aterm (prod Name T F) (aprod Name AT AF) :- !, - fake-aterm->aterm T AT, - @pi-adecl Name AT (ay\ y\ _\ _\ - fake-aterm->aterm (F y) (AF ay) - ). - fake-aterm->aterm (app L) (aapp AL) :- !, - std.map L fake-aterm->aterm AL. - fake-aterm->aterm X AX :- adecl AX _ X _ _ _, !. - % From a full aterm in the database, instantiates a aterm with holes func equals-ignore aterm -> aterm. equals-ignore X X' :- @@ -444,21 +396,4 @@ namespace aterm.util { topOf X X'. bottomOf X X :- !. topOf X X :- !. - - % {infer-aterm TR AT T T'} infers AT, T, and T' such that - % TR = [[AT]] T T' - func infer-aterm term -> aterm, term, term. - infer-aterm T _ _ _ :- - coq.say "Inferring on" T, fail, !. - infer-aterm (prod N T x\ prod N' T' x'\ prod NR (RTxx x x') rx\ RU x x' rx) - (aprod N AT ax\ AU ax) - (prod N T x\ U x) - (prod N' T' x'\ U' x') :- - @pi-decl N T x\ @pi-decl N' T' x'\ infer-aterm (RTxx x x') AT x x', - @pi-adecl N AT ax\ x\ x'\ rx\ ( - infer-aterm (RU x x' rx) (AU ax) (U x) (U' x') - ). - infer-aterm (app [ParamRelCl,T,T']) (asort S Cl) T T' :- - trocq.data.param.rel->param-class ParamRelCl Cl, !, - coq.typecheck T (sort S) ok. } \ No newline at end of file diff --git a/elpi/data.elpi b/elpi/data.elpi index d94a568..9fe7050 100644 --- a/elpi/data.elpi +++ b/elpi/data.elpi @@ -156,18 +156,6 @@ namespace trocq.data { ptriple T :- coq.env.global G T, !, trocq.db.fake-aterm-ptype _ G, !. - - func term->map-class term -> map-class. - term->map-class T N :- - coq.env.global G T, - trocq.db.map-class->indc-class N G, !. - - func param.rel->param-class term -> param-class. - param.rel->param-class T Cl :- - coq.env.global G T, - trocq.db.rel Cl G _ _ _ _, - !. - } % trocq.data namespace trocq { diff --git a/elpi/fake-aterm.elpi b/elpi/fake-aterm.elpi new file mode 100644 index 0000000..2848427 --- /dev/null +++ b/elpi/fake-aterm.elpi @@ -0,0 +1,102 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% % Trocq % +% _______ % Copyright (C) 2023 Inria & MERCE % +% |__ __| % (Mitsubishi Electric R&D Centre Europe) % +% | |_ __ ___ ___ __ _ % Cyril Cohen % +% | | '__/ _ \ / __/ _` | % Enzo Crance % +% | | | | (_) | (_| (_| | % Assia Mahboubi % +% |_|_| \___/ \___\__, | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% | | % This file is distributed under the terms of % +% |_| % GNU Lesser General Public License Version 3 % +% % (see LICENSE file for the text of the license) % +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% ----------------------------------------------------------------------------- +% Convertion from/into fake aterms. +% ----------------------------------------------------------------------------- + +accumulate "Trocq.Elpi/database". +accumulate "Trocq.Elpi/data". +accumulate "Trocq.Elpi/aterm". +accumulate "Trocq.Elpi/util-rocq". + +% `@pi-adecl N AT (ax\ x\ x'\ xr\ F x x' xr)` declares elpi pi-variables x x' xr with adecl +% x has annotated type AT +macro @pi-adecl N PAD_AT PAD_F :- + aterm.triple PAD_AT PAD_T PAD_T' PAD_TR, + util.mk-app PAD_TR [PAD_T,PAD_T'] PAD_TRx, + coq.name-suffix N "'" N', + coq.name-suffix N "R" NR, + @pi-decl N PAD_T x\ ( + @pi-decl N' PAD_T' x'\ ( + @pi-decl NR PAD_TRx xR\ ( + pi ax\ [ + adecl ax N x x' xR PAD_AT + ] => ( + logging.trace (coq.say "[adecl]" ax "==" x "@" PAD_AT "~" x' "∵" xR), + (PAD_F ax x x' xR) + )))). + +typeabbrev fake-map-class term. +typeabbrev fake-param-class term. +typeabbrev fake-aterm term. + +% Converts a fake-map-class into a map-class. +func fake-map-class->map-class fake-map-class -> map-class. +fake-map-class->map-class T N :- + coq.env.global G T, + trocq.db.map-class->indc-class N G, !. + +% Converts a fake-param-class into a param-class. +func fake-param-class->param-class fake-param-class -> param-class. +fake-param-class->param-class T Cl :- + coq.env.global G T, + trocq.db.rel Cl G _ _ _ _, + !. + +% Converts a fake-aterm into an aterm. +func fake-aterm->aterm fake-aterm -> aterm. +fake-aterm->aterm (app [PType|L]) (asort (typ U) Cl) :- + trocq.data.ptype PType, !, + L = [TM, TN], + fake-map-class->map-class TM M, + fake-map-class->map-class TN N, + if (var M, var N) + (Cl = _) + (Cl = pc M N), + coq.univ.new U. +fake-aterm->aterm (app [PTriple|L]) Out :- + trocq.data.ptriple PTriple, + L = [_,_,_,X,X',RX|L'], !, + std.map L' fake-aterm->aterm AL', + % XXX Reactivate this, but think about it first + %coq.elaborate-skeleton RX (prod `_` X _\ prod `_` X' _\ sort _) RXR Diag, + %(Diag = ok, ! ; trocq.error {coq.term->string RX} "is not a relation between" {coq.term->string X} "and" {coq.term->string X'} Diag), !, + RXR = RX, + if (X = global G) + (OutT = aglobal G X' RXR) + (if (X = pglobal G U) + (OutT = apglobal G U X' RXR) + (OutT = atriple X X' RXR)), + std.map L' fake-aterm->aterm AL', + aterm.util.mk-app OutT AL' Out. +fake-aterm->aterm (sort S) (asort S Cl) :- !, + prune Cl []. %XXX Check wether those prunes will cause any trouble. +fake-aterm->aterm (global G) (aglobal G G' RG) :- !, + prune G' [], + prune RG []. +fake-aterm->aterm (pglobal G U) (apglobal G U G' RG) :- !, + prune G' [], + prune RG []. +fake-aterm->aterm (fun Name T F) (afun Name AT AF) :- !, + fake-aterm->aterm T AT, + @pi-adecl Name AT (ay\ y\ _\ _\ + fake-aterm->aterm (F y) (AF ay) + ). +fake-aterm->aterm (prod Name T F) (aprod Name AT AF) :- !, + fake-aterm->aterm T AT, + @pi-adecl Name AT (ay\ y\ _\ _\ + fake-aterm->aterm (F y) (AF ay) + ). +fake-aterm->aterm (app L) (aapp AL) :- !, + std.map L fake-aterm->aterm AL. +fake-aterm->aterm X AX :- adecl AX _ X _ _ _, !. diff --git a/elpi/vernac.elpi b/elpi/vernac.elpi index 84f7c25..85fb0a9 100644 --- a/elpi/vernac.elpi +++ b/elpi/vernac.elpi @@ -12,7 +12,7 @@ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% accumulate "Trocq.Elpi/annot". - +accumulate "Trocq.Elpi/fake-aterm". % API @@ -76,7 +76,7 @@ func vernac.infer-aterm term -> aterm. vernac.infer-aterm uvar _AT :- !. vernac.infer-aterm T AT :- (coq.elaborate-skeleton T _ ET ok, - aterm.util.fake-aterm->aterm ET AT + fake-aterm->aterm ET AT ; trocq.error "Could not make an annotated term out of term" {coq.term->string T}), !, logging.trace (coq.say "[infer-aterm] Inferred annotated term" AT). From e5f0d337c5f7cf0a42c0a1d4fb8e0cd36e311471 Mon Sep 17 00:00:00 2001 From: lucie <135951230+lweqx@users.noreply.github.com> Date: Mon, 12 Oct 2026 11:48:17 +0200 Subject: [PATCH 3/5] Add a utility to associate uvars with Rocq evars This utility is called `uvar-to-evar`. Given a unification variable, it returns a Rocq evar (represented as an ELPI unification variable) uniquely associated to the unification: if called again on the same uvar, the same evar will be returned. `uvar-to-evar` declares no constraints between the uvar or the Rocq evar, so for instance assigning to the uvar will not result in the evar being instanciated. Code that make use of this utility can however declare a constraint. This utility is mostly useful in code that produce Rocq terms for debugging purposes: using this utility, uvars can also be represented as Rocq evars. Unfortunately, Rocq-ELPI cannot create named evars, which would allow to give the same name to the Rocq evar and the uvar. For debugging purposes, a mapping between uvars and evars is printed to Trocq's tracing output. --- elpi/util-rocq.elpi | 26 ++++++++++++++++++++++ tests/_CoqProject.hott | 2 ++ tests/_CoqProject.std | 2 ++ tests/elpi/uvar_to_evar.v | 45 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 tests/elpi/uvar_to_evar.v diff --git a/elpi/util-rocq.elpi b/elpi/util-rocq.elpi index 08a6dbd..3821a1e 100644 --- a/elpi/util-rocq.elpi +++ b/elpi/util-rocq.elpi @@ -197,6 +197,32 @@ func name-suffix name, any -> name. name-suffix uvar _ _ :- !. name-suffix X S O :- coq.name-suffix X S O. +func uvar-evar-mapping? any, term ->. +uvar-evar-mapping? (uvar as U) E :- !, + declare_constraint (uvar-evar-mapping? U E) [_]. +uvar-evar-mapping? _ _ :- !, + trocq.error "[uvar-evar-mapping] not called on a uvar!". + +func uvar-evar-mapping! any, term ->. +uvar-evar-mapping! (uvar as U) E :- !, + declare_constraint (uvar-evar-mapping! U E) [_]. +uvar-evar-mapping! _ _ :- !, + trocq.error "[uvar-evar-mapping] not called on a uvar!". + +constraint uvar-evar-mapping? uvar-evar-mapping! { + rule (uvar-evar-mapping! U T1) \ (uvar-evar-mapping? U T2) <=> (T2 = T1). + rule \ (uvar-evar-mapping? U _) <=> false. +} + +% `uvar-to-evar U E` provides a way to associated an Elpi uvar `E` to a Rocq evar `U`. +% The mapping is unique: if `uvar-to-evar` is called once again on the same uvar `U`, the same Rocq evar will be returned. +% The uvar resolving will not cause an assignment to the evar. +pred uvar-to-evar any -> term. +uvar-to-evar U E :- + uvar-evar-mapping? U E. +uvar-to-evar U E :- + logging.trace (coq.say "[uvar-evar-mapping]" U "↔" {coq.term->string E}), + uvar-evar-mapping! U E. % Elpi does not allow replacing a named clause with a named clause. This is not allowed: % coq.elpi.accumulate Scope Database (clause ClauseName (replace ClauseName) Content) diff --git a/tests/_CoqProject.hott b/tests/_CoqProject.hott index c7f56f2..2ec372f 100644 --- a/tests/_CoqProject.hott +++ b/tests/_CoqProject.hott @@ -5,6 +5,8 @@ -R ../hott/ Trocq -R ../elpi/ Trocq.Elpi +elpi/uvar_to_evar.v + unit_arrow_contravariant.v unit_coercion_deptype.v unit_coercion_onoff.v diff --git a/tests/_CoqProject.std b/tests/_CoqProject.std index 6a2bba8..fb61acc 100644 --- a/tests/_CoqProject.std +++ b/tests/_CoqProject.std @@ -4,6 +4,8 @@ -R ../std/ Trocq -R ../elpi/ Trocq.Elpi +elpi/uvar_to_evar.v + unit_arrow_contravariant.v unit_coercion_deptype.v unit_coercion_onoff.v diff --git a/tests/elpi/uvar_to_evar.v b/tests/elpi/uvar_to_evar.v new file mode 100644 index 0000000..3875e95 --- /dev/null +++ b/tests/elpi/uvar_to_evar.v @@ -0,0 +1,45 @@ +(*****************************************************************************) +(* * Trocq *) +(* _______ * Copyright (C) 2023 Inria & MERCE *) +(* |__ __| * (Mitsubishi Electric R&D Centre Europe) *) +(* | |_ __ ___ ___ __ _ * Cyril Cohen *) +(* | | '__/ _ \ / __/ _` | * Enzo Crance *) +(* | | | | (_) | (_| (_| | * Assia Mahboubi *) +(* |_|_| \___/ \___\__, | ************************************************) +(* | | * This file is distributed under the terms of *) +(* |_| * GNU Lesser General Public License Version 3 *) +(* * see LICENSE file for the text of the license *) +(*****************************************************************************) + +From elpi Require Import elpi. +From Trocq Require Import Trocq. +From Trocq.Elpi Extra Dependency "util-rocq.elpi" as util_rocq. + +Elpi Command Test. +Elpi Accumulate File util_rocq. +Elpi Accumulate lp:{{ + +main _ :- std.spy-do! [ + util.uvar-to-evar UVar1 EVar1_, + var EVar1_, + util.uvar-to-evar UVar1 EVar1', + var EVar1', + % When called on the same uvar, the same Rocq evar is returned + same_term EVar1_ EVar1', + + util.uvar-to-evar UVar2_ EVar2_, + var EVar2_, + % But when called on a different uvar, a different Rocq evar is returned + not (same_term EVar1_ EVar2_), + + % Assigning to a uvar should not assign the evar + UVar1 = 2, + var Evar1_, + + % Calling uvar-to-evar on something else than a uvar raises an error + do-not-fail => not (util.uvar-to-evar 2 Evar3_), +]. + +}}. + +Elpi Test. \ No newline at end of file From b1dd0a4d1a4b29d7217f8e6f2af48016c75c9310 Mon Sep 17 00:00:00 2001 From: lucie <135951230+lweqx@users.noreply.github.com> Date: Mon, 12 Oct 2026 15:33:49 +0200 Subject: [PATCH 4/5] Add more error messages in fake-aterm.elpi --- elpi/fake-aterm.elpi | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/elpi/fake-aterm.elpi b/elpi/fake-aterm.elpi index 2848427..32c3d9a 100644 --- a/elpi/fake-aterm.elpi +++ b/elpi/fake-aterm.elpi @@ -42,6 +42,8 @@ typeabbrev fake-aterm term. % Converts a fake-map-class into a map-class. func fake-map-class->map-class fake-map-class -> map-class. +fake-map-class->map-class (uvar as E) _ :- !, + trocq.error "[fake-map-class->map-class] called on the evar" {coq.term->string E}. fake-map-class->map-class T N :- coq.env.global G T, trocq.db.map-class->indc-class N G, !. @@ -61,8 +63,8 @@ fake-aterm->aterm (app [PType|L]) (asort (typ U) Cl) :- fake-map-class->map-class TM M, fake-map-class->map-class TN N, if (var M, var N) - (Cl = _) - (Cl = pc M N), + (Cl = _) + (Cl = pc M N), coq.univ.new U. fake-aterm->aterm (app [PTriple|L]) Out :- trocq.data.ptriple PTriple, @@ -73,14 +75,14 @@ fake-aterm->aterm (app [PTriple|L]) Out :- %(Diag = ok, ! ; trocq.error {coq.term->string RX} "is not a relation between" {coq.term->string X} "and" {coq.term->string X'} Diag), !, RXR = RX, if (X = global G) - (OutT = aglobal G X' RXR) + (OutT = aglobal G X' RXR) (if (X = pglobal G U) - (OutT = apglobal G U X' RXR) - (OutT = atriple X X' RXR)), + (OutT = apglobal G U X' RXR) + (OutT = atriple X X' RXR)), std.map L' fake-aterm->aterm AL', aterm.util.mk-app OutT AL' Out. fake-aterm->aterm (sort S) (asort S Cl) :- !, - prune Cl []. %XXX Check wether those prunes will cause any trouble. + prune Cl []. %XXX Check whether those prunes will cause any trouble. fake-aterm->aterm (global G) (aglobal G G' RG) :- !, prune G' [], prune RG []. @@ -100,3 +102,7 @@ fake-aterm->aterm (prod Name T F) (aprod Name AT AF) :- !, fake-aterm->aterm (app L) (aapp AL) :- !, std.map L fake-aterm->aterm AL. fake-aterm->aterm X AX :- adecl AX _ X _ _ _, !. +fake-aterm->aterm (uvar as E) _ :- !, + trocq.error "[fake-aterm->aterm] cannot create an annotated term out of an evar:" {coq.term->string E}. +fake-aterm->aterm T _ :- + trocq.error "[fake-aterm->aterm] not implemented for the subterm:" {coq.term->string T}. From 786e77540341b5d33c4df94bd1799282581e1c9b Mon Sep 17 00:00:00 2001 From: lucie <135951230+lweqx@users.noreply.github.com> Date: Mon, 12 Oct 2026 15:35:12 +0200 Subject: [PATCH 5/5] Add logic to convert aterms into fake-aterms --- elpi/fake-aterm.elpi | 71 +++++++++++++++++++++--- tests/_CoqProject.hott | 1 + tests/_CoqProject.std | 1 + tests/elpi/fake_aterm.v | 120 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 186 insertions(+), 7 deletions(-) create mode 100644 tests/elpi/fake_aterm.v diff --git a/elpi/fake-aterm.elpi b/elpi/fake-aterm.elpi index 32c3d9a..c86a79a 100644 --- a/elpi/fake-aterm.elpi +++ b/elpi/fake-aterm.elpi @@ -18,6 +18,7 @@ accumulate "Trocq.Elpi/database". accumulate "Trocq.Elpi/data". accumulate "Trocq.Elpi/aterm". accumulate "Trocq.Elpi/util-rocq". +accumulate "Trocq.Elpi/class". % `@pi-adecl N AT (ax\ x\ x'\ xr\ F x x' xr)` declares elpi pi-variables x x' xr with adecl % x has annotated type AT @@ -37,7 +38,6 @@ macro @pi-adecl N PAD_AT PAD_F :- )))). typeabbrev fake-map-class term. -typeabbrev fake-param-class term. typeabbrev fake-aterm term. % Converts a fake-map-class into a map-class. @@ -48,12 +48,18 @@ fake-map-class->map-class T N :- coq.env.global G T, trocq.db.map-class->indc-class N G, !. -% Converts a fake-param-class into a param-class. -func fake-param-class->param-class fake-param-class -> param-class. -fake-param-class->param-class T Cl :- - coq.env.global G T, - trocq.db.rel Cl G _ _ _ _, - !. +% Converts a map-class into a fake-map-class. +% +% Unification variables as input are supported, returning a Rocq evar. +% If the unification variable is assigned, the Rocq evar will be resolved too. +func map-class->fake-map-class map-class -> fake-map-class. +map-class->fake-map-class (uvar as E) T :- !, std.do![ + util.uvar-to-evar E T, + declare_constraint (map-class->fake-map-class E T) [E] +]. +map-class->fake-map-class N T :- + trocq.db.map-class->indc-class N G, + coq.env.global G T, !. % Converts a fake-aterm into an aterm. func fake-aterm->aterm fake-aterm -> aterm. @@ -106,3 +112,54 @@ fake-aterm->aterm (uvar as E) _ :- !, trocq.error "[fake-aterm->aterm] cannot create an annotated term out of an evar:" {coq.term->string E}. fake-aterm->aterm T _ :- trocq.error "[fake-aterm->aterm] not implemented for the subterm:" {coq.term->string T}. + +func normalize-apps term -> term. +normalize-apps (app [app [A|L]|L']) T :- !, + std.append L L' L'', + normalize-apps (app [A|L'']) T. +normalize-apps T T. + +% Converts a fake-aterm into an aterm. +func aterm->fake-aterm aterm -> fake-aterm. +aterm->fake-aterm (asort (typ _U) PC) (app [PType, NT, MT]) :- !, + % TODO: the universe level U cannot be represented in the fake-aterm because PType is not universe-polymorphic. + trocq.data.ptype PType, + PC = pc N M, + map-class->fake-map-class N NT, + map-class->fake-map-class M MT. +aterm->fake-aterm (asort S _) _ :- !, + trocq.error "[aterm->fake-aterm] sort" S "cannot be represented via a fake-aterm.". +aterm->fake-aterm (atriple N N' NR) (app [PTriple, _, _, _, N, N', NR]) :- !, + trocq.data.ptriple PTriple. +aterm->fake-aterm (aglobal G G' RG) (app [PTriple, _, _, _, global G, G', RG]) :- !, + trocq.data.ptriple PTriple. +aterm->fake-aterm (apglobal G UI G' RG) (app [PTriple, _, _, _, pglobal G UI, G', RG]) :- !, + trocq.data.ptriple PTriple. +aterm->fake-aterm (afun N A B) (fun N A' B') :- !, std.do! [ + aterm->fake-aterm A A', + @pi-adecl N A (ax\ x\ _\ _\ + aterm->fake-aterm (B ax) (B' x) + ) +]. +aterm->fake-aterm (aprod N A B) (prod N A' B') :- !, std.do! [ + aterm->fake-aterm A A', + @pi-adecl N A (ax\ x\ _\ _\ + aterm->fake-aterm (B ax) (B' x) + ) +]. +aterm->fake-aterm (alet _ _ _ _) _ :- !, + trocq.error "[aterm->fake-aterm] unsupported construct match". +aterm->fake-aterm (aapp AL) T :- !, + std.map AL aterm->fake-aterm L, + % L could be `[app ..|..]`, so `app T` could be not normalized. + normalize-apps (app L) T. +aterm->fake-aterm (amatch _ _ _) _ :- !, + trocq.error "[aterm->fake-aterm] unsupported construct match". +aterm->fake-aterm (afix _ _ _ _) _ :- !, + trocq.error "[aterm->fake-aterm] unsupported construct afix". +aterm->fake-aterm (aprimitive _) _ :- !, + trocq.error "[aterm->fake-aterm] unsupported construct aprimitive". +aterm->fake-aterm (uvar as E) T :- !, + util.uvar-to-evar E T, !, + declare_constraint (aterm->fake-aterm E T) [E]. +aterm->fake-aterm AX X :- adecl AX _ X _ _ _, !. diff --git a/tests/_CoqProject.hott b/tests/_CoqProject.hott index 2ec372f..3169114 100644 --- a/tests/_CoqProject.hott +++ b/tests/_CoqProject.hott @@ -5,6 +5,7 @@ -R ../hott/ Trocq -R ../elpi/ Trocq.Elpi +elpi/fake_aterm.v elpi/uvar_to_evar.v unit_arrow_contravariant.v diff --git a/tests/_CoqProject.std b/tests/_CoqProject.std index fb61acc..ace6f76 100644 --- a/tests/_CoqProject.std +++ b/tests/_CoqProject.std @@ -4,6 +4,7 @@ -R ../std/ Trocq -R ../elpi/ Trocq.Elpi +elpi/fake_aterm.v elpi/uvar_to_evar.v unit_arrow_contravariant.v diff --git a/tests/elpi/fake_aterm.v b/tests/elpi/fake_aterm.v new file mode 100644 index 0000000..0a667fc --- /dev/null +++ b/tests/elpi/fake_aterm.v @@ -0,0 +1,120 @@ +(*****************************************************************************) +(* * Trocq *) +(* _______ * Copyright (C) 2023 Inria & MERCE *) +(* |__ __| * (Mitsubishi Electric R&D Centre Europe) *) +(* | |_ __ ___ ___ __ _ * Cyril Cohen *) +(* | | '__/ _ \ / __/ _` | * Enzo Crance *) +(* | | | | (_) | (_| (_| | * Assia Mahboubi *) +(* |_|_| \___/ \___\__, | ************************************************) +(* | | * This file is distributed under the terms of *) +(* |_| * GNU Lesser General Public License Version 3 *) +(* * see LICENSE file for the text of the license *) +(*****************************************************************************) + +From elpi Require Import elpi. +From Trocq Require Import Trocq. +From Trocq.Elpi Extra Dependency "fake-aterm.elpi" as fake_aterm. + +Axiom P : nat -> Type. + +Elpi Command Test. +Elpi Accumulate File fake_aterm. +Elpi Accumulate Db trocq.db. +Elpi Accumulate lp:{{ + +pred test-fake-map-class->map-class. +test-fake-map-class->map-class :- std.do! [ + fake-map-class->map-class {{ map0 }} map0, + fake-map-class->map-class {{ map1 }} map1, + fake-map-class->map-class {{ map2a }} map2a, + fake-map-class->map-class {{ map2b }} map2b, + fake-map-class->map-class {{ map3 }} map3, + fake-map-class->map-class {{ map4 }} map4, + % it does not support evars. + do-not-fail => not(fake-map-class->map-class {{ _ }} _) +]. + +pred test-map-class->fake-map-class. +test-map-class->fake-map-class :- std.do! [ + map-class->fake-map-class map0 {{ map0 }}, + map-class->fake-map-class map1 {{ map1 }}, + map-class->fake-map-class map2a {{ map2a }}, + map-class->fake-map-class map2b {{ map2b }}, + map-class->fake-map-class map3 {{ map3 }}, + map-class->fake-map-class map4 {{ map4 }}, + + % it supports uvars! + map-class->fake-map-class Uvar Evar, + var Evar, + % but if the uvar is assigned, the evar should be resolved. + Uvar = map0, not (var Evar), Evar = {{ map0 }} +]. + +pred test-fake-aterm->aterm. +test-fake-aterm->aterm :- std.do! [ + coq.locate "nat" Nat, + + % Sorts. Either the param-class of the sort is exactly specified via `PType`, or not if `Type` is used. In this case, the param-class is a unification variable. + fake-aterm->aterm {{ PType map3 map1 }} (asort (typ _) (pc map3 map1)), + fake-aterm->aterm {{ Type }} (asort (typ _) PC), var PC, + + % Triples (N, N', NR). + % Such a triple can be given exactly using `PTriple N N' NR`. + % In fake aterms, a global constant G is translated to a triple (G, G', GR) where G' and GR are unification variable. However, currently, triples of grefs are represented via aglobal instead of atriple. + fake-aterm->aterm {{ nat }} (aglobal Nat Nat' Rnat), var Nat', var Rnat, + fake-aterm->aterm {{ PTriple nat bool (fun _ _ => True) }} (aglobal Nat {{ bool }} {{ fun _ _ => True }}), + fake-aterm->aterm {{ PTriple (fun x: nat => x) (fun x: nat => x) (fun _ _ => True) }} ( + atriple {{ fun x: nat => x }} {{ fun x: nat => x }} {{ fun _ _ => True }} + ), + + % Lambda abstractions, foralls and applications. + fake-aterm->aterm {{ fun (n: nat) => P n }} (afun _ {fake-aterm->aterm {{ nat }}} (n\ + aapp [{fake-aterm->aterm {{ P }}}, n] + )), + fake-aterm->aterm {{ forall (n: nat), P n }} (aprod _ {fake-aterm->aterm {{ nat }}} (n\ + aapp [{fake-aterm->aterm {{ P }}}, n] + )), + + % Constructions not supported cause an error + do-not-fail => not (fake-aterm->aterm {{ _ }} _) +]. + +pred test-aterm->fake-aterm. +test-aterm->fake-aterm :- std.do! [ + coq.locate "nat" Nat, + Id = {{ fun x: nat => x }}, + FullRel = {{ fun _ _ => True }}, + + % Triples + aterm->fake-aterm (atriple Id Id FullRel) {{ PTriple lp:Id lp:Id lp:FullRel }}, + aterm->fake-aterm (aglobal Nat {{ nat }} FullRel) {{ PTriple nat nat lp:FullRel }}, + + % Sorts + aterm->fake-aterm (asort (typ _) (pc map4 map3)) {{ PType map4 map3 }}, + + % Lambda abstractions, foralls and applications. + aterm->fake-aterm (afun `n` {fake-aterm->aterm {{ nat }}} (n\ + aapp [{fake-aterm->aterm {{ P }}}, n] + )) {{ fun (n: PTriple nat _ _) => (PTriple P _ _) n }}, + aterm->fake-aterm (aprod `n` {fake-aterm->aterm {{ nat }}} (n\ + aapp [{fake-aterm->aterm {{ P }}}, n] + )) {{ forall (n: PTriple nat _ _), (PTriple P _ _) n }}, + + % Uvars are supported too. + aterm->fake-aterm Uvar T, + var T, + % and if the uvar is assigned, the evar should be assigned too. + Uvar = (asort (typ _) (pc map4 map3)), + not (var T), + T = {{ PType map4 map3 }} +]. + +main _ :- + test-fake-map-class->map-class, + test-map-class->fake-map-class, + test-fake-aterm->aterm, + test-aterm->fake-aterm. + +}}. + +Elpi Test. \ No newline at end of file