-
Notifications
You must be signed in to change notification settings - Fork 261
/
Monoidal.lean
726 lines (630 loc) · 31.6 KB
/
Monoidal.lean
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/-
Copyright (c) 2024 Yuma Mizuno. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yuma Mizuno
-/
import Mathlib.Tactic.CategoryTheory.Coherence
/-!
# Normalization of morphisms in monoidal categories
This file provides a tactic that normalizes morphisms in monoidal categories. This is used in the
string diagram widget given in `Mathlib.Tactic.StringDiagram`.
We say that the morphism `η` in a monoidal category is in normal form if
1. `η` is of the form `α₀ ≫ η₀ ≫ α₁ ≫ η₁ ≫ ... αₘ ≫ ηₘ ≫ αₘ₊₁` where each `αᵢ` is a
structural 2-morphism (consisting of associators and unitors),
2. each `ηᵢ` is a non-structural 2-morphism of the form `f₁ ◁ ... ◁ fₘ ◁ θ`, and
3. `θ` is of the form `ι ▷ g₁ ▷ ... ▷ gₗ`
Note that the structural morphisms `αᵢ` are not necessarily normalized, as the main purpose
is to get a list of the non-structural morphisms out.
Currently, the primary application of the normalization tactic in mind is drawing string diagrams,
which are graphical representations of morphisms in monoidal categories, in the infoview. When
drawing string diagrams, we often ignore associators and unitors (i.e., drawing morphisms in
strict monoidal categories). On the other hand, in Lean, it is considered difficult to formalize
the concept of strict monoidal categories due to the feature of dependent type theory. The
normalization tactic can remove associators and unitors from the expression, extracting the
necessary data for drawing string diagrams.
The current plan on drawing string diagrams (#10581) is to use
Penrose (https://github.com/penrose) via ProofWidget. However, it should be noted that the
normalization procedure in this file does not rely on specific settings, allowing for broader
application.
Future plans include the following. At least I (Yuma) would like to work on these in the future,
but it might not be immediate. If anyone is interested, I would be happy to discuss.
- Currently (#10581), the string diagrams only do drawing. It would be better they also generate
proofs. That is, by manipulating the string diagrams displayed in the infoview with a mouse to
generate proofs. In #10581, the string diagram widget only uses the morphisms generated by the
normalization tactic and does not use proof terms ensuring that the original morphism and the
normalized morphism are equal. Proof terms will be necessary for proof generation.
- There is also the possibility of using homotopy.io (https://github.com/homotopy-io), a graphical
proof assistant for category theory, from Lean. At this point, I have very few ideas regarding
this approach.
- The normalization tactic allows for an alternative implementation of the coherent tactic.
## Main definitions
- `Tactic.Monoidal.eval`: Given a Lean expression `e` that represents a morphism in a monoidal
category, this function returns a pair of `⟨e', pf⟩` where `e'` is the normalized expression of `e`
and `pf` is a proof that `e = e'`.
## Implementation notes
The function `Tactic.Monoidal.eval` fails to produce a proof term when the environment cannot
find the necessary `MonoidalCategory C` instance. This occurs when running the string diagram
widget, and the error makes it impossible for the string diagram widget to generate the diagram.
To work around the widget failure, if the proof generation fails when `eval` running, it returns a
meaningless term `mkConst ``True` in place of the proof term.
-/
namespace Mathlib.Tactic.Monoidal
open Lean Meta Elab
open CategoryTheory
open Mathlib.Tactic.Coherence
/-- The context for evaluating expressions. -/
structure Context where
/-- The expression for the underlying category. -/
C : Expr
/-- Populate a `context` object for evaluating `e`. -/
def mkContext (e : Expr) : MetaM Context := do
match (← inferType e).getAppFnArgs with
| (``Quiver.Hom, #[_, _, f, _]) =>
let C ← inferType f
return ⟨C⟩
| _ => throwError "not a morphism"
/-- The monad for the normalization of 2-morphisms. -/
abbrev MonoidalM := ReaderT Context MetaM
/-- Run a computation in the `M` monad. -/
abbrev MonoidalM.run {α : Type} (c : Context) (m : MonoidalM α) : MetaM α :=
ReaderT.run m c
/-- Expressions for atomic 1-morphisms. -/
structure Atom₁ : Type where
/-- Extract a Lean expression from an `Atom₁` expression. -/
e : Expr
/-- Expressions for 1-morphisms. -/
inductive Mor₁ : Type
/-- `id` is the expression for `𝟙_ C`. -/
| id : Mor₁
/-- `comp X Y` is the expression for `X ⊗ Y` -/
| comp : Mor₁ → Mor₁ → Mor₁
/-- Construct the expression for an atomic 1-morphism. -/
| of : Atom₁ → Mor₁
deriving Inhabited
/-- Converts a 1-morphism into a list of its components. -/
def Mor₁.toList : Mor₁ → List Atom₁
| .id => []
| .comp f g => f.toList ++ g.toList
| .of f => [f]
/-- Returns `𝟙_ C` if the expression `e` is of the form `𝟙_ C`. -/
def isTensorUnit? (e : Expr) : MetaM (Option Expr) := do
let C ← mkFreshExprMVar none
let instC ← mkFreshExprMVar none
let instMC ← mkFreshExprMVar none
let unit := mkAppN (← mkConstWithFreshMVarLevels
``MonoidalCategoryStruct.tensorUnit) #[C, instC, instMC]
if ← withDefault <| isDefEq e unit then
return ← instantiateMVars unit
else
return none
/-- Returns `(f, g)` if the expression `e` is of the form `f ⊗ g`. -/
def isTensorObj? (e : Expr) : MetaM (Option (Expr × Expr)) := do
let C ← mkFreshExprMVar none
let f ← mkFreshExprMVar C
let g ← mkFreshExprMVar C
let instC ← mkFreshExprMVar none
let instMC ← mkFreshExprMVar none
let fg := mkAppN (← mkConstWithFreshMVarLevels
``MonoidalCategoryStruct.tensorObj) #[C, instC, instMC, f, g]
if ← withDefault <| isDefEq e fg then
return (← instantiateMVars f, ← instantiateMVars g)
else
return none
/-- Construct a `Mor₁` expression from a Lean expression. -/
partial def toMor₁ (e : Expr) : MetaM Mor₁ := do
if let some _ ← isTensorUnit? e then
return Mor₁.id
else if let some (f, g) ← isTensorObj? e then
return (← toMor₁ f).comp (← toMor₁ g)
else
return Mor₁.of ⟨e⟩
/-- Expressions for atomic structural 2-morphisms. -/
inductive StructuralAtom : Type
/-- The expression for the associator `(α_ f g h).hom`. -/
| associator (f g h : Mor₁) : StructuralAtom
/-- The expression for the inverse of the associator `(α_ f g h).inv`. -/
| associatorInv (f g h : Mor₁) : StructuralAtom
/-- The expression for the left unitor `(λ_ f).hom`. -/
| leftUnitor (f : Mor₁) : StructuralAtom
/-- The expression for the inverse of the left unitor `(λ_ f).inv`. -/
| leftUnitorInv (f : Mor₁) : StructuralAtom
/-- The expression for the right unitor `(ρ_ f).hom`. -/
| rightUnitor (f : Mor₁) : StructuralAtom
/-- The expression for the inverse of the right unitor `(ρ_ f).inv`. -/
| rightUnitorInv (f : Mor₁) : StructuralAtom
deriving Inhabited
/-- Construct a `StructuralAtom` expression from a Lean expression. -/
def structuralAtom? (e : Expr) : MetaM (Option StructuralAtom) := do
match e.getAppFnArgs with
| (``Iso.hom, #[_, _, _, _, η]) =>
match (← whnfR η).getAppFnArgs with
| (``MonoidalCategoryStruct.associator, #[_, _, _, f, g, h]) =>
return some <| .associator (← toMor₁ f) (← toMor₁ g) (← toMor₁ h)
| (``MonoidalCategoryStruct.leftUnitor, #[_, _, _, f]) =>
return some <| .leftUnitor (← toMor₁ f)
| (``MonoidalCategoryStruct.rightUnitor, #[_, _, _, f]) =>
return some <| .rightUnitor (← toMor₁ f)
| _ => return none
| (``Iso.inv, #[_, _, _, _, η]) =>
match (← whnfR η).getAppFnArgs with
| (``MonoidalCategoryStruct.associator, #[_, _, _, f, g, h]) =>
return some <| .associatorInv (← toMor₁ f) (← toMor₁ g) (← toMor₁ h)
| (``MonoidalCategoryStruct.leftUnitor, #[_, _, _, f]) =>
return some <| .leftUnitorInv (← toMor₁ f)
| (``MonoidalCategoryStruct.rightUnitor, #[_, _, _, f]) =>
return some <| .rightUnitorInv (← toMor₁ f)
| _ => return none
| _ => return none
/-- Expressions for atomic non-structural 2-morphisms. -/
structure Atom where
/-- Extract a Lean expression from an `Atom` expression. -/
e : Expr
deriving Inhabited
/-- Expressions of the form `η ▷ f₁ ▷ ... ▷ fₙ`. -/
inductive WhiskerRightExpr : Type
/-- Construct the expression for an atomic 2-morphism. -/
| of (η : Atom) : WhiskerRightExpr
/-- Construct the expression for `η ▷ f`. -/
| whisker (η : WhiskerRightExpr) (f : Atom₁) : WhiskerRightExpr
deriving Inhabited
/-- Expressions of the form `f₁ ◁ ... ◁ fₙ ◁ η`. -/
inductive WhiskerLeftExpr : Type
/-- Construct the expression for a right-whiskered 2-morphism. -/
| of (η : WhiskerRightExpr) : WhiskerLeftExpr
/-- Construct the expression for `f ◁ η`. -/
| whisker (f : Atom₁) (η : WhiskerLeftExpr) : WhiskerLeftExpr
deriving Inhabited
/-- Expressions for structural 2-morphisms. -/
inductive Structural : Type
/-- Expressions for atomic structural 2-morphisms. -/
| atom (η : StructuralAtom) : Structural
/-- Expressions for the identity `𝟙 f`. -/
| id (f : Mor₁) : Structural
/-- Expressions for the composition `η ≫ θ`. -/
| comp (α β : Structural) : Structural
/-- Expressions for the left whiskering `f ◁ η`. -/
| whiskerLeft (f : Mor₁) (η : Structural) : Structural
/-- Expressions for the right whiskering `η ▷ f`. -/
| whiskerRight (η : Structural) (f : Mor₁) : Structural
/-- Expressions for `α` in the monoidal composition `η ⊗≫ θ := η ≫ α ≫ θ`. -/
| monoidalCoherence (f g : Mor₁) (e : Expr) : Structural
deriving Inhabited
/-- Normalized expressions for 2-morphisms. -/
inductive NormalExpr : Type
/-- Construct the expression for a structural 2-morphism. -/
| nil (α : Structural) : NormalExpr
/-- Construct the normalized expression of 2-morphisms recursively. -/
| cons (head_structural : Structural) (head : WhiskerLeftExpr) (tail : NormalExpr) : NormalExpr
deriving Inhabited
/-- The domain of a morphism. -/
def src (η : Expr) : MetaM Mor₁ := do
match (← inferType η).getAppFnArgs with
| (``Quiver.Hom, #[_, _, f, _]) => toMor₁ f
| _ => throwError "{η} is not a morphism"
/-- The codomain of a morphism. -/
def tgt (η : Expr) : MetaM Mor₁ := do
match (← inferType η).getAppFnArgs with
| (``Quiver.Hom, #[_, _, _, g]) => toMor₁ g
| _ => throwError "{η} is not a morphism"
/-- The domain of a 2-morphism. -/
def Atom.src (η : Atom) : MetaM Mor₁ := do Monoidal.src η.e
/-- The codomain of a 2-morphism. -/
def Atom.tgt (η : Atom) : MetaM Mor₁ := do Monoidal.tgt η.e
/-- The domain of a 2-morphism. -/
def WhiskerRightExpr.src : WhiskerRightExpr → MetaM Mor₁
| WhiskerRightExpr.of η => η.src
| WhiskerRightExpr.whisker η f => return (← WhiskerRightExpr.src η).comp (Mor₁.of f)
/-- The codomain of a 2-morphism. -/
def WhiskerRightExpr.tgt : WhiskerRightExpr → MetaM Mor₁
| WhiskerRightExpr.of η => η.tgt
| WhiskerRightExpr.whisker η f => return (← WhiskerRightExpr.tgt η).comp (Mor₁.of f)
/-- The domain of a 2-morphism. -/
def WhiskerLeftExpr.src : WhiskerLeftExpr → MetaM Mor₁
| WhiskerLeftExpr.of η => WhiskerRightExpr.src η
| WhiskerLeftExpr.whisker f η => return (Mor₁.of f).comp (← WhiskerLeftExpr.src η)
/-- The codomain of a 2-morphism. -/
def WhiskerLeftExpr.tgt : WhiskerLeftExpr → MetaM Mor₁
| WhiskerLeftExpr.of η => WhiskerRightExpr.tgt η
| WhiskerLeftExpr.whisker f η => return (Mor₁.of f).comp (← WhiskerLeftExpr.tgt η)
/-- The domain of a 2-morphism. -/
def StructuralAtom.src : StructuralAtom → Mor₁
| .associator f g h => (f.comp g).comp h
| .associatorInv f g h => f.comp (g.comp h)
| .leftUnitor f => Mor₁.id.comp f
| .leftUnitorInv f => f
| .rightUnitor f => f.comp Mor₁.id
| .rightUnitorInv f => f
/-- The codomain of a 2-morphism. -/
def StructuralAtom.tgt : StructuralAtom → Mor₁
| .associator f g h => f.comp (g.comp h)
| .associatorInv f g h => (f.comp g).comp h
| .leftUnitor f => f
| .leftUnitorInv f => Mor₁.id.comp f
| .rightUnitor f => f
| .rightUnitorInv f => f.comp Mor₁.id
/-- The domain of a 2-morphism. -/
def Structural.src : Structural → Mor₁
| .atom η => η.src
| .id f => f
| .comp α _ => α.src
| .whiskerLeft f η => f.comp η.src
| .whiskerRight η f => η.src.comp f
| .monoidalCoherence f _ _ => f
/-- The codomain of a 2-morphism. -/
def Structural.tgt : Structural → Mor₁
| .atom η => η.tgt
| .id f => f
| .comp _ β => β.tgt
| .whiskerLeft f η => f.comp η.tgt
| .whiskerRight η f => η.tgt.comp f
| .monoidalCoherence _ g _ => g
/-- The domain of a 2-morphism. -/
def NormalExpr.src : NormalExpr → Mor₁
| NormalExpr.nil η => η.src
| NormalExpr.cons α _ _ => α.src
/-- The codomain of a 2-morphism. -/
def NormalExpr.tgt : NormalExpr → Mor₁
| NormalExpr.nil η => η.tgt
| NormalExpr.cons _ _ ηs => ηs.tgt
/-- The associator as a term of `normalExpr`. -/
def NormalExpr.associator (f g h : Mor₁) : NormalExpr :=
.nil <| .atom <| .associator f g h
/-- The inverse of the associator as a term of `normalExpr`. -/
def NormalExpr.associatorInv (f g h : Mor₁) : NormalExpr :=
.nil <| .atom <| .associatorInv f g h
/-- The left unitor as a term of `normalExpr`. -/
def NormalExpr.leftUnitor (f : Mor₁) : NormalExpr :=
.nil <| .atom <| .leftUnitor f
/-- The inverse of the left unitor as a term of `normalExpr`. -/
def NormalExpr.leftUnitorInv (f : Mor₁) : NormalExpr :=
.nil <| .atom <| .leftUnitorInv f
/-- The right unitor as a term of `normalExpr`. -/
def NormalExpr.rightUnitor (f : Mor₁) : NormalExpr :=
.nil <| .atom <| .rightUnitor f
/-- The inverse of the right unitor as a term of `normalExpr`. -/
def NormalExpr.rightUnitorInv (f : Mor₁) : NormalExpr :=
.nil <| .atom <| .rightUnitorInv f
/-- Return `η` for `η ▷ g₁ ▷ ... ▷ gₙ`. -/
def WhiskerRightExpr.atom : WhiskerRightExpr → Atom
| WhiskerRightExpr.of η => η
| WhiskerRightExpr.whisker η _ => η.atom
/-- Return `η` for `f₁ ◁ ... ◁ fₙ ◁ η ▷ g₁ ▷ ... ▷ gₙ`. -/
def WhiskerLeftExpr.atom : WhiskerLeftExpr → Atom
| WhiskerLeftExpr.of η => η.atom
| WhiskerLeftExpr.whisker _ η => η.atom
/-- Construct a `Structural` expression from a Lean expression for a structural 2-morphism. -/
partial def structural? (e : Expr) : MetaM Structural := do
match (← whnfR e).getAppFnArgs with
| (``CategoryStruct.comp, #[_, _, _, α, β]) =>
return .comp (← structural? α) (← structural? β)
| (``CategoryStruct.id, #[_, f]) => return .id (← toMor₁ f)
| (``MonoidalCategoryStruct.whiskerLeft, #[f, η]) =>
return .whiskerLeft (← toMor₁ f) (← structural? η)
| (``MonoidalCategoryStruct.whiskerRight, #[η, f]) =>
return .whiskerRight (← structural? η) (← toMor₁ f)
| (``MonoidalCoherence.hom, #[_, _, f, g, inst]) =>
return .monoidalCoherence (← toMor₁ f) (← toMor₁ g) inst
| _ => match ← structuralAtom? e with
| some η => return .atom η
| none => throwError "not a structural 2-morphism"
/-- Construct a `NormalExpr` expression from a `WhiskerLeftExpr` expression. -/
def NormalExpr.of (η : WhiskerLeftExpr) : MetaM NormalExpr := do
return .cons (.id (← η.src)) η (.nil (.id (← η.tgt)))
/-- Construct a `NormalExpr` expression from a Lean expression for an atomic 2-morphism. -/
def NormalExpr.ofExpr (η : Expr) : MetaM NormalExpr :=
NormalExpr.of <| .of <| .of ⟨η⟩
/-- If `e` is an expression of the form `η ⊗≫ θ := η ≫ α ≫ θ` in the monoidal category `C`,
return the expression for `α` .-/
def structuralOfMonoidalComp (C e : Expr) : MetaM Structural := do
let v ← mkFreshLevelMVar
let u ← mkFreshLevelMVar
_ ← isDefEq (.sort (.succ v)) (← inferType (← inferType e))
_ ← isDefEq (.sort (.succ u)) (← inferType C)
let W ← mkFreshExprMVar none
let X ← mkFreshExprMVar none
let Y ← mkFreshExprMVar none
let Z ← mkFreshExprMVar none
let f ← mkFreshExprMVar none
let g ← mkFreshExprMVar none
let α₀ ← mkFreshExprMVar none
let instC ← mkFreshExprMVar none
let αg := mkAppN (.const ``CategoryStruct.comp [v, u]) #[C, instC, X, Y, Z, α₀, g]
let fαg := mkAppN (.const ``CategoryStruct.comp [v, u]) #[C, instC, W, X, Z, f, αg]
_ ← isDefEq e fαg
structural? α₀
section
open scoped MonoidalCategory
universe v u
variable {C : Type u} [Category.{v} C] [MonoidalCategory C]
variable {f f' g g' h i j : C}
theorem evalComp_nil_cons {f g h i j : C} (α : f ⟶ g) (β : g ⟶ h) (η : h ⟶ i) (ηs : i ⟶ j) :
α ≫ (β ≫ η ≫ ηs) = (α ≫ β) ≫ η ≫ ηs := by
simp
@[nolint synTaut]
theorem evalComp_nil_nil {f g h : C} (α : f ⟶ g) (β : g ⟶ h) :
α ≫ β = α ≫ β := by
simp
theorem evalComp_cons {f g h i j : C} (α : f ⟶ g) (η : g ⟶ h) {ηs : h ⟶ i} {θ : i ⟶ j} {ι : h ⟶ j}
(pf_ι : ηs ≫ θ = ι) :
(α ≫ η ≫ ηs) ≫ θ = α ≫ η ≫ ι := by
simp [pf_ι]
@[nolint synTaut]
theorem evalWhiskerLeft_nil (f : C) (α : g ⟶ h) :
f ◁ α = f ◁ α := by
simp
theorem evalWhiskerLeft_of_cons
(α : g ⟶ h) (η : h ⟶ i) {ηs : i ⟶ j} {θ : f ⊗ i ⟶ f ⊗ j} (pf_θ : f ◁ ηs = θ) :
f ◁ (α ≫ η ≫ ηs) = f ◁ α ≫ f ◁ η ≫ θ := by
simp [pf_θ]
theorem evalWhiskerLeft_comp {η : h ⟶ i} {θ : g ⊗ h ⟶ g ⊗ i} {ι : f ⊗ g ⊗ h ⟶ f ⊗ g ⊗ i}
{ι' : f ⊗ g ⊗ h ⟶ (f ⊗ g) ⊗ i} {ι'' : (f ⊗ g) ⊗ h ⟶ (f ⊗ g) ⊗ i}
(pf_θ : g ◁ η = θ) (pf_ι : f ◁ θ = ι)
(pf_ι' : ι ≫ (α_ _ _ _).inv = ι') (pf_ι'' : (α_ _ _ _).hom ≫ ι' = ι'') :
(f ⊗ g) ◁ η = ι'' := by
simp [pf_θ, pf_ι, pf_ι', pf_ι'']
theorem evalWhiskerLeft_id {f g : C} {η : f ⟶ g}
{η' : f ⟶ 𝟙_ C ⊗ g} {η'' : 𝟙_ C ⊗ f ⟶ 𝟙_ C ⊗ g}
(pf_η' : η ≫ (λ_ _).inv = η') (pf_η'' : (λ_ _).hom ≫ η' = η'') :
𝟙_ C ◁ η = η'' := by
simp [pf_η', pf_η'']
theorem eval_comp
{η η' : f ⟶ g} {θ θ' : g ⟶ h} {ι : f ⟶ h}
(pf_η : η = η') (pf_θ : θ = θ') (pf_ηθ : η' ≫ θ' = ι) :
η ≫ θ = ι := by
simp [pf_η, pf_θ, pf_ηθ]
theorem eval_whiskerLeft
{η η' : g ⟶ h} {θ : f ⊗ g ⟶ f ⊗ h}
(pf_η : η = η') (pf_θ : f ◁ η' = θ) :
f ◁ η = θ := by
simp [pf_η, pf_θ]
theorem eval_whiskerRight
{η η' : f ⟶ g} {θ : f ⊗ h ⟶ g ⊗ h}
(pf_η : η = η') (pf_θ : η' ▷ h = θ) :
η ▷ h = θ := by
simp [pf_η, pf_θ]
theorem eval_of (η : f ⟶ g) :
η = 𝟙 _ ≫ η ≫ 𝟙 _ := by
simp
@[nolint synTaut]
theorem evalWhiskerRight_nil (α : f ⟶ g) (h : C) :
α ▷ h = α ▷ h := by
simp
theorem evalWhiskerRight_cons_of_of
(α : f ⟶ g) (η : g ⟶ h) {ηs : h ⟶ i} {θ : h ⊗ j ⟶ i ⊗ j}
(pf_θ : ηs ▷ j = θ) :
(α ≫ η ≫ ηs) ▷ j = α ▷ j ≫ η ▷ j ≫ θ := by
simp [pf_θ]
theorem evalWhiskerRight_cons_whisker
{α : g ⟶ f ⊗ h} {η : h ⟶ i} {ηs : f ⊗ i ⟶ j} {k : C}
{η₁ : h ⊗ k ⟶ i ⊗ k} {η₂ : f ⊗ (h ⊗ k) ⟶ f ⊗ (i ⊗ k)} {ηs₁ : (f ⊗ i) ⊗ k ⟶ j ⊗ k}
{ηs₂ : f ⊗ (i ⊗ k) ⟶ j ⊗ k} {η₃ : f ⊗ (h ⊗ k) ⟶ j ⊗ k} {η₄ : (f ⊗ h) ⊗ k ⟶ j ⊗ k}
{η₅ : g ⊗ k ⟶ j ⊗ k}
(pf_η₁ : (𝟙 _ ≫ η ≫ 𝟙 _ ) ▷ k = η₁) (pf_η₂ : f ◁ η₁ = η₂)
(pf_ηs₁ : ηs ▷ k = ηs₁) (pf_ηs₂ : (α_ _ _ _).inv ≫ ηs₁ = ηs₂)
(pf_η₃ : η₂ ≫ ηs₂ = η₃) (pf_η₄ : (α_ _ _ _).hom ≫ η₃ = η₄) (pf_η₅ : α ▷ k ≫ η₄ = η₅) :
(α ≫ (f ◁ η) ≫ ηs) ▷ k = η₅ := by
simp at pf_η₁
simp [pf_η₁, pf_η₂, pf_ηs₁, pf_ηs₂, pf_η₃, pf_η₄, pf_η₅]
theorem evalWhiskerRight_comp
{η : f ⟶ f'} {η₁ : f ⊗ g ⟶ f' ⊗ g} {η₂ : (f ⊗ g) ⊗ h ⟶ (f' ⊗ g) ⊗ h}
{η₃ : (f ⊗ g) ⊗ h ⟶ f' ⊗ (g ⊗ h)} {η₄ : f ⊗ (g ⊗ h) ⟶ f' ⊗ (g ⊗ h)}
(pf_η₁ : η ▷ g = η₁) (pf_η₂ : η₁ ▷ h = η₂)
(pf_η₃ : η₂ ≫ (α_ _ _ _).hom = η₃) (pf_η₄ : (α_ _ _ _).inv ≫ η₃ = η₄) :
η ▷ (g ⊗ h) = η₄ := by
simp [pf_η₁, pf_η₂, pf_η₃, pf_η₄]
theorem evalWhiskerRight_id
{η : f ⟶ g} {η₁ : f ⟶ g ⊗ 𝟙_ C} {η₂ : f ⊗ 𝟙_ C ⟶ g ⊗ 𝟙_ C}
(pf_η₁ : η ≫ (ρ_ _).inv = η₁) (pf_η₂ : (ρ_ _).hom ≫ η₁ = η₂) :
η ▷ 𝟙_ C = η₂ := by
simp [pf_η₁, pf_η₂]
theorem eval_monoidalComp
{η η' : f ⟶ g} {α : g ⟶ h} {θ θ' : h ⟶ i} {αθ : g ⟶ i} {ηαθ : f ⟶ i}
(pf_η : η = η') (pf_θ : θ = θ') (pf_αθ : α ≫ θ' = αθ) (pf_ηαθ : η' ≫ αθ = ηαθ) :
η ≫ α ≫ θ = ηαθ := by
simp [pf_η, pf_θ, pf_αθ, pf_ηαθ]
end
/-- Extract a Lean expression from a `Mor₁` expression. -/
def Mor₁.e : Mor₁ → MonoidalM Expr
| .id => do
let ctx ← read
mkAppOptM ``MonoidalCategoryStruct.tensorUnit #[ctx.C, none, none]
| .comp f g => do
mkAppM ``MonoidalCategoryStruct.tensorObj #[← Mor₁.e f, ← Mor₁.e g]
| .of f => return f.e
/-- Extract a Lean expression from a `StructuralAtom` expression. -/
def StructuralAtom.e : StructuralAtom → MonoidalM Expr
| .associator f g h => do
mkAppM ``Iso.hom #[← mkAppM ``MonoidalCategoryStruct.associator #[← f.e, ← g.e, ← h.e]]
| .associatorInv f g h => do
mkAppM ``Iso.inv #[← mkAppM ``MonoidalCategoryStruct.associator #[← f.e, ← g.e, ← h.e]]
| .leftUnitor f => do
mkAppM ``Iso.hom #[← mkAppM ``MonoidalCategoryStruct.leftUnitor #[← f.e]]
| .leftUnitorInv f => do
mkAppM ``Iso.inv #[← mkAppM ``MonoidalCategoryStruct.leftUnitor #[← f.e]]
| .rightUnitor f => do
mkAppM ``Iso.hom #[← mkAppM ``MonoidalCategoryStruct.rightUnitor #[← f.e]]
| .rightUnitorInv f => do
mkAppM ``Iso.inv #[← mkAppM ``MonoidalCategoryStruct.rightUnitor #[← f.e]]
/-- Extract a Lean expression from a `Structural` expression. -/
partial def Structural.e : Structural → MonoidalM Expr
| .atom η => η.e
| .id f => do mkAppM ``CategoryStruct.id #[← f.e]
| .comp α β => do match α, β with
| _, _ => mkAppM ``CategoryStruct.comp #[← α.e, ← β.e]
| .whiskerLeft f η => do mkAppM ``MonoidalCategoryStruct.whiskerLeft #[← f.e, ← η.e]
| .whiskerRight η f => do mkAppM ``MonoidalCategoryStruct.whiskerRight #[← η.e, ← f.e]
| .monoidalCoherence _ _ e => do
mkAppOptM ``MonoidalCoherence.hom #[none, none, none, none, e]
/-- Extract a Lean expression from a `WhiskerRightExpr` expression. -/
def WhiskerRightExpr.e : WhiskerRightExpr → MonoidalM Expr
| WhiskerRightExpr.of η => return η.e
| WhiskerRightExpr.whisker η f => do
mkAppM ``MonoidalCategoryStruct.whiskerRight #[← η.e, f.e]
/-- Extract a Lean expression from a `WhiskerLeftExpr` expression. -/
def WhiskerLeftExpr.e : WhiskerLeftExpr → MonoidalM Expr
| WhiskerLeftExpr.of η => η.e
| WhiskerLeftExpr.whisker f η => do
mkAppM ``MonoidalCategoryStruct.whiskerLeft #[f.e, ← η.e]
/-- Extract a Lean expression from a `NormalExpr` expression. -/
def NormalExpr.e : NormalExpr → MonoidalM Expr
| NormalExpr.nil α => α.e
| NormalExpr.cons α η θ => do
mkAppM ``CategoryStruct.comp #[← α.e, ← mkAppM ``CategoryStruct.comp #[← η.e, ← θ.e]]
/-- The result of evaluating an expression into normal form. -/
structure Result where
/-- The normalized expression of the 2-morphism. -/
expr : NormalExpr
/-- The proof that the normalized expression is equal to the original expression. -/
proof : Expr
/-- Evaluate the expression `η ≫ θ` into a normalized form. -/
partial def evalComp : NormalExpr → NormalExpr → MonoidalM Result
| .nil α, .cons β η ηs => do
let η' := .cons (α.comp β) η ηs
try return ⟨η', ← mkAppM ``evalComp_nil_cons #[← α.e, ← β.e, ← η.e, ← ηs.e]⟩
catch _ => return ⟨η', mkConst ``True⟩
| .nil α, .nil α' => do
try return ⟨.nil (α.comp α'), ← mkAppM ``evalComp_nil_nil #[← α.e, ← α'.e]⟩
catch _ => return ⟨.nil (α.comp α'), mkConst ``True⟩
| .cons α η ηs, θ => do
let ⟨ι, pf_ι⟩ ← evalComp ηs θ
let ι' := .cons α η ι
try return ⟨ι', ← mkAppM ``evalComp_cons #[← α.e, ← η.e, pf_ι]⟩
catch _ => return ⟨ι', mkConst ``True⟩
/-- Evaluate the expression `f ◁ η` into a normalized form. -/
partial def evalWhiskerLeftExpr : Mor₁ → NormalExpr → MonoidalM Result
| f, .nil α => do
try return ⟨.nil (.whiskerLeft f α), ← mkAppM ``evalWhiskerLeft_nil #[← f.e, ← α.e]⟩
catch _ => return ⟨.nil (.whiskerLeft f α), mkConst ``True⟩
| .of f, .cons α η ηs => do
let η' := WhiskerLeftExpr.whisker f η
let ⟨θ, pf_θ⟩ ← evalWhiskerLeftExpr (.of f) ηs
let η'' := .cons (.whiskerLeft (.of f) α) η' θ
try return ⟨η'', ← mkAppM ``evalWhiskerLeft_of_cons #[← α.e, ← η.e, pf_θ]⟩
catch _ => return ⟨η'', mkConst ``True⟩
| .comp f g, η => do
let ⟨θ, pf_θ⟩ ← evalWhiskerLeftExpr g η
let ⟨ι, pf_ι⟩ ← evalWhiskerLeftExpr f θ
let h := η.src
let h' := η.tgt
let ⟨ι', pf_ι'⟩ ← evalComp ι (NormalExpr.associatorInv f g h')
let ⟨ι'', pf_ι''⟩ ← evalComp (NormalExpr.associator f g h) ι'
try return ⟨ι'', ← mkAppM ``evalWhiskerLeft_comp #[pf_θ, pf_ι, pf_ι', pf_ι'']⟩
catch _ => return ⟨ι'', mkConst ``True⟩
| .id, η => do
let f := η.src
let g := η.tgt
let ⟨η', pf_η'⟩ ← evalComp η (NormalExpr.leftUnitorInv g)
let ⟨η'', pf_η''⟩ ← evalComp (NormalExpr.leftUnitor f) η'
try return ⟨η'', ← mkAppM ``evalWhiskerLeft_id #[pf_η', pf_η'']⟩
catch _ => return ⟨η'', mkConst ``True⟩
/-- Evaluate the expression `η ▷ f` into a normalized form. -/
partial def evalWhiskerRightExpr : NormalExpr → Mor₁ → MonoidalM Result
| .nil α, h => do
try return ⟨.nil (.whiskerRight α h), ← mkAppM ``evalWhiskerRight_nil #[← α.e, ← h.e]⟩
catch _ => return ⟨.nil (.whiskerRight α h), mkConst ``True⟩
| .cons α (.of η) ηs, .of f => do
let ⟨θ, pf_θ⟩ ← evalWhiskerRightExpr ηs (.of f)
let η' := .cons (.whiskerRight α (.of f)) (.of (.whisker η f)) θ
try return ⟨η', ← mkAppM ``evalWhiskerRight_cons_of_of #[← α.e, ← η.e, pf_θ]⟩
catch _ => return ⟨η', mkConst ``True⟩
| .cons α (.whisker f η) ηs, h => do
let g ← η.src
let g' ← η.tgt
let ⟨η₁, pf_η₁⟩ ← evalWhiskerRightExpr (.cons (.id g) η (.nil (.id g'))) h
let ⟨η₂, pf_η₂⟩ ← evalWhiskerLeftExpr (.of f) η₁
let ⟨ηs₁, pf_ηs₁⟩ ← evalWhiskerRightExpr ηs h
let α' := .whiskerRight α h
let ⟨ηs₂, pf_ηs₂⟩ ← evalComp (.associatorInv (.of f) g' h) ηs₁
let ⟨η₃, pf_η₃⟩ ← evalComp η₂ ηs₂
let ⟨η₄, pf_η₄⟩ ← evalComp (.associator (.of f) g h) η₃
let ⟨η₅, pf_η₅⟩ ← evalComp (.nil α') η₄
try return ⟨η₅,
← mkAppM ``evalWhiskerRight_cons_whisker
#[pf_η₁, pf_η₂, pf_ηs₁, pf_ηs₂, pf_η₃, pf_η₄, pf_η₅]⟩
catch _ => return ⟨η₅, mkConst ``True⟩
| η, .comp g h => do
let ⟨η₁, pf_η₁⟩ ← evalWhiskerRightExpr η g
let ⟨η₂, pf_η₂⟩ ← evalWhiskerRightExpr η₁ h
let f := η.src
let f' := η.tgt
let ⟨η₃, pf_η₃⟩ ← evalComp η₂ (.associator f' g h)
let ⟨η₄, pf_η₄⟩ ← evalComp (.associatorInv f g h) η₃
try return ⟨η₄, ← mkAppM ``evalWhiskerRight_comp #[pf_η₁, pf_η₂, pf_η₃, pf_η₄]⟩
catch _ => return ⟨η₄, mkConst ``True⟩
| η, .id => do
let f := η.src
let g := η.tgt
let ⟨η₁, pf_η₁⟩ ← evalComp η (.rightUnitorInv g)
let ⟨η₂, pf_η₂⟩ ← evalComp (.rightUnitor f) η₁
try return ⟨η₂, ← mkAppM ``evalWhiskerRight_id #[pf_η₁, pf_η₂]⟩
catch _ => return ⟨η₂, mkConst ``True⟩
/-- Evaluate the expression of a 2-morphism into a normalized form. -/
partial def eval (e : Expr) : MonoidalM Result := do
if let .some α ← structuralAtom? e then
try return ⟨.nil <| .atom α, ← mkEqRefl (← α.e)⟩
catch _ => return ⟨.nil <| .atom α, mkConst ``True⟩
else
match e.getAppFnArgs with
| (``CategoryStruct.id, #[_, _, f]) =>
try return ⟨.nil (.id (← toMor₁ f)), ← mkEqRefl (← mkAppM ``CategoryStruct.id #[f])⟩
catch _ => return ⟨.nil (.id (← toMor₁ f)), mkConst ``True⟩
| (``CategoryStruct.comp, #[_, _, _, _, _, η, θ]) =>
let ⟨η_e, pf_η⟩ ← eval η
let ⟨θ_e, pf_θ⟩ ← eval θ
let ⟨ηθ, pf⟩ ← evalComp η_e θ_e
try return ⟨ηθ, ← mkAppM ``eval_comp #[pf_η, pf_θ, pf]⟩
catch _ => return ⟨ηθ, mkConst ``True⟩
| (``MonoidalCategoryStruct.whiskerLeft, #[_, _, _, f, _, _, η]) =>
let ⟨η_e, pf_η⟩ ← eval η
let ⟨θ, pf_θ⟩ ← evalWhiskerLeftExpr (← toMor₁ f) η_e
try return ⟨θ, ← mkAppM ``eval_whiskerLeft #[pf_η, pf_θ]⟩
catch _ => return ⟨θ, mkConst ``True⟩
| (``MonoidalCategoryStruct.whiskerRight, #[_, _, _, _, _, η, h]) =>
let ⟨η_e, pf_η⟩ ← eval η
let ⟨θ, pf_θ⟩ ← evalWhiskerRightExpr η_e (← toMor₁ h)
try return ⟨θ, ← mkAppM ``eval_whiskerRight #[pf_η, pf_θ]⟩
catch _ => return ⟨θ, mkConst ``True⟩
| (``monoidalComp, #[C, _, _, _, _, _, _, η, θ]) =>
let ⟨η_e, pf_η⟩ ← eval η
let α₀ ← structuralOfMonoidalComp C e
let α := NormalExpr.nil α₀
let ⟨θ_e, pf_θ⟩ ← eval θ
let ⟨αθ, pf_θα⟩ ← evalComp α θ_e
let ⟨ηαθ, pf_ηαθ⟩ ← evalComp η_e αθ
try return ⟨ηαθ, ← mkAppM ``eval_monoidalComp #[pf_η, pf_θ, pf_θα, pf_ηαθ]⟩
catch _ => return ⟨ηαθ, mkConst ``True⟩
| _ =>
try return ⟨← NormalExpr.ofExpr e, ← mkAppM ``eval_of #[e]⟩
catch _ => return ⟨← NormalExpr.ofExpr e, mkConst ``True⟩
/-- Convert a `NormalExpr` expression into a list of `WhiskerLeftExpr` expressions. -/
def NormalExpr.toList : NormalExpr → List WhiskerLeftExpr
| NormalExpr.nil _ => []
| NormalExpr.cons _ η ηs => η :: NormalExpr.toList ηs
end Mathlib.Tactic.Monoidal
open Mathlib.Tactic.Monoidal
/-- `normalize% η` is the normalization of the 2-morphism `η`.
1. The normalized 2-morphism is of the form `α₀ ≫ η₀ ≫ α₁ ≫ η₁ ≫ ... αₘ ≫ ηₘ ≫ αₘ₊₁` where
each `αᵢ` is a structural 2-morphism (consisting of associators and unitors),
2. each `ηᵢ` is a non-structural 2-morphism of the form `f₁ ◁ ... ◁ fₘ ◁ θ`, and
3. `θ` is of the form `ι ▷ g₁ ▷ ... ▷ gₗ`
-/
elab "normalize% " t:term:51 : term => do
let e ← Lean.Elab.Term.elabTerm t none
MonoidalM.run (← mkContext e) do (← eval e).expr.e
theorem mk_eq {α : Type _} (a b a' b' : α) (ha : a = a') (hb : b = b') (h : a' = b') : a = b := by
simp [h, ha, hb]
open Lean Elab Meta Tactic in
/-- Transform an equality between 2-morphisms into the equality between their normalizations. -/
def mkEq (e : Expr) : MetaM Expr := do
let some (_, e₁, e₂) := (← whnfR <| e).eq?
| throwError "monoidal_nf requires an equality goal"
MonoidalM.run (← mkContext e₁) do
let ⟨e₁', p₁⟩ ← eval e₁
let ⟨e₂', p₂⟩ ← eval e₂
mkAppM ``mk_eq #[e₁, e₂, ← e₁'.e, ← e₂'.e, p₁, p₂]
open Lean Elab Tactic in
/-- Normalize the both sides of an equality. -/
elab "monoidal_nf" : tactic => withMainContext do
let t ← getMainTarget
let mvarIds ← (← getMainGoal).apply (← mkEq t)
replaceMainGoal mvarIds