-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrobExample.v
More file actions
293 lines (254 loc) · 7.87 KB
/
FrobExample.v
File metadata and controls
293 lines (254 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
From TensorRocq Require Export AbstractReasoning.
(** To support reasoning about abstract theories in the style of chyp,
we use a system that involves some boilerplate code. To demonstrate
how this is done, we provide the following direct translation of the
Frobenius algebra example from chyp
(frobenius.chyp)[https://github.com/akissinger/chyp/blob/master/examples/frobenius.chyp].
(The original comments are included inline, as appropriate.)
*)
(** To more easily state the assumed relations of our theory,
we define the following notation. Note that the [4] of [fin 4]
must be changed to the number of generators of the theory. *)
Notation "x === y" :=
(existT _ (existT _ (x%aprop, y%aprop)) :
{n & {m & (AProp (fin 4) n m * AProp (fin 4) n m)%type}})
(at level 70).
(** We define notations for the generators of the theory,
along with their dimensions, which allow us to state rules. *)
(** As a technical note, we define these notations to be [only parsing]
and later define [only printing] versions to ensure generators are
always printed with their names. *)
Notation m (* multiplication *) :=
(Agen (0%fin :> fin 4) 2 1) (only parsing).
Notation u (* unit *) :=
(Agen (1%fin :> fin 4) 0 1) (only parsing).
Notation n (* comultiplication *) :=
(Agen (2%fin :> fin 4) 1 2) (only parsing).
Notation v (* counit *) :=
(Agen (3%fin :> fin 4) 1 0) (only parsing).
(** We define a [Signature] for the theory.
[bool] can be replaced with any [Summable] type. *)
Definition Frob : Signature bool := {|
(** We define the type of generators... *)
gens := fin 4;
(** ... and the equivalence relation among them (in this case, equality) *)
gens_equiv := eq;
(** Then, we give the list of rules of the theory *)
rules := rules_of_rule_list [
(* (m, u) forms a monoid *)
(* rule assoc : *) m * id ;' m === id * m ;' m ;
(* rule unitL : *) u * id ;' m === id ;
(* rule unitR : *) id * u ;' m === id ;
(* (n, v) forms a comonoid *)
(* rule coassoc : *) n ;' n * id === n ;' id * n ;
(* rule counitL : *) n ;' v * id === id ;
(* rule counitR : *) n ;' id * v === id ;
(* there are many equivalent formulations of the Frobenius condition. Here's one: *)
(* rule frob : *) n * id ;' id * m === id * n ;' m * id
];
|}.
(** We define [only printing] notations for the constructors to
ensure they are printed. *)
Notation "'m'" := (@Agen _ 0%fin 2 1) (only printing).
Notation "'u'" := (@Agen _ 1%fin 0 1) (only printing).
Notation "'n'" := (@Agen _ 2%fin 1 2) (only printing).
Notation "'v'" := (@Agen _ 3%fin 1 0) (only printing).
(** We use the following notation for equality in the theory of [Frob],
as defined above. *)
Notation "x == y" :=
(x ≡ᵣ@{Frob} y)
(at level 70).
(** We restate the rules of the theory (note that here they use the
new [==] relation), and give them names. *)
(* (m, u) forms a monoid *)
Lemma assoc : m * id ;' m == id * m ;' m.
Proof. apply rules_hold. repeat constructor. Qed.
Lemma unitL : u * id ;' m == id.
Proof. apply rules_hold. repeat constructor. Qed.
Lemma unitR : id * u ;' m == id.
Proof. apply rules_hold. repeat constructor. Qed.
(* (n, v) forms a comonoid *)
Lemma coassoc : n ;' n * id == n ;' id * n.
Proof. apply rules_hold. repeat constructor. Qed.
Lemma counitL : n ;' v * id == id.
Proof. apply rules_hold. repeat constructor. Qed.
Lemma counitR : n ;' id * v == id.
Proof. apply rules_hold. repeat constructor. Qed.
Lemma frob : n * id ;' id * m == id * n ;' m * id.
Proof. apply rules_hold. repeat constructor. Qed.
(* The rule above is equivalent to the slightly more familiar pair of rules:
frobL : n * id ; id * m = m ; n
frobR : id * n ; m * id = m ; n
If m and n were both commutative, one of these would imply the other, and either
would imply "frob". However, for non-commutative Frobenius algebras, we need them
both. *)
(** Now, we can prove lemmas in the same way as chyp *)
Lemma frobL_chyp : n * id ;' id * m == m ;' n.
Proof.
(** The original proof is as follows:
n * id ; id * m
= u * n * id ; m * m by -unitL
= u * id * id ; n * id * id ; id * m * id ; id * m by -frob
= u * m ; n * id ; id * m by assoc
= u * m ; id * n ; m * id by frob
= m ; n by unitL
*)
(** We can replicate this style directly using transitivity statements,
our rewrite tactic [srw], and our SMC solver [smcat] *)
transitivity [[u * n * id ; m * m]]%aprop;
[srw unitL; smcat|].
transitivity [[u * id * id ; n * id * id ; id * m * id ; id * m]]%aprop;
[srw <- frob; smcat|].
transitivity [[u * m ; n * id ; id * m]]%aprop;
[srw assoc; smcat|].
transitivity [[u * m ; id * n ; m * id]]%aprop;
[srw frob; smcat|].
transitivity [[m ; n]]%aprop;
[srw unitL; smcat|].
smcat.
Qed.
(** However, most of these explicit transitivity steps can be elided,
as we can find the rewrites automatically.
This is the style we will prefer. *)
Lemma frobL : n * id ;' id * m == m ;' n.
Proof.
transitivity (u * n * id ;' m * m)%aprop;
[srw unitL; smcat|].
(* [[ u * n * id; m * m ]] == [[ m; n ]] *)
srw <- frob.
(* [[ u * id 2; Aswap 2 1; id * (n * id; id * m); (Aswap 2 1; m * id; sw) ]] == [[ m; n ]] *)
srw assoc.
(* [[ u * id 2; n * id 2; id * (id * m; m) ]] == [[ m; n ]] *)
srw frob.
(* [[ m * u; sw; (id * n; m * id) ]] == [[ m; n ]] *)
srw unitL.
(* [[ m; (n; sw); sw ]] == [[ m; n ]] *)
smcat.
Qed.
Lemma frobR : Aid 1 * n ;' m * Aid 1 == m ;' n.
Proof.
srw <- frob.
(* n * [[ id ]];' [[ id ]] * m == m;' n *)
srw frobL.
(* m;' n == m;' n *)
smcat.
Qed.
(** As with chyp, we can define derived morphisms.
This is the final element to translate chyp files;
we include the rest for reference. *)
Definition cup : AProp _ _ _ := u ;' n.
Definition cap : AProp _ _ _ := m ;' v.
Lemma cap_assoc : m * Aid 1 ;' cap == Aid 1 * m ;' cap.
Proof.
unfold cap.
(* m * [[ id ]];' (m;' v) == [[ id ]] * m;' (m;' v) *)
srw assoc.
(* [[ id ]] * m;' m;' v == [[ id ]] * m;' (m;' v) *)
smcat.
Qed.
Lemma cup_assoc : cup ;' n * Aid 1 == cup ;' Aid 1 * n.
Proof.
unfold cup.
(* u;' n;' n * [[ id ]] == u;' n;' [[ id ]] * n *)
srw coassoc.
(* u;' (n;' [[ id ]] * n) == u;' n;' [[ id ]] * n *)
smcat.
Qed.
Lemma yankL : cup * Aid 1 ;' Aid 1 * cap == Aid 1.
Proof.
unfold cup, cap.
(* (u;' n) * [[ id ]];' [[ id ]] * (m;' v) == [[ id ]] *)
srw frobL.
(* u * [[ id ]];' (m;' n);' (sw;' v * [[ id ]]) == [[ id ]] *)
srw unitL.
(* n;' (sw;' v * [[ id ]]) == [[ id ]] *)
srw counitR.
(* [[ id ]] == [[ id ]] *)
smcat.
Qed.
Lemma yankR : Aid 1 * cup ;' cap * Aid 1 == Aid 1.
Proof.
unfold cup, cap.
srw frobR.
srw unitR.
srw counitL.
smcat.
Qed.
Lemma m_cupL : cup * Aid 1 ;' Aid 1 * m == n.
Proof.
unfold cup.
srw frob.
srw unitL.
smcat.
Qed.
Lemma m_cupR : Aid 1 * cup ;' m * Aid 1 == n.
Proof.
unfold cup.
srw <- frob.
srw unitR.
smcat.
Qed.
Lemma n_capL : Aid 1 * n ;' cap * Aid 1 == m.
Proof.
unfold cap.
srw <- frob.
srw counitL.
smcat.
Qed.
Lemma n_capR : n * id ;' id * cap == m.
Proof.
unfold cap.
srw frob.
srw counitR.
smcat.
Qed.
Definition m2 : AProp _ _ _ := id * Aswap 1 1 * id ;' m * m.
Definition u2 : AProp _ _ _ := u * u.
Definition n2 : AProp _ _ _ := n * n ;' id * Aswap 1 1 * id.
Definition v2 : AProp _ _ _ := v * v.
Lemma assoc2 : m2 * Aid 2 ;' m2 ==
Aid 2 * m2 ;' m2.
Proof.
unfold m2.
srw assoc.
srw assoc.
smcat.
Qed.
Lemma unitL2 : u2 * Aid 2 ;' m2 == Aid 2.
Proof.
srw unitL.
srw unitL.
smcat.
Qed.
Lemma unitR2 : Aid 2 * u2 ;' m2 == Aid 2.
Proof.
srw unitR.
srw unitR.
smcat.
Qed.
Lemma coassoc2 : n2 ;' n2 * Aid 2 ==
n2 ;' Aid 2 * n2.
Proof.
unfold n2.
srw coassoc.
srw coassoc.
smcat.
Qed.
Lemma counitL2 : n2 ;' v2 * Aid 2 == Aid 2.
Proof.
srw counitL.
srw counitL.
smcat.
Qed.
Lemma counitR2 : n2 ;' Aid 2 * v2 == Aid 2.
Proof.
srw counitR.
srw counitR.
smcat.
Qed.
Lemma frob2 : n2 * Aid 2 ;' Aid 2 * m2 == Aid 2 * n2 ;' m2 * Aid 2.
Proof.
srw frob.
srw frob.
smcat.
Qed.