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
@@ -7,7 +7,7 @@ Generated by `python3 scripts/mojo/corpus.py status` from the goldens in this di
|
|||||||
| diagnostics | 0 | 0 | 0 | 42 | 132 |
|
| diagnostics | 0 | 0 | 0 | 42 | 132 |
|
||||||
| local | 18 | 0 | 0 | 0 | 0 |
|
| local | 18 | 0 | 0 | 0 | 0 |
|
||||||
| manual | 144 | 55 | 0 | 1 | 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 |
|
| quest | 170 | 0 | 0 | 0 | 0 |
|
||||||
| stress | 40 | 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-try-finally` | the order of try, except and finally | observed |
|
||||||
| `error-unhandled` | what reaches stdout, stderr, and the exit code when main raises? | 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-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-print` | how does print format Float64 values? | observed |
|
||||||
| `float-round` | what does round() do on halves? | 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 |
|
| `float-special` | how are inf, -inf and nan printed after a run-time division by zero? | observed |
|
||||||
|
|||||||
+10
-1
@@ -3129,6 +3129,14 @@
|
|||||||
"verdict": "observed",
|
"verdict": "observed",
|
||||||
"why": "did not compile"
|
"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",
|
"corpus": "probes",
|
||||||
"id": "probes/float-print",
|
"id": "probes/float-print",
|
||||||
@@ -5506,7 +5514,8 @@
|
|||||||
},
|
},
|
||||||
"probes": {
|
"probes": {
|
||||||
"observed": 71,
|
"observed": 71,
|
||||||
"pass": 13
|
"pass": 13,
|
||||||
|
"unverified": 1
|
||||||
},
|
},
|
||||||
"quest": {
|
"quest": {
|
||||||
"pass": 170
|
"pass": 170
|
||||||
|
|||||||
@@ -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)
|
||||||
@@ -919,7 +919,8 @@ class Interpreter {
|
|||||||
/* ---------- expressions ---------- */
|
/* ---------- expressions ---------- */
|
||||||
evaluate(e) {
|
evaluate(e) {
|
||||||
switch (e.kind) {
|
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': {
|
case 'Name': {
|
||||||
if (up(e.name) === 'RESULT' && e.sym?.kind === 'result') {
|
if (up(e.name) === 'RESULT' && e.sym?.kind === 'result') {
|
||||||
const s = this.frame.vars.get('RESULT');
|
const s = this.frame.vars.get('RESULT');
|
||||||
@@ -977,6 +978,7 @@ class Interpreter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
intCheck(v, e) {
|
intCheck(v, e) {
|
||||||
|
this.finiteCheck(v, e);
|
||||||
if (typeof v === 'number' && Number.isInteger(v) && !Number.isSafeInteger(v) && e.type?.k === 'INTEGER') {
|
if (typeof v === 'number' && Number.isInteger(v) && !Number.isSafeInteger(v) && e.type?.k === 'INTEGER') {
|
||||||
throw new PseudoError('overflow', v > 0
|
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.`
|
? `${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;
|
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) {
|
binary(e) {
|
||||||
const op = e.op;
|
const op = e.op;
|
||||||
if (op === 'AND') { const l = this.evaluate(e.left); return l ? Boolean(this.evaluate(e.right)) : false; }
|
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 '*': return this.intCheck(l * r, e);
|
||||||
case '/':
|
case '/':
|
||||||
if (r === 0) throw this.zeroError(e);
|
if (r === 0) throw this.zeroError(e);
|
||||||
return l / r;
|
return this.finiteCheck(l / r, e);
|
||||||
case 'DIV':
|
case 'DIV':
|
||||||
if (r === 0) throw this.zeroError(e);
|
if (r === 0) throw this.zeroError(e);
|
||||||
return Math.floor(l / r);
|
return Math.floor(l / r);
|
||||||
@@ -1131,7 +1148,7 @@ class Interpreter {
|
|||||||
return this.invoke(sub, slots, e, false);
|
return this.invoke(sub, slots, e, false);
|
||||||
}
|
}
|
||||||
const args = e.args.map((a) => this.evaluate(a));
|
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. */
|
/** 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
|
// result except that the same operation on the same values gives it
|
||||||
// again; comparisons of FLOAT values stay exact.
|
// 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 (['+', '-', '*', '/', '^'].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');
|
return app(`FLOAT${op}`, [l, r], 'float');
|
||||||
}
|
}
|
||||||
if (!ctx.spec && ['+', '-', '*', '^'].includes(op) && sortOf(e.type) === 'int') this.intArith = e.line ?? e.left?.line ?? true;
|
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.
|
// 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
|
// 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.
|
// (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.' });
|
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();
|
const groups = new Map();
|
||||||
for (const o of ex.raw) {
|
for (const o of ex.raw) {
|
||||||
|
|||||||
@@ -2,7 +2,9 @@
|
|||||||
//
|
//
|
||||||
// 1. The regression programs in tests/prover/ each name, in a `// MUST-NOT-PROVE: <kind>@<line>`
|
// 1. The regression programs in tests/prover/ each name, in a `// MUST-NOT-PROVE: <kind>@<line>`
|
||||||
// header, an obligation that a run of the program breaks. Each one was proved by an unsound
|
// 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: <runtime kind>@<line>`.
|
||||||
// 2. Across every exercise, every corpus program, every lesson solution and those regression
|
// 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,
|
// 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).
|
// 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) {
|
for (const f of files) {
|
||||||
const src = fs.readFileSync(path.join(PROVER_DIR, f), 'utf8');
|
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]) }));
|
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);
|
const st = studyOf(src);
|
||||||
assert.ok(st.ok, `${f} does not parse`);
|
assert.ok(st.ok, `${f} does not parse`);
|
||||||
const r = verify(st.program.program, { budgetMs: 3000 });
|
const r = verify(st.program.program, { budgetMs: 3000 });
|
||||||
|
|||||||
@@ -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
|
||||||
Reference in new issue
Block a user