state
stringlengths
0
159k
srcUpToTactic
stringlengths
387
167k
nextTactic
stringlengths
3
9k
declUpToTactic
stringlengths
22
11.5k
declId
stringlengths
38
95
decl
stringlengths
16
1.89k
file_tag
stringlengths
17
73
n : β„• ⊒ (bif bodd n then 0 else 1) + (bif bodd n then succ (div2 n) else div2 n) * 2 = succ n
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm]
refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n))
theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm]
Mathlib.Init.Data.Nat.Bitwise.128_0.OFUBkIQvV236FCW
theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ]
Mathlib_Init_Data_Nat_Bitwise
n : β„• ⊒ (bif bodd n then 0 else 1) + (bif bodd n then succ (div2 n) else div2 n) * 2 = succ ((bif bodd n then 1 else 0) + 2 * div2 n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n))
cases bodd n
theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n))
Mathlib.Init.Data.Nat.Bitwise.128_0.OFUBkIQvV236FCW
theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ]
Mathlib_Init_Data_Nat_Bitwise
case false n : β„• ⊒ (bif false then 0 else 1) + (bif false then succ (div2 n) else div2 n) * 2 = succ ((bif false then 1 else 0) + 2 * div2 n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;>
simp [cond, not]
theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;>
Mathlib.Init.Data.Nat.Bitwise.128_0.OFUBkIQvV236FCW
theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ]
Mathlib_Init_Data_Nat_Bitwise
case true n : β„• ⊒ (bif true then 0 else 1) + (bif true then succ (div2 n) else div2 n) * 2 = succ ((bif true then 1 else 0) + 2 * div2 n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;>
simp [cond, not]
theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;>
Mathlib.Init.Data.Nat.Bitwise.128_0.OFUBkIQvV236FCW
theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ]
Mathlib_Init_Data_Nat_Bitwise
case false n : β„• ⊒ 1 + div2 n * 2 = succ (div2 n * 2)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β·
rw [Nat.add_comm, Nat.add_succ]
theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β·
Mathlib.Init.Data.Nat.Bitwise.128_0.OFUBkIQvV236FCW
theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ]
Mathlib_Init_Data_Nat_Bitwise
case true n : β„• ⊒ succ (div2 n) * 2 = succ (1 + div2 n * 2)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β·
rw [succ_mul, Nat.add_comm 1, Nat.add_succ]
theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β·
Mathlib.Init.Data.Nat.Bitwise.128_0.OFUBkIQvV236FCW
theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ]
Mathlib_Init_Data_Nat_Bitwise
n : β„• ⊒ div2 n = n / 2
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by
refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm))
theorem div2_val (n) : div2 n = n / 2 := by
Mathlib.Init.Data.Nat.Bitwise.138_0.OFUBkIQvV236FCW
theorem div2_val (n) : div2 n = n / 2
Mathlib_Init_Data_Nat_Bitwise
n : β„• ⊒ 0 < 2
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by
decide
theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by
Mathlib.Init.Data.Nat.Bitwise.138_0.OFUBkIQvV236FCW
theorem div2_val (n) : div2 n = n / 2
Mathlib_Init_Data_Nat_Bitwise
n : β„• ⊒ n % 2 + 2 * div2 n = n
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm))
rw [mod_two_of_bodd, bodd_add_div2]
theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm))
Mathlib.Init.Data.Nat.Bitwise.138_0.OFUBkIQvV236FCW
theorem div2_val (n) : div2 n = n / 2
Mathlib_Init_Data_Nat_Bitwise
n : β„• ⊒ n + n = 0 + n + n
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by
rw [Nat.zero_add]
theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by
Mathlib.Init.Data.Nat.Bitwise.151_0.OFUBkIQvV236FCW
theorem bit0_val (n : Nat) : bit0 n = 2 * n
Mathlib_Init_Data_Nat_Bitwise
b : Bool n : β„• ⊒ bit b n = 2 * n + bif b then 1 else 0
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by
cases b
theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by
Mathlib.Init.Data.Nat.Bitwise.162_0.OFUBkIQvV236FCW
theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0
Mathlib_Init_Data_Nat_Bitwise
case false n : β„• ⊒ bit false n = 2 * n + bif false then 1 else 0 case true n : β„• ⊒ bit true n = 2 * n + bif true then 1 else 0
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b
apply bit0_val
theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b
Mathlib.Init.Data.Nat.Bitwise.162_0.OFUBkIQvV236FCW
theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0
Mathlib_Init_Data_Nat_Bitwise
case true n : β„• ⊒ bit true n = 2 * n + bif true then 1 else 0
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val
apply bit1_val
theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val
Mathlib.Init.Data.Nat.Bitwise.162_0.OFUBkIQvV236FCW
theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0
Mathlib_Init_Data_Nat_Bitwise
m n : β„• ⊒ shiftLeft' false m (n + 1) = m <<< (n + 1)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by
have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp
@[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by
Mathlib.Init.Data.Nat.Bitwise.190_0.OFUBkIQvV236FCW
@[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m
Mathlib_Init_Data_Nat_Bitwise
m n : β„• ⊒ 2 * (m * 2 ^ n) = 2 ^ (n + 1) * m
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by
rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]
@[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by
Mathlib.Init.Data.Nat.Bitwise.190_0.OFUBkIQvV236FCW
@[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m
Mathlib_Init_Data_Nat_Bitwise
m n : β„• ⊒ m * 2 ^ succ n = 2 ^ (n + 1) * m
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ];
simp
@[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ];
Mathlib.Init.Data.Nat.Bitwise.190_0.OFUBkIQvV236FCW
@[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m
Mathlib_Init_Data_Nat_Bitwise
m n : β„• this : 2 * (m * 2 ^ n) = 2 ^ (n + 1) * m ⊒ shiftLeft' false m (n + 1) = m <<< (n + 1)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp
simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this]
@[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp
Mathlib.Init.Data.Nat.Bitwise.190_0.OFUBkIQvV236FCW
@[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m
Mathlib_Init_Data_Nat_Bitwise
n : β„• h : n β‰  0 ⊒ div2 n < n
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by
rw [div2_val]
lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by
Mathlib.Init.Data.Nat.Bitwise.207_0.OFUBkIQvV236FCW
lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n
Mathlib_Init_Data_Nat_Bitwise
n : β„• h : n β‰  0 ⊒ n / 2 < n
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val]
apply (div_lt_iff_lt_mul <| succ_pos 1).2
lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val]
Mathlib.Init.Data.Nat.Bitwise.207_0.OFUBkIQvV236FCW
lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n
Mathlib_Init_Data_Nat_Bitwise
n : β„• h : n β‰  0 ⊒ n < n * succ 1
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2
have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm)
lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2
Mathlib.Init.Data.Nat.Bitwise.207_0.OFUBkIQvV236FCW
lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n
Mathlib_Init_Data_Nat_Bitwise
n : β„• h : n β‰  0 this : n * 1 < n * succ 1 ⊒ n < n * succ 1
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm)
rwa [Nat.mul_one] at this
lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm)
Mathlib.Init.Data.Nat.Bitwise.207_0.OFUBkIQvV236FCW
lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) n : β„• n0 : n = 0 ⊒ C n
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by
simp only [n0]
/-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by
Mathlib.Init.Data.Nat.Bitwise.214_0.OFUBkIQvV236FCW
/-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) n : β„• n0 : n = 0 ⊒ C 0
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0]
exact z
/-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0]
Mathlib.Init.Data.Nat.Bitwise.214_0.OFUBkIQvV236FCW
/-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) n : β„• n0 : Β¬n = 0 ⊒ C n
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by
let n' := div2 n
/-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by
Mathlib.Init.Data.Nat.Bitwise.214_0.OFUBkIQvV236FCW
/-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) n : β„• n0 : Β¬n = 0 n' : β„• := div2 n ⊒ C n
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n
have _x : bit (bodd n) n' = n := by apply bit_decomp n
/-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n
Mathlib.Init.Data.Nat.Bitwise.214_0.OFUBkIQvV236FCW
/-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) n : β„• n0 : Β¬n = 0 n' : β„• := div2 n ⊒ bit (bodd n) n' = n
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by
apply bit_decomp n
/-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by
Mathlib.Init.Data.Nat.Bitwise.214_0.OFUBkIQvV236FCW
/-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) n : β„• n0 : Β¬n = 0 n' : β„• := div2 n _x : bit (bodd n) n' = n ⊒ C n
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n
rw [← _x]
/-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n
Mathlib.Init.Data.Nat.Bitwise.214_0.OFUBkIQvV236FCW
/-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) n : β„• n0 : Β¬n = 0 n' : β„• := div2 n _x : bit (bodd n) n' = n ⊒ C (bit (bodd n) n')
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x]
exact f (bodd n) n' (binaryRec z f n')
/-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x]
Mathlib.Init.Data.Nat.Bitwise.214_0.OFUBkIQvV236FCW
/-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n
Mathlib_Init_Data_Nat_Bitwise
n : β„• n0 : Β¬n = 0 n' : β„• := div2 n _x : bit (bodd n) n' = n ⊒ (invImage (fun a => sizeOf a) instWellFoundedRelation).1 n' n
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by
exact binaryRec_decreasing n0
/-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by
Mathlib.Init.Data.Nat.Bitwise.214_0.OFUBkIQvV236FCW
/-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) ⊒ binaryRec z f 0 = z
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by
rw [binaryRec]
@[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by
Mathlib.Init.Data.Nat.Bitwise.257_0.OFUBkIQvV236FCW
@[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) ⊒ (if n0 : 0 = 0 then Eq.mpr (_ : C 0 = C 0) z else let n' := div2 0; let_fun _x := (_ : bit (bodd 0) (div2 0) = 0); Eq.mpr (_ : C 0 = C (bit (bodd 0) n')) (f (bodd 0) n' (binaryRec z f n'))) = z
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec]
rfl
@[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec]
Mathlib.Init.Data.Nat.Bitwise.257_0.OFUBkIQvV236FCW
@[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z
Mathlib_Init_Data_Nat_Bitwise
b : Bool n : β„• ⊒ bodd (bit b n) = b
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by
rw [bit_val]
theorem bodd_bit (b n) : bodd (bit b n) = b := by
Mathlib.Init.Data.Nat.Bitwise.266_0.OFUBkIQvV236FCW
theorem bodd_bit (b n) : bodd (bit b n) = b
Mathlib_Init_Data_Nat_Bitwise
b : Bool n : β„• ⊒ bodd (2 * n + bif b then 1 else 0) = b
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val]
simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false]
theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val]
Mathlib.Init.Data.Nat.Bitwise.266_0.OFUBkIQvV236FCW
theorem bodd_bit (b n) : bodd (bit b n) = b
Mathlib_Init_Data_Nat_Bitwise
b : Bool n : β„• ⊒ bodd (bif b then 1 else 0) = b
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false]
cases b
theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false]
Mathlib.Init.Data.Nat.Bitwise.266_0.OFUBkIQvV236FCW
theorem bodd_bit (b n) : bodd (bit b n) = b
Mathlib_Init_Data_Nat_Bitwise
case false n : β„• ⊒ bodd (bif false then 1 else 0) = false
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;>
cases bodd n
theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;>
Mathlib.Init.Data.Nat.Bitwise.266_0.OFUBkIQvV236FCW
theorem bodd_bit (b n) : bodd (bit b n) = b
Mathlib_Init_Data_Nat_Bitwise
case true n : β„• ⊒ bodd (bif true then 1 else 0) = true
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;>
cases bodd n
theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;>
Mathlib.Init.Data.Nat.Bitwise.266_0.OFUBkIQvV236FCW
theorem bodd_bit (b n) : bodd (bit b n) = b
Mathlib_Init_Data_Nat_Bitwise
case false.false n : β„• ⊒ bodd (bif false then 1 else 0) = false
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;>
rfl
theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;>
Mathlib.Init.Data.Nat.Bitwise.266_0.OFUBkIQvV236FCW
theorem bodd_bit (b n) : bodd (bit b n) = b
Mathlib_Init_Data_Nat_Bitwise
case false.true n : β„• ⊒ bodd (bif false then 1 else 0) = false
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;>
rfl
theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;>
Mathlib.Init.Data.Nat.Bitwise.266_0.OFUBkIQvV236FCW
theorem bodd_bit (b n) : bodd (bit b n) = b
Mathlib_Init_Data_Nat_Bitwise
case true.false n : β„• ⊒ bodd (bif true then 1 else 0) = true
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;>
rfl
theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;>
Mathlib.Init.Data.Nat.Bitwise.266_0.OFUBkIQvV236FCW
theorem bodd_bit (b n) : bodd (bit b n) = b
Mathlib_Init_Data_Nat_Bitwise
case true.true n : β„• ⊒ bodd (bif true then 1 else 0) = true
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;>
rfl
theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;>
Mathlib.Init.Data.Nat.Bitwise.266_0.OFUBkIQvV236FCW
theorem bodd_bit (b n) : bodd (bit b n) = b
Mathlib_Init_Data_Nat_Bitwise
b : Bool n : β„• ⊒ div2 (bit b n) = n
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by
rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add]
theorem div2_bit (b n) : div2 (bit b n) = n := by
Mathlib.Init.Data.Nat.Bitwise.273_0.OFUBkIQvV236FCW
theorem div2_bit (b n) : div2 (bit b n) = n
Mathlib_Init_Data_Nat_Bitwise
b : Bool n : β„• ⊒ (bif b then 1 else 0) < 2
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;>
cases b
theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;>
Mathlib.Init.Data.Nat.Bitwise.273_0.OFUBkIQvV236FCW
theorem div2_bit (b n) : div2 (bit b n) = n
Mathlib_Init_Data_Nat_Bitwise
case H b : Bool n : β„• ⊒ 0 < 2
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;>
cases b
theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;>
Mathlib.Init.Data.Nat.Bitwise.273_0.OFUBkIQvV236FCW
theorem div2_bit (b n) : div2 (bit b n) = n
Mathlib_Init_Data_Nat_Bitwise
case false n : β„• ⊒ (bif false then 1 else 0) < 2
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;>
exact by decide
theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;>
Mathlib.Init.Data.Nat.Bitwise.273_0.OFUBkIQvV236FCW
theorem div2_bit (b n) : div2 (bit b n) = n
Mathlib_Init_Data_Nat_Bitwise
n : β„• ⊒ (bif false then 1 else 0) < 2
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by
decide
theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by
Mathlib.Init.Data.Nat.Bitwise.273_0.OFUBkIQvV236FCW
theorem div2_bit (b n) : div2 (bit b n) = n
Mathlib_Init_Data_Nat_Bitwise
case true n : β„• ⊒ (bif true then 1 else 0) < 2
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;>
exact by decide
theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;>
Mathlib.Init.Data.Nat.Bitwise.273_0.OFUBkIQvV236FCW
theorem div2_bit (b n) : div2 (bit b n) = n
Mathlib_Init_Data_Nat_Bitwise
n : β„• ⊒ (bif true then 1 else 0) < 2
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by
decide
theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by
Mathlib.Init.Data.Nat.Bitwise.273_0.OFUBkIQvV236FCW
theorem div2_bit (b n) : div2 (bit b n) = n
Mathlib_Init_Data_Nat_Bitwise
case H.false n : β„• ⊒ 0 < 2
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;>
exact by decide
theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;>
Mathlib.Init.Data.Nat.Bitwise.273_0.OFUBkIQvV236FCW
theorem div2_bit (b n) : div2 (bit b n) = n
Mathlib_Init_Data_Nat_Bitwise
n : β„• ⊒ 0 < 2
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by
decide
theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by
Mathlib.Init.Data.Nat.Bitwise.273_0.OFUBkIQvV236FCW
theorem div2_bit (b n) : div2 (bit b n) = n
Mathlib_Init_Data_Nat_Bitwise
case H.true n : β„• ⊒ 0 < 2
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;>
exact by decide
theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;>
Mathlib.Init.Data.Nat.Bitwise.273_0.OFUBkIQvV236FCW
theorem div2_bit (b n) : div2 (bit b n) = n
Mathlib_Init_Data_Nat_Bitwise
n : β„• ⊒ 0 < 2
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by
decide
theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by
Mathlib.Init.Data.Nat.Bitwise.273_0.OFUBkIQvV236FCW
theorem div2_bit (b n) : div2 (bit b n) = n
Mathlib_Init_Data_Nat_Bitwise
m n : β„• ⊒ βˆ€ (k : β„•), m <<< (n + k) = m <<< n <<< k
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by
intro k
theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by
Mathlib.Init.Data.Nat.Bitwise.284_0.OFUBkIQvV236FCW
theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k
Mathlib_Init_Data_Nat_Bitwise
m n k : β„• ⊒ m <<< (n + k) = m <<< n <<< k
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k;
simp only [← shiftLeft'_false, shiftLeft'_add]
theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k;
Mathlib.Init.Data.Nat.Bitwise.284_0.OFUBkIQvV236FCW
theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k
Mathlib_Init_Data_Nat_Bitwise
b : Bool m n k : β„• h : k + 1 ≀ n + 1 ⊒ shiftLeft' b m (n + 1 - (k + 1)) = shiftLeft' b m (n + 1) >>> (k + 1)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by
rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add]
theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by
Mathlib.Init.Data.Nat.Bitwise.287_0.OFUBkIQvV236FCW
theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit]
Mathlib_Init_Data_Nat_Bitwise
b : Bool m n k : β„• h : k + 1 ≀ n + 1 ⊒ shiftLeft' b m (n - k) = bit b (shiftLeft' b m n) >>> 1 >>> k
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add]
simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero]
theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add]
Mathlib.Init.Data.Nat.Bitwise.287_0.OFUBkIQvV236FCW
theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit]
Mathlib_Init_Data_Nat_Bitwise
b : Bool m n k : β„• h : k + 1 ≀ n + 1 ⊒ shiftLeft' b m n >>> k = (bit b (shiftLeft' b m n) / 2) >>> k
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero]
simp [← div2_val, div2_bit]
theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero]
Mathlib.Init.Data.Nat.Bitwise.287_0.OFUBkIQvV236FCW
theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit]
Mathlib_Init_Data_Nat_Bitwise
x✝² x✝¹ x✝ : β„• hk : x✝ ≀ x✝¹ ⊒ x✝² <<< (x✝¹ - x✝) = x✝² <<< x✝¹ >>> x✝
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by
simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk]
theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by
Mathlib.Init.Data.Nat.Bitwise.295_0.OFUBkIQvV236FCW
theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k
Mathlib_Init_Data_Nat_Bitwise
b : Bool n : β„• ⊒ testBit (bit b n) 0 = b
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by
rw [testBit, bit]
@[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by
Mathlib.Init.Data.Nat.Bitwise.298_0.OFUBkIQvV236FCW
@[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b
Mathlib_Init_Data_Nat_Bitwise
b : Bool n : β„• ⊒ (cond b bit1 bit0 n >>> 0 &&& 1 != 0) = b
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit]
cases b
@[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit]
Mathlib.Init.Data.Nat.Bitwise.298_0.OFUBkIQvV236FCW
@[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b
Mathlib_Init_Data_Nat_Bitwise
case false n : β„• ⊒ (cond false bit1 bit0 n >>> 0 &&& 1 != 0) = false
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β·
simp [bit0, ← Nat.mul_two]
@[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β·
Mathlib.Init.Data.Nat.Bitwise.298_0.OFUBkIQvV236FCW
@[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b
Mathlib_Init_Data_Nat_Bitwise
case true n : β„• ⊒ (cond true bit1 bit0 n >>> 0 &&& 1 != 0) = true
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β·
simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne]
@[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β·
Mathlib.Init.Data.Nat.Bitwise.298_0.OFUBkIQvV236FCW
@[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b
Mathlib_Init_Data_Nat_Bitwise
case true n : β„• ⊒ (n + n + 1) % 2 β‰  0
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne]
simp only [← Nat.mul_two]
@[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne]
Mathlib.Init.Data.Nat.Bitwise.298_0.OFUBkIQvV236FCW
@[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b
Mathlib_Init_Data_Nat_Bitwise
case true n : β„• ⊒ (n * 2 + 1) % 2 β‰  0
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two]
rw [Nat.add_mod]
@[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two]
Mathlib.Init.Data.Nat.Bitwise.298_0.OFUBkIQvV236FCW
@[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b
Mathlib_Init_Data_Nat_Bitwise
case true n : β„• ⊒ (n * 2 % 2 + 1 % 2) % 2 β‰  0
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod]
simp
@[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod]
Mathlib.Init.Data.Nat.Bitwise.298_0.OFUBkIQvV236FCW
@[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b
Mathlib_Init_Data_Nat_Bitwise
n : β„• ⊒ bodd (n + 2) = (n + 2 &&& 1 != 0)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by
simpa using bodd_eq_and_one_ne_zero n
theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by
Mathlib.Init.Data.Nat.Bitwise.310_0.OFUBkIQvV236FCW
theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n
Mathlib_Init_Data_Nat_Bitwise
m : β„• b : Bool n : β„• ⊒ testBit (bit b n) (succ m) = testBit n m
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by
have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit]
theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by
Mathlib.Init.Data.Nat.Bitwise.315_0.OFUBkIQvV236FCW
theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m
Mathlib_Init_Data_Nat_Bitwise
m : β„• b : Bool n : β„• ⊒ bodd (bit b n >>> 1 >>> m) = bodd (n >>> m)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by
simp only [shiftRight_eq_div_pow]
theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by
Mathlib.Init.Data.Nat.Bitwise.315_0.OFUBkIQvV236FCW
theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m
Mathlib_Init_Data_Nat_Bitwise
m : β„• b : Bool n : β„• ⊒ bodd (bit b n / 2 ^ 1 / 2 ^ m) = bodd (n / 2 ^ m)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow]
simp [← div2_val, div2_bit]
theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow]
Mathlib.Init.Data.Nat.Bitwise.315_0.OFUBkIQvV236FCW
theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m
Mathlib_Init_Data_Nat_Bitwise
m : β„• b : Bool n : β„• this : bodd (bit b n >>> 1 >>> m) = bodd (n >>> m) ⊒ testBit (bit b n) (succ m) = testBit n m
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit]
rw [← shiftRight_add, Nat.add_comm] at this
theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit]
Mathlib.Init.Data.Nat.Bitwise.315_0.OFUBkIQvV236FCW
theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m
Mathlib_Init_Data_Nat_Bitwise
m : β„• b : Bool n : β„• this : bodd (bit b n >>> (m + 1)) = bodd (n >>> m) ⊒ testBit (bit b n) (succ m) = testBit n m
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this
simp only [bodd_eq_and_one_ne_zero] at this
theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this
Mathlib.Init.Data.Nat.Bitwise.315_0.OFUBkIQvV236FCW
theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m
Mathlib_Init_Data_Nat_Bitwise
m : β„• b : Bool n : β„• this : (bit b n >>> (m + 1) &&& 1 != 0) = (n >>> m &&& 1 != 0) ⊒ testBit (bit b n) (succ m) = testBit n m
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this
exact this
theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this
Mathlib.Init.Data.Nat.Bitwise.315_0.OFUBkIQvV236FCW
theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h : f false 0 z = z b : Bool n : β„• ⊒ binaryRec z f (bit b n) = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by
rw [binaryRec]
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h : f false 0 z = z b : Bool n : β„• ⊒ (if n0 : bit b n = 0 then Eq.mpr (_ : C (bit b n) = C 0) z else let n' := div2 (bit b n); let_fun _x := (_ : bit (bodd (bit b n)) (div2 (bit b n)) = bit b n); Eq.mpr (_ : C (bit b n) = C (bit (bodd (bit b n)) n')) (f (bodd (bit b n)) n' (binaryRec z f n'))) = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec]
by_cases h : bit b n = 0
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec]
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
case pos C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h✝ : f false 0 z = z b : Bool n : β„• h : bit b n = 0 ⊒ (if n0 : bit b n = 0 then Eq.mpr (_ : C (bit b n) = C 0) z else let n' := div2 (bit b n); let_fun _x := (_ : bit (bodd (bit b n)) (div2 (bit b n)) = bit b n); Eq.mpr (_ : C (bit b n) = C (bit (bodd (bit b n)) n')) (f (bodd (bit b n)) n' (binaryRec z f n'))) = f b n (binaryRec z f n) case neg C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h✝ : f false 0 z = z b : Bool n : β„• h : Β¬bit b n = 0 ⊒ (if n0 : bit b n = 0 then Eq.mpr (_ : C (bit b n) = C 0) z else let n' := div2 (bit b n); let_fun _x := (_ : bit (bodd (bit b n)) (div2 (bit b n)) = bit b n); Eq.mpr (_ : C (bit b n) = C (bit (bodd (bit b n)) n')) (f (bodd (bit b n)) n' (binaryRec z f n'))) = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0`
case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h'] rfl
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0`
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z b : Bool n : β„• h : bit b n = 0 ⊒ (if n0 : bit b n = 0 then Eq.mpr (_ : C (bit b n) = C 0) z else let n' := div2 (bit b n); let_fun _x := (_ : bit (bodd (bit b n)) (div2 (bit b n)) = bit b n); Eq.mpr (_ : C (bit b n) = C (bit (bodd (bit b n)) n')) (f (bodd (bit b n)) n' (binaryRec z f n'))) = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0`
case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h'] rfl
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0`
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z b : Bool n : β„• h : bit b n = 0 ⊒ (if n0 : bit b n = 0 then Eq.mpr (_ : C (bit b n) = C 0) z else let n' := div2 (bit b n); let_fun _x := (_ : bit (bodd (bit b n)) (div2 (bit b n)) = bit b n); Eq.mpr (_ : C (bit b n) = C (bit (bodd (bit b n)) n')) (f (bodd (bit b n)) n' (binaryRec z f n'))) = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' =>
simp only [dif_pos h]
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' =>
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z b : Bool n : β„• h : bit b n = 0 ⊒ Eq.mpr (_ : C (bit b n) = C 0) z = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h]
generalize binaryRec z f (bit b n) = e
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h]
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z b : Bool n : β„• h : bit b n = 0 e : C (bit b n) ⊒ Eq.mpr (_ : C (bit b n) = C 0) z = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e
revert e
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z b : Bool n : β„• h : bit b n = 0 ⊒ C (bit b n) β†’ Eq.mpr (_ : C (bit b n) = C 0) z = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e
have bf := bodd_bit b n
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z b : Bool n : β„• h : bit b n = 0 bf : bodd (bit b n) = b ⊒ C (bit b n) β†’ Eq.mpr (_ : C (bit b n) = C 0) z = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n
have n0 := div2_bit b n
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z b : Bool n : β„• h : bit b n = 0 bf : bodd (bit b n) = b n0 : div2 (bit b n) = n ⊒ C (bit b n) β†’ Eq.mpr (_ : C (bit b n) = C 0) z = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n
rw [h] at bf n0
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z b : Bool n : β„• h : bit b n = 0 bf : bodd 0 = b n0 : div2 0 = n ⊒ C (bit b n) β†’ Eq.mpr (_ : C (bit b n) = C 0) z = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0
simp only [bodd_zero, div2_zero] at bf n0
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z b : Bool n : β„• h : bit b n = 0 bf : false = b n0 : 0 = n ⊒ C (bit b n) β†’ Eq.mpr (_ : C (bit b n) = C 0) z = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0
subst bf n0
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z h : bit false 0 = 0 ⊒ C (bit false 0) β†’ Eq.mpr (_ : C (bit false 0) = C 0) z = f false 0 (binaryRec z f 0)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0
rw [binaryRec_zero]
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z h : bit false 0 = 0 ⊒ C (bit false 0) β†’ Eq.mpr (_ : C (bit false 0) = C 0) z = f false 0 z
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero]
intros
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero]
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z h : bit false 0 = 0 e✝ : C (bit false 0) ⊒ Eq.mpr (_ : C (bit false 0) = C 0) z = f false 0 z
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros
rw [h']
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z h : bit false 0 = 0 e✝ : C (bit false 0) ⊒ Eq.mpr (_ : C (bit false 0) = C 0) z = z
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h']
rfl
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h']
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
case neg C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h✝ : f false 0 z = z b : Bool n : β„• h : Β¬bit b n = 0 ⊒ (if n0 : bit b n = 0 then Eq.mpr (_ : C (bit b n) = C 0) z else let n' := div2 (bit b n); let_fun _x := (_ : bit (bodd (bit b n)) (div2 (bit b n)) = bit b n); Eq.mpr (_ : C (bit b n) = C (bit (bodd (bit b n)) n')) (f (bodd (bit b n)) n' (binaryRec z f n'))) = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h'] rfl
case neg h' => simp only [dif_neg h] generalize @id (C (bit b n) = C (bit (bodd (bit b n)) (div2 (bit b n)))) (Eq.symm (bit_decomp (bit b n)) β–Έ Eq.refl (C (bit b n))) = e revert e rw [bodd_bit, div2_bit] intros; rfl
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h'] rfl
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z b : Bool n : β„• h : Β¬bit b n = 0 ⊒ (if n0 : bit b n = 0 then Eq.mpr (_ : C (bit b n) = C 0) z else let n' := div2 (bit b n); let_fun _x := (_ : bit (bodd (bit b n)) (div2 (bit b n)) = bit b n); Eq.mpr (_ : C (bit b n) = C (bit (bodd (bit b n)) n')) (f (bodd (bit b n)) n' (binaryRec z f n'))) = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h'] rfl
case neg h' => simp only [dif_neg h] generalize @id (C (bit b n) = C (bit (bodd (bit b n)) (div2 (bit b n)))) (Eq.symm (bit_decomp (bit b n)) β–Έ Eq.refl (C (bit b n))) = e revert e rw [bodd_bit, div2_bit] intros; rfl
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h'] rfl
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z b : Bool n : β„• h : Β¬bit b n = 0 ⊒ (if n0 : bit b n = 0 then Eq.mpr (_ : C (bit b n) = C 0) z else let n' := div2 (bit b n); let_fun _x := (_ : bit (bodd (bit b n)) (div2 (bit b n)) = bit b n); Eq.mpr (_ : C (bit b n) = C (bit (bodd (bit b n)) n')) (f (bodd (bit b n)) n' (binaryRec z f n'))) = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h'] rfl case neg h' =>
simp only [dif_neg h]
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h'] rfl case neg h' =>
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z b : Bool n : β„• h : Β¬bit b n = 0 ⊒ Eq.mpr (_ : C (bit b n) = C (bit (bodd (bit b n)) (div2 (bit b n)))) (f (bodd (bit b n)) (div2 (bit b n)) (binaryRec z f (div2 (bit b n)))) = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h'] rfl case neg h' => simp only [dif_neg h]
generalize @id (C (bit b n) = C (bit (bodd (bit b n)) (div2 (bit b n)))) (Eq.symm (bit_decomp (bit b n)) β–Έ Eq.refl (C (bit b n))) = e
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h'] rfl case neg h' => simp only [dif_neg h]
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z b : Bool n : β„• h : Β¬bit b n = 0 e : C (bit b n) = C (bit (bodd (bit b n)) (div2 (bit b n))) ⊒ Eq.mpr e (f (bodd (bit b n)) (div2 (bit b n)) (binaryRec z f (div2 (bit b n)))) = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h'] rfl case neg h' => simp only [dif_neg h] generalize @id (C (bit b n) = C (bit (bodd (bit b n)) (div2 (bit b n)))) (Eq.symm (bit_decomp (bit b n)) β–Έ Eq.refl (C (bit b n))) = e
revert e
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h'] rfl case neg h' => simp only [dif_neg h] generalize @id (C (bit b n) = C (bit (bodd (bit b n)) (div2 (bit b n)))) (Eq.symm (bit_decomp (bit b n)) β–Έ Eq.refl (C (bit b n))) = e
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z b : Bool n : β„• h : Β¬bit b n = 0 ⊒ βˆ€ (e : C (bit b n) = C (bit (bodd (bit b n)) (div2 (bit b n)))), Eq.mpr e (f (bodd (bit b n)) (div2 (bit b n)) (binaryRec z f (div2 (bit b n)))) = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h'] rfl case neg h' => simp only [dif_neg h] generalize @id (C (bit b n) = C (bit (bodd (bit b n)) (div2 (bit b n)))) (Eq.symm (bit_decomp (bit b n)) β–Έ Eq.refl (C (bit b n))) = e revert e
rw [bodd_bit, div2_bit]
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h'] rfl case neg h' => simp only [dif_neg h] generalize @id (C (bit b n) = C (bit (bodd (bit b n)) (div2 (bit b n)))) (Eq.symm (bit_decomp (bit b n)) β–Έ Eq.refl (C (bit b n))) = e revert e
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z b : Bool n : β„• h : Β¬bit b n = 0 ⊒ βˆ€ (e : C (bit b n) = C (bit b n)), Eq.mpr e (f b n (binaryRec z f n)) = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h'] rfl case neg h' => simp only [dif_neg h] generalize @id (C (bit b n) = C (bit (bodd (bit b n)) (div2 (bit b n)))) (Eq.symm (bit_decomp (bit b n)) β–Έ Eq.refl (C (bit b n))) = e revert e rw [bodd_bit, div2_bit]
intros
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h'] rfl case neg h' => simp only [dif_neg h] generalize @id (C (bit b n) = C (bit (bodd (bit b n)) (div2 (bit b n)))) (Eq.symm (bit_decomp (bit b n)) β–Έ Eq.refl (C (bit b n))) = e revert e rw [bodd_bit, div2_bit]
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
C : β„• β†’ Sort u z : C 0 f : (b : Bool) β†’ (n : β„•) β†’ C n β†’ C (bit b n) h' : f false 0 z = z b : Bool n : β„• h : Β¬bit b n = 0 e✝ : C (bit b n) = C (bit b n) ⊒ Eq.mpr e✝ (f b n (binaryRec z f n)) = f b n (binaryRec z f n)
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Init.Data.Nat.Lemmas import Init.WFTactics import Mathlib.Data.Bool.Basic import Mathlib.Init.Data.Bool.Lemmas import Mathlib.Init.ZeroOne import Mathlib.Tactic.Cases import Mathlib.Tactic.Says #align_import init.data.nat.bitwise from "leanprover-community/lean"@"53e8520d8964c7632989880372d91ba0cecbaf00" /-! # Lemmas about bitwise operations on natural numbers. Possibly only of archaeological significance. -/ set_option autoImplicit true universe u -- Once we're in the `Nat` namespace, `xor` will inconveniently resolve to `Nat.xor`. /-- `bxor` denotes the `xor` function i.e. the exclusive-or function on type `Bool`. -/ local notation "bxor" => _root_.xor namespace Nat set_option linter.deprecated false /-- `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βŒ‹` -/ def boddDiv2 : β„• β†’ Bool Γ— β„• | 0 => (false, 0) | succ n => match boddDiv2 n with | (false, m) => (true, m) | (true, m) => (false, succ m) #align nat.bodd_div2 Nat.boddDiv2 /-- `div2 n = ⌊n/2βŒ‹` the greatest integer smaller than `n/2`-/ def div2 (n : β„•) : β„• := (boddDiv2 n).2 #align nat.div2 Nat.div2 /-- `bodd n` returns `true` if `n` is odd-/ def bodd (n : β„•) : Bool := (boddDiv2 n).1 #align nat.bodd Nat.bodd @[simp] theorem bodd_zero : bodd 0 = false := rfl #align nat.bodd_zero Nat.bodd_zero theorem bodd_one : bodd 1 = true := rfl #align nat.bodd_one Nat.bodd_one theorem bodd_two : bodd 2 = false := rfl #align nat.bodd_two Nat.bodd_two @[simp] theorem bodd_succ (n : β„•) : bodd (succ n) = not (bodd n) := by simp only [bodd, boddDiv2] let ⟨b,m⟩ := boddDiv2 n cases b <;> rfl #align nat.bodd_succ Nat.bodd_succ @[simp] theorem bodd_add (m n : β„•) : bodd (m + n) = bxor (bodd m) (bodd n) := by induction n <;> simp_all [add_succ, Bool.xor_not] #align nat.bodd_add Nat.bodd_add @[simp] theorem bodd_mul (m n : β„•) : bodd (m * n) = (bodd m && bodd n) := by induction' n with n IH Β· simp Β· simp [mul_succ, IH] cases bodd m <;> cases bodd n <;> rfl #align nat.bodd_mul Nat.bodd_mul theorem mod_two_of_bodd (n : β„•) : n % 2 = cond (bodd n) 1 0 := by have := congr_arg bodd (mod_add_div n 2) simp? [not] at this says simp only [bodd_add, bodd_mul, bodd_succ, not, bodd_zero, Bool.false_and, Bool.xor_false] at this have _ : βˆ€ b, and false b = false := by intro b cases b <;> rfl have _ : βˆ€ b, bxor b false = b := by intro b cases b <;> rfl rw [← this] cases' mod_two_eq_zero_or_one n with h h <;> rw [h] <;> rfl #align nat.mod_two_of_bodd Nat.mod_two_of_bodd @[simp] theorem div2_zero : div2 0 = 0 := rfl #align nat.div2_zero Nat.div2_zero theorem div2_one : div2 1 = 0 := rfl #align nat.div2_one Nat.div2_one theorem div2_two : div2 2 = 1 := rfl #align nat.div2_two Nat.div2_two @[simp] theorem div2_succ (n : β„•) : div2 (succ n) = cond (bodd n) (succ (div2 n)) (div2 n) := by simp only [bodd, boddDiv2, div2] cases' boddDiv2 n with fst snd cases fst case mk.false => simp case mk.true => simp #align nat.div2_succ Nat.div2_succ attribute [local simp] Nat.add_comm Nat.add_assoc Nat.add_left_comm Nat.mul_comm Nat.mul_assoc theorem bodd_add_div2 : βˆ€ n, cond (bodd n) 1 0 + 2 * div2 n = n | 0 => rfl | succ n => by simp only [bodd_succ, Bool.cond_not, div2_succ, Nat.mul_comm] refine' Eq.trans _ (congr_arg succ (bodd_add_div2 n)) cases bodd n <;> simp [cond, not] Β· rw [Nat.add_comm, Nat.add_succ] Β· rw [succ_mul, Nat.add_comm 1, Nat.add_succ] #align nat.bodd_add_div2 Nat.bodd_add_div2 theorem div2_val (n) : div2 n = n / 2 := by refine' Nat.eq_of_mul_eq_mul_left (by decide) (Nat.add_left_cancel (Eq.trans _ (Nat.mod_add_div n 2).symm)) rw [mod_two_of_bodd, bodd_add_div2] #align nat.div2_val Nat.div2_val /-- `bit b` appends the digit `b` to the binary representation of its natural number input. -/ def bit (b : Bool) : β„• β†’ β„• := cond b bit1 bit0 #align nat.bit Nat.bit theorem bit0_val (n : Nat) : bit0 n = 2 * n := calc n + n = 0 + n + n := by rw [Nat.zero_add] _ = n * 2 := rfl _ = 2 * n := Nat.mul_comm _ _ #align nat.bit0_val Nat.bit0_val theorem bit1_val (n : Nat) : bit1 n = 2 * n + 1 := congr_arg succ (bit0_val _) #align nat.bit1_val Nat.bit1_val theorem bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by cases b apply bit0_val apply bit1_val #align nat.bit_val Nat.bit_val theorem bit_decomp (n : Nat) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans <| (Nat.add_comm _ _).trans <| bodd_add_div2 _ #align nat.bit_decomp Nat.bit_decomp /-- For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for any given natural number. -/ def bitCasesOn {C : Nat β†’ Sort u} (n) (h : βˆ€ b n, C (bit b n)) : C n := bit_decomp n β–Έ h _ _ #align nat.bit_cases_on Nat.bitCasesOn theorem bit_zero : bit false 0 = 0 := rfl #align nat.bit_zero Nat.bit_zero /--`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-/ def shiftLeft' (b : Bool) (m : β„•) : β„• β†’ β„• | 0 => m | n + 1 => bit b (shiftLeft' b m n) #align nat.shiftl' Nat.shiftLeft' @[simp] theorem shiftLeft'_false : βˆ€ n, shiftLeft' false m n = m <<< n | 0 => rfl | n + 1 => by have : 2 * (m * 2^n) = 2^(n+1)*m := by rw [Nat.mul_comm, Nat.mul_assoc, ← pow_succ]; simp simp [shiftLeft_eq, shiftLeft', bit_val, shiftLeft'_false, this] /-- Std4 takes the unprimed name for `Nat.shiftLeft_eq m n : m <<< n = m * 2 ^ n`. -/ @[simp] lemma shiftLeft_eq' (m n : Nat) : shiftLeft m n = m <<< n := rfl @[simp] lemma shiftRight_eq (m n : Nat) : shiftRight m n = m >>> n := rfl #align nat.test_bit Nat.testBit lemma binaryRec_decreasing (h : n β‰  0) : div2 n < n := by rw [div2_val] apply (div_lt_iff_lt_mul <| succ_pos 1).2 have := Nat.mul_lt_mul_of_pos_left (lt_succ_self 1) (lt_of_le_of_ne n.zero_le h.symm) rwa [Nat.mul_one] at this /-- A recursion principle for `bit` representations of natural numbers. For a predicate `C : Nat β†’ Sort*`, if instances can be constructed for natural numbers of the form `bit b n`, they can be constructed for all natural numbers. -/ def binaryRec {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : βˆ€ n, C n := fun n => if n0 : n = 0 then by simp only [n0] exact z else by let n' := div2 n have _x : bit (bodd n) n' = n := by apply bit_decomp n rw [← _x] exact f (bodd n) n' (binaryRec z f n') decreasing_by exact binaryRec_decreasing n0 #align nat.binary_rec Nat.binaryRec /-- `size n` : Returns the size of a natural number in bits i.e. the length of its binary representation -/ def size : β„• β†’ β„• := binaryRec 0 fun _ _ => succ #align nat.size Nat.size /-- `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 bits : β„• β†’ List Bool := binaryRec [] fun b _ IH => b :: IH #align nat.bits Nat.bits #align nat.bitwise Nat.bitwise #align nat.lor Nat.lor #align nat.land Nat.land #align nat.lxor Nat.xor /--`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.-/ def ldiff : β„• β†’ β„• β†’ β„• := bitwise fun a b => a && not b #align nat.ldiff Nat.ldiff @[simp] theorem binaryRec_zero {C : Nat β†’ Sort u} (z : C 0) (f : βˆ€ b n, C n β†’ C (bit b n)) : binaryRec z f 0 = z := by rw [binaryRec] rfl #align nat.binary_rec_zero Nat.binaryRec_zero /-! bitwise ops -/ theorem bodd_bit (b n) : bodd (bit b n) = b := by rw [bit_val] simp only [Nat.mul_comm, Nat.add_comm, bodd_add, bodd_mul, bodd_succ, bodd_zero, Bool.not_false, Bool.not_true, Bool.and_false, Bool.xor_false] cases b <;> cases bodd n <;> rfl #align nat.bodd_bit Nat.bodd_bit theorem div2_bit (b n) : div2 (bit b n) = n := by rw [bit_val, div2_val, Nat.add_comm, add_mul_div_left, div_eq_of_lt, Nat.zero_add] <;> cases b <;> exact by decide #align nat.div2_bit Nat.div2_bit theorem shiftLeft'_add (b m n) : βˆ€ k, shiftLeft' b m (n + k) = shiftLeft' b (shiftLeft' b m n) k | 0 => rfl | k + 1 => congr_arg (bit b) (shiftLeft'_add b m n k) #align nat.shiftl'_add Nat.shiftLeft'_add theorem shiftLeft_add (m n : Nat) : βˆ€ k, m <<< (n + k) = (m <<< n) <<< k := by intro k; simp only [← shiftLeft'_false, shiftLeft'_add] theorem shiftLeft'_sub (b m) : βˆ€ {n k}, k ≀ n β†’ shiftLeft' b m (n - k) = (shiftLeft' b m n) >>> k | n, 0, _ => rfl | n + 1, k + 1, h => by rw [succ_sub_succ_eq_sub, shiftLeft', Nat.add_comm, shiftRight_add] simp only [shiftLeft'_sub, Nat.le_of_succ_le_succ h, shiftRight_succ, shiftRight_zero] simp [← div2_val, div2_bit] #align nat.shiftl'_sub Nat.shiftLeft'_sub theorem shiftLeft_sub : βˆ€ (m : Nat) {n k}, k ≀ n β†’ m <<< (n - k) = (m <<< n) >>> k := fun _ _ _ hk => by simp only [← shiftLeft'_false, shiftLeft'_sub false _ hk] @[simp] theorem testBit_zero (b n) : testBit (bit b n) 0 = b := by rw [testBit, bit] cases b Β· simp [bit0, ← Nat.mul_two] Β· simp only [cond_true, bit1, bit0, shiftRight_zero, and_one_is_mod, bne_iff_ne] simp only [← Nat.mul_two] rw [Nat.add_mod] simp #align nat.test_bit_zero Nat.testBit_zero theorem bodd_eq_and_one_ne_zero : βˆ€ n, bodd n = (n &&& 1 != 0) | 0 => rfl | 1 => rfl | n + 2 => by simpa using bodd_eq_and_one_ne_zero n theorem testBit_succ (m b n) : testBit (bit b n) (succ m) = testBit n m := by have : bodd (((bit b n) >>> 1) >>> m) = bodd (n >>> m) := by simp only [shiftRight_eq_div_pow] simp [← div2_val, div2_bit] rw [← shiftRight_add, Nat.add_comm] at this simp only [bodd_eq_and_one_ne_zero] at this exact this #align nat.test_bit_succ Nat.testBit_succ theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h'] rfl case neg h' => simp only [dif_neg h] generalize @id (C (bit b n) = C (bit (bodd (bit b n)) (div2 (bit b n)))) (Eq.symm (bit_decomp (bit b n)) β–Έ Eq.refl (C (bit b n))) = e revert e rw [bodd_bit, div2_bit] intros;
rfl
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n) := by rw [binaryRec] by_cases h : bit b n = 0 -- Note: this renames the original `h : f false 0 z = z` to `h'` and leaves `h : bit b n = 0` case pos h' => simp only [dif_pos h] generalize binaryRec z f (bit b n) = e revert e have bf := bodd_bit b n have n0 := div2_bit b n rw [h] at bf n0 simp only [bodd_zero, div2_zero] at bf n0 subst bf n0 rw [binaryRec_zero] intros rw [h'] rfl case neg h' => simp only [dif_neg h] generalize @id (C (bit b n) = C (bit (bodd (bit b n)) (div2 (bit b n)))) (Eq.symm (bit_decomp (bit b n)) β–Έ Eq.refl (C (bit b n))) = e revert e rw [bodd_bit, div2_bit] intros;
Mathlib.Init.Data.Nat.Bitwise.324_0.OFUBkIQvV236FCW
theorem binaryRec_eq {C : Nat β†’ Sort u} {z : C 0} {f : βˆ€ b n, C n β†’ C (bit b n)} (h : f false 0 z = z) (b n) : binaryRec z f (bit b n) = f b n (binaryRec z f n)
Mathlib_Init_Data_Nat_Bitwise
α✝ : Type u Ξ² : Type v Ξ± : Type u_1 inst✝ : Sup Ξ± sup_comm : βˆ€ (a b : Ξ±), a βŠ” b = b βŠ” a sup_assoc : βˆ€ (a b c : Ξ±), a βŠ” b βŠ” c = a βŠ” (b βŠ” c) sup_idem : βˆ€ (a : Ξ±), a βŠ” a = a a b c : Ξ± hab : a ≀ b hbc : b ≀ c ⊒ a ≀ c
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl -/ import Mathlib.Data.Bool.Basic import Mathlib.Init.Order.Defs import Mathlib.Order.Monotone.Basic import Mathlib.Order.ULift import Mathlib.Tactic.GCongr.Core #align_import order.lattice from "leanprover-community/mathlib"@"3ba15165bd6927679be7c22d6091a87337e3cd0c" /-! # (Semi-)lattices Semilattices are partially ordered sets with join (greatest lower bound, or `sup`) or meet (least upper bound, or `inf`) operations. Lattices are posets that are both join-semilattices and meet-semilattices. Distributive lattices are lattices which satisfy any of four equivalent distributivity properties, of `sup` over `inf`, on the left or on the right. ## Main declarations * `SemilatticeSup`: a type class for join semilattices * `SemilatticeSup.mk'`: an alternative constructor for `SemilatticeSup` via proofs that `βŠ”` is commutative, associative and idempotent. * `SemilatticeInf`: a type class for meet semilattices * `SemilatticeSup.mk'`: an alternative constructor for `SemilatticeInf` via proofs that `βŠ“` is commutative, associative and idempotent. * `Lattice`: a type class for lattices * `Lattice.mk'`: an alternative constructor for `Lattice` via proofs that `βŠ”` and `βŠ“` are commutative, associative and satisfy a pair of "absorption laws". * `DistribLattice`: a type class for distributive lattices. ## Notations * `a βŠ” b`: the supremum or join of `a` and `b` * `a βŠ“ b`: the infimum or meet of `a` and `b` ## TODO * (Semi-)lattice homomorphisms * Alternative constructors for distributive lattices from the other distributive properties ## Tags semilattice, lattice -/ universe u v w variable {Ξ± : Type u} {Ξ² : Type v} #align le_antisymm' le_antisymm /-! ### Join-semilattices -/ -- TODO: automatic construction of dual definitions / theorems /-- A `SemilatticeSup` is a join-semilattice, that is, a partial order with a join (a.k.a. lub / least upper bound, sup / supremum) operation `βŠ”` which is the least element larger than both factors. -/ class SemilatticeSup (Ξ± : Type u) extends Sup Ξ±, PartialOrder Ξ± where /-- The supremum is an upper bound on the first argument -/ protected le_sup_left : βˆ€ a b : Ξ±, a ≀ a βŠ” b /-- The supremum is an upper bound on the second argument -/ protected le_sup_right : βˆ€ a b : Ξ±, b ≀ a βŠ” b /-- The supremum is the *least* upper bound -/ protected sup_le : βˆ€ a b c : Ξ±, a ≀ c β†’ b ≀ c β†’ a βŠ” b ≀ c #align semilattice_sup SemilatticeSup /-- A type with a commutative, associative and idempotent binary `sup` operation has the structure of a join-semilattice. The partial order is defined so that `a ≀ b` unfolds to `a βŠ” b = b`; cf. `sup_eq_right`. -/ def SemilatticeSup.mk' {Ξ± : Type*} [Sup Ξ±] (sup_comm : βˆ€ a b : Ξ±, a βŠ” b = b βŠ” a) (sup_assoc : βˆ€ a b c : Ξ±, a βŠ” b βŠ” c = a βŠ” (b βŠ” c)) (sup_idem : βˆ€ a : Ξ±, a βŠ” a = a) : SemilatticeSup Ξ± where sup := (Β· βŠ” Β·) le a b := a βŠ” b = b le_refl := sup_idem le_trans a b c hab hbc := by -- Porting note: dsimp doesn't work here? -- This is the same issue as discussed at https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/unfolding.20earlier.20fields
show a βŠ” c = c
/-- A type with a commutative, associative and idempotent binary `sup` operation has the structure of a join-semilattice. The partial order is defined so that `a ≀ b` unfolds to `a βŠ” b = b`; cf. `sup_eq_right`. -/ def SemilatticeSup.mk' {Ξ± : Type*} [Sup Ξ±] (sup_comm : βˆ€ a b : Ξ±, a βŠ” b = b βŠ” a) (sup_assoc : βˆ€ a b c : Ξ±, a βŠ” b βŠ” c = a βŠ” (b βŠ” c)) (sup_idem : βˆ€ a : Ξ±, a βŠ” a = a) : SemilatticeSup Ξ± where sup := (Β· βŠ” Β·) le a b := a βŠ” b = b le_refl := sup_idem le_trans a b c hab hbc := by -- Porting note: dsimp doesn't work here? -- This is the same issue as discussed at https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/unfolding.20earlier.20fields
Mathlib.Order.Lattice.80_0.wE3igZl9MFbJBfv
/-- A type with a commutative, associative and idempotent binary `sup` operation has the structure of a join-semilattice. The partial order is defined so that `a ≀ b` unfolds to `a βŠ” b = b`; cf. `sup_eq_right`. -/ def SemilatticeSup.mk' {Ξ± : Type*} [Sup Ξ±] (sup_comm : βˆ€ a b : Ξ±, a βŠ” b = b βŠ” a) (sup_assoc : βˆ€ a b c : Ξ±, a βŠ” b βŠ” c = a βŠ” (b βŠ” c)) (sup_idem : βˆ€ a : Ξ±, a βŠ” a = a) : SemilatticeSup Ξ± where sup
Mathlib_Order_Lattice
α✝ : Type u Ξ² : Type v Ξ± : Type u_1 inst✝ : Sup Ξ± sup_comm : βˆ€ (a b : Ξ±), a βŠ” b = b βŠ” a sup_assoc : βˆ€ (a b c : Ξ±), a βŠ” b βŠ” c = a βŠ” (b βŠ” c) sup_idem : βˆ€ (a : Ξ±), a βŠ” a = a a b c : Ξ± hab : a ≀ b hbc : b ≀ c ⊒ a βŠ” c = c
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl -/ import Mathlib.Data.Bool.Basic import Mathlib.Init.Order.Defs import Mathlib.Order.Monotone.Basic import Mathlib.Order.ULift import Mathlib.Tactic.GCongr.Core #align_import order.lattice from "leanprover-community/mathlib"@"3ba15165bd6927679be7c22d6091a87337e3cd0c" /-! # (Semi-)lattices Semilattices are partially ordered sets with join (greatest lower bound, or `sup`) or meet (least upper bound, or `inf`) operations. Lattices are posets that are both join-semilattices and meet-semilattices. Distributive lattices are lattices which satisfy any of four equivalent distributivity properties, of `sup` over `inf`, on the left or on the right. ## Main declarations * `SemilatticeSup`: a type class for join semilattices * `SemilatticeSup.mk'`: an alternative constructor for `SemilatticeSup` via proofs that `βŠ”` is commutative, associative and idempotent. * `SemilatticeInf`: a type class for meet semilattices * `SemilatticeSup.mk'`: an alternative constructor for `SemilatticeInf` via proofs that `βŠ“` is commutative, associative and idempotent. * `Lattice`: a type class for lattices * `Lattice.mk'`: an alternative constructor for `Lattice` via proofs that `βŠ”` and `βŠ“` are commutative, associative and satisfy a pair of "absorption laws". * `DistribLattice`: a type class for distributive lattices. ## Notations * `a βŠ” b`: the supremum or join of `a` and `b` * `a βŠ“ b`: the infimum or meet of `a` and `b` ## TODO * (Semi-)lattice homomorphisms * Alternative constructors for distributive lattices from the other distributive properties ## Tags semilattice, lattice -/ universe u v w variable {Ξ± : Type u} {Ξ² : Type v} #align le_antisymm' le_antisymm /-! ### Join-semilattices -/ -- TODO: automatic construction of dual definitions / theorems /-- A `SemilatticeSup` is a join-semilattice, that is, a partial order with a join (a.k.a. lub / least upper bound, sup / supremum) operation `βŠ”` which is the least element larger than both factors. -/ class SemilatticeSup (Ξ± : Type u) extends Sup Ξ±, PartialOrder Ξ± where /-- The supremum is an upper bound on the first argument -/ protected le_sup_left : βˆ€ a b : Ξ±, a ≀ a βŠ” b /-- The supremum is an upper bound on the second argument -/ protected le_sup_right : βˆ€ a b : Ξ±, b ≀ a βŠ” b /-- The supremum is the *least* upper bound -/ protected sup_le : βˆ€ a b c : Ξ±, a ≀ c β†’ b ≀ c β†’ a βŠ” b ≀ c #align semilattice_sup SemilatticeSup /-- A type with a commutative, associative and idempotent binary `sup` operation has the structure of a join-semilattice. The partial order is defined so that `a ≀ b` unfolds to `a βŠ” b = b`; cf. `sup_eq_right`. -/ def SemilatticeSup.mk' {Ξ± : Type*} [Sup Ξ±] (sup_comm : βˆ€ a b : Ξ±, a βŠ” b = b βŠ” a) (sup_assoc : βˆ€ a b c : Ξ±, a βŠ” b βŠ” c = a βŠ” (b βŠ” c)) (sup_idem : βˆ€ a : Ξ±, a βŠ” a = a) : SemilatticeSup Ξ± where sup := (Β· βŠ” Β·) le a b := a βŠ” b = b le_refl := sup_idem le_trans a b c hab hbc := by -- Porting note: dsimp doesn't work here? -- This is the same issue as discussed at https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/unfolding.20earlier.20fields show a βŠ” c = c
rw [← hbc, ← sup_assoc, hab]
/-- A type with a commutative, associative and idempotent binary `sup` operation has the structure of a join-semilattice. The partial order is defined so that `a ≀ b` unfolds to `a βŠ” b = b`; cf. `sup_eq_right`. -/ def SemilatticeSup.mk' {Ξ± : Type*} [Sup Ξ±] (sup_comm : βˆ€ a b : Ξ±, a βŠ” b = b βŠ” a) (sup_assoc : βˆ€ a b c : Ξ±, a βŠ” b βŠ” c = a βŠ” (b βŠ” c)) (sup_idem : βˆ€ a : Ξ±, a βŠ” a = a) : SemilatticeSup Ξ± where sup := (Β· βŠ” Β·) le a b := a βŠ” b = b le_refl := sup_idem le_trans a b c hab hbc := by -- Porting note: dsimp doesn't work here? -- This is the same issue as discussed at https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/unfolding.20earlier.20fields show a βŠ” c = c
Mathlib.Order.Lattice.80_0.wE3igZl9MFbJBfv
/-- A type with a commutative, associative and idempotent binary `sup` operation has the structure of a join-semilattice. The partial order is defined so that `a ≀ b` unfolds to `a βŠ” b = b`; cf. `sup_eq_right`. -/ def SemilatticeSup.mk' {Ξ± : Type*} [Sup Ξ±] (sup_comm : βˆ€ a b : Ξ±, a βŠ” b = b βŠ” a) (sup_assoc : βˆ€ a b c : Ξ±, a βŠ” b βŠ” c = a βŠ” (b βŠ” c)) (sup_idem : βˆ€ a : Ξ±, a βŠ” a = a) : SemilatticeSup Ξ± where sup
Mathlib_Order_Lattice
α✝ : Type u Ξ² : Type v Ξ± : Type u_1 inst✝ : Sup Ξ± sup_comm : βˆ€ (a b : Ξ±), a βŠ” b = b βŠ” a sup_assoc : βˆ€ (a b c : Ξ±), a βŠ” b βŠ” c = a βŠ” (b βŠ” c) sup_idem : βˆ€ (a : Ξ±), a βŠ” a = a a b : Ξ± hab : a ≀ b hba : b ≀ a ⊒ a = b
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl -/ import Mathlib.Data.Bool.Basic import Mathlib.Init.Order.Defs import Mathlib.Order.Monotone.Basic import Mathlib.Order.ULift import Mathlib.Tactic.GCongr.Core #align_import order.lattice from "leanprover-community/mathlib"@"3ba15165bd6927679be7c22d6091a87337e3cd0c" /-! # (Semi-)lattices Semilattices are partially ordered sets with join (greatest lower bound, or `sup`) or meet (least upper bound, or `inf`) operations. Lattices are posets that are both join-semilattices and meet-semilattices. Distributive lattices are lattices which satisfy any of four equivalent distributivity properties, of `sup` over `inf`, on the left or on the right. ## Main declarations * `SemilatticeSup`: a type class for join semilattices * `SemilatticeSup.mk'`: an alternative constructor for `SemilatticeSup` via proofs that `βŠ”` is commutative, associative and idempotent. * `SemilatticeInf`: a type class for meet semilattices * `SemilatticeSup.mk'`: an alternative constructor for `SemilatticeInf` via proofs that `βŠ“` is commutative, associative and idempotent. * `Lattice`: a type class for lattices * `Lattice.mk'`: an alternative constructor for `Lattice` via proofs that `βŠ”` and `βŠ“` are commutative, associative and satisfy a pair of "absorption laws". * `DistribLattice`: a type class for distributive lattices. ## Notations * `a βŠ” b`: the supremum or join of `a` and `b` * `a βŠ“ b`: the infimum or meet of `a` and `b` ## TODO * (Semi-)lattice homomorphisms * Alternative constructors for distributive lattices from the other distributive properties ## Tags semilattice, lattice -/ universe u v w variable {Ξ± : Type u} {Ξ² : Type v} #align le_antisymm' le_antisymm /-! ### Join-semilattices -/ -- TODO: automatic construction of dual definitions / theorems /-- A `SemilatticeSup` is a join-semilattice, that is, a partial order with a join (a.k.a. lub / least upper bound, sup / supremum) operation `βŠ”` which is the least element larger than both factors. -/ class SemilatticeSup (Ξ± : Type u) extends Sup Ξ±, PartialOrder Ξ± where /-- The supremum is an upper bound on the first argument -/ protected le_sup_left : βˆ€ a b : Ξ±, a ≀ a βŠ” b /-- The supremum is an upper bound on the second argument -/ protected le_sup_right : βˆ€ a b : Ξ±, b ≀ a βŠ” b /-- The supremum is the *least* upper bound -/ protected sup_le : βˆ€ a b c : Ξ±, a ≀ c β†’ b ≀ c β†’ a βŠ” b ≀ c #align semilattice_sup SemilatticeSup /-- A type with a commutative, associative and idempotent binary `sup` operation has the structure of a join-semilattice. The partial order is defined so that `a ≀ b` unfolds to `a βŠ” b = b`; cf. `sup_eq_right`. -/ def SemilatticeSup.mk' {Ξ± : Type*} [Sup Ξ±] (sup_comm : βˆ€ a b : Ξ±, a βŠ” b = b βŠ” a) (sup_assoc : βˆ€ a b c : Ξ±, a βŠ” b βŠ” c = a βŠ” (b βŠ” c)) (sup_idem : βˆ€ a : Ξ±, a βŠ” a = a) : SemilatticeSup Ξ± where sup := (Β· βŠ” Β·) le a b := a βŠ” b = b le_refl := sup_idem le_trans a b c hab hbc := by -- Porting note: dsimp doesn't work here? -- This is the same issue as discussed at https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/unfolding.20earlier.20fields show a βŠ” c = c rw [← hbc, ← sup_assoc, hab] le_antisymm a b hab hba := by
rwa [← hba, sup_comm]
/-- A type with a commutative, associative and idempotent binary `sup` operation has the structure of a join-semilattice. The partial order is defined so that `a ≀ b` unfolds to `a βŠ” b = b`; cf. `sup_eq_right`. -/ def SemilatticeSup.mk' {Ξ± : Type*} [Sup Ξ±] (sup_comm : βˆ€ a b : Ξ±, a βŠ” b = b βŠ” a) (sup_assoc : βˆ€ a b c : Ξ±, a βŠ” b βŠ” c = a βŠ” (b βŠ” c)) (sup_idem : βˆ€ a : Ξ±, a βŠ” a = a) : SemilatticeSup Ξ± where sup := (Β· βŠ” Β·) le a b := a βŠ” b = b le_refl := sup_idem le_trans a b c hab hbc := by -- Porting note: dsimp doesn't work here? -- This is the same issue as discussed at https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/unfolding.20earlier.20fields show a βŠ” c = c rw [← hbc, ← sup_assoc, hab] le_antisymm a b hab hba := by
Mathlib.Order.Lattice.80_0.wE3igZl9MFbJBfv
/-- A type with a commutative, associative and idempotent binary `sup` operation has the structure of a join-semilattice. The partial order is defined so that `a ≀ b` unfolds to `a βŠ” b = b`; cf. `sup_eq_right`. -/ def SemilatticeSup.mk' {Ξ± : Type*} [Sup Ξ±] (sup_comm : βˆ€ a b : Ξ±, a βŠ” b = b βŠ” a) (sup_assoc : βˆ€ a b c : Ξ±, a βŠ” b βŠ” c = a βŠ” (b βŠ” c)) (sup_idem : βˆ€ a : Ξ±, a βŠ” a = a) : SemilatticeSup Ξ± where sup
Mathlib_Order_Lattice
α✝ : Type u Ξ² : Type v Ξ± : Type u_1 inst✝ : Sup Ξ± sup_comm : βˆ€ (a b : Ξ±), a βŠ” b = b βŠ” a sup_assoc : βˆ€ (a b c : Ξ±), a βŠ” b βŠ” c = a βŠ” (b βŠ” c) sup_idem : βˆ€ (a : Ξ±), a βŠ” a = a a b : Ξ± ⊒ a βŠ” (a βŠ” b) = a βŠ” b
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl -/ import Mathlib.Data.Bool.Basic import Mathlib.Init.Order.Defs import Mathlib.Order.Monotone.Basic import Mathlib.Order.ULift import Mathlib.Tactic.GCongr.Core #align_import order.lattice from "leanprover-community/mathlib"@"3ba15165bd6927679be7c22d6091a87337e3cd0c" /-! # (Semi-)lattices Semilattices are partially ordered sets with join (greatest lower bound, or `sup`) or meet (least upper bound, or `inf`) operations. Lattices are posets that are both join-semilattices and meet-semilattices. Distributive lattices are lattices which satisfy any of four equivalent distributivity properties, of `sup` over `inf`, on the left or on the right. ## Main declarations * `SemilatticeSup`: a type class for join semilattices * `SemilatticeSup.mk'`: an alternative constructor for `SemilatticeSup` via proofs that `βŠ”` is commutative, associative and idempotent. * `SemilatticeInf`: a type class for meet semilattices * `SemilatticeSup.mk'`: an alternative constructor for `SemilatticeInf` via proofs that `βŠ“` is commutative, associative and idempotent. * `Lattice`: a type class for lattices * `Lattice.mk'`: an alternative constructor for `Lattice` via proofs that `βŠ”` and `βŠ“` are commutative, associative and satisfy a pair of "absorption laws". * `DistribLattice`: a type class for distributive lattices. ## Notations * `a βŠ” b`: the supremum or join of `a` and `b` * `a βŠ“ b`: the infimum or meet of `a` and `b` ## TODO * (Semi-)lattice homomorphisms * Alternative constructors for distributive lattices from the other distributive properties ## Tags semilattice, lattice -/ universe u v w variable {Ξ± : Type u} {Ξ² : Type v} #align le_antisymm' le_antisymm /-! ### Join-semilattices -/ -- TODO: automatic construction of dual definitions / theorems /-- A `SemilatticeSup` is a join-semilattice, that is, a partial order with a join (a.k.a. lub / least upper bound, sup / supremum) operation `βŠ”` which is the least element larger than both factors. -/ class SemilatticeSup (Ξ± : Type u) extends Sup Ξ±, PartialOrder Ξ± where /-- The supremum is an upper bound on the first argument -/ protected le_sup_left : βˆ€ a b : Ξ±, a ≀ a βŠ” b /-- The supremum is an upper bound on the second argument -/ protected le_sup_right : βˆ€ a b : Ξ±, b ≀ a βŠ” b /-- The supremum is the *least* upper bound -/ protected sup_le : βˆ€ a b c : Ξ±, a ≀ c β†’ b ≀ c β†’ a βŠ” b ≀ c #align semilattice_sup SemilatticeSup /-- A type with a commutative, associative and idempotent binary `sup` operation has the structure of a join-semilattice. The partial order is defined so that `a ≀ b` unfolds to `a βŠ” b = b`; cf. `sup_eq_right`. -/ def SemilatticeSup.mk' {Ξ± : Type*} [Sup Ξ±] (sup_comm : βˆ€ a b : Ξ±, a βŠ” b = b βŠ” a) (sup_assoc : βˆ€ a b c : Ξ±, a βŠ” b βŠ” c = a βŠ” (b βŠ” c)) (sup_idem : βˆ€ a : Ξ±, a βŠ” a = a) : SemilatticeSup Ξ± where sup := (Β· βŠ” Β·) le a b := a βŠ” b = b le_refl := sup_idem le_trans a b c hab hbc := by -- Porting note: dsimp doesn't work here? -- This is the same issue as discussed at https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/unfolding.20earlier.20fields show a βŠ” c = c rw [← hbc, ← sup_assoc, hab] le_antisymm a b hab hba := by rwa [← hba, sup_comm] le_sup_left a b := show a βŠ” (a βŠ” b) = a βŠ” b by
rw [← sup_assoc, sup_idem]
/-- A type with a commutative, associative and idempotent binary `sup` operation has the structure of a join-semilattice. The partial order is defined so that `a ≀ b` unfolds to `a βŠ” b = b`; cf. `sup_eq_right`. -/ def SemilatticeSup.mk' {Ξ± : Type*} [Sup Ξ±] (sup_comm : βˆ€ a b : Ξ±, a βŠ” b = b βŠ” a) (sup_assoc : βˆ€ a b c : Ξ±, a βŠ” b βŠ” c = a βŠ” (b βŠ” c)) (sup_idem : βˆ€ a : Ξ±, a βŠ” a = a) : SemilatticeSup Ξ± where sup := (Β· βŠ” Β·) le a b := a βŠ” b = b le_refl := sup_idem le_trans a b c hab hbc := by -- Porting note: dsimp doesn't work here? -- This is the same issue as discussed at https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/unfolding.20earlier.20fields show a βŠ” c = c rw [← hbc, ← sup_assoc, hab] le_antisymm a b hab hba := by rwa [← hba, sup_comm] le_sup_left a b := show a βŠ” (a βŠ” b) = a βŠ” b by
Mathlib.Order.Lattice.80_0.wE3igZl9MFbJBfv
/-- A type with a commutative, associative and idempotent binary `sup` operation has the structure of a join-semilattice. The partial order is defined so that `a ≀ b` unfolds to `a βŠ” b = b`; cf. `sup_eq_right`. -/ def SemilatticeSup.mk' {Ξ± : Type*} [Sup Ξ±] (sup_comm : βˆ€ a b : Ξ±, a βŠ” b = b βŠ” a) (sup_assoc : βˆ€ a b c : Ξ±, a βŠ” b βŠ” c = a βŠ” (b βŠ” c)) (sup_idem : βˆ€ a : Ξ±, a βŠ” a = a) : SemilatticeSup Ξ± where sup
Mathlib_Order_Lattice
α✝ : Type u Ξ² : Type v Ξ± : Type u_1 inst✝ : Sup Ξ± sup_comm : βˆ€ (a b : Ξ±), a βŠ” b = b βŠ” a sup_assoc : βˆ€ (a b c : Ξ±), a βŠ” b βŠ” c = a βŠ” (b βŠ” c) sup_idem : βˆ€ (a : Ξ±), a βŠ” a = a a b : Ξ± ⊒ b βŠ” (a βŠ” b) = a βŠ” b
/- Copyright (c) 2017 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl -/ import Mathlib.Data.Bool.Basic import Mathlib.Init.Order.Defs import Mathlib.Order.Monotone.Basic import Mathlib.Order.ULift import Mathlib.Tactic.GCongr.Core #align_import order.lattice from "leanprover-community/mathlib"@"3ba15165bd6927679be7c22d6091a87337e3cd0c" /-! # (Semi-)lattices Semilattices are partially ordered sets with join (greatest lower bound, or `sup`) or meet (least upper bound, or `inf`) operations. Lattices are posets that are both join-semilattices and meet-semilattices. Distributive lattices are lattices which satisfy any of four equivalent distributivity properties, of `sup` over `inf`, on the left or on the right. ## Main declarations * `SemilatticeSup`: a type class for join semilattices * `SemilatticeSup.mk'`: an alternative constructor for `SemilatticeSup` via proofs that `βŠ”` is commutative, associative and idempotent. * `SemilatticeInf`: a type class for meet semilattices * `SemilatticeSup.mk'`: an alternative constructor for `SemilatticeInf` via proofs that `βŠ“` is commutative, associative and idempotent. * `Lattice`: a type class for lattices * `Lattice.mk'`: an alternative constructor for `Lattice` via proofs that `βŠ”` and `βŠ“` are commutative, associative and satisfy a pair of "absorption laws". * `DistribLattice`: a type class for distributive lattices. ## Notations * `a βŠ” b`: the supremum or join of `a` and `b` * `a βŠ“ b`: the infimum or meet of `a` and `b` ## TODO * (Semi-)lattice homomorphisms * Alternative constructors for distributive lattices from the other distributive properties ## Tags semilattice, lattice -/ universe u v w variable {Ξ± : Type u} {Ξ² : Type v} #align le_antisymm' le_antisymm /-! ### Join-semilattices -/ -- TODO: automatic construction of dual definitions / theorems /-- A `SemilatticeSup` is a join-semilattice, that is, a partial order with a join (a.k.a. lub / least upper bound, sup / supremum) operation `βŠ”` which is the least element larger than both factors. -/ class SemilatticeSup (Ξ± : Type u) extends Sup Ξ±, PartialOrder Ξ± where /-- The supremum is an upper bound on the first argument -/ protected le_sup_left : βˆ€ a b : Ξ±, a ≀ a βŠ” b /-- The supremum is an upper bound on the second argument -/ protected le_sup_right : βˆ€ a b : Ξ±, b ≀ a βŠ” b /-- The supremum is the *least* upper bound -/ protected sup_le : βˆ€ a b c : Ξ±, a ≀ c β†’ b ≀ c β†’ a βŠ” b ≀ c #align semilattice_sup SemilatticeSup /-- A type with a commutative, associative and idempotent binary `sup` operation has the structure of a join-semilattice. The partial order is defined so that `a ≀ b` unfolds to `a βŠ” b = b`; cf. `sup_eq_right`. -/ def SemilatticeSup.mk' {Ξ± : Type*} [Sup Ξ±] (sup_comm : βˆ€ a b : Ξ±, a βŠ” b = b βŠ” a) (sup_assoc : βˆ€ a b c : Ξ±, a βŠ” b βŠ” c = a βŠ” (b βŠ” c)) (sup_idem : βˆ€ a : Ξ±, a βŠ” a = a) : SemilatticeSup Ξ± where sup := (Β· βŠ” Β·) le a b := a βŠ” b = b le_refl := sup_idem le_trans a b c hab hbc := by -- Porting note: dsimp doesn't work here? -- This is the same issue as discussed at https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/unfolding.20earlier.20fields show a βŠ” c = c rw [← hbc, ← sup_assoc, hab] le_antisymm a b hab hba := by rwa [← hba, sup_comm] le_sup_left a b := show a βŠ” (a βŠ” b) = a βŠ” b by rw [← sup_assoc, sup_idem] le_sup_right a b := show b βŠ” (a βŠ” b) = a βŠ” b by
rw [sup_comm, sup_assoc, sup_idem]
/-- A type with a commutative, associative and idempotent binary `sup` operation has the structure of a join-semilattice. The partial order is defined so that `a ≀ b` unfolds to `a βŠ” b = b`; cf. `sup_eq_right`. -/ def SemilatticeSup.mk' {Ξ± : Type*} [Sup Ξ±] (sup_comm : βˆ€ a b : Ξ±, a βŠ” b = b βŠ” a) (sup_assoc : βˆ€ a b c : Ξ±, a βŠ” b βŠ” c = a βŠ” (b βŠ” c)) (sup_idem : βˆ€ a : Ξ±, a βŠ” a = a) : SemilatticeSup Ξ± where sup := (Β· βŠ” Β·) le a b := a βŠ” b = b le_refl := sup_idem le_trans a b c hab hbc := by -- Porting note: dsimp doesn't work here? -- This is the same issue as discussed at https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/unfolding.20earlier.20fields show a βŠ” c = c rw [← hbc, ← sup_assoc, hab] le_antisymm a b hab hba := by rwa [← hba, sup_comm] le_sup_left a b := show a βŠ” (a βŠ” b) = a βŠ” b by rw [← sup_assoc, sup_idem] le_sup_right a b := show b βŠ” (a βŠ” b) = a βŠ” b by
Mathlib.Order.Lattice.80_0.wE3igZl9MFbJBfv
/-- A type with a commutative, associative and idempotent binary `sup` operation has the structure of a join-semilattice. The partial order is defined so that `a ≀ b` unfolds to `a βŠ” b = b`; cf. `sup_eq_right`. -/ def SemilatticeSup.mk' {Ξ± : Type*} [Sup Ξ±] (sup_comm : βˆ€ a b : Ξ±, a βŠ” b = b βŠ” a) (sup_assoc : βˆ€ a b c : Ξ±, a βŠ” b βŠ” c = a βŠ” (b βŠ” c)) (sup_idem : βˆ€ a : Ξ±, a βŠ” a = a) : SemilatticeSup Ξ± where sup
Mathlib_Order_Lattice