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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
This commit is contained in:
7 files changed
+58
-7
No files matched your search
@@ -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. */
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user