11/* Hierarchy Builder: algebraic hierarchies made easy
22 This software is released under the terms of the MIT license */
33
4- shorten coq.{ term->gref, term-is-gref?, subst-fun, safe-dest-app, mk-app, mk-eta, subst-prod }.
4+ shorten coq.{ term->gref, subst-fun, safe-dest-app, mk-app, mk-eta, subst-prod }.
55
66%%%%%%%%% HB database %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77
@@ -14,13 +14,6 @@ from_mixin (from _ X _) X.
1414pred from_builder i:prop, o:term.
1515from_builder (from _ _ X) (global X).
1616
17- % for a given clause mixin-src with eventually a condition,
18- % returns the type of the first argument
19- pred mixin-src-w-cond_type i:prop, o:term.
20- mixin-src-w-cond_type(mixin-src (global G) _ _ :- _) (global G).
21- mixin-src-w-cond_type (mixin-src (app [T|_]) _ _ :- _) T.
22- mixin-src-w-cond_type (pi x \ (C x)) T :- pi y \ mixin-src-w-cond_type (C y) T.
23-
2417pred mixin-src_mixin i:prop, o:mixinname.
2518mixin-src_mixin (mixin-src _ M _) M.
2619
@@ -146,16 +139,31 @@ undup-grefs L UL :- std.do! [
146139 coq.gref.set.elements S UL,
147140].
148141
149- % remove duplicates from a list of terms, but all the terms have to be global references
150- pred undup-terms i:list term, o:list term.
151- undup-terms L UL :- std.do! [
152- std.map L coq.term->gref LG,
153- undup-grefs LG ULG,
154- std.map ULG (coq.env.global ) UL,
142+ pred undup-sorts i:list sort, o:list sort.
143+ undup-sorts L R :- std.do! [
144+
145+ if (std.mem L prop) (R1 = [prop]) (R1 = []),
146+ if (std.mem L sprop) (R2 = [sprop]) (R2 = []),
147+ if (std.mem L (typ _)) (R3 = [typ _]) (R3 = []),
148+
149+ std.flatten [R1, R2, R3] R,
155150].
156151
152+ % also prunes cs-default
153+ pred undup-cs-patterns i:list cs-pattern, o:list cs-pattern.
154+ undup-cs-patterns L R :- std.do! [
155+ std.map-filter L (x\r\ x = cs-gref r) LGR,
156+ undup-grefs LGR ULGR,
157+ std.map ULGR (x\r\ r = cs-gref x) R1,
157158
159+ std.map-filter L (x\r\ x = cs-sort r) LS,
160+ undup-sorts LS ULS,
161+ std.map ULS (x\r\ r = cs-sort x) R2,
158162
163+ if (std.mem L cs-prod) (R3 = [cs-prod]) (R3 = []),
164+
165+ std.flatten [R1, R2, R3] R,
166+ ].
159167
160168% Mixins can be topologically sorted according to their dependencies
161169pred toposort-mixins i:list (w-args mixinname), o:list (w-args mixinname).
@@ -220,10 +228,13 @@ pred findall-has-mixin-instance o:list prop.
220228findall-has-mixin-instance CL :-
221229 std.findall (has-mixin-instance _ _ _) CL.
222230
231+ pred has-mixin-instance_key i:prop, o:cs-pattern.
232+ has-mixin-instance_key (has-mixin-instance P _ _) P.
233+
223234pred findall-mixin-src i:term, o:list mixinname.
224235findall-mixin-src T ML :-
225236 std.map {std.findall (mixin-src T M_ V_)} mixin-src_mixin ML.
226-
237+
227238pred findall-local-canonical o:list constant.
228239findall-local-canonical CL :-
229240 std.map {std.findall (local-canonical C_)} local-canonical-gref CL.
@@ -300,7 +311,6 @@ assert-good-coverage! MLSortedRev CNL :- std.do! [
300311 true
301312].
302313
303-
304314%%%%% Coq Database %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305315
306316% [get-structure-coercion S1 S2 F] finds the coecion F from the structure S1 to S2
@@ -359,72 +369,47 @@ mixin-src->has-mixin-instance (mixin-src (sort U) M I) (has-mixin-instance (cs-s
359369% this auxiliary function iterates over the list of arguments of an application,
360370% and create the necessary unify condition for each arguments
361371% and at the end returns the mixin-src clause with all the conditions
362- pred mk-src- aux
372+ pred mixin-instance-type->mixin-src. aux
363373 i:list term, % list of arguments
364374 i:term, % head of the original application
365375 i:mixinname, % name of mixin
366376 i:term, % instance body
367377 i:list prop, % Cond list
368378 o:prop.
369- mk-src- aux [] T M I Cond (mixin-src T M I :- Cond).
370- mk-src- aux [A|Args] T M I Cond (pi a \ C a) :-
379+ mixin-instance-type->mixin-src. aux [] T M I Cond (mixin-src T M I :- Cond).
380+ mixin-instance-type->mixin-src. aux [A|Args] T M I Cond (pi a \ C a) :-
371381 pi a \
372382 sigma Ta\
373383 coq.mk-app T [a] Ta,
374- mk-src- aux Args Ta M I [coq.unify-eq A a ok|Cond] (C a).
384+ mixin-instance-type->mixin-src. aux Args Ta M I [coq.unify-eq A a ok|Cond] (C a).
375385
376386
377- % transforms the has-mixin-instance clause arguments into a
378- % mixin-src clause with eventuals conditions regarding its parameters
379- pred mk -src
387+ % transforms the type of a mixin instance into a
388+ % mixin-src clause with eventual conditions regarding its parameters
389+ pred mixin-instance-type->mixin -src
380390 i:term, % type of the instance Ty
381391 i:mixinname, % name of mixin
382392 i:term, % instance body I of type Ty
383393 i:list prop, % Cond list
384394 o:prop.
385395
386- mk- src (app [_|Args] ) M I Cond C :-
387- std.last Args Last ,
388- safe-dest-app Last Hd ArgsLast ,
389- mk-src- aux ArgsLast Hd M I Cond C.
396+ mixin-instance-type->mixin- src (app _ as F ) M I Cond C :-
397+ factory? F (triple _ _ Subject) ,
398+ safe-dest-app Subject Hd Args ,
399+ mixin-instance-type->mixin-src. aux Args Hd M I Cond C.
390400
391- mk -src (prod N_ _ F) M I Cond (pi a \ C a) :-
401+ mixin-instance-type->mixin -src (prod N_ _ F) M I Cond (pi a \ C a) :-
392402 pi a\
393403 sigma Ia \
394404 coq.mk-app I [a] Ia,
395- mk-src (F a) M Ia Cond (C a).
396-
397-
398- % for a type T, create as many holes as there are foralls and returns that enriched type
399- pred enrich-type i:term, o:term .
400- enrich-type T ET :-
401- coq.typecheck T TH ok,
402- coq.count-prods TH N,
403- if (N = 0) (ET = T) (coq.mk-app T {coq.mk-n-holes N} ET).
405+ mixin-instance-type->mixin-src (F a) M Ia Cond (C a).
404406
405- % wrapper for mk-src so it can be called by a map on a list of clauses has-mixin-instance
406- pred mk-src-map i:prop, o:prop.
407- mk-src-map (has-mixin-instance _ M IHd) C :- std.do![
407+ pred has-mixin-instance->mixin-src i:prop, o:prop.
408+ has-mixin-instance->mixin-src (has-mixin-instance _ M IHd) C :- std.do![
408409 T = global IHd,
409410 coq.env.typeof IHd Ty,
410- mk-src Ty M T [] C,
411- ].
412-
413- pred cs-pattern->term i:cs-pattern, o:term.
414- cs-pattern->term (cs-gref GR) (global GR).
415- cs-pattern->term (cs-sort U) (sort U).
416-
417- pred term->cs-pattern i:term, o:cs-pattern.
418- term->cs-pattern (prod _ _ _) cs-prod.
419- term->cs-pattern (sort U) (cs-sort U).
420- term->cs-pattern T (cs-gref GR) :- term->gref T GR.
421- term->cs-pattern T _ :- coq.error T "HB database: is not a valid canonical key".
422-
423- pred cs-pattern->name i:cs-pattern, o:string.
424- cs-pattern->name cs-prod "prod".
425- cs-pattern->name (cs-sort _) "sort".
426- cs-pattern->name cs-default "default".
427- cs-pattern->name (cs-gref GR) Name :- gref->modname-label GR 1 "_" Name.
411+ mixin-instance-type->mixin-src Ty M T [] C,
412+ ].
428413
429414pred get-canonical-structures i:term, o:list structure.
430415get-canonical-structures TyTrm StructL :- std.do! [
0 commit comments