Skip to content

Step 14 handles indirect state first, and final return expressions return a tuple ​

Status: accepted · Date: 2026-11-30

Step 14 of the plan asks for four things: general final return expressions in local functions that change captured variables, implicit transitive captures, longer chains of sibling calls, and value-returning callers that change state only through their siblings. The open question was which of them comes first, and how the lifted function gives back both its value and the captured state. This record settles both.

The first slice: a sibling call statement changes captured state ​

The first slice accepts a value-returning local function that changes captured state only by calling a sibling as a statement:

ts
function add(value: bigint): void { total += value; }
function apply(value: bigint): bigint {
  add(value);
  return total;
}

It comes first because it needs no new output form. The lifted apply already returns the state tuple, and the call reads total from it, as the local-function decision describes for a direct write.

The interpreter needed one fix. Its call of a local function chooses a path with changesCaptured. That check counted only assignments and collection writes in the body. So for apply above it took the plain path, which returns the value and drops the caller's changed variables. JS keeps them, because add writes the binding that apply and its caller share. Now changesCaptured also counts a statement n(…); where n is a captured name (Stmt.changesName). A local function that a caller calls as a statement returns nothing, and Tolean requires such a function to change a captured let local, so the call is a change. The check may count a call that changes nothing in a particular run. That costs nothing, because the state path writes back the values the body left, and those are the old ones.

The translator uses the same predicate. A value-returning local whose captured state changes must write a captured variable or call a sibling as a statement. The proofs only replace the old test with the new one.

A call of such a function inside a larger expression has no value in the interpreter, as for a direct write, and the translator refuses it. Before this change, the interpreter gave the value without the sibling's change. A test in interpreter.test.ts pins this case.

What stays refused in this slice ​

A caller whose only change comes from a value-returning sibling, such as const t = add(value); with an add that changes total, stays refused. The interpreter does not count that call as a change, because the predicate looks at call statements only. Counting it needs the closure's body, and the proofs do not track which closure each local function name holds outside a call.

Transitive captures and longer sibling chains stay refused, as before. The fourth slice, below, accepts the longer chains.

Final return expressions return the value with the state ​

For count += 1n; return count * 2n;, the lifted function will return the tuple of the changed variables and then the value. The call binds the variables again and takes the last element as its value. This follows the plan's table and computes the body once.

The rejected alternative lifts the body twice: one function returns the value and another returns the state. Its proof reuses the existing value and state paths, but the output runs the body twice at every call, and nested calls multiply that cost.

The tuple form needs a proof step that the code lacks today. At the final return, the interpreter evaluates the expression in the environment that the body built, which includes the body's own const locals. The proof's statement relation, StmtSim, says that a normal end leaves the two sides in agreement on the names that were in scope before the statements. It does not say that the new locals agree. So the relation cannot show that the expression has the same value on both sides. The next slice either adds that fact for statement lists that cannot continue, or translates ss ++ [return e] with a tail that keeps the environment at a return, as execReturnState does. An earlier return in a state-writing local stays refused.

The second slice: a final return over parameters and captured variables ​

The second slice accepts a state-writing local function whose body ends with return e, where e reads only parameters and captured variables through literals and operators. The lifted function returns the tuple of the changed variables and then the value of e. A const initializer or a whole return can hold the call.

The two proof approaches above were compared by size. Adding the new locals' agreement to StmtSim changes the relation that every statement case proves, so each case of stmts_sound would need new work. A tail that keeps the environment at a return needs a new Tail and new cases in every lemma that matches on the tail. This slice takes a third, smaller route that changes neither: when e reads only names that were in scope before the body, the existing StmtSim already says that both sides agree on them, so e has the same value on both sides. The new lemmas are local to the lifted call. The translator checks the reads with plainNames, and the body before the final return must contain no other return (Body.valueFree), so the prefix cannot leave early.

Still refused, with a message each:

  • a final return that reads a const or let declared in the body;
  • a final return that calls a function, or uses a member, index or other construct beyond literals, names and operators;
  • an earlier return in a state-writing local function.

Lifting these needs one of the two larger approaches above.

The third slice: any final return expression ​

The third slice lifts the first two refusals of the second slice. The translator reads ss; return e as ss; const g' = e, where g' is the local's name with a prime, refused when the body already names it. It translates those statements with the tail .state (changes ++ [g']), so e is translated by the ordinary declaration path, in the environment of the body's own locals, and every expression that a const initializer accepts works, calls included.

The proof adds one conjunct, Keeps, to the normal end of StmtSim: for a .state names tail, each name of names that the source binds at the end has the same value in the output. Most statement cases pass it through unchanged, since their source environment is the same list split differently, and the cases that end with no new bindings get it from the Agree they already prove (Keeps.of_agree, Keeps.of_frame). This gives the value of g', which is a name declared in the body and so outside what Agree on the outer environment covers. A source lemma, returnState_of_decl, shows that the statements ending in const g' = e end with g' bound to what execReturnState returns for the statements ending in return e, in front of the same environment; it needs only that both final statements have the same constAssignedIn effect.

This was chosen over the alternatives recorded above. Translating the body with a ret tail and a source tuple return [changes..., e] gives no Frame at the return, so writing back unchanged captured variables could not be proved without a new semantic frame lemma. Binding the result name at the caller breaks Agree and the check that each call translates the body the same way. A new Tail, or full agreement on new locals, changes far more cases than one conjunct that most cases pass through.

An earlier return in a state-writing local function stays refused, by Body.valueFree.

The fourth slice: longer sibling chains ​

The fourth slice drops the refusal of a called sibling that calls another local function, so addFour can call addTwice, which calls add. It was picked over an earlier return in a state-writing local function because it needs no new proof. The interpreter has no result for an early return with captured changes, so that needs new semantics as well as new proofs.

The refusal was in translateLocalHead, which only narrows the subset. program_sound does not rely on it. Each call checks again what its proof needs, through localCallProblem and the check that the call translates the body the same way. A call down a chain is one more sibling call with the same facts: the callee's closure is a captured value of the caller, which the caller passes on, as a plain sibling call already does. So the proof of the existing sibling case covers chains, and the slice adds no lemma.

The caller must still name each variable and each sibling that its callee captures, so third that calls second, which calls first, must name first too. Implicit transitive captures stay refused, with the existing message.

Splitting the earlier return ​

An earlier return in a state-writing local has no result in the model. callLocal gives none when execStmt ends in .ret and the body changes captured variables, because .ret carries the value but not the environment that writeBack reads. Giving it one in general means adding the environment to .ret, or a Tail that keeps it. Both change the relation that each statement case of stmts_sound proves. That is too much for one pull request.

The work is split in two. The first slice accepts only guard returns: an if at the top level of the body whose branch ends in return. execReturnState already walks the top-level statements and keeps the environment, so it gets cases for such an if. The translator ends each branch with the tail that the third slice uses for the final return. Returns inside loops or a switch stay refused until the second slice, which takes the general route.

The guard slice ​

The first of the two slices accepts guards only at the start of the body, and only if (c) return e; or if (c) { return e; } with no else. Before any guard runs, the body has changed nothing, so the guard's return needs no state from statements before it. In the model, callLocal runs the body with execGuarded. It walks the leading guards, evaluates each test, and at a guard that fires runs execReturnState on that guard's return. After the guards it runs execReturnState as before. A guard counts only where the statements after it end in a value return. This keeps callLocal unchanged for every body without guards (execGuarded_of_returnState).

The translator lifts a guard to an if on the translated test. Its first branch is the third slice's lifted body for return e alone, and its other branch is the lifted rest of the body. The proof states what a lifted body gives as LiftSim. liftSim_leaf proves it for the third slice's body, taken out of the old call lemma, and liftSim_guard proves it for each guard. StmtSim and Tail do not change.

This slice does not accept guards after another statement, a guard branch that runs statements before its return, or guards in a body whose final return is a changed captured name (the stateReturn path). These need the environment at an arbitrary return, which is the second slice.

Closing step 14 ​

Step 14 closes with the guard slice. No small, well-defined slice remains:

  • A guard after another statement returns state that the earlier statements changed. execGuarded would have to run those statements and keep the environment, and the proof would have to carry Keeps through them. That is the general return with an environment, not a guard case.
  • A guard with an else scopes the rest of the body in a block, so it is not the same as a leading guard. Its only gain over writing the guard without else is syntax.
  • A guard in a body that ends with return total; goes through the stateReturn path, which has its own model and proofs. It would need the guard machinery a second time for a form that return total + 0n; already reaches.
  • Returns inside loops or a switch need the environment on .ret, or a new Tail, and change every statement case of stmts_sound.

These stay refused with their messages, as do callers whose only change comes from a value-returning sibling and implicit transitive captures, for the reasons above. A later step can give .ret an environment and accept all earlier returns at once.

This page is docs/decisions/2026-11-30-local-function-state.md at commit 5f1d031.