The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V02. Numbers

Mathlib.Data.Nat.Init

68 declarations. Mathlib docs · Source

Basic operations on the natural numbers

This file contains:

  • some basic lemmas about natural numbers
  • extra recursors:
    • leRecOn, le_induction: recursion and induction principles starting at non-zero numbers
    • decreasing_induction: recursion growing downwards
    • le_rec_on', decreasing_induction': versions with slightly weaker assumptions
    • strong_rec': recursion based on strong inequalities
  • decidability instances on predicates about the natural numbers

This file should not depend on anything defined in Mathlib (except for notation), so that it can be upstreamed to Batteries or the Lean standard library easily.

See note [foundational algebra order theory].

succ, pred

add

sub

mul

div

pow

Recursion and induction principles

This section is here due to dependencies -- the lemmas here require some of the lemmas proved above, and some of the results in later sections depend on the definitions in this section.

mod, dvd

Decidability of predicates

Nat.AtLeastTwo

theorem Nat.leRec.match_1.congr_eq_2 E1 #

∀ {n : ℕ} (motive : (x : ℕ) → n ≤ x → Sort u_1) (x : ℕ) (x_1 : n ≤ x) (h_1 : (H : n ≤ 0) → motive 0 H)
  (h_2 : (m : ℕ) → (H : n ≤ m + 1) → motive m.succ H) (m : ℕ) (H : n ≤ m + 1),
  x = m.succ →
    x_1 ≍ H →
      (match x, x_1 with
        | 0, H => h_1 H
        | m.succ, H => h_2 m H) ≍
        h_2 m H

theorem Nat.pincerRecursion.match_1.congr_eq_2 E1 #

∀ (motive : ℕ → ℕ → Sort u_1) (x x_1 : ℕ) (h_1 : (m : ℕ) → motive m 0) (h_2 : (n : ℕ) → motive 0 n)
  (h_3 : (n n_1 : ℕ) → motive n.succ n_1.succ) (n : ℕ),
  x = 0 →
    x_1 = n →
      (n = 0 → False) →
        (match x, x_1 with
          | m, 0 => h_1 m
          | 0, n => h_2 n
          | n.succ, n_1.succ => h_3 n n_1) ≍
          h_2 n

theorem Nat.leRec.match_1.congr_eq_1 E1 #

∀ {n : ℕ} (motive : (x : ℕ) → n ≤ x → Sort u_1) (x : ℕ) (x_1 : n ≤ x) (h_1 : (H : n ≤ 0) → motive 0 H)
  (h_2 : (m : ℕ) → (H : n ≤ m + 1) → motive m.succ H) (H : n ≤ 0),
  x = 0 →
    x_1 ≍ H →
      (match x, x_1 with
        | 0, H => h_1 H
        | m.succ, H => h_2 m H) ≍
        h_1 H

theorem Or.by_cases.congr_simp E1 #

∀ {p q : Prop} {inst : Decidable p} [inst_1 : Decidable p] {α : Sort u} (h : p ∨ q) (h₁ h₁_1 : p → α),
  h₁ = h₁_1 → ∀ (h₂ h₂_1 : q → α), h₂ = h₂_1 → h.by_cases h₁ h₂ = h.by_cases h₁_1 h₂_1

theorem Eq.ndrec.congr_simp E1 #

∀ {α : Sort u2} {a : α} {motive : α → Sort u1} (m m_1 : motive a), m = m_1 → ∀ {b : α} (h : a = b), h ▸ m = h ▸ m_1

def LibraryNote.foundational_algebra_order_theory E1 #

Batteries.Util.LibraryNote

Batteries has a home-baked development of the algebraic and order-theoretic theory of ℕ and ℤ which, in particular, is not typeclass-mediated. This is useful to set up the algebra and finiteness libraries in mathlib (naturals and integers show up as indices/offsets in lists, cardinality in finsets, powers in groups, ...).

Less basic uses of ℕ and ℤ should however use the typeclass-mediated development.

The relevant files are:

  • Mathlib/Data/Nat/Basic.lean for the continuation of the home-baked development on ℕ
  • Mathlib/Data/Int/Init.lean for the continuation of the home-baked development on ℤ
  • Mathlib/Algebra/Group/Nat/Defs.lean for the monoid instances on ℕ
  • Mathlib/Algebra/Group/Int/Defs.lean for the group instance on ℤ
  • Mathlib/Algebra/Ring/Nat.lean for the semiring instance on ℕ
  • Mathlib/Algebra/Ring/Int/Defs.lean for the ring instance on ℤ
  • Mathlib/Algebra/Order/Group/Nat.lean for the ordered monoid instance on ℕ
  • Mathlib/Algebra/Order/Group/Int.lean for the ordered group instance on ℤ
  • Mathlib/Algebra/Order/Ring/Nat.lean for the ordered semiring instance on ℕ
  • Mathlib/Algebra/Order/Ring/Int.lean for the ordered ring instance on ℤ

theorem Nat.succ_pos' E1 #

∀ {n : ℕ}, 0 < n.succ

theorem LT.lt.nat_succ_le E1 #

∀ {n m : ℕ}, n < m → n.succ ≤ m

Alias of Nat.succ_le_of_lt.

theorem Nat.of_le_succ E1 #

∀ {m n : ℕ}, m ≤ n.succ → m ≤ n ∨ m = n.succ

Alias of the forward direction of Nat.le_succ_iff.

theorem Nat.two_lt_of_ne E1 #

∀ {n : ℕ}, n ≠ 0 → n ≠ 1 → n ≠ 2 → 2 < n

theorem Nat.two_le_iff E0 #

∀ (n : ℕ), 2 ≤ n ↔ n ≠ 0 ∧ n ≠ 1

theorem Nat.mul_def E0 #

∀ {m n : ℕ}, m.mul n = m * n

theorem Nat.two_mul_ne_two_mul_add_one E1 #

∀ {m n : ℕ}, 2 * n ≠ 2 * m + 1

theorem Nat.le_div_two_iff_mul_two_le E1 #

∀ {n m : ℕ}, m ≤ n / 2 ↔ ↑m * 2 ≤ ↑n

theorem Nat.div_lt_self' E1 #

∀ (a b : ℕ), (a + 1) / (b + 2) < a + 1

A version of Nat.div_lt_self using successors, rather than additional hypotheses.

theorem Nat.two_mul_odd_div_two E1 #

∀ {n : ℕ}, n % 2 = 1 → 2 * (n / 2) = n - 1

theorem Nat.one_le_pow' E1 #

∀ (n m : ℕ), 1 ≤ (m + 1) ^ n

theorem Nat.sq_sub_sq E1 #

∀ (a b : ℕ), a ^ 2 - b ^ 2 = (a + b) * (a - b)

Alias of Nat.pow_two_sub_pow_two.

theorem Nat.rec_zero E0 #

∀ {motive : ℕ → Sort u_1} (zero : motive 0) (succ : (n : ℕ) → motive n → motive (n + 1)), Nat.rec zero succ 0 = zero

theorem Nat.rec_add_one E1 #

∀ {motive : ℕ → Sort u_1} (zero : motive 0) (succ : (n : ℕ) → motive n → motive (n + 1)) (n : ℕ),
  Nat.rec zero succ (n + 1) = succ n (Nat.rec zero succ n)

theorem Nat.rec_one E0 #

∀ {motive : ℕ → Sort u_1} (zero : motive 0) (succ : (n : ℕ) → motive n → motive (n + 1)),
  Nat.rec zero succ 1 = succ 0 zero

def Nat.leRec E1 #

{n : ℕ} →
  {motive : (m : ℕ) → n ≤ m → Sort u_1} →
    motive n ⋯ → (⦃k : ℕ⦄ → (h : n ≤ k) → motive k h → motive (k + 1) ⋯) → {m : ℕ} → (h : n ≤ m) → motive m h

Recursion starting at a non-zero number: given a map motive k → motive (k + 1) for each k ≥ n, there is a map from motive n to each motive m, n ≤ m.

This is a version of Nat.le.rec that works for Sort u. Similarly to Nat.le.rec, it can be used as

induction hle using Nat.leRec with
| refl => sorry
| le_succ_of_le hle ih => sorry

theorem Nat.leRec_eq_leRec E1 #

@Nat.leRec = @Nat.le.rec

theorem Nat.leRec_self E0 #

∀ {n : ℕ} {motive : (m : ℕ) → n ≤ m → Sort u_1} (refl : motive n ⋯)
  (le_succ_of_le : ⦃k : ℕ⦄ → (h : n ≤ k) → motive k h → motive (k + 1) ⋯), Nat.leRec refl le_succ_of_le ⋯ = refl

theorem Nat.leRec_succ E0 #

∀ {m n : ℕ} {motive : (m : ℕ) → n ≤ m → Sort u_1} (refl : motive n ⋯)
  (le_succ_of_le : ⦃k : ℕ⦄ → (h : n ≤ k) → motive k h → motive (k + 1) ⋯) (h1 : n ≤ m) {h2 : n ≤ m + 1},
  Nat.leRec refl le_succ_of_le h2 = le_succ_of_le h1 (Nat.leRec refl le_succ_of_le h1)

theorem Nat.leRec_succ' E1 #

∀ {n : ℕ} {motive : (m : ℕ) → n ≤ m → Sort u_1} (refl : motive n ⋯)
  (le_succ_of_le : ⦃k : ℕ⦄ → (h : n ≤ k) → motive k h → motive (k + 1) ⋯),
  Nat.leRec refl le_succ_of_le ⋯ = le_succ_of_le ⋯ refl

theorem Nat.leRec_trans E1 #

∀ {n m k : ℕ} {motive : (m : ℕ) → n ≤ m → Sort u_1} (refl : motive n ⋯)
  (le_succ_of_le : ⦃k : ℕ⦄ → (h : n ≤ k) → motive k h → motive (k + 1) ⋯) (hnm : n ≤ m) (hmk : m ≤ k),
  Nat.leRec refl le_succ_of_le ⋯ =
    Nat.leRec (Nat.leRec refl (fun x h ↦ le_succ_of_le h) hnm) (fun x h ↦ le_succ_of_le ⋯) hmk

theorem Nat.leRec_succ_left E1 #

∀ {n : ℕ} {motive : (m : ℕ) → n ≤ m → Sort u_1} (refl : motive n ⋯)
  (le_succ_of_le : ⦃k : ℕ⦄ → (h : n ≤ k) → motive k h → motive (k + 1) ⋯) {m : ℕ} (h1 : n ≤ m) (h2 : n + 1 ≤ m),
  Nat.leRec (le_succ_of_le ⋯ refl) (fun x h ih ↦ le_succ_of_le ⋯ ih) h2 = Nat.leRec refl le_succ_of_le h1

def Nat.leRecOn E1 #

{motive : ℕ → Sort u_1} → {n m : ℕ} → n ≤ m → ({k : ℕ} → motive k → motive (k + 1)) → motive n → motive m

Recursion starting at a non-zero number: given a map motive k → motive (k + 1) for each k, there is a map from motive n to each motive m, n ≤ m. For a version where the assumption is only made when k ≥ n, see Nat.leRec.

theorem Nat.leRecOn_self E1 #

∀ {motive : ℕ → Sort u_1} {n : ℕ} {next : {k : ℕ} → motive k → motive (k + 1)} (base : motive n),
  Nat.leRecOn ⋯ next base = base

theorem Nat.leRecOn_succ E1 #

∀ {motive : ℕ → Sort u_1} {n m : ℕ} (hm : n ≤ m) {next : {k : ℕ} → motive k → motive (k + 1)} (base : motive n),
  Nat.leRecOn ⋯ next base = next (Nat.leRecOn hm next base)

theorem Nat.leRecOn_succ' E1 #

∀ {motive : ℕ → Sort u_1} {n : ℕ} {next : {k : ℕ} → motive k → motive (k + 1)} (base : motive n),
  Nat.leRecOn ⋯ next base = next base

theorem Nat.leRecOn_trans E1 #

∀ {motive : ℕ → Sort u_1} {n m k : ℕ} (hnm : n ≤ m) (hmk : m ≤ k) {next : {k : ℕ} → motive k → motive (k + 1)}
  (base : motive n), Nat.leRecOn ⋯ next base = Nat.leRecOn hmk next (Nat.leRecOn hnm next base)

theorem Nat.leRecOn_succ_left E1 #

∀ {motive : ℕ → Sort u_1} {n m : ℕ} {next : {k : ℕ} → motive k → motive (k + 1)} (base : motive n) (hm : n + 1 ≤ m),
  Nat.leRecOn hm next (next base) = Nat.leRecOn ⋯ next base

theorem Nat.le_induction E2 #

∀ {m : ℕ} {motive : (n : ℕ) → m ≤ n → Prop},
  motive m ⋯ → (∀ (n : ℕ) (hmn : m ≤ n), motive n hmn → motive (n + 1) ⋯) → ∀ (n : ℕ) (hmn : m ≤ n), motive n hmn

Induction principle starting at a non-zero number. To use in an induction proof, the syntax is induction n, hn using Nat.le_induction.

This is an alias of Nat.leRec, specialized to Prop.

def Nat.twoStepInduction E1 #

{motive : ℕ → Sort u_1} →
  motive 0 → motive 1 → ((n : ℕ) → motive n → motive (n + 1) → motive (n + 2)) → (a : ℕ) → motive a

Induction principle deriving the next case from the two previous ones.

def Nat.stepInduction E1 #

{motive : ℕ → Sort u_1} →
  (k : ℕ) →
    ((i : ℕ) → i < k → motive i) → ((n : ℕ) → ((i : ℕ) → i < k → motive (n + i)) → motive (n + k)) → (a : ℕ) → motive a

Induction principle deriving the next case from the k previous ones. Use as

induction n using stepInduction 3 with
| base n hn => ...
| step n ih => ...

theorem Nat.strong_induction_on E1 #

∀ {motive : ℕ → Prop} (n : ℕ), (∀ (n : ℕ), (∀ m < n, motive m) → motive n) → motive n

theorem Nat.case_strong_induction_on E1 #

∀ {motive : ℕ → Prop} (a : ℕ), motive 0 → (∀ (n : ℕ), (∀ m ≤ n, motive m) → motive (n + 1)) → motive a

def Nat.decreasingInduction E1 #

{n : ℕ} →
  {motive : (m : ℕ) → m ≤ n → Sort u_1} →
    ((k : ℕ) → (h : k < n) → motive (k + 1) h → motive k ⋯) → motive n ⋯ → {m : ℕ} → (mn : m ≤ n) → motive m mn

Decreasing induction: if motive (k + 1) implies motive k for all k < n, then motive n implies motive m for all m ≤ n. Also works for functions to Sort*.

For a version also assuming m ≤ k, see Nat.decreasingInduction'.

theorem Nat.decreasingInduction_self E0 #

∀ {n : ℕ} {motive : (m : ℕ) → m ≤ n → Sort u_1} (of_succ : (k : ℕ) → (h : k < n) → motive (k + 1) h → motive k ⋯)
  (self : motive n ⋯), Nat.decreasingInduction of_succ self ⋯ = self

theorem Nat.decreasingInduction_succ E1 #

∀ {m n : ℕ} {motive : (m : ℕ) → m ≤ n + 1 → Sort u_1}
  (of_succ : (k : ℕ) → (h : k < n + 1) → motive (k + 1) h → motive k ⋯) (self : motive (n + 1) ⋯) (mn : m ≤ n)
  (msn : m ≤ n + 1),
  Nat.decreasingInduction of_succ self msn = Nat.decreasingInduction (fun x x_1 ↦ of_succ x ⋯) (of_succ n ⋯ self) mn

theorem Nat.decreasingInduction_succ' E0 #

∀ {n : ℕ} {motive : (m : ℕ) → m ≤ n + 1 → Sort u_1}
  (of_succ : (k : ℕ) → (h : k < n + 1) → motive (k + 1) h → motive k ⋯) (self : motive (n + 1) ⋯),
  Nat.decreasingInduction of_succ self ⋯ = of_succ n ⋯ self

theorem Nat.decreasingInduction_trans E1 #

∀ {m n k : ℕ} {motive : (m : ℕ) → m ≤ k → Sort u_1} (hmn : m ≤ n) (hnk : n ≤ k)
  (of_succ : (k_1 : ℕ) → (h : k_1 < k) → motive (k_1 + 1) h → motive k_1 ⋯) (self : motive k ⋯),
  Nat.decreasingInduction of_succ self ⋯ =
    Nat.decreasingInduction (fun x x_1 ↦ of_succ x ⋯) (Nat.decreasingInduction of_succ self hnk) hmn

theorem Nat.decreasingInduction_succ_left E1 #

∀ {m n : ℕ} {motive : (m : ℕ) → m ≤ n → Sort u_1} (of_succ : (k : ℕ) → (h : k < n) → motive (k + 1) h → motive k ⋯)
  (self : motive n ⋯) (smn : m + 1 ≤ n) (mn : m ≤ n),
  Nat.decreasingInduction of_succ self mn = of_succ m smn (Nat.decreasingInduction of_succ self smn)

def Nat.strongSubRecursion E1 #

{motive : ℕ → ℕ → Sort u_1} →
  ((m n : ℕ) → ((x y : ℕ) → x < m → y < n → motive x y) → motive m n) → (n m : ℕ) → motive n m

Given motive : ℕ → ℕ → Sort*, if for all m n : ℕ we can extend motive from the rectangle strictly below (m, n) to motive m n, then we have motive n m for all n m : ℕ. Note that for non-Prop output it is preferable to use the equation compiler directly if possible, since this produces equation lemmas.

def Nat.pincerRecursion E1 #

{motive : ℕ → ℕ → Sort u_1} →
  ((m : ℕ) → motive m 0) →
    ((n : ℕ) → motive 0 n) →
      ((x y : ℕ) → motive x y.succ → motive x.succ y → motive x.succ y.succ) → (n m : ℕ) → motive n m

Given motive : ℕ → ℕ → Sort*, if we have motive m 0 and motive 0 n for all m n : ℕ, and for any m n : ℕ we can extend motive from (m, n + 1) and (m + 1, n) to (m + 1, n + 1) then we have motive m n for all m n : ℕ.

Note that for non-Prop output it is preferable to use the equation compiler directly if possible, since this produces equation lemmas.

def Nat.decreasingInduction' E1 #

{m n : ℕ} →
  {motive : ℕ → Sort u_1} → ((k : ℕ) → k < n → m ≤ k → motive (k + 1) → motive k) → m ≤ n → motive n → motive m

Decreasing induction: if motive (k + 1) implies motive k for all m ≤ k < n, then motive n implies motive m. Also works for functions to Sort*.

Weakens the assumptions of Nat.decreasingInduction.

theorem Nat.diag_induction E1 #

∀ (motive : ℕ → ℕ → Prop),
  (∀ (a : ℕ), motive (a + 1) (a + 1)) →
    (∀ (b : ℕ), motive 0 (b + 1)) →
      (∀ (a b : ℕ), a < b → motive (a + 1) b → motive a (b + 1) → motive (a + 1) (b + 1)) →
        ∀ (a b : ℕ), a < b → motive a b

Given a predicate on two naturals motive : ℕ → ℕ → Prop, motive a b is true for all a < b if motive (a + 1) (a + 1) is true for all a, motive 0 (b + 1) is true for all b and for all a < b, motive (a + 1) b is true and motive a (b + 1) is true implies motive (a + 1) (b + 1) is true.

theorem Nat.not_pos_pow_dvd E1 #

∀ {a n : ℕ}, 1 < a → 1 < n → ¬a ^ n ∣ a

theorem Nat.not_two_dvd_bit1 E0 #

∀ (n : ℕ), ¬2 ∣ 2 * n + 1

theorem Nat.dvd_add_self_left E0 #

∀ {m n : ℕ}, m ∣ m + n ↔ m ∣ n

A natural number m divides the sum m + n if and only if m divides n.

theorem Nat.dvd_add_self_right E0 #

∀ {m n : ℕ}, m ∣ n + m ↔ m ∣ n

A natural number m divides the sum n + m if and only if m divides n.

theorem Nat.dvd_div_of_mul_dvd_left E1 #

∀ {a b c : ℕ}, a * b ∣ c → b ∣ c / a

Alias of Nat.dvd_div_of_mul_dvd, to specify which factor is divided out, so that Nat.dvd_div_of_mul_dvd_right can have a symmetrical name.

theorem Nat.dvd_div_of_mul_dvd_right E1 #

∀ {a b c : ℕ}, a * b ∣ c → a ∣ c / b

theorem Nat.dvd_right_iff_eq E0 #

∀ {m n : ℕ}, (∀ (a : ℕ), m ∣ a ↔ n ∣ a) ↔ m = n

Two natural numbers are equal if and only if they have the same multiples.

theorem Nat.dvd_left_iff_eq E0 #

∀ {m n : ℕ}, (∀ (a : ℕ), a ∣ m ↔ a ∣ n) ↔ m = n

Two natural numbers are equal if and only if they have the same divisors.

instance Nat.decidableLoHi E0 #

(lo hi : ℕ) → (P : ℕ → Prop) → [DecidablePred P] → Decidable (∀ (x : ℕ), lo ≤ x → x < hi → P x)

instance Nat.decidableLoHiLe E0 #

(lo hi : ℕ) → (P : ℕ → Prop) → [DecidablePred P] → Decidable (∀ (x : ℕ), lo ≤ x → x ≤ hi → P x)

instance Nat.instNeZeroNatAbsOfInt_mathlib E0 #

∀ (n : ℤ) [NeZero n], NeZero n.natAbs

class Nat.AtLeastTwo E1 #

ℕ → Prop

A type class for natural numbers which are greater than or equal to 2.

NeZero and AtLeastTwo are used for numeric literals, and also for groups of related lemmas sharing a common value of n that needs to be nonzero, or at least 2, and where it is convenient to pass this information implicitly. Instances for these classes cover some of the cases where it is most structurally obvious from the syntactic form of n that it satisfies the required conditions, such as m + 1. Less widely used cases may be defined as lemmas rather than global instances and then made into instances locally where needed. If implicit arguments, appearing before other explicit arguments, are allowed to be autoParams in a future version of Lean, such an autoParam that is proved by lia might be a more general replacement for the use of typeclass inference for this purpose.

ctor Nat.AtLeastTwo.mk E0 #

∀ {n : ℕ}, 2 ≤ n → n.AtLeastTwo

theorem Nat.AtLeastTwo.prop E1 #

∀ {n : ℕ} [self : n.AtLeastTwo], 2 ≤ n

instance Nat.instAtLeastTwoHAddOfNat E0 #

∀ (n : ℕ) [NeZero n], (n + 1).AtLeastTwo

theorem Nat.AtLeastTwo.one_lt E1 #

∀ {n : ℕ} [n.AtLeastTwo], 1 < n

theorem Nat.AtLeastTwo.ne_one E1 #

∀ {n : ℕ} [n.AtLeastTwo], n ≠ 1

instance Nat.AtLeastTwo.toNeZero E0 #

∀ (n : ℕ) [n.AtLeastTwo], NeZero n

theorem Nat.AtLeastTwo.neZero_sub_one E1 #

∀ (n : ℕ) [n.AtLeastTwo], NeZero (n - 1)