diff --git a/tests/prover-soundness.test.mjs b/tests/prover-soundness.test.mjs index 1b9bdb8..1635bca 100644 --- a/tests/prover-soundness.test.mjs +++ b/tests/prover-soundness.test.mjs @@ -123,3 +123,30 @@ test('nothing the prover proves is broken by a run or by generated inputs', () = assert.ok(proved >= 150, `only ${proved} proved obligations were checked`); assert.deepEqual(problems, []); }); + +test('canary: after a program that runs to its end, ASSERT 1 == 2 is never proved', () => { + // A false claim proved at a point every run reaches means the checker holds a contradiction + // there: some rule assumed a fact a run can break. This finds such a rule even when no + // program happens to assert something false after it (tests/prover/foreach-empty.pseudo). + const problems = []; + let canaries = 0; + for (const p of programs()) { + const st = studyOf(p.src); + if (!st.ok || /^\s*INPUT\b/m.test(p.src)) continue; + let run; + try { run = runPseudo(st, {}); } catch { continue; } + if (run.error || run.truncated) continue; + const lines = p.src.replace(/\s+$/, '').split('\n'); + const line = lines.length + 1; + const withCanary = studyOf(`${lines.join('\n')}\nASSERT 1 == 2\n`); + if (!withCanary.ok) continue; + const r = verify(withCanary.program.program, { budgetMs: 3000 }); + const main = r.units.find((u) => u.kind === 'main'); + const o = main?.obligations.find((x) => x.kind === 'assert' && x.line === line); + if (!o) continue; + canaries++; + if (o.status === 'proved') problems.push(`${p.name}: ASSERT 1 == 2 after the last line is proved`); + } + assert.ok(canaries >= 60, `only ${canaries} programs carried a canary`); + assert.deepEqual(problems, []); +}); diff --git a/tests/prover/foreach-empty.pseudo b/tests/prover/foreach-empty.pseudo new file mode 100644 index 0000000..a364e22 --- /dev/null +++ b/tests/prover/foreach-empty.pseudo @@ -0,0 +1,13 @@ +// The way out of a FOR EACH loop includes the case of no pass at all. A +// checker that kept "X is in Xs" on the way out held FALSE after looping over +// an empty list, and proved anything, even 1 == 2. +// MUST-NOT-PROVE: assert@12 +DECLARE Xs : LIST OF INTEGER +DECLARE S : INTEGER +Xs <- [] +S <- 0 +FOR EACH X IN Xs + S <- S + X +NEXT X +ASSERT 1 == 2 +OUTPUT S