-
Notifications
You must be signed in to change notification settings - Fork 260
[ add ] inequalities for 2 ^ log₂ n
#2925
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
Changes from 2 commits
7286a54
f05723d
70b1148
76bd712
9ca32d6
79ac364
5911038
a2687d5
4e6b150
cc1b10f
209b2da
2cc2abf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -41,6 +41,9 @@ open import Relation.Binary.PropositionalEquality.Core using (_≡_) | |||||
| ⌊log₂[2^n]⌋≡n : ∀ n → ⌊log₂ (2 ^ n) ⌋ ≡ n | ||||||
| ⌊log₂[2^n]⌋≡n n = ⌊log2⌋2^n≡n n | ||||||
|
|
||||||
| 2^⌊log₂n⌋≤n : ∀ n → .{{ _ : NonZero n }} → 2 ^ ⌊log₂ n ⌋ ≤ n | ||||||
| 2^⌊log₂n⌋≤n (ℕ.suc n) = 2^⌊log2n⌋≤n n (<-wellFounded (ℕ.suc n)) | ||||||
|
||||||
| 2^⌊log₂n⌋≤n (ℕ.suc n) = 2^⌊log2n⌋≤n n (<-wellFounded (ℕ.suc n)) | |
| 2^⌊log₂n⌋≤n n = 2^⌊log2n⌋≤n n (<-wellFounded n) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,7 +15,7 @@ open import Data.Nat.Properties | |||||||||||||||
| open import Data.Nat.Induction using (<-wellFounded) | ||||||||||||||||
| open import Induction.WellFounded using (Acc; acc) | ||||||||||||||||
| open import Relation.Binary.PropositionalEquality.Core | ||||||||||||||||
| using (_≡_; refl; cong; sym) | ||||||||||||||||
| using (_≡_; refl; cong; sym; trans) | ||||||||||||||||
jamesmckinna marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||
| open import Relation.Binary.PropositionalEquality.Properties | ||||||||||||||||
| using (module ≡-Reasoning) | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -81,6 +81,19 @@ open import Relation.Binary.PropositionalEquality.Properties | |||||||||||||||
| suc n ∎ | ||||||||||||||||
| where open ≡-Reasoning | ||||||||||||||||
|
|
||||||||||||||||
| 2^⌊log2n⌋≤n : ∀ n → (acc : Acc _<_ (1 + n)) → 2 ^ (⌊log2⌋ (1 + n) acc) ≤ 1 + n | ||||||||||||||||
| 2^⌊log2n⌋≤n ℕ.zero _ = s≤s z≤n | ||||||||||||||||
| 2^⌊log2n⌋≤n (ℕ.suc n) (acc rs) = | ||||||||||||||||
| begin | ||||||||||||||||
|
||||||||||||||||
| 2^⌊log2n⌋≤n : ∀ n → (acc : Acc _<_ (1 + n)) → 2 ^ (⌊log2⌋ (1 + n) acc) ≤ 1 + n | |
| 2^⌊log2n⌋≤n ℕ.zero _ = s≤s z≤n | |
| 2^⌊log2n⌋≤n (ℕ.suc n) (acc rs) = | |
| begin | |
| 2^⌊log2n⌋≤n : ∀ n .{{_ : NonZero n}} → (acc : Acc _<_ n) → 2 ^ (⌊log2⌋ n acc) ≤ n | |
| 2^⌊log2n⌋≤n (ℕ.suc n) (acc rs) = | |
| begin |
I haven't checked that the rest of the proof goes through unchanged, but... it shouldn't require much more work to fix things?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My proof needs the case
2^⌊log2n⌋≤n ℕ.zero _ = s≤s z≤n
at some point because the proof is by induction on n and I can't image how the induction should work without the base case. I tried it but I don't see how to update the proof such that it still compiles for your suggested type
2^⌊log2n⌋≤n : ∀ n .{{_ : NonZero n}} → (acc : Acc _<_ n) → 2 ^ (⌊log2⌋ n acc) ≤ n
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're quite right. See below!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2^⌊log2n⌋≤n : ∀ n .{{_ : NonZero n}} → (acc : Acc _<_ n) → 2 ^ (⌊log2⌋ n acc) ≤ n
2^⌊log2n⌋≤n (suc zero) _ = ≤-refl
2^⌊log2n⌋≤n (suc (suc n)) (acc rs) = begin
2 ^ (⌊log2⌋ (suc (suc n)) (acc rs)) ≡⟨⟩
2 * 2 ^ (⌊log2⌋ (suc ⌊ n /2⌋) _) ≤⟨ *-monoʳ-≤ 2 (2^⌊log2n⌋≤n _ _) ⟩
2 * (suc ⌊ n /2⌋) ≡⟨ 2*suc[n]≡2+n+n _ ⟩
2 + (⌊ n /2⌋ + ⌊ n /2⌋) ≤⟨ +-monoʳ-≤ (2 + ⌊ n /2⌋) (⌊n/2⌋≤⌈n/2⌉ _) ⟩
2 + (⌊ n /2⌋ + ⌈ n /2⌉) ≡⟨ cong (2 +_) (⌊n/2⌋+⌈n/2⌉≡n _) ⟩
2 + n
∎
where open ≤-ReasoningNB. style-guide mandates that begin should be on the same line as the LHS of the definition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I've updated it (also in the Changelog). However, I find the definition in Logarithm.agda then more difficult to understand, because it is not visible that the property of n being NonZero is passed to the call:
2^⌊log₂n⌋≤n : ∀ n → .{{ _ : NonZero n }} → 2 ^ ⌊log₂ n ⌋ ≤ n
2^⌊log₂n⌋≤n n = 2^⌊log2n⌋≤n n (<-wellFounded n)
But it's fine for me. :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah!
There are two ways to think about this problem (there may be others... I offer only two here!?), of which the first is what I take to be your 'difficulty' above:
instancearguments are mysterious, because we neither see them explicitly bound on the LHS, nor see them explicitly passed as arguments on the RHS;instancearguments are fantastically useful, especially as here, when used as irrelevant arguments, because we neither see them explicitly bound on the LHS, nor see them explicitly passed as arguments on the RHS.
Spelt out, the definition above amounts to (and I believe this is what the type checker is more or less doing):
2^⌊log₂n⌋≤n n {{nz}} = 2^⌊log2n⌋≤n n (<-wellFounded n) {{something}}
where
something = <a proof of NonZero n>in such a way that:
somethingcan be uniquely computed from the 'visible' arguments, and any other declaredinstances, in scope (this is the requirement for instance inference to succeed)- argument
nzcannot actually be used in a relevant way, but that's OK, because here, the function2^⌊log2n⌋≤nalso expects its instance argument to be used in an irrelevant way - so... taking
something = nzseems the only available choice, and moreover it satisfies the conditions above.
Ie.
2^⌊log₂n⌋≤n n {{nz}} = 2^⌊log2n⌋≤n n (<-wellFounded n) {{nz}}or even, to emphasise that we are not allowed even to consider using nz in a relevant way
2^⌊log₂n⌋≤n n {{_}} = 2^⌊log2n⌋≤n n (<-wellFounded n) {{_}}where the use of _ on the LHS means "I don't care", while on the RHS means "you figure it out", in the dialogue game between user ("I") and typechecker ("you").
Once we arrive at the version with underscores, well, then they really are just noise, so...
Uh oh!
There was an error while loading. Please reload this page.