From c652b86a2a4689bd06d1cd987aeb020c9eb444bd Mon Sep 17 00:00:00 2001 From: Alex Maldonado Date: Mon, 5 Oct 2026 03:21:18 +0000 Subject: [PATCH] Stop a run on a FLOAT that is not finite, so proofs about FLOAT hold The prover reasons about a FLOAT as a number: Y == Y holds, and X < Z or X >= Z. The interpreter let 1.0E308 * 10.0 become infinity and X - X become NaN and carried on, so a run broke claims the prover had proved (tests/prover/float-overflow.pseudo proved ASSERT Y == Y with Y NaN). The notation already stops on a FLOAT division by zero and on a whole number past 2^53 - 1. It now also stops, with an 'overflow' error, when an arithmetic result, a built-in's result or a literal is not a finite number (finiteCheck in interp.js). Every run then stays inside what a proof covers. Regression programs whose fix was to the language rather than to a rule now say where the run must stop, with a RUN-STOPS header the soundness test checks. A unit that does FLOAT arithmetic now carries a 'float-model' note: FLOAT is treated as a finite number, rounding is opaque, and the lab stops past about 1.8e308, while the generated Mojo goes on with inf or nan (stated for a division by zero, the case probes/float-special has verified). New probe mojo/probes/float-overflow.mojo asks the compiler what a Float64 product past the largest double gives; it is unverified and needs pixi run mojo-verify-item 'probes/float-overflow' (network). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ --- mojo/golden/STATUS.md | 3 ++- mojo/golden/status.json | 11 ++++++++++- mojo/probes/float-overflow.mojo | 5 +++++ src/engines/pseudo/interp.js | 23 ++++++++++++++++++++--- src/engines/pseudo/vcgen.js | 2 ++ tests/prover-soundness.test.mjs | 11 +++++++++-- tests/prover/float-overflow.pseudo | 10 ++++++++++ 7 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 mojo/probes/float-overflow.mojo create mode 100644 tests/prover/float-overflow.pseudo diff --git a/mojo/golden/STATUS.md b/mojo/golden/STATUS.md index fb184e4..1243bed 100644 --- a/mojo/golden/STATUS.md +++ b/mojo/golden/STATUS.md @@ -7,7 +7,7 @@ Generated by `python3 scripts/mojo/corpus.py status` from the goldens in this di | diagnostics | 0 | 0 | 0 | 42 | 132 | | local | 18 | 0 | 0 | 0 | 0 | | manual | 144 | 55 | 0 | 1 | 0 | -| probes | 13 | 0 | 71 | 0 | 0 | +| probes | 13 | 0 | 71 | 0 | 1 | | quest | 170 | 0 | 0 | 0 | 0 | | stress | 40 | 0 | 0 | 0 | 0 | @@ -94,6 +94,7 @@ Generated by `python3 scripts/mojo/corpus.py status` from the goldens in this di | `error-try-finally` | the order of try, except and finally | observed | | `error-unhandled` | what reaches stdout, stderr, and the exit code when main raises? | observed | | `float-mixed` | can an Int and a Float64 be combined without a conversion? | observed | +| `float-overflow` | does a Float64 product past the largest double give inf at run time, and inf - inf nan? | unverified | | `float-print` | how does print format Float64 values? | observed | | `float-round` | what does round() do on halves? | observed | | `float-special` | how are inf, -inf and nan printed after a run-time division by zero? | observed | diff --git a/mojo/golden/status.json b/mojo/golden/status.json index a7d16c8..b6d80c1 100644 --- a/mojo/golden/status.json +++ b/mojo/golden/status.json @@ -3129,6 +3129,14 @@ "verdict": "observed", "why": "did not compile" }, + { + "corpus": "probes", + "id": "probes/float-overflow", + "key": null, + "question": "does a Float64 product past the largest double give inf at run time, and inf - inf nan?", + "verdict": "unverified", + "why": "no answer from the pinned compiler yet" + }, { "corpus": "probes", "id": "probes/float-print", @@ -5506,7 +5514,8 @@ }, "probes": { "observed": 71, - "pass": 13 + "pass": 13, + "unverified": 1 }, "quest": { "pass": 170 diff --git a/mojo/probes/float-overflow.mojo b/mojo/probes/float-overflow.mojo new file mode 100644 index 0000000..e5e6554 --- /dev/null +++ b/mojo/probes/float-overflow.mojo @@ -0,0 +1,5 @@ +# probe: does a Float64 product past the largest double give inf at run time, and inf - inf nan? +def main(): + var big = 1.0e308 + var x = big * 10.0 + print(x, x - x) diff --git a/src/engines/pseudo/interp.js b/src/engines/pseudo/interp.js index edae7e1..ace9c57 100644 --- a/src/engines/pseudo/interp.js +++ b/src/engines/pseudo/interp.js @@ -919,7 +919,8 @@ class Interpreter { /* ---------- expressions ---------- */ evaluate(e) { switch (e.kind) { - case 'Num': case 'Str': case 'Char': case 'Bool': return e.value; + case 'Num': return this.finiteCheck(e.value, e); + case 'Str': case 'Char': case 'Bool': return e.value; case 'Name': { if (up(e.name) === 'RESULT' && e.sym?.kind === 'result') { const s = this.frame.vars.get('RESULT'); @@ -977,6 +978,7 @@ class Interpreter { } intCheck(v, e) { + this.finiteCheck(v, e); if (typeof v === 'number' && Number.isInteger(v) && !Number.isSafeInteger(v) && e.type?.k === 'INTEGER') { throw new PseudoError('overflow', v > 0 ? `${sourceOf(e)} comes to more than 9,007,199,254,740,991, the largest whole number this lab can hold exactly.` @@ -985,6 +987,21 @@ class Interpreter { return v; } + /** + * A FLOAT result that is not a finite number stops the run, as a division by + * zero does. The prover reasons about FLOAT values as numbers (X == X, and + * X < Y or X >= Y), which infinities and NaN are not; stopping here keeps every + * run inside what a proof covers (tests/prover/float-overflow.pseudo). + */ + finiteCheck(v, e) { + if (typeof v === 'number' && !Number.isFinite(v)) { + throw new PseudoError('overflow', Number.isNaN(v) + ? `${sourceOf(e)} has no number as its answer: the calculation went past the largest FLOAT this lab can hold, about 1.8 × 10^308, along the way.` + : `${sourceOf(e)} comes to ${v > 0 ? 'more than' : 'less than minus'} about 1.8 × 10^308, beyond the largest FLOAT this lab can hold.`, e); + } + return v; + } + binary(e) { const op = e.op; if (op === 'AND') { const l = this.evaluate(e.left); return l ? Boolean(this.evaluate(e.right)) : false; } @@ -997,7 +1014,7 @@ class Interpreter { case '*': return this.intCheck(l * r, e); case '/': if (r === 0) throw this.zeroError(e); - return l / r; + return this.finiteCheck(l / r, e); case 'DIV': if (r === 0) throw this.zeroError(e); return Math.floor(l / r); @@ -1131,7 +1148,7 @@ class Interpreter { return this.invoke(sub, slots, e, false); } const args = e.args.map((a) => this.evaluate(a)); - return this.builtin(up(e.name), args, e); + return this.finiteCheck(this.builtin(up(e.name), args, e), e); } /** A whole-number result that must still fit exactly. */ diff --git a/src/engines/pseudo/vcgen.js b/src/engines/pseudo/vcgen.js index bb73323..ae295e5 100644 --- a/src/engines/pseudo/vcgen.js +++ b/src/engines/pseudo/vcgen.js @@ -202,6 +202,7 @@ class Exec { // result except that the same operation on the same values gives it // again; comparisons of FLOAT values stay exact. if (['+', '-', '*', '/', '^'].includes(op) && (op === '/' || sortOf(e.type) === 'float' || e.left.type?.k === 'FLOAT' || e.right.type?.k === 'FLOAT')) { + if (!ctx.spec) this.floatArith = true; return app(`FLOAT${op}`, [l, r], 'float'); } if (!ctx.spec && ['+', '-', '*', '^'].includes(op) && sortOf(e.type) === 'int') this.intArith = e.line ?? e.left?.line ?? true; @@ -887,6 +888,7 @@ export function verifyUnit(program, unit, options = {}) { // The number model behind "proved": INTEGER is a whole number of any size. // Neither the lab's limit (a run stops past 2^53 - 1) nor Mojo's 64-bit Int // (which wraps past 2^63 - 1, probes/int-overflow-int) is part of the proof. + if (ex.floatArith) ex.notes.push({ line: unit.node?.line ?? 1, kind: 'float-model', text: 'The proofs here treat a FLOAT as a finite number and assume nothing about a rounded result except that the same calculation gives it again. A run in this lab stops when a FLOAT passes about 1.8 × 10^308; in the Mojo made from this program, a Float64 division by zero gives inf or nan and the run goes on (probes/float-special).' }); if (ex.intArith) ex.notes.push({ line: unit.node?.line ?? 1, kind: 'int-model', text: 'The proofs here treat INTEGER as a whole number of any size. A run in this lab stops when a whole number passes 9,007,199,254,740,991, and the Mojo made from this program uses a 64-bit Int, which wraps around past 9,223,372,036,854,775,807. Neither limit is part of the proof.' }); const groups = new Map(); for (const o of ex.raw) { diff --git a/tests/prover-soundness.test.mjs b/tests/prover-soundness.test.mjs index d08c2e3..1b9bdb8 100644 --- a/tests/prover-soundness.test.mjs +++ b/tests/prover-soundness.test.mjs @@ -2,7 +2,9 @@ // // 1. The regression programs in tests/prover/ each name, in a `// MUST-NOT-PROVE: @` // header, an obligation that a run of the program breaks. Each one was proved by an unsound -// rule once; the rule is fixed, and the obligation may never be proved again. +// rule once; the rule is fixed, and the obligation may never be proved again. A program whose +// fix was to the language instead (a run that now stops where it used to go on outside what a +// proof covers) names where it stops with `// RUN-STOPS: @`. // 2. Across every exercise, every corpus program, every lesson solution and those regression // programs, no obligation the prover marks proved is broken by the fuzzer (matched by kind, // line and column) or by running the program as written (matched by kind and line). @@ -71,7 +73,12 @@ test('regression programs: an obligation an unsound rule once proved is never pr for (const f of files) { const src = fs.readFileSync(path.join(PROVER_DIR, f), 'utf8'); const marks = [...src.matchAll(/^\/\/ MUST-NOT-PROVE: ([a-z-]+)@(\d+)\s*$/gm)].map((m) => ({ kind: m[1], line: Number(m[2]) })); - assert.ok(marks.length, `${f} has no MUST-NOT-PROVE line`); + const stops = [...src.matchAll(/^\/\/ RUN-STOPS: ([a-z-]+)@(\d+)\s*$/gm)].map((m) => `${m[1]}@${m[2]}`); + assert.ok(marks.length || stops.length, `${f} has no MUST-NOT-PROVE or RUN-STOPS line`); + if (stops.length) { + const run = runPseudo(studyOf(src), {}); + assert.ok(run.error && stops.includes(`${run.error.kind}@${run.error.line}`), `${f}: the run should stop at ${stops.join(' or ')}, and ${run.error ? `stopped at ${run.error.kind}@${run.error.line}` : 'it finished'}`); + } const st = studyOf(src); assert.ok(st.ok, `${f} does not parse`); const r = verify(st.program.program, { budgetMs: 3000 }); diff --git a/tests/prover/float-overflow.pseudo b/tests/prover/float-overflow.pseudo new file mode 100644 index 0000000..6fdad76 --- /dev/null +++ b/tests/prover/float-overflow.pseudo @@ -0,0 +1,10 @@ +// The prover treats a FLOAT as a number: Y == Y, and X < X + 1.0 or not. +// Infinity and NaN are not numbers in that sense. A run that let 1.0E308 * +// 10.0 become infinity, and X - X become NaN, broke both claims while the +// checker proved them. Such a run now stops at the overflow instead. +// RUN-STOPS: overflow@7 +DECLARE X, Y : FLOAT +X <- 1.0E308 * 10.0 +Y <- X - X +ASSERT Y == Y +OUTPUT Y