data.nat.bits

# Additional properties of binary recursion on nat#

THIS FILE IS SYNCHRONIZED WITH MATHLIB4. Any changes to this file require a corresponding PR to mathlib4.

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 shiftl/shiftr), and nat.digits.

### bodd_div2 and bodd#

@[simp]
theorem nat.bodd_div2_eq (n : ) :
n.bodd_div2 = (n.bodd, n.div2)
@[simp]
theorem nat.bodd_bit0 (n : ) :
@[simp]
theorem nat.bodd_bit1 (n : ) :
@[simp]
theorem nat.div2_bit0 (n : ) :
(bit0 n).div2 = n
@[simp]
theorem nat.div2_bit1 (n : ) :
(bit1 n).div2 = n

### bit0 and bit1#

@[simp]
theorem nat.bit0_eq_bit0 {m n : } :
bit0 m = bit0 n m = n
@[simp]
theorem nat.bit1_eq_bit1 {m n : } :
bit1 m = bit1 n m = n
@[simp]
theorem nat.bit1_eq_one {n : } :
bit1 n = 1 n = 0
@[simp]
theorem nat.one_eq_bit1 {n : } :
1 = bit1 n n = 0
theorem nat.bit_add (b : bool) (n m : ) :
(n + m) = + m
theorem nat.bit_add' (b : bool) (n m : ) :
(n + m) = n +
theorem nat.bit_ne_zero (b : bool) {n : } (h : n 0) :
n 0
theorem nat.bit0_mod_two {n : } :
bit0 n % 2 = 0
theorem nat.bit1_mod_two {n : } :
bit1 n % 2 = 1
theorem nat.pos_of_bit0_pos {n : } (h : 0 < bit0 n) :
0 < n
@[simp]
theorem nat.bit_cases_on_bit {C : Sort u} (H : Π (b : bool) (n : ), C (nat.bit b n)) (b : bool) (n : ) :
(nat.bit b n).bit_cases_on H = H b n
@[simp]
theorem nat.bit_cases_on_bit0 {C : Sort u} (H : Π (b : bool) (n : ), C (nat.bit b n)) (n : ) :
@[simp]
theorem nat.bit_cases_on_bit1 {C : Sort u} (H : Π (b : bool) (n : ), C (nat.bit b n)) (n : ) :
theorem nat.bit_cases_on_injective {C : Sort u} :
function.injective (λ (H : Π (b : bool) (n : ), C (nat.bit b n)) (n : ), n.bit_cases_on H)
@[simp]
theorem nat.bit_cases_on_inj {C : Sort u} (H₁ H₂ : Π (b : bool) (n : ), C (nat.bit b n)) :
((λ (n : ), n.bit_cases_on H₁) = λ (n : ), n.bit_cases_on H₂) H₁ = H₂
@[protected]
theorem nat.bit0_eq_zero {n : } :
bit0 n = 0 n = 0
theorem nat.bit_eq_zero_iff {n : } {b : bool} :
n = 0 n = 0
theorem nat.binary_rec_eq' {C : Sort u_1} {z : C 0} {f : Π (b : bool) (n : ), C n C (nat.bit b n)} (b : bool) (n : ) (h : 0 z = z (n = 0 b = bool.tt)) :
(nat.bit b n) = f b n f n)

The same as binary_rec_eq, but that one unfortunately requires f to be the identity when appending ff to 0. Here, we allow you to explicitly say that that case is not happening, i.e. supplying n = 0 → b = tt.

def nat.binary_rec' {C : Sort u_1} (z : C 0) (f : Π (b : bool) (n : ), (n = 0 b = bool.tt) C n C (nat.bit b n)) (n : ) :
C n

The same as binary_rec, but the induction step can assume that if n=0, the bit being appended is tt

Equations
def nat.binary_rec_from_one {C : Sort u_1} (z₀ : C 0) (z₁ : C 1) (f : Π (b : bool) (n : ), n 0 C n C (nat.bit b n)) (n : ) :
C n

The same as binary_rec, but special casing both 0 and 1 as base cases

Equations
@[simp]
theorem nat.zero_bits  :
@[simp]
theorem nat.bits_append_bit (n : ) (b : bool) (hn : n = 0 ) :
(nat.bit b n).bits = b :: n.bits
@[simp]
theorem nat.bit0_bits (n : ) (hn : n 0) :
(bit0 n).bits =
@[simp]
theorem nat.bit1_bits (n : ) :
(bit1 n).bits =
@[simp]
theorem nat.one_bits  :
theorem nat.bodd_eq_bits_head (n : ) :