Skip to content

Commit 31672e2

Browse files
committed
feat: tag Pow.mk with to_additive (#36302)
This PR uses the new `reorder` support in `to_additive` to automatically generate `SMul` instances from `Pow` instances. I've tried to address most such instances, but this is probably not exhaustive. I've also taken the opportunity to uniformize the instance names a bit. I wonder, should we change the order of the arguments in `Monoid.npow : ℕ → M → M` and `npowRec` to be consistent with the usual power operation, now that `to_additive` can support this kind of reordering? I suspect that the current ordering only exists for the sake of `to_additive`.
1 parent f399585 commit 31672e2

File tree

26 files changed

+82
-238
lines changed

26 files changed

+82
-238
lines changed

Mathlib/Algebra/Group/Defs.lean

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -623,14 +623,10 @@ class Monoid (M : Type u) extends Semigroup M, MulOneClass M where
623623
/-- Raising to the power `(n + 1 : ℕ)` behaves as expected. -/
624624
protected npow_succ : ∀ (n : ℕ) (x), npow (n + 1) x = npow n x * x := by intros; rfl
625625

626-
@[default_instance high] instance Monoid.toNatPow {M : Type*} [Monoid M] : Pow M ℕ :=
626+
@[default_instance high, to_additive]
627+
instance Monoid.toPow {M : Type*} [Monoid M] : Pow M ℕ :=
627628
fun x n ↦ Monoid.npow n x⟩
628629

629-
instance AddMonoid.toNatSMul {M : Type*} [AddMonoid M] : SMul ℕ M :=
630-
⟨AddMonoid.nsmul⟩
631-
632-
attribute [to_additive existing toNatSMul] Monoid.toNatPow
633-
634630
section Monoid
635631
variable {M : Type*} [Monoid M] {a b c : M}
636632

Mathlib/Algebra/Group/Hom/Instances.lean

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,13 @@ universe uM uN uP uQ
3232

3333
variable {M : Type uM} {N : Type uN} {P : Type uP} {Q : Type uQ}
3434

35-
instance ZeroHom.instNatSMul [Zero M] [AddMonoid N] : SMul ℕ (ZeroHom M N) where
36-
smul a f :=
37-
{ toFun := a • f
38-
map_zero' := by simp }
39-
40-
instance AddMonoidHom.instNatSMul [AddZeroClass M] [AddCommMonoid N] : SMul ℕ (M →+ N) where
41-
smul a f :=
42-
{ toFun := a • f
43-
map_zero' := by simp
44-
map_add' x y := by simp [nsmul_add] }
45-
46-
@[to_additive existing ZeroHom.instNatSMul]
35+
@[to_additive]
4736
instance OneHom.instPow [One M] [Monoid N] : Pow (OneHom M N) ℕ where
4837
pow f n :=
4938
{ toFun := f ^ n
5039
map_one' := by simp }
5140

52-
@[to_additive existing AddMonoidHom.instNatSMul]
41+
@[to_additive]
5342
instance MonoidHom.instPow [MulOneClass M] [CommMonoid N] : Pow (M →* N) ℕ where
5443
pow f n :=
5544
{ toFun := f ^ n
@@ -84,25 +73,14 @@ instance MonoidHom.instCommMonoid [MulOneClass M] [CommMonoid N] : CommMonoid (M
8473
fast_instance%
8574
DFunLike.coe_injective.commMonoid DFunLike.coe rfl (fun _ _ => rfl) (fun _ _ => rfl)
8675

87-
instance ZeroHom.instIntSMul [Zero M] [AddGroup N] : SMul ℤ (ZeroHom M N) where
88-
smul a f :=
89-
{ toFun := a • f
90-
map_zero' := by simp [zsmul_zero] }
91-
92-
instance AddMonoidHom.instIntSMul [AddZeroClass M] [AddCommGroup N] : SMul ℤ (M →+ N) where
93-
smul a f :=
94-
{ toFun := a • f
95-
map_zero' := by simp [zsmul_zero]
96-
map_add' x y := by simp [zsmul_add] }
97-
98-
@[to_additive existing ZeroHom.instIntSMul]
99-
instance OneHom.instIntPow [One M] [Group N] : Pow (OneHom M N) ℤ where
76+
@[to_additive]
77+
instance OneHom.instZPow [One M] [Group N] : Pow (OneHom M N) ℤ where
10078
pow f n :=
10179
{ toFun := f ^ n
10280
map_one' := by simp }
10381

104-
@[to_additive existing AddMonoidHom.instIntSMul]
105-
instance MonoidHom.instIntPow [MulOneClass M] [CommGroup N] : Pow (M →* N) ℤ where
82+
@[to_additive]
83+
instance MonoidHom.instZPow [MulOneClass M] [CommGroup N] : Pow (M →* N) ℤ where
10684
pow f n :=
10785
{ toFun := f ^ n
10886
map_one' := by simp

Mathlib/Algebra/Group/Pointwise/Finset/Basic.lean

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -695,30 +695,19 @@ section Instances
695695

696696
variable [DecidableEq α] [DecidableEq β]
697697

698-
/-- Repeated pointwise addition (not the same as pointwise repeated addition!) of a `Finset`. See
699-
note [pointwise nat action]. -/
700-
@[instance_reducible]
701-
protected def nsmul [Zero α] [Add α] : SMul ℕ (Finset α) :=
702-
⟨nsmulRec⟩
703-
704698
/-- Repeated pointwise multiplication (not the same as pointwise repeated multiplication!) of a
705699
`Finset`. See note [pointwise nat action]. -/
706-
@[instance_reducible]
700+
@[to_additive (attr := instance_reducible)
701+
/-- Repeated pointwise addition (not the same as pointwise repeated addition!) of a `Finset`. See
702+
note [pointwise nat action]. -/]
707703
protected def npow [One α] [Mul α] : Pow (Finset α) ℕ :=
708704
fun s n => npowRec n s⟩
709705

710-
attribute [to_additive existing] Finset.npow
711-
712-
713-
/-- Repeated pointwise addition/subtraction (not the same as pointwise repeated
714-
addition/subtraction!) of a `Finset`. See note [pointwise nat action]. -/
715-
@[instance_reducible]
716-
protected def zsmul [Zero α] [Add α] [Neg α] : SMul ℤ (Finset α) :=
717-
⟨zsmulRec⟩
718-
719706
/-- Repeated pointwise multiplication/division (not the same as pointwise repeated
720707
multiplication/division!) of a `Finset`. See note [pointwise nat action]. -/
721-
@[instance_reducible, to_additive existing]
708+
@[to_additive (attr := instance_reducible)
709+
/-- Repeated pointwise addition/subtraction (not the same as pointwise repeated
710+
addition/subtraction!) of a `Finset`. See note [pointwise nat action]. -/]
722711
protected def zpow [One α] [Mul α] [Inv α] : Pow (Finset α) ℤ :=
723712
fun s n => zpowRec npowRec n s⟩
724713

Mathlib/Algebra/Group/Pointwise/Set/Basic.lean

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -531,27 +531,20 @@ theorem union_div_inter_subset_union : (s₁ ∪ s₂) / (t₁ ∩ t₂) ⊆ s
531531

532532
end Div
533533

534-
/-- Repeated pointwise addition (not the same as pointwise repeated addition!) of a `Set`. See
535-
note [pointwise nat action]. -/
536-
@[instance_reducible]
537-
protected def NSMul [Zero α] [Add α] : SMul ℕ (Set α) :=
538-
⟨nsmulRec⟩
539-
534+
-- TODO: rename `NPow` to `npow` and `ZPow` to `zpow`.
540535
/-- Repeated pointwise multiplication (not the same as pointwise repeated multiplication!) of a
541536
`Set`. See note [pointwise nat action]. -/
542-
@[instance_reducible, to_additive existing]
537+
@[to_additive (attr := instance_reducible)
538+
/-- Repeated pointwise addition (not the same as pointwise repeated addition!) of a `Set`. See
539+
note [pointwise nat action]. -/]
543540
protected def NPow [One α] [Mul α] : Pow (Set α) ℕ :=
544541
fun s n => npowRec n s⟩
545542

546-
/-- Repeated pointwise addition/subtraction (not the same as pointwise repeated
547-
addition/subtraction!) of a `Set`. See note [pointwise nat action]. -/
548-
@[instance_reducible]
549-
protected def ZSMul [Zero α] [Add α] [Neg α] : SMul ℤ (Set α) :=
550-
⟨zsmulRec⟩
551-
552543
/-- Repeated pointwise multiplication/division (not the same as pointwise repeated
553544
multiplication/division!) of a `Set`. See note [pointwise nat action]. -/
554-
@[instance_reducible, to_additive existing]
545+
@[to_additive (attr := instance_reducible)
546+
/-- Repeated pointwise addition/subtraction (not the same as pointwise repeated
547+
addition/subtraction!) of a `Set`. See note [pointwise nat action]. -/]
555548
protected def ZPow [One α] [Mul α] [Inv α] : Pow (Set α) ℤ :=
556549
fun s n => zpowRec npowRec n s⟩
557550

Mathlib/Algebra/Group/Subgroup/Defs.lean

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,9 @@ theorem subset_union [LE S] [IsConcreteLE S G] {H K L : S} :
190190
instance div {G S : Type*} [DivInvMonoid G] [SetLike S G] [SubgroupClass S G] {H : S} : Div H :=
191191
fun a b => ⟨a / b, div_mem a.2 b.2⟩⟩
192192

193-
/-- An additive subgroup of an `AddGroup` inherits an integer scaling. -/
194-
instance _root_.AddSubgroupClass.zsmul {M S} [SubNegMonoid M] [SetLike S M]
195-
[AddSubgroupClass S M] {H : S} : SMul ℤ H :=
196-
fun n a => ⟨n • a.1, zsmul_mem a.2 n⟩⟩
197-
198193
/-- A subgroup of a group inherits an integer power. -/
199-
@[to_additive existing]
200-
instance zpow {M S} [DivInvMonoid M] [SetLike S M] [SubgroupClass S M] {H : S} : Pow H ℤ :=
194+
@[to_additive /-- An additive subgroup of an `AddGroup` inherits an integer scaling. -/]
195+
instance instZPow {M S} [DivInvMonoid M] [SetLike S M] [SubgroupClass S M] {H : S} : Pow H ℤ :=
201196
fun a n => ⟨a.1 ^ n, zpow_mem a.2 n⟩⟩
202197

203198
@[to_additive (attr := simp, norm_cast)]
@@ -508,21 +503,13 @@ instance inv : Inv H :=
508503
instance div : Div H :=
509504
fun a b => ⟨a / b, H.div_mem a.2 b.2⟩⟩
510505

511-
/-- An `AddSubgroup` of an `AddGroup` inherits a natural scaling. -/
512-
instance _root_.AddSubgroup.nsmul {G} [AddGroup G] {H : AddSubgroup G} : SMul ℕ H :=
513-
fun n a => ⟨n • a, H.nsmul_mem a.2 n⟩⟩
514-
515506
/-- A subgroup of a group inherits a natural power -/
516-
@[to_additive existing]
507+
@[to_additive /-- An `AddSubgroup` of an `AddGroup` inherits a natural scaling. -/]
517508
protected instance npow : Pow H ℕ :=
518509
fun a n => ⟨a ^ n, H.pow_mem a.2 n⟩⟩
519510

520-
/-- An `AddSubgroup` of an `AddGroup` inherits an integer scaling. -/
521-
instance _root_.AddSubgroup.zsmul {G} [AddGroup G] {H : AddSubgroup G} : SMul ℤ H :=
522-
fun n a => ⟨n • a, H.zsmul_mem a.2 n⟩⟩
523-
524511
/-- A subgroup of a group inherits an integer power -/
525-
@[to_additive existing]
512+
@[to_additive /-- An `AddSubgroup` of an `AddGroup` inherits an integer scaling. -/]
526513
instance zpow : Pow H ℤ :=
527514
fun a n => ⟨a ^ n, H.zpow_mem a.2 n⟩⟩
528515

Mathlib/Algebra/Group/Subgroup/ZPowers/Basic.lean

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,9 @@ instance zpowers_isMulCommutative (g : G) : IsMulCommutative (zpowers g) :=
121121
theorem zpowers_le {g : G} {H : Subgroup G} : zpowers g ≤ H ↔ g ∈ H := by
122122
rw [zpowers_eq_closure, closure_le, Set.singleton_subset_iff, SetLike.mem_coe]
123123

124+
@[to_additive]
124125
alias ⟨_, zpowers_le_of_mem⟩ := zpowers_le
125126

126-
alias ⟨_, _root_.AddSubgroup.zmultiples_le_of_mem⟩ := AddSubgroup.zmultiples_le
127-
128-
attribute [to_additive existing] zpowers_le_of_mem
129-
130127
@[to_additive (attr := simp)]
131128
theorem zpowers_eq_bot {g : G} : zpowers g = ⊥ ↔ g = 1 := by rw [eq_bot_iff, zpowers_le, mem_bot]
132129

Mathlib/Algebra/Group/Submonoid/Defs.lean

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,19 +357,13 @@ end OneMemClass
357357

358358
variable {A : Type*} [MulOneClass M] [SetLike A M] [hA : SubmonoidClass A M] (S' : A)
359359

360-
/-- An `AddSubmonoid` of an `AddMonoid` inherits a scalar multiplication. -/
361-
instance AddSubmonoidClass.nSMul {M} [AddMonoid M] {A : Type*} [SetLike A M]
362-
[AddSubmonoidClass A M] (S : A) : SMul ℕ S :=
363-
fun n a => ⟨n • a.1, nsmul_mem a.2 n⟩⟩
364-
365360
namespace SubmonoidClass
366361

367362
/-- A submonoid of a monoid inherits a power operator. -/
368-
instance nPow {M} [Monoid M] {A : Type*} [SetLike A M] [SubmonoidClass A M] (S : A) : Pow S ℕ :=
363+
@[to_additive /-- An `AddSubmonoid` of an `AddMonoid` inherits a scalar multiplication. -/]
364+
instance instPow {M} [Monoid M] {A : Type*} [SetLike A M] [SubmonoidClass A M] (S : A) : Pow S ℕ :=
369365
fun a n => ⟨a.1 ^ n, pow_mem a.2 n⟩⟩
370366

371-
attribute [to_additive existing nSMul] nPow
372-
373367
@[to_additive (attr := simp, norm_cast)]
374368
theorem coe_pow {M} [Monoid M] {A : Type*} [SetLike A M] [SubmonoidClass A M] {S : A} (x : S)
375369
(n : ℕ) : ↑(x ^ n) = (x : M) ^ n :=

Mathlib/Algebra/Group/TransferInstance.lean

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,13 @@ lemma inv_def [Inv β] (x : α) :
6767
letI := e.Inv
6868
x⁻¹ = e.symm (e x)⁻¹ := rfl
6969

70-
variable (M) in
71-
/-- Transfer `SMul` across an `Equiv` -/
72-
@[to_additive /-- Transfer `VAdd` across an `Equiv` -/]
73-
protected abbrev smul [SMul M β] : SMul M α where smul r x := e.symm (r • e x)
74-
75-
@[to_additive]
76-
lemma smul_def [SMul M β] (r : M) (x : α) :
77-
letI := e.smul M
78-
r • x = e.symm (r • e x) := rfl
79-
8070
variable (M) in
8171
/-- Transfer `Pow` across an `Equiv` -/
82-
@[to_additive existing smul]
72+
@[to_additive (attr := to_additive /-- Transfer `VAdd` across an `Equiv` -/) smul
73+
/-- Transfer `SMul` across an `Equiv` -/]
8374
protected abbrev pow [Pow β M] : Pow α M where pow x n := e.symm (e x ^ n)
8475

85-
@[to_additive existing smul_def]
76+
@[to_additive (attr := to_additive) smul_def]
8677
lemma pow_def [Pow β M] (n : M) (x : α) :
8778
letI := e.pow M
8879
x ^ n = e.symm (e x ^ n) := rfl

Mathlib/Algebra/Group/ULift.lean

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,11 @@ instance inv [Inv α] : Inv (ULift α) :=
6060
theorem inv_down [Inv α] : x⁻¹.down = x.down⁻¹ :=
6161
rfl
6262

63-
@[to_additive]
64-
instance smul [SMul α β] : SMul α (ULift β) :=
65-
fun n x => up (n • x.down)⟩
66-
67-
@[to_additive (attr := simp)]
68-
theorem smul_down [SMul α β] (a : α) (b : ULift.{w} β) : (a • b).down = a • b.down :=
69-
rfl
70-
71-
@[to_additive existing smul]
63+
@[to_additive (attr := to_additive) smul]
7264
instance pow [Pow α β] : Pow (ULift α) β :=
7365
fun x n => up (x.down ^ n)⟩
7466

75-
@[to_additive existing (attr := simp) smul_down]
67+
@[to_additive (attr := to_additive, simp) smul_down]
7668
theorem pow_down [Pow α β] (a : ULift.{w} α) (b : β) : (a ^ b).down = a.down ^ b :=
7769
rfl
7870

Mathlib/Algebra/Notation/Defs.lean

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ class VSub (G : outParam Type*) (P : Type*) where
6464
type-dependent, but it is intended to be used for additive torsors. -/
6565
vsub : P → P → G
6666

67-
attribute [to_additive] SMul
67+
attribute [to_additive existing] SMul HSMul
68+
attribute [to_additive (attr := default_instance)] instHSMul
69+
6870
attribute [ext] SMul VAdd
6971

7072
@[inherit_doc] infixr:65 " +ᵥ " => HVAdd.hVAdd
@@ -73,19 +75,8 @@ attribute [ext] SMul VAdd
7375
recommended_spelling "vadd" for "+ᵥ" in [HVAdd.hVAdd, «term_+ᵥ_»]
7476
recommended_spelling "vsub" for "-ᵥ" in [VSub.vsub, «term_-ᵥ_»]
7577

76-
attribute [to_additive existing] Mul Div HMul instHMul HDiv instHDiv HSMul
77-
attribute [to_additive (reorder := 1 2) SMul] Pow
78-
attribute [to_additive (reorder := 1 2)] HPow
79-
attribute [to_additive existing (reorder := 1 2, 5 6) hSMul] HPow.hPow
80-
attribute [to_additive existing (reorder := 1 2, 4 5) smul] Pow.pow
81-
82-
attribute [to_additive (attr := default_instance)] instHSMul
83-
attribute [to_additive existing] instHPow
84-
8578
variable {G : Type*}
8679

87-
attribute [to_additive, notation_class] Inv
88-
8980
section Star
9081

9182
/-- Notation typeclass (with no default notation!) for an algebraic structure with a star operation.

0 commit comments

Comments
 (0)