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:
1 parent
8f403226fb
commit
08c74c16d2
6 files changed
+56
-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 |
|
||||
| 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 |
|
||||
|
||||
+10
-1
@@ -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
|
||||
|
||||
@@ -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 ---------- */
|
||||
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. */
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
//
|
||||
// 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
|
||||
// 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
|
||||
// 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 });
|
||||
|
||||
@@ -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