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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.