Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - refactor: add an ofNat() elaborator #20169

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Mathlib/Algebra/CharP/Defs.lean
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,12 @@ lemma natCast_eq_natCast' (h : a ≡ b [MOD p]) : (a : R) = b := by

@[simp] lemma cast_eq_zero : (p : R) = 0 := (cast_eq_zero_iff R p p).2 dvd_rfl

-- See note [no_index around OfNat.ofNat]
--
-- TODO: This lemma needs to be `@[simp]` for confluence in the presence of `CharP.cast_eq_zero` and
-- `Nat.cast_ofNat`, but with `no_index` on its entire LHS, it matches literally every expression so
-- is too expensive. If https://github.com/leanprover/lean4/issues/2867 is fixed in a performant way, this can be made `@[simp]`.
--
-- @[simp]
lemma ofNat_eq_zero [p.AtLeastTwo] : no_index (OfNat.ofNat p : R) = 0 := cast_eq_zero R p
lemma ofNat_eq_zero [p.AtLeastTwo] : ofNat(p) = 0 := cast_eq_zero R p

lemma natCast_eq_natCast_mod (a : ℕ) : (a : R) = a % p :=
natCast_eq_natCast' R p (Nat.mod_modEq a p).symm
Expand Down
3 changes: 1 addition & 2 deletions Mathlib/Algebra/DirectSum/Ring.lean
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,8 @@ instance : NatCast (A 0) :=
theorem of_natCast (n : ℕ) : of A 0 n = n :=
rfl

-- See note [no_index around OfNat.ofNat]
@[simp]
theorem of_zero_ofNat (n : ℕ) [n.AtLeastTwo] : of A 0 (no_index (OfNat.ofNat n)) = OfNat.ofNat n :=
theorem of_zero_ofNat (n : ℕ) [n.AtLeastTwo] : of A 0 ofNat(n) = ofNat(n) :=
of_natCast A n

/-- The `Semiring` structure derived from `GSemiring A`. -/
Expand Down
4 changes: 2 additions & 2 deletions Mathlib/Algebra/Group/Defs.lean
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Mathlib.Algebra.Group.ZeroOne
import Mathlib.Algebra.Group.Operations
import Mathlib.Logic.Function.Defs
import Mathlib.Tactic.Simps.Basic
import Mathlib.Tactic.OfNat
import Batteries.Logic

/-!
Expand Down Expand Up @@ -902,9 +903,8 @@ theorem zpow_natCast (a : G) : ∀ n : ℕ, a ^ (n : ℤ) = a ^ n
@[deprecated (since := "2024-03-20")] alias zpow_coe_nat := zpow_natCast
@[deprecated (since := "2024-03-20")] alias coe_nat_zsmul := natCast_zsmul

-- See note [no_index around OfNat.ofNat]
@[to_additive ofNat_zsmul]
lemma zpow_ofNat (a : G) (n : ℕ) : a ^ (no_index (OfNat.ofNat n) : ℤ) = a ^ OfNat.ofNat n :=
lemma zpow_ofNat (a : G) (n : ℕ) : a ^ (ofNat(n) : ℤ) = a ^ OfNat.ofNat n :=
zpow_natCast ..

theorem zpow_negSucc (a : G) (n : ℕ) : a ^ (Int.negSucc n) = (a ^ (n + 1))⁻¹ := by
Expand Down
3 changes: 1 addition & 2 deletions Mathlib/Algebra/Group/Hom/End.lean
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ instance instAddMonoidWithOne (M) [AddCommMonoid M] : AddMonoidWithOne (AddMonoi
@[simp]
lemma natCast_apply [AddCommMonoid M] (n : ℕ) (m : M) : (↑n : AddMonoid.End M) m = n • m := rfl

-- See note [no_index around OfNat.ofNat]
@[simp] lemma ofNat_apply [AddCommMonoid M] (n : ℕ) [n.AtLeastTwo] (m : M) :
(no_index (OfNat.ofNat n : AddMonoid.End M)) m = n • m := rfl
(ofNat(n) : AddMonoid.End M) m = n • m := rfl

instance instSemiring [AddCommMonoid M] : Semiring (AddMonoid.End M) :=
{ AddMonoid.End.monoid M, AddMonoidHom.addCommMonoid, AddMonoid.End.instAddMonoidWithOne M with
Expand Down
6 changes: 2 additions & 4 deletions Mathlib/Algebra/Group/Opposite.lean
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,9 @@ end DivInvMonoid
theorem op_natCast [NatCast α] (n : ℕ) : op (n : α) = n :=
rfl

-- See note [no_index around OfNat.ofNat]
@[to_additive (attr := simp)]
theorem op_ofNat [NatCast α] (n : ℕ) [n.AtLeastTwo] :
op (no_index (OfNat.ofNat n : α)) = OfNat.ofNat n :=
op (ofNat(n) : α) = ofNat(n) :=
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, this is one example of fixing a theorem statement, right (as a no_index is also put on the RHS), right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably this doesn't change the statement, only the indexing of the statement. The corrections I was referring to in the description are the ones where a missing OfNat.ofNat is added.

rfl

@[to_additive (attr := simp, norm_cast)]
Expand All @@ -222,10 +221,9 @@ theorem op_intCast [IntCast α] (n : ℤ) : op (n : α) = n :=
theorem unop_natCast [NatCast α] (n : ℕ) : unop (n : αᵐᵒᵖ) = n :=
rfl

-- See note [no_index around OfNat.ofNat]
@[to_additive (attr := simp)]
theorem unop_ofNat [NatCast α] (n : ℕ) [n.AtLeastTwo] :
unop (no_index (OfNat.ofNat n : αᵐᵒᵖ)) = OfNat.ofNat n :=
unop (ofNat(n) : αᵐᵒᵖ) = ofNat(n) :=
rfl

@[to_additive (attr := simp, norm_cast)]
Expand Down
6 changes: 2 additions & 4 deletions Mathlib/Algebra/Group/ULift.lean
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,9 @@ instance instIntCast [IntCast α] : IntCast (ULift α) := ⟨(up ·)⟩
theorem up_natCast [NatCast α] (n : ℕ) : up (n : α) = n :=
rfl

-- See note [no_index around OfNat.ofNat]
@[simp]
theorem up_ofNat [NatCast α] (n : ℕ) [n.AtLeastTwo] :
up (no_index (OfNat.ofNat n : α)) = OfNat.ofNat n :=
up (ofNat(n) : α) = ofNat(n) :=
rfl

@[simp, norm_cast]
Expand All @@ -126,10 +125,9 @@ theorem up_intCast [IntCast α] (n : ℤ) : up (n : α) = n :=
theorem down_natCast [NatCast α] (n : ℕ) : down (n : ULift α) = n :=
rfl

-- See note [no_index around OfNat.ofNat]
@[simp]
theorem down_ofNat [NatCast α] (n : ℕ) [n.AtLeastTwo] :
down (no_index (OfNat.ofNat n : ULift α)) = OfNat.ofNat n :=
down (ofNat(n) : ULift α) = ofNat(n) :=
rfl

@[simp, norm_cast]
Expand Down
3 changes: 1 addition & 2 deletions Mathlib/Algebra/Module/NatInt.lean
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ lemma Nat.cast_smul_eq_nsmul (n : ℕ) (b : M) : (n : R) • b = n • b := by
| succ n ih => rw [Nat.cast_succ, add_smul, add_smul, one_smul, ih, one_smul]

/-- `nsmul` is equal to any other module structure via a cast. -/
-- See note [no_index around OfNat.ofNat]
lemma ofNat_smul_eq_nsmul (n : ℕ) [n.AtLeastTwo] (b : M) :
(no_index (OfNat.ofNat n) : R) • b = OfNat.ofNat n • b := Nat.cast_smul_eq_nsmul ..
(ofNat(n) : R) • b = OfNat.ofNat n • b := Nat.cast_smul_eq_nsmul ..

/-- `nsmul` is equal to any other module structure via a cast. -/
@[deprecated Nat.cast_smul_eq_nsmul (since := "2024-07-23")]
Expand Down
15 changes: 5 additions & 10 deletions Mathlib/Algebra/MvPolynomial/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -922,9 +922,8 @@ theorem eval₂_one : (1 : MvPolynomial σ R).eval₂ f g = 1 :=
@[simp] theorem eval₂_natCast (n : Nat) : (n : MvPolynomial σ R).eval₂ f g = n :=
(eval₂_C _ _ _).trans (map_natCast f n)

-- See note [no_index around OfNat.ofNat]
@[simp] theorem eval₂_ofNat (n : Nat) [n.AtLeastTwo] :
(no_index (OfNat.ofNat n) : MvPolynomial σ R).eval₂ f g = OfNat.ofNat n :=
(ofNat(n) : MvPolynomial σ R).eval₂ f g = ofNat(n) :=
eval₂_natCast f g n

@[simp]
Expand Down Expand Up @@ -1077,9 +1076,8 @@ theorem eval_C : ∀ a, eval f (C a) = a :=
theorem eval_X : ∀ n, eval f (X n) = f n :=
eval₂_X _ _

-- See note [no_index around OfNat.ofNat]
@[simp] theorem eval_ofNat (n : Nat) [n.AtLeastTwo] :
(no_index (OfNat.ofNat n) : MvPolynomial σ R).eval f = OfNat.ofNat n :=
(ofNat(n) : MvPolynomial σ R).eval f = ofNat(n) :=
map_ofNat _ n

@[simp]
Expand Down Expand Up @@ -1142,9 +1140,8 @@ theorem map_monomial (s : σ →₀ ℕ) (a : R) : map f (monomial s a) = monomi
theorem map_C : ∀ a : R, map f (C a : MvPolynomial σ R) = C (f a) :=
map_monomial _ _

-- See note [no_index around OfNat.ofNat]
@[simp] protected theorem map_ofNat (n : Nat) [n.AtLeastTwo] :
(no_index (OfNat.ofNat n) : MvPolynomial σ R).map f = OfNat.ofNat n :=
(ofNat(n) : MvPolynomial σ R).map f = ofNat(n) :=
_root_.map_ofNat _ _

@[simp]
Expand Down Expand Up @@ -1362,9 +1359,8 @@ theorem aeval_X (s : σ) : aeval f (X s : MvPolynomial _ R) = f s :=
theorem aeval_C (r : R) : aeval f (C r) = algebraMap R S₁ r :=
eval₂_C _ _ _

-- See note [no_index around OfNat.ofNat]
@[simp] theorem aeval_ofNat (n : Nat) [n.AtLeastTwo] :
aeval f (no_index (OfNat.ofNat n) : MvPolynomial σ R) = OfNat.ofNat n :=
aeval f (ofNat(n) : MvPolynomial σ R) = ofNat(n) :=
map_ofNat _ _

theorem aeval_unique (φ : MvPolynomial σ R →ₐ[R] S₁) : φ = aeval (φ ∘ X) := by
Expand Down Expand Up @@ -1498,10 +1494,9 @@ theorem aevalTower_X (i : σ) : aevalTower g y (X i) = y i :=
theorem aevalTower_C (x : R) : aevalTower g y (C x) = g x :=
eval₂_C _ _ _

-- See note [no_index around OfNat.ofNat]
@[simp]
theorem aevalTower_ofNat (n : Nat) [n.AtLeastTwo] :
aevalTower g y (no_index (OfNat.ofNat n) : MvPolynomial σ R) = OfNat.ofNat n :=
aevalTower g y (ofNat(n) : MvPolynomial σ R) = ofNat(n) :=
_root_.map_ofNat _ _

@[simp]
Expand Down
Loading
Loading