forked from leanprover-community/mathlib4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdjunctions.lean
More file actions
401 lines (333 loc) · 13.9 KB
/
Copy pathAdjunctions.lean
File metadata and controls
401 lines (333 loc) · 13.9 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
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
/-
Copyright (c) 2021 Kim Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kim Morrison, Johan Commelin
-/
module
public import Mathlib.Algebra.Category.ModuleCat.Monoidal.Basic
public import Mathlib.Algebra.MonoidAlgebra.Module
public import Mathlib.CategoryTheory.Linear.LinearFunctor
public import Mathlib.CategoryTheory.Monoidal.Types.Basic
public import Mathlib.LinearAlgebra.DirectSum.Finsupp
/-!
The functor of forming finitely supported functions on a type with values in a `[Ring R]`
is the left adjoint of
the forgetful functor from `R`-modules to types.
-/
@[expose] public noncomputable section
assert_not_exists Cardinal
open CategoryTheory
open scoped MonoidAlgebra
namespace ModuleCat
universe u
variable (R : Type u)
section
variable [Ring R]
/-- The free functor `Type u ⥤ ModuleCat R` sending a type `X` to the
free `R`-module with generators `x : X`, implemented as the type `X →₀ R`.
-/
def free : Type u ⥤ ModuleCat R where
obj X := ModuleCat.of R (X →₀ R)
map {_ _} f := ofHom <| Finsupp.lmapDomain _ _ (f : _ → _)
/-- The free functor `Type u ⥤ ModuleCat R` sending a type `X` to the
free `R`-module with generators `x : X`, implemented as the monoid algebra `R[X]`.
-/
@[simps]
def monoidAlgebraFree : Type u ⥤ ModuleCat.{u} R where
obj X := .of R R[X]
map f := ofHom (MonoidAlgebra.mapDomainLinearMap R R f)
variable {R}
/-- Constructor for elements in the module `(free R).obj X`. -/
noncomputable def freeMk {X : Type u} (x : X) : (free R).obj X := Finsupp.single x 1
@[ext 1200]
lemma free_hom_ext {X : Type u} {M : ModuleCat.{u} R} {f g : (free R).obj X ⟶ M}
(h : ∀ (x : X), f (freeMk x) = g (freeMk x)) :
f = g :=
ModuleCat.hom_ext (Finsupp.lhom_ext' (fun x ↦ LinearMap.ext_ring (h x)))
/-- The morphism of modules `(free R).obj X ⟶ M` corresponding
to a map `f : X ⟶ M`. -/
noncomputable def freeDesc {X : Type u} {M : ModuleCat.{u} R} (f : X ⟶ M) :
(free R).obj X ⟶ M :=
ofHom <| Finsupp.lift M R X f
@[simp]
lemma freeDesc_apply {X : Type u} {M : ModuleCat.{u} R} (f : X ⟶ M) (x : X) :
freeDesc f (freeMk x) = f x := by
dsimp [freeDesc]
erw [Finsupp.lift_apply, Finsupp.sum_single_index]
all_goals simp
@[simp]
lemma free_map_apply {X Y : Type u} (f : X ⟶ Y) (x : X) :
(free R).map f (freeMk x) = freeMk (f x) := by
apply Finsupp.mapDomain_single
/-- The bijection `((free R).obj X ⟶ M) ≃ (X → M)` when `X` is a type and `M` a module. -/
@[simps]
def freeHomEquiv {X : Type u} {M : ModuleCat.{u} R} :
((free R).obj X ⟶ M) ≃ (X ⟶ M) where
toFun φ := ↾fun x ↦ φ (freeMk x)
invFun ψ := freeDesc (↾ψ)
left_inv _ := by ext; simp
right_inv _ := by ext; simp
variable (R)
set_option backward.isDefEq.respectTransparency.types false in
/-- The free-forgetful adjunction for R-modules. -/
def adj : free R ⊣ forget (ModuleCat.{u} R) :=
Adjunction.mkOfHomEquiv
{ homEquiv := fun _ _ => freeHomEquiv
homEquiv_naturality_left_symm := fun {X Y M} f g ↦ by ext; simp [freeHomEquiv] }
@[simp]
lemma adj_homEquiv (X : Type u) (M : ModuleCat.{u} R) :
(adj R).homEquiv X M = freeHomEquiv := by
simp only [adj, Adjunction.mkOfHomEquiv_homEquiv]
instance : (forget (ModuleCat.{u} R)).IsRightAdjoint :=
(adj R).isRightAdjoint
end
section Free
open MonoidalCategory
variable [CommRing R]
namespace FreeMonoidal
set_option backward.isDefEq.respectTransparency.types false in
/-- The canonical isomorphism `𝟙_ (ModuleCat R) ≅ (free R).obj (𝟙_ (Type u))`.
(This should not be used directly: it is part of the implementation of the
monoidal structure on the functor `free R`.) -/
def εIso : 𝟙_ (ModuleCat R) ≅ (free R).obj (𝟙_ (Type u)) where
hom := ofHom <| Finsupp.lsingle PUnit.unit
inv := ofHom <| Finsupp.lapply PUnit.unit
hom_inv_id := by
ext
simp [free]
inv_hom_id := by
ext ⟨⟩
dsimp [freeMk]
erw [Finsupp.lapply_apply, Finsupp.lsingle_apply]
rw [Finsupp.single_eq_same]
set_option backward.isDefEq.respectTransparency.types false in
@[simp]
lemma εIso_hom_one : (εIso R).hom 1 = freeMk PUnit.unit := rfl
set_option backward.isDefEq.respectTransparency.types false in
@[simp]
lemma εIso_inv_freeMk (x : PUnit) : (εIso R).inv (freeMk x) = 1 := by
dsimp [εIso, freeMk]
erw [Finsupp.lapply_apply]
rw [Finsupp.single_eq_same]
/-- The canonical isomorphism `(free R).obj X ⊗ (free R).obj Y ≅ (free R).obj (X ⊗ Y)`
for two types `X` and `Y`.
(This should not be used directly: it is part of the implementation of the
monoidal structure on the functor `free R`.) -/
def μIso (X Y : Type u) :
(free R).obj X ⊗ (free R).obj Y ≅ (free R).obj (X ⊗ Y) :=
(finsuppTensorFinsupp' R _ _).toModuleIso
@[simp]
lemma μIso_hom_freeMk_tmul_freeMk {X Y : Type u} (x : X) (y : Y) :
(μIso R X Y).hom (freeMk x ⊗ₜ freeMk y) = freeMk (x, y) := by
dsimp [μIso, freeMk]
erw [finsuppTensorFinsupp'_single_tmul_single]
rw [mul_one]
set_option backward.isDefEq.respectTransparency false in
@[simp]
lemma μIso_inv_freeMk {X Y : Type u} (z : X ⊗ Y) :
(μIso R X Y).inv (freeMk z) = freeMk z.1 ⊗ₜ freeMk z.2 := by
dsimp [μIso, freeMk]
erw [finsuppTensorFinsupp'_symm_single_eq_single_one_tmul]
end FreeMonoidal
set_option backward.isDefEq.respectTransparency.types false in
open FreeMonoidal in
/-- The free functor `Type u ⥤ ModuleCat R` is a monoidal functor. -/
instance : (free R).Monoidal :=
Functor.CoreMonoidal.toMonoidal
{ εIso := εIso R
μIso := μIso R
μIso_hom_natural_left := fun {X Y} f X' ↦ by
rw [← cancel_epi (μIso R X X').inv]
aesop
μIso_hom_natural_right := fun {X Y} X' f ↦ by
rw [← cancel_epi (μIso R X' X).inv]
aesop
associativity := fun X Y Z ↦ by
rw [← cancel_epi ((μIso R X Y).inv ▷ _), ← cancel_epi (μIso R _ _).inv]
ext ⟨⟨x, y⟩, z⟩
dsimp
rw [μIso_inv_freeMk, MonoidalCategory.whiskerRight_apply, μIso_inv_freeMk,
MonoidalCategory.whiskerRight_apply, μIso_hom_freeMk_tmul_freeMk,
μIso_hom_freeMk_tmul_freeMk, free_map_apply, CategoryTheory.associator_hom_apply,
MonoidalCategory.associator_hom_apply, MonoidalCategory.whiskerLeft_apply,
μIso_hom_freeMk_tmul_freeMk, μIso_hom_freeMk_tmul_freeMk]
left_unitality := fun X ↦ by
rw [← cancel_epi (λ_ _).inv, Iso.inv_hom_id]
aesop
right_unitality := fun X ↦ by
rw [← cancel_epi (ρ_ _).inv, Iso.inv_hom_id]
aesop }
open Functor.LaxMonoidal Functor.OplaxMonoidal
set_option backward.isDefEq.respectTransparency.types false in
@[simp]
lemma free_ε_one : ε (free R) 1 = freeMk PUnit.unit := rfl
set_option backward.isDefEq.respectTransparency.types false in
@[simp]
lemma free_η_freeMk (x : PUnit) : η (free R) (freeMk x) = 1 := by
apply FreeMonoidal.εIso_inv_freeMk
@[simp]
lemma free_μ_freeMk_tmul_freeMk {X Y : Type u} (x : X) (y : Y) :
μ (free R) _ _ (freeMk x ⊗ₜ freeMk y) = freeMk (x, y) := by
apply FreeMonoidal.μIso_hom_freeMk_tmul_freeMk
@[simp]
lemma free_δ_freeMk {X Y : Type u} (z : X ⊗ Y) :
δ (free R) _ _ (freeMk z) = freeMk z.1 ⊗ₜ freeMk z.2 := by
apply FreeMonoidal.μIso_inv_freeMk
end Free
end ModuleCat
namespace CategoryTheory
universe v u
/-- `Free R C` is a type synonym for `C`, which, given `[CommRing R]` and `[Category* C]`,
we will equip with a category structure where the morphisms are formal `R`-linear combinations
of the morphisms in `C`.
-/
@[nolint unusedArguments]
def Free (_ : Type*) (C : Type u) :=
C
/-- Consider an object of `C` as an object of the `R`-linear completion.
It may be preferable to use `(Free.embedding R C).obj X` instead;
this functor can also be used to lift morphisms.
-/
def Free.of (R : Type*) {C : Type u} (X : C) : Free R C :=
X
variable (R : Type*) [CommRing R] (C : Type u) [Category.{v} C]
open Finsupp
-- Conceptually, it would be nice to construct this via "transport of enrichment",
-- using the fact that `ModuleCat.Free R : Type ⥤ ModuleCat R` and `ModuleCat.forget` are both lax
-- monoidal. This still seems difficult, so we just do it by hand.
set_option backward.isDefEq.respectTransparency.types false in
instance categoryFree : Category (Free R C) where
Hom := fun X Y : C => (X ⟶ Y) →₀ R
id := fun X : C => Finsupp.single (𝟙 X) 1
comp {X _ Z : C} f g :=
(f.sum (fun f' s => g.sum (fun g' t => Finsupp.single (f' ≫ g') (s * t))) : (X ⟶ Z) →₀ R)
assoc {W X Y Z} f g h := by
-- This imitates the proof of associativity for `MonoidAlgebra`.
simp [sum_sum_index, add_mul, mul_add, Category.assoc, mul_assoc]
namespace Free
section
set_option backward.isDefEq.respectTransparency.types false in
instance : Preadditive (Free R C) where
homGroup _ _ := Finsupp.instAddCommGroup
add_comp X Y Z f f' g := by
dsimp +instances [CategoryTheory.categoryFree]
rw [Finsupp.sum_add_index'] <;> · simp [add_mul]
comp_add X Y Z f g g' := by
dsimp +instances [CategoryTheory.categoryFree]
rw [← Finsupp.sum_add]
congr; ext r h
rw [Finsupp.sum_add_index'] <;> · simp [mul_add]
set_option backward.isDefEq.respectTransparency.types false in
instance : Linear R (Free R C) where
homModule _ _ := Finsupp.module _ R
smul_comp X Y Z r f g := by
dsimp +instances [CategoryTheory.categoryFree]
rw [Finsupp.sum_smul_index] <;> simp [Finsupp.smul_sum, mul_assoc]
comp_smul X Y Z f r g := by
dsimp +instances [CategoryTheory.categoryFree]
simp_rw [Finsupp.smul_sum]
congr; ext h s
rw [Finsupp.sum_smul_index] <;> simp [mul_left_comm]
set_option backward.isDefEq.respectTransparency false in
theorem single_comp_single {X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) (r s : R) :
(single f r ≫ single g s : Free.of R X ⟶ Free.of R Z) = single (f ≫ g) (r * s) := by
dsimp +instances [CategoryTheory.categoryFree]
simp
end
attribute [local simp] single_comp_single
set_option backward.isDefEq.respectTransparency false in
/-- A category embeds into its `R`-linear completion.
-/
@[simps]
def embedding : C ⥤ Free R C where
obj X := X
map {_ _} f := Finsupp.single f 1
map_id _ := rfl
map_comp {X Y Z} f g := by
-- Porting note (https://github.com/leanprover-community/mathlib4/issues/10959): simp used to be able to close this goal
rw [single_comp_single, one_mul]
variable {C} {D : Type u} [Category.{v} D] [Preadditive D] [Linear R D]
open Preadditive Linear
set_option backward.isDefEq.respectTransparency false in
/-- A functor to an `R`-linear category lifts to a functor from its `R`-linear completion.
-/
@[simps]
def lift (F : C ⥤ D) : Free R C ⥤ D where
obj X := F.obj X
map {_ _} f := f.sum fun f' r => r • F.map f'
map_id := by
dsimp +instances [CategoryTheory.categoryFree]
simp
map_comp {X Y Z} f g := by
induction f using Finsupp.induction_linear with
| zero => simp
| add f₁ f₂ w₁ w₂ =>
rw [add_comp]
rw [Finsupp.sum_add_index', Finsupp.sum_add_index']
· simp only [w₁, w₂, add_comp]
· intros; rw [zero_smul]
· intros; simp only [add_smul]
· intros; rw [zero_smul]
· intros; simp only [add_smul]
| single f' r =>
induction g using Finsupp.induction_linear with
| zero => simp
| add f₁ f₂ w₁ w₂ =>
rw [comp_add]
rw [Finsupp.sum_add_index', Finsupp.sum_add_index']
· simp only [w₁, w₂, comp_add]
· intros; rw [zero_smul]
· intros; simp only [add_smul]
· intros; rw [zero_smul]
· intros; simp only [add_smul]
| single g' s =>
rw [single_comp_single _ _ f' g' r s]
simp [mul_comm r s, mul_smul]
set_option backward.isDefEq.respectTransparency.types false in
theorem lift_map_single (F : C ⥤ D) {X Y : C} (f : X ⟶ Y) (r : R) :
(lift R F).map (single f r) = r • F.map f := by simp
set_option backward.defeqAttrib.useBackward true in
set_option backward.isDefEq.respectTransparency false in
instance lift_additive (F : C ⥤ D) : (lift R F).Additive where
map_add {X Y} f g := by
dsimp
rw [Finsupp.sum_add_index'] <;> simp [add_smul]
set_option backward.defeqAttrib.useBackward true in
set_option backward.isDefEq.respectTransparency false in
instance lift_linear (F : C ⥤ D) : (lift R F).Linear R where
map_smul {X Y} f r := by
dsimp
rw [Finsupp.sum_smul_index] <;> simp [Finsupp.smul_sum, mul_smul]
set_option backward.isDefEq.respectTransparency.types false in
set_option backward.defeqAttrib.useBackward true in
/-- The embedding into the `R`-linear completion, followed by the lift,
is isomorphic to the original functor.
-/
def embeddingLiftIso (F : C ⥤ D) : embedding R C ⋙ lift R F ≅ F :=
NatIso.ofComponents fun _ => Iso.refl _
set_option backward.isDefEq.respectTransparency false in
/-- Two `R`-linear functors out of the `R`-linear completion are isomorphic iff their
compositions with the embedding functor are isomorphic.
-/
def ext {F G : Free R C ⥤ D} [F.Additive] [F.Linear R] [G.Additive] [G.Linear R]
(α : embedding R C ⋙ F ≅ embedding R C ⋙ G) : F ≅ G :=
NatIso.ofComponents (fun X => α.app X)
(by
intro X Y f
induction f using Finsupp.induction_linear with
| zero => simp
| add f₁ f₂ w₁ w₂ =>
rw [Functor.map_add, add_comp, w₁, w₂, Functor.map_add, comp_add]
| single f' r =>
rw [Iso.app_hom, Iso.app_hom, ← smul_single_one, F.map_smul, G.map_smul, smul_comp,
comp_smul]
change r • (embedding R C ⋙ F).map f' ≫ _ = r • _ ≫ (embedding R C ⋙ G).map f'
rw [α.hom.naturality f'])
/-- `Free.lift` is unique amongst `R`-linear functors `Free R C ⥤ D`
which compose with `embedding ℤ C` to give the original functor.
-/
def liftUnique (F : C ⥤ D) (L : Free R C ⥤ D) [L.Additive] [L.Linear R]
(α : embedding R C ⋙ L ≅ F) : L ≅ lift R F :=
ext R (α.trans (embeddingLiftIso R F).symm)
end Free
end CategoryTheory