Skip to content

Commit 03b1be8

Browse files
committed
2 parents c155232 + 287e090 commit 03b1be8

File tree

5 files changed

+54
-51
lines changed

5 files changed

+54
-51
lines changed

Diff for: Batteries/Data/Fin/OfBits.lean

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
/-
22
Copyright (c) 2025 François G. Dorais. All rights reserved.
3-
Released under Apache 2.0 license as described in the file LICENSE.
3+
Released under Apache 2. license as described in the file LICENSE.
44
Authors: François G. Dorais
55
-/
66

7-
import Batteries.Data.Nat.OfBits
7+
import Batteries.Data.Nat.Lemmas
88

99
namespace Fin
1010

11-
/-- Construct a `Fin (2 ^ n)` element from bit values (little endian). -/
11+
/--
12+
Construct an element of `Fin (2 ^ n)` from a sequence of bits (little endian).
13+
-/
1214
abbrev ofBits (f : Fin n → Bool) : Fin (2 ^ n) := ⟨Nat.ofBits f, Nat.ofBits_lt_two_pow f⟩
1315

14-
theorem val_ofBits (f : Fin n → Bool) : (ofBits f).val = Nat.ofBits f := rfl
16+
@[simp] theorem val_ofBits (f : Fin n → Bool) : (ofBits f).val = Nat.ofBits f := rfl

Diff for: Batteries/Data/Nat.lean

-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@ import Batteries.Data.Nat.Basic
22
import Batteries.Data.Nat.Bisect
33
import Batteries.Data.Nat.Gcd
44
import Batteries.Data.Nat.Lemmas
5-
import Batteries.Data.Nat.OfBits
6-

Diff for: Batteries/Data/Nat/Basic.lean

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ Authors: Leonardo de Moura, Jeremy Avigad, Mario Carneiro
66

77
namespace Nat
88

9-
109
/--
1110
Recursor identical to `Nat.recOn` but uses notations `0` for `Nat.zero` and `·+1` for `Nat.succ`
1211
-/
@@ -103,3 +102,9 @@ where
103102
else
104103
guess
105104
termination_by guess
105+
106+
/--
107+
Construct a natural number from a sequence of bits using little endian convention.
108+
-/
109+
@[inline] def ofBits (f : Fin n → Bool) : Nat :=
110+
Fin.foldr n (fun i v => 2 * v + (f i).toNat) 0

Diff for: Batteries/Data/Nat/Lemmas.lean

+42
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,45 @@ protected def sum_trichotomy (a b : Nat) : a < b ⊕' a = b ⊕' b < a :=
162162

163163
@[simp] theorem sum_append {l₁ l₂ : List Nat}: (l₁ ++ l₂).sum = l₁.sum + l₂.sum := by
164164
induction l₁ <;> simp [*, Nat.add_assoc]
165+
166+
/-! ### ofBits -/
167+
168+
@[simp] theorem ofBits_zero (f : Fin 0 → Bool) : ofBits f = 0 := rfl
169+
170+
theorem ofBits_succ (f : Fin (n+1) → Bool) : ofBits f = 2 * ofBits (f ∘ Fin.succ) + (f 0).toNat :=
171+
Fin.foldr_succ ..
172+
173+
theorem ofBits_lt_two_pow (f : Fin n → Bool) : ofBits f < 2 ^ n := by
174+
induction n with
175+
| zero => simp
176+
| succ n ih =>
177+
calc ofBits f
178+
= 2 * ofBits (f ∘ Fin.succ) + (f 0).toNat := ofBits_succ ..
179+
_ < 2 * (ofBits (f ∘ Fin.succ) + 1) := Nat.add_lt_add_left (Bool.toNat_lt _) ..
180+
_ ≤ 2 * 2 ^ n := Nat.mul_le_mul_left 2 (ih ..)
181+
_ = 2 ^ (n + 1) := Nat.pow_add_one' .. |>.symm
182+
183+
@[simp] theorem testBit_ofBits_lt (f : Fin n → Bool) (i : Nat) (h : i < n) :
184+
(ofBits f).testBit i = f ⟨i, h⟩ := by
185+
induction n generalizing i with
186+
| zero => contradiction
187+
| succ n ih =>
188+
simp only [ofBits_succ]
189+
match i with
190+
| 0 => simp [mod_eq_of_lt (Bool.toNat_lt _)]
191+
| i+1 =>
192+
rw [testBit_add_one, mul_add_div Nat.zero_lt_two, Nat.div_eq_of_lt (Bool.toNat_lt _)]
193+
exact ih (f ∘ Fin.succ) i (Nat.lt_of_succ_lt_succ h)
194+
195+
@[simp] theorem testBit_ofBits_ge (f : Fin n → Bool) (i : Nat) (h : n ≤ i) :
196+
(ofBits f).testBit i = false := by
197+
apply testBit_lt_two_pow
198+
apply Nat.lt_of_lt_of_le
199+
· exact ofBits_lt_two_pow f
200+
· exact pow_le_pow_of_le_right Nat.zero_lt_two h
201+
202+
theorem testBit_ofBits (f : Fin n → Bool) :
203+
(ofBits f).testBit i = if h : i < n then f ⟨i, h⟩ else false := by
204+
cases Nat.lt_or_ge i n with
205+
| inl h => simp [h]
206+
| inr h => simp [h, Nat.not_lt_of_ge h]

Diff for: Batteries/Data/Nat/OfBits.lean

-44
This file was deleted.

0 commit comments

Comments
 (0)