[Add] Properties of rounding in Rational#2953
[Add] Properties of rounding in Rational#2953aortega0703 wants to merge 16 commits intoagda:masterfrom
Conversation
|
As to 'advice', it might be worth mentioning #1434 ... ... and speculating whether it is more 'efficient' to prove instead: (these may be false, or else insufficiently sharp; I haven't checked ;-)) as somehow 'what we want from the specification of floor and ceiling'
from which some of your properties would follow...but I confess its a while since I could remember any approximation theory ;-) so maybe take these with a pinch of salt. Not sure how to deal with the bounds on rounding, either ;-) |
Co-authored-by: jamesmckinna <31931406+jamesmckinna@users.noreply.github.com>
JacquesCarette
left a comment
There was a problem hiding this comment.
Review pass 1. There will likely be a pass 2.
| ∣-∣-nonNeg (mkℚᵘ -[1+ _ ] _) = _ | ||
|
|
||
| -q≤p≤q⇒∣p∣≤q : ∀ p q → - q ≤ p → p ≤ q → ∣ p ∣ ≤ q | ||
| -q≤p≤q⇒∣p∣≤q p q -q≤p p≤q = |
There was a problem hiding this comment.
I'm pretty sure this proof would be much nicer if it used with on ∣p∣≡p∨∣p∣≡-p p followed by pattern-matching all the way down to refl, which would remove the need for subst.
Alternatively, create two lemmas, one for each of the cases, and then use them here.
Perhaps even simpler would be to case on whether p is non-negative or not?
There was a problem hiding this comment.
I tried following the first approach:
-q≤p≤q⇒∣p∣≤q : ∀ {p q} → - q ≤ p → p ≤ q → ∣ p ∣ ≤ q
-q≤p≤q⇒∣p∣≤q {p} {q} -q≤p p≤q with ∣p∣≡p∨∣p∣≡-p p
... | inj₁ x = {!!}
... | inj₂ y = {!!}but when I try splitting either x or y to pattern-match against refl I'm hit with
I'm not sure if there should be a case for the constructor refl,
because I get stuck when trying to solve the following unification
problems (inferred index ≟ expected index):
∣ p₁ ∣ ≟ p₁
so I don't know how to proceed there. In the meantime I changed my proof to
-q≤p≤q⇒∣p∣≤q : ∀ {p q} → - q ≤ p → p ≤ q → ∣ p ∣ ≤ q
-q≤p≤q⇒∣p∣≤q {p} {q} -q≤p p≤q with ∣p∣≡p∨∣p∣≡-p p
... | inj₁ ∣p∣≡p rewrite ∣p∣≡p = p≤q
... | inj₂ ∣p∣≡-p rewrite ∣p∣≡-p =
subst (_ ≤_) (neg-involutive-≡ q) (neg-mono-≤ -q≤p)which is a bit nicer? On the third approach, I tried with on p <ᵇ 0ℚ (which I think is what you meant?) to work on the true/false arms but I can't figure how to tell Agda to reduce the ∣ p ∣ in the goal to either - p or p in each case. I'm still finding my way around Agda and the standard lib so thanks for the patience and comments!
| ------------------------------------------------------------------------ | ||
| -- Bounds of ⌊_⌋ and ⌈_⌉ | ||
|
|
||
| ⌊q⌋≤q : ∀ q → ⌊ q ⌋ / 1 ≤ q |
There was a problem hiding this comment.
That floor changes type and so we have to change back to do ≤ makes sense. But this / 1 idiom seems too low-level. Do we not have some nicer way of saying this?
There was a problem hiding this comment.
There is a 1/_ for reciprocals but I don't see anything like a _/1 to simply "cast" the type. I saw that fracPart used / 1 and ran with it as the nicer constructor compared to mkℚᵘ.
There was a problem hiding this comment.
I think I'd be happy with introducing an explicit coercion from ℤ to ℚᵘ as mkℚᵘ_ 0... choosing a good name for such a thing being the hard part! Possible solution:
ℤtoℚᵘ : ℤ → ℚᵘ
ℤtoℚᵘ i = mkℚᵘ i 0
syntax ℤtoℚᵘ i = [ i ]ℤThere's a separate question as to whether new syntax declarations is a good idea at all, and/or whether, as in this case, because the associated function is in fact a pattern (specialising mkℚᵘ), whether it itself should be introduced as
pattern ℤtoℚᵘ i = mkℚᵘ i 0
syntax ℤtoℚᵘ i = [ i ]ℤ... but it's not clear whether we could ever use this in a pattern match!?
There was a problem hiding this comment.
Alternatively, we can use my favourite identifier
ι : ℤ → ℚᵘ -- ℤ⇒ℚᵘ
ι i = mkℚᵘ i 0
ιℕ : ℕ → ℚᵘ -- ℕ⇒ℚᵘ
ιℕ n = ι (+ n)| n ≡⟨ ℤ.a≡a%n+[a/n]*n n d ⟩ | ||
| ℤ.+ (n ℤ.% d) ℤ.+ ⌊ q ⌋ ℤ.* d | ||
| <⟨ ℤ.+-monoˡ-< (⌊ q ⌋ ℤ.* d) (ℤ.+<+ (ℤ.n%d<d n d)) ⟩ | ||
| d ℤ.+ ⌊ q ⌋ ℤ.* d |
There was a problem hiding this comment.
feels like the steps from here to the end should be pulled out as a lemma.
There was a problem hiding this comment.
See my comments above: this is really the proof that floor and ceil are tight bounds, and is properly a lemma about integers?
|
@jamesmckinna Sorry for not replying to your first comment earlier, proving
⌈q⌉≤⌊q⌋+1 : ∀ q → ⌈ q ⌉ ℤ.≤ ⌊ q ⌋ ℤ.+ 1ℤbut when I tried actually proving the implications I hit q≤⌊q⌋+1 : ∀ q → q ≤ ⌊ q ⌋ / 1 + 1ℚᵘ
q≤⌊q⌋+1 q = begin
q ≤⟨ q≤⌈q⌉ q ⟩
⌈ q ⌉ / 1 ≤⟨ {!!} ⟩ {!!} -- ≤ (⌊ q ⌋ + 1) / 1
where open ≤-Reasoningand I don't think there's anything like |
db71f55 to
2b072f9
Compare
|
Thanks @aortega0703 for querying my original half-baked thoughts. I wrote something manifestly incorrect in the night when only half-awake (and when I should have been asleep ;-)), and my version of the story seems a bit less clear than yours. Once I've worked out the details, I'll try and repost. But there is something about all these functions being piecewise-linear, indeed piecewise constant, with discontinuities at the But that analysis, and the fiddliness involved in constructing the corresponding view machinery to instrument that case analysis, seems ... differently complicated to your approach. |
|
I tried cleaning my proof a bit, mostly by adding some integer lemmas (me shufling things around). Although, after thinking about it some more and fiddling with some proofs I no longer think |
|
Thanks @aortega0703 . Indeed, where I had got to was:
Is everything else provable from this case analysis? |
|
@jamesmckinna The non-integer case is not clear to me. I think an outline for floor would be Ceil is defined as But I don't know how to get |
|
Well it's entirely possible that I'm complicating things more than necessary! |
Hi, I added some properties of the rounding functions for
Rational.Unnormalised. Namely, bounds forfloorandceil:And approximation errors (absolute difference) for
floor,ceil, andround:I also added a property of
∣_∣to help me prove the errors:I still haven't translated these proofs to normalised
Rationals, and the proof for∣q-round[q]∣≤½is quite long, so any advice is welcome.