Mathlib.Data.Nat.Bits
51 declarations. Mathlib docs · Source
Additional properties of binary recursion on Nat
This file documents additional properties of binary recursion,
which allows us to more easily work with operations which do depend
on the number of leading zeros in the binary representation of n.
For example, we can more easily work with Nat.bits and Nat.size.
See also: Nat.bitwise, Nat.pow (for various lemmas about size and shiftLeft/shiftRight),
and Nat.digits.
bitwise ops
boddDiv2_eq and bodd
bit0 and bit1
theorem Nat.shiftLeft'.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
theorem Nat.shiftLeft'.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 Nat.div2 E1 #
ℕ → ℕ
div2 n = ⌊n/2⌋ the greatest integer smaller than n/2
theorem Nat.div2_val E0 #
∀ (n : ℕ), n.div2 = n / 2
def Nat.bodd E1 #
ℕ → Bool
bodd n returns true if n is odd
theorem Nat.bodd_zero E0 #
Nat.bodd 0 = false
theorem Nat.bodd_one E0 #
Nat.bodd 1 = true
theorem Nat.bodd_two E1 #
Nat.bodd 2 = false
theorem Nat.bodd_succ E0 #
∀ (n : ℕ), n.succ.bodd = !n.bodd
theorem Nat.bodd_add E0 #
∀ (m n : ℕ), (m + n).bodd = (m.bodd ^^ n.bodd)
theorem Nat.bodd_mul E0 #
∀ (m n : ℕ), (m * n).bodd = (m.bodd && n.bodd)
theorem Nat.bodd_bit E0 #
∀ (b : Bool) (n : ℕ), (Nat.bit b n).bodd = b
theorem Nat.mod_two_of_bodd E1 #
∀ (n : ℕ), n % 2 = n.bodd.toNat
theorem Nat.div2_zero E0 #
Nat.div2 0 = 0
theorem Nat.div2_one E0 #
Nat.div2 1 = 0
theorem Nat.div2_two E1 #
Nat.div2 2 = 1
theorem Nat.div2_succ E0 #
∀ (n : ℕ), (n + 1).div2 = bif n.bodd then n.div2.succ else n.div2
theorem Nat.div2_bit E0 #
∀ (b : Bool) (n : ℕ), (Nat.bit b n).div2 = n
theorem Nat.bodd_add_div2 E1 #
∀ (n : ℕ), n.bodd.toNat + 2 * n.div2 = n
theorem Nat.bit_bodd_div2 E0 #
∀ (n : ℕ), Nat.bit n.bodd n.div2 = n
theorem Nat.bit_false_zero E1 #
Nat.bit false 0 = 0
def Nat.shiftLeft' E1 #
Bool → ℕ → ℕ → ℕ
shiftLeft' b m n performs a left shift of m n times
and adds the bit b as the least significant bit each time.
Returns the corresponding natural number
theorem Nat.shiftLeft'_false E0 #
∀ {m : ℕ} (n : ℕ), Nat.shiftLeft' false m n = m <<< n
theorem Nat.shiftRight_eq E0 #
∀ (m n : ℕ), m.shiftRight n = m >>> n
theorem Nat.binaryRec_decreasing E1 #
∀ {n : ℕ}, n ≠ 0 → n.div2 < n
def Nat.size E1 #
ℕ → ℕ
size n : Returns the size of a natural number in
bits i.e. the length of its binary representation
def Nat.bits E1 #
ℕ → List Bool
bits n returns a list of Bools which correspond to the binary representation of n, where
the head of the list represents the least significant bit
def Nat.ldiff E1 #
ℕ → ℕ → ℕ
ldiff a b performs bitwise set difference. For each corresponding
pair of bits taken as Booleans, say aᵢ and bᵢ, it applies the
Boolean operation aᵢ ∧ ¬bᵢ to obtain the iᵗʰ bit of the result.
theorem Nat.shiftLeft'_add E1 #
∀ (b : Bool) (m n k : ℕ), Nat.shiftLeft' b m (n + k) = Nat.shiftLeft' b (Nat.shiftLeft' b m n) k
theorem Nat.shiftLeft'_sub E1 #
∀ (b : Bool) (m : ℕ) {n k : ℕ}, k ≤ n → Nat.shiftLeft' b m (n - k) = Nat.shiftLeft' b m n >>> k
theorem Nat.shiftLeft_sub E1 #
∀ (m : ℕ) {n k : ℕ}, k ≤ n → m <<< (n - k) = m <<< n >>> k
theorem Nat.bodd_eq_one_and_ne_zero E1 #
∀ (n : ℕ), n.bodd = (1 &&& n != 0)
theorem Nat.testBit_bit_succ E1 #
∀ (m : ℕ) (b : Bool) (n : ℕ), (Nat.bit b n).testBit m.succ = n.testBit m
theorem Nat.div2_bit0 E0 #
∀ (n : ℕ), (2 * n).div2 = n
theorem Nat.div2_bit1 E1 #
∀ (n : ℕ), (2 * n + 1).div2 = n
theorem Nat.bit_add E1 #
∀ (b : Bool) (n m : ℕ), Nat.bit b (n + m) = Nat.bit false n + Nat.bit b m
theorem Nat.bit_add' E1 #
∀ (b : Bool) (n m : ℕ), Nat.bit b (n + m) = Nat.bit b n + Nat.bit false m
theorem Nat.bit_ne_zero E1 #
∀ (b : Bool) {n : ℕ}, n ≠ 0 → Nat.bit b n ≠ 0
theorem Nat.bitCasesOn_bit0 E0 #
∀ {motive : ℕ → Sort u} (H : (b : Bool) → (n : ℕ) → motive (Nat.bit b n)) (n : ℕ), Nat.bitCasesOn (2 * n) H = H false n
theorem Nat.bitCasesOn_bit1 E0 #
∀ {motive : ℕ → Sort u} (H : (b : Bool) → (n : ℕ) → motive (Nat.bit b n)) (n : ℕ),
Nat.bitCasesOn (2 * n + 1) H = H true n
theorem Nat.bit_cases_on_injective E1 #
∀ {motive : ℕ → Sort u}, Function.Injective fun H n ↦ Nat.bitCasesOn n H
theorem Nat.bit_cases_on_inj E0 #
∀ {motive : ℕ → Sort u} (H₁ H₂ : (b : Bool) → (n : ℕ) → motive (Nat.bit b n)),
((fun n ↦ Nat.bitCasesOn n H₁) = fun n ↦ Nat.bitCasesOn n H₂) ↔ H₁ = H₂
theorem Nat.bit_le E1 #
∀ (b : Bool) {m n : ℕ}, m ≤ n → Nat.bit b m ≤ Nat.bit b n
theorem Nat.bit_lt_bit E1 #
∀ {m n : ℕ} (a b : Bool), m < n → Nat.bit a m < Nat.bit b n
theorem Nat.zero_bits E0 #
Nat.bits 0 = []
theorem Nat.bits_append_bit E0 #
∀ (n : ℕ) (b : Bool), (n = 0 → b = true) → (Nat.bit b n).bits = b :: n.bits
theorem Nat.bit0_bits E0 #
∀ (n : ℕ), n ≠ 0 → (2 * n).bits = false :: n.bits
theorem Nat.bit1_bits E0 #
∀ (n : ℕ), (2 * n + 1).bits = true :: n.bits
theorem Nat.one_bits E0 #
Nat.bits 1 = [true]
theorem Nat.bodd_eq_bits_head E1 #
∀ (n : ℕ), n.bodd = n.bits.headI
theorem Nat.div2_bits_eq_tail E1 #
∀ (n : ℕ), n.div2.bits = n.bits.tail