Skip to content

Commit 63a5283

Browse files
garesCohenCyril
authored andcommitted
automatic inference of a structure on structure paramters
1 parent 4487313 commit 63a5283

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

_CoqProject

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ tests/instance_params_no_type.v
5353
tests/test_CS_db_filtering.v
5454
tests/subtype.v
5555
tests/exports.v
56+
tests/infer.v
5657

5758
-R . HB

hb.elpi

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ type mterm list term -> term -> list mixinname -> term -> mterm.
660660
% - [id] using [unify-arg]
661661
kind phant-arg type.
662662
type real-arg name -> phant-arg.
663+
type infer-type name -> phant-arg.
663664
type implicit-arg phant-arg.
664665
type unify-arg phant-arg.
665666

@@ -934,6 +935,9 @@ mk-phant-abbrev.term K F [] K F.
934935
mk-phant-abbrev.term K F [real-arg N|AL] K'' (fun N _ AbbrevFx) :- !,
935936
pi x\ mk-phant-abbrev.term K {mk-app F [x]} AL K' (AbbrevFx x),
936937
K'' is K' + 1.
938+
mk-phant-abbrev.term K F [infer-type N|AL] K'' (fun N _ AbbrevFx) :- !,
939+
pi x\ mk-phant-abbrev.term K {mk-app F [{{ lib:hb.Phant lp:x }}]} AL K' (AbbrevFx x),
940+
K'' is K' + 1.
937941
mk-phant-abbrev.term K F [implicit-arg|AL] K' FAbbrev :- !,
938942
mk-phant-abbrev.term K {mk-app F [_]} AL K' FAbbrev.
939943
mk-phant-abbrev.term K F [unify-arg|AL] K' FAbbrev :- !,
@@ -1513,7 +1517,8 @@ synthesize-fields.body _Params T ML (record "axioms" {{ Type }} "Class" FS) :-
15131517
synthesize-fields T ML FS.
15141518

15151519
pred mk-record+sort-field i:name, i:term, i:(term -> record-decl), o:indt-decl.
1516-
mk-record+sort-field _ T F (record "type" {{ Type }} "Pack" (field _ "sort" T F)).
1520+
mk-record+sort-field _ T F (record RecordName {{ Type }} "Pack" (field _ "sort" T F)) :-
1521+
if (get-option "infer" _) (RecordName = "type_") (RecordName = "type").
15171522

15181523
pred mk-class-field i:classname, i:list term, i:term, i:list (w-args mixinname), o:record-decl.
15191524
mk-class-field ClassName Params T _ (field _ "class" (app [global ClassName|Args]) _\end-record) :-
@@ -1648,6 +1653,24 @@ pack-body ClassName PL T MLwA F :- std.do! [
16481653
under-mixins.then MLwA mk-fun (pack-body.mixins PL T BuildC PackS) F
16491654
].
16501655

1656+
pred declare-auto-infer-params-abbrev i:structure, i:list-w-params mixinname.
1657+
declare-auto-infer-params-abbrev GR MLwP :- get-option "infer" Map, !,
1658+
Map => mk-infer (global GR) MLwP PHARGS TSK,
1659+
std.assert-ok! (coq.elaborate-skeleton TSK _ T) "infer illtyped",
1660+
mk-phant-abbrev "type" (phant-term PHARGS T) _ _.
1661+
declare-auto-infer-params-abbrev _ _.
1662+
1663+
pred mk-infer i:term, i:list-w-params mixinname, o:list phant-arg, o:term.
1664+
mk-infer T (w-params.nil _ _ _) [] T.
1665+
mk-infer T (w-params.cons N Ty W) [implicit-arg, infer-type N|A] R1 :- coq.name->id N ID, (get-option ID "Type" ; get-option ID ""), !,
1666+
R1 = (fun N Ty n\ fun `ph` {{ lib:@hb.phant lp:n }} _\ R n),
1667+
@pi-decl N Ty n\
1668+
mk-infer {mk-app T [n]} (W n) A (R n).
1669+
mk-infer T (w-params.cons N Ty W) [real-arg N|A] (fun N Ty R) :- coq.name->id N ID, not (get-option ID _), !,
1670+
@pi-decl N Ty x\ mk-infer {mk-app T [x]} (W x) A (R x).
1671+
mk-infer _ (w-params.cons N _ _) _ _ :- coq.name->id N ID, get-option ID Infer,
1672+
coq.error "Automatic inference of paramter" N "from" Infer "not supported".
1673+
16511674
% HB.structure Definition S P1 P2 := { T of F1 P1 T & F2 P1 (P2*P2) T }
16521675
% cons p1\ cons p2\ nil t\ [triple f1 [p1] t,triple f2 [p1, {{p1 * p2}}] t]
16531676
pred main-declare-structure i:string, i:list-w-params gref, i:bool.
@@ -1682,6 +1705,8 @@ main-declare-structure Module GRFSwP ClosureCheck :- std.do! [
16821705
ClassRequires = factory-requires (ClassName) NilwP,
16831706
ClassAlias = (factory-alias->gref ClassName ClassName),
16841707
CurrentClass = (class ClassName Structure MLwP),
1708+
1709+
declare-auto-infer-params-abbrev Structure MLwP,
16851710

16861711
if-verbose (coq.say "HB: declaring clone abbreviation"),
16871712

@@ -2227,4 +2252,4 @@ declare-mixin-or-factory Sort1 Fields GRFSwP Module TheType D TheParams :- std.d
22272252
export Exports,
22282253

22292254
declare-factory-abbrev Module FactAbbrev,
2230-
].
2255+
].

structures.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Register Coq.Init.Datatypes.Some as hb.some.
1818
Register Coq.Init.Datatypes.pair as hb.pair.
1919
Register Coq.Init.Datatypes.prod as hb.prod.
2020
Register Coq.Init.Specif.sigT as hb.sigT.
21+
Register Coq.ssr.ssreflect.phant as hb.phant.
22+
Register Coq.ssr.ssreflect.Phant as hb.Phant.
2123
Definition indexed {T} (x : T) := x.
2224
Bind Scope type_scope with indexed.
2325
Register indexed as hb.indexed.

tests/infer.v

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
From HB Require Import structures.
2+
3+
HB.mixin Record foom T := {
4+
op : T -> T -> T
5+
}.
6+
7+
HB.structure Definition foo := { T of foom T }.
8+
9+
HB.instance Definition _ := foom.Build nat plus.
10+
11+
HB.mixin Record barm (P : foo.type) (T : indexed Type) := {
12+
law : P -> T
13+
}.
14+
15+
#[infer(P)]
16+
HB.structure Definition bar P := { T of barm P T }.
17+
18+
Fail Check bar.type_ nat.
19+
Print bar.phant_type.
20+
Print bar.type.
21+
Check bar.type nat.
22+
23+
Fail #[infer(P = "whatever")]
24+
HB.structure Definition bar1 P := { T of barm P T & foom T }.
25+
26+
#[infer(P = "Type")]
27+
HB.structure Definition bar1 P := { T of barm P T & foom T }.

0 commit comments

Comments
 (0)