Skip to content

Commit ab4fbd2

Browse files
[ add ] inequalities for 2 ^ log₂ n (#2925)
* [ add ] inequalities for `2 ^ log₂ n` This adds the inequalities `2 ^ ⌊log₂ n ⌋ ≤ n` for non-zero `n` and `n ≤ 2 ^ ⌈log₂ n ⌉` for all natural numbers `n`. Similar to the existing properties of the logarithm, the main proofs that keep track of the accumulators are added to `Data.Nat.Logarithm.Core`. Both use a lemma that `2 * (ℕ.suc n) ≡ 2 + (n + n)` which is added to `Data.Nat.Logarithm.Core` as well. The respective simplified versions on `⌊log₂_⌋` and `⌈log₂_⌉` are added directly to `Data.Nat.Logarithm`. * move 2*suc[n]≡2+n+n to Data.Nat.Properties * ℕ.suc -> suc * Further ℕ.suc -> suc * replace 1 + with suc * Switch to equational reasoning * Update Changelog * RM unused 'trans' * Use NonZero instance * Update Changelog * Update src/Data/Nat/Logarithm.agda Co-authored-by: jamesmckinna <31931406+jamesmckinna@users.noreply.github.com> * Update CHANGELOG.md Co-authored-by: jamesmckinna <31931406+jamesmckinna@users.noreply.github.com> --------- Co-authored-by: jamesmckinna <31931406+jamesmckinna@users.noreply.github.com>
1 parent dbdebad commit ab4fbd2

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,18 @@ Additions to existing modules
236236
map : P ⊆ Q → All P xs → All Q xs
237237
```
238238

239+
* In `Data.Nat.Logarithm`
240+
```agda
241+
2^⌊log₂n⌋≤n : ∀ n .{{ _ : NonZero n }} → 2 ^ ⌊log₂ n ⌋ ≤ n
242+
n≤2^⌈log₂n⌉ : ∀ n → n ≤ 2 ^ ⌈log₂ n ⌉
243+
```
244+
245+
* In `Data.Nat.Logarithm.Core`
246+
```agda
247+
2^⌊log2n⌋≤n : ∀ n .{{_ : NonZero n}} → (acc : Acc _<_ n) → 2 ^ (⌊log2⌋ n acc) ≤ n
248+
n≤2^⌈log2n⌉ : ∀ n → (acc : Acc _<_ n) → n ≤ 2 ^ (⌈log2⌉ n acc)
249+
```
250+
239251
* In `Data.Nat.ListAction.Properties`
240252
```agda
241253
*-distribˡ-sum : ∀ m ns → m * sum ns ≡ sum (map (m *_) ns)
@@ -248,6 +260,7 @@ Additions to existing modules
248260
≟-≢ : (m≢n : m ≢ n) → (m ≟ n) ≡ no m≢n
249261
∸-suc : m ≤ n → suc n ∸ m ≡ suc (n ∸ m)
250262
^-distribʳ-* : ∀ m n o → (n * o) ^ m ≡ n ^ m * o ^ m
263+
2*suc[n]≡2+n+n : ∀ n → 2 * (suc n) ≡ 2 + (n + n)
251264
```
252265

253266
* In `Data.Product.Properties`:

src/Data/Nat/Logarithm.agda

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ open import Relation.Binary.PropositionalEquality.Core using (_≡_)
4141
⌊log₂[2^n]⌋≡n : n ⌊log₂ (2 ^ n) ⌋ ≡ n
4242
⌊log₂[2^n]⌋≡n n = ⌊log2⌋2^n≡n n
4343

44+
2^⌊log₂n⌋≤n : n .{{ _ : NonZero n }} 2 ^ ⌊log₂ n ⌋ ≤ n
45+
2^⌊log₂n⌋≤n n = 2^⌊log2n⌋≤n n (<-wellFounded n)
46+
4447
------------------------------------------------------------------------
4548
-- Properties of ⌈log₂_⌉
4649

@@ -55,3 +58,6 @@ open import Relation.Binary.PropositionalEquality.Core using (_≡_)
5558

5659
⌈log₂2^n⌉≡n : n ⌈log₂ (2 ^ n) ⌉ ≡ n
5760
⌈log₂2^n⌉≡n n = ⌈log2⌉2^n≡n n
61+
62+
n≤2^⌈log₂n⌉ : n n ≤ 2 ^ ⌈log₂ n ⌉
63+
n≤2^⌈log₂n⌉ n = n≤2^⌈log2n⌉ n (<-wellFounded n)

src/Data/Nat/Logarithm/Core.agda

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ open import Relation.Binary.PropositionalEquality.Properties
8181
suc n ∎
8282
where open ≡-Reasoning
8383

84+
2^⌊log2n⌋≤n : n .{{_ : NonZero n}} (acc : Acc _<_ n) 2 ^ (⌊log2⌋ n acc) ≤ n
85+
2^⌊log2n⌋≤n (suc zero) _ = ≤-refl
86+
2^⌊log2n⌋≤n (suc (suc n)) (acc rs) = begin
87+
2 ^ (⌊log2⌋ (suc (suc n)) (acc rs)) ≡⟨⟩
88+
2 * 2 ^ (⌊log2⌋ (suc ⌊ n /2⌋) _) ≤⟨ *-monoʳ-≤ 2 (2^⌊log2n⌋≤n _ _) ⟩
89+
2 * (suc ⌊ n /2⌋) ≡⟨ 2*suc[n]≡2+n+n _ ⟩
90+
2 + (⌊ n /2⌋ + ⌊ n /2⌋) ≤⟨ +-monoʳ-≤ (2 + ⌊ n /2⌋) (⌊n/2⌋≤⌈n/2⌉ _) ⟩
91+
2 + (⌊ n /2⌋ + ⌈ n /2⌉) ≡⟨ cong (2 +_) (⌊n/2⌋+⌈n/2⌉≡n _) ⟩
92+
2 + n
93+
94+
where open ≤-Reasoning
95+
8496
------------------------------------------------------------------------
8597
-- Properties of ⌈log2⌉
8698

@@ -124,3 +136,17 @@ open import Relation.Binary.PropositionalEquality.Properties
124136
1 + ⌈log2⌉ (2 ^ n) (<-wellFounded _) ≡⟨ cong suc (⌈log2⌉2^n≡n n) ⟩
125137
suc n ∎
126138
where open ≡-Reasoning
139+
140+
n≤2^⌈log2n⌉ : n (acc : Acc _<_ n) n ≤ 2 ^ (⌈log2⌉ n acc)
141+
n≤2^⌈log2n⌉ zero _ = z≤n
142+
n≤2^⌈log2n⌉ (suc zero) _ = s≤s z≤n
143+
n≤2^⌈log2n⌉ (suc (suc n)) (acc rs) =
144+
begin
145+
2 + n ≡⟨ cong (2 +_) (⌊n/2⌋+⌈n/2⌉≡n _) ⟨
146+
2 + (⌊ n /2⌋ + ⌈ n /2⌉) ≤⟨ +-monoʳ-≤ 2 (+-monoˡ-≤ _ (⌊n/2⌋≤⌈n/2⌉ n)) ⟩
147+
2 + (⌈ n /2⌉ + ⌈ n /2⌉) ≡⟨ 2*suc[n]≡2+n+n _ ⟨
148+
2 * (suc ⌊ suc n /2⌋) ≤⟨ *-monoʳ-≤ 2 (n≤2^⌈log2n⌉ (suc ⌊ suc n /2⌋) _) ⟩
149+
2 * 2 ^ (⌈log2⌉ (suc ⌊ suc n /2⌋) _) ≡⟨⟩
150+
2 ^ (⌈log2⌉ (2 + n) (acc rs))
151+
152+
where open ≤-Reasoning

src/Data/Nat/Properties.agda

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,12 @@ m+n≮m m n = subst (_≮ m) (+-comm n m) (m+n≮n n m)
774774
suc (m + (n + m * n)) ≡⟨⟩
775775
suc m + suc m * n ∎
776776

777+
2*suc[n]≡2+n+n : n 2 * (suc n) ≡ 2 + (n + n)
778+
2*suc[n]≡2+n+n n = begin-equality
779+
2 * (suc n) ≡⟨ cong (suc n +_) (+-identityʳ _) ⟩
780+
suc n + suc n ≡⟨ +-suc (suc n) n ⟩
781+
2 + (n + n) ∎
782+
777783
------------------------------------------------------------------------
778784
-- Algebraic properties of _*_
779785

0 commit comments

Comments
 (0)