Resolve mutual recursion between sibling let-bound closures - #55
Merged
Conversation
Extends the letrec pre-declare (v0.8.1's self-reference fix) to cover two-or-more sibling `let`-bound closures that reference each other by name -- fnliterals.scad-style isEven()/isOdd(), each calling the other. All function-literal-RHS assignments in a LetOp now get their own slot pre-declared up front (not just each one's own, right before compiling it), so a forward reference to a not-yet-compiled sibling also resolves as a genuine upvalue. Unlike self-reference, a forward reference has nothing to read OR self-patch into at Op::MakeClosure time -- the sibling it names doesn't exist AT ALL yet, not just "not yet stored." Such captures are left out of the closure's own capture list entirely at compile time and deferred to a new Op::PatchClosureCapture instruction, emitted right after the referenced sibling's own StoreLocal actually runs -- mutating the earlier closure's already-real capturedTrail in place, the same shared TrailView its own later invocations already read through. Verified: 2-way (isEven/isOdd) and 3-way (f1->f2->f3->f1) cycles both resolve correctly, including a case mixing a genuine outer capture with a forward-sibling reference in the same closure. Both scale linearly (100,000-deep isEven/isOdd chain in ~0.07s) -- no repeat of the capturedLet-chaining regression the self-reference fix required a guard against, since a resolved forward reference is a real capture snapshotted once, not a value re-derived on every call. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This was referenced Jul 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
let-bound closures that reference each other by name — fnliterals.scad-styleisEven()/isOdd(), each calling the other. All function-literal-RHS assignments in aLetOpnow get their own slot pre-declared up front (not just each one's own, right before compiling it), so a forward reference to a not-yet-compiled sibling also resolves as a genuine upvalue.Op::MakeClosuretime — the sibling it names doesn't exist at all yet. Such captures are left out of the closure's own capture list entirely at compile time and deferred to a newOp::PatchClosureCaptureinstruction, emitted right after the referenced sibling's ownStoreLocalactually runs — mutating the earlier closure's already-realcapturedTrailin place.f1->f2->f3->f1) cycles both resolve correctly, including a case mixing a genuine outer capture with a forward-sibling reference in the same closure. Both scale linearly (100,000-deepisEven/isOddchain in ~0.07s) — no repeat of thecapturedLet-chaining regression the self-reference fix needed a guard against, since a resolved forward reference is a real capture snapshotted once, not re-derived every call.Test plan
MutualRecursionBetweenSiblingLetBoundClosuresResolvesCorrectlyandThreeWaySiblingMutualRecursionResolvesCorrectly.count_to,f_1arg,reduce()at scale,let(x=x+1)shadowing, ternary-wrapped self-reference,accumulate()/while()) unaffected.🤖 Generated with Claude Code