Freshware · Interactive Workbook
MDX Source Edition Residual Systems & Verified Compilation Learning Center & Videos
Part III · The Second Residue · Chapter 04

Phase Separation, Futamura Projections, and Reverse-JIT Lowering

From interpreting prose on every request to partially evaluating business intent into a fast residual program with confidence-guarded bailouts.

Reading time: 10 min Interactive: Lab 03 · Futamura & Reverse-JIT Simulator Source: src/content/chapters/04-phase-separation-and-lowering.mdx

When developers first wire language models into operational pipelines, they almost always construct a universal runtime interpreter:

Agent(playbook_prose + tools + history, live_event)  --->  action + output

That design wastes energy twice. First, the model re-parses the exact same static business rules on every single incoming document or support event. Second, because conversational models emit open-ended token streams, every conditional branch carries latent token latency, parsing friction, and tail failure risk.

The First Futamura Projection for AI Systems

In 1971, Yoshihiko Futamura proved that partially evaluating a general interpreter I with respect to a fixed source program s yields a specialized residual program P_s:

mix(Interpreter, Program_s)  --->  ResidualProgram_s(input)

Applied to AI software, this turns into a concrete engineering recipe:

  1. Your general reasoning model + alignment specs + playbooks act as the slow, flexible interpreter I.
  2. Your specific business workflow (vendor rules, catalog categories, invoice heuristics) acts as the slowly changing source program s.
  3. Phase 1 compilation runs mix(I, s) only when s or the external world shifts — checked by sha256 content hashing so untouched documents cost zero tokens.
  4. Phase 0 execution runs the compact Computational Residue P_s(input) at millisecond speed without dragging an open-ended agent loop into every transaction.

Combined with Matthew Flatt’s macro phase separation (“You Want It When?”), the answer to when intelligence should run becomes obvious: compile-time reasoning interprets ambiguous human intent; runtime execution evaluates narrow, verified primitives.

Smart Software and the Fourth Semantic Logic Gate

What does that Computational Residue look like inside production code?

Instead of forcing one large text-generating model to drive control flow, Smart Software keeps deterministic code in charge of state, permissions, math, locks, and side effects — and invokes focused System One decision models only at exact semantic branches.

As Diogo Almeida framed the primitive, a calibrated typed judgment API acts like a fourth semantic logic gate sitting alongside AND, OR, and NOT. Instead of returning chat prose, you pass structured state and a closed question schema (binary, choice, or ordinal), and receive calibrated probabilities over names your application already owns.

Reverse-JIT Lowering and Deoptimization Bailouts

Even better: the code emitted by Phase 1 does not need uniform intelligence across every stage. Just as a modern dynamic runtime lowers hot functions from interpreted execution down to specialized machine code — and bails out (deoptimizes) whenever a type assumption fails — Freshware treats intelligence as a 4-rung lowering axis:

RungMechanismUnit Latency / CostBest FitBailout Condition
Rung 0 · Pure CodeDeterministic functions, SQL, regex, state machines~1 ms · $0Arithmetic, locking, dedupe, stable lookupsUnseen enum or invariant check failure
Rung 1 · Decision ModelSystem One typed classifier (binary / choice / ordinal)~350 ms · $0.00005Semantic routing, entity matching, DuckDB SQL filtersProbability enters abstain band (0.35 < p < 0.65)
Rung 2 · ExtractionMultimodal / structured perception model~1,800 ms · $0.002Messy scanned PDFs, visual tables, layout mapsLine-sum arithmetic mismatch or missing field
Rung 3 · Frontier AgentOpen-ended reasoning model + tools~25,000 ms · $0.04Novel anomalies, Phase 1 compilation, /wayfinderEscalate to Phase 2 human decision thread

Experiment with the simulator below. Assign each pipeline stage across Rungs 0–3, adjust world drift and your confidence abstain band, and observe how lowering cuts compute cost and latency by more than 95% while deoptimization bailouts contain tail errors.

Interactive Lab 03 · Futamura & Reverse-JIT Lowering

How Smart Should Each Step Be at Runtime?

Pipeline p50 Latency 3920 ms 96.9% faster than all-agent
Monthly Compute Cost $42.97 97.9% cheaper than all-agent
Deopt Bailout Rate 28.8% Events escalated up one rung
Expected Tail Error 7.00% Combined pipeline miss rate
Triage
Rung 0 · Pure Code
Extraction
Rung 2 · Extraction
EntityMatch
Rung 1 · Decision Model
PolicyCheck
Rung 1 · Decision Model
LedgerPost
Rung 0 · Pure Code
Inspect Reverse-JIT Latency, Cost & Error Equations

For each stage i assigned rung r_i in {0,1,2,3} with base unit latency L(r), unit cost C(r), base error E(r), drift sensitivity k(r), world drift d in [0,1], and abstain band width a in [0,0.5]:

  • Stage Bailout Probability b_i: 0.45 * d on Rung 0 guards; a * (0.35 + 0.8 * d) on Rung 1 classifiers; 0.15 * a + 0.12 * d on Rung 2 extractors.
  • Effective Latency: L_eff(i) = L(r_i) + b_i * (L(min(3, r_i + 1)) - L(r_i)).
  • Mitigated Tail Error: E_i = (E(r_i) + 0.22 * d * k(r_i)) * (1 - min(0.78, 1.3 * a)) on Rungs 0–2.
  • Pipeline Rollup: Latencies and costs sum linearly; end-to-end bailout is 1 - prod(1 - b_i) and pipeline error is 1 - prod(1 - E_i).
Theoretical Archive · 02 Macros & Futamura

Go Deeper: Futamura Projections & Hygienic Macro Talks

Explore full Learning Center (6 in pillar) →

Phase separation and partial evaluation from Yoshihiko Futamura and Matthew Flatt: compiling ambiguous human intent once into a fast residual runtime.

Edoardo Vacchi · DevConf · 2023

Dr. Futamura's Projection Machine!

Guannan Wei · Type Theory Forall #37 · 2024

Compilers, Staging, and Futamura Projections