Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: de-mathlib Nat.binaryRec #314

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
dde0327
feat: de-mathlib `Nat.binaryRec`
FR-vdash-bot Oct 22, 2023
148c53f
specialize
FR-vdash-bot Oct 22, 2023
6b9474b
lint
FR-vdash-bot Oct 22, 2023
230166f
inline
FR-vdash-bot Oct 22, 2023
730782c
generalize & golf
FR-vdash-bot Oct 22, 2023
9da444d
mathlib dislike `binaryRec` with `elab_as_elim`
FR-vdash-bot Oct 22, 2023
6c6cd81
move & golf
FR-vdash-bot Oct 23, 2023
b33a41f
Merge remote-tracking branch 'upstream/main' into bitwise
FR-vdash-bot Nov 15, 2023
ff6786b
fix
FR-vdash-bot Nov 15, 2023
e638457
Merge remote-tracking branch 'upstream/main' into bitwise
FR-vdash-bot Dec 15, 2023
cff0bf7
fix merge
FR-vdash-bot Dec 15, 2023
615cfc9
fix merge & golf
FR-vdash-bot Dec 15, 2023
6982af0
feat: more `Bool` lemmas
FR-vdash-bot Dec 15, 2023
2aacb40
style
FR-vdash-bot Dec 15, 2023
a138ebb
Merge branch 'more_lemmas_for_bool' into bitwise
FR-vdash-bot Dec 15, 2023
3fed6e4
fix
FR-vdash-bot Dec 15, 2023
7607939
fix
FR-vdash-bot Dec 15, 2023
abd94e2
fix
FR-vdash-bot Dec 15, 2023
daf7634
fix
FR-vdash-bot Dec 15, 2023
198f289
Merge branch 'more_lemmas_for_bool' into bitwise
FR-vdash-bot Dec 15, 2023
3e9efc4
feat: more lemmas about equality
FR-vdash-bot Dec 15, 2023
fbf3b82
no `↦`
FR-vdash-bot Dec 15, 2023
03265b6
lint
FR-vdash-bot Dec 15, 2023
7738c3a
Merge branch 'more_lemmas_about_eq' into bitwise
FR-vdash-bot Dec 15, 2023
661debc
fix name
FR-vdash-bot Dec 18, 2023
ba2cdc5
it was not `elab_as_elim` in mathlib but eventually I think it should be
FR-vdash-bot Dec 19, 2023
f536348
implicit
FR-vdash-bot Dec 19, 2023
bd518d9
suggestion
FR-vdash-bot Dec 29, 2023
c5b411a
Merge remote-tracking branch 'upstream/main' into bitwise
FR-vdash-bot Jan 19, 2024
1736834
fix merge
FR-vdash-bot Jan 19, 2024
8901a71
Merge remote-tracking branch 'upstream/main' into more_lemmas_for_bool
FR-vdash-bot Jan 20, 2024
86627c6
Update Bool.lean
FR-vdash-bot Jan 20, 2024
70bbab4
Merge branch 'more_lemmas_for_bool' into bitwise
FR-vdash-bot Jan 20, 2024
67605c3
fix merge
FR-vdash-bot Jan 20, 2024
89a4e75
Merge remote-tracking branch 'upstream/main' into bitwise
FR-vdash-bot Jan 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Std/Data/Nat/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ where
guess
termination_by iter guess => guess

/-!
### testBit
We define an operation for testing individual bits in the binary representation
of a number.
-/

/-- `testBit m n` returns whether the `(n+1)` least significant bit is `1` or `0`-/
def testBit (m n : Nat) : Bool := (m >>> n) &&& 1 != 0

/-- `bodd n` returns `true` if `n` is odd-/
def bodd (n : Nat) : Bool :=
(1 &&& n) != 0
Expand Down Expand Up @@ -215,3 +224,11 @@ def binaryRecFromOne {C : Nat → Sort u} (z₀ : C 0) (z₁ : C 1)
rw [h', h h']
exact z₁
else f b n h' ih
/-!
### testBit
We define an operation for testing individual bits in the binary representation
of a number.
-/

/-- `testBit m n` returns whether the `(n+1)` least significant bit is `1` or `0`-/
def testBit (m n : Nat) : Bool := (m >>> n) &&& 1 != 0
42 changes: 39 additions & 3 deletions Std/Data/Nat/Lemmas.lean
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,20 @@ theorem succ_eq_one_add (n) : succ n = 1 + n := (one_add _).symm
theorem succ_add_eq_add_succ (a b) : succ a + b = a + succ b := Nat.succ_add ..
@[deprecated] alias succ_add_eq_succ_add := Nat.succ_add_eq_add_succ

protected theorem add_eq_zero {n m : Nat} : n + m = 0 ↔ n = 0 ∧ m = 0 :=
⟨eq_zero_of_add_eq_zero, by simp (config := {contextual := true})⟩
theorem eq_zero_of_add_eq_zero : ∀ {n m}, n + m = 0 → n = 0 ∧ m = 0
| 0, 0, _ => ⟨rfl, rfl⟩
| _+1, 0, h => Nat.noConfusion h

protected theorem add_left_cancel_iff {n m k : Nat} : n + m = n + k ↔ m = k :=
protected theorem eq_zero_of_add_eq_zero_right (h : n + m = 0) : n = 0 :=
(Nat.eq_zero_of_add_eq_zero h).1

protected theorem eq_zero_of_add_eq_zero_left (h : n + m = 0) : m = 0 :=
(Nat.eq_zero_of_add_eq_zero h).2

protected theorem add_eq_zero_iff : n + m = 0 ↔ n = 0 ∧ m = 0 :=
FR-vdash-bot marked this conversation as resolved.
Show resolved Hide resolved
⟨Nat.eq_zero_of_add_eq_zero, fun ⟨h₁, h₂⟩ => h₂.symm ▸ h₁⟩

protected theorem add_left_cancel_iff {n : Nat} : n + m = n + k ↔ m = k :=
FR-vdash-bot marked this conversation as resolved.
Show resolved Hide resolved
⟨Nat.add_left_cancel, fun | rfl => rfl⟩

protected theorem add_right_cancel_iff {n : Nat} : m + n = k + n ↔ m = k :=
Expand Down Expand Up @@ -955,6 +965,11 @@ theorem mul_div_le (m n : Nat) : n * (m / n) ≤ m := by
| _, Or.inl rfl => rw [Nat.zero_mul]; exact m.zero_le
| n, Or.inr h => rw [Nat.mul_comm, ← Nat.le_div_iff_mul_le h]; exact Nat.le_refl _

theorem mod_two_eq_zero_or_one (n : Nat) : n % 2 = 0 ∨ n % 2 = 1 :=
match n % 2, @Nat.mod_lt n 2 (by decide) with
| 0, _ => .inl rfl
| 1, _ => .inr rfl

theorem le_of_mod_lt {a b : Nat} (h : a % b < a) : b ≤ a :=
Nat.not_lt.1 fun hf => (ne_of_lt h).elim (Nat.mod_eq_of_lt hf)

Expand Down Expand Up @@ -1297,6 +1312,27 @@ theorem shiftRight_eq_div_pow (m : Nat) : ∀ n, m >>> n = m / 2 ^ n
rw [shiftRight_add, shiftRight_eq_div_pow m k]
simp [Nat.div_div_eq_div_mul, ← Nat.pow_succ]

theorem mul_add_div {m : Nat} (m_pos : m > 0) (x y : Nat) : (m * x + y) / m = x + y / m := by
FR-vdash-bot marked this conversation as resolved.
Show resolved Hide resolved
match x with
| 0 => simp
| x + 1 =>
simp [Nat.mul_succ, Nat.add_assoc _ m,
mul_add_div m_pos x (m+y),
div_eq (m+y) m,
m_pos,
Nat.le_add_right m, Nat.add_succ, Nat.succ_add]

theorem mul_add_mod (m x y : Nat) : (m * x + y) % m = y % m := by
match x with
| 0 => simp
| x + 1 =>
simp [Nat.mul_succ, Nat.add_assoc _ m, mul_add_mod _ x]

@[simp] theorem mod_div_self (m n : Nat) : m % n / n = 0 := by
cases n
· exact (m % 0).div_zero
· case succ n => exact Nat.div_eq_of_lt (m.mod_lt n.succ_pos)

/-! ### shiftLeft -/

theorem shiftLeft_zero (m) : m <<< 0 = m := rfl
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.