Anonymous Author(s)
We show that the generate-execute-splice loop in Recursive Language Models (Zhang et al. 2026) has the structure of a monad over staged computation. Taha and Sheard's (1997) MetaML provides the type system: bracket/escape/run map onto quasiquotation, unquote, and sub-LLM dispatch. Davies and Pfenning's (2001) modal analysis explains cross-context breakage as a type error — treating context-dependent values (type A) as context-independent (type Box A). Filinski's (1994) representation theorem closes the circle: any monad can be implemented via delimited continuations, connecting the monadic description of RLM execution to the Scheme implementation of hygienic orchestration via shift/reset.
This note develops the formal connection as a companion to Scope Hygiene in Recursive Language Models (Anonymous 2026), which presents the empirical evidence and proof-of-concept implementation. The monadic framing adds no new implementation — it provides a type-theoretic lens that classifies the scope failures as type errors in a well-studied formal structure, validating the architectural choices and suggesting where static guarantees (phantom types, modal type systems) could strengthen them.
The RLM loop sequences sub-LLM calls, threading scope metadata (provenance, model identity, iteration count) through each step. This threading is a monad (Moggi 1991; Wadler 1995): return wraps a datum in a syntax object (tagging it with scope provenance); bind is the generate-execute-splice step (dispatch to a sub-LLM, wrap the result in a fresh syntax object). The scope failures identified in the companion paper are cases where bind drops or corrupts the metadata.
Think of the RLM loop as an assembly line: at each station, a worker (sub-LLM) receives a partially assembled product (prompt), does some work, and passes the result forward. The monad is the conveyor belt — it sequences the stations and carries metadata (scope marks, provenance) alongside the product. The scope failures are cases where metadata falls off the belt: the next station cannot tell whether a piece came from the original blueprint (instruction) or was attached by a previous worker (data).
Taha and Sheard's MetaML (1997) gave this structure a type system. MetaML is a language for multi-stage programming with three operations that map directly onto the Scheme coordination layer:
MetaML Scheme layer Assembly line analogy
────────────── ───────────────────────── ─────────────────────────────────
bracket <e> quasiquote `(... ,data ...) Tag a product with metadata
escape ~e unquote ,variable Attach a sub-component to the product
run !e llm-query dispatch Send the product to the next station
MetaML's type system enforces consistency: you cannot run a value that was never bracketed (send unstructured data to a sub-LLM), and you cannot escape outside a bracket (splice data without a template). The RLM's scope failures are exactly the errors MetaML prevents.
Davies and Pfenning (2001) revealed why MetaML's type system works: staged computation corresponds to modal logic. The type Box A ("necessarily A") represents a closed computation valid in any evaluation context. The type A represents an open computation that depends on the current context.
This maps directly to cross-context breakage: a model-agnostic prompt has type Box A (portable); a GPT-5-specific prompt has type A (context-dependent). Cross-context breakage is the type error of treating A as Box A. In the Scheme layer, datum->syntax is the explicit type coercion — the programmer acknowledges the context crossing.
Each scope failure corresponds to a type error in the monadic/modal framework:
| Failure | Type Error | Scheme Prevention |
|---|---|---|
| Delimiter capture | Confusing a tagged product <string> with a raw part string |
(finish val) is shift; "finish" in a string is inert |
| Referential opacity | Attaching a sub-component without tagging its origin | Quasiquote + syntax objects tag every splice |
| Namespace collision | A station corrupts another station's metadata | Module system makes metadata read-only |
| Cross-context breakage | Sending a station-specific product A as if it were universal Box A |
datum->syntax makes context crossing explicit |
The table above describes what should be prevented. But why do Scheme's specific primitives — shift/reset, modules, syntax objects — actually implement these guarantees? Filinski (1994) proved the key result: any monad can be represented using delimited continuations (shift/reset).
The theorem is constructive. Given the orchestration monad's return and bind:
return(tag a product with metadata) is implemented byreset— it establishes the scope boundary.bind(pass a product to the next station, preserving metadata) is implemented byshift— it captures "everything after this sub-call" and passes control forward.finishisshiftwith an empty continuation — the line ends and the final product is returned.
The Scheme layer already uses shift/reset for finish. Filinski's theorem reveals this is not an isolated choice: the same two primitives are sufficient to implement the entire orchestration monad. The monadic description and the Scheme implementation are two views of the same formal structure, connected by this representation theorem.
The monadic framing suggests three directions beyond the empirical work in the companion paper:
-
Static guarantees via phantom types. Haskell's
Scoped s apattern could make scope violations compile-time errors. The RLM's restricted code surface (the LLM generates from a small grammar) makes this feasible — the scaffold could type-check generated code before execution. -
Modal type systems for multi-model coordination. Davies and Pfenning's
Box A/Adistinction could be enforced at the API level: prompts tagged as model-specific (A) cannot be sent to a different model without explicit coercion. This would prevent cross-context breakage by construction. -
Formal verification of the orchestration monad. The monadic laws (left/right identity, associativity) correspond to properties of scope propagation. Violations of these laws would indicate scope bugs that the current test harness cannot detect.
These are theoretical directions. The companion paper's Python testbed validates the concepts empirically; the monadic framing validates them formally and points toward stronger guarantees.
-
Davies, R. and Pfenning, F. 2001. A modal analysis of staged computation. Journal of the ACM 48, 3, 555–604.
-
Filinski, A. 1994. Representing monads. In Proceedings of the 21st ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL '94). ACM, 446–457.
-
Moggi, E. 1991. Notions of computation and monads. Information and Computation 93, 1, 55–92.
-
Taha, W. and Sheard, T. 1997. Multi-stage programming with explicit annotations. In Proceedings of the ACM SIGPLAN Symposium on Partial Evaluation and Semantics-Based Program Manipulation (PEPM '97). ACM, 168–180.
-
Wadler, P. 1995. Monads for functional programming. In Advanced Functional Programming, Lecture Notes in Computer Science, vol. 925. Springer, 24–52.
-
Zhang, A. L., Kraska, T., and Khattab, O. 2026. Recursive Language Models. arXiv:2512.24601v2.