Skip to content

Commit e37c1c7

Browse files
committed
Enter HB.from (from factories)
1 parent f92dd4c commit e37c1c7

File tree

4 files changed

+91
-16
lines changed

4 files changed

+91
-16
lines changed

HB/common/synthesis.elpi

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ infer-all-args-let Ps T GR X Diag :- std.do! [
7474
private.instantiate-all-args-let FT T X Diag,
7575
].
7676

77+
pred try-infer-all-args-let i:list term, i:term, i:gref, o:term.
78+
try-infer-all-args-let Ps T GR X :- std.do! [
79+
coq.env.typeof GR Ty,
80+
coq.mk-eta (-1) Ty (global GR) EtaF,
81+
coq.subst-fun {std.append Ps [T]} EtaF FT,
82+
private.try-instantiate-all-args-let FT T X,
83+
].
7784

7885
% [assert!-infer-mixin TheType M Out] infers one mixin M on TheType and
7986
% aborts with an error message if the mixin cannot be inferred
@@ -271,6 +278,15 @@ instantiate-all-args-let (fun N Tm F) T (let N Tm X R) Diag :- !, std.do! [
271278
].
272279
instantiate-all-args-let F _ F ok.
273280

281+
pred try-instantiate-all-args-let i:term, i:term, o:term.
282+
try-instantiate-all-args-let (fun N Tm F) T (let N Tm X R) :- !, std.do! [
283+
coq.safe-dest-app Tm (global TmGR) _,
284+
factory-alias->gref TmGR M,
285+
(mixin-for T M X ; true),
286+
(@pi-def N Tm X m\ try-instantiate-all-args-let (F m) T (R m)),
287+
].
288+
try-instantiate-all-args-let F _ F.
289+
274290
% [structure-instance->mixin-srcs TheType Structure] finds a CS instance for
275291
% Structure on TheType (if any) and builds mixin-src clauses for all the mixins
276292
% which can be candidates from that class instance. It finds instances which are

HB/pack.elpi

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ main Ty Args Instance :- std.do! [
1212
std.assert! (Args = [trm TSkel|FactoriesSkel]) "HB.pack: not enough arguments",
1313

1414
get-constructor Class KC,
15-
get-constructor Structure KS,
1615

1716
std.assert-ok! (d\
1817
(coq.elaborate-ty-skeleton TSkel _ T d, d = ok) ;
@@ -29,30 +28,67 @@ main Ty Args Instance :- std.do! [
2928
(AllFactories = Factories)
3029
(AllFactories = Factories, Tkey = T), % it's a factory, won't add anything
3130

32-
private.synth-instance Params KC KS T Tkey AllFactories Instance,
31+
private.synth-instance Params KC Tkey AllFactories ClassInstance,
32+
33+
get-constructor Structure KS,
34+
std.append Params [T, ClassInstance] InstanceArgs,
35+
Instance = app[global KS | InstanceArgs]
3336

3437
].
3538

39+
pred main-use-factories i:term, i:list argument, o:term.
40+
main-use-factories Ty FactoriesSkel ClassInstance :- std.do! [
41+
std.assert! (not(var Ty)) "HB.from: the class cannot be unknown",
42+
43+
factory? {unwind {whd Ty []}} (triple Class Params T),
44+
45+
std.assert! (class-def (class Class _ _)) "HB.from: not a class",
46+
47+
get-constructor Class KC,
48+
49+
private.elab-factories FactoriesSkel T Factories,
50+
51+
if (var T) (coq.error "HB.from: you must pass a type or at least one factory") true,
52+
53+
if2 (T = app[global (const SortProj)|ProjParams], structure-key SortProj ClassProj _)
54+
(AllFactories = [app[global (const ClassProj)|ProjParams] | Factories], Tkey = T) % already existing class on T
55+
(def T _ _ Tkey) % we unfold letins if we can, they may hide constants with CS instances
56+
(AllFactories = Factories)
57+
(AllFactories = Factories, Tkey = T), % it's a factory, won't add anything
58+
59+
private.try-synth-instance Params KC Tkey AllFactories ClassInstance,
60+
61+
].
62+
63+
3664
/* ------------------------------------------------------------------------- */
3765
/* ----------------------------- private code ------------------------------ */
3866
/* ------------------------------------------------------------------------- */
3967

4068
namespace private {
4169

42-
pred synth-instance i:list term, i:gref, i:gref, i:term, i:term, i:list term, o:term.
43-
synth-instance Params KC KS T Tkey [Factory|Factories] Instance :-
70+
pred synth-instance i:list term, i:gref, i:term, i:list term, o:term.
71+
synth-instance Params KC Tkey [Factory|Factories] ClassInstance :-
4472
synthesis.under-new-mixin-src-from-factory.do! Tkey Factory (_\
45-
synth-instance Params KC KS T Tkey Factories Instance).
46-
synth-instance Params KC KS T Tkey [] Instance :- coq.safe-dest-app Tkey (global _) _, !,
73+
synth-instance Params KC Tkey Factories ClassInstance).
74+
synth-instance Params KC Tkey [] ClassInstance :- coq.safe-dest-app Tkey (global _) _, !,
4775
synthesis.under-local-canonical-mixins-of.do! Tkey [
4876
std.assert-ok! (synthesis.infer-all-args-let Params Tkey KC ClassInstance) "HB.pack: cannot infer the instance",
49-
std.append Params [T, ClassInstance] InstanceArgs,
50-
Instance = app[global KS | InstanceArgs]
5177
].
52-
synth-instance Params KC KS T Tkey [] Instance :- std.do! [
78+
synth-instance Params KC Tkey [] ClassInstance :- std.do! [
5379
std.assert-ok! (synthesis.infer-all-args-let Params Tkey KC ClassInstance) "HB.pack: cannot infer the instance",
54-
std.append Params [T, ClassInstance] InstanceArgs,
55-
Instance = app[global KS | InstanceArgs]
80+
].
81+
82+
pred try-synth-instance i:list term, i:gref, i:term, i:list term, o:term.
83+
try-synth-instance Params KC Tkey [Factory|Factories] ClassInstance :-
84+
synthesis.under-new-mixin-src-from-factory.do! Tkey Factory (_\
85+
try-synth-instance Params KC Tkey Factories ClassInstance).
86+
try-synth-instance Params KC Tkey [] ClassInstance :- coq.safe-dest-app Tkey (global _) _, !,
87+
synthesis.under-local-canonical-mixins-of.do! Tkey [
88+
synthesis.try-infer-all-args-let Params Tkey KC ClassInstance,
89+
].
90+
try-synth-instance Params KC Tkey [] ClassInstance :- std.do! [
91+
synthesis.try-infer-all-args-let Params Tkey KC ClassInstance,
5692
].
5793

5894
pred elab-factories i:list argument, i:term, o:list term.

structures.v

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,29 @@ solve (goal _ _ Ty _ Args as G) GLS :- with-attributes (with-logging (std.do! [
539539
Elpi Typecheck.
540540
Elpi Export HB.pack.
541541

542+
Elpi Tactic HB.from.
543+
Elpi Accumulate Db hb.db.
544+
Elpi Accumulate File "HB/common/stdpp.elpi".
545+
Elpi Accumulate File "HB/common/database.elpi".
546+
#[skip="8.1[56].*"] Elpi Accumulate File "HB/common/compat_acc_clauses_all.elpi".
547+
#[only="8.1[56].*"] Elpi Accumulate File "HB/common/compat_acc_clauses_816.elpi".
548+
Elpi Accumulate File "HB/common/utils.elpi".
549+
Elpi Accumulate File "HB/common/log.elpi".
550+
Elpi Accumulate File "HB/common/synthesis.elpi".
551+
Elpi Accumulate File "HB/pack.elpi".
552+
Elpi Accumulate lp:{{
553+
554+
solve (goal _ _ Ty _ Args as G) GLS :- with-attributes (with-logging (std.do! [
555+
pack.main-use-factories Ty Args Instance,
556+
refine Instance G GLS,
557+
])).
558+
559+
}}.
560+
Elpi Typecheck.
561+
562+
Tactic Notation "HB.from" open_constr_list(L) :=
563+
elpi HB.from ltac_term_list:(L).
564+
542565
(* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% *)
543566
(* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% *)
544567
(* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% *)

theories/cat.v

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,16 +1056,15 @@ split=> [] sq.
10561056

10571057
pose red := @from _ (pb_terminal sq_ispb) blue_red_black_square.
10581058

1059-
1059+
admit.
10601060

10611061

10621062

10631063
have p2 : prepullback_isTerminal.axioms_ Q C B (Cospan (h \; f) g) (Span w (z \; v)) p1.
10641064
constructor. econstructor=> /=.
1065+
admit.
10651066

1066-
pose xx : Pullback.type (Cospan (h \; f) g) :=
1067-
HB.pack (Span w (z \; v)) p2 p1.
1068-
apply: Pullback.class xx.
1067+
by HB.from p1 p2.
10691068

10701069
Admitted.
10711070

@@ -1079,7 +1078,8 @@ Variables (Q : precat) (A B : Q) (c : cospan A B).
10791078
Variable (p : pullback c).
10801079
Check pb_terminal p : terminal _.
10811080

1082-
1081+
End test.
1082+
End test.
10831083

10841084

10851085

0 commit comments

Comments
 (0)