Documentation

Mathlib.Data.Nat.Bits

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.

boddDiv2 n returns a 2-tuple of type (Bool, Nat) where the Bool value indicates whether n is odd or not and the Nat value returns ⌊n/2⌋

Equations
Instances For
    def Nat.div2 (n : ) :

    div2 n = ⌊n/2⌋ the greatest integer smaller than n/2

    Equations
    • n.div2 = n.boddDiv2.2
    Instances For
      def Nat.bodd (n : ) :

      bodd n returns true if n is odd

      Equations
      • n.bodd = n.boddDiv2.1
      Instances For
        @[simp]
        @[simp]
        @[simp]
        theorem Nat.bodd_succ (n : ) :
        n.succ.bodd = !n.bodd
        @[simp]
        theorem Nat.bodd_add (m : ) (n : ) :
        (m + n).bodd = (m.bodd ^^ n.bodd)
        @[simp]
        theorem Nat.bodd_mul (m : ) (n : ) :
        (m * n).bodd = (m.bodd && n.bodd)
        theorem Nat.mod_two_of_bodd (n : ) :
        n % 2 = n.bodd.toNat
        @[simp]
        theorem Nat.div2_zero :
        @[simp]
        theorem Nat.div2_one :
        @[simp]
        theorem Nat.div2_succ (n : ) :
        (n + 1).div2 = bif n.bodd then n.div2.succ else n.div2
        theorem Nat.bodd_add_div2 (n : ) :
        n.bodd.toNat + 2 * n.div2 = n
        theorem Nat.div2_val (n : ) :
        n.div2 = n / 2
        theorem Nat.bit_decomp (n : ) :
        Nat.bit n.bodd n.div2 = n
        def Nat.shiftLeft' (b : Bool) (m : ) :

        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

        Equations
        Instances For
          @[simp]
          theorem Nat.shiftLeft'_false {m : } (n : ) :
          @[simp]
          theorem Nat.shiftLeft_eq' (m : ) (n : ) :
          m.shiftLeft n = m <<< n

          Lean takes the unprimed name for Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n.

          @[simp]
          theorem Nat.shiftRight_eq (m : ) (n : ) :
          m.shiftRight n = m >>> n
          theorem Nat.binaryRec_decreasing {n : } (h : n 0) :
          n.div2 < n
          def Nat.size :

          size n : Returns the size of a natural number in bits i.e. the length of its binary representation

          Equations
          Instances For

            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

            Equations
            Instances For
              def Nat.ldiff :

              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.

              Equations
              Instances For

                bitwise ops

                theorem Nat.bodd_bit (b : Bool) (n : ) :
                (Nat.bit b n).bodd = b
                theorem Nat.div2_bit (b : Bool) (n : ) :
                (Nat.bit b n).div2 = n
                theorem Nat.shiftLeft'_add (b : Bool) (m : ) (n : ) (k : ) :
                theorem Nat.shiftLeft'_sub (b : Bool) (m : ) {n : } {k : } :
                k nNat.shiftLeft' b m (n - k) = Nat.shiftLeft' b m n >>> k
                theorem Nat.shiftLeft_sub (m : ) {n : } {k : } :
                k nm <<< (n - k) = m <<< n >>> k
                theorem Nat.bodd_eq_one_and_ne_zero (n : ) :
                n.bodd = (1 &&& n != 0)
                theorem Nat.testBit_bit_succ (m : ) (b : Bool) (n : ) :
                (Nat.bit b n).testBit m.succ = n.testBit m

                boddDiv2_eq and bodd #

                @[simp]
                theorem Nat.boddDiv2_eq (n : ) :
                n.boddDiv2 = (n.bodd, n.div2)
                @[simp]
                theorem Nat.div2_bit0 (n : ) :
                (2 * n).div2 = n
                theorem Nat.div2_bit1 (n : ) :
                (2 * n + 1).div2 = n

                bit0 and bit1 #

                theorem Nat.bit_add (b : Bool) (n : ) (m : ) :
                Nat.bit b (n + m) = Nat.bit false n + Nat.bit b m
                theorem Nat.bit_add' (b : Bool) (n : ) (m : ) :
                Nat.bit b (n + m) = Nat.bit b n + Nat.bit false m
                theorem Nat.bit_ne_zero (b : Bool) {n : } (h : n 0) :
                Nat.bit b n 0
                @[simp]
                theorem Nat.bitCasesOn_bit0 {motive : Sort u} (H : (b : Bool) → (n : ) → motive (Nat.bit b n)) (n : ) :
                Nat.bitCasesOn (2 * n) H = H false n
                @[simp]
                theorem Nat.bitCasesOn_bit1 {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 {motive : Sort u} :
                Function.Injective fun (H : (b : Bool) → (n : ) → motive (Nat.bit b n)) (n : ) => Nat.bitCasesOn n H
                @[simp]
                theorem Nat.bit_cases_on_inj {motive : Sort u} (H₁ : (b : Bool) → (n : ) → motive (Nat.bit b n)) (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 (b : Bool) {m : } {n : } :
                m nNat.bit b m Nat.bit b n
                theorem Nat.bit_lt_bit {m : } {n : } (a : Bool) (b : Bool) (h : m < n) :
                Nat.bit a m < Nat.bit b n
                @[simp]
                theorem Nat.zero_bits :
                Nat.bits 0 = []
                @[simp]
                theorem Nat.bits_append_bit (n : ) (b : Bool) (hn : n = 0b = true) :
                (Nat.bit b n).bits = b :: n.bits
                @[simp]
                theorem Nat.bit0_bits (n : ) (hn : n 0) :
                (2 * n).bits = false :: n.bits
                @[simp]
                theorem Nat.bit1_bits (n : ) :
                (2 * n + 1).bits = true :: n.bits
                @[simp]
                theorem Nat.bodd_eq_bits_head (n : ) :
                n.bodd = n.bits.headI
                theorem Nat.div2_bits_eq_tail (n : ) :
                n.div2.bits = n.bits.tail