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.PSub

19 declarations. Mathlib docs · Source

Partial predecessor and partial subtraction on the natural numbers

The usual definition of natural number subtraction (Nat.sub) returns 0 as a "garbage value" for a - b when a < b. Similarly, Nat.pred 0 is defined to be 0. The functions in this file wrap the result in an Option type instead:

Main definitions

  • Nat.ppred: a partial predecessor operation
  • Nat.psub: a partial subtraction operation

theorem Nat.ppred.match_1.congr_eq_1 E1 #

∀ (motive : ℕ → Sort u_1) (x : ℕ) (h_1 : Unit → motive 0) (h_2 : (n : ℕ) → motive n.succ),
  x = 0 →
    (match x with
      | 0 => h_1 ()
      | n.succ => h_2 n) ≍
      h_1 ()

theorem Nat.ppred.match_1.congr_eq_2 E1 #

∀ (motive : ℕ → Sort u_1) (x : ℕ) (h_1 : Unit → motive 0) (h_2 : (n : ℕ) → motive n.succ) (n : ℕ),
  x = n.succ →
    (match x with
      | 0 => h_1 ()
      | n.succ => h_2 n) ≍
      h_2 n

def Nat.ppred E3 #

ℕ → Option ℕ

Partial predecessor operation. Returns ppred n = some m if n = m + 1, otherwise none.

theorem Nat.ppred_zero E0 #

Nat.ppred 0 = none

theorem Nat.ppred_succ E0 #

∀ {n : ℕ}, n.succ.ppred = some n

def Nat.psub E3 #

ℕ → ℕ → Option ℕ

Partial subtraction operation. Returns psub m n = some k if m = n + k, otherwise none.

theorem Nat.psub_zero E0 #

∀ {m : ℕ}, m.psub 0 = some m

theorem Nat.psub_succ E0 #

∀ {m n : ℕ}, m.psub n.succ = m.psub n >>= Nat.ppred

theorem Nat.pred_eq_ppred E1 #

∀ (n : ℕ), n.pred = n.ppred.getD 0

theorem Nat.sub_eq_psub E1 #

∀ (m n : ℕ), m - n = (m.psub n).getD 0

theorem Nat.ppred_eq_some E0 #

∀ {m n : ℕ}, n.ppred = some m ↔ m.succ = n

theorem Nat.ppred_eq_none E0 #

∀ {n : ℕ}, n.ppred = none ↔ n = 0

theorem Nat.psub_eq_some E1 #

∀ {m n k : ℕ}, m.psub n = some k ↔ k + n = m

theorem Nat.psub_eq_none E1 #

∀ {m n : ℕ}, m.psub n = none ↔ m < n

theorem Nat.ppred_eq_pred E1 #

∀ {n : ℕ}, 0 < n → n.ppred = some n.pred

theorem Nat.psub_eq_sub E1 #

∀ {m n : ℕ}, n ≤ m → m.psub n = some (m - n)

theorem Nat.psub_add E1 #

∀ (m n k : ℕ),
  m.psub (n + k) = do
    let __do_lift ← m.psub n
    __do_lift.psub k

def Nat.psub' E1 #

ℕ → ℕ → Option ℕ

Same as psub, but with a more efficient implementation.

theorem Nat.psub'_eq_psub E1 #

∀ (m n : ℕ), m.psub' n = m.psub n