← Jev From First Principles

Semantic Programs

What does a program with deterministic and semantic computation together look like, end to end?

This chapter is model-free: the program runs over stub providers with hand-supplied numbers (labelled ILLUSTRATIVE) and deterministic retrieval (BM25, Chapter 21). The generation step is a stub. The ReAct-style comparison is a count over a declared task script — no agent or generative LLM is run in this chapter.

The question

The book has spent sixteen chapters building pieces: decide, if decide, match decide, where decide, the spec, the compiler, the graph. This chapter asks the question the pieces were for: what does a program with deterministic and semantic computation together look like, end to end?

The hypothesis and prior work

Our hypothesis: on a fixed claim-research task, the semantic program makes the same decisions as a ReAct-style loop but with every decision typed and countable and a replayable graph — and it gives up flexibility to get that. Three papers frame it:

  1. Yao et al. (2022), ReAct (2210.03629) — interleaves free-text reasoning traces with tool actions; on HotpotQA and FEVER it mitigates hallucination by querying a Wikipedia API. The challenge: every “Thought” is an untyped, free-text decision. (Read at abstract level.)
  2. Gao et al. (2022), PAL (2211.10435) — the model generates a program and the interpreter computes; the paper reports state-of-the-art few-shot accuracy on GSM8K. The method this chapter copies in miniature: delegate the deterministic work to code, keep the semantic decisions typed. (Read at abstract level.)
  3. Schick et al. (2023), Toolformer (2302.04761) — models learn when to call tools; tool use is itself a decision. The support this chapter needs: deciding to act is a decision, and making it typed is the whole point. (Read at abstract level.)

The paper that weakens the expectation is ReAct itself: its flexibility (the agent rewrites its plan from free text mid-run) is exactly what the semantic program gives up. The chapter’s comparison is therefore not “which is more accurate” — it is what the free text costs, counted as untyped decisions.

The build

src/arbiter/semantic_program.py runs the program: load a hand-built corpus of nine documents, retrieve the top 3 with BM25, decide relevance for each (where decide), decide relation for each kept document (match decide, with the tie rule from Chapter 18: a near-tie resolves to CONFLICTING_EVIDENCE), generate a [stub] summary, aggregate a support verdict, and gate the side effect with a permit/deny/escalate boundary. Every decision is recorded in a Chapter 26 decision graph; the graph is replayed from disk.

Decision flow for Chapter 34

Everything on this diagram is ordinary control flow except the three diamonds, and each diamond has an explicit exit for uncertainty. The program is deliberately a fixed graph: it cannot decide to look something else up, which is the flexibility the ReAct comparison below is about.

from arbiter.graph import categorise
from arbiter.semantic_program import research_claim, replay_seeded
# 1. research a claim: retrieve, where decide, match decide, support, act
r = research_claim("compound Q reduced inflammatory markers", seed=3)
print("1. claim A")
print(f"   retrieved {r['retrieved']} kept {r['kept']}")
print(f"   verdict {r['verdict']} p={r['p_support']} -> {r['action']} "
      f"({r['reason']})")
print(f"   typed decisions {r['typed_decisions']}, free-text {r['free_text_decisions']}")
# 2. the runtime boundary across the three cases
print("2. runtime boundary")
for tag, claim in (("B", "the alpine study recorded temperature ranges"),
                   ("C", "small pilot study reported modest drop")):
    x = research_claim(claim, seed=3)
    print(f"   {tag}: {x['verdict']} p={x['p_support']} -> {x['action']}")
# 3. replay from the Chapter 26 decision graph
print("3. replay")
print(f"   same seed 3: {categorise(replay_seeded(r, 3))}")
print(f"   changed seed 8: {categorise(replay_seeded(r, 8))}")
# 4. free-text decision count (ANALYTICAL; nothing run)
print("4. free-text decisions")
print(f"   semantic program: 0 (typed {r['typed_decisions']})")
print("   react-style loop: 5 (one free-text Thought per step, counted)")
1. claim A
   retrieved ['d9', 'd1', 'd2'] kept ['d9', 'd1', 'd2']
   verdict CONFLICTING p=0.88 -> escalate (conflicting or unknown evidence)
   typed decisions 9, free-text 0
2. runtime boundary
   B: SUPPORTED p=0.9 -> permit
   C: SUPPORTED p=0.55 -> deny
3. replay
   same seed 3: {'no_change': 9}
   changed seed 8: {'no_change': 5, 'decision_changed': 3, 'state_changed_decision_same': 1}
4. free-text decisions
   semantic program: 0 (typed 9)
   react-style loop: 5 (one free-text Thought per step, counted)

The run

run_ch34.py writes results/ch34.jsonl and the replayable graph evidence/ch34-replay.jsonl (mode program, replay, analytical).

The program on three claims (seed 3, declared; every provider a stub):

claim kept relations verdict p action
A: compound Q reduced inflammatory markers d9, d1, d2 uncertain (tie rule), supports, contradicts CONFLICTING 0.88 escalate
B: the alpine study recorded temperature ranges d3 supports SUPPORTED 0.90 permit
C: small pilot study reported modest drop d5 supports SUPPORTED 0.55 deny

The three cases cover the boundary completely: permit on confident support, deny on low-confidence support (the runtime refuses an action), escalate on conflicting evidence. Document d9 also exercises the Chapter 18 tie rule in the main run: its supports/contradicts probabilities (0.42/0.40) are within ε=0.05, so it resolves to CONFLICTING_EVIDENCE — never an arbitrary winner.

Replay (Chapter 26 graph, claim A, 9 nodes): same-seed replay gives no_change 9/9 — decisions and state hashes agree, so the stored graph is a faithful record. Replaying behind seed 8 changes three nodes — mat:d1 (supports→contradicts) and its descendants support and act — exactly the sampled nodes and their dependents, nothing else. The graph you could store yesterday tells you which decisions change when the sampling changes, and which do not.

Free-text decisions (mode analytical; nothing was run):

system free-text decisions typed decisions note
semantic program 0 9 every ask has an answer set
ReAct-style loop 5 0 one free-text Thought per step (search→read→verify→decide→finish), counted

The ReAct number is a count over the declared five-step task script, swept to show the dependence: at 3–6 steps the free-text count is 3–6, with an assumed 50–200 tokens per Thought (rows carry the sweep). No accuracy claim is made for either system; the acceptance criterion was the count, and it is reported.

What the results show

  1. The surviving construct set is enough to write the program. decide (every ask), if decide (the support/boundary branches), match decide (relations, with its tie rule firing on d9), where decide (the relevance filter). The one thing Chapter 23 rejected — for decide — appears only in its library form (a for loop over BM25Index.search). The constructs the earlier chapters kept are the ones this program used. P5 holds.

  2. The runtime boundary is a decision, not a guard clause. Permit/deny/ escalate is itself recorded in the graph with its antecedents; the deny on claim C is a decision about acting, and replay shows it. The boundary refuses an action on low confidence, and the refusal is auditable. P2 holds.

  3. Replay is the audit. Same-seed 9/9 agreement; changed-seed changes only the sampled nodes (and their ancestors). A ReAct trace is free text; you can replay it only by re-running the model. P4 holds.

  4. The count is the honest comparison. The semantic program makes 0 free-text decisions for 9 typed ones; a ReAct-style loop makes one per Thought, all 5 by the minimal script. P3 holds — and it is the entire claim: no cost or accuracy number is implied by this chapter (the only cost row is an assumed-tokens sweep, labelled ANALYTICAL).

  5. The corpus story is a property of the stubs. The verdicts are consequences of the hand-supplied relation triples and the BM25 retrieval; they are ILLUSTRATIVE, exactly as rule 14 demands. P1 holds only as “the program resolves each claim to the verdict its stub evidence dictates”.

Wrong / Correct. Wrong: “The semantic program and a ReAct loop solve the same task with the same flexibility.” Correct: “The semantic program trades ReAct’s free-text flexibility for typed, countable, replayable decisions: 0 untyped decisions, 9 typed, a graph that replays 9/9 — and gives up the agent’s ability to re-plan in natural language. Which one you want depends on whether you can enumerate the decisions in advance.”

The construct set, stated plainly

construct Chapter verdict used here
decide 16 kept every ask (relevance, relation, support, act)
if decide 17 kept support/boundary branches
match decide 18 kept relations; tie rule fired on d9
where decide 22 kept (typed, abstaining; LOTUS sem_filter, narrower, no novelty) relevance filter
for decide 23 rejected library for over retrieval

No new syntax layer was built: Chapter 23’s rejection and Chapter 22’s “filter choice matters more than syntax” make the library form the honest result, and the program is written in it.

Three claims with conflicting, supported and low-confidence outcomes, beside the typed-versus-free-text accounting and the replay result.
Figure 34.1. The construct set is sufficient to write the program, and replay is the audit a free-text trace cannot give. The provider is a stub, so this is a program result, not a model result.

How much does the comparison show?

Less than the table suggests, and it is worth saying exactly how much. The ReAct column is a count over a script we declared: five steps, one free-text Thought each. The program column counts typed decisions, nine, in a program we wrote. So “0 versus 5” is true by construction. It illustrates a difference in kind (the program’s decision points are named, typed and recorded in advance; the loop’s are generated at run time), and it does not measure anything about either system’s behaviour. If a real ReAct agent solved this task in three steps or in nine, the count would change and the distinction would not.

What the comparison can support is a statement about what you can do with each trace afterwards. The program’s decisions can be replayed from disk and the nodes that change under a different seed can be named. A ReAct trace is a transcript; to ask what would have happened under a different sample you must run the model again.

Prior art, engaged directly

ReAct and PAL are used for their main sections, Toolformer for §2-§4 and §7.

  • ReAct (2210.03629): the challenge. Its Thoughts are generated text that does not touch the environment but updates the context for later steps (§2). On HotpotQA and FEVER with a frozen PaLM-540B it scored 27.4 exact match and 60.9% accuracy, against 29.4 and 56.3% for chain-of-thought alone (Table 1). Its best results came from combining ReAct with self-consistent chain-of-thought (35.1 on HotpotQA, 64.6% on FEVER), and the paper’s error analysis found ReAct’s dominant failure to be reasoning error including repetitive loops (47%), while chain-of-thought’s was hallucination (56%) (§3.3). That error analysis is the part most relevant here. A free-text loop can loop; a fixed graph cannot, but it also cannot recover by re-planning. On ALFWorld and WebShop, where the action space is large, ReAct’s flexibility is what produced its gains (§4). The semantic program gives up exactly that.
  • PAL (2211.10435): the method, with a caveat on the analogy. PAL has the model write the program and offloads only the computation to an interpreter; on GSM8K it scored 72.0% with Codex against 65.6% for chain-of-thought, and the gap widened sharply with large numbers (about 61% against 23% on its hard variant; §4-§5). The arithmetic failure that PAL fixes is a failure of generating text that should have been computed. Our program is hand-written, so it borrows PAL’s separation of semantic work from deterministic work and not its mechanism of generating the program. PAL also found that code only helps when the model codes well enough, and that meaningful variable names matter (§6), a hint that readable structure is part of the method.
  • Toolformer (2302.04761): the support (§2-§4, §7). The model samples candidate API calls and keeps those that lower its loss on the following text by a threshold, then is fine-tuned on the kept calls, with no human labels of when to call. So choosing to call a tool is learned as a text-generation habit, and this chapter makes the same choice a typed, gated decision instead. Toolformer’s own limitations are the case for the gate: tools cannot be chained, use is not interactive, behaviour is sensitive to wording, and the model does not account for the cost of a call (§7).

No novelty is claimed over any of them; the chapter’s contribution is the count and the replay, both measured on the program’s own deterministic runs.

What to carry forward

Chapter 35 writes the verdict. It will need the honest lists: which constructs survived (this chapter used them) and which were rejected (Chapter 23’s for decide, shown in library form here). And it will need the Part VI record: the same 188 SciFact dev claims served as the test split of Chapters 21–23 and 25 — this chapter’s corpus is new and hand-built, so it does not inherit that leak, but it does inherit the discipline of labelling every stub.

Limitations

  • The corpus, relation triples, thresholds (keep 0.60, permit 0.80, ε 0.05) and sampling seed (3) are all assumed; every verdict is a consequence of them, reported as such (ILLUSTRATIVE).
  • The generation step is a [stub] template with zero tokens; no cost claim is made for it.
  • The ReAct comparison is ANALYTICAL: a count over a declared five-step script with a swept token assumption; no agent ran and no accuracy comparison is claimed (this chapter does not run a generative LLM).
  • ReAct, PAL and Toolformer are used for their methods, not a numerical comparison with this program. Their primary abstracts were checked: ReAct, PAL and Toolformer. This chapter measures neither their accuracy nor their cost.
  • The program records decisions in the graph but not their probabilities; the probabilities live in the result rows and drive the boundary, and the replay agreement is over decisions and state hashes (Chapter 26 semantics).
  • The sampling seed for the showcased run (3) differs from the preregistration’s placeholder “seed 0 and 1”; the deviation is disclosed here and in the metadata, and the replay test still covers same-seed vs changed-seed.

What this chapter establishes

Which constructs did the program actually use? All the survivors — decide, if decide, match decide, where decide — and the rejected one only as a library loop. That is the chapter’s answer to its own question, and it is the narrowest claim the book will carry into Chapter 35.