Skip to content

Commit

Permalink
🚀 Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ecranceMERCE committed Nov 10, 2023
0 parents commit 32ed566
Show file tree
Hide file tree
Showing 35 changed files with 5,713 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.elpi linguist-language=prolog
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.*.aux
.lia.cache
*.glob
*.vo
*.vok
*.vos
*~
.*.d
Makefile.coq*
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Changelog
165 changes: 165 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.


This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.

0. Additional Definitions.

As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.

"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.

An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.

A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".

The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.

The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.

1. Exception to Section 3 of the GNU GPL.

You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.

2. Conveying Modified Versions.

If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:

a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or

b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.

3. Object Code Incorporating Material from Library Header Files.

The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:

a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.

b) Accompany the object code with a copy of the GNU GPL and this license
document.

4. Combined Works.

You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:

a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.

b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.

c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.

d) Do one of the following:

0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.

1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.

e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)

5. Combined Libraries.

You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:

a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.

b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.

6. Revised Versions of the GNU Lesser General Public License.

The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.

Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.

If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.PHONY: all

all: Makefile.coq
$(MAKE) -f Makefile.coq all

Makefile.coq:
coq_makefile -f _CoqProject -o Makefile.coq

%:: Makefile.coq
$(MAKE) -f Makefile.coq $@
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Trocq

This repository contains Trocq, a modular parametricity plugin for proof transfer in Coq.

NB: it relies on a [custom version of Coq-Elpi](https://github.com/ecranceMERCE/coq-elpi/tree/strat), mainly changing its behaviour regarding the translation of universe variables between Elpi and Coq.
22 changes: 22 additions & 0 deletions _CoqProject
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-arg -noinit
-arg -indices-matter

-R theories/ Trocq
-R elpi/ Trocq.Elpi

theories/HoTT_additions.v
theories/Hierarchy.v
theories/Param_Type.v
theories/Param_forall.v
theories/Param_arrow.v
theories/Database.v
theories/Param.v
theories/Trocq.v
theories/Param_paths.v

examples/Example.v
examples/int_to_Zp.v
examples/peano_bin_nat.v
examples/setoid_rewrite.v
examples/summable.v
examples/trocq_setoid_rewrite.v
39 changes: 39 additions & 0 deletions coq-trocq.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
opam-version: "2.0"
name: "coq-trocq"
version: "dev"
maintainer: "Enzo Crance <[email protected]>"
authors: [ "Cyril Cohen", "Enzo Crance", "Assia Mahboubi" ]
homepage: "https://github.com/ecranceMERCE/trocq"
bug-reports: "https://github.com/ecranceMERCE/trocq/issues"
dev-repo: "https://github.com/ecranceMERCE/trocq.git"
# doc: "https://ecranceMERCE.github.io/trocq"
build: [ make "-j%{jobs}%" ]
install: [ make "install" ]
depends: [
"coq" {>= "8.17.0" & < "8.18~" }
"coq-elpi" {= "dev"}
"coq-hott" {>= "8.17" & < "8.18~"}
]
tags: [
"category:Computer Science/Decision Procedures and Certified Algorithms/Decision procedures"
"category:Miscellaneous/Coq Extensions"
"keyword:automation"
"keyword:elpi"
"date:2023-11-10"
"logpath:Trocq"
]
synopsis: "A modular parametricity plugin for proof transfer in Coq"
description: """
Trocq is a prototype of modular parametricity plugin for Coq, aiming to perform proof transfer by translating the goal into an associated goal featuring the target data structures as well as a rich parametricity witness from which a function justifying the goal substitution can be extracted.

The plugin features a hierarchy of parametricity witness types, ranging from structure-less relations to a new formulation of type equivalence, gathering several pre-existing parametricity translations, among which univalent parametricity (*), under the same framework.

This modular translation performs a fine-grained analysis and generates witnesses that are rich enough to preprocess the goal yet are not always a full-blown type equivalence, allowing to perform proof transfer with the power of univalent parametricity, but trying not to pull in the univalence axiom in cases where it is not required.

The translation is implemented in Coq-Elpi and features transparent and readable code with respect to a sequent-style theoretical presentation.

(*) The marriage of univalence and parametricity, Tabareau et al. (2021)
"""
url {
src: "git+https://github.com/ecranceMERCE/trocq.git"
}
133 changes: 133 additions & 0 deletions elpi/annot.elpi
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % Trocq %
% _______ % Copyright (C) 2023 MERCE %
% |__ __| % (Mitsubishi Electric R&D Centre Europe) %
% | |_ __ ___ ___ __ _ % Cyril Cohen <[email protected]> %
% | | '__/ _ \ / __/ _` | % Enzo Crance <[email protected]> %
% | | | | (_) | (_| (_| | % Assia Mahboubi <[email protected]> %
% |_|_| \___/ \___\__, | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% | | % This file is distributed under the terms of the %
% |_| % GNU Lesser General Public License Version 3 %
% % (see LICENSE file for the text of the license) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --------------------------------------------------------------------------------------------------
% annotated type theory
% --------------------------------------------------------------------------------------------------

% this allows to easily make sure an application is beta-reduced
:before "subst-fun:fail"
coq.subst-fun Args F FArgs :- !, coq.mk-app F Args FArgs.

% annotate a term
% NB: the output type is term because the annotations are encoded directly in Coq
% see PType in Param.v
pred term->annot-term i:term, o:term.
term->annot-term (sort (typ U)) (app [pglobal (const PType) UI, M, N]) :- param.db.ptype PType, !,
coq.univ-instance UI [{coq.univ.variable U}],
cstr.univ-link _ M N.
term->annot-term (fun N T F) (fun N T' F') :- !,
term->annot-term T T',
pi x\ term->annot-term (F x) (F' x).
term->annot-term (prod N T F) (prod N T' F') :- !,
term->annot-term T T',
pi x\ term->annot-term (F x) (F' x).
term->annot-term (app L) (app L') :- !,
std.map L term->annot-term L'.
term->annot-term X X :- (name X ; X = global _ ; X = pglobal _ _), !.
term->annot-term T _ :- coq.error "term->annot-term:" T.

macro @annot-pi-decl N T F :- pi x\ annot.adecl x N T => F x.

namespace annot {

% typechecking a term calls Coq's typechecker, which gives back a non annotated term
% calling term->annot-term on it will add fresh annotations
% if we want to typecheck a variable several times and get the same type everytime, we need to
% add some memory, which is the purpose of adecl
pred adecl i:term, o:name, o:term.

pred typecheck i:term, o:term.
typecheck T ATy :- adecl T _ ATy, !.
typecheck T ATy :-
coq.typecheck T Ty ok, !,
term->annot-term Ty ATy.

% sub-typing predicate following the sub-typing rules in the article
pred sub-type i:term, i:term.
% SubUniv
sub-type (app [pglobal (const PType) _, M1, N1]) (app [pglobal (const PType) _, M2, N2]) :-
param.db.ptype PType, !,
cstr.univ-link C1 M1 N1,
cstr.univ-link C2 M2 N2,
cstr.geq C1 C2.
% SubPi
sub-type (prod N T F) (prod _ T' F') :- !,
sub-type T' T,
@annot-pi-decl N T' x\ sub-type (F x) (F' x).
% SubLam
sub-type (fun N T F) (fun _ T F') :- !,
@annot-pi-decl N T x\ sub-type (F x) (F' x).
% SubApp
sub-type (app [F|Xs]) (app [F'|Xs']) :- !,
sub-type F F',
std.forall2 Xs Xs' eq.
% SubConv
sub-type X X :- (name X ; X = global _ ; X = pglobal _ _), !.
sub-type X _ :- coq.error "annot.sub-type:" X.

% syntactic term equality, taking care of adding annotation equalities in the constraint graph
pred eq i:term, i:term.
eq (app [pglobal (const PType) UI, M1, N1]) (app [pglobal (const PType) UI, M2, N2]) :-
param.db.ptype PType, !,
cstr.univ-link C1 M1 N1,
cstr.univ-link C2 M2 N2,
cstr.eq C1 C2.
eq (prod _ T F) (prod _ T' F') :- !,
eq T T',
pi x\ eq (F x) (F' x).
eq (fun _ T F) (fun _ T' F') :- !,
eq T T',
pi x\ eq (F x) (F' x).
eq (app L) (app L') :- !,
std.forall2 L L' eq.
eq X X :- (name X ; X = global _ ; X = pglobal _ _), !.
eq X _ :- coq.error "annot.eq:" X.

% get all the annotations in a type T, as well as the "output class", i.e., the parametricity class
% of the output type of T, as an option basically returning some (A,B) for an output type PType A B,
% and none for output types that are not sorts, because it means values of type T are not type
% constructors, so their translation will be made at class (0,0)
pred classes i:term, o:list param-class, o:option param-class.
classes T Cs' Out :-
all-classes T Cs,
out-class T Out,
if (not (Cs = []), Out = some C, std.last Cs LastC, LastC == C) (
std.drop-last 1 Cs Cs'
) (
Cs' = Cs
).

pred all-classes i:term, o:list param-class.
all-classes (app [pglobal (const PType) _, M, N]) [C] :- param.db.ptype PType, !,
cstr.univ-link C M N.
all-classes X Cs :- (X = prod _ T F ; X = fun _ T F), !,
pi x\ std.append {all-classes T} {all-classes (F x)} Cs.
all-classes (app L) Cs :- !,
std.flatten {std.map L all-classes} Cs.
all-classes X [] :- (name X ; X = global _ ; X = pglobal _ _), !.
all-classes X _ :- coq.error "all-classes:" X.

pred out-class i:term, o:option param-class.
out-class (app [pglobal (const PType) _, M, N]) (some C) :- param.db.ptype PType, !,
cstr.univ-link C M N.
out-class (prod _ _ F) Out :- !, pi x\ out-class (F x) Out.
out-class (fun _ _ _) none :- !.
out-class (app [F|Xs]) Out :- !,
coq.subst-fun Xs F App,
if (App = app [F|Xs]) (Out = none) (out-class App Out).
out-class X none :- (name X ; X = global _ ; X = pglobal _ _), !.
out-class X _ :- coq.error "out-class:" X.
% constant applications convertible to terms having a sort as output are not supported

} % annot
Loading

0 comments on commit 32ed566

Please sign in to comment.