Skip to content

Pass changed maps back to the caller ​

Status: accepted design, partly implemented · Date: 2026-11-14

A function that changes a caller's map should return the new map, and the caller should bind that result in place of its old map. We choose this borrow translation rather than a state monad. This record settles the design question in step 8 of the pure TypeScript plan. The first implementation and three later extensions now cover the straight-line case described below.

The result carries the maps ​

The translator must determine which Map parameters a function changes. A function that changes one map and returns nothing returns that map in Lean. A function that changes several maps returns a tuple in parameter order. If the function also returns a value, the tuple contains that value first, followed by the changed maps. The caller binds each changed map again and uses the ordinary result where the source uses it.

A ReadonlyMap parameter stays a plain input. A mutable Map parameter that the function only reads does too. The annotation alone must not make a call change the caller's bindings.

This keeps the maps that a function changes visible in its type. A state monad would obscure that information and require state-monad lemmas in proofs about otherwise plain functions. The effects plan reserves the monad for global state.

Only an exclusive caller may lend a map ​

Copying a map in and back out is faithful only if no other reference can observe it during the call. The caller must own each changed map. A second local name, a field that holds the map, a captured reference, or another argument that holds the same map must make the call fail translation. In particular, passing one map to two changed parameters is not two independent borrows.

The callee must not let the borrowed map escape through a stored reference or its ordinary result. Forwarding a borrow to another function needs the same checks and must pass the final map back through every call.

The spec must return the state too ​

The original interpreter bound arguments as values. Stmt.ownsChanges refused every function that changed a parameter, and evalCall returned only the ordinary result. Removing that guard would lose the change at the call, so it would not implement a borrow.

The call rule must copy each borrowed map into the callee's parameter binding and copy its final value back into the caller's argument binding. Both normal completion and an explicit return must preserve that final state. The current statement result for a return has no environment. Exceptional completion needs the same care: JavaScript retains changes made before a throw, even when a caller catches the error.

The IR evaluator and the interpreter must agree on these results before the translator accepts a borrow. The proof of program_sound must cover the new call rule, and tests must compare the caller's observations with Node. Both translators must print the same tuple and rebinding code.

Boundary when decided ​

When this decision was made, every changed map parameter was refused, including an exclusive borrow of one map. The decision chose the result shape without changing that boundary.

Current implementation ​

A void function may now change one or more Map parameters through straight-line set, delete and forwarding calls with atomic arguments. It may end with a bare return. A forwarded borrow must go to an earlier borrowing function in the same file. At the outer call, the caller owns the map and passes it exactly once in a top-level call statement. When a function changes several maps, every mutable Map parameter must change. The function returns a tuple of the final maps in parameter order, and the caller binds each name again. The translator refuses a call that passes one map for two changed parameters. The interpreter gives that call no result either. Other arguments must not keep that map, and the ownership check still counts collections that those arguments let escape.

The source interpreter copies the final map back, and the IR evaluator uses the same state result. The proof of program_sound covers this call rule. Corpus tests check the generated code, and interpreter tests compare the caller's observations with Node. Readonly inputs retain their existing translation.

A borrowing function may also return an ordinary value through one final return e; after its updates. The spec runs the body with that return turned into an assignment to a fresh variable, so the call completes normally and the final maps are copied back as before. The Lean function returns a tuple of the value and the final maps, and the caller binds the value to the const or let that the call initializes. Only such a declaration may make the call.

A borrowing function may throw. It may call a function of the same file that borrows nothing as a statement of its own, if that call passes no borrowed map, and it may forward its map to a borrowing function that throws. The output then returns Except of the maps, and each caller binds them with >>=, which passes the error on. The interpreter does not copy the maps back after a throw, because no try body may make a call, so no caller can observe them. The hoisting that moves a throwing call into a declaration of its own leaves a call of a borrowing function in place, because only the call statement copies the maps back. A function that borrows nothing must end with a return, so that the output never gives an ordinary call a state result. A borrowing function that returns a value may throw as well. The spec ends the declaration that makes the call with the same error, and CallsAgree.valueError relates the throw of the output to the throw of the source.

A borrowing function may also change its maps inside if statements and for…of loops, and it may throw. The translator already turns these statements into Lean that threads the maps through, so the borrow check only had to accept them. The output body's state check (Body.returnedState) now looks through branches, loops and non-final if statements. A branch that throws has no state, so the other branch decides. A void function may also return early from an if at the top of its body. The spec moves the rest of the body into an else of that if, because JavaScript runs the same statements either way. The translator translates that same body, so program_sound covers these functions without a new proof.

A branch or loop may forward a map to another borrowing function too. The ownership check (borrowOwnershipBody) and the forwarding check (borrowCallsValid) now look inside if statements, for…of loops and blocks. The proofs read neither check, so they did not change.

A borrowing body may also read values (issue #334). Expr.borrowValue now accepts field and element reads, unary operators and ?: too, and set arguments and forwarding arguments use it. A const declaration of such a value may stand among the updates. The output state check (Body.returnedState) also looks through match branches, so a body that tests the kind of a union keeps its maps. The proofs take the body and the state check as given, so they did not change. A void function that is not a borrow now fails with the reason, rather than with void as an unsupported type.

A function that returns a value may return early as well. borrowBody turns each early return e into r = e in the branch, and puts the rest of the body in an else. borrowResult? follows the last else to find r. The proofs treat borrowResult? and the body as given, so they did not change.

A loop may break and continue too. The loop translation already carries its state through both, so only the shape check changed.

A void function may return; inside its final loop, outside nested loops. borrowSplit turns such a return; into break; (borrowLoopReturns). Nothing follows the loop, so JavaScript ends the function in both cases. The spec and the translator read the same rewritten body, so the proofs did not change.

A borrowing body may hold a switch at its top level (issue #334). borrowSplit writes it as an if chain (borrowSwitches): each clause becomes if (d === label), and a default clause becomes the final else. JavaScript evaluates the scrutinee once, but the chain evaluates it for each test. That is the same here, because a borrowing body's tests only read values. Each clause must end with break;, so no clause falls through, and no other break may end the switch early. In the last statement of a void function a clause may end with return; instead, because nothing runs after the switch. A switch that does not fit stays as it is, and the borrow check refuses it with the usual message. The spec and the translator read the same rewritten body, so the proofs did not change.

Two limits keep the if chain exact (issues #407 and #408). A trailing clause that does nothing goes only when its label is a literal, because JS evaluates a label when no case before it matched, and another label may change a map or throw. A switch whose clauses all do nothing goes only when its scrutinee is a literal too. And a switch whose clauses declare a local outside braces stays a switch, so the borrow check refuses it. The clauses share one scope, so such a local has a temporal dead zone across them, and two clauses may not declare the same name. Separate branches would lose both. A clause that is one block followed by break; keeps its declarations in the block, which the branch holds as it is.

A switch on the tag of a union may end in a default that checks that the cases cover every member, as in const unhandled: never = place; (issue #392). The if chain could not hold it: after the cases before the last one, a single member is left, and the translator refuses a test of its tag. So borrowSwitches keeps such a switch as a switch (endsInNeverCheck). The translator then reads it as for an ordinary function, as a match on the tag, and checksNever checks the default. In the last statement of a void function, a clause's return; becomes break;, because nothing follows. The borrow checks read the kept switch as an if on its scrutinee that holds all its clauses (borrowView). The spec runs the same body, so program_sound covers it without new proofs.

A call of a borrowing function now finds the function through resolve, as an ordinary call does, and runs its body in the function's own file. That file's borrow list says which parameters the function borrows. For a function of the caller's file nothing changes. But the proofs no longer assume that a borrowing function lives in the caller's file, which is the first step towards imported borrows.

A file may now import a borrowing function. The spec's borrow list for a file, borrowsOf, now holds the file's own borrowing functions and then its imported ones (importedBorrows). An import finds its function through resolveExport, and the declaring file's own list (borrowAt) says what it borrows. A name that the file exports as a string enum borrows nothing. The translator builds the same list from the interfaces, and importBorrows_eq proves the two equal. That proof rests on a new invariant, FaithfulB: each function of an interface borrows what its file's list says, and its name is not an exported string enum. Two checks give the proof these facts per file: exportsFaithful and importsKnown. Each states a fact that the translator already ensured, so no file of the corpus that was accepted before is refused now. A borrowing function still forwards only to borrowing functions of its own file.

What Step 8 leaves out ​

Step 8 is closed. Four narrowings remain, and each is refused at translation.

A borrowing function may not forward its maps to an imported borrowing function. The forwarding check (borrowCallsValid) reads only the borrow list of the function's own file. Lifting this would need that check to read the imported list too. No program of the corpus or of the issues needs it.

A return inside a loop is refused, except a return; in the final loop of a void function. The spec runs a borrowing body to normal completion only. A return in the middle of a loop would need the loop translation to carry an early exit together with the maps. The reject fixture mapBorrowLoopEarlyReturn.ts records this.

A switch must stand at the top of the body. Each of its clauses must end with break;, except the last, or with return; in the final statement of a void function. The rewrite into an if chain is exact only for these shapes. A clause that falls through would need the rest of the switch to run as well.

tolean certify refuses a file that declares a borrowing function. It checks the file on its own, and when the file's borrow list (borrowsOf) is not empty it stops with "a map borrow has no certificate yet". So the file's other functions get no certificate either. program_sound still covers these files.

Reject fixtures in tests/typescript/corpus/translate/reject/mapBorrow*.ts and tests/typescript/corpus/translate/projects/reject/importedBorrowForward record this boundary. Each of them fails translation today. Several of the refusals still use a general message, such as the one for a void return type, because the translator accepts no borrow shape that they would match.

This page is docs/decisions/2026-11-14-map-borrows.md at commit 5f1d031.