@@ -133,6 +133,38 @@ factories-provide FLwP MLwP :- std.do! [
133133 w-params.map UnsortedMLwP (p\t\ toposort-mixins) MLwP,
134134].
135135
136+ pred undup-grefs i:list gref, o:list gref.
137+ undup-grefs L UL :- std.do! [
138+ coq.gref.list->set L S,
139+ coq.gref.set.elements S UL,
140+ ].
141+
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,
150+ ].
151+
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,
158+
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,
162+
163+ if (std.mem L cs-prod) (R3 = [cs-prod]) (R3 = []),
164+
165+ std.flatten [R1, R2, R3] R,
166+ ].
167+
136168% Mixins can be topologically sorted according to their dependencies
137169pred toposort-mixins i:list (w-args mixinname), o:list (w-args mixinname).
138170toposort-mixins In Out :- std.do! [
@@ -192,6 +224,13 @@ findall-builders LFIL :-
192224 std.map {std.findall (builder-decl B_)} extract-builder LFILunsorted,
193225 std.bubblesort LFILunsorted leq-builder LFIL.
194226
227+ pred findall-has-mixin-instance o:list prop.
228+ findall-has-mixin-instance CL :-
229+ std.findall (has-mixin-instance _ _ _) CL.
230+
231+ pred has-mixin-instance_key i:prop, o:cs-pattern.
232+ has-mixin-instance_key (has-mixin-instance P _ _) P.
233+
195234pred findall-mixin-src i:term, o:list mixinname.
196235findall-mixin-src T ML :-
197236 std.map {std.findall (mixin-src T M_ V_)} mixin-src_mixin ML.
@@ -299,7 +338,7 @@ get-constructor (indt R) (indc K) :- !,
299338 if (coq.env.indt R _ _ _ _ [K] _) true (coq.error "Not a record" R).
300339get-constructor I _ :- coq.error "get-constructor: not an inductive" I.
301340
302- %% finding for locally defined structures
341+ % finding for locally defined structures
303342pred get-cs-structure i:cs-instance, o:structure.
304343get-cs-structure (cs-instance _ _ (const Inst)) Struct :- std.do! [
305344 coq.env.typeof (const Inst) InstTy,
@@ -313,17 +352,64 @@ get-cs-instance (cs-instance _ _ (const Inst)) Inst.
313352pred has-cs-instance i:gref, i:cs-instance.
314353has-cs-instance GTy (cs-instance _ (cs-gref GTy) _).
315354
316- pred term->cs-pattern i:term, o:cs-pattern.
317- term->cs-pattern (prod _ _ _) cs-prod.
318- term->cs-pattern (sort U) (cs-sort U).
319- term->cs-pattern T (cs-gref GR) :- term->gref T GR.
320- term->cs-pattern T _ :- coq.error T "HB database: is not a valid canonical key".
321-
322- pred cs-pattern->name i:cs-pattern, o:string.
323- cs-pattern->name cs-prod "prod".
324- cs-pattern->name (cs-sort _) "sort".
325- cs-pattern->name cs-default "default".
326- cs-pattern->name (cs-gref GR) Name :- gref->modname-label GR 1 "_" Name.
355+
356+ pred mixin-src->has-mixin-instance i:prop, o:prop.
357+ mixin-src->has-mixin-instance (mixin-src (global GR) M I) (has-mixin-instance (cs-gref GR) M IHd) :-
358+ term->gref I IHd.
359+
360+ mixin-src->has-mixin-instance (mixin-src (app [global GR|_] ) M I) (has-mixin-instance (cs-gref GR) M IHd) :-
361+ term->gref I IHd.
362+
363+ mixin-src->has-mixin-instance (mixin-src (prod _ _ _ ) M I) (has-mixin-instance cs-prod M IHd):-
364+ term->gref I IHd.
365+
366+ mixin-src->has-mixin-instance (mixin-src (sort U) M I) (has-mixin-instance (cs-sort U) M IHd):-
367+ term->gref I IHd.
368+
369+ % this auxiliary function iterates over the list of arguments of an application,
370+ % and create the necessary unify condition for each arguments
371+ % and at the end returns the mixin-src clause with all the conditions
372+ pred mixin-instance-type->mixin-src.aux
373+ i:list term, % list of arguments
374+ i:term, % head of the original application
375+ i:mixinname, % name of mixin
376+ i:term, % instance body
377+ i:list prop, % Cond list
378+ o:prop.
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) :-
381+ pi a \
382+ sigma Ta\
383+ coq.mk-app T [a] Ta,
384+ mixin-instance-type->mixin-src.aux Args Ta M I [coq.unify-eq A a ok|Cond] (C a).
385+
386+
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
390+ i:term, % type of the instance Ty
391+ i:mixinname, % name of mixin
392+ i:term, % instance body I of type Ty
393+ i:list prop, % Cond list
394+ o:prop.
395+
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.
400+
401+ mixin-instance-type->mixin-src (prod N_ _ F) M I Cond (pi a \ C a) :-
402+ pi a\
403+ sigma Ia \
404+ coq.mk-app I [a] Ia,
405+ mixin-instance-type->mixin-src (F a) M Ia Cond (C a).
406+
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![
409+ T = global IHd,
410+ coq.env.typeof IHd Ty,
411+ mixin-instance-type->mixin-src Ty M T [] C,
412+ ].
327413
328414pred get-canonical-structures i:term, o:list structure.
329415get-canonical-structures TyTrm StructL :- std.do! [
0 commit comments