Skip to content

feat: assume a text holds at most 2^53 - 1 code points - #100

Merged
Sequela02 merged 5 commits into
mainfrom
feat/stage-11-text-length-limit
Nov 19, 2026
Merged

Sequela02 merged 5 commits into
mainfrom
feat/stage-11-text-length-limit

Conversation

@Sequela02

Copy link
Copy Markdown
Contributor

Follow-up to 11b (#99), stacked on it: every text is assumed to hold at most 2^53 − 1 code points, so arithmetic on length(text) can be proved to stay within i64.

The problem

SMT-LIB's str.len is unbounded. On #99, length(word) + 1 and length(word) * 2 are undecided on "stays within i64": cvc5 answers with a text 2^63 code points long and Z3 answers unknown. Callers also assume a callee's i64 result fits.

The change

  • obligation/lengths.rs asserts (<= (str.len X) 9007199254740991) for each (str.len X) an assertion holds, under the guards and lets of the quantifiers around it. 2^53 − 1 is the most a JavaScript string holds, so generated code guarantees it.
  • The pass runs in Solvers::decide_calls, where every script reaches the solvers: proofs, call contracts, count axioms, mutants, open-case searches and diff.
  • It never states the limit of a define-fun's parameter. That would say every string is short, which the theory of strings contradicts, so every claim would be proved. Each use of a definition is expanded first, with its bound names renamed so no argument is captured.
  • It adds nothing under a quantifier over anything but Int, or of a shape other than the list-index guard, and nothing to a script with no str.len.
  • The receipt's not_proved gains text_assumptions where the function or a callee's contract takes a text's length. Docs: syntax.md, receipt.md, row 6 of native-code-constraints.md (native code must guarantee or check the limit).

The measurement

docs/design/measurements/2026-11-18-text-length-limit: the 45 scripts #99's compiler writes for twelve functions, run with no limit, a limit on parameters (A), a limit at every use (B, this PR) and a saturated length, five seeds per solver.

none A B saturate
Proved 23 26 36 32
  • B proves every script the others do. The 10 that only B proves take a length of a list element, inside a quantifier, or through a callee's contract.
  • No answer changed between seeds, and cvc5 didn't crash on the 27 scripts with an index-quantified fact (Unbounded recursion with --mbqi on a quantifier over a Seq variable cvc5/cvc5#13161 is about quantifiers over sequences).
  • Saturating the length loses 4 proofs that even no limit keeps.

Review focus

  • The pass works on the finished SMT text rather than the syntax tree. That way no generator can miss it, at the cost of a small s-expression parser.
  • Cost: in one open-case script, Z3 takes 4 to 10 s under the limit against 0.1 s without it (unknown either way). The conformance suite went from about 37 s to about 60 s; the two new cases each hold an obligation that runs to the 10 s solver limit, which accounts for most of that, not measured separately.

Verification

On Windows, with the pinned Z3 5.3.3 and cvc5 1.4.2:

  • text-length-limit.accord was written first: 7 of its 17 obligations were undecided before the change, and all 17 are proved after. text-length-limit-fault.accord stays undecided, as it must.
  • 7 unit tests for the pass: ground and quantified sites, a definition's parameter, capture, an unknown shape, a quantifier over texts, a text literal.
  • cargo fmt --all --check passes; cargo clippy --workspace --all-targets --locked -- -D warnings is clean; cargo test --workspace --locked --no-fail-fast passes.
  • score.py --self-test and materials.py target/debug/accord pass; review hashes are unchanged.
  • Running the measured programs through the new compiler gives the outcomes the measurement predicts.

Not established: Linux and macOS (CI with full-ci). Not measured: nested lists of texts, texts in enum fields, large programs.

A separate finding, filed on its own: false contracts over List<Text> come out undecided rather than refuted, because Z3 answers unknown. The limit doesn't change it.

Risk note

Two-way door: a new trusted assumption, listed in the receipt; the receipt schema doesn't change. Blast radius: scripts that take a text's length, which only exist since #99; every other script is unchanged, so its ledger entries are reused. Evidence: the cases, unit tests and measurement above.

Refs #83

Atrium-Closes: 01a1f241-0f3c-74d1-97c0-e14fa2ac3cf6

Decision 12 makes `length` and `<` on `Text` count and compare code
points, as the solvers' `str.len` and `str.<` do, so a program that
orders texts is no longer an error. The cases pin it before the
compiler does: a contract that holds and one that breaks for each,
enum ordering still refused, and a pair across U+FFFF ("z" before
"😀"), where UTF-16 order disagrees. A TypeScript case pins the
generated module. text-ordered-law pins that cvc5 proves no order
law of `str.<`, so such an obligation is undecided.

Refs #83
`length` on a text and `<`, `<=`, `>`, `>=` between texts now check,
prove and run. The checker writes them as their own nodes, so each
backend can't confuse them with list length or integer order: proofs
use `str.len`, `str.<` and `str.<=`, and the interpreter compares
Rust strings, whose UTF-8 order is code-point order.

JavaScript's `.length` and `<` count and compare UTF-16 units, which
disagree with the solvers above U+FFFF. Generated TypeScript calls
its own `textLength$` and `textLess$` instead, emitted only in the
modules that use them, and the differential samples gain U+FF5A,
which UTF-16 orders after U+1D11E, so a unit-based helper fails it.

Refs #83
SMT-LIB's str.len is unbounded, so the solvers can't show that
arithmetic on `length(text)` stays within `i64`: cvc5 answers with a
text 2^63 code points long. The measurement runs the scripts Accord
writes for twelve functions with no limit, a limit on parameters, a
limit at every use, and a saturated length, under five seeds.

A limit at every use proves 36 of 45 scripts against 26 and 23, keeps
every proof the others give, and stays clear of cvc5#13161. Saturating
the length loses four proofs.

Refs #83
The limit must decide arithmetic on a length wherever it is taken: of
a parameter, a field, an element, inside `all`, `any` and `count`, and
through a callee's contract. The fault case checks it decides nothing
it shouldn't: 2000 times a length can still leave `i64`.

Refs #83
Without a limit, no proof that arithmetic on a text's length stays
within `i64` goes through, and callers assume a callee's length fits.
2^53 - 1 is the most a JavaScript string holds, so generated code
guarantees it; native code will have to check it.

Every script now states the limit of each length its assertions take,
under the quantifiers around it, as the measurement did. The pass runs
where every script reaches the solvers, so no generator can miss it.
It never states the limit of a definition's parameter, which would
limit every string and contradict the theory, and it skips quantifiers
it doesn't recognize. The receipt lists the assumption where a proof
can rest on it.

Refs #83
@Sequela02 Sequela02 added the full-ci Run Windows and macOS CI once before merging label Nov 19, 2026
@Sequela02
Sequela02 changed the base branch from feat/stage-11-text-code-points to main November 19, 2026 06:15
@Sequela02
Sequela02 merged commit 93c29c1 into main Nov 19, 2026
5 checks passed
@Sequela02
Sequela02 deleted the feat/stage-11-text-length-limit branch November 19, 2026 06:27
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