Chez numeric tower: exact ints / Ratio / double for JVM parity (jolt-n6al)

jolt was all-flonum (one :number type, inherited from Janet whose only number
type is a double). The Chez runtime has a full numeric tower, so the zero-Janet
path now carries it = JVM Clojure semantics:

  (/ 1 2)      => 1/2      (exact Ratio, was 0.5)
  (integer? 3) => true   (integer? 3.0) => false   (float? 3.0) => true
  (ratio? (/ 1 2)) => true   (= 3 3.0) => false   (== 3 3.0) => true
  (+ 1 2) => 3 (exact)   (/ 1.0 2) => 0.5 (double)

jolt= was already exactness-aware (values.ss) and == is value-equality, so
=/== match the JVM split. The reader preserves exactness (integer literals exact,
a/b ratios exact rationals, decimals/exponents flonums); backend_scheme emit-const
renders exact ints/ratios and flonums faithfully; the value-position arithmetic,
count, int, compare, bit ops, parseLong, string .length/.indexOf, range,
timestamps, and array bytes return exact integers (= JVM int/long) instead of
coercing to flonum. double/parseDouble/clojure.math floor|ceil|signum stay double.

Only the zero-Janet path carries the tower (the Janet reader loses exactness into
a double before emit). The prelude/all-flonum path is unaffected for compiled code;
the runtime reader is shared, so a couple of all-flonum reader assertions become
value (==) assertions. ~16 numeric corpus cases now give the JVM tower value vs the
Janet-era :expected and are allowlisted as tower divergences (Chez == reference
JVM) pending the corpus flip to JVM (jolt-ecz0). No BigDecimal type (1M).

Re-minted. zero-janet 2682 (floor 2698->2682, the reclassified tower cases), 0 new
divergences; fixpoint 10/10, bootstrap 6/6, spine 35/35, cli 49/49; Janet gate 155
files 0 failed.
This commit is contained in:
Yogthos 2026-06-20 23:09:27 -04:00
parent eb1c3298a4
commit 467ad75ff7
20 changed files with 291 additions and 210 deletions

View file

@ -73,9 +73,13 @@
;; jolt models EVERY number as a double (emit-const lowers integer literals to
;; flonums too), so the reader coerces every parsed number to inexact — else a
;; read int (exact) is not jolt= to a source int literal (flonum).
;; Preserve exactness for the Chez numeric tower (JVM parity): integer literals
;; read as exact integers (= Long/BigInt, arbitrary precision), a/b ratios as
;; exact rationals (= Ratio), and decimal/exponent literals as flonums (= double).
;; Only the zero-Janet path (this reader) carries exactness through to runtime;
;; the Janet-reader prelude path stays all-flonum (Janet has only doubles).
(define (rdr-try-number tok)
(let ((raw (rdr-try-number-raw tok)))
(and raw (exact->inexact raw))))
(rdr-try-number-raw tok))
(define (rdr-try-number-raw tok)
(let ((len (string-length tok)))
@ -108,12 +112,13 @@
(blen (string-length body))
(slash (rdr-string-index-char body #\/)))
(cond
;; ratio a/b -> flonum (the seed has no exact ratios)
;; ratio a/b -> exact rational (= JVM Ratio); reduces to an exact integer
;; when d divides n.
(slash
(let ((n (string->number (substring body 0 slash)))
(d (string->number (substring body (+ slash 1) blen))))
(and (integer? n) (integer? d) (not (= d 0))
(* sign (exact->inexact (/ n d))))))
(* sign (/ n d)))))
;; hex 0x..
((and (>= blen 2) (char=? (string-ref body 0) #\0)
(or (char=? (string-ref body 1) #\x) (char=? (string-ref body 1) #\X)))
@ -137,9 +142,8 @@
(and n (exact->inexact (* sign n)))))
(else
(let ((n (string->number tok))) ; tok carries its own sign
(and (number? n) (real? n)
;; never surface an exact non-integer ratio
(if (and (exact? n) (not (integer? n))) (exact->inexact n) n)))))))
;; keep exactness: "42" -> exact int, "3.14"/"1e3" -> flonum.
(and (number? n) (real? n) n))))))
;; --- string / char literals -------------------------------------------------
(define (rdr-hex->int s i n) ; n hex digits at i -> (values int j)