Keep "X is in the collection" inside a FOR EACH pass, not on the way out

The way out of a FOR EACH loop includes the case of no pass at all, but the
checker added "X is in the collection" to the loop's head state and then used
that same state as the way out. After a loop over an empty list the fact is
FALSE, so the checker held a contradiction and proved anything after the
loop, ASSERT 1 == 2 included (tests/prover/foreach-empty.pseudo). The fact is
now assumed only for the body of a pass.

The soundness test gains a canary: after every program that runs to its end
without input, ASSERT 1 == 2 is appended and must not be proved, since a
proof there means a contradiction on a path every run takes. A one-off scan
with the canary after every top-level statement of every program (476
points) found nothing else.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
This commit is contained in:
alexmandClaude committed 2026-10-05 03:26:43 +00:00
1 parent f219384e57
commit 765459c340
3 files changed
+48 -3

No files matched your search

+8 -3
View File
@@ -625,11 +625,16 @@ class Exec {
const H = this.havoc(E, modified, s.line);
const x = this.fresh(s.var, sortOf(s.eachSym?.type));
H.env.set(X, x);
if (coll.k === 'app' && coll.f === 'RANGE') H.facts.push(and(cmp('<=', coll.args[0], x), cmp('<', x, coll.args[1])));
else if (coll.t === 'list' || coll.t === 'set' || coll.t === 'map') H.facts.push(member(x, coll));
for (const inv of this.userInvariants(s, H)) H.facts.push(inv.term);
// X is in the collection only inside a pass. The loop also ends without
// any pass when the collection is empty, so the way out must not know it:
// for an empty list that fact is FALSE, and the checker would then prove
// anything after the loop (tests/prover/foreach-empty.pseudo).
let inPass = H;
if (coll.k === 'app' && coll.f === 'RANGE') inPass = H.assume(and(cmp('<=', coll.args[0], x), cmp('<', x, coll.args[1])));
else if (coll.t === 'list' || coll.t === 'set' || coll.t === 'map') inPass = H.assume(member(x, coll));
const inner = { breaks: [], continues: [] };
const ends = this.run(s.body, [H], inner);
const ends = this.run(s.body, [inPass], inner);
for (const st of [...ends, ...inner.continues]) {
for (const inv of this.userInvariants(s, st)) this.obligation('invariant-keep', inv.node, `each pass keeps the INVARIANT`, st, [], inv.term);
}