Skip to content

Instance before structure #475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 37 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
2d7d478
test showing the problem
gares Feb 7, 2023
1e51163
first try on modifying HB
ThomasPortet Feb 14, 2023
bd2a356
fixing bug on issue #335 should work but no tests yet
ThomasPortet Feb 15, 2023
90559fc
changing conflicting predicate name and completing the test for that …
ThomasPortet Feb 15, 2023
1dfc12c
completing the test
ThomasPortet Feb 15, 2023
c7f0e3e
starting to write the function for #336
ThomasPortet Feb 15, 2023
a832dbf
taking out duplicates for the function in #336 and including a test f…
ThomasPortet Feb 16, 2023
62378c1
starting to write function in #337
ThomasPortet Feb 16, 2023
45153aa
#335
ThomasPortet Feb 17, 2023
77cc438
#335
ThomasPortet Feb 20, 2023
3a6341b
implements the new approach mentioned in #335 and adds the steps in #…
ThomasPortet Feb 22, 2023
fd7d32b
starting to work on the undup function for list of terms with sets
ThomasPortet Feb 22, 2023
050163a
working on undup function and starting to rewrite the test for it
ThomasPortet Feb 23, 2023
051df0c
rewriting the function to remove duplicates and the test for it
ThomasPortet Feb 24, 2023
6815e50
typo
ThomasPortet Feb 27, 2023
0f428cb
first draft at modifying the mixin src clauses for type parameters
ThomasPortet Mar 13, 2023
071619a
continuing the transformation of clauses
ThomasPortet Mar 14, 2023
d2a8a5f
working on saturating instances with types
ThomasPortet Mar 14, 2023
d79445b
starting to work on #347
ThomasPortet Mar 15, 2023
c499513
changing how the boolean list works : with section parameters
ThomasPortet Mar 17, 2023
4025a2c
writing the boolean computing functions
ThomasPortet Mar 22, 2023
3c7b01a
fixing mixin-src->has-mixin-instance
ThomasPortet Mar 22, 2023
c167f78
writing the find instance function
ThomasPortet Mar 23, 2023
7a83809
reorganising declare all and working on mk-src
ThomasPortet Mar 27, 2023
5675a26
modifying how we get the first argument of mixin-src : it's pre-proce…
ThomasPortet Mar 28, 2023
3c04b9f
using the right types to declare classes
ThomasPortet Mar 28, 2023
9621cad
fixing the list of type upon which we call declare-all
ThomasPortet Mar 29, 2023
fdb2f58
starting to rewrite mk-src
ThomasPortet Mar 29, 2023
3532146
fixing mk-src, still need to check find-instance
ThomasPortet Mar 30, 2023
06268e3
fixing clause generation
ThomasPortet Mar 31, 2023
cc36c74
saturating instance works !
ThomasPortet Mar 31, 2023
3febe7f
cleaning up code and adding comments and a unit test
ThomasPortet Apr 5, 2023
c51263d
creating unit tests and cleaning up code
ThomasPortet Apr 7, 2023
6450bf4
more tests
ThomasPortet Apr 11, 2023
971749f
more tests and fixing the instance parameters case
ThomasPortet Apr 12, 2023
64b5ee9
fixing coqproject and fixing a bug on mk-src
ThomasPortet Apr 13, 2023
7fd35ef
changing prints into warnings to allow test suite to work
ThomasPortet Apr 13, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ _minted-*
*.vtc

*.dot
*.json
151 changes: 148 additions & 3 deletions HB/common/database.elpi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Hierarchy Builder: algebraic hierarchies made easy
This software is released under the terms of the MIT license */

shorten coq.{ term->gref, subst-fun, safe-dest-app, mk-app, mk-eta, subst-prod }.
shorten coq.{ term->gref, term-is-gref?, subst-fun, safe-dest-app, mk-app, mk-eta, subst-prod }.

%%%%%%%%% HB database %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Expand All @@ -14,12 +14,29 @@ from_mixin (from _ X _) X.
pred from_builder i:prop, o:term.
from_builder (from _ _ X) (global X).

pred has-mixin-instance_cspat i:prop, o:cs-pattern.
has-mixin-instance_cspat (has-mixin-instance P _ _ _) P.

pred mixin-src_type i:prop, o:term.
mixin-src_type (mixin-src T _ _ ) T.

pred mixin-src-w-cond_type i:prop, o:term.
mixin-src-w-cond_type(mixin-src (global G) _ _ :- _) (global G).
mixin-src-w-cond_type (mixin-src (app [T|_]) _ _ :- _) T.
mixin-src-w-cond_type (pi x \ (C x)) T :- pi y \ mixin-src-w-cond_type (C y) T.

pred mixin-src_ty i:prop, o:term.
mixin-src_ty (mixin-src T _ _ ) Ty :-coq.typecheck T Ty ok.

pred mixin-src_mixin i:prop, o:mixinname.
mixin-src_mixin (mixin-src _ M _) M.

pred mixin-src_src i:prop, o:term.
mixin-src_src (mixin-src _ _ S) S.

pred mixin-src_srcty i:prop, o:term.
mixin-src_src (mixin-src _ _ S) Ty :- coq.env.typeof {coq.prod-tgt->gref S} Ty .

pred local-canonical-gref i:prop, o:constant.
local-canonical-gref (local-canonical C) C.

Expand All @@ -32,6 +49,9 @@ class_structure (class _ S _) S.
pred class-def_name i:prop, o:classname.
class-def_name (class-def (class N _ _)) N.

pred has-db-instance_type i:prop, o:term.
has-db-instance_type (has-db-instance TheType) TheType.

pred mixin-class_class i:prop, o:classname.
mixin-class_class (mixin-class _ C) C.

Expand Down Expand Up @@ -133,6 +153,28 @@ factories-provide FLwP MLwP :- std.do! [
w-params.map UnsortedMLwP (p\t\ toposort-mixins) MLwP,
].

pred undup-grefs i:list gref, o:list gref.
undup-grefs L UL :- std.do! [
coq.gref.list->set L S,
coq.gref.set.elements S UL,
].

pred undup-terms i:list term, o:list term.
undup-terms L UL :- std.do! [
std.map L coq.term->gref LG,
undup-grefs LG ULG,
std.map ULG (coq.env.global ) UL,
].

% % finds all unique types for which an instance was created
pred findall-cs-types o:list term.
findall-cs-types TypeList :- std.do![
std.findall (has-db-instance _) Clauses,
std.map Clauses has-db-instance_type TypeListDup,
undup-terms TypeListDup TypeList,
].


% Mixins can be topologically sorted according to their dependencies
pred toposort-mixins i:list (w-args mixinname), o:list (w-args mixinname).
toposort-mixins In Out :- std.do! [
Expand Down Expand Up @@ -192,10 +234,18 @@ findall-builders LFIL :-
std.map {std.findall (builder-decl B_)} extract-builder LFILunsorted,
std.bubblesort LFILunsorted leq-builder LFIL.

pred findall-has-mixin-instance o:list prop.
findall-has-mixin-instance CL :-
std.findall (has-mixin-instance _ _ _ _) CL.

pred findall-type-src o:list cs-pattern.
findall-type-src TL :-
std.map {std.findall (has-mixin-instance T_ M_ S_ L_)} has-mixin-instance_cspat TL.

pred findall-mixin-src i:term, o:list mixinname.
findall-mixin-src T ML :-
std.map {std.findall (mixin-src T M_ V_)} mixin-src_mixin ML.

pred findall-local-canonical o:list constant.
findall-local-canonical CL :-
std.map {std.findall (local-canonical C_)} local-canonical-gref CL.
Expand Down Expand Up @@ -268,6 +318,7 @@ assert-good-coverage! MLSortedRev CNL :- std.do! [
true
].


%%%%% Coq Database %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% [get-structure-coercion S1 S2 F] finds the coecion F from the structure S1 to S2
Expand Down Expand Up @@ -295,7 +346,7 @@ get-constructor (indt R) (indc K) :- !,
if (coq.env.indt R _ _ _ _ [K] _) true (coq.error "Not a record" R).
get-constructor I _ :- coq.error "get-constructor: not an inductive" I.

%% finding for locally defined structures
% finding for locally defined structures
pred get-cs-structure i:cs-instance, o:structure.
get-cs-structure (cs-instance _ _ (const Inst)) Struct :- std.do! [
coq.env.typeof (const Inst) InstTy,
Expand All @@ -309,6 +360,100 @@ get-cs-instance (cs-instance _ _ (const Inst)) Inst.
pred has-cs-instance i:gref, i:cs-instance.
has-cs-instance GTy (cs-instance _ (cs-gref GTy) _).

pred param-in-term i:term, i:constant, o:bool.
param-in-term (app [global (const F)]) C tt :- (coq.gref->id (const F) ID), (coq.gref->id (const C) ID).
param-in-term (app [_|Args]) C B :- param-in-term (app Args) C B.
param-in-term (app []) _ ff.
param-in-term (prod _ _ F) C B :- pi x\ param-in-term (F x) C B.
param-in-term (sort _) _ ff.
param-in-term T C B :- whd1 T T1, !, param-in-term T1 C B.

% given a list of section parameters SectionParams and a term T,
% returns a list of bool corresponding to whether or not each parameter appears in T.
pred sect-params-in-term i:list constant, i:term, o:list bool.
sect-params-in-term SectionParams T L :-
safe-dest-app T (global GR) Args,
factory-alias->gref GR F, factory-nparams F NP, std.split-at NP Args Params _Args2,
TyP = app [global GR|Params],
std.map SectionParams (x\ o \ param-in-term TyP x o) L.

pred has-mixin-instance o:cs-pattern, o:mixinname, o:gref, o:list bool.


pred mixin-src->has-mixin-instance i:list constant, i:prop, o:prop.

mixin-src->has-mixin-instance SectionParams (mixin-src (global GR) M I) (has-mixin-instance (cs-gref GR) M IHd A) :-
term->gref I IHd, coq.typecheck I Ty ok, sect-params-in-term SectionParams Ty A.

mixin-src->has-mixin-instance SectionParams (mixin-src (app [global GR|_] ) M I) (has-mixin-instance (cs-gref GR) M IHd A) :-
term->gref I IHd, coq.typecheck I Ty ok, sect-params-in-term SectionParams Ty A.

mixin-src->has-mixin-instance SectionParams (mixin-src (prod _ _ _ ) M I) (has-mixin-instance cs-prod M IHd BL):-
term->gref I IHd, coq.typecheck I Ty ok, sect-params-in-term SectionParams Ty BL.

mixin-src->has-mixin-instance SectionParams (mixin-src (sort U) M I) (has-mixin-instance (cs-sort U) M IHd BL):-
term->gref I IHd, coq.typecheck I Ty ok, sect-params-in-term SectionParams Ty BL.


pred mk-src-aux i:list term, i:term, i:mixinname, i:term, i:list prop, o:prop.
mk-src-aux [] T M I Cond (mixin-src T M I :- Cond).
mk-src-aux [A|Rest] T M I Cond ( pi a \ C a) :-
pi a \
sigma Ta\
coq.mk-app T [a] Ta,
mk-src-aux Rest Ta M I [coq.unify-eq A a ok|Cond] (C a).

% transforms the has-mixin-instance clause arguments into a
% mixin-src clause with eventuals conditions regarding its parameters
pred mk-src
i:term, % type of the instance Ty
i:mixinname, % name of mixin
i:term, % instance body I of type Ty
i:list bool, % list of bool for each param, true if they're instance parameters
i:list prop, % Cond list
o:prop.

mk-src (app [_|Args]) M I [] Cond C :-
std.last Args Last,
safe-dest-app Last Hd ArgsLast,
mk-src-aux ArgsLast Hd M I Cond C.

mk-src (prod N_ _ F) M I [ff|Rest] Cond (pi a \ C a) :-
pi a\
sigma Ia \
coq.mk-app I [a] Ia,
mk-src (F a) M Ia Rest Cond (C a).

mk-src (prod N_ _ F) M I [tt|Rest] Cond C :-
sigma a Ia \
coq.mk-app I [a] Ia,
mk-src (F a) M Ia Rest Cond C.

mk-src (prod N_ _ F) M I [] Cond (pi a \ C a) :-
pi a\
sigma Ia \
coq.mk-app I [a] Ia,
mk-src (F a) M Ia [] Cond (C a).

% for a type T, create as many holes as there are foralls and returns that enriched type
pred enrich-type i:term, o:term .
enrich-type T ET :-
coq.typecheck T TH ok,
coq.count-prods TH N,
if (N = 0) (ET = T) (coq.mk-app T {coq.mk-n-holes N} ET).

% wrapper for mk-src so it can be called by a map on a list of clauses has-mixin-instance
pred mk-src-map i:prop, o:prop.
mk-src-map (has-mixin-instance _ M IHd L) C :- std.do![
T = global IHd,
coq.env.typeof IHd Ty,
mk-src Ty M T L [] C,
].

pred cs-pattern->term i:cs-pattern, o:term.
cs-pattern->term (cs-gref GR) (global GR).
cs-pattern->term (cs-sort U) (sort U).

pred term->cs-pattern i:term, o:cs-pattern.
term->cs-pattern (prod _ _ _) cs-prod.
term->cs-pattern (sort U) (cs-sort U).
Expand Down
6 changes: 6 additions & 0 deletions HB/common/stdpp.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ pred coq.env.current-library o:string.
coq.env.current-library L :- loc.fields {get-option "elpi.loc"} L _ _ _ _.
coq.env.current-library "dummy.v".

pred coq.term-is-gref? i:term, o:gref.
coq.term-is-gref? (global GR) GR :- !.
coq.term-is-gref? (pglobal GR _) GR :- !.
coq.term-is-gref? (app [Hd|_]) GR :- !, coq.term-is-gref? Hd GR.
coq.term-is-gref? (let _ _ T x\x) GR :- !, coq.term-is-gref? T GR.

pred coq.prod-tgt->gref i:term, o:gref.
coq.prod-tgt->gref T GR :- whd1 T T1, !, coq.prod-tgt->gref T1 GR.
coq.prod-tgt->gref (prod N Src Tgt) GR :- !, @pi-decl N Src x\ coq.prod-tgt->gref (Tgt x) GR.
Expand Down
5 changes: 4 additions & 1 deletion HB/common/utils.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,7 @@ prod-last X X :- !.

pred prod-last-gref i:term, o:gref.
prod-last-gref (prod N S X) GR :- !, @pi-decl N S x\ prod-last-gref (X x) GR.
prod-last-gref X GR :- coq.term->gref X GR.
prod-last-gref X GR :- coq.term->gref X GR.

pred prod-structure i:term, o:term.
prod-structure (prod _ S _) S.
2 changes: 1 addition & 1 deletion HB/factory.elpi
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ argument->w-mixins (ctx-decl Decl) (pr MLwP ArgwP) :- !, std.do! [
pred argument-name i:argument, o:string.
argument-name (const-decl Id _ _) Id :- !.
argument-name (indt-decl (parameter _ _ _ R)) Id :- !,
argument-name (indt-decl (R (sort prop))) Id.
argument-name (indt-decl (R (sort prop))) Id.
argument-name (indt-decl (record Id _ _ _)) Id :- !.
argument-name (indt-decl (inductive Id _ _ _)) Id :- !.
argument-name (ctx-decl _) "_" :- !.
Expand Down
Loading
Loading