Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 0858157

Browse files
Yury G. Kudryashovmergify[bot]
authored andcommitted
refactor(category_theory/category): reorder arguments of End.has_mul (#1128)
* Reorder arguments of `End.has_mul` and `Aut.has_mul`, adjust `category/fold` * clean up proofs in `category.fold`
1 parent e310349 commit 0858157

File tree

4 files changed

+54
-48
lines changed

4 files changed

+54
-48
lines changed

src/category/fold.lean

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -86,28 +86,30 @@ We can use traverse and const to construct this composition:
8686
And this is how `const` turns a monoid into an applicative functor and
8787
how the monoid of endofunctions define `foldl`.
8888
-/
89-
@[reducible] def foldl (α : Type u) : Type u := End α
90-
def foldl.mk (f : α → α) : foldl α := f
89+
@[reducible] def foldl (α : Type u) : Type u := (End α)ᵒᵖ
90+
def foldl.mk (f : α → α) : foldl α := op f
91+
def foldl.get (x : foldl α) : α → α := unop x
9192
def foldl.of_free_monoid (f : β → α → β) (xs : free_monoid α) : monoid.foldl β :=
92-
flip (list.foldl f) xs
93+
op $ flip (list.foldl f) xs
9394

94-
@[reducible] def foldr (α : Type u) : Type u := opposite (End α)
95-
def foldr.mk (f : α → α) : foldr α := op f
96-
def foldr.get (x : foldr α) : α → α := unop x
95+
@[reducible] def foldr (α : Type u) : Type u := End α
96+
def foldr.mk (f : α → α) : foldr α := f
97+
def foldr.get (x : foldr α) : α → α := x
9798
def foldr.of_free_monoid (f : α → β → β) (xs : free_monoid α) : monoid.foldr β :=
98-
op $ flip (list.foldr f) xs
99+
flip (list.foldr f) xs
99100

100-
@[reducible] def mfoldl (m : Type u → Type u) [monad m] (α : Type u) : Type u := End $ Kleisli.mk m α
101-
def mfoldl.mk (f : α → m α) : mfoldl m α := f
101+
@[reducible] def mfoldl (m : Type u → Type u) [monad m] (α : Type u) : Type u := opposite $ End $ Kleisli.mk m α
102+
def mfoldl.mk (f : α → m α) : mfoldl m α := op f
103+
def mfoldl.get (x : mfoldl m α) : α → m α := unop x
102104
def mfoldl.of_free_monoid (f : β → α → m β) (xs : free_monoid α) : monoid.mfoldl m β :=
103-
flip (list.mfoldl f) xs
105+
op $ flip (list.mfoldl f) xs
104106

105107
@[reducible] def mfoldr (m : Type u → Type u) [monad m] (α : Type u) : Type u :=
106-
opposite (End $ Kleisli.mk m α)
107-
def mfoldr.mk (f : α → m α) : mfoldr m α := op f
108-
def mfoldr.get (x : mfoldr m α) : α → m α := unop x
108+
End $ Kleisli.mk m α
109+
def mfoldr.mk (f : α → m α) : mfoldr m α := f
110+
def mfoldr.get (x : mfoldr m α) : α → m α := x
109111
def mfoldr.of_free_monoid (f : α → β → m β) (xs : free_monoid α) : monoid.mfoldr m β :=
110-
op $ flip (list.mfoldr f) xs
112+
flip (list.mfoldr f) xs
111113

112114
end monoid
113115

@@ -121,10 +123,10 @@ def fold_map {α ω} [has_one ω] [has_mul ω] (f : α → ω) : t α → ω :=
121123
traverse (const.mk' ∘ f)
122124

123125
def foldl (f : α → β → α) (x : α) (xs : t β) : α :=
124-
fold_map (foldl.mk ∘ flip f) xs x
126+
(fold_map (foldl.mk ∘ flip f) xs).get x
125127

126128
def foldr (f : α → β → β) (x : β) (xs : t α) : β :=
127-
unop (fold_map (foldr.mk ∘ f) xs) x
129+
(fold_map (foldr.mk ∘ f) xs).get x
128130

129131
/--
130132
Conceptually, `to_list` collects all the elements of a collection
@@ -152,10 +154,10 @@ down $ foldl (λ l _, up $ l.down + 1) (up 0) xs
152154
variables {m : Type u → Type u} [monad m]
153155

154156
def mfoldl (f : α → β → m α) (x : α) (xs : t β) : m α :=
155-
fold_map (mfoldl.mk ∘ flip f) xs x
157+
(fold_map (mfoldl.mk ∘ flip f) xs).get x
156158

157159
def mfoldr (f : α → β → m β) (x : β) (xs : t α) : m β :=
158-
unop (fold_map (mfoldr.mk ∘ f) xs) x
160+
(fold_map (mfoldr.mk ∘ f) xs).get x
159161

160162
end defs
161163

@@ -184,32 +186,31 @@ instance (f : α → β) : is_monoid_hom (free.map f) :=
184186
instance fold_foldl (f : β → α → β) :
185187
is_monoid_hom (foldl.of_free_monoid f) :=
186188
{ map_one := rfl,
187-
map_mul := by { intros, unfold_projs, simp only [foldl.of_free_monoid, flip, list.foldl_append], } }
189+
map_mul := by intros; simp only [free_monoid.mul_def, foldl.of_free_monoid, flip, unop_op, list.foldl_append, op_inj_iff]; refl }
188190

189-
lemma foldr.unop_of_free_monoid (f : αβ → β) (xs : free_monoid α) (a : β) :
190-
unop (foldr.of_free_monoid f xs) a = list.foldr f a xs := rfl
191+
lemma foldl.unop_of_free_monoid (f : βα → β) (xs : free_monoid α) (a : β) :
192+
unop (foldl.of_free_monoid f xs) a = list.foldl f a xs := rfl
191193

192194
instance fold_foldr (f : α → β → β) :
193195
is_monoid_hom (foldr.of_free_monoid f) :=
194196
{ map_one := rfl,
195-
map_mul := by { intros, apply unop_inj, ext, simp only [foldr.of_free_monoid,flip,free_add_monoid.add_def, list.foldr_append], refl } }
197+
map_mul := by intros; simp only [free_monoid.mul_def, foldr.of_free_monoid, list.foldr_append, flip]; refl }
196198

197199
variables (m : Type u → Type u) [monad m] [is_lawful_monad m]
198200

201+
@[simp]
202+
lemma mfoldl.unop_of_free_monoid (f : β → α → m β) (xs : free_monoid α) (a : β) :
203+
unop (mfoldl.of_free_monoid f xs) a = list.mfoldl f a xs := rfl
204+
199205
instance fold_mfoldl (f : β → α → m β) :
200206
is_monoid_hom (mfoldl.of_free_monoid f) :=
201207
{ map_one := rfl,
202-
map_mul := by { intros, unfold_projs, simp only [mfoldl.of_free_monoid,flip, list.mfoldl_append] } }
203-
204-
@[simp]
205-
lemma mfoldr.unop_of_free_monoid (f : α → β → m β) (xs : free_monoid α) (a : β) :
206-
unop (mfoldr.of_free_monoid f xs) a = list.mfoldr f a xs := rfl
208+
map_mul := by intros; apply unop_inj; ext; apply list.mfoldl_append }
207209

208210
instance fold_mfoldr (f : α → β → m β) :
209211
is_monoid_hom (mfoldr.of_free_monoid f) :=
210212
{ map_one := rfl,
211-
map_mul := by { intros, apply unop_inj, ext, simp only [list.mfoldr_append, mfoldr.unop_of_free_monoid, free_add_monoid.add_def],
212-
apply bind_ext_congr, simp only [mfoldr.unop_of_free_monoid, eq_self_iff_true, forall_true_iff], } }
213+
map_mul := by intros; ext; apply list.mfoldr_append }
213214

214215
variables {t : Type u → Type u} [traversable t] [is_lawful_traversable t]
215216
open is_lawful_traversable
@@ -254,11 +255,11 @@ lemma foldr.of_free_monoid_comp_free_mk (f : β → α → α) : foldr.of_free_m
254255

255256
@[simp]
256257
lemma mfoldl.of_free_monoid_comp_free_mk {m} [monad m] [is_lawful_monad m] (f : α → β → m α) : mfoldl.of_free_monoid f ∘ free.mk = mfoldl.mk ∘ flip f :=
257-
by { ext, simp only [(∘), mfoldl.of_free_monoid, mfoldl.mk, flip, fold_mfoldl_cons] }
258+
by ext; simp only [(∘), mfoldl.of_free_monoid, mfoldl.mk, flip, fold_mfoldl_cons]; refl
258259

259260
@[simp]
260261
lemma mfoldr.of_free_monoid_comp_free_mk {m} [monad m] [is_lawful_monad m] (f : β → α → m α) : mfoldr.of_free_monoid f ∘ free.mk = mfoldr.mk ∘ f :=
261-
by { ext, apply unop_inj, simp only [(∘),mfoldr.of_free_monoid,mfoldr.mk,flip,fold_mfoldr_cons] }
262+
by { ext, simp only [(∘), mfoldr.of_free_monoid, mfoldr.mk, flip, fold_mfoldr_cons] }
262263

263264
lemma to_list_spec (xs : t α) :
264265
to_list xs = (fold_map free.mk xs : free_monoid _) :=
@@ -267,25 +268,27 @@ calc fold_map free.mk xs
267268
= (fold_map free.mk xs).reverse.reverse : by simp only [list.reverse_reverse]
268269
... = (list.foldr cons [] (fold_map free.mk xs).reverse).reverse
269270
: by simp only [list.foldr_eta]
270-
... = (foldl.of_free_monoid (flip cons) (fold_map free.mk xs) []).reverse
271-
: by simp only [flip,list.foldr_reverse,foldl.of_free_monoid]
272-
... = to_list xs : by { rw fold_map_hom_free (foldl.of_free_monoid (flip cons)),
273-
simp only [to_list, foldl, list.reverse_inj, foldl.of_free_monoid_comp_free_mk],
274-
all_goals { apply_instance } }
271+
... = (unop (foldl.of_free_monoid (flip cons) (fold_map free.mk xs)) []).reverse
272+
: by simp only [flip,list.foldr_reverse,foldl.of_free_monoid, unop_op]
273+
... = to_list xs : begin
274+
rw fold_map_hom_free (foldl.of_free_monoid (flip cons)),
275+
simp only [to_list, foldl, list.reverse_inj, foldl.get, foldl.of_free_monoid_comp_free_mk],
276+
all_goals { apply_instance }
277+
end
275278

276279
lemma fold_map_map [monoid γ] (f : α → β) (g : β → γ) (xs : t α) :
277280
fold_map g (f <$> xs) = fold_map (g ∘ f) xs :=
278281
by simp only [fold_map,traverse_map]
279282

280283
lemma foldl_to_list (f : α → β → α) (xs : t β) (x : α) :
281284
foldl f x xs = list.foldl f x (to_list xs) :=
282-
by { change _ = foldl.of_free_monoid _ _ _,
283-
simp only [foldl, to_list_spec, fold_map_hom_free (foldl.of_free_monoid f), foldl.of_free_monoid_comp_free_mk] }
285+
by { rw ← foldl.unop_of_free_monoid,
286+
simp only [foldl, to_list_spec, fold_map_hom_free (foldl.of_free_monoid f), foldl.of_free_monoid_comp_free_mk, foldl.get] }
284287

285288
lemma foldr_to_list (f : α → β → β) (xs : t α) (x : β) :
286289
foldr f x xs = list.foldr f x (to_list xs) :=
287-
by { rw ← foldr.unop_of_free_monoid,
288-
simp only [foldr, to_list_spec, fold_map_hom_free (foldr.of_free_monoid f), foldr.of_free_monoid_comp_free_mk] }
290+
by { change _ = foldr.of_free_monoid _ _ _,
291+
simp only [foldr, to_list_spec, fold_map_hom_free (foldr.of_free_monoid f), foldr.of_free_monoid_comp_free_mk, foldr.get] }
289292

290293
lemma to_list_map (f : α → β) (xs : t α) :
291294
to_list (f <$> xs) = f <$> to_list xs :=
@@ -330,13 +333,13 @@ variables {m : Type u → Type u} [monad m] [is_lawful_monad m]
330333

331334
lemma mfoldl_to_list {f : α → β → m α} {x : α} {xs : t β} :
332335
mfoldl f x xs = list.mfoldl f x (to_list xs) :=
333-
by { change _ = mfoldl.of_free_monoid f (to_list xs) x,
334-
simp only [mfoldl, to_list_spec, fold_map_hom_free (mfoldl.of_free_monoid f),mfoldl.of_free_monoid_comp_free_mk] }
336+
by { change _ = unop (mfoldl.of_free_monoid f (to_list xs)) x,
337+
simp only [mfoldl, to_list_spec, fold_map_hom_free (mfoldl.of_free_monoid f), mfoldl.of_free_monoid_comp_free_mk, mfoldl.get] }
335338

336339
lemma mfoldr_to_list (f : α → β → m β) (x : β) (xs : t α) :
337340
mfoldr f x xs = list.mfoldr f x (to_list xs) :=
338-
by { change _ = unop (mfoldr.of_free_monoid f (to_list xs)) x,
339-
simp only [mfoldr, to_list_spec, fold_map_hom_free (mfoldr.of_free_monoid f),mfoldr.of_free_monoid_comp_free_mk] }
341+
by { change _ = mfoldr.of_free_monoid f (to_list xs) x,
342+
simp only [mfoldr, to_list_spec, fold_map_hom_free (mfoldr.of_free_monoid f), mfoldr.of_free_monoid_comp_free_mk, mfoldr.get] }
340343

341344
@[simp] theorem mfoldl_map (g : β → γ) (f : α → γ → m α) (a : α) (l : t β) :
342345
mfoldl f a (g <$> l) = mfoldl (λ x y, f x (g y)) a l :=

src/category_theory/category.lean

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,14 @@ variables {C : Type u}
131131
def End [has_hom.{v} C] (X : C) := X ⟶ X
132132

133133
instance End.has_one [category_struct.{v+1} C] {X : C} : has_one (End X) := by refine { one := 𝟙 X }
134-
instance End.has_mul [category_struct.{v+1} C] {X : C} : has_mul (End X) := by refine { mul := λ x y, x ≫ y }
134+
/-- Multiplication of endomorphisms agrees with `function.comp`, not `category_struct.comp`. -/
135+
instance End.has_mul [category_struct.{v+1} C] {X : C} : has_mul (End X) := by refine { mul := λ x y, y ≫ x }
135136
instance End.monoid [category.{v+1} C] {X : C} : monoid (End X) :=
136137
by refine { .. End.has_one, .. End.has_mul, .. }; dsimp [has_mul.mul,has_one.one]; obviously
137138

138139
@[simp] lemma End.one_def {C : Type u} [category_struct.{v+1} C] {X : C} : (1 : End X) = 𝟙 X := rfl
139140

140-
@[simp] lemma End.mul_def {C : Type u} [category_struct.{v+1} C] {X : C} (xs ys : End X) : xs * ys = xsys := rfl
141+
@[simp] lemma End.mul_def {C : Type u} [category_struct.{v+1} C] {X : C} (xs ys : End X) : xs * ys = ysxs := rfl
141142

142143
end
143144

src/category_theory/isomorphism.lean

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,5 @@ attribute [extensionality Aut] iso.ext
239239
instance {X : C} : group (Aut X) :=
240240
by refine { one := iso.refl X,
241241
inv := iso.symm,
242-
mul := iso.trans, .. } ; obviously
243-
242+
mul := flip iso.trans, .. } ; dunfold flip; obviously
244243
end category_theory

src/data/opposite.lean

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ def unop : αᵒᵖ → α := id
4747
lemma op_inj : function.injective (op : α → αᵒᵖ) := λ _ _, id
4848
lemma unop_inj : function.injective (unop : αᵒᵖ → α) := λ _ _, id
4949

50+
@[simp] lemma op_inj_iff (x y : α) : op x = op y ↔ x = y := iff.refl _
51+
@[simp] lemma unop_inj_iff (x y : αᵒᵖ) : unop x = unop y ↔ x = y := iff.refl _
52+
5053
@[simp] lemma op_unop (x : αᵒᵖ) : op (unop x) = x := rfl
5154
@[simp] lemma unop_op (x : α) : unop (op x) = x := rfl
5255

0 commit comments

Comments
 (0)