Skip to content

feat: let specifications call recursive spec functions (stage 11d) - #102

Merged
Sequela02 merged 4 commits into
mainfrom
feat/stage-11-spec-fn
Nov 28, 2026
Merged

Sequela02 merged 4 commits into
mainfrom
feat/stage-11-spec-fn

Conversation

@Sequela02

Copy link
Copy Markdown
Contributor

The semver port (11h) states its comparator's contract through a recursive definition, the first index where two lists differ, and until now a specification couldn't call any function. Slice 11d of stage 11 (#83) adds spec fn, per syntax decision 11 and decisions 1 and 2 of the 2026-11-13 second set. The conformance cases come in their own commit, before the compiler code; a second commit corrects three of them that running showed wrong.

Where to start reading: crates/accord/src/obligation/specs.rs (the encoding), then check/calls.rs and check.rs (the rules).

What it adds

  • spec fn name(params) -> Type body: called only from requires, ensures, decreases and other spec functions' bodies; never from code, never in an example, never written as TypeScript. Its integers are mathematical, so its arithmetic carries no range obligation.
  • A spec function is its own definition, for every input: no requires, ensures or open when. Its own obligations (index in range, and termination at each call to itself) assume nothing of its parameters.
  • Encoding, as measured in docs/design/measurements/2026-11-13-semver-contract/ (B2, ground + reveal):
    • one that calls itself is an uninterpreted f.fuel, reached through a define-fun that gives it one unit of fuel, with its body as f.unfold;
    • its unfolding and fuel-irrelevance axioms are written once per list a script's calls name, quantified only over the fuel and the other parameters, and only into scripts that call it (ground_specs, which runs beside limit_lengths in the solver);
    • each call that a function's own requires makes outside any quantifier also gets one ground step of its unfolding;
    • its own obligations declare no f.unfold for it, so its calls to itself are uninterpreted: an inconsistent definition, such as f(n) = f(n) + 1, can't prove its own termination.
  • The interpreter runs spec functions inside contracts, with mathematical integers, so counterexamples and examples that reach one replay. Contract evaluation (broken, meets, covered, admitted) now takes the program's functions.
  • A caller's identity covers the body of every spec function its contracts reach; a spec function's canonical text is marked (spec).

Decisions taken within the slice

  • A spec function that doesn't call itself is a define-fun, exact, with no fuel.
  • Rejected for now, each liftable later without breaking a program: pub spec fn; a spec function returning a type that holds a list; a call to itself that changes a parameter holding a list (the solvers unfold only at lists the obligation names; the measurement's "a spec function whose recursion changes a sequence argument gets no axiom"); a call to a spec function from a machine's clauses; accord build of a requires that calls one (the TypeScript boundary would have to run it).
  • accord diff reports a contract that calls a spec function as undecided: each revision's spec functions are normalized apart, so one name could stand for two definitions.

Verification

On Windows with the pinned Z3 5.3.3 and cvc5 1.4.2:

  • cargo fmt --all --check: passed. cargo clippy --workspace --all-targets --locked -- -D warnings: passed.
  • cargo test --workspace --locked: 118 passed, 0 failed, 1 ignored (already ignored on main).
  • score.py --self-test and materials.py target/debug/accord: passed; review hashes unchanged.
  • Conformance: 21 new cases. 9 that check, with 4 planted faults: a caller's bound (refuted), a spec function's unguarded index (refuted), a measure that goes negative (refuted), and a wrong result in find (undecided, its example fails; below). 12 rejections, each confirmed by hand to fail for the reason it states. conformance/typescript/spec-fn matches byte for byte; its expected module was written before the code, from the same program without the spec function, which the generator already wrote.
  • New tests: renaming a spec function changes no identity; changing its body changes every caller's, directly or through another spec function; a spec fn and a fn with one body differ; accord build refuses a requires that calls a spec function. The differential test confirmed that refusal is needed: a module written past it threw a ReferenceError.
  • Linux and macOS: pending (CI, full-ci).

Not established

  • A fault refuted only through a recursive spec function stays undecided. On spec-fn-recursive-fault, Z3 and cvc5 both answer unknown within 10 s on the ensures (no model satisfies the quantified axioms), as the measurement's part B found; the case expects undecided and its failing example catches the fault. Refutation by a replayed input is 11e.
  • No proof-time measurement of Accord-generated spec obligations beyond the conformance run, which the measurement left open.
  • The scripts/check-*.sh integrations weren't run; no existing program uses spec fn.

Risk note

Two-way door: a language addition behind conformance cases; the receipt schema doesn't change. Contract evaluation now takes the program's functions, an internal signature. Blast radius: programs without spec fn write the same scripts (no f.unfold, so ground_specs returns them unchanged) and keep their outcomes, identities and TypeScript; the full suite shows no change to them. Evidence: the checks above.

Refs #83

Atrium: 01a1e409-d7af-75da-ba16-29e2cf249547

Stage 11d (#83) adds spec functions: callable only from specifications,
recursive with decreases, defined for every input and absent from the
generated TypeScript. These cases fix the expected outcomes before the
compiler code: proofs through a recursive spec function, planted faults in
a caller, in a spec function's index and in its measure, a requires that
needs one unfolding, and the declarations to reject.

Refs #83
Specifications need recursive definitions the solvers can unfold: the
semver port states its comparator's contract through a first-difference
function (syntax decision 11, #83). A spec fn is called only from
specifications, has mathematical integers, no contract of its own, and
obligations that hold for every input.

It is encoded as the 2026-11-13 measurement settled: a fuel-limited
uninterpreted function with unfolding and fuel-irrelevance axioms written
once per list a script names, never quantified over a sequence, since cvc5
1.4.2 recurses without bound on such a quantifier (cvc5/cvc5#13161); and a
call-site reveal for each recursive call a requires makes. A spec
function's own obligations don't assume its definition, so an inconsistent
one can't prove itself. The interpreter runs spec functions inside
contracts so models still replay, and a caller's identity covers the
bodies of the spec functions its contracts reach.

Refs #83
Two cases passed an i32 to an i64 parameter, which the checker rejects
before any obligation. The planted fault in find's contract stays
undecided: neither solver finds a model of the fuel axioms, as the
measurement's part B predicted, so it waits for refutation by a replayed
input (11e); its example still catches it. A refused build is a
diagnostic, not a case without out/, so that case moves to the build
refusal test.

Refs #83
The syntax reference gains a section on spec fn and marks stage 11's
syntax decisions implemented, decision 12 included, which shipped in
#99. The TypeScript reference names the new refusal.

Refs #83
@Sequela02 Sequela02 added the full-ci Run Windows and macOS CI once before merging label Nov 28, 2026
@Sequela02
Sequela02 merged commit 83eb410 into main Nov 28, 2026
5 checks passed
@Sequela02
Sequela02 deleted the feat/stage-11-spec-fn branch November 28, 2026 06:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci Run Windows and macOS CI once before merging

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant