The evaluator: a CEK machine for CBPV
The machine steps a computation closure or unwinds a break; a produced value meets its continuation in the same step. Lexical bindings travel with code; shell effects act on the caller’s live state.
- The rules below describe checked
Val/CompKindIR, not surface ral (compilation ladder, IR invariant). Machine { cclos, stk }is the configuration.CompClosure = Settled<(Arc<Comp>, Env)>is ⟨M, ρ⟩ orErr(Break); there is no value register and no runtimereturn Vnode.Stackis K plus the run’sMooring;step,settle,eval,resume,unwindandcallare its methods, soMachine::runkeeps ownership of every frame across a panic.- Value representation: runtime-values-are-value-closures. Symbol map: evaluator.
Notation
⟨M,ρ | K;σ⟩ computation closure ⟨raise b | K;σ⟩ Err(b)
K ::= ε | F :: K leftmost frame is the top
b ::= Error(e) | Escape(a)vis a value closure ⟨V,ρ⟩;ρ[x ↦ v]is persistent extension.Σis the fixedSignature(prelude, then builtins); lookup is ρ then Σ.μis the run’sMooring;σis liveShellstate: cwd, env overrides, handlers, grants, sinks, scopes, workers, audit. Popping a frame does not roll back an external effect.- Rule results are not configurations:
C(M,ρ)is the next closure,D(v)a value handed down now.S(n,K;σ)issettle;meet(t,F,K;σ)isresumeafter poppingF, forteitherC(λx.M,ρ)orD(v).
S(C(M,ρ),K;σ) = ⟨M,ρ | K;σ⟩ S(Err(b),K;σ) = ⟨raise b | K;σ⟩
S(D(v),ε;σ) = finish Ok(v) S(D(v),F :: K;σ) = meet(D(v),F,K;σ)
⟨λx.M,ρ | F :: K;σ⟩ → meet(C(λx.M,ρ),F,K;σ)
⟨λx.M,ρ | ε;σ⟩ → finish Ok(boxed) ⟨raise b | F :: K;σ⟩ → unwind(F,b,K;σ)
⟨raise b | ε;σ⟩ → finish Err(b)steppops beforeresumeorunwind. A helper failure installsErr(b)with stack and shell as they then stand.boxed(C(λx.M,ρ)) = ⟨thunk (λx.M),ρ↾occ(λx.M)⟩andboxed(D(v)) = v. Pass-through frames preservet; value-demanding frames useboxed(t).
Values and environments
Closing a value executes nothing and reads no ambient shell state.
close(x,ρ,Σ) = lookup(x,ρ,Σ), or Error(undefined x)
close(V,ρ,Σ) = ⟨V,ρ↾occ(V) + Σ's answers for missing direct names⟩ V not a variable- A node’s
Occ(Occ::of, computed on first use) over-approximates the names it mentions.closeandValClos::thunkrestrict at formation; a component read shares the whole saved ρ and never consults Σ, andViewreads a head’s constructor only. Names under a thunk resolve ρ then Σ when it runs. A list or map built at run time is syntax over%itemKvariables bound in ρ (runtime-values-are-value-closures). Env::EMPTYis refcount-free; small environments areThinArc. Compiled bounds:Env ≤ 16,ValClos ≤ 48,Frame ≤ 128bytes.- No sink, cwd, handler stack, worker ownership or lease enters a capture: forcing restores bindings, not shell state (scoping).
Application and force
A builtin forces to a λ-chain ending in δ; partial application is ordinary
β. For a builtin c of arity n, ηc = λa₀.…λaₙ₋₁.δ(b; a⃗) with b ↦ c.
⟨return V,ρ | K;σ⟩ → S(D(close(V,ρ,Σ)),K;σ)
enter(⟨thunk M,ρc⟩,K;σ) = ⟨M,ρc | K;σ⟩
enter(⟨builtin c,ρc⟩,K;σ) = ⟨ηc,{b ↦ ⟨builtin c,ρc⟩} | K;σ⟩
enter(v,K;σ) = ⟨raise Error(cannot-force) | K;σ⟩ otherwise
⟨force V,ρ | K;σ⟩ → enter(close(V,ρ,Σ),K;σ) literal thunk: ⟨M,ρ | K;σ⟩
vᵢ = close(Vᵢ,ρ,Σ)
⟨M · (V₁,…,Vₙ),ρ | K;σ⟩ → ⟨M,ρ | Arg(v₁) :: … :: Arg(vₙ) :: K;σ⟩
meet(C(λx.M,ρc),Arg(v),K;σ) = ⟨M,ρc[x ↦ v] | K;σ⟩ β
meet(D(v),Arg(w),K;σ) = ⟨raise Error(not-a-function) | K;σ⟩
ρ(b) = builtin c v⃗ = ρ(a⃗) run_native(c,v⃗,μ,σ) = (Ok(v),σ′)
⟨δ(b; a⃗),ρ | K;σ⟩ → S(D(v),K;σ′)force_valenters a literal thunk in the current ρ without forming a value closure;forceenters a saved thunk or builtin η. Neither pushes a restoration frame or polls.Appcloses arguments in order and pushes them right-to-left, reserving all frames at once. β polls after consuming its argument. A tail call consumes the caller’s remaining arguments, so no call frame survives. A value meetingArgis an error, never an implicit force.Stack::callruns a builtin directly when given exactly its arity, else pushes the arguments and forces. Arity zero runs on force. Bound heads, handlers and hostapplyshare it. δ reads the captured entry, never a name-dispatched Σ row.- Boundary builtins run saturated with the checker’s
Site; inference expands partial boundaries overExec. - A residual λ is boxed only at a value demand or run end. A builtin residual
prints
<native NAME, k of n arguments>, has type nameLambdaand crosses as builtin name, environment row and supplied count;BuiltinEntry::residualrebuilds the receiver’s η suffix,ValClos::residualrecognises it (transport). run_nativemay write, fail, escape or apply a callback in a nested machine; its break is located at the builtin call.- A returned thunk is data: checked application through one is
M to h. (force h) · V. Everyletruns its RHS.
Sequencing, choice and recursion
Only To and Seq carry a computation with its saved ρ.
⟨M to p.N,ρ | K;σ⟩ → ⟨M,ρ | To(p,N,ρ) :: K;σ⟩
match(p,boxed(t)) = θ: meet(t,To(p,N,ρ),K;σ) = ⟨N,ρ ⊕ θ | K;σ⟩
match(p,boxed(t)) = Error(e): meet(t,To(p,N,ρ),K;σ) = ⟨raise Error(e) | K;σ⟩
⟨M ⨟ N,ρ | K;σ⟩ → ⟨M,ρ | Seq(N,ρ) :: K;σ⟩ meet(t,Seq(N,ρ),K;σ) = ⟨N,ρ | K;σ⟩
close(V,ρ,Σ) = Bool(b) ⟨if V then T else F,ρ | K;σ⟩ → force_val of the chosen arm
close(V,ρ,Σ) = Variant(ℓ,v), arm(ℓ) = W
⟨case V of arms,ρ | K;σ⟩ → ⟨W,ρ | Arg(v) :: K;σ⟩ (W forced in place)
G = [(xⱼ,Mⱼ)] ρ₀ = ρ↾occ(G) ρG = ρ₀[xⱼ ↦ ⟨thunk (rec G j),ρ₀⟩]
⟨rec G i,ρ | K;σ⟩ → ⟨Mᵢ,ρG | K;σ⟩ (polls first)
v⃗ = close(V⃗,ρ,Σ) δq(v⃗) = Ok(v) ⟨prim q(V⃗),ρ | K;σ⟩ → S(D(v),K;σ)- Destructuring collects all bindings before installing any. A list pattern
without rest needs exact length. A mismatch occurs after
Tois popped: no partial binding, no resurrected frame. Toboxes its input;Seqdiscards it. On a breakArgpropagates andTo/Seqabandon the rest; only errors through writtenlets and sequences gain the abandoned-tail fact, compiler binds do not.casepushes the payload before forcing; a bare tag supplies unit. A malformed head or a missing arm is an error.- Recursive siblings share
Gandρ₀; no environment is cyclic. Force reinstalls the group. δqis value-only: arithmetic, indexing, rendering, decoding, assembly. It may fail or cost O(data) and runs no operand computation.Decodedrops its environment before taking the bytes, removes one trailing LF or CRLF and checks UTF-8 strictly.
Failure, recovery and cleanup
Errors and escapes share unwinding; only errors are caught by Try.
Neither is an IR constructor.
h = close(W,ρ,Σ) V = thunk M ⟨try V W,ρ | K;σ⟩ → ⟨M,ρ | Try(h) :: K;σ⟩
meet(t,Try(h),K;σ) = S(t,K;σ)
unwind(Try(h),Error(e),K;σ) = call(h,[record(e,σ)],K;σ)
unwind(Try(h),Escape(a),K;σ) = ⟨raise Escape(a) | K;σ⟩- The handler is closed and the body forced before
Tryis pushed; the poppedTrycannot catch its handler’s failure.recordis the plain[frame, message, status, site]data, after which the error no longer exists; a returned block runs later, outside the catch.
guard shields acquisition and installs release before the body’s first
poll. With o ::= Ok(v) | Err(b), restore(Ok(v)) = D(v),
restore(Err(b)) = Err(b), and shield(σ,μ.grace) = (σs,s₀) saving the
displaced scope s₀:
room(3,K) poll(σ) = () a,k,r = close(A,ρ,Σ),close(V,ρ,Σ),close(W,ρ,Σ)
⟨guard A V W,ρ | K;σ⟩ → enter(a,Acquire(k,r,s₀) :: K;σs)
meet(t,Acquire(k,r,s₀),K;σ) = call(k,[boxed t],Guard(r,boxed t) :: K;σ[scope := s₀])
unwind(Acquire(k,r,s₀),b,K;σ) = ⟨raise b | K;σ[scope := s₀]⟩
meet(t,Guard(r,u),K;σ) = call(r,[u,exit_case(Ok(boxed t))],Cleanup(Ok(boxed t),s₀) :: K;σs)
unwind(Guard(r,u),b,K;σ) = call(r,[u,exit_case(Err(b))],Cleanup(Err(b),s₀) :: K;σs)
meet(t,Cleanup(o,s₀),K;σ) = S(restore(o),K;σ[scope := s₀])
unwind(Cleanup(o,s₀),b′,K;σ) = ⟨raise b′ | K;σ[scope := s₀]⟩- Entry reserves the deepest need,
Cleanupplus twoArgs; poppingAcquireorGuardreturns to entry depth, so later pushes cannot fail. Provenance frames do not count. - A pending stop runs no acquisition; a failing acquire runs no release. On
success, scope restoration and pushing
Guardhave no intervening poll; the body’s β polls withGuardbelow its argument. exit_caseborrows the saved outcome:okomits the body value,erris the plain record,exitedcarries the byte,stoppedthe cause. A successful release restores the saved outcome; a release error or escape overrides it.- Acquisition and release share one grace from the strike, so a late acquisition can leave release no time. Shielding is cooperative, not protection from terminal signals.
- Shields change
scope, notcradle. Stages follow scope; workers follow their owner’s entry scope; agatheropened inside a shield owns masked workers (cancellation).
Capture and shell frames
Capture returns whole bytes or fails; it exposes no partial prefix.
s = σ.stdout β = freshbuf()
⟨capture M,ρ | K;σ⟩ → ⟨M,ρ | Capture(s,β) :: K;σ[stdout := β]⟩
meet(t,Capture(s,β),K;σ) = S(D(⟨bytes,∅⟩),K;σ[stdout := s]) β whole
= ⟨raise Error(capture-overflow) | K;σ[stdout := s]⟩ otherwise
unwind(Capture(s,β),b,K;σ) = ⟨raise discard(b,total(β)) | K;σ[stdout := s]⟩- Capture ignores its input, a λ included. On a break it drops the bytes;
discardadds their count, saturating, to an error’s unwind report and leaves escapes unchanged. - Pumps, process drains and worker output have their own lifetimes (output capture and detachment).
A shell frame owns its undo, not a snapshot of the shell. Installation closes inputs, reserves, installs state, pushes the undo, then forces the body.
| Frame | meet(t,…) after pop | unwind(…,b,…) after pop |
|---|---|---|
Redirect | commit; S(t,K;σ′) | abort; keep b |
Within, Grant, Unmask | undo; S(t,K;σ′) | undo; keep b |
Audit | close trail; report of boxed(t) | close; report an error, pass an escape |
Gather | restore cradle; join boxed(t) through exit_brood | restore cradle; end brood; keep b |
CallSite | restore provenance; S(t,K;σ′) | locate; restore provenance; keep b |
Withinrestores cwd, overrides and handlers;Grantremoves its layer by position;Unmaskrestores a removed handler (handler dispatch). A scope body’s λ passes through.- Redirect commit cannot unstream bytes; atomic writes can abort. A commit failure is a break on the remaining stack.
Gatherowns aBroodGuardand the displaced cradle, with the current scope as the new cradle’sunder(worker ownership).Exec,Pipeline,Spawn,Tilde,Useand builtin bodies are host operations. APipeNoderuns in one shell arm; ral stages enter fresh machines on threads and external stages are processes (pipeline execution).
Two reductions
A saved thunk keeps its lexical name
With ρ₀ = [x ↦ ⟨7,∅⟩], f = ⟨thunk (λy.return x),ρ₀⟩ and
ρ₁ = ρ₀[x ↦ ⟨99,∅⟩, f ↦ f]:
⟨(force f) · (1),ρ₁ | ε⟩ → ⟨force f,ρ₁ | Arg(⟨1,∅⟩) :: ε⟩
→ ⟨λy.return x,ρ₀ | Arg(⟨1,∅⟩) :: ε⟩ → ⟨return x,ρ₀[y ↦ ⟨1,∅⟩] | ε⟩
→ finish Ok(⟨7,∅⟩)Caller x = 99 never replaces captured x = 7; a literal
force (thunk (return x)) enters in ρ₁ and returns 99. A cd in the
thunk persists unless an enclosing scope owns restoration: a lexical closure
is not a subshell.
Release overrides the body before a catch
A body fails with e₁ holding u; release fails with e₂:
⟨raise Error(e₁) | Guard(r,u) :: Try(h) :: K;σ⟩
→ call(r,[u,exit_case(Err(Error(e₁)))],Cleanup(Err(Error(e₁)),s₀) :: Try(h) :: K;σs)
⇒* ⟨raise Error(e₂) | Cleanup(Err(Error(e₁)),s₀) :: Try(h) :: K;σs′⟩
→ ⟨raise Error(e₂) | Try(h) :: K;σs′[scope := s₀]⟩
→ call(h,[record(e₂,σs′[scope := s₀])],K;σs′[scope := s₀])A successful release restores e₁. The handler sees the winning error with
scope restored; an enclosing capture still receives release and handler
writes.
Run boundaries and space
The top level is a sequence of closed machines, not a growing continuation.
run_phrasesreadmits imported residual data at its solved type, then runs eachPhrase::{Define, Run}as its own machine. A landedDefinesurvives a later failure; block-local binds do not escape.evaluate(M,ρ)startsC(M,ρ)overε.apply(f,args)runscallfirst and starts a machine only whencallyields a closure. A value finishes insettle, a λ finishes boxed instep, a break finishes after every frame unwinds.- β consumes its
Arg, and force retains no consumed frame, so tail recursion needs bounded stack, not bounded data. Pending binds, cleanup and captures retain their work. Slot, fromreserve, guards every computational push againstsession.stack_limit(default 100 000,--recursion-limit). The check precedes installation; a refusal unwinds the stack as it stands.- Cancellation is cooperative at binds, sequences, application, β, recursion, guard entry and host poll points. No step promises constant work or one poll.
- A builtin that applies a function nests a machine on the host stack;
NESTED_MACHINE_LIMITbounds the nesting. Builtin bodies receive no caller ρ, and no frame crosses a thread or marshalling boundary.
Provenance and panic undo are implementation work the equations erase.
- Named operations run under
at_site;Exec,AppandForcepush aCallSitethat restores provenance on success, break and panic. Adjacent call sites coalesce and cost no stack budget (the error record). - On a panic
runabandons frames top-down throughFrame::abandon, which restores sinks, scopes, handlers, grants, cradle and provenance and aborts redirects and audits. It does not run guard release code; a panic is a defect, not a catchable error.
The rationale is CBPV; SPEC §§5, 7, 13 and 17 state the user-visible contract.