Skip to content

Commit

Permalink
chore(Data/Nat): Use Std lemmas (#11661)
Browse files Browse the repository at this point in the history
Move basic `Nat` lemmas from `Data.Nat.Order.Basic` and `Data.Nat.Pow` to `Data.Nat.Defs`. Most proofs need adapting, but that's easily solved by replacing the general mathlib lemmas by the new Std `Nat`-specific lemmas and using `omega`.

## Other changes

* Move the last few lemmas left in `Data.Nat.Pow` to `Algebra.GroupPower.Order`
* Move the deprecated aliases from `Data.Nat.Pow` to `Algebra.GroupPower.Order`
* Move the `bit`/`bit0`/`bit1` lemmas from `Data.Nat.Order.Basic` to `Data.Nat.Bits`
* Fix some fallout from not importing `Data.Nat.Order.Basic` anymore
* Add a few `Nat`-specific lemmas to help fix the fallout (look for `nolint simpNF`)
* Turn `Nat.mul_self_le_mul_self_iff` and `Nat.mul_self_lt_mul_self_iff` around (they were misnamed)
* Make more arguments to `Nat.one_lt_pow` implicit
  • Loading branch information
YaelDillies committed Mar 26, 2024
1 parent 7151d90 commit 5bc68d9
Show file tree
Hide file tree
Showing 39 changed files with 969 additions and 1,051 deletions.
2 changes: 1 addition & 1 deletion Archive/Imo/Imo2008Q3.lean
Expand Up @@ -82,6 +82,6 @@ theorem imo2008_q3 : ∀ N : ℕ, ∃ n : ℕ, n ≥ N ∧
obtain ⟨n, hnat, hreal⟩ := p_lemma p hpp hpmod4 (by linarith [hineq₁, Nat.zero_le (N ^ 2)])
have hineq₂ : n ^ 2 + 1 ≥ p := Nat.le_of_dvd (n ^ 2).succ_pos hnat
have hineq₃ : n * n ≥ N * N := by linarith [hineq₁, hineq₂]
have hn_ge_N : n ≥ N := Nat.mul_self_le_mul_self_iff.mpr hineq₃
have hn_ge_N : n ≥ N := Nat.mul_self_le_mul_self_iff.1 hineq₃
exact ⟨n, hn_ge_N, p, hpp, hnat, hreal⟩
#align imo2008_q3 imo2008_q3
1 change: 0 additions & 1 deletion Archive/MiuLanguage/DecisionSuf.lean
Expand Up @@ -4,7 +4,6 @@ Released under Apache 2.0 license as described in the file LICENSE.
Authors: Gihan Marasingha
-/
import Archive.MiuLanguage.DecisionNec
import Mathlib.Data.Nat.Pow
import Mathlib.Tactic.Linarith

#align_import miu_language.decision_suf from "leanprover-community/mathlib"@"f694c7dead66f5d4c80f446c796a5aad14707f0e"
Expand Down
1 change: 0 additions & 1 deletion Mathlib.lean
Expand Up @@ -1942,7 +1942,6 @@ import Mathlib.Data.Nat.Pairing
import Mathlib.Data.Nat.Parity
import Mathlib.Data.Nat.PartENat
import Mathlib.Data.Nat.Periodic
import Mathlib.Data.Nat.Pow
import Mathlib.Data.Nat.Prime
import Mathlib.Data.Nat.PrimeFin
import Mathlib.Data.Nat.PrimeNormNum
Expand Down
5 changes: 4 additions & 1 deletion Mathlib/Algebra/BigOperators/Multiset/Basic.lean
Expand Up @@ -3,7 +3,10 @@ Copyright (c) 2015 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Data.List.BigOperators.Basic
import Mathlib.Algebra.GroupPower.Hom
import Mathlib.Algebra.GroupWithZero.Divisibility
import Mathlib.Algebra.Order.Monoid.OrderDual
import Mathlib.Algebra.Ring.Divisibility.Basic
import Mathlib.Data.Multiset.Basic

#align_import algebra.big_operators.multiset.basic from "leanprover-community/mathlib"@"6c5f73fd6f6cc83122788a80a27cdd54663609f4"
Expand Down
1 change: 0 additions & 1 deletion Mathlib/Algebra/ContinuedFractions/TerminatedStable.lean
Expand Up @@ -4,7 +4,6 @@ Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kevin Kappelmann
-/
import Mathlib.Algebra.ContinuedFractions.Translations
import Mathlib.Data.Nat.Order.Basic

#align_import algebra.continued_fractions.terminated_stable from "leanprover-community/mathlib"@"a7e36e48519ab281320c4d192da6a7b348ce40ad"

Expand Down
1 change: 0 additions & 1 deletion Mathlib/Algebra/EuclideanDomain/Instances.lean
Expand Up @@ -7,7 +7,6 @@ import Mathlib.Algebra.EuclideanDomain.Defs
import Mathlib.Algebra.Field.Defs
import Mathlib.Algebra.GroupWithZero.Units.Basic
import Mathlib.Data.Int.Order.Basic
import Mathlib.Data.Nat.Order.Basic

#align_import algebra.euclidean_domain.instances from "leanprover-community/mathlib"@"e1bccd6e40ae78370f01659715d3c948716e3b7e"

Expand Down
28 changes: 26 additions & 2 deletions Mathlib/Algebra/GroupPower/Order.lean
Expand Up @@ -5,6 +5,7 @@ Authors: Jeremy Avigad, Robert Y. Lewis
-/
import Mathlib.Algebra.GroupPower.CovariantClass
import Mathlib.Algebra.GroupPower.Ring
import Mathlib.Algebra.Order.Ring.Canonical

#align_import algebra.group_power.order from "leanprover-community/mathlib"@"00f91228655eecdcd3ac97a7fd8dbcb139fe990a"

Expand All @@ -15,6 +16,8 @@ Note that some lemmas are in `Algebra/GroupPower/Lemmas.lean` as they import fil
depend on this file.
-/

assert_not_exists Set.range

open Function Int

variable {α M R : Type*}
Expand Down Expand Up @@ -209,7 +212,7 @@ theorem pow_le_pow_right (ha : 1 ≤ a) (h : n ≤ m) : a ^ n ≤ a ^ m := pow_r
#align pow_le_pow pow_le_pow_right

theorem le_self_pow (ha : 1 ≤ a) (h : m ≠ 0) : a ≤ a ^ m := by
simpa only [pow_one] using pow_le_pow_right ha <| pos_iff_ne_zero.2 h
simpa only [pow_one] using pow_le_pow_right ha <| Nat.pos_iff_ne_zero.2 h
#align self_le_pow le_self_pow
#align le_self_pow le_self_pow

Expand Down Expand Up @@ -261,6 +264,7 @@ lemma pow_right_strictMono (h : 1 < a) : StrictMono (a ^ ·) :=
@[gcongr]
theorem pow_lt_pow_right (h : 1 < a) (hmn : m < n) : a ^ m < a ^ n := pow_right_strictMono h hmn
#align pow_lt_pow_right pow_lt_pow_right
#align nat.pow_lt_pow_of_lt_right pow_lt_pow_right

lemma pow_lt_pow_iff_right (h : 1 < a) : a ^ n < a ^ m ↔ n < m := (pow_right_strictMono h).lt_iff_lt
#align pow_lt_pow_iff_ pow_lt_pow_iff_right
Expand Down Expand Up @@ -432,7 +436,7 @@ protected lemma Even.add_pow_le (hn : ∃ k, 2 * k = n) :
rw [Commute.mul_pow]; simp [Commute, SemiconjBy, two_mul, mul_two]
_ ≤ 2 ^ n * (2 ^ (n - 1) * ((a ^ 2) ^ n + (b ^ 2) ^ n)) := mul_le_mul_of_nonneg_left
(add_pow_le (sq_nonneg _) (sq_nonneg _) _) $ pow_nonneg (zero_le_two (α := R)) _
_ = _ := by simp only [← mul_assoc, ← pow_add, ← pow_mul]; cases n; rfl; simp [two_mul]
_ = _ := by simp only [← mul_assoc, ← pow_add, ← pow_mul]; cases n; rfl; simp [Nat.two_mul]

end LinearOrderedSemiring

Expand Down Expand Up @@ -524,6 +528,20 @@ theorem map_sub_swap (x y : R) : f (x - y) = f (y - x) := by rw [← map_neg, ne

end MonoidHom

namespace Nat
variable {n : ℕ} {f : α → ℕ}

/-- See also `pow_left_strictMonoOn`. -/
protected lemma pow_left_strictMono (hn : n ≠ 0) : StrictMono (. ^ n : ℕ → ℕ) :=
fun _ _ h ↦ Nat.pow_lt_pow_left h hn
#align nat.pow_left_strict_mono Nat.pow_left_strictMono

lemma _root_.StrictMono.nat_pow [Preorder α] (hn : n ≠ 0) (hf : StrictMono f) :
StrictMono (f · ^ n) := (Nat.pow_left_strictMono hn).comp hf
#align strict_mono.nat_pow StrictMono.nat_pow

end Nat

/-!
### Deprecated lemmas
Expand All @@ -546,3 +564,9 @@ Those lemmas have been deprecated on 2023-12-23.
@[deprecated] alias lt_of_pow_lt_pow := lt_of_pow_lt_pow_left
@[deprecated] alias le_of_pow_le_pow := le_of_pow_le_pow_left
@[deprecated] alias self_le_pow := le_self_pow
@[deprecated] alias Nat.pow_lt_pow_of_lt_left := Nat.pow_lt_pow_left
@[deprecated] alias Nat.pow_le_iff_le_left := Nat.pow_le_pow_iff_left
@[deprecated] alias Nat.pow_lt_pow_of_lt_right := pow_lt_pow_right
@[deprecated] protected alias Nat.pow_right_strictMono := pow_right_strictMono
@[deprecated] alias Nat.pow_le_iff_le_right := pow_le_pow_iff_right
@[deprecated] alias Nat.pow_lt_iff_lt_right := pow_lt_pow_iff_right
1 change: 0 additions & 1 deletion Mathlib/Algebra/GroupPower/Ring.lean
Expand Up @@ -8,7 +8,6 @@ import Mathlib.Algebra.GroupWithZero.Commute
import Mathlib.Algebra.GroupWithZero.Divisibility
import Mathlib.Algebra.Ring.Commute
import Mathlib.Algebra.Ring.Divisibility.Basic
import Mathlib.Data.Nat.Order.Basic

#align_import algebra.group_power.ring from "leanprover-community/mathlib"@"fc2ed6f838ce7c9b7c7171e58d78eaf7b438fb0e"

Expand Down
1 change: 0 additions & 1 deletion Mathlib/Algebra/Order/Floor/Div.lean
Expand Up @@ -6,7 +6,6 @@ Authors: Yaël Dillies
import Mathlib.Algebra.Order.Module.Defs
import Mathlib.Algebra.Order.Pi
import Mathlib.Data.Finsupp.Order
import Mathlib.Data.Nat.Order.Basic
import Mathlib.Order.GaloisConnection

/-!
Expand Down
4 changes: 2 additions & 2 deletions Mathlib/Combinatorics/Additive/SalemSpencer.lean
Expand Up @@ -338,8 +338,8 @@ theorem mulRothNumber_le : mulRothNumber s ≤ s.card := Nat.findGreatest_le s.c
@[to_additive]
theorem mulRothNumber_spec :
∃ (t : _) (_ : t ⊆ s), t.card = mulRothNumber s ∧ MulSalemSpencer (t : Set α) :=
@Nat.findGreatest_spec _ _
(fun m => ∃ (t : _) (_ : t ⊆ s), t.card = m ∧ MulSalemSpencer (t : Set α)) _ (Nat.zero_le _)
Nat.findGreatest_spec
(P := fun m => ∃ (t : _) (_ : t ⊆ s), t.card = m ∧ MulSalemSpencer (t : Set α)) (Nat.zero_le _)
⟨∅, empty_subset _, card_empty, by norm_cast; exact mulSalemSpencer_empty⟩
#align mul_roth_number_spec mulRothNumber_spec
#align add_roth_number_spec addRothNumber_spec
Expand Down
2 changes: 0 additions & 2 deletions Mathlib/Computability/Ackermann.lean
Expand Up @@ -6,8 +6,6 @@ Authors: Violeta Hernández Palacios
import Mathlib.Computability.Primrec
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
import Mathlib.Algebra.GroupPower.Order
import Mathlib.Data.Nat.Pow

#align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383"

Expand Down
2 changes: 1 addition & 1 deletion Mathlib/Data/BitVec/Defs.lean
Expand Up @@ -3,9 +3,9 @@ Copyright (c) 2015 Joe Hendrix. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Joe Hendrix, Sebastian Ullrich, Harun Khan, Alex Keizer, Abdalrhman M Mohamed
-/

import Mathlib.Data.Fin.Basic
import Mathlib.Data.ZMod.Defs
import Mathlib.Init.Data.Nat.Bitwise
import Std.Data.BitVec

#align_import data.bitvec.core from "leanprover-community/mathlib"@"1126441d6bccf98c81214a0780c73d499f6721fe"
Expand Down
1 change: 1 addition & 0 deletions Mathlib/Data/Fintype/Basic.lean
Expand Up @@ -3,6 +3,7 @@ Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Mathlib.Algebra.Ring.Hom.Defs -- FIXME: This import is bogus
import Mathlib.Data.Finset.Image
import Mathlib.Data.Fin.OrderHom

Expand Down
10 changes: 5 additions & 5 deletions Mathlib/Data/Int/Bitwise.lean
Expand Up @@ -411,20 +411,20 @@ theorem shiftLeft_add : ∀ (m : ℤ) (n : ℕ) (k : ℤ), m <<< (n + k) = (m <<
| (m : ℕ), n, -[k+1] =>
subNatNat_elim n k.succ (fun n k i => (↑m) <<< i = (Nat.shiftLeft' false m n) >>> k)
(fun (i n : ℕ) =>
by dsimp; simp [← Nat.shiftLeft_sub _ , add_tsub_cancel_left])
by dsimp; simp [← Nat.shiftLeft_sub _ , Nat.add_sub_cancel_left])
fun i n => by
dsimp
simp only [Nat.shiftLeft'_false, Nat.shiftRight_add, le_refl, ← Nat.shiftLeft_sub,
tsub_eq_zero_of_le, Nat.shiftLeft_zero]
Nat.sub_eq_zero_of_le, Nat.shiftLeft_zero]
rfl
| -[m+1], n, -[k+1] =>
subNatNat_elim n k.succ
(fun n k i => -[m+1] <<< i = -[(Nat.shiftLeft' true m n) >>> k+1])
(fun i n =>
congr_arg negSucc <| by
rw [← Nat.shiftLeft'_sub, add_tsub_cancel_left]; apply Nat.le_add_right)
rw [← Nat.shiftLeft'_sub, Nat.add_sub_cancel_left]; apply Nat.le_add_right)
fun i n =>
congr_arg negSucc <| by rw [add_assoc, Nat.shiftRight_add, ← Nat.shiftLeft'_sub, tsub_self]
congr_arg negSucc <| by rw [add_assoc, Nat.shiftRight_add, ← Nat.shiftLeft'_sub, Nat.sub_self]
<;> rfl
#align int.shiftl_add Int.shiftLeft_add

Expand All @@ -441,7 +441,7 @@ theorem shiftRight_eq_div_pow : ∀ (m : ℤ) (n : ℕ), m >>> (n : ℤ) = m / (
| (m : ℕ), n => by rw [shiftRight_coe_nat, Nat.shiftRight_eq_div_pow _ _]; simp
| -[m+1], n => by
rw [shiftRight_negSucc, negSucc_ediv, Nat.shiftRight_eq_div_pow]; rfl
exact ofNat_lt_ofNat_of_lt (pow_pos (by decide) _)
exact ofNat_lt_ofNat_of_lt (Nat.pow_pos (by decide))
#align int.shiftr_eq_div_pow Int.shiftRight_eq_div_pow

theorem one_shiftLeft (n : ℕ) : 1 <<< (n : ℤ) = (2 ^ n : ℕ) :=
Expand Down
2 changes: 1 addition & 1 deletion Mathlib/Data/Int/Dvd/Pow.lean
Expand Up @@ -33,7 +33,7 @@ theorem pow_dvd_of_le_of_pow_dvd {p m n : ℕ} {k : ℤ} (hmn : m ≤ n) (hdiv :
#align int.pow_dvd_of_le_of_pow_dvd Int.pow_dvd_of_le_of_pow_dvd

theorem dvd_of_pow_dvd {p k : ℕ} {m : ℤ} (hk : 1 ≤ k) (hpk : ↑(p ^ k) ∣ m) : ↑p ∣ m :=
(dvd_pow_self _ <| pos_iff_ne_zero.1 hk).natCast.trans hpk
(dvd_pow_self _ <| Nat.ne_of_gt hk).natCast.trans hpk
#align int.dvd_of_pow_dvd Int.dvd_of_pow_dvd

end Int
1 change: 0 additions & 1 deletion Mathlib/Data/Int/Order/Lemmas.lean
Expand Up @@ -4,7 +4,6 @@ Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad
-/
import Mathlib.Algebra.Order.Ring.Abs
import Mathlib.Data.Nat.Pow

#align_import data.int.order.lemmas from "leanprover-community/mathlib"@"fc2ed6f838ce7c9b7c7171e58d78eaf7b438fb0e"

Expand Down
12 changes: 5 additions & 7 deletions Mathlib/Data/List/Perm.lean
Expand Up @@ -615,9 +615,8 @@ theorem Perm.drop_inter {α} [DecidableEq α] {xs ys : List α} (n : ℕ) (h : x
xs.drop n ~ ys.inter (xs.drop n) := by
by_cases h'' : n ≤ xs.length
· let n' := xs.length - n
have h₀ : n = xs.length - n' := by
rwa [tsub_tsub_cancel_of_le]
have h₁ : n' ≤ xs.length := by apply tsub_le_self
have h₀ : n = xs.length - n' := by rwa [Nat.sub_sub_self]
have h₁ : n' ≤ xs.length := Nat.sub_le ..
have h₂ : xs.drop n = (xs.reverse.take n').reverse := by
rw [reverse_take _ h₁, h₀, reverse_reverse]
rw [h₂]
Expand All @@ -627,7 +626,7 @@ theorem Perm.drop_inter {α} [DecidableEq α] {xs ys : List α} (n : ℕ) (h : x
apply (reverse_perm _).trans; assumption
· have : drop n xs = [] := by
apply eq_nil_of_length_eq_zero
rw [length_drop, tsub_eq_zero_iff_le]
rw [length_drop, Nat.sub_eq_zero_iff_le]
apply le_of_not_ge h''
simp [this, List.inter]
#align list.perm.drop_inter List.Perm.drop_inter
Expand Down Expand Up @@ -802,7 +801,7 @@ theorem nthLe_permutations'Aux (s : List α) (x : α) (n : ℕ)
(hn : n < length (permutations'Aux x s)) :
(permutations'Aux x s).nthLe n hn = s.insertNth n x := by
induction' s with y s IH generalizing n
· simp only [length, zero_add, lt_one_iff] at hn
· simp only [length, zero_add, Nat.lt_one_iff] at hn
simp [hn]
· cases n
· simp [nthLe]
Expand Down Expand Up @@ -864,8 +863,7 @@ theorem nodup_permutations'Aux_iff {s : List α} {x : α} : Nodup (permutations'
intro H
obtain ⟨k, hk, hk'⟩ := nthLe_of_mem H
rw [nodup_iff_nthLe_inj] at h
suffices k = k + 1 by simp at this
refine' h k (k + 1) _ _ _
refine k.succ_ne_self.symm $ h k (k + 1) ?_ ?_ ?_
· simpa [Nat.lt_succ_iff] using hk.le
· simpa using hk
rw [nthLe_permutations'Aux, nthLe_permutations'Aux]
Expand Down
23 changes: 18 additions & 5 deletions Mathlib/Data/Nat/Basic.lean
Expand Up @@ -10,12 +10,25 @@ import Mathlib.Algebra.GroupWithZero.Defs
#align_import data.nat.basic from "leanprover-community/mathlib"@"bd835ef554f37ef9b804f0903089211f89cb370b"

/-!
# Basic instances for the natural numbers
# Algebraic instances for the natural numbers
This file contains:
* Algebraic instances on the natural numbers
* Basic lemmas about natural numbers that require more imports than available in
`Data.Nat.Defs`.
This file contains algebraic instances on the natural numbers and a few lemmas about them.
## Implementation note
Std has a home-baked development of the algebraic and order theoretic theory of `ℕ` which, in
particular, is not typeclass-mediated. This is useful to set up the algebra and finiteness libraries
in mathlib (naturals show up as indices in lists, cardinality in finsets, powers in groups, ...).
This home-baked development is pursued in `Data.Nat.Defs`.
Less basic uses of `ℕ` should however use the typeclass-mediated development. This file is the one
giving access to the algebraic instances. `Data.Nat.Order.Basic` is the one giving access to the
algebraic order instances.
## TODO
The names of this file, `Data.Nat.Defs` and `Data.Nat.Order.Basic` are archaic and don't match up
with reality anymore. Rename them.
-/

open Multiplicative
Expand Down

0 comments on commit 5bc68d9

Please sign in to comment.