feat: add faster rat semantics for testing, and show equivalence against slow semantics#35
Closed
feat: add faster rat semantics for testing, and show equivalence against slow semantics#35
Conversation
abdoo8080
reviewed
Jan 21, 2026
abdoo8080
reviewed
Jan 21, 2026
Fp/SmtLibQSemantics.lean
Outdated
Comment on lines
162
to
185
| /-- The upper approximant of 'v'. | ||
| Returns the smallest 'x : X' such that 'r ≤ v x'. -/ | ||
| def upper (e s : Nat) (r : ExtRat) : PackedFloat e s := | ||
| (lower e s r.neg).neg | ||
|
|
||
| /-- Lower half, return 'true' iff we are strictly in the lower half. -/ | ||
| def lh (e s : Nat) (r : ExtRat) : Bool := | ||
| (r - (lower e s r).toExtRat) < (upper e s r).toExtRat - r | ||
|
|
||
| /-- Tiebreak, return 'true' iff we are exactly in the middle of the lower and upper approximants. -/ | ||
| def tb (e s : Nat) (r : ExtRat) : Bool := | ||
| r - (lower e s r).toExtRat = (upper e s r).toExtRat - r | ||
| /-- Upper half, return 'true' iff we are strictly in the upper half. -/ | ||
| def uh (e s : Nat) (r : ExtRat) : Bool := | ||
| (r - (lower e s r).toExtRat) > (upper e s r).toExtRat - r | ||
|
|
||
| /-- Check if 'X' is even. -/ | ||
| def ev (e s : Nat) (x : PackedFloat e s) : Bool := | ||
| match x.toExtRat with | ||
| | .Number n => | ||
| let den := n.den | ||
| let num := n.num | ||
| num = 0 ∨ (den = 1 ∧ num.natAbs % 2 = 0) | ||
| | _ => false |
Collaborator
There was a problem hiding this comment.
These differ from the definition in the paper...
Contributor
Author
There was a problem hiding this comment.
not sure what you mean --- the paper just says "let there exist an integer such that 2 * integer equals the rat", and this should be the computational version of this?
Contributor
Author
|
While the slow / reference implementation works, the fast one doesn't seem to. Debugging what's going on... |
abdoo8080
reviewed
Jan 21, 2026
Fp/SmtLibQSemantics.lean
Outdated
Comment on lines
198
to
213
| if _hz : r = .Number 0 then rsz e s sign | ||
| else | ||
| if _hlh : lh e s r | ||
| then lower e s | ||
| else | ||
| if _htb : tb e s r | ||
| then | ||
| if _heven : ev e s (lower e s r) | ||
| then lower e s | ||
| else upper e s | ||
| else | ||
| -- not tie break, not lower, so we are in upper half. | ||
| -- have : uh r v := by | ||
| -- have := trichotomy_lh_tb_uh r v | ||
| -- grind | ||
| upper e s |
Collaborator
There was a problem hiding this comment.
Could you flatten the ites to mirror the paper? Don't worry about introducing redundancies in the conditions.
…etween the abstract and concrete impls
… and upper definitions
Recall that according to RNE, you should round to +infty if you're between/higher than the largest value, and what *would* be the next representable floating point value. However, in my 'computable' implementaiton, I don't actually implement this spec.
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The fast semantics should be closer to the 'round' implementation, which will help. inspired by @abdoo8080 , who suggested that we have this for fast execution.