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

Commit 442382d

Browse files
committed
feat(tactic/to_additive): Improvements to to_additive (#5487)
Change a couple of things in to_additive: - First add a `tactic.copy_attribute'` intended for user attributes with parameters very similar to `tactic.copy_attribute` that just copies the parameter over when setting the attribute. This allows to_additive after many other attributes to transfer those attributes properly (e.g. norm_cast) - Have to additive register generated equation lemmas as such, this necessitates a bit of restructuring, first all declarations must be generated (including equational lemmas), then the equational lemmas need their attributes, then they are registered as equation lemmas, finally the attributes are added to the main declaration. - I also fixup the library in many places to account for these changes simplifying the source files, only one new lemma was added, in src/analysis/normed/group/quotient.lean `quotient_norm_mk_le'` to replace the unprimed version in the proof of `norm_normed_mk_le` as simp got better thanks to the new simp lemmas introduced by this PR. Probably many more handwritten additive versions can now be deleted in a future PR, especially defs and instances. - All other library changes are just simplifications by using to additive for some hand generated declarations, and many more attributes on the generated lemmas. - The attribute mono is trickier as it contains for its parameter not actual exprs containing names but exprs making names from strings, so I don't see how to handle it right now. We now warn the user about this, and fix the library in a couple of places.
1 parent 8b7e16f commit 442382d

36 files changed

+156
-250
lines changed

src/algebra/big_operators/order.lean

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,12 @@ namespace fintype
443443

444444
variables [fintype ι]
445445

446-
@[mono, to_additive sum_mono]
446+
@[to_additive sum_mono, mono]
447447
lemma prod_mono' [ordered_comm_monoid M] : monotone (λ f : ι → M, ∏ i, f i) :=
448448
λ f g hfg, finset.prod_le_prod'' $ λ x _, hfg x
449449

450+
attribute [mono] sum_mono
451+
450452
@[to_additive sum_strict_mono]
451453
lemma prod_strict_mono' [ordered_cancel_comm_monoid M] : strict_mono (λ f : ι → M, ∏ x, f x) :=
452454
λ f g hfg, let ⟨hle, i, hlt⟩ := pi.lt_def.mp hfg in

src/algebra/category/Group/basic.lean

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ lemma one_apply (G H : Group) (g : G) : (1 : G ⟶ H) g = 1 := rfl
7171
lemma ext (G H : Group) (f₁ f₂ : G ⟶ H) (w : ∀ x, f₁ x = f₂ x) : f₁ = f₂ :=
7272
by { ext1, apply w }
7373

74-
-- should to_additive do this automatically?
75-
attribute [ext] AddGroup.ext
76-
7774
@[to_additive has_forget_to_AddMon]
7875
instance has_forget_to_Mon : has_forget₂ Group Mon := bundled_hom.forget₂ _ _
7976

@@ -128,12 +125,10 @@ instance one.unique : unique (1 : CommGroup) :=
128125
@[simp, to_additive]
129126
lemma one_apply (G H : CommGroup) (g : G) : (1 : G ⟶ H) g = 1 := rfl
130127

131-
@[to_additive,ext]
128+
@[ext, to_additive]
132129
lemma ext (G H : CommGroup) (f₁ f₂ : G ⟶ H) (w : ∀ x, f₁ x = f₂ x) : f₁ = f₂ :=
133130
by { ext1, apply w }
134131

135-
attribute [ext] AddCommGroup.ext
136-
137132
@[to_additive has_forget_to_AddGroup]
138133
instance has_forget_to_Group : has_forget₂ CommGroup Group := bundled_hom.forget₂ _ _
139134

src/algebra/category/Mon/filtered_colimits.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def cocone_morphism (j : J) : F.obj j ⟶ colimit :=
199199
rw [F.map_id, id_apply, id_apply], refl,
200200
end }
201201

202-
@[to_additive, simp]
202+
@[simp, to_additive]
203203
lemma cocone_naturality {j j' : J} (f : j ⟶ j') :
204204
F.map f ≫ (cocone_morphism j') = cocone_morphism j :=
205205
monoid_hom.coe_inj ((types.colimit_cocone (F ⋙ forget Mon)).ι.naturality f)

src/algebra/free.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ attribute [pattern] has_mul.mul
5555
theorem mul_eq (x y : free_magma α) : mul x y = x * y := rfl
5656

5757
/-- Recursor for `free_magma` using `x * y` instead of `free_magma.mul x y`. -/
58-
@[to_additive "Recursor for `free_add_magma` using `x + y` instead of `free_add_magma.add x y`."]
58+
@[elab_as_eliminator, to_additive
59+
"Recursor for `free_add_magma` using `x + y` instead of `free_add_magma.add x y`."]
5960
def rec_on' {C : free_magma α → Sort l} (x)
6061
(ih1 : ∀ x, C (of x)) (ih2 : ∀ x y, C x → C y → C (x * y)) :
6162
C x :=
6263
free_magma.rec_on x ih1 ih2
63-
attribute [elab_as_eliminator] rec_on' free_add_magma.rec_on'
6464

6565
end free_magma
6666

src/algebra/free_monoid.lean

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,17 @@ lemma of_injective : function.injective (@of α) :=
5757
λ a b, list.head_eq_of_cons_eq
5858

5959
/-- Recursor for `free_monoid` using `1` and `of x * xs` instead of `[]` and `x :: xs`. -/
60-
@[to_additive
60+
@[elab_as_eliminator, to_additive
6161
"Recursor for `free_add_monoid` using `0` and `of x + xs` instead of `[]` and `x :: xs`."]
6262
def rec_on {C : free_monoid α → Sort*} (xs : free_monoid α) (h0 : C 1)
6363
(ih : Π x xs, C xs → C (of x * xs)) : C xs := list.rec_on xs h0 ih
6464

65-
attribute [elab_as_eliminator] rec_on free_add_monoid.rec_on
66-
67-
@[to_additive]
65+
@[ext, to_additive]
6866
lemma hom_eq ⦃f g : free_monoid α →* M⦄ (h : ∀ x, f (of x) = g (of x)) :
6967
f = g :=
7068
monoid_hom.ext $ λ l, rec_on l (f.map_one.trans g.map_one.symm) $
7169
λ x xs hxs, by simp only [h, hxs, monoid_hom.map_mul]
7270

73-
attribute [ext] hom_eq free_add_monoid.hom_eq
74-
7571
/-- Equivalence between maps `α → M` and monoid homomorphisms `free_monoid α →* M`. -/
7672
@[to_additive "Equivalence between maps `α → A` and additive monoid homomorphisms
7773
`free_add_monoid α →+ A`."]

src/algebra/group/basic.lean

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ variables {G : Type u} [comm_semigroup G]
9494
@[no_rsimp, to_additive]
9595
lemma mul_left_comm : ∀ a b c : G, a * (b * c) = b * (a * c) :=
9696
left_comm has_mul.mul mul_comm mul_assoc
97-
attribute [no_rsimp] add_left_comm
9897

9998
@[to_additive]
10099
lemma mul_right_comm : ∀ a b c : G, a * b * c = a * c * b :=

src/algebra/group/defs.lean

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ variables {G : Type u} [semigroup G]
7676
lemma mul_assoc : ∀ a b c : G, a * b * c = a * (b * c) :=
7777
semigroup.mul_assoc
7878

79-
attribute [no_rsimp] add_assoc -- TODO(Mario): find out why this isn't copying
80-
8179
@[to_additive]
8280
instance semigroup.to_is_associative : is_associative G (*) :=
8381
⟨mul_assoc⟩
@@ -101,7 +99,6 @@ variables {G : Type u} [comm_semigroup G]
10199
@[no_rsimp, to_additive]
102100
lemma mul_comm : ∀ a b : G, a * b = b * a :=
103101
comm_semigroup.mul_comm
104-
attribute [no_rsimp] add_comm
105102

106103
@[to_additive]
107104
instance comm_semigroup.to_is_commutative : is_commutative G (*) :=
@@ -206,8 +203,6 @@ begin
206203
exact (one_mul₂ one₁).symm.trans (mul_one₁ one₂),
207204
end
208205

209-
attribute [ext] add_zero_class.ext
210-
211206
section mul_one_class
212207
variables {M : Type u} [mul_one_class M]
213208

@@ -219,8 +214,6 @@ mul_one_class.one_mul
219214
lemma mul_one : ∀ a : M, a * 1 = a :=
220215
mul_one_class.mul_one
221216

222-
attribute [ematch] add_zero zero_add -- TODO(Mario): Make to_additive transfer this
223-
224217
@[to_additive]
225218
instance mul_one_class.to_is_left_id : is_left_id M (*) 1 :=
226219
⟨ mul_one_class.one_mul ⟩
@@ -382,8 +375,6 @@ begin
382375
subst h_npow,
383376
end
384377

385-
attribute [ext] add_monoid.ext
386-
387378
section monoid
388379
variables {M : Type u} [monoid M]
389380

@@ -432,8 +423,6 @@ end
432423
lemma comm_monoid.ext {M : Type*} ⦃m₁ m₂ : comm_monoid M⦄ (h_mul : m₁.mul = m₂.mul) : m₁ = m₂ :=
433424
comm_monoid.to_monoid_injective $ monoid.ext h_mul
434425

435-
attribute [ext] add_comm_monoid.ext
436-
437426
section left_cancel_monoid
438427

439428
/-- An additive monoid in which addition is left-cancellative.
@@ -459,8 +448,6 @@ lemma left_cancel_monoid.ext {M : Type*} ⦃m₁ m₂ : left_cancel_monoid M⦄
459448
(h_mul : m₁.mul = m₂.mul) : m₁ = m₂ :=
460449
left_cancel_monoid.to_monoid_injective $ monoid.ext h_mul
461450

462-
attribute [ext] add_left_cancel_monoid.ext
463-
464451
end left_cancel_monoid
465452

466453
section right_cancel_monoid
@@ -488,8 +475,6 @@ lemma right_cancel_monoid.ext {M : Type*} ⦃m₁ m₂ : right_cancel_monoid M
488475
(h_mul : m₁.mul = m₂.mul) : m₁ = m₂ :=
489476
right_cancel_monoid.to_monoid_injective $ monoid.ext h_mul
490477

491-
attribute [ext] add_right_cancel_monoid.ext
492-
493478
end right_cancel_monoid
494479

495480
section cancel_monoid
@@ -518,8 +503,6 @@ lemma cancel_monoid.ext {M : Type*} ⦃m₁ m₂ : cancel_monoid M⦄
518503
(h_mul : m₁.mul = m₂.mul) : m₁ = m₂ :=
519504
cancel_monoid.to_left_cancel_monoid_injective $ left_cancel_monoid.ext h_mul
520505

521-
attribute [ext] add_cancel_monoid.ext
522-
523506
/-- Commutative version of add_cancel_monoid. -/
524507
@[protect_proj, ancestor add_left_cancel_monoid add_comm_monoid]
525508
class add_cancel_comm_monoid (M : Type u) extends add_left_cancel_monoid M, add_comm_monoid M
@@ -541,8 +524,6 @@ lemma cancel_comm_monoid.ext {M : Type*} ⦃m₁ m₂ : cancel_comm_monoid M⦄
541524
(h_mul : m₁.mul = m₂.mul) : m₁ = m₂ :=
542525
cancel_comm_monoid.to_comm_monoid_injective $ comm_monoid.ext h_mul
543526

544-
attribute [ext] add_cancel_comm_monoid.ext
545-
546527
@[priority 100, to_additive] -- see Note [lower instance priority]
547528
instance cancel_comm_monoid.to_cancel_monoid (M : Type u) [cancel_comm_monoid M] :
548529
cancel_monoid M :=
@@ -676,8 +657,6 @@ begin
676657
subst h_gpow,
677658
end
678659

679-
attribute [ext] sub_neg_monoid.ext
680-
681660
@[to_additive]
682661
lemma div_eq_mul_inv {G : Type u} [div_inv_monoid G] :
683662
∀ a b : G, a / b = a * b⁻¹ :=
@@ -794,8 +773,6 @@ begin
794773
exact h_inv
795774
end
796775

797-
attribute [ext] add_group.ext
798-
799776
/-- A commutative group is a group with commutative `(*)`. -/
800777
@[protect_proj, ancestor group comm_monoid]
801778
class comm_group (G : Type u) extends group G, comm_monoid G
@@ -821,8 +798,6 @@ lemma comm_group.ext {G : Type*} ⦃g₁ g₂ : comm_group G⦄
821798
(h_mul : g₁.mul = g₂.mul) : g₁ = g₂ :=
822799
comm_group.to_group_injective $ group.ext h_mul
823800

824-
attribute [ext] add_comm_group.ext
825-
826801
section comm_group
827802

828803
variables {G : Type u} [comm_group G]

src/algebra/group/hom.lean

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,6 @@ lemma monoid_with_zero_hom.ext [mul_zero_one_class M] [mul_zero_one_class N]
269269
⦃f g : monoid_with_zero_hom M N⦄ (h : ∀ x, f x = g x) : f = g :=
270270
monoid_with_zero_hom.coe_inj (funext h)
271271

272-
attribute [ext] zero_hom.ext add_hom.ext add_monoid_hom.ext
273-
274272
@[to_additive]
275273
lemma one_hom.ext_iff [has_one M] [has_one N] {f g : one_hom M N} : f = g ↔ ∀ x, f x = g x :=
276274
⟨λ h x, h ▸ rfl, λ h, one_hom.ext h⟩

src/algebra/group/to_additive.lean

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,10 +509,14 @@ protected meta def attr : user_attribute unit value_type :=
509509
then proceed_fields env src tgt prio
510510
else do
511511
transform_decl_with_prefix_dict dict val.replace_all val.trace relevant ignore reorder src tgt
512-
[`reducible, `_refl_lemma, `simp, `instance, `refl, `symm, `trans, `elab_as_eliminator,
513-
`no_rsimp, `measurability],
512+
[`reducible, `_refl_lemma, `simp, `norm_cast, `instance, `refl, `symm, `trans,
513+
`elab_as_eliminator, `no_rsimp, `continuity, `ext, `ematch, `measurability, `alias,
514+
`_ext_core, `_ext_lemma_core, `nolint],
514515
mwhen (has_attribute' `simps src)
515516
(trace "Apply the simps attribute after the to_additive attribute"),
517+
mwhen (has_attribute' `mono src)
518+
(trace $ "to_additive does not work with mono, apply the mono attribute to both" ++
519+
"versions after"),
516520
match val.doc with
517521
| some doc := add_doc_string tgt doc
518522
| none := skip

src/algebra/group/units.lean

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,8 @@ ext hv
106106

107107
variables (a b : units α) {c : units α}
108108
@[simp, norm_cast, to_additive] lemma coe_mul : (↑(a * b) : α) = a * b := rfl
109-
attribute [norm_cast] add_units.coe_add
110109

111110
@[simp, norm_cast, to_additive] lemma coe_one : ((1 : units α) : α) = 1 := rfl
112-
attribute [norm_cast] add_units.coe_zero
113111

114112
@[simp, norm_cast, to_additive] lemma coe_eq_one {a : units α} : (a : α) = 1 ↔ a = 1 :=
115113
by rw [←units.coe_one, eq_iff]

0 commit comments

Comments
 (0)