Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions docs/elpi-dependencies.dot
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ digraph {
"util-rocq" [URL="https://github.com/rocq-community/trocq/blob/master/elpi/util-rocq.elpi"];
"database" [shape=box, URL="https://github.com/rocq-community/trocq/blob/master/elpi/database.elpi"];
"aterm" [URL="https://github.com/rocq-community/trocq/blob/master/elpi/aterm.elpi"];
"fake-aterm" [URL="https://github.com/rocq-community/trocq/blob/master/elpi/fake-aterm.elpi"];
"aterm-type" [shape=box, URL="https://github.com/rocq-community/trocq/blob/master/elpi/aterm-type.elpi"];
"c/constraints" [URL="https://github.com/rocq-community/trocq/blob/master/elpi/constraints/constraints.elpi"];
"constraints" [URL="https://github.com/rocq-community/trocq/blob/master/elpi/constraints.elpi"];
Expand All @@ -25,13 +26,14 @@ digraph {
"vernac" [URL="https://github.com/rocq-community/trocq/blob/master/elpi/vernac.elpi"];

## DEPENDENCIES
"util" -> {"database" "util-rocq"};
"logging" -> {"aterm"};
"util" -> {"database"};
"logging" -> {"util-rocq"};
"util-rocq" -> {"param-class-util"};
"class" -> {"aterm-type"};
"database" -> {"logging" "param-class-util"};
"aterm" -> {"c/constraints"};
"aterm-type" -> {"database" "util-rocq"};
"aterm" -> {"c/constraints" "fake-aterm"};
"aterm-type" -> {"database"};
"fake-aterm" -> {"vernac"}
"constraints" -> "annot";
"c/constraints" -> "c/simple-graph";
"c/simple-graph" -> "c/constraint-graph";
Expand Down
307 changes: 158 additions & 149 deletions docs/elpi-dependencies.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 1 addition & 67 deletions elpi/aterm.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
% -----------------------------------------------------------------------------

accumulate "Trocq.Elpi/data".
accumulate "Trocq.Elpi/logging".

% `@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
Expand Down Expand Up @@ -296,54 +295,6 @@ namespace aterm.util {
annotate-legacy (pglobal G U) [] none (apglobal G U TG TG) :-
coq.env.global G TG.


func fake-term->aterm term -> aterm.
fake-term->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-term->aterm (app [PTriple|L]) Out :-
trocq.data.ptriple PTriple,
L = [_,_,_,X,X',RX|L'], !,
std.map L' fake-term->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-term->aterm AL',
aterm.util.mk-app OutT AL' Out.
fake-term->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) :- !,
prune G' [],
prune RG [].
fake-term->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,
@pi-adecl Name AT (ay\ y\ _\ _\
fake-term->aterm (F y) (AF ay)
).
fake-term->aterm (prod Name T F) (aprod Name AT AF) :- !,
fake-term->aterm T AT,
@pi-adecl Name AT (ay\ y\ _\ _\
fake-term->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 _ _ _, !.

% From a full aterm in the database, instantiates a aterm with holes
func equals-ignore aterm -> aterm.
equals-ignore X X' :-
Expand Down Expand Up @@ -441,21 +392,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 _ (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.
}
}
12 changes: 0 additions & 12 deletions elpi/data.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,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 {
Expand Down
161 changes: 161 additions & 0 deletions elpi/fake-aterm.elpi
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % Trocq %
% _______ % Copyright (C) 2023 Inria & MERCE %
% |__ __| % (Mitsubishi Electric R&D Centre Europe) %
% | |_ __ ___ ___ __ _ % Cyril Cohen <cyril.cohen@inria.fr> %
% | | '__/ _ \ / __/ _` | % Enzo Crance <enzo.crance@inria.fr> %
% | | | | (_) | (_| (_| | % Assia Mahboubi <assia.mahboubi@inria.fr> %
% |_|_| \___/ \___\__, | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% | | % 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/aterm".

% `@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.raw-triple PAD_AT PAD_T PAD_T' PAD_RT,
util.name-suffix N "'" N',
util.name-suffix N "R" NR,
@pi-decl N PAD_T x\ (
@pi-decl N' PAD_T' x'\ (
util.mk-app PAD_RT [x, x'] (PAD_RTx x x'),
@pi-decl NR (PAD_RTx x x') 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-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, !.

% 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.
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 whether 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 _ _ _, !.
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 _ _ _, !.
29 changes: 27 additions & 2 deletions elpi/util-rocq.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
% utility predicates related to Rocq
% -----------------------------------------------------------------------------

accumulate "Trocq.Elpi/util".
accumulate "Trocq.Elpi/aterm-type".
accumulate "Trocq.Elpi/logging".

% Some of the Coq native predicates raise errors in Elpi, stopping all
% ongoing computations. Adding a custom flag like do-not-fail gives us an
Expand Down Expand Up @@ -197,6 +196,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)
Expand Down
3 changes: 2 additions & 1 deletion elpi/vernac.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

accumulate "Trocq.Elpi/annot".
accumulate "Trocq.Elpi/fake-aterm".

% API

Expand Down Expand Up @@ -75,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
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).

Expand Down
3 changes: 3 additions & 0 deletions tests/_CoqProject.hott
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
-R ../hott/ Trocq
-R ../elpi/ Trocq.Elpi

elpi/fake_aterm.v
elpi/uvar_to_evar.v

unit_arrow_contravariant.v
unit_coercion_deptype.v
unit_coercion_onoff.v
Expand Down
3 changes: 3 additions & 0 deletions tests/_CoqProject.std
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
-R ../std/ Trocq
-R ../elpi/ Trocq.Elpi

elpi/fake_aterm.v
elpi/uvar_to_evar.v

unit_arrow_contravariant.v
unit_coercion_deptype.v
unit_coercion_onoff.v
Expand Down
Loading
Loading