Skip to content

[0.5.0] #279: a fallible loop's ends are in its function's tail (RB-21) - #300

Merged
humuhumu33 merged 8 commits into
release/v0.5from
claude/issue-279-loop-end-tail-call
Oct 25, 2026
Merged

humuhumu33 merged 8 commits into
release/v0.5from
claude/issue-279-loop-end-tail-call

Conversation

@humuhumu33

Copy link
Copy Markdown
Contributor

Fixes #279. Targets release/v0.5, merged with its head bde798bd6 (#285, #287).

The rule

A function whose self calls are all tail calls is rendered as a loop (§17.16, RB-14). A tail self call continues the loop with Ok of the next state; every other tail value e ends it as Err(e), so the loop breaks with e. When the function is fallible and the loop is its value, e is what the function returns. The operand of each such Err is therefore in tail position.

The lowering already rendered the module of #279 that way (Err::<u64, R<u64>>(f2(v1)), then break done). But rust::validate checked the whole loop body with tail = false, so Checker::call refused the call with LLB6005. The validator now checks the operand of each Err at a tail of such a loop as the function's tail, and every other position of the body as before. LexLeanBoot.RustValidate (BS-05) mirrors this: Cx gains ends, and endArgsC checks those operands with tail = true.

One behaviour change beyond the issue: the old end check looked only at an operand's outer construct, so a fallible loop ending with Err(if … { () } else { () }) was accepted. Checking the operand as the function's tail refuses it, in the oracle and in the module alike. No rendered crate has that shape (all 223 are still accepted). One hand-built BS-05 crate does (run 153).

Rows

  • RB-21 (new, §17.13, §17.16). The project tests/rust-backend/loop-ends holds the module of Production: a looped fallible function cannot tail-call another fallible function (LLB6005) #279 as filed and a variant whose callee is itself a loop. The case checks that both:

    • render in rust-std and end their loops with the callee's result as it is;
    • agree with the calculus evaluator under the pinned rustc on 54 inputs, the callee's overflow among them;
    • under Lean 4.32.1, export and replay, with exported programs equal to the ones the case ran.

    It also plants the call bound before the loop's end, and the end dropped; both are refused.

  • BS-05 (counts). 3 hand-built loop crates (37): the issue's shape is accepted, and both plants are refused. A seventeenth bootstrap plant. Vectors are 409 validateCrate (168 refused) and 272 correspondCrate (49 refused), 681 in all; evaluator vectors are 386 + 249.

  • Register 444 → 445 after the merge with AU-14. README count, RB-01..RB-21.

Plants (all detected locally)

  • RB-21, the fix reverted: LoopEnds.Main.run: no rendering in rust-std: arithmetic: the fallible call of f2 does not propagate its overflow (the issue's own error).
  • RB-21, every tail of the loop body checked as the function's tail: ... the value a fallible function returns, construct:ok, is not a fallible value.
  • BS-05 plant 17, the end of a fallible loop not in the function's tail (ends := gated}) body gated) → ends := false}) body false)): run 153, where the planted module gives ok () and the oracle refuses lit:unit.
  • The 16 earlier BS-05 plants are still detected.

Identity

validate.rs is a renderer source, so rust_renderer moves, and with it the language-1.2 compiler-semantics ID (5262e5b4c8b5… → 138ffb6279c2…). All 241 language-1.2 locks were relocked; only the compiler_semantics line changes. The records were regenerated with check-calculus, check-fixtures, check-golden, check-production-export and verify-examples. uor-atlas, a language-1.0 project the move does not touch, was left out. Of the 2,266 changed files, the 1,029 deletions are hash-named audit files, each renamed with a successor.

Run locally

conformance_rb_21 (208 s), conformance_bs_05 (952 s: 681 + 4,000 rustc runs, the evaluator, 17 plants), conformance_rp_07, rp_11, ns_07, au_14; validate-model, validate-spec-links, check-test-inventory, rustfmt, clippy -D warnings on lexlean and repo-conformance, lexlean fmt --check.

Not verified locally

The full bdd suite and the uor-atlas records; CI carries them. The other open PRs on release/v0.5 that change the register or move the 1.2 identity (#286, #288, #289, #290, #295, #296) will conflict with this branch on counts and records. Whichever lands second regenerates.

Numbering

RB-15..RB-17 are on release/v0.4, RB-18 is in #228/#238, RB-19 in #255, and RB-20 in #295, so this row takes RB-21.

Question for @afflom

A fallible closure application (Expr::Apply) at a loop's end has the same shape as the call fixed here. apply_types still checks a loop body with tail = false, so I expect it to be refused (not tested). #279 is about calls, so I left applications alone. Should they be in this PR, in a follow-up, or not at all?

🤖 Generated with Claude Code

humuhumu33 and others added 6 commits October 22, 2026 23:42
…ible loop (#279)

R3 first: the row RB-21 (§17.13, §17.16), its scenario in
rust-backend.feature, conformance_rb_21, and the committed project
tests/rust-backend/loop-ends, whose Main is the module of #279 as filed and
whose Looped ends its loop with a call of a function that is itself a loop.

The case lowers both roots, renders them in rust-std, and expects the loop
to end with the callee's result as it is; it plants the call bound before
the end and the end dropped, runs the packages under rustc beside the
calculus evaluator, and under Lean exports and replays both roots.

Red on release/v0.5 2959a4d for the issue's reason:
LoopEnds.Main.run: no rendering in rust-std: arithmetic: the fallible call
of f2 does not propagate its overflow

Register 443 -> 444; README count and the RB range (RB-15..RB-20 are held
by the v0.4 line and open PRs, so this is RB-21).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A function whose self calls are all tail calls is rendered as a loop
(RB-14): a tail self call continues it with Ok of the next state, and every
other tail value e ends it as Err(e), so the loop breaks with e. When the
function is fallible and the loop is its value, e is what the function
returns, so a call of another fallible function there returns that
function's result as it is. rust::validate checked the loop body with
tail = false, so it refused the call (LLB6005) and the module of #279 had no
rust-std rendering.

- calculus/rust/validate.rs: the operand of each Err at a tail of such a
  loop is checked as the function's tail; every other position of the body
  as before.
- bootstrap RustValidate (BS-05): Cx gains ends; endArgsC checks those
  operands as the tail. Three hand-built loop crates (37) and a seventeenth
  plant; counts 409 (168) / 272 (49). The old end check looked only at an
  operand's outer construct, so a fallible loop ending with Err of a branch
  of two units was accepted; it is now refused by both (BS-05 run 153).
- SPEC §17.16 (failure typing) and §17.23 (Crate data, BS-05 counts),
  VERIFICATION (RB-21 entry with two plants, BS-05 counts and plant 17),
  CHANGELOG.
- rust_renderer moves, and with it the language-1.2 compiler-semantics ID
  5262e5b4c8b5 -> 138ffb6279c2: 241 locks relocked, records regenerated with
  check-calculus, check-fixtures, check-golden, check-production-export and
  verify-examples (without uor-atlas, a language-1.0 project).

conformance_rb_21 and conformance_bs_05 pass locally; RB-21 includes the
rustc differential and, under Lean 4.32.1, export and replay.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
 into claude/issue-279-loop-end-tail-call

# Conflicts:
#	CHANGELOG.md
…-end-tail-call

CHANGELOG.md conflicted on the unreleased entries; both sides are kept, the
line's (#112, #119) before RB-21.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…ail-call

CHANGELOG.md conflicted again on the unreleased entries (#288 moved the
AU-14 bullet); both sides are kept, the line's before RB-21.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…279)

`just vv (rest)` on 6085271 failed eight cases. `rb_02`..`rb_05`, `rb_07`,
`gc_01` and `gn_01` compared the 351 committed packages, provenance files
and negative manifests under compiler/rust with their generator: their
`sources` named the compiler build's semantic ID before the identity move
(7a08f62b…) while compiler/expected/build/manifest.json names the build
after it (3d5f7b04…), because `check-calculus --write` ran before the
compiler record was regenerated. `check-calculus --write` rewrites them;
nothing else in a record changes. `ns_01` found `spec/src/IdsRB.lex.tex`
not canonical: `lexlean fmt` adds the blank line before `rb21`.

With #288 (RP-17) on release/v0.5 the register holds 446 IDs: README's
count sentence and SPEC.md's total (`validate-model --write`) say so.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@humuhumu33

Copy link
Copy Markdown
Contributor Author

Round on this branch: it is merged with release/v0.5 at 655d9ae06 (#288) and green locally on the cases that were red. Head abda6f4ec; GitHub reports it mergeable.

Merges (merge commits, no rebase)

Both times only CHANGELOG.md conflicted, on the unreleased entries. Both sides are kept, the line's entries (#113, #112, #119, AU-14) before RB-21. Nothing in spec/src, the locks or the identity records conflicted.

Why just vv (rest) was red on 608527165
Eight cases failed; BS-05 was not among them.

  • rb_02, rb_03, rb_04, rb_05, rb_07, gc_01, gn_01: the committed records under compiler/rust (166 package.json, 166 provenance.json, 19 negative manifests) named the compiler build 7a08f62b… in sources. This branch had already moved compiler/expected/build/manifest.json to 3d5f7b04…, so the records were written before the compiler record was regenerated.
  • ns_01: spec/src/IdsRB.lex.tex was not canonical; lexlean fmt wanted a blank line before the rb21 definition.

Both causes are in this PR's change, not on the line, where both the build record and the Rust records name 7a08f62b….

Fix: abda6f4ec

Local results on abda6f4ec's tree (WSL, memory-capped)

  • Passed: conformance_rb_02, rb_03, rb_04, rb_05, rb_07, gc_01, gn_01, ns_01, rp_07, rp_11, rb_21, and bs_05.
  • Passed: validate-model (446 ids, audits clean), validate-spec-links (446), check-fixtures (291), check-golden, check-test-inventory, cargo fmt --check, and lexlean fmt --check in spec/ and bootstrap/.
  • Not run locally: clippy, the full bdd suite, verify-examples, and uor-atlas. CI carries them.

CI on abda6f4ec
plan, honesty and repro passed. just vv (rest) is running.

The PR stays a draft. The question for @afflom in the description (fallible closure applications at a loop's end) is still open.

…ail-call

The line took PC-01 and PC-02 (register 447); RB-21 keeps its number, the
register is 448. README and SPEC.md state 448, the CHANGELOG bullet ends
"Register 448". CHANGELOG.md and VERIFICATION.md merged with both sides, the
line's first. The spec lock keeps this change's compiler-semantics ID
(138ffb62...) with the line's project config hash (it now lists IdsPC). The
performance corpus is a language-1.2 project, so its lock, build manifest and
oracle files regenerate under the moved compiler-semantics ID; the calculus
records, the verification records and the golden oracles are rewritten for it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@humuhumu33

Copy link
Copy Markdown
Contributor Author

Merged release/v0.5 at 34cfc12 (#289) into this branch: merge commit ff7fd46. The PR is MERGEABLE again.

  • Conflicts: README.md and SPEC.md (register count), spec/lexlean.lock. CHANGELOG.md and VERIFICATION.md merged with both sides, the line's first.
  • Renumbering: none. The line took PC-01 and PC-02 in a new family, so RB-21 keeps its number. The register is 448, and README, SPEC.md and the CHANGELOG bullet ("Register 448") say so.
  • Identity move: the spec lock keeps this change's compiler-semantics ID (138ffb62…) and takes the line's project config hash. examples/performance-corpus (new on the line) was locked under the moved ID, and the calculus records, verification records and golden oracles were regenerated. The Atlas is not regenerated, as before.

Local results at ff7fd46: validate-model (448 ids), validate-spec-links (448 rows), fmt (cargo and spec), check-test-inventory, check-fixtures (291) and verify-examples without the Atlas pass. These cases pass: conformance_rb_21, rp_11, rp_07, ns_01, pc_01, pc_02, rb_02..rb_05, rb_07, gc_01, gn_01 (13 cases), and conformance_bs_05 alone. Not run locally: clippy, just vv, the Atlas legs; CI covers them once the PR is ready.

…ail-call

The line took PF-24 (register 448); RB-21 keeps its number, the register is
449. README, SPEC.md and CONFORMANCE.md state 449, the CHANGELOG bullet ends
"Register 449". CHANGELOG.md merged with both sides, the line's first. #296
moved the language-1.2 compiler-semantics ID, and this change's renderer
digest moves it again, so every lock, calculus record, fixture expectation,
verification record, golden oracle and production export is regenerated by
the writers from the merged tree; the Atlas is not regenerated.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@humuhumu33

Copy link
Copy Markdown
Contributor Author

Merged release/v0.5 at 80bf4ba (#296) into this branch again: merge commit 72f106e. MERGEABLE, ready for review, the full gate restarts on this head.

  • Conflicts: CHANGELOG.md (both sides, the line's first) and the generated records (taken from the line, then regenerated). No renumbering: the line took PF-24, RB-21 keeps its number. The register is 449 (README, SPEC.md, CONFORMANCE.md, CHANGELOG bullet "Register 449").
  • #263 PF-24: runtime bounds a law proof cites (split_exact, UTF-8 decode, UInt32 checked_add) #296 moved the 1.2 compiler-semantics ID and this change's renderer digest moves it again, so the locks (including tests/rust-backend/loop-ends), calculus records, fixture expectations, verification records (without the Atlas), golden oracles and production export are regenerated from the merged tree. The Atlas is not regenerated.

Local results: validate-model (449 ids), validate-spec-links, check-calculus, check-fixtures (291), check-golden, check-production-export, check-test-inventory and cargo fmt pass. Cases rb_21 (after the relock above), pf_24, pc_01, pc_02, rb_02..rb_05, rb_07, gc_01, gn_01, ns_01, rp_07 and rp_11 pass. Not rerun on the final tree: conformance_bs_05, clippy, just vv, the Atlas legs; CI covers them.

afflom commented Oct 25, 2026

Copy link
Copy Markdown
Member

Maintainer review of 72f106e27 on release/v0.5 at 80bf4bae9 (read from the diff, nothing built; the gate on this head is green, 30 of 30). Accepted, one short round before the merge; nothing blocking.

What checked out. The defect is as #279 states it: succeed already flipped a fallible tail call to propagate: false and retarget wrapped it as Err(f2(v1)), but the checker read the loop body with tail = false, so Checker::call refused the call as a dropped failure (LLB6005). The fix is where it belongs: Checker.ends (validate.rs:92-96), the loop body checked as tail exactly when the function is fallible and the loop is its tail (validate.rs:540-545), the operands of an Err checked as the function's tail with ends cleared, If/Match/Block passing the position on (validate.rs:320-340); the bootstrap mirror (Cx.ends, endArgsC) is the same shape. R3 order held: RB-21 row (ids.toml:1813), scenario rust-backend.feature:97-101 byte-equal to the register statement, conformance_rb_21 (conformance.rs:1508), then the implementation. §31 row and IdsRB.lex.tex match; the counts say 449 at README, SPEC and CHANGELOG (spec_links.rs and repository.rs compare against the model's length and carry no literal). Two plants with their red lines (VERIFICATION.md:14593-14640; line 253 of rust_loop_ends.rs is the unwrap_or_else after rust::lower, as recorded), the seventeenth BS-05 plant with its program and oracle, and the behaviour change (Err(if … {()} else {()}) now refused) stated rather than hidden. No deferral marker, no new public diagnostic, every refusal still through the rejected wrapper.

Your question, Expr::Apply at a loop's end. Read, not run: the lowering produces the shape (lower.rs:1968-1995, succeed, then retarget wraps it as Err(Block{…, Apply{propagate: false}})), and Checker::expr accepts it, but apply_types_expr still enters Expr::Loop with tail = false (validate.rs:722), so the application is refused at validate.rs:683-688 ("an application of Fn{n} drops a failure its apply can have"), surfaced as LLB6005. Fail-closed, the same class as #279, reachable whenever a looped function applies a function-typed parameter at a non-self tail. Ruling: a follow-up, not this PR. Open the issue on the 0.5.0 line now (yours), and in this PR narrow the new §17.13 sentence to calls: as written ("the operand of an Err … is in tail position") it claims applications too, and the register must not say more than the checker does.

One push, then I merge on green:

  1. §17.13 (Chapter17.lex.tex and SPEC.md): the sentence names calls, and says that an application at a loop's end is still refused, with the follow-up's number.
  2. tests/rust-backend/loop-ends/src/Main.lex.tex and Looped.lex.tex both carry \title{Natural number addition}; give them their own titles (the lock follows).

Say the head here when it is up; the gate reruns on it and I merge by merge commit.


Generated by Claude Code

afflom commented Oct 25, 2026

Copy link
Copy Markdown
Member

One more item for the same push, found while ruling on #116: RB-21 collides with the v0.4 heap line, where H6 (#304, landing this week) registers RB-21 with another statement, and the forward merge of release/v0.4 into release/v0.5 keeps the release line's numbers. Take RB-22 here (row, scenario tag, conformance_rb_22, §31 and IdsRB.lex.tex rows, VERIFICATION, CHANGELOG); the identity does not move for it.


Generated by Claude Code

afflom commented Oct 25, 2026

Copy link
Copy Markdown
Member

Maintainer, after the fact: this PR was merged at 72f106e27 (14:44Z) with the round of 06:42Z and 06:52Z unanswered and the push it asked for not made. On this repository the maintainer merges, on a head the maintainer has accepted; that is how #144 went wrong once already, and it does not happen again on the 0.5.0 line: a PR merged ahead of its round is reverted, whatever its state.

The merged head is sound (the gate was green and the review found nothing blocking), so no revert this time. What it owes now lands as one small follow-up PR on release/v0.5, before #303 and before anything else moves that line:

  1. RB-21 → RB-22 (row, scenario tag, conformance_rb_22, §31 and IdsRB.lex.tex rows, VERIFICATION, CHANGELOG): the v0.4 line's H6 (#91 stage 4, H6: the number functions of the std runtime as machine calls with their refinement theorems, and every runtime item has its refinement statement (SP-29, RB-20, SP-30) #304) carries RB-21 with another statement and the release line's numbers are kept at the forward merge, so release/v0.5 as merged now collides with it.
  2. §17.13 (spec/src/Chapter17.lex.tex and SPEC.md): the sentence names calls, and says an application at a loop's end is still refused, naming the follow-up issue.
  3. The follow-up issue for Expr::Apply at a loop's end (apply_types_expr enters Expr::Loop with tail = false, validate.rs:722), on the 0.5.0 line.
  4. tests/rust-backend/loop-ends/src/Main.lex.tex and Looped.lex.tex get their own titles (the lock follows).

None of it moves the identity. Say the PR number here; I review and merge it.


Generated by Claude Code

afflom commented Oct 25, 2026

Copy link
Copy Markdown
Member

Maintainer: the follow-up PR asked for above is withdrawn (owner's direction: no new PRs or issues outside v0.4 until the tag). The RB-21 → RB-22 renumbering and the §17.13 sentence are done in the forward-merge commit of release/v0.4 into release/v0.5; the Expr::Apply note is one line on #279; the fixture titles stay. Nothing to do here.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants