Skip to content

Commit 22b5208

Browse files
authored
Merge pull request #277 from math-comp/GReTA
GReTA talk demo + Draft of category theory
2 parents 23aa065 + b05a2d6 commit 22b5208

File tree

9 files changed

+774
-11
lines changed

9 files changed

+774
-11
lines changed

HB/common/utils.elpi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,7 @@ w-params_1 X Y :- w-params.then X ignore ignore (p\ t\ triple_1) Y.
318318
pred purge-id i:term, o:term.
319319
purge-id T T1 :-
320320
(pi fresh t v\ copy {{lib:@hb.id lp:t lp:v}} fresh :- !) => copy T T1.
321+
322+
pred prod-last i:term, o:term.
323+
prod-last (prod N S X) Y :- !, @pi-decl N S x\ prod-last (X x) Y.
324+
prod-last X X :- !.

HB/structure.elpi

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ namespace structure {
55

66
% HB.structure Definition S P1 P2 := { T of F1 P1 T & F2 P1 (P2*P2) T }
77
% cons p1\ cons p2\ nil t\ [triple f1 [p1] t,triple f2 [p1, {{p1 * p2}}] t]
8-
pred declare i:string, i:term.
9-
declare Module B :- std.do! [
8+
pred declare i:string, i:term, i:universe.
9+
declare Module B Sort :- std.do! [
1010
purge-id B B1, std.assert-ok! (coq.elaborate-skeleton B1 _ B2) "illtyped structure definition",
1111
private.sigT->list-w-params B2 GRFSwP ClosureCheck,
1212

@@ -35,7 +35,7 @@ declare Module B :- std.do! [
3535

3636
log.coq.env.begin-module Module none,
3737

38-
private.declare-class+structure MLwP
38+
private.declare-class+structure MLwP Sort
3939
ClassName Structure SortProjection ClassProjection Factories StructKeyClause,
4040

4141
w-params.map MLwP (_\_\_\ mk-nil) NilwP,
@@ -493,17 +493,20 @@ pred synthesize-fields.body i:list term, i:term, i:list (w-args mixinname), o:in
493493
synthesize-fields.body _Params T ML (record "axioms_" {{ Type }} "Class" FS) :-
494494
synthesize-fields T ML FS.
495495

496-
pred mk-record+sort-field i:name, i:term, i:(term -> record-decl), o:indt-decl.
497-
mk-record+sort-field _ T F (record RecordName {{ Type }} "Pack" (field _ "sort" T F)) :-
498-
if (get-option "infer" _) (RecordName = "type_") (RecordName = "type").
496+
pred mk-record+sort-field i:universe, i:name, i:term, i:(term -> record-decl), o:indt-decl.
497+
mk-record+sort-field Sort _ T F (record RecordName (sort Sort) "Pack" (field _ "sort" T F)) :- !, std.do! [
498+
if (get-option "infer" _) (RecordName = "type_") (RecordName = "type")
499+
].
499500

500501
pred mk-class-field i:classname, i:list term, i:term, i:list (w-args mixinname), o:record-decl.
501502
mk-class-field ClassName Params T _ (field [canonical ff] "class" (app [global ClassName|Args]) _\end-record) :-
502503
std.append Params [T] Args.
503504

504505
% Builds the axioms record and the factories from this class to each mixin
505-
pred declare-class+structure i:mixins, o:factoryname, o:structure, o:term, o:term, o:list prop, o:prop.
506-
declare-class+structure MLwP (indt ClassInd) (indt StructureInd) SortProjection ClassProjection AllFactories (structure-key SortP ClassP (indt StructureInd)):- std.do! [
506+
pred declare-class+structure i:mixins, i:universe, o:factoryname, o:structure, o:term, o:term, o:list prop, o:prop.
507+
declare-class+structure MLwP Sort
508+
(indt ClassInd) (indt StructureInd) SortProjection ClassProjection AllFactories
509+
(structure-key SortP ClassP (indt StructureInd)):- std.do! [
507510

508511
if-verbose (coq.say {header} "declare axioms record"MLwP ),
509512

@@ -525,7 +528,7 @@ declare-class+structure MLwP (indt ClassInd) (indt StructureInd) SortProjection
525528

526529
if-verbose (coq.say {header} "declare type record"),
527530

528-
w-params.then MLwP (mk-parameter explicit) mk-record+sort-field
531+
w-params.then MLwP (mk-parameter explicit) (mk-record+sort-field Sort)
529532
(mk-class-field (indt ClassInd)) StructureDeclaration,
530533

531534
std.assert-ok! (coq.typecheck-indt-decl StructureDeclaration) "declare: illtyped",

_CoqProject

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
structures.v
22

3-
-Q . HB
3+
-Q . HB

examples/GReTA_talk/V1.v

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Require Import ZArith ssreflect ssrfun.
2+
From HB Require Import structures.
3+
4+
HB.mixin Record is_monoid (M : Type) := {
5+
zero : M;
6+
add : M -> M -> M;
7+
addrA : associative add; (* add is associative. *)
8+
add0r : forall x, add zero x = x; (* zero is neutral *)
9+
addr0 : forall x, add x zero = x; (* wrt add. *)
10+
}.
11+
12+
HB.structure Definition Monoid := { M of is_monoid M }.
13+
14+
HB.instance Definition Z_is_monoid : is_monoid Z :=
15+
is_monoid.Build Z 0%Z Z.add Z.add_assoc Z.add_0_l Z.add_0_r.

examples/GReTA_talk/V2.v

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Require Import ZArith ssreflect ssrfun.
2+
From HB Require Import structures.
3+
4+
HB.mixin Record is_semigroup (S : Type) := {
5+
add : S -> S -> S;
6+
addrA : associative add;
7+
}.
8+
HB.structure Definition SemiGroup :=
9+
{ S of is_semigroup S }.
10+
11+
HB.mixin Record semigroup_is_monoid M of is_semigroup M := {
12+
zero : M;
13+
add0r : forall x, add zero x = x;
14+
addr0 : forall x, add x zero = x;
15+
}.
16+
HB.structure Definition Monoid :=
17+
{ M of is_semigroup M & semigroup_is_monoid M }.
18+
19+
(* is_monoid does not exist anymore *)
20+
Fail Check is_monoid.
21+
22+
HB.instance Definition Z_is_monoid : is_monoid Z :=
23+
is_monoid.Build Z 0%Z Z.add Z.add_assoc Z.add_0_l Z.add_0_r.
24+
HB.mixin Record xxxx P A := { F : bool }.

examples/GReTA_talk/V3.v

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Require Import ZArith ssreflect ssrfun.
2+
From HB Require Import structures.
3+
4+
HB.mixin Record is_semigroup (S : Type) := {
5+
add : S -> S -> S;
6+
addrA : associative add;
7+
}.
8+
HB.structure Definition SemiGroup :=
9+
{ S & is_semigroup S }.
10+
11+
HB.mixin Record semigroup_is_monoid M of is_semigroup M := {
12+
zero : M;
13+
add0r : forall x, add zero x = x;
14+
addr0 : forall x, add x zero = x;
15+
}.
16+
17+
HB.factory Record is_monoid (M : Type) := {
18+
zero : M;
19+
add : M -> M -> M;
20+
addrA : associative add;
21+
add0r : forall x, add zero x = x;
22+
addr0 : forall x, add x zero = x;
23+
}.
24+
HB.builders Context (M : Type) (f : is_monoid M).
25+
HB.instance Definition _ : is_semigroup M :=
26+
is_semigroup.Build M add addrA.
27+
HB.instance Definition _ : semigroup_is_monoid M :=
28+
semigroup_is_monoid.Build M zero add0r addr0.
29+
HB.end.
30+
31+
HB.structure Definition Monoid :=
32+
{ M & is_monoid M }.
33+
34+
HB.instance Definition Z_is_monoid : is_monoid Z :=
35+
is_monoid.Build Z 0%Z Z.add
36+
Z.add_assoc Z.add_0_l Z.add_0_r.

examples/GReTA_talk/V4.v

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Require Import ZArith ssreflect ssrfun.
2+
From HB Require Import structures.
3+
4+
HB.mixin Record is_semigroup (S : Type) := {
5+
add : S -> S -> S;
6+
addrA : associative add;
7+
}.
8+
HB.structure Definition SemiGroup := { S & is_semigroup S }.
9+
10+
HB.mixin Record semigroup_is_monoid M of is_semigroup M := {
11+
zero : M;
12+
add0r : forall x, add zero x = x;
13+
addr0 : forall x, add x zero = x;
14+
}.
15+
16+
HB.factory Record is_monoid M := {
17+
zero : M;
18+
add : M -> M -> M;
19+
addrA : associative add;
20+
add0r : forall x, add zero x = x;
21+
addr0 : forall x, add x zero = x;
22+
}.
23+
HB.builders Context (M : Type) of is_monoid M.
24+
HB.instance Definition _ := is_semigroup.Build M add addrA.
25+
HB.instance Definition _ := semigroup_is_monoid.Build M zero add0r addr0.
26+
HB.end.
27+
28+
HB.structure Definition Monoid := { M & is_monoid M }.
29+
30+
HB.mixin Record monoid_is_group G of is_monoid G := {
31+
opp : G -> G;
32+
subrr : forall x, add x (opp x) = zero;
33+
addNr : forall x, add (opp x) x = zero;
34+
}.
35+
36+
HB.factory Record is_group G := {
37+
zero : G;
38+
add : G -> G -> G;
39+
opp : G -> G;
40+
addrA : associative add;
41+
add0r : forall x, add zero x = x;
42+
(* addr0 : forall x, add x zero = x; (* spurious *) *)
43+
subrr : forall x, add x (opp x) = zero;
44+
addNr : forall x, add (opp x) x = zero;
45+
}.
46+
HB.builders Context G of is_group G.
47+
Let addr0 : forall x, add x zero = x.
48+
Proof. by move=> x; rewrite -(addNr x) addrA subrr add0r. Qed.
49+
HB.instance Definition _ := is_monoid.Build G zero add addrA add0r addr0.
50+
HB.instance Definition _ := monoid_is_group.Build G opp subrr addNr.
51+
HB.end.
52+
53+
HB.instance Definition Z_is_group : is_group Z :=
54+
is_group.Build Z 0%Z Z.add Z.opp
55+
Z.add_assoc Z.add_0_l (* Z.add_0_r (*spurious *) *)
56+
Z.sub_diag Z.add_opp_diag_l.

0 commit comments

Comments
 (0)