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.Cast.Defs

39 declarations. Mathlib docs · Source

Cast of natural numbers

This file defines the canonical homomorphism from the natural numbers into an AddMonoid with a one. In additive monoids with one, there exists a unique such homomorphism and we store it in the natCast : ℕ → R field.

Preferentially, the homomorphism is written as the coercion Nat.cast.

Main declarations

  • NatCast: Type class for Nat.cast.
  • AddMonoidWithOne: Type class for which Nat.cast is a canonical monoid homomorphism from ℕ.
  • Nat.cast: Canonical homomorphism ℕ → R.

Additive monoids with one

theorem Nat.unaryCast.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 AddCommMonoidWithOne.ctorIdx E1 #

{R : Type u_2} → AddCommMonoidWithOne R → ℕ

theorem Nat.unaryCast.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 ()

def AddMonoidWithOne.ctorIdx E1 #

{R : Type u_2} → AddMonoidWithOne R → ℕ

def Nat.unaryCast E1 #

{R : Type u_1} → [One R] → [Zero R] → [Add R] → ℕ → R

The numeral ((0+1)+⋯)+1.

instance instOfNatAtLeastTwo E0 #

{R : Type u_1} → {n : ℕ} → [NatCast R] → [n.AtLeastTwo] → OfNat R n

Recognize numeric literals which are at least 2 as terms of R via Nat.cast. This instance is what makes things like 37 : R type check. Note that 0 and 1 are not needed because they are recognized as terms of R (at least when R is an AddMonoidWithOne) through Zero and One, respectively.

def LibraryNote.«no_index_around_OfNat.ofNat» E1 #

Batteries.Util.LibraryNote

When writing lemmas about OfNat.ofNat that assume Nat.AtLeastTwo, the term needs to be wrapped in no_index so as not to confuse simp, as no_index (OfNat.ofNat n).

Rather than referencing this library note, use ofNat(n) as a shorthand for no_index (OfNat.ofNat n).

Some discussion is on Zulip here.

theorem Nat.cast_ofNat E0 #

∀ {R : Type u_1} {n : ℕ} [inst : NatCast R] [inst_1 : n.AtLeastTwo], ↑(OfNat.ofNat n) = OfNat.ofNat n

class AddMonoidWithOne E3 #

Type u_2 → Type u_2

An AddMonoidWithOne is an AddMonoid with a 1. It also contains data for the unique homomorphism ℕ → R.

ctor AddMonoidWithOne.mk E0 #

{R : Type u_2} →
  [toNatCast : NatCast R] →
    [toAddMonoid : AddMonoid R] →
      [toOne : One R] →
        autoParam (↑0 = 0) AddMonoidWithOne.natCast_zero._autoParam →
          autoParam (∀ (n : ℕ), ↑(n + 1) = ↑n + 1) AddMonoidWithOne.natCast_succ._autoParam → AddMonoidWithOne R

instance AddMonoidWithOne.toNatCast E0 #

{R : Type u_2} → [self : AddMonoidWithOne R] → NatCast R

instance AddMonoidWithOne.toOne E0 #

{R : Type u_2} → [self : AddMonoidWithOne R] → One R

instance AddMonoidWithOne.toAddMonoid E0 #

{R : Type u_2} → [self : AddMonoidWithOne R] → AddMonoid R

theorem AddMonoidWithOne.natCast_zero E2 #

∀ {R : Type u_2} [self : AddMonoidWithOne R], ↑0 = 0

The canonical map ℕ → R sends 0 : ℕ to 0 : R.

theorem AddMonoidWithOne.natCast_succ E2 #

∀ {R : Type u_2} [self : AddMonoidWithOne R] (n : ℕ), ↑(n + 1) = ↑n + 1

The canonical map ℕ → R is a homomorphism.

class AddCommMonoidWithOne E1 #

Type u_2 → Type u_2

An AddCommMonoidWithOne is an AddMonoidWithOne satisfying a + b = b + a.

instance AddCommMonoidWithOne.toAddCommMonoid E0 #

{R : Type u_2} → [self : AddCommMonoidWithOne R] → AddCommMonoid R

instance AddCommMonoidWithOne.toAddMonoidWithOne E0 #

{R : Type u_2} → [self : AddCommMonoidWithOne R] → AddMonoidWithOne R

ctor AddCommMonoidWithOne.mk E0 #

{R : Type u_2} → [toAddMonoidWithOne : AddMonoidWithOne R] → (∀ (a b : R), a + b = b + a) → AddCommMonoidWithOne R

theorem AddCommMonoidWithOne.add_comm E1 #

∀ {R : Type u_2} [self : AddCommMonoidWithOne R] (a b : R), a + b = b + a

Addition is commutative in a commutative additive magma.

theorem Nat.cast_zero E0 #

∀ {R : Type u_1} [inst : AddMonoidWithOne R], ↑0 = 0

theorem Nat.cast_succ E1 #

∀ {R : Type u_1} [inst : AddMonoidWithOne R] (n : ℕ), ↑n.succ = ↑n + 1

theorem Nat.cast_ite E0 #

∀ {R : Type u_1} [inst : AddMonoidWithOne R] (P : Prop) [inst_1 : Decidable P] (m n : ℕ),
  ↑(if P then m else n) = if P then ↑m else ↑n

theorem Nat.cast_one E0 #

∀ {R : Type u_1} [inst : AddMonoidWithOne R], ↑1 = 1

theorem Nat.cast_add E0 #

∀ {R : Type u_1} [inst : AddMonoidWithOne R] (m n : ℕ), ↑(m + n) = ↑m + ↑n

theorem Nat.cast_add_one E1 #

∀ {R : Type u_1} [inst : AddMonoidWithOne R] (n : ℕ), ↑(n + 1) = ↑n + 1

theorem Nat.cast_one_add E1 #

∀ {R : Type u_1} [inst : AddMonoidWithOne R] (n : ℕ), ↑(1 + n) = 1 + ↑n

def Nat.binCast E1 #

{R : Type u_1} → [Zero R] → [One R] → [Add R] → ℕ → R

Computationally friendlier cast than Nat.unaryCast, using binary representation.

theorem Nat.binCast_eq E0 #

∀ {R : Type u_1} [inst : AddMonoidWithOne R] (n : ℕ), n.binCast = ↑n

theorem Nat.cast_two E1 #

∀ {R : Type u_1} [inst : NatCast R], ↑2 = 2

theorem Nat.cast_three E1 #

∀ {R : Type u_1} [inst : NatCast R], ↑3 = 3

theorem Nat.cast_four E1 #

∀ {R : Type u_1} [inst : NatCast R], ↑4 = 4

abbrev AddMonoidWithOne.unary E1 #

{R : Type u_1} → [AddMonoid R] → [One R] → AddMonoidWithOne R

AddMonoidWithOne implementation using unary recursion.

abbrev AddMonoidWithOne.binary E1 #

{R : Type u_1} → [AddMonoid R] → [One R] → AddMonoidWithOne R

AddMonoidWithOne implementation using binary recursion.

theorem one_add_one_eq_two E1 #

∀ {R : Type u_1} [inst : AddMonoidWithOne R], 1 + 1 = 2

theorem two_add_one_eq_three E1 #

∀ {R : Type u_1} [inst : AddMonoidWithOne R], 2 + 1 = 3

theorem three_add_one_eq_four E1 #

∀ {R : Type u_1} [inst : AddMonoidWithOne R], 3 + 1 = 4

theorem two_add_two_eq_four E1 #

∀ {R : Type u_1} [inst : AddMonoidWithOne R], 2 + 2 = 4

theorem nsmul_one E0 #

∀ {A : Type u_2} [inst : AddMonoidWithOne A] (n : ℕ), n • 1 = ↑n