Skip to main content

logicaffeine_language/parser/
mod.rs

1//! Recursive descent parser for natural language to first-order logic.
2//!
3//! This module implements a hand-written recursive descent parser that transforms
4//! natural language sentences into logical expressions. The parser operates in two
5//! modes: **Declarative** for natural language propositions (Logicaffeine mode) and
6//! **Imperative** for strict, deterministic scoping (LOGOS mode).
7//!
8//! # Architecture
9//!
10//! The parser is split into specialized submodules:
11//!
12//! | Module | Responsibility |
13//! |--------|----------------|
14//! | `clause` | Sentence-level parsing: conditionals, conjunctions, relative clauses |
15//! | `noun` | Noun phrase parsing with determiners, adjectives, possessives |
16//! | `verb` | Verb phrase parsing, event semantics, thematic roles |
17//! | `modal` | Modal verbs (can, must, might) with Kripke semantics |
18//! | `quantifier` | Quantifier scope: every, some, no, most |
19//! | `question` | Wh-movement and question formation |
20//! | `pragmatics` | Pragmatic inference during parsing |
21//! | `common` | Shared utilities (copula lists, etc.) |
22//!
23//! # Key Types
24//!
25//! - [`Parser`]: The main parser struct holding tokens, state, and arenas
26//! - [`ParserMode`]: Declarative (NL) vs Imperative (LOGOS) mode
27//! - [`ParserGuard`]: RAII guard for speculative parsing with automatic rollback
28//! - [`NegativeScopeMode`]: Wide vs narrow scope for lexically negative verbs
29//! - [`ModalPreference`]: Default, epistemic, or deontic readings for modals
30//!
31//! # Example
32//!
33//! ```no_run
34//! use logicaffeine_language::parser::{Parser, ParserMode, ClauseParsing};
35//! # use logicaffeine_base::{Arena, Interner};
36//! # use logicaffeine_language::{lexer::Lexer, drs::WorldState, arena_ctx::AstContext, analysis::DiscoveryPass, ParseError};
37//! # fn main() -> Result<(), ParseError> {
38//! # let mut interner = Interner::new();
39//! # let mut lexer = Lexer::new("Every cat runs.", &mut interner);
40//! # let tokens = lexer.tokenize();
41//! # let type_registry = { let mut d = DiscoveryPass::new(&tokens, &mut interner); d.run() };
42//! # let ea = Arena::new(); let ta = Arena::new(); let na = Arena::new();
43//! # let sa = Arena::new(); let ra = Arena::new(); let pa = Arena::new();
44//! # let ctx = AstContext::new(&ea, &ta, &na, &sa, &ra, &pa);
45//! # let mut ws = WorldState::new();
46//! # let mut parser = Parser::new(tokens, &mut ws, &mut interner, ctx, type_registry);
47//!
48//! // Lexer produces tokens, then parser produces LogicExpr
49//! let expr = parser.parse_sentence()?;
50//! # Ok(())
51//! # }
52//! ```
53
54mod clause;
55mod component;
56mod exclusivity;
57pub mod lgx_lower;
58mod lgx_resolve;
59mod math;
60mod modal;
61mod nonstandard;
62mod noun;
63mod pragmatics;
64mod quantifier;
65mod question;
66mod verb;
67
68#[cfg(test)]
69#[path = "../../../logicaffeine_tests/unit/logicaffeine_language/parser/tests.rs"]
70mod tests;
71
72pub use clause::ClauseParsing;
73pub use modal::ModalParsing;
74pub use noun::NounParsing;
75pub use pragmatics::PragmaticsParsing;
76pub use quantifier::QuantifierParsing;
77pub use question::QuestionParsing;
78pub use verb::{ImperativeVerbParsing, LogicVerbParsing};
79
80use crate::analysis::TypeRegistry;
81use crate::arena_ctx::AstContext;
82use crate::ast::stmt::{Pattern, ReadSource, StmtId};
83use crate::ast::{
84    AspectOperator, BinaryOpKind, CompressionCodec, Expr, Literal, LogicExpr, MatchArm,
85    NeoEventData, NumberKind, QuantifierKind, SendLayout, Stmt, TemporalOperator, Term,
86    ThematicRole, TypeExpr,
87};
88use crate::drs::{Case, Gender, Number, ReferentSource};
89use crate::drs::{Drs, DrsMark, WorldState};
90use crate::error::{ParseError, ParseErrorKind};
91use crate::lexer::Lexer;
92use crate::lexicon::{self, Aspect, Definiteness, Time, VerbClass};
93use crate::optimization::{by_keyword, pin_from_str, Opt, OptimizationConfig, PinSet};
94use crate::token::{BlockType, FocusKind, Span, Token, TokenType};
95use logicaffeine_base::{Interner, Symbol, SymbolEq};
96use std::collections::HashSet;
97
98pub(super) type ParseResult<T> = Result<T, ParseError>;
99
100use std::ops::{Deref, DerefMut};
101
102/// Where each statement read inside a block sits in the source — a function's
103/// body, a loop's, a branch's — by [`StmtId`]. Top-level statements are in
104/// [`Parser::stmt_spans`] instead: they live in the returned `Vec`, whose buffer
105/// may be freed and reused, while a block lives in the arena for as long as the
106/// AST does. A statement the parser wrote itself (a desugaring's) has no entry,
107/// so a finding in it falls back to the statement the author wrote around it.
108#[derive(Debug, Clone, Default)]
109pub struct NestedStmtSpans(std::collections::HashMap<StmtId, Span>);
110
111impl NestedStmtSpans {
112    pub fn get(&self, stmt: StmtId) -> Option<Span> {
113        self.0.get(&stmt).copied()
114    }
115
116    pub fn len(&self) -> usize {
117        self.0.len()
118    }
119
120    pub fn is_empty(&self) -> bool {
121        self.0.is_empty()
122    }
123}
124
125/// The statements gathered for one block, each with the source it was read
126/// from — `None` for one the parser wrote itself (a desugaring's). A
127/// statement's identity only exists once its block is allocated, so the spans
128/// travel with the statements until [`Parser::alloc_read_block`] registers them.
129#[derive(Default)]
130pub(super) struct StmtList<'a> {
131    stmts: Vec<Stmt<'a>>,
132    spans: Vec<Option<Span>>,
133}
134
135impl<'a> StmtList<'a> {
136    /// A statement from the source, spelled by `span`.
137    pub(super) fn read(&mut self, stmt: Stmt<'a>, span: Span) {
138        self.carry(stmt, Some(span));
139    }
140
141    /// A statement the parser writes itself; it claims no source.
142    pub(super) fn wrote(&mut self, stmt: Stmt<'a>) {
143        self.carry(stmt, None);
144    }
145
146    /// A statement moved from another list or block, keeping the span it had.
147    pub(super) fn carry(&mut self, stmt: Stmt<'a>, span: Option<Span>) {
148        self.stmts.push(stmt);
149        self.spans.push(span);
150    }
151
152    /// Every statement of `rest`, after these.
153    pub(super) fn append(&mut self, rest: StmtList<'a>) {
154        self.stmts.extend(rest.stmts);
155        self.spans.extend(rest.spans);
156    }
157
158    pub(super) fn last(&self) -> Option<&Stmt<'a>> {
159        self.stmts.last()
160    }
161}
162
163impl<'a> IntoIterator for StmtList<'a> {
164    type Item = (Stmt<'a>, Option<Span>);
165    type IntoIter = std::iter::Zip<std::vec::IntoIter<Stmt<'a>>, std::vec::IntoIter<Option<Span>>>;
166
167    fn into_iter(self) -> Self::IntoIter {
168        self.stmts.into_iter().zip(self.spans)
169    }
170}
171
172/// Determines how the parser interprets sentences.
173#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
174pub enum ParserMode {
175    /// Logicaffeine mode: propositions, NeoEvents, ambiguity allowed.
176    #[default]
177    Declarative,
178    /// LOGOS mode: statements, strict scoping, deterministic.
179    Imperative,
180}
181
182/// Temporal modifier after copula: "is always Y", "is never Y", "is eventually Y".
183#[derive(Debug, Clone, Copy)]
184pub(super) enum CopulaTemporal {
185    Always,
186    Never,
187    Eventually,
188}
189
190/// Controls scope of negation for lexically negative verbs (lacks, miss).
191/// "user who lacks a key" can mean:
192///   - Wide:   ¬∃y(Key(y) ∧ Have(x,y)) - "has NO keys" (natural reading)
193///   - Narrow: ∃y(Key(y) ∧ ¬Have(x,y)) - "missing SOME key" (literal reading)
194#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
195pub enum NegativeScopeMode {
196    /// Narrow scope negation (literal reading): ∃y(Key(y) ∧ ¬Have(x,y))
197    /// "User is missing some key" - need all keys (default/traditional reading)
198    #[default]
199    Narrow,
200    /// Wide scope negation (natural reading): ¬∃y(Key(y) ∧ Have(x,y))
201    /// "User has no keys" - need at least one key
202    Wide,
203}
204
205/// Controls interpretation of polysemous modals (may, can, could).
206/// Used by compile_forest to generate multiple semantic readings.
207///
208/// Semantic Matrix:
209///   may:   Default=Permission (Deontic, Root)    Epistemic=Possibility (Alethic, Epistemic)
210///   can:   Default=Ability (Alethic, Root)       Deontic=Permission (Deontic, Root)
211///   could: Default=PastAbility (Alethic, Root)   Epistemic=Possibility (Alethic, Epistemic)
212#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
213pub enum ModalPreference {
214    /// Default readings: may=Permission, can=Ability, could=PastAbility
215    #[default]
216    Default,
217    /// Epistemic readings: may=Possibility (wide scope), could=Possibility (wide scope)
218    Epistemic,
219    /// Deontic readings: can=Permission (narrow scope, deontic domain)
220    Deontic,
221}
222
223/// Result of pronoun resolution during parsing.
224///
225/// Determines whether a pronoun refers to a bound variable (anaphoric) or
226/// Where an indefinite introduced at a point of the parse lives, which decides both its DRS
227/// source and whether it takes an existential of its own.
228#[derive(Debug, Clone, Copy, PartialEq, Eq)]
229pub(super) enum IndefiniteScope {
230    /// Under a "No" quantifier or a negation: existential inside the negation, inaccessible
231    /// outside it, never universally bound ("If John does not own a donkey, …" denies one).
232    Negated,
233    /// In a conditional antecedent: the DRS binds it universally over the whole conditional
234    /// ("If a farmer owns A DONKEY, he beats it" is every donkey he owns), so it contributes
235    /// its condition alone — an existential of its own would bind it apart from the
236    /// consequent's pronouns.
237    Antecedent,
238    /// Anywhere else: existential where it stands.
239    Local,
240}
241
242/// a deictic constant (referring to an entity outside the discourse).
243#[derive(Debug, Clone, Copy)]
244pub enum ResolvedPronoun {
245    /// Bound variable from DRS or telescope (use [`Term::Variable`]).
246    Variable(Symbol),
247    /// Constant (deictic or proper name) (use [`Term::Constant`]).
248    Constant(Symbol),
249}
250
251/// Where a speculative parse started: its token position and the DRS as it stood. Returning
252/// here abandons everything the attempt read, the referents it registered included, while the
253/// variable numbering runs on — unlike a full [`ParserCheckpoint`], whose rewind would renumber
254/// the variables a later reading prints.
255#[derive(Clone)]
256struct Backtrack {
257    pos: usize,
258    drs: DrsMark,
259}
260
261#[derive(Clone)]
262struct ParserCheckpoint {
263    pos: usize,
264    var_counter: usize,
265    bindings_len: usize,
266    island: u32,
267    time: Option<Time>,
268    negative_depth: u32,
269    drs: DrsMark,
270}
271
272/// RAII guard for speculative parsing with automatic rollback.
273///
274/// Created by [`Parser::guard()`], this guard saves the parser state
275/// on creation and automatically restores it if the guard is dropped
276/// without being committed.
277///
278/// # Usage
279///
280/// ```no_run
281/// # use logicaffeine_base::{Arena, Interner};
282/// # use logicaffeine_language::{lexer::Lexer, drs::WorldState, arena_ctx::AstContext, analysis::DiscoveryPass, ParseError};
283/// # use logicaffeine_language::parser::Parser;
284/// # fn try_parse<T>(_: &mut T) -> Result<(), ParseError> { todo!() }
285/// # fn main() -> Result<(), ParseError> {
286/// # let mut interner = Interner::new();
287/// # let mut lexer = Lexer::new("test.", &mut interner);
288/// # let tokens = lexer.tokenize();
289/// # let tr = { let mut d = DiscoveryPass::new(&tokens, &mut interner); d.run() };
290/// # let ea = Arena::new(); let ta = Arena::new(); let na = Arena::new();
291/// # let sa = Arena::new(); let ra = Arena::new(); let pa = Arena::new();
292/// # let ctx = AstContext::new(&ea, &ta, &na, &sa, &ra, &pa);
293/// # let mut ws = WorldState::new();
294/// # let mut parser = Parser::new(tokens, &mut ws, &mut interner, ctx, tr);
295/// let mut guard = parser.guard();
296/// if let Ok(result) = try_parse(&mut *guard) {
297///     guard.commit(); // Success - keep changes
298///     return Ok(result);
299/// }
300/// // guard dropped here - parser state restored
301/// # Ok(())
302/// # }
303/// ```
304pub struct ParserGuard<'p, 'a, 'ctx, 'int> {
305    parser: &'p mut Parser<'a, 'ctx, 'int>,
306    checkpoint: ParserCheckpoint,
307    committed: bool,
308}
309
310impl<'p, 'a, 'ctx, 'int> ParserGuard<'p, 'a, 'ctx, 'int> {
311    /// Commits the parse, preventing rollback when the guard is dropped.
312    pub fn commit(mut self) {
313        self.committed = true;
314    }
315}
316
317impl<'p, 'a, 'ctx, 'int> Drop for ParserGuard<'p, 'a, 'ctx, 'int> {
318    fn drop(&mut self) {
319        if !self.committed {
320            self.parser.restore(self.checkpoint.clone());
321        }
322    }
323}
324
325impl<'p, 'a, 'ctx, 'int> Deref for ParserGuard<'p, 'a, 'ctx, 'int> {
326    type Target = Parser<'a, 'ctx, 'int>;
327    fn deref(&self) -> &Self::Target {
328        self.parser
329    }
330}
331
332impl<'p, 'a, 'ctx, 'int> DerefMut for ParserGuard<'p, 'a, 'ctx, 'int> {
333    fn deref_mut(&mut self) -> &mut Self::Target {
334        self.parser
335    }
336}
337
338/// Template for constructing Neo-Davidsonian events.
339///
340/// Used during parsing to accumulate thematic roles and modifiers
341/// before the agent is known (e.g., in VP ellipsis resolution).
342#[derive(Clone, Debug)]
343pub struct EventTemplate<'a> {
344    /// The verb predicate.
345    pub verb: Symbol,
346    /// The agent term, for gapping patterns where the agent is shared/gapped
347    /// ("John gave Mary a book and Sue a pen" — John is the agent of both).
348    pub agent: Option<Term<'a>>,
349    /// Thematic roles excluding the agent (filled later).
350    pub non_agent_roles: Vec<(ThematicRole, Term<'a>)>,
351    /// Adverbial modifiers.
352    pub modifiers: Vec<Symbol>,
353}
354
355/// A mixture's gated activation: SwiGLU, or gpt-oss's SwiGLU with the gate clamped to at most `limit` and the up to
356/// `[−limit, limit]`, its sigmoid sharpened by `slope`.
357#[derive(Clone, Copy, Debug, PartialEq)]
358enum MoeAct {
359    Swiglu,
360    ClampedSwiglu { limit: f64, slope: f64 },
361}
362
363/// How a mixture scores its experts: a softmax over every router logit, or each logit's own sigmoid (`by sigmoid`).
364#[derive(Clone, Copy, Debug, PartialEq, Eq)]
365enum MoeScore {
366    Softmax,
367    Sigmoid,
368}
369
370/// The options a mixture's sugar takes beyond its weights: the router's bias, the experts' gate, up and down biases,
371/// how the experts are scored, the selection bias that chooses but never weighs (`biased by B`), the group limit
372/// (`within the top G of N groups`, as `(G, N)`), whether the kept experts' weights are renormalized to sum to one,
373/// the routed scale every kept weight is multiplied by (`scaled by S`), whether a kept expert's weight scales the input
374/// it reads rather than the output it adds (`weighting the inputs`), and the activation.
375#[derive(Clone, Copy, Debug)]
376struct MoeOptions {
377    router_bias: Option<Symbol>,
378    biases: [Option<Symbol>; 3],
379    score: MoeScore,
380    selection_bias: Option<Symbol>,
381    groups: Option<(i64, i64)>,
382    renormalize: bool,
383    scale: Option<f64>,
384    weigh_inputs: bool,
385    act: MoeAct,
386}
387
388/// An attention's dimensions: `h` query heads over `kv` key/value heads, keys `hd` and values `vd` wide per head, a
389/// cache of `l` positions, `rows` query rows.
390#[derive(Clone, Copy, Debug)]
391struct AttentionShape {
392    h: i64,
393    kv: i64,
394    hd: i64,
395    vd: i64,
396    l: i64,
397    rows: i64,
398}
399
400/// The positions a query attending `n` keeps: the last `W` of them (`within a window of W`), or those of its own chunk
401/// of `C`, `⌊(n − 1)/C⌋·C + 1 ..= n` (`within chunks of C`, Llama 4's chunked-local attention).
402#[derive(Clone, Copy, Debug)]
403enum Window {
404    Sliding(i64),
405    Chunked(i64),
406}
407
408/// The clauses an attention takes after its length: `within a window of W` or `within chunks of C`, `scaled by S`,
409/// `capped at C`, `with sinks S`.
410#[derive(Clone, Copy, Debug)]
411struct AttentionOptions {
412    window: Option<Window>,
413    scale: Option<f64>,
414    cap: Option<f64>,
415    sinks: Option<Symbol>,
416}
417
418/// Recursive descent parser for natural language to first-order logic.
419///
420/// The parser transforms a token stream (from [`Lexer`]) into logical expressions
421/// ([`LogicExpr`]). It handles complex linguistic phenomena including:
422///
423/// - Quantifier scope ambiguity
424/// - Pronoun resolution via DRS
425/// - Modal verb interpretation
426/// - Temporal and aspectual marking
427/// - VP ellipsis resolution
428///
429/// # Lifetimes
430///
431/// - `'a`: Arena lifetime for allocated AST nodes
432/// - `'ctx`: WorldState lifetime for discourse tracking
433/// - `'int`: Interner lifetime for symbol management
434/// The elementwise map an integer model's tensor forms apply, each `Y[p] = map(X[p])` in one loop.
435#[derive(Clone, Copy)]
436enum TensorElemMap {
437    /// `X times M shifted right by S` — the requant `(x · M) >> S`, an arithmetic shift of the exact product.
438    Requant { m: i64, s: i64 },
439    /// `X clamped between LO and HI` — saturation, `min(max(x, LO), HI)`.
440    Clamp { lo: i64, hi: i64 },
441    /// `relu of X` — `max(x, 0)` in the tensor's element kind.
442    Relu,
443}
444
445/// The tensor binary operators the Phase-2 sugar recognizes: `matmul` (contraction) and the elementwise
446/// `plus` / `hadamard`. Each is a distinct word, so `Let C be A <op> B` reads plainly.
447#[derive(Clone, Copy)]
448enum TensorBinop {
449    Matmul,
450    Add,
451    Hadamard,
452}
453
454impl TensorBinop {
455    fn word(self) -> &'static str {
456        match self {
457            TensorBinop::Matmul => "matmul",
458            TensorBinop::Add => "plus",
459            TensorBinop::Hadamard => "hadamard",
460        }
461    }
462}
463
464/// The unary float reduction tensor ops: `softmax of X`, `rmsnorm of X`, and `layernorm of X`.
465#[derive(Clone, Copy)]
466enum ReductionKind {
467    Softmax,
468    Rmsnorm,
469    Layernorm,
470}
471
472impl ReductionKind {
473    fn word(self) -> &'static str {
474        match self {
475            ReductionKind::Softmax => "softmax",
476            ReductionKind::Rmsnorm => "rmsnorm",
477            ReductionKind::Layernorm => "layernorm",
478        }
479    }
480}
481
482/// What a construction site's `<base> [from <qualifier>]` resolves to — see
483/// `Parser::resolve_construction_target`.
484enum ConstructionTarget {
485    /// A specific enum's variant, named directly (`from <Enum>`) or the sole unambiguous
486    /// candidate the registry knows for a bare `<base>`.
487    Variant { enum_name: Symbol, variant: Symbol },
488    /// Not a variant construction: the type name for the ordinary generic/struct-init/`Expr::New`
489    /// path — the bare name, or `Module::Base` when `from` named an import module (Phase 36).
490    Type(Symbol),
491}
492
493/// One UI `Mount <Name> [at "<target>"]` sentence, as written.
494#[derive(Clone, Debug)]
495pub(super) struct MountSite {
496    pub(super) name: Symbol,
497    /// The component name's span.
498    pub(super) span: Span,
499    /// Whether this mount says where the PROGRAM begins. A mount inside a `## Test:` drives a
500    /// scenario, and must not suppress a page's own mount.
501    pub(super) is_root: bool,
502    /// The literal `at "<target>"`, when one was written.
503    pub(super) target: Option<String>,
504}
505
506pub struct Parser<'a, 'ctx, 'int> {
507    /// Token stream from lexer.
508    pub(super) tokens: Vec<Token>,
509    /// Current position in token stream.
510    pub(super) current: usize,
511    /// File-level optimization decorators: a `## No <X>` that is NOT attached to a
512    /// following `## To` function (it appears before `## Main` or at EOF) applies
513    /// program-wide. Read after `parse_program` via [`Parser::program_opt_flags`].
514    pub(super) program_opt_flags: OptimizationConfig,
515    /// File-level tiered-optimizer pins from `## Tier <opt> <value>` decorators
516    /// (HOTSWAP §8): each pins one optimization to a hotness tier (or `eager`/`never`).
517    /// Program-wide; read after `parse_program` via [`Parser::program_tier_pins`].
518    pub(super) program_tier_pins: PinSet,
519    /// Optimizations DEMANDED via a file-level `## Require <opt>` decorator — the
520    /// inverse of `## No`. After compile, each must have fired or the compile
521    /// fails. Read via [`Parser::required_optimizations`].
522    pub(super) required_opts: Vec<Opt>,
523    /// Counter for generating fresh variables.
524    pub(super) var_counter: usize,
525    /// Every name the program BINDS (`Let`, function params) — an
526    /// over-approximation that lets word literals (`infinity`, `nan`) yield
527    /// to user variables of the same name (ambiguity-preserving: a bound
528    /// name is never shadowed by a literal reading).
529    pub(super) user_bound: std::collections::HashSet<Symbol>,
530    /// Static shapes of names bound to a `Tensor` (`Let A be a R by C tensor of Int`).
531    /// A `Tensor` is a compile-time concept — a `(rows, cols)` shape over a flat, row-major
532    /// backing `Seq` — so `Let C be A matmul B` looks the operands' shapes up here to check
533    /// the contraction and bake the loop bounds. Grows monotonically like `user_bound`.
534    pub(super) tensor_shapes: std::collections::HashMap<Symbol, (i64, i64)>,
535    /// The tensors whose elements are `Int` (a declared `tensor of Int`, or a `matmul`/elementwise result);
536    /// every other tensor holds `Real`. Kept beside `tensor_shapes` and written ONLY through `bind_tensor`, so
537    /// a name re-bound as a different kind of tensor can never keep a stale element kind.
538    pub(super) tensor_int: std::collections::HashSet<Symbol>,
539    /// Names declared by a `## Component <Name>` block. A reference to one in a
540    /// view position (`Counter`) is composed — rendered through the component
541    /// runtime — rather than treated as an HTML tag. Grows monotonically (all
542    /// components are visible program-wide).
543    pub(super) components: std::collections::HashSet<Symbol>,
544    /// Names declared by a `## To <name> […]:` header, pre-scanned from the raw token stream the
545    /// same way `components` is (`declare_function_names`) — so `Call <name>.`'s callee position
546    /// (`parse_call_statement`) can accept ANY word a `## To` header accepts as a name, whatever
547    /// lexical class the word happens to carry (a speech-act verb, a focus particle, …), instead
548    /// of guessing from a fixed, narrower set of token kinds.
549    pub(super) declared_function_names: std::collections::HashSet<Symbol>,
550    /// Names declared by a `## Directive <name> (…)` block: used as a plain attribute in a
551    /// view (`tooltip={text}`), it runs against that element instead of being set on it.
552    pub(super) directives: std::collections::HashSet<Symbol>,
553    /// The subset of `components` declared in the LGX component model (`## Component Name`
554    /// with sections) — `Mount` mounts them as the page's lowered components (`__mount_<C>`), not
555    /// through a synthesized `render`.
556    pub(super) lgx_components: std::collections::HashSet<Symbol>,
557    /// Every declared `## Store`, with the actions it declares — the table the end-of-parse
558    /// resolution pass checks `Call <Store>'s <action>` against.
559    pub(super) lgx_stores: std::collections::HashMap<Symbol, Vec<Symbol>>,
560    /// Whether the program declared `## Session`. Distinct from the `Session` record existing in
561    /// the registry — a program may define a record of that name without asking for a session.
562    pub(super) session_declared: bool,
563    /// The `keyed by` value to stamp on the NEXT element the LGX render walk creates.
564    ///
565    /// ★ `keyed by` REACHED NOTHING THAT RENDERS BEFORE THIS. The loop's key was read for the
566    /// per-item component SCOPE key and the render walk destructured `ViewNode::Each` without it,
567    /// so no browser node ever carried one — and the reconciler's entire keyed path (match a row
568    /// by key, reuse THAT node) was dead code, with every row matched positionally instead. A
569    /// reorder then looked correct, because each row's text is patched in place, while the row's
570    /// identity silently stayed put: what you typed into row one appeared in row two.
571    ///
572    /// Carried on the parser rather than in `LowerTrail` because the trail is lifetime-free and a
573    /// key is an arena expression; taken (not read) by the first element, so it lands on the row's
574    /// own node and never on its children.
575    pub(super) lgx_pending_key: Option<&'a Expr<'a>>,
576    /// The render function's own `reuse` parameter (0 on a first render, else the id of THIS
577    /// instance's own root element from its last render), consumed by the first element the view
578    /// walk creates — the same element `lgx_pending_key` stamps an identity onto. That element
579    /// becomes `el` by reusing the given id when nonzero rather than creating a fresh one, so a
580    /// component's own repaint can clear and rebuild ITS OWN root without touching the parent it
581    /// was placed in (`repaint_target`/`repaint_def`).
582    pub(super) lgx_pending_reuse: Option<Symbol>,
583    /// The state fields of the component whose `Actions:` are being lowered, by name and declared
584    /// type — set around each component's walk and empty otherwise.
585    ///
586    /// `Load "draft" into note.` names the field it writes, and browser storage holds only Text, so
587    /// the lowering has to know what `note` IS to give the field back what `Save` was handed.
588    pub(super) lgx_state_types: Vec<(Symbol, Option<Symbol>)>,
589    /// The component's `When a fetch fails …:` handler, as `(handler index, url binding, why
590    /// binding)` — what a typed fetch's error paths call, and the names they bind.
591    pub(super) lgx_fetch_fails: Option<(usize, Symbol, Symbol)>,
592    /// `(component, handler index)` for every component declaring `When this component
593    /// disappears:` — the ones whose seen-map a paint has to sweep. Declaration order, so the
594    /// emitted sweep is deterministic.
595    pub(super) lgx_sweeps: Vec<(Symbol, usize)>,
596    /// `(component, handler index, repeating, name)` for every `Every …:` / `After …:` in the
597    /// program — what `__lgx_tick` walks. A period is read per instance when it is drawn
598    /// (`__PERIOD_<C>_<i>`), so none is kept here.
599    pub(super) lgx_timers: Vec<(Symbol, usize, bool, Option<Symbol>)>,
600    /// `(component, handler index)` for every `When an action fails …:` — what `__lgx_on_failure`
601    /// dispatches over, and the order that assigns each component its id.
602    pub(super) lgx_fail_handlers: Vec<(Symbol, usize)>,
603    /// `(selector, event, value)` an in-flight turn asked to deliver — emitted AFTER its
604    /// write-back, so a delivery's own turn reads what this turn committed.
605    ///
606    /// ★ ALL THREE ARE EXPRESSIONS, which is what lets one queue serve every deferred delivery.
607    /// It carried a `&'static str` event and no value while the only members were the element
608    /// commands, whose event is known at compile time and carries nothing. `Publish M on "t"`
609    /// needs both halves computed — the topic may be a value and the message certainly is — and
610    /// the choice was a second queue or this one shaped like the dispatcher it feeds. A deferred
611    /// delivery is a dispatch that has not happened yet, so it should hold a dispatch's arguments.
612    pub(super) lgx_pending_events: Vec<(&'a Expr<'a>, &'a Expr<'a>, &'a Expr<'a>)>,
613    /// Whether the turn being lowered asked to reload — emitted after its write-back, and it
614    /// suppresses the paint, because a reload leaves nothing to paint.
615    pub(super) lgx_pending_reload: bool,
616    /// How many wrapper placements the server emitter has drawn children for — the counter that
617    /// keeps each placement's `__lgx_kidsN` local its own, so a wrapper inside a wrapper's
618    /// children does not overwrite the markup still being built around it.
619    pub(super) lgx_html_slots: usize,
620    /// The components whose view has a `{children}` hole — the ones that cannot repaint alone.
621    pub(super) lgx_wrappers: Vec<Symbol>,
622    /// Whether `__LGX_PASS` has been declared — it is one counter for the whole program, declared
623    /// beside the first live-instance registry that needs it.
624    pub(super) lgx_declared_pass: bool,
625    /// `(component, store, field, handler index)` — every `When <Store>'s <field> changes:` of a
626    /// lowered component: which fields a store action flags when it moves them, and what
627    /// `__lgx_wake_stores` runs for each.
628    pub(super) lgx_store_watchers: Vec<(Symbol, Symbol, Symbol, usize)>,
629    /// Which instance `key_expr` names, when it is not the one whose code is being written —
630    /// a wrapper's turn repainting the PAGE reads the page's own key, not the wrapper's.
631    pub(super) lgx_key_override: Option<Symbol>,
632    /// Every component PLACED by some other component's view — the placement graph's edges.
633    pub(super) lgx_placed: Vec<Symbol>,
634    /// The components some view places INSIDE a `{for each}`. They share one mount point with
635    /// their siblings, so a repaint of one must not be scoped to it — see `repaint_target`.
636    pub(super) lgx_loop_placed: Vec<Symbol>,
637    /// The component nobody places: the page a wrapper's repaint has to go through.
638    pub(super) lgx_paint_root: Option<Symbol>,
639    /// Every declared component, so a repaint can find the page's root.
640    pub(super) lgx_all_components: Vec<crate::ast::component::ComponentDef<'a>>,
641    /// Every `Call <Store>'s <action>` site, recorded WHERE IT IS PARSED so no nesting can hide
642    /// one from the check (a walker over finished statements could).
643    pub(super) store_calls: Vec<(Symbol, Symbol, Span)>,
644    /// Every `Use <Store>.` site, recorded WHERE IT IS PARSED — `ComponentDef.uses` keeps only the
645    /// name, so a `Use` that names no declared store is checked here, at the sentence.
646    pub(super) use_sites: Vec<(Symbol, Span)>,
647    /// Every write's root name and where it was written, for the WHOLE program — unlike
648    /// `component_writes` (reset per component, checked against that component's own props),
649    /// this survives to end of parse, where a write whose root names a declared `## Store` is
650    /// refused: `Call <Store>'s <action>.` is the only way to change one.
651    pub(super) all_writes: Vec<(Symbol, Span, component::WriteVerb)>,
652    /// Every action name written in `the journal shows A then B` or `Replay these actions: …`,
653    /// with where it was written — checked once the whole program's actions are known, so an
654    /// action declared below the test that names it still counts.
655    pub(super) journal_action_sites: Vec<(Symbol, Span)>,
656    /// Every UI `Mount <Name>` site, recorded WHERE IT IS PARSED — the legacy dialect DESUGARS a
657    /// mount into a synthesized `render` function the moment it is read, so by the end of the
658    /// file the sentence the author wrote is no longer in the tree to check. Checked at
659    /// end-of-parse against every component the file declares.
660    pub(super) mount_sites: Vec<MountSite>,
661    /// Every `Stop the timer <name>` and `Start the timer <name>` site in the component being
662    /// parsed, checked against the timers that component declares when its sections close.
663    pub(super) timer_stops: Vec<(Symbol, Span)>,
664    /// Every `Set <name> to …` target in the component being parsed, checked against that
665    /// component's PROPS when its sections close: a prop is what the parent said, not a cell.
666    pub(super) component_writes: Vec<component::ComponentWrite>,
667    /// Whether the cursor is inside an LGX component definition, where `State:` /
668    /// `Actions:` / `View:` / `Style:` are sections rather than a mistake.
669    pub(super) in_component: bool,
670    /// Whether this parse is a single `##` block re-parsed on its own (the editor's per-block
671    /// recovery, L11), rather than a whole file. `pub`, not `pub(super)`: `logicaffeine_lsp` sets
672    /// it before calling [`Parser::parse_program`] on tokens that are only one block — false for
673    /// every other caller (a whole file), which is the default a fresh `Parser` starts with.
674    ///
675    /// Gates exactly the four checks in `parse_program` that only mean something once every `##`
676    /// block in the file has been seen (`Mount` names something real, a page mounts itself, and
677    /// the LGX resolution pass that synthesizes render/router functions for the whole program) —
678    /// a single re-parsed block has no OTHER block to check `Mount`/routes against, so running
679    /// them against ONLY this block's own statements would report false refusals for names the
680    /// REST of the file declares.
681    ///
682    /// ★ THIS DOES NOT YET MAKE CROSS-BLOCK LGX CHECKS (`resolve_store_calls`, `check_wire_
683    /// contract`, …) CORRECT INSIDE A FRAGMENT — it makes them ABSENT there instead of WRONG,
684    /// which is the fix this session's grant covers. Giving them the whole program's name tables
685    /// from inside a fragment reparse needs each one retrofitted to read persistent, incrementally
686    /// -updated tables instead of the `statements` slice handed to `resolve_lgx` — `resolve_lgx`'s
687    /// OWN twelve sub-passes are cross-block-dependent throughout, not only in this file's four
688    /// gated calls, and doing that safely is real, separate work this grant did not scope. A
689    /// fragment reparse gets its full, correct LGX resolution from the next WHOLE-file reparse
690    /// (on save, or whatever the editor's own debounce already does) — this flag only stops a
691    /// fragment reparse from reporting a WRONG error about the rest of the file in the meantime.
692    pub fragment_mode: bool,
693    /// A monotonic parse-time counter minting a unique, render-stable scope key for
694    /// each composed component instance (`Counter#0`, `Counter#1`, …), so sibling
695    /// components keep independent scope-keyed signal state across re-renders.
696    pub(super) component_occurrence: usize,
697    /// Top-level functions synthesized while parsing a statement (the reactive
698    /// `render` entry a `Mount <Component> at "#sel"` needs). A statement can only
699    /// return one `Stmt`, but the VM/wasm backends collect functions from the
700    /// top-level list only — so these are PREPENDED to the program by `parse_program`,
701    /// making them visible (and registered before Main runs) on every tier.
702    pub(super) deferred_functions: Vec<Stmt<'a>>,
703    /// Every listener the lowering attached, in registration order — its INDEX is the id recorded
704    /// in `__LISTENER_FN`, and the dispatcher switches on it. Built during the render lowering and
705    /// read once when the dispatcher is emitted, so the two cannot disagree about what id 3 means.
706    pub(super) lgx_listeners: Vec<crate::parser::lgx_lower::ListenerSite>,
707    /// Every distinct event name any listener registered against; its INDEX is that event's code.
708    /// The dispatcher turns the runtime event name back into a code with a chain over exactly these.
709    pub(super) lgx_listener_events: Vec<Symbol>,
710    /// A page program's `-> View` templates and what the walk knows of View values
711    /// (`lgx_lower::views`).
712    pub(super) lgx_views: crate::parser::lgx_lower::ViewLowering<'a>,
713    /// Every component in the file that carries a `Style:` block, so a paint can reset their
714    /// "already emitted this pass" flags.
715    pub(super) styled_components: Vec<Symbol>,
716    /// Every view tree this parse produced, in source order — the LGX view IR
717    /// ([`crate::ast::view::ViewDef`]) that `Render:` / `Render into` / a component's
718    /// `View:` parse INTO before lowering. Read by tooling (`largo expand`, the LSP)
719    /// and by `view_ir_lock`; the lowering consumes it in place.
720    pub(super) views: Vec<crate::ast::view::ViewDef<'a>>,
721    /// Records whose derived JSON encoder a synthesized lowering CALLS — the route dispatcher
722    /// wraps a record-answering route in `toJson_<T>`, and that call is written by the compiler
723    /// rather than by the author, so the derive's reachability scan of the source cannot see it.
724    pub(super) json_encoders_needed: Vec<Symbol>,
725    /// Records a DECODER is wanted for — the program named one, or a typed fetch asks for one.
726    /// Separate from the encoder list because answering JSON is not reading it.
727    pub(super) json_decoders_needed: Vec<Symbol>,
728    /// Template findings this parse collected — the WARNINGS only, since an Error stops the
729    /// parse where it is found. Exposed for the LSP and `largo check`; see [`Parser::lgx_findings`].
730    pub(super) lgx_findings: Vec<crate::lgx::check::Finding>,
731    /// The value-semantics copy lint's own findings — see [`Parser::value_copy_findings`].
732    pub(super) value_copy_findings: Vec<crate::lint::Finding>,
733    /// The copy-lint scope for whichever function/action body is currently being parsed, if
734    /// any — `None` outside one. `parse_statement` feeds every statement it returns into
735    /// whichever scope is open; the function-body parser opens one on entry and drains it
736    /// (`CopyLintScope::finish`) into `value_copy_findings` on exit.
737    pub(super) copy_lint_scope: Option<crate::lint::CopyLintScope>,
738    /// Pending tense from temporal adverbs.
739    pub(super) pending_time: Option<Time>,
740    /// Donkey bindings: (noun, var, is_donkey_used, wide_scope_negation).
741    pub(super) donkey_bindings: Vec<(Symbol, Symbol, bool, bool)>,
742    /// String interner for symbol management.
743    pub(super) interner: &'int mut Interner,
744    /// Arena context for AST allocation.
745    pub(super) ctx: AstContext<'a>,
746    /// Current scope island ID.
747    pub(super) current_island: u32,
748    /// Whether PP attaches to noun (vs verb).
749    pub(super) pp_attach_to_noun: bool,
750    /// Filler for wh-movement gap.
751    pub(super) filler_gap: Option<Symbol>,
752    /// Depth of negation scope.
753    pub(super) negative_depth: u32,
754    /// Event variable for discourse coherence.
755    pub(super) discourse_event_var: Option<Symbol>,
756    /// Last event template for VP ellipsis.
757    pub(super) last_event_template: Option<EventTemplate<'a>>,
758    /// Pragmatic-enrichment mode: when set, gradable predication emits the degree-
759    /// standard form (§7.2) and vagueness penumbra (§8.5), and "either…or" emits its
760    /// exclusivity. Off by default so `compile()` stays literal/truth-conditional and
761    /// the proof engine sees simple predicates.
762    pub(super) pragmatic: bool,
763    /// Whether to prefer noun readings.
764    pub(super) noun_priority_mode: bool,
765    /// Whether plural NPs get collective readings.
766    pub(super) collective_mode: bool,
767    /// A floated quantifier ("the boys ALL/EACH left") forces the
768    /// distributive reading of a mixed verb that would otherwise default
769    /// to the collective one.
770    pub(super) distributive_marker: bool,
771    /// Pending cardinal for delayed quantification.
772    pub(super) pending_cardinal: Option<u32>,
773    /// Parser mode: Declarative or Imperative.
774    pub(super) mode: ParserMode,
775    /// Type registry for LOGOS mode.
776    pub(super) type_registry: Option<TypeRegistry>,
777    /// The stack of constructors currently being parsed, outermost first: `(type_name, enum
778    /// hint, fields assigned so far)`. `parse_constructor_fields` pushes its own frame on entry
779    /// and truncates back to it on every exit — it is what lets a NESTED constructor
780    /// decide whether a field name it has already assigned, seen again, is a genuine
781    /// duplicate (no enclosing frame still wants that name) or belongs to one of the
782    /// constructors enclosing it (an ancestor frame's own declared fields still needs it). The
783    /// enum hint is the enum a qualified (`from <Enum>`) or unambiguous variant construction
784    /// already settled on, so `any_enclosing_frame_wants` reads an ancestor's OWN field list
785    /// rather than an arbitrary same-named variant of a different enum.
786    pub(super) constructor_frames: Vec<(Symbol, Option<Symbol>, Vec<Symbol>)>,
787    /// Whether to produce event readings.
788    pub(super) event_reading_mode: bool,
789    /// Internal DRS for sentence-level scope tracking.
790    pub(super) drs: Drs,
791    /// Negation scope mode for lexically negative verbs.
792    pub(super) negative_scope_mode: NegativeScopeMode,
793    /// Modal interpretation preference.
794    pub(super) modal_preference: ModalPreference,
795    /// WorldState for discourse-level parsing.
796    pub(super) world_state: &'ctx mut WorldState,
797    /// Whether inside "No X" quantifier scope.
798    pub(super) in_negative_quantifier: bool,
799    /// Pending partitive superset captured from an "of the [Num]" frame:
800    /// (superset_cardinality, restriction_predicate, restriction_variable). The asserted
801    /// count quantifier is wrapped in a `Presupposition` whose presupposition is the
802    /// superset's `Cardinal` quantifier over the same restriction (§5.3).
803    pub(super) pending_partitive: Option<(u32, &'a LogicExpr<'a>, Symbol)>,
804    /// A subject's relative-clause restriction that cannot be carried on the
805    /// `NounPhrase` (it is a full clause, not an adjective/PP/possessor) and so
806    /// is stashed — keyed to the subject noun and expressed over
807    /// `Constant(noun)` — to be folded in at the `wrap_with_definiteness`
808    /// chokepoint when the predicate (e.g. a copula complement) is built over
809    /// the same constant. Without this, "The scent that sold for $75 is the
810    /// white fragrance." silently drops the relative clause.
811    pub(super) pending_subject_restriction: Option<(Symbol, &'a LogicExpr<'a>)>,
812    /// Set while parsing an object NP that a determiner/cardinal/counting
813    /// quantifier has ALREADY established as nominal (e.g. the cardinal was
814    /// consumed by the object-quantifier gate before `parse_noun_phrase` runs).
815    /// A bare verb-word after an adjective is then a DEVERBAL NOUN head ("49
816    /// previous JUMPS", "six previous RUNS") — a cardinal/determiner cannot be
817    /// followed by a finite verb — so `parse_noun_phrase` recovers it as the
818    /// head instead of stranding it. Outside this context the determiner gate
819    /// (`definiteness.is_some()`) is the only signal, to avoid eating a real
820    /// main verb ("studies hard pass").
821    pub(super) nominal_np_context: bool,
822    /// Named acceptance contracts declared by `Accept computed <Name> where <param> is an
823    /// Int from <lo> to <hi>` → `(lo, hi)`. Pure parse-time sugar: a `Run <f> on <arg> under
824    /// <Name> into <var>` statement desugars to `Let <var> be run_accepted(<f>, <arg>, lo, hi)`,
825    /// inlining the named contract's bounds — no new AST node, no runtime contract registry.
826    pub(super) contracts: std::collections::HashMap<String, (i64, i64)>,
827    /// True while parsing a `## Test` body — gates the test-only statements
828    /// (`Expect`, `Advance time`, `Given a file`) that are refused elsewhere.
829    pub(super) in_test_block: bool,
830    /// Every `## Test` prose name seen so far — duplicates are a parse error
831    /// (a test's name is its identity: filter key, report line, artifact name).
832    pub(super) test_names: std::collections::HashSet<String>,
833    /// One source span per top-level statement `parse_program` returned, in
834    /// push order — the side-table that gives typechecker diagnostics,
835    /// ownership cause-links, and the rustc sourcemap real spans without a
836    /// span field on every `Stmt` variant. Read via [`Parser::stmt_spans`].
837    ///
838    /// ★ PARSE-TIME ONLY, AND TOP-LEVEL ONLY — which is exactly what [`Stmt::Site`]/
839    /// [`Parser::failure_sites`] exist to cover instead. This table is gone by the time a COMPILED
840    /// program (bytecode, wasm, C) traps at runtime — nothing about it survives compilation — and
841    /// it was never built for anything nested inside an action's own body to begin with. The two
842    /// mechanisms answer different questions: this one is "where did the PARSER see statement i of
843    /// the top-level program", read by tools that still have the parse tree in hand; `failure_sites`
844    /// is "where did the RUNNING PROGRAM last pass through", read by whatever reports a trap.
845    pub(super) stmt_spans: Vec<Span>,
846    /// The span of each call the author wrote — `f(a, b)`, `a.f(b)`, `Call f with a` — keyed by the
847    /// arena address of its FIRST argument, the one node every call that can break the law of
848    /// exclusivity is sure to have: a call lends only through an argument. Read by
849    /// [`Parser::check_lends_are_exclusive`] to refuse an overlapping lend at the call itself.
850    pub(super) call_spans: std::collections::HashMap<usize, Span>,
851    /// `Some((component, action))` while parsing the BODY of an `Actions:`/event-handler sentence
852    /// list — the ambient context [`Parser::parse_indented_block`] reads to decide whether (and
853    /// under what name) to insert a [`Stmt::Site`] marker before each sentence it assembles. `None`
854    /// everywhere else (a `## Route`'s own body, a `## Directive`'s, a top-level `## Main` sentence)
855    /// — Wave 3 scopes this to action/handler sentences specifically, where a `Call`/`Fetch`/store
856    /// write can trap at runtime for reasons no static check catches. Set and restored around
857    /// exactly the two body-parsing calls in `parse_actions_section`, the same save/restore shape
858    /// `in_component` and `lgx_key_override` already use for their own ambient context — so a
859    /// NESTED `{if}`/`{repeat}` inside an action's body still sees it (the recursive
860    /// `parse_indented_block` call inherits whatever `self` already holds), and a sibling section
861    /// parsed afterward does not inherit a stale value.
862    pub(super) site_tracking: Option<(Symbol, Symbol)>,
863    /// Every [`Stmt::Site`] marker inserted so far, in the id order `Stmt::Site(id)` names — `id`
864    /// is this vector's own index at insertion, so `failure_sites[id]` is the marker's answer.
865    /// Read via [`Parser::failure_sites`] once parsing finishes; nothing removes an entry once
866    /// pushed; a program that discards a would-be dead branch (`{if}` in a component this tier
867    /// refuses to lower, say) simply leaves that entry unreferenced by any `Stmt::Site` the final
868    /// program actually keeps — harmless, not a leak in any sense that matters at these sizes.
869    pub(super) failure_sites: Vec<crate::ast::stmt::FailureSite>,
870    /// The span of every statement read inside a block, by identity — the
871    /// nested half of `stmt_spans`. Read via [`Parser::nested_stmt_spans`].
872    pub(super) nested_stmt_spans: NestedStmtSpans,
873    /// The moves `Give` statements have made so far, each with the ownership
874    /// it replaced, oldest first — so the moves made inside a block can be
875    /// undone when the block ends. See [`Parser::release_moves_since`].
876    pub(super) moves: Vec<(Symbol, crate::drs::OwnershipState)>,
877    /// Live recursion depth of the imperative descent (parenthesized
878    /// expressions, nested blocks). Checked against
879    /// [`crate::ast_depth::max_ast_depth`] so a parenthesis tower or block
880    /// pyramid errors gracefully instead of overflowing the parser's own
881    /// stack — the parse-time half of the AST depth gate.
882    pub(super) recursion_depth: usize,
883}
884
885impl<'a, 'ctx, 'int> Parser<'a, 'ctx, 'int> {
886    /// Create a parser with WorldState for discourse-level parsing.
887    /// WorldState is REQUIRED - there is no "single sentence mode".
888    /// A single sentence is just a discourse of length 1.
889    pub fn new(
890        mut tokens: Vec<Token>,
891        world_state: &'ctx mut WorldState,
892        interner: &'int mut Interner,
893        ctx: AstContext<'a>,
894        types: TypeRegistry,
895    ) -> Self {
896        // The parser's walk is total only over an EOF-terminated stream;
897        // fragments and block slices can arrive without one, and a missing
898        // terminator used to surface as an index-out-of-bounds panic deep
899        // in `peek()`.
900        if !matches!(tokens.last().map(|t| &t.kind), Some(TokenType::EOF)) {
901            let end = tokens.last().map(|t| t.span.end).unwrap_or(0);
902            let empty = interner.intern("");
903            tokens.push(Token::new(TokenType::EOF, empty, Span::new(end, end)));
904        }
905        Parser {
906            tokens,
907            current: 0,
908            contracts: std::collections::HashMap::new(),
909            in_test_block: false,
910            test_names: std::collections::HashSet::new(),
911            program_opt_flags: OptimizationConfig::all_on(),
912            program_tier_pins: PinSet::none(),
913            required_opts: Vec::new(),
914            var_counter: 0,
915            user_bound: std::collections::HashSet::new(),
916            tensor_shapes: std::collections::HashMap::new(),
917            tensor_int: std::collections::HashSet::new(),
918            components: std::collections::HashSet::new(),
919            declared_function_names: std::collections::HashSet::new(),
920            directives: std::collections::HashSet::new(),
921            lgx_components: std::collections::HashSet::new(),
922            lgx_stores: std::collections::HashMap::new(),
923            session_declared: false,
924            lgx_pending_key: None,
925            lgx_pending_reuse: None,
926            lgx_state_types: Vec::new(),
927            lgx_fetch_fails: None,
928            lgx_sweeps: Vec::new(),
929            lgx_timers: Vec::new(),
930            lgx_fail_handlers: Vec::new(),
931            lgx_pending_events: Vec::new(),
932            lgx_declared_pass: false,
933            lgx_pending_reload: false,
934            lgx_html_slots: 0,
935            lgx_wrappers: Vec::new(),
936            lgx_store_watchers: Vec::new(),
937            lgx_key_override: None,
938            lgx_placed: Vec::new(),
939            lgx_loop_placed: Vec::new(),
940            lgx_paint_root: None,
941            lgx_all_components: Vec::new(),
942            store_calls: Vec::new(),
943            use_sites: Vec::new(),
944            all_writes: Vec::new(),
945            journal_action_sites: Vec::new(),
946            mount_sites: Vec::new(),
947            timer_stops: Vec::new(),
948            component_writes: Vec::new(),
949            in_component: false,
950            fragment_mode: false,
951            site_tracking: None,
952            failure_sites: Vec::new(),
953            component_occurrence: 0,
954            views: Vec::new(),
955            lgx_findings: Vec::new(),
956            value_copy_findings: Vec::new(),
957            copy_lint_scope: None,
958            json_encoders_needed: Vec::new(),
959            json_decoders_needed: Vec::new(),
960            deferred_functions: Vec::new(),
961            styled_components: Vec::new(),
962            lgx_listeners: Vec::new(),
963            lgx_listener_events: Vec::new(),
964            lgx_views: Default::default(),
965            pending_time: None,
966            donkey_bindings: Vec::new(),
967            interner,
968            ctx,
969            current_island: 0,
970            pp_attach_to_noun: false,
971            filler_gap: None,
972            negative_depth: 0,
973            discourse_event_var: None,
974            last_event_template: None,
975            pragmatic: false,
976            noun_priority_mode: false,
977            collective_mode: false,
978            distributive_marker: false,
979            pending_cardinal: None,
980            mode: ParserMode::Declarative,
981            type_registry: Some(types),
982            constructor_frames: Vec::new(),
983            event_reading_mode: false,
984            drs: Drs::new(), // Internal DRS for sentence-level scope tracking
985            negative_scope_mode: NegativeScopeMode::default(),
986            modal_preference: ModalPreference::default(),
987            pending_subject_restriction: None,
988            nominal_np_context: false,
989            world_state,
990            in_negative_quantifier: false,
991            pending_partitive: None,
992            stmt_spans: Vec::new(),
993            call_spans: std::collections::HashMap::new(),
994            nested_stmt_spans: NestedStmtSpans::default(),
995            moves: Vec::new(),
996            recursion_depth: 0,
997        }
998    }
999
1000    /// Record the span of the call whose arguments are `args`, from token `start_tok` through the
1001    /// last one consumed ([`Parser::call_spans`]).
1002    fn record_call_span(&mut self, args: &[&'a Expr<'a>], start_tok: usize) {
1003        if let Some(first) = args.first() {
1004            let span = self.span_from(start_tok);
1005            self.call_spans.insert(*first as *const Expr as usize, span);
1006        }
1007    }
1008
1009    /// The span of each top-level statement from the last `parse_program`
1010    /// call, aligned 1:1 with the returned statement list.
1011    pub fn stmt_spans(&self) -> &[Span] {
1012        &self.stmt_spans
1013    }
1014
1015    /// The span of every statement read inside a block — a function's body, a
1016    /// loop's, a branch's — from the last `parse_program` call.
1017    pub fn nested_stmt_spans(&self) -> &NestedStmtSpans {
1018        &self.nested_stmt_spans
1019    }
1020
1021    /// Every `Stmt::Site` marker's own `(component, action, span)`, indexed by the `id` a
1022    /// `Stmt::Site(id)` in the returned program names — `failure_sites()[id]` answers what a
1023    /// runtime failure at that site means. See [`crate::ast::stmt::FailureSite`]'s own doc comment
1024    /// for why a single "current site" is enough, with no call stack, to answer "where did this
1025    /// trap happen".
1026    pub fn failure_sites(&self) -> &[crate::ast::stmt::FailureSite] {
1027        &self.failure_sites
1028    }
1029
1030    /// The span from the first token of `start_tok` through the last CONSUMED token (a
1031    /// terminating period or dedent already advanced past, included) — the shared computation
1032    /// [`Parser::record_stmt_span`] and a [`Stmt::Site`] marker's own span both need.
1033    fn span_from(&self, start_tok: usize) -> Span {
1034        let start = self
1035            .tokens
1036            .get(start_tok)
1037            .map(|t| t.span.start)
1038            .unwrap_or(0);
1039        let end = self
1040            .tokens
1041            .get(self.current.saturating_sub(1))
1042            .map(|t| t.span.end)
1043            .unwrap_or(start);
1044        Span::new(start, end)
1045    }
1046
1047    /// Record the span for the statement just pushed: from the first token of
1048    /// `start_tok` through the last consumed token (terminating period or
1049    /// dedent included). `parse_program` asserts 1:1 alignment on return.
1050    fn record_stmt_span(&mut self, start_tok: usize) {
1051        let span = self.span_from(start_tok);
1052        self.stmt_spans.push(span);
1053    }
1054
1055    /// What a construct parsed since `start_tok` spells: from that token's
1056    /// start through the last consumed token that is text rather than layout,
1057    /// so a block's span stops at its last full stop, not at the next line's
1058    /// indentation where its closing `Dedent` sits.
1059    fn span_since(&self, start_tok: usize) -> Span {
1060        let start = self
1061            .tokens
1062            .get(start_tok)
1063            .map(|t| t.span.start)
1064            .unwrap_or(0);
1065        let end = self
1066            .tokens
1067            .get(start_tok..self.current)
1068            .unwrap_or_default()
1069            .iter()
1070            .rev()
1071            .find(|t| {
1072                !matches!(
1073                    t.kind,
1074                    TokenType::Indent | TokenType::Dedent | TokenType::Newline
1075                )
1076            })
1077            .map(|t| t.span.end)
1078            .unwrap_or(start);
1079        Span::new(start, end.max(start))
1080    }
1081
1082    pub fn set_discourse_event_var(&mut self, var: Symbol) {
1083        self.discourse_event_var = Some(var);
1084    }
1085
1086    /// Get mutable reference to the active DRS (from WorldState).
1087    pub fn drs_mut(&mut self) -> &mut Drs {
1088        &mut self.world_state.drs
1089    }
1090
1091    /// Get immutable reference to the active DRS (from WorldState).
1092    pub fn drs_ref(&self) -> &Drs {
1093        &self.world_state.drs
1094    }
1095
1096    /// Swap DRS between Parser and WorldState.
1097    /// Call at start of parsing to get the accumulated DRS from WorldState.
1098    /// Call at end of parsing to save the updated DRS back to WorldState.
1099    pub fn swap_drs_with_world_state(&mut self) {
1100        std::mem::swap(&mut self.drs, &mut self.world_state.drs);
1101    }
1102
1103    /// WorldState is always present (no "single sentence mode")
1104    pub fn has_world_state(&self) -> bool {
1105        true
1106    }
1107
1108    pub fn mode(&self) -> ParserMode {
1109        self.mode
1110    }
1111
1112    /// Check if a symbol is a known type in the registry.
1113    /// Used to disambiguate "Stack of Integers" (generic type) vs "Owner of House" (possessive).
1114    pub fn is_known_type(&self, sym: Symbol) -> bool {
1115        self.type_registry
1116            .as_ref()
1117            .map(|r| r.is_type(sym))
1118            .unwrap_or(false)
1119    }
1120
1121    /// Check if a symbol is a known generic type (takes type parameters).
1122    /// Used to parse "Stack of Integers" as generic instantiation.
1123    pub fn is_generic_type(&self, sym: Symbol) -> bool {
1124        self.type_registry
1125            .as_ref()
1126            .map(|r| r.is_generic(sym))
1127            .unwrap_or(false)
1128    }
1129
1130    /// Get the parameter count for a generic type.
1131    fn get_generic_param_count(&self, sym: Symbol) -> Option<usize> {
1132        use crate::analysis::TypeDef;
1133        self.type_registry.as_ref().and_then(|r| {
1134            match r.get(sym) {
1135                Some(TypeDef::Generic { param_count }) => Some(*param_count),
1136                // A user's own `A Box of [T] has: …`/`An Outcome of [S] and [E] is either: …`
1137                // registers as `Struct`/`Enum` with a non-empty `generics` list, never as
1138                // `Generic` (that shape is for a BUILT-IN parametric type only) — the same two
1139                // arms `TypeRegistry::is_generic` already reads, so a type this function says IS
1140                // generic must answer how many parameters it takes, not fall through to `None`.
1141                Some(TypeDef::Struct { generics, .. }) | Some(TypeDef::Enum { generics, .. })
1142                    if !generics.is_empty() =>
1143                {
1144                    Some(generics.len())
1145                }
1146                _ => None,
1147            }
1148        })
1149    }
1150
1151    /// Phase 33: Check if a symbol is a known enum variant and return the enum name.
1152    ///
1153    /// A bare construction (`a new Failure with …`, `North`) has no context to prefer one
1154    /// enum over another the way an `Inspect` arm can (`resolve_arm_variant` — an established
1155    /// enum from an earlier arm, or the scrutinee), so when
1156    /// `TypeRegistry::find_distinct_variant_candidates` reports MORE than one enum declaring this
1157    /// variant name (an imported enum and the bare twin the assembler spliced beside it count once),
1158    /// this refuses at `span` rather than silently (if now at least
1159    /// deterministically) picking one — a construction whose meaning depends on which of two
1160    /// declared types the author had in mind is not something a compiler should guess at. The
1161    /// refusal teaches `<Variant> from <Enum>` (see `resolve_construction_target`), the
1162    /// qualification that names one of the candidates directly instead.
1163    /// `Ok(None)` means `sym` is not a variant of anything, so the caller reads it as an
1164    /// ordinary type, struct, or identifier instead.
1165    fn find_variant(&self, sym: Symbol, span: Span) -> ParseResult<Option<Symbol>> {
1166        let Some(registry) = self.type_registry.as_ref() else {
1167            return Ok(None);
1168        };
1169        let candidates = registry.find_distinct_variant_candidates(sym, self.interner);
1170        match candidates.len() {
1171            0 => Ok(None),
1172            1 => Ok(Some(candidates[0].0)),
1173            _ => {
1174                let variant_str = self.interner.resolve(sym);
1175                let mut enum_names: Vec<&str> = candidates
1176                    .iter()
1177                    .map(|(e, _)| self.interner.resolve(*e))
1178                    .collect();
1179                enum_names.sort_unstable();
1180                enum_names.dedup();
1181                let named = match enum_names.split_last() {
1182                    Some((last, [])) => (*last).to_string(),
1183                    Some((last, rest)) => format!("{} and {last}", rest.join(", ")),
1184                    None => String::new(),
1185                };
1186                let forms: Vec<String> = enum_names
1187                    .iter()
1188                    .map(|e| format!("`a new {variant_str} from {e}`"))
1189                    .collect();
1190                let taught = match forms.split_last() {
1191                    Some((last, [])) => last.clone(),
1192                    Some((last, rest)) => format!("{} or {last}", rest.join(", ")),
1193                    None => String::new(),
1194                };
1195                Err(ParseError {
1196                    kind: ParseErrorKind::Custom(format!(
1197                        "'{variant_str}' is declared by more than one type here: {named}. 'a \
1198                        new {variant_str}' cannot say which one is meant: write {taught} to name \
1199                        the one you want. (An `Inspect` arm matching a value already known to be \
1200                        one of them has no such ambiguity and needs no `from`.)"
1201                    )),
1202                    span,
1203                })
1204            }
1205        }
1206    }
1207
1208    /// What `<base_type_name> [from <qualifier>]` resolves to at a construction site (`a new
1209    /// <Base>`, `new <Base>`, or a bare variant word) — the current token is just past
1210    /// `<base_type_name>` when this is called, so an optional `from <qualifier>` is still
1211    /// there to consume.
1212    ///
1213    /// Two axes share the `from` preposition and must never be confused with each other: Phase
1214    /// 36's `<Type> from <Module>` (an import namespace — the module itself owns no entry in the
1215    /// type registry; only the qualified `Module::Type` names it brings in do) and this
1216    /// construction's `<Variant> from <Enum>` (an enum is a namespace of its OWN variants,
1217    /// resolved directly by name — no import involved). Checked in this order so neither axis
1218    /// can silently shadow the other:
1219    ///
1220    /// 1. `<qualifier>::<base>` is itself a real registered type (the Phase 36 shape) AND
1221    ///    `<qualifier>` is ALSO an enum declaring `<base>` as a variant: both readings are live
1222    ///    at once and nothing here can tell which was meant — refused.
1223    /// 2. `<qualifier>::<base>` is a real registered type and that clash does not hold: Phase 36
1224    ///    module qualification, exactly as before.
1225    /// 3. `<qualifier>` is a real enum declaring `<base>` as a variant: the qualified variant
1226    ///    construction this adds — deterministic by construction, whether or not `<base>` is
1227    ///    ALSO declared by some other enum entirely.
1228    /// 4. `<qualifier>` is a real enum, just not one declaring `<base>`: refused, naming the
1229    ///    enum actually asked for and suggesting its nearest-spelled variant.
1230    /// 5. `<base>` is a variant of at least one enum somewhere (so `from` reads as an attempted
1231    ///    variant qualification) but `<qualifier>` names no enum at all: refused, naming every
1232    ///    enum that DOES declare `<base>` so the message teaches a form that would work.
1233    /// 6. Otherwise `<base>` is not a variant of anything anywhere: `from` was always Phase 36
1234    ///    module qualification of a name today's registry does not (yet) know — `type_name` is
1235    ///    the qualified symbol, deferred to whatever check reports an unknown type.
1236    ///
1237    /// With no `from` at all, this is exactly the old bare `find_variant` call: the sole
1238    /// candidate, or `None` read as an ordinary type/struct/identifier — untouched by anything
1239    /// above.
1240    fn resolve_construction_target(
1241        &mut self,
1242        base_type_name: Symbol,
1243        base_span: Span,
1244    ) -> ParseResult<ConstructionTarget> {
1245        if !self.check(&TokenType::From) {
1246            return Ok(match self.find_variant(base_type_name, base_span)? {
1247                Some(enum_name) => ConstructionTarget::Variant {
1248                    enum_name,
1249                    variant: base_type_name,
1250                },
1251                None => ConstructionTarget::Type(base_type_name),
1252            });
1253        }
1254        self.advance(); // consume "from"
1255        let qualifier_span = self.current_span();
1256        let qualifier = self.expect_identifier()?;
1257        let qualified = {
1258            let joined = format!(
1259                "{}::{}",
1260                self.interner.resolve(qualifier),
1261                self.interner.resolve(base_type_name)
1262            );
1263            self.interner.intern(&joined)
1264        };
1265
1266        use crate::analysis::TypeDef;
1267        let registry = self.type_registry.as_ref();
1268        let qualified_is_real_type = registry.is_some_and(|r| r.is_type(qualified));
1269        let is_declared_variant =
1270            registry.is_some_and(|r| r.variant_in(qualifier, base_type_name).is_some());
1271        let qualifier_is_enum =
1272            registry.is_some_and(|r| matches!(r.get(qualifier), Some(TypeDef::Enum { .. })));
1273
1274        if qualified_is_real_type && is_declared_variant {
1275            let base_str = self.interner.resolve(base_type_name);
1276            let qualifier_str = self.interner.resolve(qualifier);
1277            return Err(ParseError {
1278                kind: ParseErrorKind::Custom(format!(
1279                    "'{qualifier_str}' names both an enum declaring the variant '{base_str}' \
1280                    and an imported module whose own '{qualifier_str}::{base_str}' is a type — \
1281                    'a new {base_str} from {qualifier_str}' cannot say which is meant. Rename \
1282                    one of the two so 'from' has only one reading here."
1283                )),
1284                span: qualifier_span,
1285            });
1286        }
1287        if qualified_is_real_type {
1288            return Ok(ConstructionTarget::Type(qualified));
1289        }
1290        if is_declared_variant {
1291            return Ok(ConstructionTarget::Variant {
1292                enum_name: qualifier,
1293                variant: base_type_name,
1294            });
1295        }
1296        if qualifier_is_enum {
1297            let base_str = self.interner.resolve(base_type_name).to_string();
1298            let qualifier_str = self.interner.resolve(qualifier).to_string();
1299            let names = self
1300                .type_registry
1301                .as_ref()
1302                .and_then(|r| r.variant_names_of(qualifier));
1303            let suggestion = names.and_then(|names| {
1304                let owned: Vec<String> = names
1305                    .iter()
1306                    .map(|n| self.interner.resolve(*n).to_string())
1307                    .collect();
1308                let refs: Vec<&str> = owned.iter().map(String::as_str).collect();
1309                crate::suggest::find_similar(&base_str, &refs, 2).map(str::to_string)
1310            });
1311            let hint = suggestion
1312                .map(|s| format!(" Did you mean '{s}'?"))
1313                .unwrap_or_default();
1314            return Err(ParseError {
1315                kind: ParseErrorKind::Custom(format!(
1316                    "'{base_str}' is not a variant of '{qualifier_str}'.{hint}"
1317                )),
1318                span: qualifier_span,
1319            });
1320        }
1321        let candidates = self
1322            .type_registry
1323            .as_ref()
1324            .map(|r| r.find_distinct_variant_candidates(base_type_name, self.interner))
1325            .unwrap_or_default();
1326        if !candidates.is_empty() {
1327            let base_str = self.interner.resolve(base_type_name).to_string();
1328            let qualifier_str = self.interner.resolve(qualifier).to_string();
1329            let mut enum_names: Vec<&str> = candidates
1330                .iter()
1331                .map(|(e, _)| self.interner.resolve(*e))
1332                .collect();
1333            enum_names.sort_unstable();
1334            enum_names.dedup();
1335            let forms: Vec<String> = enum_names
1336                .iter()
1337                .map(|e| format!("`a new {base_str} from {e}`"))
1338                .collect();
1339            let taught = match forms.split_last() {
1340                Some((last, [])) => last.clone(),
1341                Some((last, rest)) => format!("{} or {last}", rest.join(", ")),
1342                None => String::new(),
1343            };
1344            return Err(ParseError {
1345                kind: ParseErrorKind::Custom(format!(
1346                    "'{qualifier_str}' is not an enum, so 'a new {base_str} from {qualifier_str}' \
1347                    cannot build a variant from it. '{base_str}' is a variant of: write {taught}."
1348                )),
1349                span: qualifier_span,
1350            });
1351        }
1352        Ok(ConstructionTarget::Type(qualified))
1353    }
1354
1355    /// Whether a `from` sitting right after a BARE identifier `sym` (no `new`/`a new` — Phase
1356    /// 33's zero-keyword variant word, `resolve_construction_target`'s third construction site)
1357    /// is this construction's own qualifier, as opposed to an unrelated statement's OWN
1358    /// separator that merely happens to sit where a bare identifier's expression parse would
1359    /// otherwise reach for it first: `Remove k from m.`, `Pop x from xs.` and `Take n from xs.`
1360    /// all parse their own operand (`k`/`x`/`n`) through this exact bare-word path before their
1361    /// own statement parser ever gets to consume `from` itself — and `k`/`x`/`n` are ordinary
1362    /// variables, never variants, in the overwhelming common case. Called with the current token
1363    /// AT `from` (not yet consumed); consumes nothing either way.
1364    ///
1365    /// `sym from <qualifier>` is only ever THIS qualifier when BOTH hold: `sym` is a declared
1366    /// variant of SOME enum (so nothing else this sentence could mean survives the check), and
1367    /// `<qualifier>` itself resolves to a real enum or names a real module-qualified
1368    /// `<qualifier>::<sym>` type — exactly `resolve_construction_target`'s own two positive
1369    /// branches, checked here first so an enclosing statement's unrelated `from` is never
1370    /// consumed by mistake.
1371    fn bare_word_from_is_a_qualifier(&mut self, sym: Symbol) -> bool {
1372        let sym_is_a_variant_somewhere = self
1373            .type_registry
1374            .as_ref()
1375            .is_some_and(|r| !r.find_variant_candidates(sym).is_empty());
1376        if !sym_is_a_variant_somewhere {
1377            return false;
1378        }
1379        let Some(qualifier_sym) = self.peek_field_name_at(1) else {
1380            return false;
1381        };
1382        use crate::analysis::TypeDef;
1383        let qualifier_is_enum = self
1384            .type_registry
1385            .as_ref()
1386            .is_some_and(|r| matches!(r.get(qualifier_sym), Some(TypeDef::Enum { .. })));
1387        if qualifier_is_enum {
1388            return true;
1389        }
1390        let qualified = {
1391            let joined = format!(
1392                "{}::{}",
1393                self.interner.resolve(qualifier_sym),
1394                self.interner.resolve(sym)
1395            );
1396            self.interner.intern(&joined)
1397        };
1398        self.type_registry
1399            .as_ref()
1400            .is_some_and(|r| r.is_type(qualified))
1401    }
1402
1403    /// Consume a type name token (doesn't check entity registration).
1404    fn consume_type_name(&mut self) -> ParseResult<Symbol> {
1405        let t = self.advance().clone();
1406        match t.kind {
1407            TokenType::Noun(s) | TokenType::Adjective(s) => Ok(s),
1408            TokenType::ProperName(s) => Ok(s),
1409            // Verbs can be type names when lexed ambiguously (e.g., "Set" as type vs verb)
1410            TokenType::Verb { .. } => Ok(t.lexeme),
1411            // Phase 49b: CRDT type keywords are valid type names
1412            TokenType::Tally => Ok(self.interner.intern("Tally")),
1413            TokenType::SharedSet => Ok(self.interner.intern("SharedSet")),
1414            TokenType::SharedSequence => Ok(self.interner.intern("SharedSequence")),
1415            TokenType::CollaborativeSequence => Ok(self.interner.intern("CollaborativeSequence")),
1416            TokenType::SharedMap => Ok(self.interner.intern("SharedMap")),
1417            TokenType::Divergent => Ok(self.interner.intern("Divergent")),
1418            // Type parameter names (e.g. T, U) can be tokenized as articles by the English lexer
1419            TokenType::Article(_) => Ok(t.lexeme),
1420            // ★ IDENTIFIER FREEDOM, BY THE SAME RULE `expect_identifier` USES — not by a list.
1421            //
1422            // LOGOS keywords are CONTEXTUAL, never reserved: `Note that …` opens an observation and
1423            // `Note` is also an ordinary noun, so `## A Note has:` names a record and `(n: Note)`
1424            // types a parameter with it. Both are unambiguous, because a keyword only means
1425            // something at the start of a block or a sentence.
1426            //
1427            // This arm used to be a HAND-MAINTAINED LIST of freed tokens, and `expect_identifier`
1428            // holds a second one. They drifted, exactly as two lists of the same thing always do:
1429            // `Note`, `Record`, `Warn`, `During`, `Swap`, `Read`, `Write`, `Merge`, `Add`,
1430            // `Remove`, `First`, `Both`, `Either`, `Some`, `All` and the rest could all NAME a
1431            // parameter and none of them could TYPE one. So `## A Note has:` declared cleanly and
1432            // `## To titleOf (n: Note)` was refused — told that its noun was not a noun, with
1433            // nothing anywhere pointing at the collision.
1434            //
1435            // A fix that lands in one naming position and not the others is how a language ends up
1436            // with an arbitrary list of words you cannot use. So the question is asked ONCE, of the
1437            // predicate that decides it: is this token's lexeme a plain identifier word? The
1438            // canonical-name arms above stay because they intern a SPELLING (`Tally`), which is a
1439            // different fact from "this may be a name".
1440            _ if self.token_is_word_identifier(&t) => Ok(t.lexeme),
1441            other => Err(ParseError {
1442                kind: ParseErrorKind::ExpectedContentWord { found: other },
1443                span: self.current_span(),
1444            }),
1445        }
1446    }
1447
1448    /// Parse a type expression: Int, Text, List of Int, Result of Int and Text.
1449    /// Phase 36: Also supports "Type from Module" for qualified imports.
1450    /// Uses TypeRegistry to distinguish primitives from generics.
1451    /// Also handles `fn(A, B) -> C` function types.
1452    fn parse_type_expression(&mut self) -> ParseResult<TypeExpr<'a>> {
1453        use noun::NounParsing;
1454
1455        // Handle `fn(A, B) -> C` function type syntax
1456        if self.check_word("fn") {
1457            if let Some(next) = self.tokens.get(self.current + 1) {
1458                if matches!(next.kind, TokenType::LParen) {
1459                    self.advance(); // consume "fn"
1460                    self.advance(); // consume "("
1461
1462                    // Parse input types
1463                    let mut inputs = Vec::new();
1464                    if !self.check(&TokenType::RParen) {
1465                        inputs.push(self.parse_type_expression()?);
1466                        while self.check(&TokenType::Comma) {
1467                            self.advance(); // consume ","
1468                            inputs.push(self.parse_type_expression()?);
1469                        }
1470                    }
1471
1472                    if !self.check(&TokenType::RParen) {
1473                        return Err(ParseError {
1474                            kind: ParseErrorKind::ExpectedKeyword {
1475                                keyword: ")".to_string(),
1476                            },
1477                            span: self.current_span(),
1478                        });
1479                    }
1480                    self.advance(); // consume ")"
1481
1482                    // Expect ->
1483                    if !self.check(&TokenType::Arrow) {
1484                        return Err(ParseError {
1485                            kind: ParseErrorKind::ExpectedKeyword {
1486                                keyword: "->".to_string(),
1487                            },
1488                            span: self.current_span(),
1489                        });
1490                    }
1491                    self.advance(); // consume "->"
1492
1493                    let output = self.parse_type_expression()?;
1494                    let output_ref = self.ctx.alloc_type_expr(output);
1495                    let inputs_ref = self.ctx.alloc_type_exprs(inputs);
1496                    return Ok(TypeExpr::Function {
1497                        inputs: inputs_ref,
1498                        output: output_ref,
1499                    });
1500                }
1501            }
1502        }
1503
1504        // Parenthesized type: a NAMED RECORD (`(quotient: Int, remainder: Int)` — a colon follows the first
1505        // name), an ANONYMOUS TUPLE (`(Int, Int)` — comma-separated), or GROUPING (`Seq of (Seq of Int)`).
1506        if self.check(&TokenType::LParen) {
1507            self.advance(); // consume "("
1508                            // A colon right after the first name marks a named record.
1509            if self.current + 1 < self.tokens.len()
1510                && matches!(self.tokens[self.current + 1].kind, TokenType::Colon)
1511            {
1512                let mut names = Vec::new();
1513                let mut types = Vec::new();
1514                loop {
1515                    names.push(self.consume_type_name()?); // field name
1516                    if !self.check(&TokenType::Colon) {
1517                        return Err(ParseError {
1518                            kind: ParseErrorKind::ExpectedKeyword {
1519                                keyword: ":".to_string(),
1520                            },
1521                            span: self.current_span(),
1522                        });
1523                    }
1524                    self.advance(); // consume ":"
1525                    types.push(self.parse_type_expression()?);
1526                    if self.check(&TokenType::Comma) {
1527                        self.advance(); // consume ","
1528                        continue;
1529                    }
1530                    break;
1531                }
1532                if !self.check(&TokenType::RParen) {
1533                    return Err(ParseError {
1534                        kind: ParseErrorKind::ExpectedKeyword {
1535                            keyword: ")".to_string(),
1536                        },
1537                        span: self.current_span(),
1538                    });
1539                }
1540                self.advance(); // consume ")"
1541                let field_names = self.ctx.alloc_syms(names);
1542                let field_types = self.ctx.alloc_type_exprs(types);
1543                return Ok(TypeExpr::Record {
1544                    field_names,
1545                    field_types,
1546                });
1547            }
1548            let inner = self.parse_type_expression()?;
1549            if self.check(&TokenType::Comma) {
1550                // Anonymous tuple type: collect the remaining comma-separated element types.
1551                let mut elems = vec![inner];
1552                while self.check(&TokenType::Comma) {
1553                    self.advance(); // consume ","
1554                    elems.push(self.parse_type_expression()?);
1555                }
1556                if !self.check(&TokenType::RParen) {
1557                    return Err(ParseError {
1558                        kind: ParseErrorKind::ExpectedKeyword {
1559                            keyword: ")".to_string(),
1560                        },
1561                        span: self.current_span(),
1562                    });
1563                }
1564                self.advance(); // consume ")"
1565                let params = self.ctx.alloc_type_exprs(elems);
1566                return Ok(TypeExpr::Tuple(params));
1567            }
1568            if !self.check(&TokenType::RParen) {
1569                return Err(ParseError {
1570                    kind: ParseErrorKind::ExpectedKeyword {
1571                        keyword: ")".to_string(),
1572                    },
1573                    span: self.current_span(),
1574                });
1575            }
1576            self.advance(); // consume ")"
1577            return Ok(inner);
1578        }
1579
1580        // Phase 53: Handle "Persistent T" type modifier
1581        if self.check(&TokenType::Persistent) {
1582            self.advance(); // consume "Persistent"
1583            let inner = self.parse_type_expression()?;
1584            let inner_ref = self.ctx.alloc_type_expr(inner);
1585            return Ok(TypeExpr::Persistent { inner: inner_ref });
1586        }
1587
1588        // Get the base type name (must be a noun or proper name - type names bypass entity check)
1589        let mut base = self.consume_type_name()?;
1590
1591        // Phase 49c: Check for bias modifier on SharedSet: "SharedSet (RemoveWins) of T"
1592        let base_name = self.interner.resolve(base);
1593        if base_name == "SharedSet" || base_name == "ORSet" {
1594            if self.check(&TokenType::LParen) {
1595                self.advance(); // consume "("
1596                if self.check(&TokenType::RemoveWins) {
1597                    self.advance(); // consume "RemoveWins"
1598                    base = self.interner.intern("SharedSet_RemoveWins");
1599                } else if self.check(&TokenType::AddWins) {
1600                    self.advance(); // consume "AddWins"
1601                                    // AddWins is default, but we can be explicit
1602                    base = self.interner.intern("SharedSet_AddWins");
1603                }
1604                if !self.check(&TokenType::RParen) {
1605                    return Err(ParseError {
1606                        kind: ParseErrorKind::ExpectedKeyword {
1607                            keyword: ")".to_string(),
1608                        },
1609                        span: self.current_span(),
1610                    });
1611                }
1612                self.advance(); // consume ")"
1613            }
1614        }
1615
1616        // Phase 49c: Check for algorithm modifier on SharedSequence: "SharedSequence (YATA) of T"
1617        let base_name = self.interner.resolve(base);
1618        if base_name == "SharedSequence" || base_name == "RGA" {
1619            if self.check(&TokenType::LParen) {
1620                self.advance(); // consume "("
1621                if self.check(&TokenType::YATA) {
1622                    self.advance(); // consume "YATA"
1623                    base = self.interner.intern("SharedSequence_YATA");
1624                }
1625                if !self.check(&TokenType::RParen) {
1626                    return Err(ParseError {
1627                        kind: ParseErrorKind::ExpectedKeyword {
1628                            keyword: ")".to_string(),
1629                        },
1630                        span: self.current_span(),
1631                    });
1632                }
1633                self.advance(); // consume ")"
1634            }
1635        }
1636
1637        // Phase 36: Check for "from Module" qualification
1638        let base_type = if self.check(&TokenType::From) {
1639            self.advance(); // consume "from"
1640            let module_name = self.consume_type_name()?;
1641            let module_str = self.interner.resolve(module_name);
1642            let base_str = self.interner.resolve(base);
1643            let qualified = format!("{}::{}", module_str, base_str);
1644            let qualified_sym = self.interner.intern(&qualified);
1645            TypeExpr::Named(qualified_sym)
1646        } else {
1647            // Phase 38: Get param count from registry OR from built-in std types
1648            let base_name = self.interner.resolve(base);
1649            // `Quantity of <Dimension>` reads its parameter leniently (a dimension word may collide
1650            // with a keyword token); capture the predicate as a Copy bool so the `base_name` borrow
1651            // does not span the `self.advance()` calls in the parameter loop below.
1652            let base_is_quantity = base_name == "Quantity";
1653            let param_count = self
1654                .get_generic_param_count(base)
1655                // The built-in arities live in ONE table the field parsers read too, so a
1656                // `Result of Int and E` cannot mean two parameters here and one there.
1657                .or_else(|| crate::analysis::registry::builtin_generic_param_count(base_name));
1658
1659            // Check if it's a known generic type with parameters
1660            if let Some(count) = param_count {
1661                let has_preposition =
1662                    self.check_of_preposition() || self.check_preposition_is("from");
1663                // Maybe supports direct syntax without "of": "Maybe Int", "Maybe Text"
1664                let maybe_direct = !has_preposition
1665                    && base_name == "Maybe"
1666                    && matches!(
1667                        self.peek().kind,
1668                        TokenType::Noun(_)
1669                            | TokenType::Adjective(_)
1670                            | TokenType::ProperName(_)
1671                            | TokenType::Verb { .. }
1672                    );
1673                if has_preposition || maybe_direct {
1674                    if has_preposition {
1675                        self.advance(); // consume "of" or "from"
1676                    }
1677
1678                    let mut params = Vec::new();
1679                    for i in 0..count {
1680                        if i > 0 {
1681                            // Expect separator for params > 1: "and", "to", or ","
1682                            if self.check(&TokenType::And)
1683                                || self.check_to_preposition()
1684                                || self.check(&TokenType::Comma)
1685                            {
1686                                self.advance();
1687                            }
1688                        }
1689                        // `Quantity of <Dimension>` takes a DIMENSION NAME, which can collide with a
1690                        // keyword token (`Length` is the `length of` keyword). Read it leniently by
1691                        // lexeme so any dimension word is accepted in this position.
1692                        let param = if base_is_quantity {
1693                            let dim_sym = self.peek().lexeme;
1694                            self.advance();
1695                            TypeExpr::Named(dim_sym)
1696                        } else {
1697                            self.parse_type_expression()?
1698                        };
1699                        params.push(param);
1700                    }
1701
1702                    let params_slice = self.ctx.alloc_type_exprs(params);
1703                    TypeExpr::Generic {
1704                        base,
1705                        params: params_slice,
1706                    }
1707                } else {
1708                    // Generic type without parameters - treat as primitive or named
1709                    let is_primitive = self
1710                        .type_registry
1711                        .as_ref()
1712                        .map(|r| r.is_type(base))
1713                        .unwrap_or(false)
1714                        || matches!(
1715                            base_name,
1716                            "Int"
1717                                | "Nat"
1718                                | "Text"
1719                                | "Bool"
1720                                | "Boolean"
1721                                | "Real"
1722                                | "Unit"
1723                                | "Word8"
1724                                | "Word16"
1725                                | "Word32"
1726                                | "Word64"
1727                                | "Lanes8Word32"
1728                                | "Lanes4Word32"
1729                                | "Lanes16Word8"
1730                                | "Lanes4Word64"
1731                                | "Lanes16Word16"
1732                        );
1733                    if is_primitive {
1734                        TypeExpr::Primitive(base)
1735                    } else {
1736                        TypeExpr::Named(base)
1737                    }
1738                }
1739            } else {
1740                // Check if it's a known primitive type (Int, Nat, Text, Bool, Real, Unit)
1741                let is_primitive = self
1742                    .type_registry
1743                    .as_ref()
1744                    .map(|r| r.is_type(base))
1745                    .unwrap_or(false)
1746                    || matches!(
1747                        base_name,
1748                        "Int"
1749                            | "Nat"
1750                            | "Text"
1751                            | "Bool"
1752                            | "Boolean"
1753                            | "Real"
1754                            | "Unit"
1755                            | "Word8"
1756                            | "Word16"
1757                            | "Word32"
1758                            | "Word64"
1759                            | "Lanes8Word32"
1760                            | "Lanes4Word32"
1761                            | "Lanes16Word8"
1762                            | "Lanes4Word64"
1763                            | "Lanes16Word16"
1764                    );
1765                if is_primitive {
1766                    TypeExpr::Primitive(base)
1767                } else {
1768                    // User-defined or unknown type
1769                    TypeExpr::Named(base)
1770                }
1771            }
1772        };
1773
1774        // Phase 43C: Check for refinement "where" clause
1775        if self.check(&TokenType::Where) {
1776            self.advance(); // consume "where"
1777
1778            // Parse the predicate expression (supports compound: `x > 0 and x < 100`)
1779            let predicate_expr = self.parse_condition()?;
1780
1781            // Extract bound variable from the left side of the expression
1782            let bound_var = self
1783                .extract_bound_var(&predicate_expr)
1784                .unwrap_or_else(|| self.interner.intern("it"));
1785
1786            // Convert imperative Expr to logic LogicExpr
1787            let predicate = self
1788                .expr_to_logic_predicate(&predicate_expr, bound_var)
1789                .ok_or_else(|| ParseError {
1790                    kind: ParseErrorKind::InvalidRefinementPredicate,
1791                    span: self.peek().span,
1792                })?;
1793
1794            // Allocate the base type
1795            let base_alloc = self.ctx.alloc_type_expr(base_type);
1796
1797            return Ok(TypeExpr::Refinement {
1798                base: base_alloc,
1799                var: bound_var,
1800                predicate,
1801            });
1802        }
1803
1804        Ok(base_type)
1805    }
1806
1807    /// Extracts the leftmost identifier from an expression as the bound variable.
1808    fn extract_bound_var(&self, expr: &Expr<'a>) -> Option<Symbol> {
1809        match expr {
1810            Expr::Identifier(sym) => Some(*sym),
1811            Expr::BinaryOp { left, .. } => self.extract_bound_var(left),
1812            _ => None,
1813        }
1814    }
1815
1816    /// Converts an imperative comparison Expr to a Logic Kernel LogicExpr.
1817    /// Used for refinement type predicates: `Int where x > 0`
1818    fn expr_to_logic_predicate(
1819        &mut self,
1820        expr: &Expr<'a>,
1821        bound_var: Symbol,
1822    ) -> Option<&'a LogicExpr<'a>> {
1823        match expr {
1824            Expr::BinaryOp { op, left, right } => {
1825                // Map BinaryOpKind to predicate name
1826                let pred_name = match op {
1827                    BinaryOpKind::Gt => "Greater",
1828                    BinaryOpKind::Lt => "Less",
1829                    BinaryOpKind::GtEq => "GreaterEqual",
1830                    BinaryOpKind::LtEq => "LessEqual",
1831                    BinaryOpKind::Eq => "Equal",
1832                    BinaryOpKind::NotEq => "NotEqual",
1833                    BinaryOpKind::And => {
1834                        // Handle compound `x > 0 and x < 100`
1835                        let left_logic = self.expr_to_logic_predicate(left, bound_var)?;
1836                        let right_logic = self.expr_to_logic_predicate(right, bound_var)?;
1837                        return Some(self.ctx.exprs.alloc(LogicExpr::BinaryOp {
1838                            left: left_logic,
1839                            op: TokenType::And,
1840                            right: right_logic,
1841                        }));
1842                    }
1843                    BinaryOpKind::Or => {
1844                        let left_logic = self.expr_to_logic_predicate(left, bound_var)?;
1845                        let right_logic = self.expr_to_logic_predicate(right, bound_var)?;
1846                        return Some(self.ctx.exprs.alloc(LogicExpr::BinaryOp {
1847                            left: left_logic,
1848                            op: TokenType::Or,
1849                            right: right_logic,
1850                        }));
1851                    }
1852                    _ => return None, // Arithmetic ops not valid as predicates
1853                };
1854                let pred_sym = self.interner.intern(pred_name);
1855
1856                // Convert operands to Terms
1857                let left_term = self.expr_to_term(left)?;
1858                let right_term = self.expr_to_term(right)?;
1859
1860                let args = self.ctx.terms.alloc_slice([left_term, right_term]);
1861                Some(self.ctx.exprs.alloc(LogicExpr::Predicate {
1862                    name: pred_sym,
1863                    args,
1864                    world: None,
1865                }))
1866            }
1867            _ => None,
1868        }
1869    }
1870
1871    /// Converts an imperative Expr to a logic Term.
1872    fn expr_to_term(&mut self, expr: &Expr<'a>) -> Option<Term<'a>> {
1873        match expr {
1874            Expr::Identifier(sym) => Some(Term::Variable(*sym)),
1875            Expr::Literal(lit) => {
1876                match lit {
1877                    Literal::Number(n) => Some(Term::Value {
1878                        kind: NumberKind::Integer(*n),
1879                        unit: None,
1880                        dimension: None,
1881                    }),
1882                    Literal::Boolean(b) => {
1883                        let sym = self.interner.intern(if *b { "true" } else { "false" });
1884                        Some(Term::Constant(sym))
1885                    }
1886                    _ => None, // Text, Nothing not supported in predicates
1887                }
1888            }
1889            _ => None,
1890        }
1891    }
1892
1893    pub fn process_block_headers(&mut self) {
1894        use crate::token::BlockType;
1895
1896        while self.current < self.tokens.len() {
1897            if let TokenType::BlockHeader { block_type } = &self.tokens[self.current].kind {
1898                self.mode = match block_type {
1899                    BlockType::Main
1900                    | BlockType::Function
1901                    | BlockType::Component
1902                    | BlockType::Test { .. }
1903                    | BlockType::Page { .. }
1904                    | BlockType::Store
1905                    | BlockType::Session
1906                    | BlockType::Directive
1907                    | BlockType::Route { .. }
1908                    | BlockType::Middleware
1909                    | BlockType::Redirect { .. }
1910                    | BlockType::ErrorPage { .. } => ParserMode::Imperative,
1911                    BlockType::Theorem
1912                    | BlockType::Definition
1913                    | BlockType::Define
1914                    | BlockType::Proof
1915                    | BlockType::Example
1916                    | BlockType::Logic
1917                    | BlockType::Note
1918                    | BlockType::TypeDef
1919                    | BlockType::Policy
1920                    | BlockType::Requires
1921                    | BlockType::Axiom
1922                    | BlockType::Theory
1923                    | BlockType::Hardware
1924                    | BlockType::Property
1925                    | BlockType::SuspectedTypo { .. } => ParserMode::Declarative,
1926                    BlockType::No | BlockType::Tier | BlockType::Require => self.mode, // Annotation — keep current mode
1927                };
1928                self.current += 1;
1929            } else {
1930                break;
1931            }
1932        }
1933    }
1934
1935    /// An event and the conditions on it — its PPs, `In(e, Park)` — as one formula. The
1936    /// conditions are about THIS event, so they go under its own ∃e; an if-clause's event is
1937    /// bound outside (`suppress_existential`), and its conditions simply join it.
1938    pub(super) fn event_with_conditions(
1939        &mut self,
1940        event: NeoEventData<'a>,
1941        conditions: Vec<&'a LogicExpr<'a>>,
1942    ) -> &'a LogicExpr<'a> {
1943        let binds_its_conditions = !event.suppress_existential && !conditions.is_empty();
1944        let event_var = event.event_var;
1945        let mut combined = self
1946            .ctx
1947            .exprs
1948            .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
1949                suppress_existential: event.suppress_existential || binds_its_conditions,
1950                ..event
1951            })));
1952        for condition in conditions {
1953            combined = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
1954                left: combined,
1955                op: TokenType::And,
1956                right: condition,
1957            });
1958        }
1959        if binds_its_conditions {
1960            combined = self.ctx.exprs.alloc(LogicExpr::Quantifier {
1961                kind: QuantifierKind::Existential,
1962                variable: event_var,
1963                body: combined,
1964                island_id: self.current_island,
1965            });
1966        }
1967        combined
1968    }
1969
1970    pub fn get_event_var(&mut self) -> Symbol {
1971        self.discourse_event_var
1972            .unwrap_or_else(|| self.interner.intern("e"))
1973    }
1974
1975    pub fn capture_event_template(
1976        &mut self,
1977        verb: Symbol,
1978        roles: &[(ThematicRole, Term<'a>)],
1979        modifiers: &[Symbol],
1980    ) {
1981        let non_agent_roles: Vec<_> = roles
1982            .iter()
1983            .filter(|(role, _)| *role != ThematicRole::Agent)
1984            .cloned()
1985            .collect();
1986        let agent = roles
1987            .iter()
1988            .find(|(role, _)| *role == ThematicRole::Agent)
1989            .map(|(_, t)| t.clone());
1990        self.last_event_template = Some(EventTemplate {
1991            verb,
1992            agent,
1993            non_agent_roles,
1994            modifiers: modifiers.to_vec(),
1995        });
1996    }
1997
1998    /// A locative/circumstance relative clause headed by "where" ("the episode WHERE
1999    /// the survivor brought the knife"). The head `gap_var` is the LOCATION of the
2000    /// clause's event: parse the embedded subject + its full VP, then conjoin
2001    /// In(event, gap_var). The event var is the stable discourse "e" the VP builder
2002    /// uses, so In(e, gap) sits alongside the ∃e(...) like other event PPs.
2003    /// A possessive relative clause headed by "whose" ("the town WHOSE mayor is Kevin
2004    /// King", "the team WHOSE final score was 739"). The possessed NP belongs to the
2005    /// head `gap_var`; introduce it as a fresh entity m with Noun(m) ∧ Possesses(gap,
2006    /// m), then predicate the clause's VP over m, all under ∃m.
2007    pub(super) fn parse_whose_relative(
2008        &mut self,
2009        gap_var: Symbol,
2010    ) -> ParseResult<&'a LogicExpr<'a>> {
2011        self.advance(); // "whose"
2012        let possessed = self.parse_noun_phrase(false)?;
2013        let m = self.next_var_name();
2014        let noun_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
2015            name: possessed.noun,
2016            args: self.ctx.terms.alloc_slice([Term::Variable(m)]),
2017            world: None,
2018        });
2019        let possesses = self.interner.intern("Possesses");
2020        let poss_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
2021            name: possesses,
2022            args: self
2023                .ctx
2024                .terms
2025                .alloc_slice([Term::Variable(gap_var), Term::Variable(m)]),
2026            world: None,
2027        });
2028        let mut body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
2029            left: noun_pred,
2030            op: TokenType::And,
2031            right: poss_pred,
2032        });
2033        for &adj in possessed.adjectives {
2034            let adj_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
2035                name: adj,
2036                args: self.ctx.terms.alloc_slice([Term::Variable(m)]),
2037                world: None,
2038            });
2039            body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
2040                left: body,
2041                op: TokenType::And,
2042                right: adj_pred,
2043            });
2044        }
2045        let vp = self.parse_predicate_with_subject_as_var(m)?;
2046        body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
2047            left: body,
2048            op: TokenType::And,
2049            right: vp,
2050        });
2051        Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
2052            kind: QuantifierKind::Existential,
2053            variable: m,
2054            body,
2055            island_id: self.current_island,
2056        }))
2057    }
2058
2059    pub(super) fn parse_where_relative(
2060        &mut self,
2061        gap_var: Symbol,
2062    ) -> ParseResult<&'a LogicExpr<'a>> {
2063        self.advance(); // "where"
2064                        // The embedded clause's own subject: a pronoun ("they"), a definite NP ("the
2065                        // survivor"), or a proper name. A pronoun is not parsed by parse_noun_phrase,
2066                        // so take its (capitalized) lexeme as the agent constant.
2067        let subj_sym = if self.check_pronoun() {
2068            let lex = self.interner.resolve(self.peek().lexeme).to_string();
2069            self.advance();
2070            let mut chars = lex.chars();
2071            let cap = match chars.next() {
2072                Some(f) => f.to_uppercase().collect::<String>() + chars.as_str(),
2073                None => lex,
2074            };
2075            self.interner.intern(&cap)
2076        } else {
2077            self.parse_noun_phrase(true)?.noun
2078        };
2079        // Bound the embedded VP at the relative-clause boundary so it does not swallow
2080        // an OUTER list coordinator ("the town where they met, and the city" / "…, and
2081        // Carol"): a comma, or an "and"/"or" that introduces a NEW NP (article / proper
2082        // name / quantifier), as opposed to a genuine VP coordinator ("where they met
2083        // and left"). Temporarily retag that token as a clause terminator while the VP
2084        // parses (the same boundary trick the of-pair path uses), then restore it.
2085        let boundary = {
2086            let mut found = None;
2087            let mut k = self.current;
2088            while let Some(tok) = self.tokens.get(k) {
2089                match &tok.kind {
2090                    TokenType::Comma => {
2091                        found = Some(k);
2092                        break;
2093                    }
2094                    TokenType::And | TokenType::Or
2095                        if self
2096                            .tokens
2097                            .get(k + 1)
2098                            .is_some_and(|t| t.kind.is_generalized_quantifier())
2099                            || matches!(
2100                                self.tokens.get(k + 1).map(|t| &t.kind),
2101                                Some(TokenType::Article(_))
2102                                    | Some(TokenType::ProperName(_))
2103                                    | Some(TokenType::All)
2104                                    | Some(TokenType::Some)
2105                                    | Some(TokenType::Any)
2106                                    | Some(TokenType::No)
2107                            ) =>
2108                    {
2109                        found = Some(k);
2110                        break;
2111                    }
2112                    TokenType::Period
2113                    | TokenType::EOF
2114                    | TokenType::Is
2115                    | TokenType::Are
2116                    | TokenType::Was
2117                    | TokenType::Were => break,
2118                    _ => k += 1,
2119                }
2120            }
2121            found
2122        };
2123        let saved_boundary = boundary.map(|i| (i, self.tokens[i].clone()));
2124        if let Some(i) = boundary {
2125            let span = self.tokens[i].span;
2126            self.tokens[i] =
2127                crate::token::Token::new(TokenType::Period, self.interner.intern("."), span);
2128        }
2129        let inner_result = self.parse_predicate_with_subject(subj_sym);
2130        if let Some((i, tok)) = saved_boundary {
2131            self.tokens[i] = tok;
2132        }
2133        let inner = inner_result?;
2134        let ev = self.get_event_var();
2135        let loc = self.interner.intern("In");
2136        let loc_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
2137            name: loc,
2138            args: self
2139                .ctx
2140                .terms
2141                .alloc_slice([Term::Variable(ev), Term::Variable(gap_var)]),
2142            world: None,
2143        });
2144        Ok(self.ctx.exprs.alloc(LogicExpr::BinaryOp {
2145            left: inner,
2146            op: TokenType::And,
2147            right: loc_pred,
2148        }))
2149    }
2150
2151    fn parse_embedded_wh_clause(&mut self) -> ParseResult<&'a LogicExpr<'a>> {
2152        // Parse embedded question body: "who runs", "what John ate"
2153        let var_name = self.interner.intern("x");
2154        let var_term = Term::Variable(var_name);
2155
2156        if self.check_verb() {
2157            // "who runs" pattern
2158            let verb = self.consume_verb();
2159            let body = self.ctx.exprs.alloc(LogicExpr::Predicate {
2160                name: verb,
2161                args: self.ctx.terms.alloc_slice([var_term]),
2162                world: None,
2163            });
2164            return Ok(body);
2165        }
2166
2167        if self.check_content_word() || self.check_article() {
2168            // "what John ate" pattern
2169            let subject = self.parse_noun_phrase(true)?;
2170            if self.check_verb() {
2171                let verb = self.consume_verb();
2172                let body = self.ctx.exprs.alloc(LogicExpr::Predicate {
2173                    name: verb,
2174                    args: self
2175                        .ctx
2176                        .terms
2177                        .alloc_slice([Term::Constant(subject.noun), var_term]),
2178                    world: None,
2179                });
2180                return Ok(body);
2181            }
2182        }
2183
2184        // Fallback: just the wh-variable
2185        Ok(self.ctx.exprs.alloc(LogicExpr::Atom(var_name)))
2186    }
2187
2188    pub fn set_pp_attachment_mode(&mut self, attach_to_noun: bool) {
2189        self.pp_attach_to_noun = attach_to_noun;
2190    }
2191
2192    pub fn set_noun_priority_mode(&mut self, mode: bool) {
2193        self.noun_priority_mode = mode;
2194    }
2195
2196    pub fn set_collective_mode(&mut self, mode: bool) {
2197        self.collective_mode = mode;
2198    }
2199
2200    /// Force the distributive (per-member) reading of mixed verbs with
2201    /// definite plurals — the readings enumerator's counterpart to the
2202    /// default collective reading.
2203    pub fn set_distributive_marker(&mut self, on: bool) {
2204        self.distributive_marker = on;
2205    }
2206
2207    /// Enable pragmatic enrichment (scalar implicatures, degree standards)
2208    /// for whole-program parses — the defeasible theorem door needs the
2209    /// implicature channel that the literal parse omits.
2210    pub fn set_pragmatic_mode(&mut self, on: bool) {
2211        self.pragmatic = on;
2212    }
2213
2214    pub fn set_event_reading_mode(&mut self, mode: bool) {
2215        self.event_reading_mode = mode;
2216    }
2217
2218    pub fn set_negative_scope_mode(&mut self, mode: NegativeScopeMode) {
2219        self.negative_scope_mode = mode;
2220    }
2221
2222    pub fn set_modal_preference(&mut self, pref: ModalPreference) {
2223        self.modal_preference = pref;
2224    }
2225
2226    fn checkpoint(&self) -> ParserCheckpoint {
2227        ParserCheckpoint {
2228            pos: self.current,
2229            var_counter: self.var_counter,
2230            bindings_len: self.donkey_bindings.len(),
2231            island: self.current_island,
2232            time: self.pending_time,
2233            negative_depth: self.negative_depth,
2234            drs: self.drs.mark(),
2235        }
2236    }
2237
2238    fn restore(&mut self, cp: ParserCheckpoint) {
2239        self.current = cp.pos;
2240        self.var_counter = cp.var_counter;
2241        self.donkey_bindings.truncate(cp.bindings_len);
2242        self.current_island = cp.island;
2243        self.pending_time = cp.time;
2244        self.negative_depth = cp.negative_depth;
2245        self.drs.rewind(&cp.drs);
2246    }
2247
2248    /// Where a speculative parse starts, to return to if it does not pan out.
2249    fn backtrack_point(&self) -> Backtrack {
2250        Backtrack {
2251            pos: self.current,
2252            drs: self.drs.mark(),
2253        }
2254    }
2255
2256    /// Return to `point`: the token position, and the DRS as it stood — a referent the
2257    /// abandoned attempt registered does not outlive it.
2258    fn backtrack_to(&mut self, point: &Backtrack) {
2259        self.current = point.pos;
2260        self.drs.rewind(&point.drs);
2261    }
2262
2263    fn is_negative_context(&self) -> bool {
2264        self.negative_depth % 2 == 1
2265    }
2266
2267    /// Where an indefinite introduced here lives — see [`IndefiniteScope`].
2268    pub(super) fn indefinite_scope(&self) -> IndefiniteScope {
2269        if self.in_negative_quantifier || self.is_negative_context() {
2270            IndefiniteScope::Negated
2271        } else if self.drs.in_conditional_antecedent() {
2272            IndefiniteScope::Antecedent
2273        } else {
2274            IndefiniteScope::Local
2275        }
2276    }
2277
2278    /// Register an indefinite's referent in the DRS as its scope decides.
2279    pub(super) fn introduce_indefinite(
2280        &mut self,
2281        var: Symbol,
2282        noun: Symbol,
2283        gender: Gender,
2284        number: Number,
2285    ) -> IndefiniteScope {
2286        let scope = self.indefinite_scope();
2287        if scope == IndefiniteScope::Negated {
2288            self.drs.introduce_referent_with_source(
2289                var,
2290                noun,
2291                gender,
2292                number,
2293                ReferentSource::NegationScope,
2294            );
2295        } else {
2296            self.drs.introduce_referent(var, noun, gender, number);
2297        }
2298        scope
2299    }
2300
2301    pub fn guard(&mut self) -> ParserGuard<'_, 'a, 'ctx, 'int> {
2302        ParserGuard {
2303            checkpoint: self.checkpoint(),
2304            parser: self,
2305            committed: false,
2306        }
2307    }
2308
2309    pub(super) fn try_parse<F, T>(&mut self, op: F) -> Option<T>
2310    where
2311        F: FnOnce(&mut Self) -> ParseResult<T>,
2312    {
2313        let cp = self.checkpoint();
2314        match op(self) {
2315            Ok(res) => Some(res),
2316            Err(_) => {
2317                self.restore(cp);
2318                None
2319            }
2320        }
2321    }
2322
2323    fn resolve_pronoun(&mut self, gender: Gender, number: Number) -> ParseResult<ResolvedPronoun> {
2324        // MODAL BARRIER: In discourse mode, try telescope FIRST if prior sentence was modal.
2325        // This ensures the modal barrier check runs before we search the swapped DRS.
2326        // The DRS contains referents from all prior sentences (merged via swap), but
2327        // telescope has modal filtering to block hypothetical entities from reality.
2328        if self.world_state.in_discourse_mode() && self.world_state.has_prior_modal_context() {
2329            // Only use telescope for cross-sentence reference when prior was modal
2330            // Telescope has modal barrier: won't return modal candidates unless we're in modal context
2331            if let Some(candidate) = self.world_state.resolve_via_telescope(gender) {
2332                return Ok(ResolvedPronoun::Variable(candidate.variable));
2333            }
2334            // Modal barrier blocked telescope resolution - pronoun can't access hypothetical entities
2335            // from the prior modal sentence. Check if there are ANY telescope candidates that
2336            // were blocked due to modal scope. If so, this is a modal barrier error.
2337            let blocked_candidates: Vec<_> = self
2338                .world_state
2339                .telescope_candidates()
2340                .iter()
2341                .filter(|c| c.in_modal_scope)
2342                .collect();
2343            if !blocked_candidates.is_empty() {
2344                // Before returning error, look ahead for modal subordinating verbs (would, could, etc.)
2345                // If we see one, this sentence is also modal and can access the hypothetical entity.
2346                let has_upcoming_modal = self.has_modal_subordination_ahead();
2347                if has_upcoming_modal {
2348                    // Modal subordination detected - allow access to the first matching candidate
2349                    if let Some(candidate) = blocked_candidates.into_iter().find(|c| {
2350                        c.gender == gender
2351                            || gender == Gender::Unknown
2352                            || c.gender == Gender::Unknown
2353                    }) {
2354                        return Ok(ResolvedPronoun::Variable(candidate.variable));
2355                    }
2356                }
2357                // There were candidates but they're all in modal scope - modal barrier blocks access
2358                return Err(ParseError {
2359                    kind: ParseErrorKind::ScopeViolation(
2360                        "Cannot access hypothetical entity from reality. Use modal subordination (e.g., 'would') to continue a hypothetical context.".to_string()
2361                    ),
2362                    span: self.current_span(),
2363                });
2364            }
2365            // No modal candidates were blocked, continue to check for same-sentence referents
2366        }
2367
2368        // Try DRS resolution (scope-aware) for same-sentence referents
2369        let current_box = self.drs.current_box_index();
2370        match self.drs.resolve_pronoun(current_box, gender, number) {
2371            Ok(sym) => {
2372                // A rigid referent (proper name / deictic constant) denotes
2373                // the constant itself — "He has children." after "His
2374                // children…" keeps Him a constant, matching the premise.
2375                return Ok(if self.drs.is_rigid_referent(sym) {
2376                    ResolvedPronoun::Constant(sym)
2377                } else {
2378                    ResolvedPronoun::Variable(sym)
2379                });
2380            }
2381            Err(crate::drs::ScopeError::InaccessibleReferent {
2382                gender: g, reason, ..
2383            }) => {
2384                // Referent exists but is trapped in inaccessible scope.
2385                // In multi-sentence discourse, try telescoping first - the referent
2386                // from a previous sentence's conditional may be in telescope candidates.
2387                if self.world_state.in_discourse_mode() {
2388                    if let Some(candidate) = self.world_state.resolve_via_telescope(g) {
2389                        return Ok(ResolvedPronoun::Variable(candidate.variable));
2390                    }
2391                }
2392                // No telescope candidate found - this is a hard scope error
2393                return Err(ParseError {
2394                    kind: ParseErrorKind::ScopeViolation(reason),
2395                    span: self.current_span(),
2396                });
2397            }
2398            Err(crate::drs::ScopeError::NoMatchingReferent {
2399                gender: g,
2400                number: n,
2401            }) => {
2402                // Try telescoping across sentence boundaries (if not already tried above)
2403                if !self.world_state.has_prior_modal_context() {
2404                    if let Some(candidate) = self.world_state.resolve_via_telescope(g) {
2405                        return Ok(ResolvedPronoun::Variable(candidate.variable));
2406                    }
2407                }
2408
2409                // In discourse mode (multi-sentence context), unresolved pronouns are an error
2410                if self.world_state.in_discourse_mode() {
2411                    return Err(ParseError {
2412                        kind: ParseErrorKind::UnresolvedPronoun {
2413                            gender: g,
2414                            number: n,
2415                        },
2416                        span: self.current_span(),
2417                    });
2418                }
2419
2420                // No prior referent - introduce deictic referent (pointing to someone in the world)
2421                // This handles sentences like "She told him a story" without prior discourse
2422                let deictic_name = match (g, n) {
2423                    (Gender::Male, Number::Singular) => "Him",
2424                    (Gender::Female, Number::Singular) => "Her",
2425                    (Gender::Neuter, Number::Singular) => "It",
2426                    (Gender::Male, Number::Plural) | (Gender::Female, Number::Plural) => "Them",
2427                    (Gender::Neuter, Number::Plural) => "Them",
2428                    (Gender::Unknown, _) => "Someone",
2429                };
2430                let sym = self.interner.intern(deictic_name);
2431                // Introduce the deictic as a RIGID referent (it denotes a
2432                // fixed individual in the world) so later pronouns resolving
2433                // to it also yield the constant.
2434                self.drs.introduce_referent_with_source(
2435                    sym,
2436                    sym,
2437                    g,
2438                    n,
2439                    crate::drs::ReferentSource::ProperName,
2440                );
2441                return Ok(ResolvedPronoun::Constant(sym));
2442            }
2443        }
2444    }
2445
2446    fn resolve_donkey_pronoun(&mut self, gender: Gender) -> Option<Symbol> {
2447        for (noun_class, var_name, used, _wide_neg) in self.donkey_bindings.iter_mut().rev() {
2448            let noun_str = self.interner.resolve(*noun_class);
2449            let noun_gender = Self::infer_noun_gender(noun_str);
2450            if noun_gender == gender || gender == Gender::Neuter || noun_gender == Gender::Unknown {
2451                *used = true; // Mark as used by a pronoun (donkey anaphor)
2452                return Some(*var_name);
2453            }
2454        }
2455        None
2456    }
2457
2458    fn infer_noun_gender(noun: &str) -> Gender {
2459        let lower = noun.to_lowercase();
2460        if lexicon::is_female_noun(&lower) {
2461            Gender::Female
2462        } else if lexicon::is_male_noun(&lower) {
2463            Gender::Male
2464        } else if lexicon::is_neuter_noun(&lower) {
2465            Gender::Neuter
2466        } else {
2467            Gender::Unknown
2468        }
2469    }
2470
2471    fn is_plural_noun(noun: &str) -> bool {
2472        let lower = noun.to_lowercase();
2473        // Proper names like "Socrates", "James", "Chris" end in 's' but aren't plurals
2474        if lexicon::is_proper_name(&lower) {
2475            return false;
2476        }
2477        if lexicon::is_irregular_plural(&lower) {
2478            return true;
2479        }
2480        lower.ends_with('s') && !lower.ends_with("ss") && lower.len() > 2
2481    }
2482
2483    fn singularize_noun(noun: &str) -> String {
2484        let lower = noun.to_lowercase();
2485        if let Some(singular) = lexicon::singularize(&lower) {
2486            return singular.to_string();
2487        }
2488        if lower.ends_with('s') && !lower.ends_with("ss") && lower.len() > 2 {
2489            let base = &lower[..lower.len() - 1];
2490            let mut chars: Vec<char> = base.chars().collect();
2491            if !chars.is_empty() {
2492                chars[0] = chars[0].to_uppercase().next().unwrap();
2493            }
2494            return chars.into_iter().collect();
2495        }
2496        let mut chars: Vec<char> = lower.chars().collect();
2497        if !chars.is_empty() {
2498            chars[0] = chars[0].to_uppercase().next().unwrap();
2499        }
2500        chars.into_iter().collect()
2501    }
2502
2503    fn infer_gender(name: &str) -> Gender {
2504        let lower = name.to_lowercase();
2505        if lexicon::is_male_name(&lower) {
2506            Gender::Male
2507        } else if lexicon::is_female_name(&lower) {
2508            Gender::Female
2509        } else {
2510            Gender::Unknown
2511        }
2512    }
2513
2514    /// Builds the agent term for a subject, applying metonymic coercion (§8.6):
2515    /// a place that conventionally denotes an institution ("the White House") is
2516    /// coerced to that institution when it acts — `GovernmentOf(white_house)`.
2517    /// Strengthens a weak scalar quantifier with its scalar implicature (§8.7): on the
2518    /// Horn scale ⟨some, most, many … all⟩ a weak term implicates the negation of its
2519    /// stronger "all" alternative. Given `Q x(R(x) ∧ S(x))` with `Q ∈ {∃, Most, Many}`,
2520    /// produces `Q x(R(x) ∧ S(x)) +> ¬∀x(R(x) → S(x))`. Returns `None` for a strong term
2521    /// ("all") or any non-scalar shape. (Open-class scalars like warm/hot would extend
2522    /// this through a lexicon Horn-scale; the closed-class quantifier scale is here.)
2523    fn scalar_implicature(&mut self, expr: &'a LogicExpr<'a>) -> Option<&'a LogicExpr<'a>> {
2524        if let LogicExpr::Quantifier {
2525            kind: QuantifierKind::Existential | QuantifierKind::Most | QuantifierKind::Many,
2526            variable,
2527            body,
2528            island_id,
2529        } = expr
2530        {
2531            // The "all" alternative: restriction implies scope, universally.
2532            let alt_body = if let LogicExpr::BinaryOp {
2533                left,
2534                op: TokenType::And,
2535                right,
2536            } = body
2537            {
2538                self.ctx.exprs.alloc(LogicExpr::BinaryOp {
2539                    left,
2540                    op: TokenType::Implies,
2541                    right,
2542                })
2543            } else {
2544                body
2545            };
2546            let universal = self.ctx.exprs.alloc(LogicExpr::Quantifier {
2547                kind: QuantifierKind::Universal,
2548                variable: *variable,
2549                body: alt_body,
2550                island_id: *island_id,
2551            });
2552            let negated = self.ctx.exprs.alloc(LogicExpr::UnaryOp {
2553                op: TokenType::Not,
2554                operand: universal,
2555            });
2556            Some(self.ctx.exprs.alloc(LogicExpr::Implicature {
2557                assertion: expr,
2558                implicature: negated,
2559            }))
2560        } else {
2561            None
2562        }
2563    }
2564
2565    fn coerce_agent(&mut self, np: &crate::ast::NounPhrase<'a>) -> Term<'a> {
2566        let base = self.noun_phrase_to_term(np);
2567        if crate::lexicon::is_institution_metonym(&self.interner.resolve(np.noun).to_lowercase()) {
2568            let gov = self.interner.intern("GovernmentOf");
2569            Term::Function(gov, self.ctx.terms.alloc_slice([base]))
2570        } else {
2571            base
2572        }
2573    }
2574
2575    fn next_var_name(&mut self) -> Symbol {
2576        const VARS: &[&str] = &["x", "y", "z", "w", "v", "u"];
2577        let idx = self.var_counter;
2578        self.var_counter += 1;
2579        if idx < VARS.len() {
2580            self.interner.intern(VARS[idx])
2581        } else {
2582            let name = format!("x{}", idx - VARS.len() + 1);
2583            self.interner.intern(&name)
2584        }
2585    }
2586
2587    /// Parse an INTEGER numeral lexeme EXACTLY: digit separators
2588    /// (`1_000_000`), radix literals (`0xff` / `0b1010` / `0o755`), and a
2589    /// HARD error when the value cannot fit an Int — never a silent 0.
2590    /// (BigInt literals ride the numeric-tower work; until then an overlong
2591    /// literal fails loudly.)
2592    fn parse_i64_numeral(&self, num_str: &str) -> ParseResult<i64> {
2593        self.parse_i64_numeral_signed(num_str, false)
2594    }
2595
2596    /// The signed core: `negative` attaches the sign to the DIGITS before
2597    /// parsing, so `-9223372036854775808` (i64::MIN, whose magnitude alone
2598    /// overflows by one) parses exactly.
2599    fn parse_i64_numeral_signed(&self, num_str: &str, negative: bool) -> ParseResult<i64> {
2600        let clean = num_str.replace('_', "");
2601        let sign = if negative { "-" } else { "" };
2602        // A RADIX literal (`0x…`/`0b…`/`0o…`) denotes a BIT PATTERN, so it may use the FULL unsigned
2603        // 64-bit range: `0xFFFFFFFFFFFFFFFF` is the all-ones `Word64`, and a SHA-512 round constant like
2604        // `0xb5c0fbcfec4d3b2f` (≥ 2⁶³) is a legitimate 64-bit value. When the signed parse overflows,
2605        // an unsigned value in (i64::MAX, u64::MAX] reinterprets as its two's-complement i64 bits —
2606        // exactly what `word64(…)`/`word32(…)` read back. A DECIMAL literal is magnitude, so it stays
2607        // i64-bound (BigInt magnitudes ride the numeric-tower work) and still fails loudly.
2608        let radix = |digits: &str, base: u32| -> Result<i64, ()> {
2609            if let Ok(v) = i64::from_str_radix(&format!("{}{}", sign, digits), base) {
2610                return Ok(v);
2611            }
2612            if !negative {
2613                if let Ok(u) = u64::from_str_radix(digits, base) {
2614                    return Ok(u as i64);
2615                }
2616            }
2617            Err(())
2618        };
2619        let parsed = if let Some(hex) = clean
2620            .strip_prefix("0x")
2621            .or_else(|| clean.strip_prefix("0X"))
2622        {
2623            radix(hex, 16)
2624        } else if let Some(bin) = clean
2625            .strip_prefix("0b")
2626            .or_else(|| clean.strip_prefix("0B"))
2627        {
2628            radix(bin, 2)
2629        } else if let Some(oct) = clean
2630            .strip_prefix("0o")
2631            .or_else(|| clean.strip_prefix("0O"))
2632        {
2633            radix(oct, 8)
2634        } else {
2635            format!("{}{}", sign, clean).parse::<i64>().map_err(|_| ())
2636        };
2637        parsed.map_err(|_| ParseError {
2638            kind: ParseErrorKind::ExpectedKeyword {
2639                keyword: format!(
2640                    "a representable Int literal — `{}` does not fit the Int range (±9223372036854775807)",
2641                    num_str
2642                ),
2643            },
2644            span: self.current_span(),
2645        })
2646    }
2647
2648    /// True when a numeral lexeme is a RADIX literal (`0x…`/`0b…`/`0o…`) —
2649    /// checked BEFORE the float sniff, because `0x1E` contains an `E` that
2650    /// is a hex digit, not scientific notation.
2651    fn is_radix_numeral(num_str: &str) -> bool {
2652        let s = num_str.strip_prefix('_').unwrap_or(num_str);
2653        s.len() > 2
2654            && s.starts_with('0')
2655            && matches!(s.as_bytes()[1], b'x' | b'X' | b'b' | b'B' | b'o' | b'O')
2656    }
2657
2658    /// Parse the NUMERAL after `item`/`items` into an index literal. An
2659    /// integer numeral is guarded against the 1-based footgun (`item 0` is a
2660    /// clean compile error); a NON-integer numeral (a float map key like
2661    /// `item 1.0 of prices`) is a Float literal — never the silent
2662    /// `unwrap_or(0)` that used to turn it into a phantom `item 0`.
2663    fn parse_index_numeral(&mut self, sym: Symbol) -> ParseResult<&'a Expr<'a>> {
2664        let num_str = self.interner.resolve(sym).to_string();
2665        if let Ok(n) = self.parse_i64_numeral(&num_str) {
2666            if n == 0 {
2667                return Err(ParseError {
2668                    kind: ParseErrorKind::ZeroIndex,
2669                    span: self.current_span(),
2670                });
2671            }
2672            return Ok(self
2673                .ctx
2674                .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Number(n))));
2675        }
2676        if let Ok(f) = num_str.parse::<f64>() {
2677            return Ok(self
2678                .ctx
2679                .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Float(f))));
2680        }
2681        Err(ParseError {
2682            kind: ParseErrorKind::ExpectedKeyword {
2683                keyword: format!("a numeric index (got '{}')", num_str),
2684            },
2685            span: self.current_span(),
2686        })
2687    }
2688
2689    /// A fresh, user-uncollidable temporary for parser desugars ([`Stmt::Splice`]
2690    /// output). Shares `var_counter` so `ParserGuard` checkpoints roll it back.
2691    fn fresh_desugar_temp(&mut self, tag: &str) -> Symbol {
2692        let idx = self.var_counter;
2693        self.var_counter += 1;
2694        self.interner.intern(&format!("__{}_{}", tag, idx))
2695    }
2696
2697    /// Capture a mutation target's item keys from its root outward. The rebuilt
2698    /// place contains only saved keys, so both its read and write-back use the
2699    /// same path without evaluating an ancestor a second time.
2700    fn capture_place_indices(
2701        &mut self,
2702        place: &'a Expr<'a>,
2703        prefix: &mut Vec<Stmt<'a>>,
2704    ) -> &'a Expr<'a> {
2705        match place {
2706            Expr::Identifier(_) => place,
2707            Expr::FieldAccess { object, field } => {
2708                let object = self.capture_place_indices(object, prefix);
2709                self.ctx.alloc_imperative_expr(Expr::FieldAccess {
2710                    object,
2711                    field: *field,
2712                })
2713            }
2714            Expr::Index { collection, index } => {
2715                let collection = self.capture_place_indices(collection, prefix);
2716                let key = self.fresh_desugar_temp("place_key");
2717                prefix.push(Stmt::Let {
2718                    var: key,
2719                    ty: None,
2720                    value: index,
2721                    mutable: false,
2722                });
2723                let index = self.ctx.alloc_imperative_expr(Expr::Identifier(key));
2724                self.ctx
2725                    .alloc_imperative_expr(Expr::Index { collection, index })
2726            }
2727            _ => {
2728                let root = self.fresh_desugar_temp("place_root");
2729                prefix.push(Stmt::Let {
2730                    var: root,
2731                    ty: None,
2732                    value: place,
2733                    mutable: true,
2734                });
2735                self.ctx.alloc_imperative_expr(Expr::Identifier(root))
2736            }
2737        }
2738    }
2739
2740    /// Lower a write into a NESTED index place — `Set item j of (item i of
2741    /// grid) to v` — into read → element write → write-back:
2742    ///
2743    /// ```text
2744    /// Let __place_i be j.  Let __place_v be v.  Let __place_k be i.
2745    /// Let __place_t be item __place_k of grid.
2746    /// Set item __place_i of __place_t to __place_v.
2747    /// Set item __place_k of grid to __place_t.        (recursive for depth)
2748    /// ```
2749    ///
2750    /// The element write targets a plain BINDING, so each engine's own
2751    /// copy-on-write machinery preserves value semantics (an alias of the
2752    /// inner row never observes the write — `diff_let_binding_isolates`); the
2753    /// write-back stores the possibly-fresh inner collection into the outer
2754    /// place. Temp order (index, value, collection) matches the engines'
2755    /// evaluation order for a flat `SetIndex`.
2756    fn desugar_place_set_index(
2757        &mut self,
2758        collection: &'a Expr<'a>,
2759        index: &'a Expr<'a>,
2760        value: &'a Expr<'a>,
2761    ) -> Stmt<'a> {
2762        let (base, key) = match collection {
2763            Expr::Index {
2764                collection: base,
2765                index: key,
2766            } => (*base, *key),
2767            _ => {
2768                return Stmt::SetIndex {
2769                    collection,
2770                    index,
2771                    value,
2772                }
2773            }
2774        };
2775        let t_idx = self.fresh_desugar_temp("place_i");
2776        let t_val = self.fresh_desugar_temp("place_v");
2777        let t_key = self.fresh_desugar_temp("place_k");
2778        let t_inner = self.fresh_desugar_temp("place_t");
2779        let idx_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(t_idx));
2780        let val_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(t_val));
2781        let key_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(t_key));
2782        let inner_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(t_inner));
2783        let mut body = vec![
2784            Stmt::Let {
2785                var: t_idx,
2786                ty: None,
2787                value: index,
2788                mutable: false,
2789            },
2790            Stmt::Let {
2791                var: t_val,
2792                ty: None,
2793                value,
2794                mutable: false,
2795            },
2796        ];
2797        let base = self.capture_place_indices(base, &mut body);
2798        let read = self.ctx.alloc_imperative_expr(Expr::Index {
2799            collection: base,
2800            index: key_ref,
2801        });
2802        let write_back = self.desugar_place_set_index(base, key_ref, inner_ref);
2803        body.extend([
2804            Stmt::Let {
2805                var: t_key,
2806                ty: None,
2807                value: key,
2808                mutable: false,
2809            },
2810            Stmt::Let {
2811                var: t_inner,
2812                ty: None,
2813                value: read,
2814                mutable: true,
2815            },
2816            Stmt::SetIndex {
2817                collection: inner_ref,
2818                index: idx_ref,
2819                value: val_ref,
2820            },
2821            write_back,
2822        ]);
2823        let body = self.alloc_block(body);
2824        Stmt::Splice { body }
2825    }
2826
2827    /// Lower `Push v to <indexed place>` the same way (read → push into the
2828    /// binding → write-back). See [`Self::desugar_place_set_index`].
2829    fn desugar_place_push(&mut self, value: &'a Expr<'a>, collection: &'a Expr<'a>) -> Stmt<'a> {
2830        let (base, key) = match collection {
2831            Expr::Index {
2832                collection: base,
2833                index: key,
2834            } => (*base, *key),
2835            _ => return Stmt::Push { value, collection },
2836        };
2837        let t_val = self.fresh_desugar_temp("place_v");
2838        let t_key = self.fresh_desugar_temp("place_k");
2839        let t_inner = self.fresh_desugar_temp("place_t");
2840        let val_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(t_val));
2841        let key_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(t_key));
2842        let inner_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(t_inner));
2843        let mut body = vec![Stmt::Let {
2844            var: t_val,
2845            ty: None,
2846            value,
2847            mutable: false,
2848        }];
2849        let base = self.capture_place_indices(base, &mut body);
2850        let read = self.ctx.alloc_imperative_expr(Expr::Index {
2851            collection: base,
2852            index: key_ref,
2853        });
2854        let write_back = self.desugar_place_set_index(base, key_ref, inner_ref);
2855        body.extend([
2856            Stmt::Let {
2857                var: t_key,
2858                ty: None,
2859                value: key,
2860                mutable: false,
2861            },
2862            Stmt::Let {
2863                var: t_inner,
2864                ty: None,
2865                value: read,
2866                mutable: true,
2867            },
2868            Stmt::Push {
2869                value: val_ref,
2870                collection: inner_ref,
2871            },
2872            write_back,
2873        ]);
2874        let body = self.alloc_block(body);
2875        Stmt::Splice { body }
2876    }
2877
2878    /// `every <x> in <list> where <condition>` at the value of a `Let` or `Set`: the name, the list
2879    /// and the condition — or `None`, nothing consumed, when the value is anything else.
2880    fn try_parse_every_where(
2881        &mut self,
2882    ) -> ParseResult<Option<(Symbol, &'a Expr<'a>, &'a Expr<'a>)>> {
2883        if !self.check_word("every") {
2884            return Ok(None);
2885        }
2886        let saved = self.backtrack_point();
2887        self.advance(); // every
2888        if !self.check_word_at(self.current + 1, "in") {
2889            self.backtrack_to(&saved);
2890            return Ok(None);
2891        }
2892        let Ok(binder) = self.expect_identifier() else {
2893            self.backtrack_to(&saved);
2894            return Ok(None);
2895        };
2896        self.advance(); // in
2897        let list = self.parse_imperative_expr()?;
2898        self.expect_keyword("where")?;
2899        let condition = self.parse_condition()?;
2900        Ok(Some((binder, list, condition)))
2901    }
2902
2903    /// The items of `list` for which `condition` holds (`keep`) or does not (`!keep`), in order,
2904    /// gathered into a fresh mutable binding that answers them: a copy of `list` — so it has the
2905    /// list's own element kind on every tier, with no type to name — emptied, then refilled by
2906    /// walking `list`. `binder` names each item as the walk's own loop variable, so it belongs to
2907    /// the condition alone — it neither shadows a name in scope nor outlives the statement — and an
2908    /// error the condition raises names it as the author wrote it.
2909    fn desugar_keep_where(
2910        &mut self,
2911        binder: Symbol,
2912        list: &'a Expr<'a>,
2913        condition: &'a Expr<'a>,
2914        keep: bool,
2915    ) -> (Symbol, Vec<Stmt<'a>>) {
2916        let kept = self.fresh_desugar_temp("where_k");
2917        let test = if keep {
2918            condition
2919        } else {
2920            self.ctx
2921                .alloc_imperative_expr(Expr::Not { operand: condition })
2922        };
2923        let kept_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(kept));
2924        let item_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(binder));
2925        let length = self.ctx.alloc_imperative_expr(Expr::Length {
2926            collection: kept_ref,
2927        });
2928        let zero = self
2929            .ctx
2930            .alloc_imperative_expr(Expr::Literal(Literal::Number(0)));
2931        let not_empty = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
2932            op: BinaryOpKind::Gt,
2933            left: length,
2934            right: zero,
2935        });
2936        let pop = self.alloc_block(vec![Stmt::Pop {
2937            collection: kept_ref,
2938            into: None,
2939        }]);
2940        let push = self.alloc_block(vec![Stmt::Push {
2941            value: item_ref,
2942            collection: kept_ref,
2943        }]);
2944        let per_item = self.alloc_block(vec![Stmt::If {
2945            cond: test,
2946            then_block: push,
2947            else_block: None,
2948        }]);
2949        let body = vec![
2950            Stmt::Let {
2951                var: kept,
2952                ty: None,
2953                value: list,
2954                mutable: true,
2955            },
2956            Stmt::While {
2957                cond: not_empty,
2958                body: pop,
2959                decreasing: None,
2960            },
2961            Stmt::Repeat {
2962                pattern: Pattern::Identifier(binder),
2963                iterable: list,
2964                body: per_item,
2965            },
2966        ];
2967        (kept, body)
2968    }
2969
2970    // ---- Tensor sugar (Phase 2) --------------------------------------------------------------------------
2971    // A `Tensor` in LOGOS is a COMPILE-TIME concept: a `(rows, cols)` shape over a flat, row-major backing
2972    // `Seq`. The two sugars below desugar to ordinary `Seq` + loop statements (a transparent `Stmt::Splice`),
2973    // so every execution tier runs them ≡ the VM, and `Let C be A matmul B` emits the exact i-k-j triple-loop
2974    // idiom `recognize_matmul` lifts into the optimized accelerator backends. Shapes are looked up in
2975    // `self.tensor_shapes`; the operands must be tensors declared earlier in the same scope.
2976
2977    #[inline]
2978    fn te_int(&self, n: i64) -> &'a Expr<'a> {
2979        self.ctx
2980            .alloc_imperative_expr(Expr::Literal(Literal::Number(n)))
2981    }
2982    #[inline]
2983    fn te_float(&self, v: f64) -> &'a Expr<'a> {
2984        self.ctx
2985            .alloc_imperative_expr(Expr::Literal(Literal::Float(v)))
2986    }
2987    #[inline]
2988    fn te_call(&self, function: Symbol, args: Vec<&'a Expr<'a>>) -> &'a Expr<'a> {
2989        self.ctx
2990            .alloc_imperative_expr(Expr::Call { function, args })
2991    }
2992    #[inline]
2993    fn te_id(&self, s: Symbol) -> &'a Expr<'a> {
2994        self.ctx.alloc_imperative_expr(Expr::Identifier(s))
2995    }
2996    #[inline]
2997    fn te_bin(&self, op: BinaryOpKind, left: &'a Expr<'a>, right: &'a Expr<'a>) -> &'a Expr<'a> {
2998        self.ctx
2999            .alloc_imperative_expr(Expr::BinaryOp { op, left, right })
3000    }
3001    #[inline]
3002    fn te_index(&self, collection: &'a Expr<'a>, index: &'a Expr<'a>) -> &'a Expr<'a> {
3003        self.ctx
3004            .alloc_imperative_expr(Expr::Index { collection, index })
3005    }
3006    #[inline]
3007    fn te_block(&self, stmts: Vec<Stmt<'a>>) -> &'a [Stmt<'a>] {
3008        self.alloc_block(stmts)
3009    }
3010
3011    /// Read the current token as a positive integer literal, advancing past it; `None` (no advance) otherwise.
3012    fn take_int_literal(&mut self) -> Option<i64> {
3013        if let TokenType::Number(sym) = &self.peek().kind {
3014            let sym = *sym;
3015            let s = self.interner.resolve(sym).to_string();
3016            if let Ok(n) = self.parse_i64_numeral_signed(&s, false) {
3017                self.advance();
3018                return Some(n);
3019            }
3020        }
3021        None
3022    }
3023
3024    /// A norm's optional `with epsilon E` clause after its operands: `E` a positive number, 1e-6 when the clause is
3025    /// absent — the epsilon the norm divides by `sqrt(… + E)` with.
3026    fn take_epsilon(&mut self, norm: &str) -> ParseResult<f64> {
3027        if !(self.check_word("with") && self.check_word_at(self.current + 1, "epsilon")) {
3028            return Ok(1e-6);
3029        }
3030        self.advance();
3031        self.advance();
3032        let value = match self.peek().kind {
3033            TokenType::Number(sym) => self.interner.resolve(sym).replace('_', "").parse::<f64>().ok(),
3034            _ => None,
3035        };
3036        match value {
3037            Some(eps) if eps.is_finite() && eps > 0.0 => {
3038                self.advance();
3039                Ok(eps)
3040            }
3041            _ => Err(ParseError {
3042                kind: ParseErrorKind::Custom(format!(
3043                    "`{norm} … with epsilon E` — E must be a positive number."
3044                )),
3045                span: self.current_span(),
3046            }),
3047        }
3048    }
3049
3050    /// An attention's optional window clause after its attended length: `within a window of W`, the positions a query
3051    /// keeps its own and the `W − 1` before it; or `within chunks of C`, the positions of its own chunk of `C`. `W` and
3052    /// `C` are positive whole numbers.
3053    fn take_window(&mut self) -> ParseResult<Option<Window>> {
3054        if !self.check_word("within") {
3055            return Ok(None);
3056        }
3057        self.advance();
3058        let (words, form): (&[&str], &str) = if self.check_word("chunks") {
3059            (&["chunks", "of"], "within chunks of C")
3060        } else {
3061            (&["a", "window", "of"], "within a window of W")
3062        };
3063        for w in words {
3064            if !self.check_word(w) {
3065                return Err(ParseError {
3066                    kind: ParseErrorKind::Custom(format!("`attention … {form}` — expected `{w}`.")),
3067                    span: self.current_span(),
3068                });
3069            }
3070            self.advance();
3071        }
3072        match self.take_int_literal() {
3073            Some(n) if n > 0 && words[0] == "chunks" => Ok(Some(Window::Chunked(n))),
3074            Some(n) if n > 0 => Ok(Some(Window::Sliding(n))),
3075            _ => Err(ParseError {
3076                kind: ParseErrorKind::Custom(format!(
3077                    "`attention … {form}` — {} must be a positive whole number of positions.",
3078                    &form[form.len() - 1..]
3079                )),
3080                span: self.current_span(),
3081            }),
3082        }
3083    }
3084
3085    /// An attention's optional `with sinks S` clause, last: `S` one row of a logit per query head (`heads` of them), each
3086    /// joining its head's softmax and taking no value.
3087    fn take_sinks(&mut self, heads: i64) -> ParseResult<Option<Symbol>> {
3088        if !(self.check_word("with") && self.check_word_at(self.current + 1, "sinks")) {
3089            return Ok(None);
3090        }
3091        self.advance();
3092        self.advance();
3093        let sym = self.peek().lexeme;
3094        let Some(&(r, c)) = self.tensor_shapes.get(&sym) else {
3095            return Err(ParseError {
3096                kind: ParseErrorKind::Custom(
3097                    "`attention … with sinks S` — S must be a declared tensor of a logit per query head.".to_string(),
3098                ),
3099                span: self.current_span(),
3100            });
3101        };
3102        if (r, c) != (1, heads) {
3103            return Err(ParseError {
3104                kind: ParseErrorKind::Custom(format!(
3105                    "`attention … with sinks S` — S must be 1×{heads} (a logit per query head), and `{}` is {r}×{c}.",
3106                    self.interner.resolve(sym)
3107                )),
3108                span: self.current_span(),
3109            });
3110        }
3111        self.advance();
3112        Ok(Some(sym))
3113    }
3114
3115    /// An attention's optional `scaled by S` clause, after its window: `S` a positive number, the factor every score
3116    /// takes in place of `1/√HD`.
3117    fn take_attention_scale(&mut self) -> ParseResult<Option<f64>> {
3118        if !self.check_word("scaled") {
3119            return Ok(None);
3120        }
3121        self.advance();
3122        if !self.check_word("by") {
3123            return Err(ParseError {
3124                kind: ParseErrorKind::Custom("`attention … scaled by S` — expected `by` after `scaled`.".to_string()),
3125                span: self.current_span(),
3126            });
3127        }
3128        self.advance();
3129        let v = match self.peek().kind {
3130            TokenType::Number(sym) => self.interner.resolve(sym).replace('_', "").parse::<f64>().ok(),
3131            _ => None,
3132        };
3133        match v.filter(|v| v.is_finite() && *v > 0.0) {
3134            Some(v) => {
3135                self.advance();
3136                Ok(Some(v))
3137            }
3138            None => Err(ParseError {
3139                kind: ParseErrorKind::Custom("`attention … scaled by S` — S must be a positive number.".to_string()),
3140                span: self.current_span(),
3141            }),
3142        }
3143    }
3144
3145    /// An optional `capped at C` clause, `C` a positive number written out: after an attention's scale, every score `s`
3146    /// taken as `C·tanh(s/C)` before its softmax; after a tensor, every element. `what` names the sugar in a refusal.
3147    fn take_cap(&mut self, what: &str) -> ParseResult<Option<f64>> {
3148        if !self.check_word("capped") {
3149            return Ok(None);
3150        }
3151        self.advance();
3152        if !self.check_word("at") {
3153            return Err(ParseError {
3154                kind: ParseErrorKind::Custom(format!("`{what} capped at C` — expected `at` after `capped`.")),
3155                span: self.current_span(),
3156            });
3157        }
3158        self.advance();
3159        let v = match self.peek().kind {
3160            TokenType::Number(sym) => self.interner.resolve(sym).replace('_', "").parse::<f64>().ok(),
3161            _ => None,
3162        };
3163        match v.filter(|v| v.is_finite() && *v > 0.0) {
3164            Some(v) => {
3165                self.advance();
3166                Ok(Some(v))
3167            }
3168            None => Err(ParseError {
3169                kind: ParseErrorKind::Custom(format!("`{what} capped at C` — C must be a positive number.")),
3170                span: self.current_span(),
3171            }),
3172        }
3173    }
3174
3175    /// An optional `across H heads` clause, `H` a positive whole number; `what` names the sugar in a refusal.
3176    fn take_across_heads(&mut self, what: &str) -> ParseResult<Option<i64>> {
3177        if !self.check_word("across") {
3178            return Ok(None);
3179        }
3180        self.advance();
3181        let h = self.take_int_literal().filter(|h| *h > 0);
3182        if h.is_none() || !self.check_word("heads") {
3183            return Err(ParseError {
3184                kind: ParseErrorKind::Custom(format!("`{what} across H heads` — H must be a positive whole number of heads.")),
3185                span: self.current_span(),
3186            });
3187        }
3188        self.advance();
3189        Ok(h)
3190    }
3191
3192    /// A mixture's optional `using a swiglu clamped at L with slope A` clause, after `choosing K of E`: the gate clamped
3193    /// to at most `L` and the up to `[−L, L]`, `min(g, L)·σ(A·min(g, L))·(clamp(u) + 1)` — gpt-oss's SwiGLU.
3194    fn take_moe_activation(&mut self) -> ParseResult<MoeAct> {
3195        if !self.check_word("using") {
3196            return Ok(MoeAct::Swiglu);
3197        }
3198        self.advance();
3199        let clause = "`moe … using a swiglu clamped at L with slope A`";
3200        let want = |s: &mut Self, w: &str| -> ParseResult<()> {
3201            if !s.check_word(w) {
3202                return Err(ParseError {
3203                    kind: ParseErrorKind::Custom(format!("{clause} — expected `{w}`.")),
3204                    span: s.current_span(),
3205                });
3206            }
3207            s.advance();
3208            Ok(())
3209        };
3210        let number = |s: &mut Self, what: &str| -> ParseResult<f64> {
3211            let v = match s.peek().kind {
3212                TokenType::Number(sym) => s.interner.resolve(sym).replace('_', "").parse::<f64>().ok(),
3213                _ => None,
3214            };
3215            match v.filter(|v| v.is_finite() && *v > 0.0) {
3216                Some(v) => {
3217                    s.advance();
3218                    Ok(v)
3219                }
3220                None => Err(ParseError {
3221                    kind: ParseErrorKind::Custom(format!("{clause} — {what} must be a positive number.")),
3222                    span: s.current_span(),
3223                }),
3224            }
3225        };
3226        for w in ["a", "swiglu", "clamped", "at"] {
3227            want(self, w)?;
3228        }
3229        let limit = number(self, "L")?;
3230        want(self, "with")?;
3231        want(self, "slope")?;
3232        let slope = number(self, "A")?;
3233        Ok(MoeAct::ClampedSwiglu { limit, slope })
3234    }
3235
3236    /// A fresh zero-filled backing `Seq` of length `rows*cols` bound to `var` (mutable). Emitted as the body
3237    /// of a transparent `Stmt::Splice` so `var` leaks into the enclosing scope for later element writes.
3238    /// Record `var` as a `rows × cols` tensor whose elements are `Int` (`int`) or `Real`. The one writer of
3239    /// `tensor_shapes` and `tensor_int`.
3240    fn bind_tensor(&mut self, var: Symbol, shape: (i64, i64), int: bool) {
3241        self.tensor_shapes.insert(var, shape);
3242        if int {
3243            self.tensor_int.insert(var);
3244        } else {
3245            self.tensor_int.remove(&var);
3246        }
3247    }
3248
3249    /// A fresh `Seq of Int` or `Seq of Real` — a tensor's backing store, in its element kind.
3250    fn new_tensor_seq(&mut self, int: bool) -> &'a Expr<'a> {
3251        let seq_sym = self.interner.intern("Seq");
3252        let elem_sym = self.interner.intern(if int { "Int" } else { "Real" });
3253        self.ctx.alloc_imperative_expr(Expr::New {
3254            type_name: seq_sym,
3255            type_args: vec![TypeExpr::Named(elem_sym)],
3256            init_fields: vec![],
3257        })
3258    }
3259
3260    /// The zero of a tensor's element kind: `0` or `0.0`.
3261    fn tensor_zero(&mut self, int: bool) -> &'a Expr<'a> {
3262        if int {
3263            self.te_int(0)
3264        } else {
3265            self.ctx
3266                .alloc_imperative_expr(Expr::Literal(Literal::Float(0.0)))
3267        }
3268    }
3269
3270    fn desugar_tensor_new(
3271        &mut self,
3272        var: Symbol,
3273        rows: i64,
3274        cols: i64,
3275        is_float: bool,
3276    ) -> Stmt<'a> {
3277        let new_seq = self.new_tensor_seq(!is_float);
3278        let zero = self.tensor_zero(!is_float);
3279        let ctr = self.fresh_desugar_temp("tfill");
3280        let fill_body = self.te_block(vec![
3281            Stmt::Push {
3282                value: zero,
3283                collection: self.te_id(var),
3284            },
3285            Stmt::Set {
3286                target: ctr,
3287                value: self.te_bin(BinaryOpKind::Add, self.te_id(ctr), self.te_int(1)),
3288            },
3289        ]);
3290        let fill_loop = Stmt::While {
3291            cond: self.te_bin(BinaryOpKind::Lt, self.te_id(ctr), self.te_int(rows * cols)),
3292            body: fill_body,
3293            decreasing: None,
3294        };
3295        let body = self.te_block(vec![
3296            Stmt::Let {
3297                var,
3298                ty: None,
3299                value: new_seq,
3300                mutable: true,
3301            },
3302            Stmt::Let {
3303                var: ctr,
3304                ty: None,
3305                value: self.te_int(0),
3306                mutable: true,
3307            },
3308            fill_loop,
3309        ]);
3310        Stmt::Splice { body }
3311    }
3312
3313    /// `Let C be A matmul B` → a zero-filled `m×n` backing `Seq` for `C`, then the i-k-j triple loop
3314    /// `C[i*n+j+1] += A[i*k+kk+1] * B[kk*n+j+1]` (1-based flat, row-major) — verbatim the shape
3315    /// `recognize_matmul` accepts. `m,k,n` come from the operands' recorded shapes; `int` is the result's element
3316    /// kind, which the backing `Seq` is declared in — a typed tier stores exactly what it is told to.
3317    fn desugar_tensor_matmul(
3318        &mut self,
3319        var: Symbol,
3320        a: Symbol,
3321        b: Symbol,
3322        m: i64,
3323        k: i64,
3324        n: i64,
3325        int: bool,
3326    ) -> Stmt<'a> {
3327        let new_seq = self.new_tensor_seq(int);
3328        let zero = self.tensor_zero(int);
3329        let fctr = self.fresh_desugar_temp("cfill");
3330        let (vi, vk, vj) = (
3331            self.fresh_desugar_temp("mm_i"),
3332            self.fresh_desugar_temp("mm_k"),
3333            self.fresh_desugar_temp("mm_j"),
3334        );
3335        // zero-fill C with m*n entries
3336        let fill_body = self.te_block(vec![
3337            Stmt::Push {
3338                value: zero,
3339                collection: self.te_id(var),
3340            },
3341            Stmt::Set {
3342                target: fctr,
3343                value: self.te_bin(BinaryOpKind::Add, self.te_id(fctr), self.te_int(1)),
3344            },
3345        ]);
3346        let fill_loop = Stmt::While {
3347            cond: self.te_bin(BinaryOpKind::Lt, self.te_id(fctr), self.te_int(m * n)),
3348            body: fill_body,
3349            decreasing: None,
3350        };
3351        // 1-based flat indices, row-major
3352        let cidx = self.te_bin(
3353            BinaryOpKind::Add,
3354            self.te_bin(
3355                BinaryOpKind::Add,
3356                self.te_bin(BinaryOpKind::Multiply, self.te_id(vi), self.te_int(n)),
3357                self.te_id(vj),
3358            ),
3359            self.te_int(1),
3360        );
3361        let aidx = self.te_bin(
3362            BinaryOpKind::Add,
3363            self.te_bin(
3364                BinaryOpKind::Add,
3365                self.te_bin(BinaryOpKind::Multiply, self.te_id(vi), self.te_int(k)),
3366                self.te_id(vk),
3367            ),
3368            self.te_int(1),
3369        );
3370        let bidx = self.te_bin(
3371            BinaryOpKind::Add,
3372            self.te_bin(
3373                BinaryOpKind::Add,
3374                self.te_bin(BinaryOpKind::Multiply, self.te_id(vk), self.te_int(n)),
3375                self.te_id(vj),
3376            ),
3377            self.te_int(1),
3378        );
3379        let prod = self.te_bin(
3380            BinaryOpKind::Multiply,
3381            self.te_index(self.te_id(a), aidx),
3382            self.te_index(self.te_id(b), bidx),
3383        );
3384        let acc = self.te_bin(
3385            BinaryOpKind::Add,
3386            self.te_index(self.te_id(var), cidx),
3387            prod,
3388        );
3389        let set_c = Stmt::SetIndex {
3390            collection: self.te_id(var),
3391            index: cidx,
3392            value: acc,
3393        };
3394        // innermost j-loop
3395        let j_body = self.te_block(vec![
3396            set_c,
3397            Stmt::Set {
3398                target: vj,
3399                value: self.te_bin(BinaryOpKind::Add, self.te_id(vj), self.te_int(1)),
3400            },
3401        ]);
3402        let j_loop = Stmt::While {
3403            cond: self.te_bin(BinaryOpKind::Lt, self.te_id(vj), self.te_int(n)),
3404            body: j_body,
3405            decreasing: None,
3406        };
3407        // k-loop: reset j, run j-loop, k++
3408        let k_body = self.te_block(vec![
3409            Stmt::Let {
3410                var: vj,
3411                ty: None,
3412                value: self.te_int(0),
3413                mutable: true,
3414            },
3415            j_loop,
3416            Stmt::Set {
3417                target: vk,
3418                value: self.te_bin(BinaryOpKind::Add, self.te_id(vk), self.te_int(1)),
3419            },
3420        ]);
3421        let k_loop = Stmt::While {
3422            cond: self.te_bin(BinaryOpKind::Lt, self.te_id(vk), self.te_int(k)),
3423            body: k_body,
3424            decreasing: None,
3425        };
3426        // i-loop: reset k, run k-loop, i++
3427        let i_body = self.te_block(vec![
3428            Stmt::Let {
3429                var: vk,
3430                ty: None,
3431                value: self.te_int(0),
3432                mutable: true,
3433            },
3434            k_loop,
3435            Stmt::Set {
3436                target: vi,
3437                value: self.te_bin(BinaryOpKind::Add, self.te_id(vi), self.te_int(1)),
3438            },
3439        ]);
3440        let i_loop = Stmt::While {
3441            cond: self.te_bin(BinaryOpKind::Lt, self.te_id(vi), self.te_int(m)),
3442            body: i_body,
3443            decreasing: None,
3444        };
3445        let body = self.te_block(vec![
3446            Stmt::Let {
3447                var,
3448                ty: None,
3449                value: new_seq,
3450                mutable: true,
3451            },
3452            Stmt::Let {
3453                var: fctr,
3454                ty: None,
3455                value: self.te_int(0),
3456                mutable: true,
3457            },
3458            fill_loop,
3459            Stmt::Let {
3460                var: vi,
3461                ty: None,
3462                value: self.te_int(0),
3463                mutable: true,
3464            },
3465            i_loop,
3466        ]);
3467        Stmt::Splice { body }
3468    }
3469
3470    /// `Let C be A plus B` / `Let C be A hadamard B` → a fresh backing `Seq` filled by ONE element loop
3471    /// `C[p] = A[p] ⊕ B[p]` (1-based, `p` in `1..=total`). The operands share the shape, or `B` is one `row` as wide
3472    /// as A's rows and is read at each element's column (checked by the caller), so a single flat pass suffices — no
3473    /// nesting. `op` is `Add` or `Multiply`.
3474    fn desugar_tensor_elementwise(
3475        &mut self,
3476        var: Symbol,
3477        a: Symbol,
3478        b: Symbol,
3479        total: i64,
3480        op: BinaryOpKind,
3481        int: bool,
3482        row: Option<i64>,
3483    ) -> Stmt<'a> {
3484        let new_seq = self.new_tensor_seq(int);
3485        let p = self.fresh_desugar_temp("ew_p");
3486        // A row `B` is read at the column element `p` falls in: `((p − 1) mod W) + 1`.
3487        let b_at = match row {
3488            Some(w) => self.te_bin(
3489                BinaryOpKind::Add,
3490                self.te_bin(
3491                    BinaryOpKind::Modulo,
3492                    self.te_bin(BinaryOpKind::Subtract, self.te_id(p), self.te_int(1)),
3493                    self.te_int(w),
3494                ),
3495                self.te_int(1),
3496            ),
3497            None => self.te_id(p),
3498        };
3499        let elem = self.te_bin(
3500            op,
3501            self.te_index(self.te_id(a), self.te_id(p)),
3502            self.te_index(self.te_id(b), b_at),
3503        );
3504        let loop_body = self.te_block(vec![
3505            Stmt::Push {
3506                value: elem,
3507                collection: self.te_id(var),
3508            },
3509            Stmt::Set {
3510                target: p,
3511                value: self.te_bin(BinaryOpKind::Add, self.te_id(p), self.te_int(1)),
3512            },
3513        ]);
3514        let loop_stmt = Stmt::While {
3515            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p), self.te_int(total)),
3516            body: loop_body,
3517            decreasing: None,
3518        };
3519        let body = self.te_block(vec![
3520            Stmt::Let {
3521                var,
3522                ty: None,
3523                value: new_seq,
3524                mutable: true,
3525            },
3526            Stmt::Let {
3527                var: p,
3528                ty: None,
3529                value: self.te_int(1),
3530                mutable: true,
3531            },
3532            loop_stmt,
3533        ]);
3534        Stmt::Splice { body }
3535    }
3536
3537    /// `Let Y be X scaled by C` → `Y[p] = X[p] · C` into a fresh `Seq of Real`, one loop.
3538    fn desugar_tensor_scale(&mut self, var: Symbol, x: Symbol, total: i64, factor: f64) -> Stmt<'a> {
3539        let new_seq = self.new_tensor_seq(false);
3540        let p = self.fresh_desugar_temp("sc_p");
3541        let elem = self.te_bin(BinaryOpKind::Multiply, self.te_index(self.te_id(x), self.te_id(p)), self.te_float(factor));
3542        let loop_body = self.te_block(vec![
3543            Stmt::Push {
3544                value: elem,
3545                collection: self.te_id(var),
3546            },
3547            Stmt::Set {
3548                target: p,
3549                value: self.te_bin(BinaryOpKind::Add, self.te_id(p), self.te_int(1)),
3550            },
3551        ]);
3552        let body = self.te_block(vec![
3553            Stmt::Let {
3554                var,
3555                ty: None,
3556                value: new_seq,
3557                mutable: true,
3558            },
3559            Stmt::Let {
3560                var: p,
3561                ty: None,
3562                value: self.te_int(1),
3563                mutable: true,
3564            },
3565            Stmt::While {
3566                cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p), self.te_int(total)),
3567                body: loop_body,
3568                decreasing: None,
3569            },
3570        ]);
3571        Stmt::Splice { body }
3572    }
3573
3574    /// `x` soft-capped at `c`: `c·tanh(x/c)` as `c·(1 − 2/(e^(x·(2/c)) + 1))`, one exponential that reaches `±c` in
3575    /// both tails (`e^(+∞)` makes the fraction 0, `e^(−∞)` makes it 2) where `(t − 1)/(t + 1)` would be `∞/∞`.
3576    fn capped(&self, x: &'a Expr<'a>, c: f64) -> &'a Expr<'a> {
3577        let e = self.te_bin(BinaryOpKind::Pow, self.te_float(std::f64::consts::E), self.te_bin(BinaryOpKind::Multiply, x, self.te_float(2.0 / c)));
3578        let fraction = self.te_bin(BinaryOpKind::Divide, self.te_float(2.0), self.te_bin(BinaryOpKind::Add, e, self.te_float(1.0)));
3579        self.te_bin(BinaryOpKind::Multiply, self.te_float(c), self.te_bin(BinaryOpKind::Subtract, self.te_float(1.0), fraction))
3580    }
3581
3582    /// `Let Y be X capped at C` → `Y ← C·tanh(X[p]/C)` for every element, in [`Self::capped`]'s form.
3583    fn desugar_tensor_cap(&mut self, var: Symbol, x: Symbol, total: i64, cap: f64) -> Stmt<'a> {
3584        let new_seq = self.new_tensor_seq(false);
3585        let p = self.fresh_desugar_temp("cp_p");
3586        let elem = self.capped(self.te_index(self.te_id(x), self.te_id(p)), cap);
3587        let loop_body = self.te_block(vec![
3588            Stmt::Push { value: elem, collection: self.te_id(var) },
3589            Stmt::Set { target: p, value: self.te_bin(BinaryOpKind::Add, self.te_id(p), self.te_int(1)) },
3590        ]);
3591        let body = self.te_block(vec![
3592            Stmt::Let { var, ty: None, value: new_seq, mutable: true },
3593            Stmt::Let { var: p, ty: None, value: self.te_int(1), mutable: true },
3594            Stmt::While {
3595                cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p), self.te_int(total)),
3596                body: loop_body,
3597                decreasing: None,
3598            },
3599        ]);
3600        Stmt::Splice { body }
3601    }
3602
3603    /// `Let iv be 1. While iv ≤ width: Push <read(iv)> to var. Set iv to iv + 1.` — one part's copy, the shape every
3604    /// head slice and join pushes its parts with.
3605    fn part_copy(&mut self, var: Symbol, width: i64, read: impl Fn(&Self, Symbol) -> &'a Expr<'a>) -> [Stmt<'a>; 2] {
3606        let iv = self.fresh_desugar_temp("pc_i");
3607        let body = self.te_block(vec![
3608            Stmt::Push { value: read(self, iv), collection: self.te_id(var) },
3609            Stmt::Set { target: iv, value: self.te_bin(BinaryOpKind::Add, self.te_id(iv), self.te_int(1)) },
3610        ]);
3611        [
3612            Stmt::Let { var: iv, ty: None, value: self.te_int(1), mutable: true },
3613            Stmt::While {
3614                cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(iv), self.te_int(width)),
3615                body,
3616                decreasing: None,
3617            },
3618        ]
3619    }
3620
3621    /// `Let iv be 0. While iv < bound: <body>. Set iv to iv + 1.` — a zero-based counted loop.
3622    fn counted(&self, iv: Symbol, bound: i64, mut body: Vec<Stmt<'a>>) -> [Stmt<'a>; 2] {
3623        body.push(Stmt::Set { target: iv, value: self.te_bin(BinaryOpKind::Add, self.te_id(iv), self.te_int(1)) });
3624        [
3625            Stmt::Let { var: iv, ty: None, value: self.te_int(0), mutable: true },
3626            Stmt::While {
3627                cond: self.te_bin(BinaryOpKind::Lt, self.te_id(iv), self.te_int(bound)),
3628                body: self.te_block(body),
3629                decreasing: None,
3630            },
3631        ]
3632    }
3633
3634    /// `Let P be the first K of every N of X` / `the last K of every N of X` → for each of the `groups` groups `g` of `N`
3635    /// consecutive elements of `X`, push its `K` elements from offset `from`: `P ← X[(g·N + i) + from]`, `i` in `1..=K`.
3636    fn desugar_tensor_head_slice(
3637        &mut self,
3638        var: Symbol,
3639        x: Symbol,
3640        groups: i64,
3641        every: i64,
3642        from: i64,
3643        width: i64,
3644        int: bool,
3645    ) -> Stmt<'a> {
3646        let new_seq = self.new_tensor_seq(int);
3647        let g = self.fresh_desugar_temp("hs_g");
3648        let copy = self.part_copy(var, width, |s, iv| {
3649            let at = s.te_bin(BinaryOpKind::Add, s.te_bin(BinaryOpKind::Multiply, s.te_id(g), s.te_int(every)), s.te_id(iv));
3650            s.te_index(s.te_id(x), s.te_bin(BinaryOpKind::Add, at, s.te_int(from)))
3651        });
3652        let mut stmts = vec![Stmt::Let { var, ty: None, value: new_seq, mutable: true }];
3653        stmts.extend(self.counted(g, groups, Vec::from(copy)));
3654        Stmt::Splice { body: self.te_block(stmts) }
3655    }
3656
3657    /// `Let J be A joined with B across H heads` → for each row `r` and head `h`, push the head's `ha` elements of `A`
3658    /// then its `hb` elements of `B`: `J ← A[(r·(H·ha) + h·ha) + i]`, then `J ← B[(r·(H·hb) + h·hb) + i]`. One head is the
3659    /// join of the rows.
3660    #[allow(clippy::too_many_arguments)]
3661    fn desugar_tensor_join(
3662        &mut self,
3663        var: Symbol,
3664        a: Symbol,
3665        b: Symbol,
3666        rows: i64,
3667        heads: i64,
3668        ha: i64,
3669        hb: i64,
3670        int: bool,
3671    ) -> Stmt<'a> {
3672        let new_seq = self.new_tensor_seq(int);
3673        let (r, h) = (self.fresh_desugar_temp("jn_r"), self.fresh_desugar_temp("jn_h"));
3674        let part = |s: &Self, t: Symbol, width: i64, iv: Symbol| {
3675            let row = s.te_bin(BinaryOpKind::Multiply, s.te_id(r), s.te_int(heads * width));
3676            let head = s.te_bin(BinaryOpKind::Multiply, s.te_id(h), s.te_int(width));
3677            s.te_index(s.te_id(t), s.te_bin(BinaryOpKind::Add, s.te_bin(BinaryOpKind::Add, row, head), s.te_id(iv)))
3678        };
3679        let mut per_head = Vec::from(self.part_copy(var, ha, |s, iv| part(s, a, ha, iv)));
3680        per_head.extend(self.part_copy(var, hb, |s, iv| part(s, b, hb, iv)));
3681        let per_row = Vec::from(self.counted(h, heads, per_head));
3682        let mut stmts = vec![Stmt::Let { var, ty: None, value: new_seq, mutable: true }];
3683        stmts.extend(self.counted(r, rows, per_row));
3684        Stmt::Splice { body: self.te_block(stmts) }
3685    }
3686
3687    /// `Let Y be X matmul W across H heads` → a zero-filled `m × (H·b)` result, then the matmul's nest with a head loop
3688    /// between the row loop and the contraction:
3689    /// `Y[t·(H·b) + h·b + r + 1] += X[t·(H·a) + h·a + e + 1] · W[(h·a + e)·b + r + 1]` — the loops `t`, `h`, `e`, `r` over
3690    /// `m`, `H`, `a`, `b`, the shape `recognize_head_matmul` accepts.
3691    #[allow(clippy::too_many_arguments)]
3692    fn desugar_tensor_head_matmul(
3693        &mut self,
3694        var: Symbol,
3695        x: Symbol,
3696        w: Symbol,
3697        m: i64,
3698        heads: i64,
3699        a: i64,
3700        b: i64,
3701        int: bool,
3702    ) -> Stmt<'a> {
3703        let new_seq = self.new_tensor_seq(int);
3704        let zero = self.tensor_zero(int);
3705        let fill = self.fresh_desugar_temp("hm_fill");
3706        let (t, h, e, r) = (
3707            self.fresh_desugar_temp("hm_t"),
3708            self.fresh_desugar_temp("hm_h"),
3709            self.fresh_desugar_temp("hm_e"),
3710            self.fresh_desugar_temp("hm_r"),
3711        );
3712        let id = |s: Symbol| self.te_id(s);
3713        let it = |n: i64| self.te_int(n);
3714        let add = |l: &'a Expr<'a>, r: &'a Expr<'a>| self.te_bin(BinaryOpKind::Add, l, r);
3715        let mul = |l: &'a Expr<'a>, r: &'a Expr<'a>| self.te_bin(BinaryOpKind::Multiply, l, r);
3716        let y_at = add(add(add(mul(id(t), it(heads * b)), mul(id(h), it(b))), id(r)), it(1));
3717        let x_at = add(add(add(mul(id(t), it(heads * a)), mul(id(h), it(a))), id(e)), it(1));
3718        let w_at = add(add(mul(add(mul(id(h), it(a)), id(e)), it(b)), id(r)), it(1));
3719        let acc = add(
3720            self.te_index(id(var), y_at),
3721            mul(self.te_index(id(x), x_at), self.te_index(id(w), w_at)),
3722        );
3723        let set = Stmt::SetIndex { collection: id(var), index: y_at, value: acc };
3724        let per_r = Vec::from(self.counted(r, b, vec![set]));
3725        let per_e = Vec::from(self.counted(e, a, per_r));
3726        let per_h = Vec::from(self.counted(h, heads, per_e));
3727        let nest = self.counted(t, m, per_h);
3728        let filled = self.counted(fill, m * heads * b, vec![Stmt::Push { value: zero, collection: id(var) }]);
3729        let mut stmts = vec![Stmt::Let { var, ty: None, value: new_seq, mutable: true }];
3730        stmts.extend(filled);
3731        stmts.extend(nest);
3732        Stmt::Splice { body: self.te_block(stmts) }
3733    }
3734
3735    /// `Y[p] = map(X[p])` for `p` in `1..=total`, into a fresh backing `Seq` of the result's element kind:
3736    /// `(X[p] * m) shifted right by s` for a requant, `min(max(X[p], lo), hi)` for a clamp, `max(X[p], 0)` for a
3737    /// ReLU. One flat loop with one push — the shape `recognize_elem_map` lifts.
3738    fn desugar_tensor_map(
3739        &mut self,
3740        var: Symbol,
3741        x: Symbol,
3742        total: i64,
3743        map: TensorElemMap,
3744        int: bool,
3745    ) -> Stmt<'a> {
3746        let new_seq = self.new_tensor_seq(int);
3747        let p = self.fresh_desugar_temp("em_p");
3748        let xp = self.te_index(self.te_id(x), self.te_id(p));
3749        let elem = match map {
3750            TensorElemMap::Requant { m, s } => self.te_bin(
3751                BinaryOpKind::Shr,
3752                self.te_bin(BinaryOpKind::Multiply, xp, self.te_int(m)),
3753                self.te_int(s),
3754            ),
3755            TensorElemMap::Clamp { lo, hi } => {
3756                let (min_sym, max_sym) = (self.interner.intern("min"), self.interner.intern("max"));
3757                let lower = self.te_call(max_sym, vec![xp, self.te_int(lo)]);
3758                self.te_call(min_sym, vec![lower, self.te_int(hi)])
3759            }
3760            TensorElemMap::Relu => {
3761                let max_sym = self.interner.intern("max");
3762                let zero = self.tensor_zero(int);
3763                self.te_call(max_sym, vec![xp, zero])
3764            }
3765        };
3766        let loop_body = self.te_block(vec![
3767            Stmt::Push {
3768                value: elem,
3769                collection: self.te_id(var),
3770            },
3771            Stmt::Set {
3772                target: p,
3773                value: self.te_bin(BinaryOpKind::Add, self.te_id(p), self.te_int(1)),
3774            },
3775        ]);
3776        let loop_stmt = Stmt::While {
3777            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p), self.te_int(total)),
3778            body: loop_body,
3779            decreasing: None,
3780        };
3781        let body = self.te_block(vec![
3782            Stmt::Let {
3783                var,
3784                ty: None,
3785                value: new_seq,
3786                mutable: true,
3787            },
3788            Stmt::Let {
3789                var: p,
3790                ty: None,
3791                value: self.te_int(1),
3792                mutable: true,
3793            },
3794            loop_stmt,
3795        ]);
3796        Stmt::Splice { body }
3797    }
3798
3799    /// `Let Y be rmsnorm of X` → `Y[i] = X[i] / sqrt(mean(X²) + eps)` (`eps` the program's, 1e-6 by default). Desugars
3800    /// to a sum-of-squares loop, a `sqrt` of the mean, and a normalize loop into a fresh `Seq of Real`.
3801    fn desugar_tensor_rmsnorm(&mut self, var: Symbol, x: Symbol, total: i64, eps: f64) -> Stmt<'a> {
3802        let sqrt_sym = self.interner.intern("sqrt");
3803        let seq_sym = self.interner.intern("Seq");
3804        let real_sym = self.interner.intern("Real");
3805        let ss = self.fresh_desugar_temp("rn_ss");
3806        let p = self.fresh_desugar_temp("rn_p");
3807        let denom = self.fresh_desugar_temp("rn_d");
3808        let q = self.fresh_desugar_temp("rn_q");
3809        // Σ X[p]²
3810        let sq = self.te_bin(
3811            BinaryOpKind::Multiply,
3812            self.te_index(self.te_id(x), self.te_id(p)),
3813            self.te_index(self.te_id(x), self.te_id(p)),
3814        );
3815        let ss_body = self.te_block(vec![
3816            Stmt::Set {
3817                target: ss,
3818                value: self.te_bin(BinaryOpKind::Add, self.te_id(ss), sq),
3819            },
3820            Stmt::Set {
3821                target: p,
3822                value: self.te_bin(BinaryOpKind::Add, self.te_id(p), self.te_int(1)),
3823            },
3824        ]);
3825        let ss_loop = Stmt::While {
3826            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p), self.te_int(total)),
3827            body: ss_body,
3828            decreasing: None,
3829        };
3830        // denom = sqrt(ss / total + eps)
3831        let mean = self.te_bin(
3832            BinaryOpKind::Divide,
3833            self.te_id(ss),
3834            self.te_float(total as f64),
3835        );
3836        let mean_eps = self.te_bin(BinaryOpKind::Add, mean, self.te_float(eps));
3837        let denom_expr = self.te_call(sqrt_sym, vec![mean_eps]);
3838        let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
3839            type_name: seq_sym,
3840            type_args: vec![TypeExpr::Named(real_sym)],
3841            init_fields: vec![],
3842        });
3843        // Y[q] = X[q] / denom
3844        let norm = self.te_bin(
3845            BinaryOpKind::Divide,
3846            self.te_index(self.te_id(x), self.te_id(q)),
3847            self.te_id(denom),
3848        );
3849        let n_body = self.te_block(vec![
3850            Stmt::Push {
3851                value: norm,
3852                collection: self.te_id(var),
3853            },
3854            Stmt::Set {
3855                target: q,
3856                value: self.te_bin(BinaryOpKind::Add, self.te_id(q), self.te_int(1)),
3857            },
3858        ]);
3859        let n_loop = Stmt::While {
3860            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(q), self.te_int(total)),
3861            body: n_body,
3862            decreasing: None,
3863        };
3864        let body = self.te_block(vec![
3865            Stmt::Let {
3866                var: ss,
3867                ty: None,
3868                value: self.te_float(0.0),
3869                mutable: true,
3870            },
3871            Stmt::Let {
3872                var: p,
3873                ty: None,
3874                value: self.te_int(1),
3875                mutable: true,
3876            },
3877            ss_loop,
3878            Stmt::Let {
3879                var: denom,
3880                ty: None,
3881                value: denom_expr,
3882                mutable: false,
3883            },
3884            Stmt::Let {
3885                var,
3886                ty: None,
3887                value: new_seq,
3888                mutable: true,
3889            },
3890            Stmt::Let {
3891                var: q,
3892                ty: None,
3893                value: self.te_int(1),
3894                mutable: true,
3895            },
3896            n_loop,
3897        ]);
3898        Stmt::Splice { body }
3899    }
3900
3901    /// `Let Y be rmsnorm of X [plus R] with weight W` → PER-ROW (optionally fused-add) weighted RMSNorm over an
3902    /// `R by C` tensor `X`, an optional same-shape residual `R`, and a length-`C` weight `W`:
3903    /// `Y[r,i] = (X[r,i] + R[r,i]) / sqrt(mean_i((X[r,·]+R[r,·])²) + eps) * W[i]` (`eps` the program's, 1e-6 by default;
3904    /// residual omitted when `None`) — the FIB `001_fused_add_rmsnorm` shape. Desugars to a row loop with a per-row
3905    /// sum-of-squares reduction and a weighted normalize loop into a fresh `Seq of Real` (row-major). With
3906    /// `weight_rows` above 1 the weight holds that many rows of `C`, and row `r` takes its row `(r − 1) mod weight_rows`
3907    /// — the lanes of a hyper-connected residual, each normed by its own weight (`in L lanes`).
3908    #[allow(clippy::too_many_arguments)]
3909    fn desugar_tensor_rmsnorm_weighted(
3910        &mut self,
3911        var: Symbol,
3912        x: Symbol,
3913        residual: Option<Symbol>,
3914        weight: Symbol,
3915        rows: i64,
3916        cols: i64,
3917        eps: f64,
3918        weight_rows: i64,
3919    ) -> Stmt<'a> {
3920        let sqrt_sym = self.interner.intern("sqrt");
3921        let seq_sym = self.interner.intern("Seq");
3922        let real_sym = self.interner.intern("Real");
3923        let r = self.fresh_desugar_temp("wr_r");
3924        let ss = self.fresh_desugar_temp("wr_ss");
3925        let i = self.fresh_desugar_temp("wr_i");
3926        let denom = self.fresh_desugar_temp("wr_d");
3927        let j = self.fresh_desugar_temp("wr_j");
3928        // idx(inner) = (r-1)*cols + inner — the 1-based row-major flat index of X[row r, column `inner`].
3929        let idx = |s: &Self, inner: Symbol| {
3930            s.te_bin(
3931                BinaryOpKind::Add,
3932                s.te_bin(
3933                    BinaryOpKind::Multiply,
3934                    s.te_bin(BinaryOpKind::Subtract, s.te_id(r), s.te_int(1)),
3935                    s.te_int(cols),
3936                ),
3937                s.te_id(inner),
3938            )
3939        };
3940        // elem(inner) = X[idx]  (plain)  OR  X[idx] + R[idx]  (fused residual add).
3941        let elem = |s: &Self, inner: Symbol| {
3942            let xe = s.te_index(s.te_id(x), idx(s, inner));
3943            match residual {
3944                Some(rz) => s.te_bin(
3945                    BinaryOpKind::Add,
3946                    xe,
3947                    s.te_index(s.te_id(rz), idx(s, inner)),
3948                ),
3949                None => xe,
3950            }
3951        };
3952        // inner: Σ elem² over columns
3953        let xi = elem(self, i);
3954        let xi2 = elem(self, i);
3955        let ss_body = self.te_block(vec![
3956            Stmt::Set {
3957                target: ss,
3958                value: self.te_bin(
3959                    BinaryOpKind::Add,
3960                    self.te_id(ss),
3961                    self.te_bin(BinaryOpKind::Multiply, xi, xi2),
3962                ),
3963            },
3964            Stmt::Set {
3965                target: i,
3966                value: self.te_bin(BinaryOpKind::Add, self.te_id(i), self.te_int(1)),
3967            },
3968        ]);
3969        let ss_loop = Stmt::While {
3970            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(i), self.te_int(cols)),
3971            body: ss_body,
3972            decreasing: None,
3973        };
3974        // denom = sqrt(ss/cols + eps)
3975        let mean = self.te_bin(
3976            BinaryOpKind::Divide,
3977            self.te_id(ss),
3978            self.te_float(cols as f64),
3979        );
3980        let mean_eps = self.te_bin(BinaryOpKind::Add, mean, self.te_float(eps));
3981        let denom_expr = self.te_call(sqrt_sym, vec![mean_eps]);
3982        // inner: Push (elem / denom) * W[j], or W[((r − 1) mod weight_rows)·cols + j] for a weight of several rows
3983        let xj = elem(self, j);
3984        let normed = self.te_bin(BinaryOpKind::Divide, xj, self.te_id(denom));
3985        let w_at = if weight_rows > 1 {
3986            self.te_bin(
3987                BinaryOpKind::Add,
3988                self.te_bin(
3989                    BinaryOpKind::Multiply,
3990                    self.te_bin(
3991                        BinaryOpKind::Modulo,
3992                        self.te_bin(BinaryOpKind::Subtract, self.te_id(r), self.te_int(1)),
3993                        self.te_int(weight_rows),
3994                    ),
3995                    self.te_int(cols),
3996                ),
3997                self.te_id(j),
3998            )
3999        } else {
4000            self.te_id(j)
4001        };
4002        let weighted = self.te_bin(
4003            BinaryOpKind::Multiply,
4004            normed,
4005            self.te_index(self.te_id(weight), w_at),
4006        );
4007        let n_body = self.te_block(vec![
4008            Stmt::Push {
4009                value: weighted,
4010                collection: self.te_id(var),
4011            },
4012            Stmt::Set {
4013                target: j,
4014                value: self.te_bin(BinaryOpKind::Add, self.te_id(j), self.te_int(1)),
4015            },
4016        ]);
4017        let n_loop = Stmt::While {
4018            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(j), self.te_int(cols)),
4019            body: n_body,
4020            decreasing: None,
4021        };
4022        // per-row body: reset accumulators, reduce, compute denom, weighted-normalize
4023        let row_body = self.te_block(vec![
4024            Stmt::Set {
4025                target: ss,
4026                value: self.te_float(0.0),
4027            },
4028            Stmt::Set {
4029                target: i,
4030                value: self.te_int(1),
4031            },
4032            ss_loop,
4033            Stmt::Set {
4034                target: denom,
4035                value: denom_expr,
4036            },
4037            Stmt::Set {
4038                target: j,
4039                value: self.te_int(1),
4040            },
4041            n_loop,
4042            Stmt::Set {
4043                target: r,
4044                value: self.te_bin(BinaryOpKind::Add, self.te_id(r), self.te_int(1)),
4045            },
4046        ]);
4047        let row_loop = Stmt::While {
4048            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(r), self.te_int(rows)),
4049            body: row_body,
4050            decreasing: None,
4051        };
4052        let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
4053            type_name: seq_sym,
4054            type_args: vec![TypeExpr::Named(real_sym)],
4055            init_fields: vec![],
4056        });
4057        let body = self.te_block(vec![
4058            Stmt::Let {
4059                var,
4060                ty: None,
4061                value: new_seq,
4062                mutable: true,
4063            },
4064            Stmt::Let {
4065                var: r,
4066                ty: None,
4067                value: self.te_int(1),
4068                mutable: true,
4069            },
4070            Stmt::Let {
4071                var: ss,
4072                ty: None,
4073                value: self.te_float(0.0),
4074                mutable: true,
4075            },
4076            Stmt::Let {
4077                var: i,
4078                ty: None,
4079                value: self.te_int(1),
4080                mutable: true,
4081            },
4082            Stmt::Let {
4083                var: denom,
4084                ty: None,
4085                value: self.te_float(0.0),
4086                mutable: true,
4087            },
4088            Stmt::Let {
4089                var: j,
4090                ty: None,
4091                value: self.te_int(1),
4092                mutable: true,
4093            },
4094            row_loop,
4095        ]);
4096        Stmt::Splice { body }
4097    }
4098
4099    /// `Let Y be layernorm of X with weight W and bias B` → PER-ROW LayerNorm over an `R by C` tensor `X` with
4100    /// length-`C` `W`/`B`: `Y[r,i] = (X[r,i] - mean_i) / sqrt(var_i + eps) * W[i] + B[i]` (`eps` the program's, 1e-6 by
4101    /// default), for each
4102    /// row. Desugars to a two-pass per-row reduction (mean, then variance) and a scale+shift normalize loop.
4103    fn desugar_tensor_layernorm(
4104        &mut self,
4105        var: Symbol,
4106        x: Symbol,
4107        weight: Symbol,
4108        bias: Symbol,
4109        rows: i64,
4110        cols: i64,
4111        eps: f64,
4112    ) -> Stmt<'a> {
4113        let sqrt_sym = self.interner.intern("sqrt");
4114        let seq_sym = self.interner.intern("Seq");
4115        let real_sym = self.interner.intern("Real");
4116        let r = self.fresh_desugar_temp("ln_r");
4117        let sm = self.fresh_desugar_temp("ln_sm");
4118        let mean = self.fresh_desugar_temp("ln_mean");
4119        let vs = self.fresh_desugar_temp("ln_vs");
4120        let denom = self.fresh_desugar_temp("ln_d");
4121        let i = self.fresh_desugar_temp("ln_i");
4122        let j = self.fresh_desugar_temp("ln_j");
4123        let idx = |s: &Self, inner: Symbol| {
4124            s.te_bin(
4125                BinaryOpKind::Add,
4126                s.te_bin(
4127                    BinaryOpKind::Multiply,
4128                    s.te_bin(BinaryOpKind::Subtract, s.te_id(r), s.te_int(1)),
4129                    s.te_int(cols),
4130                ),
4131                s.te_id(inner),
4132            )
4133        };
4134        // pass 1: sm += X[idx(i)]  →  mean = sm / cols
4135        let sum_body = self.te_block(vec![
4136            Stmt::Set {
4137                target: sm,
4138                value: self.te_bin(
4139                    BinaryOpKind::Add,
4140                    self.te_id(sm),
4141                    self.te_index(self.te_id(x), idx(self, i)),
4142                ),
4143            },
4144            Stmt::Set {
4145                target: i,
4146                value: self.te_bin(BinaryOpKind::Add, self.te_id(i), self.te_int(1)),
4147            },
4148        ]);
4149        let sum_loop = Stmt::While {
4150            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(i), self.te_int(cols)),
4151            body: sum_body,
4152            decreasing: None,
4153        };
4154        let mean_expr = self.te_bin(
4155            BinaryOpKind::Divide,
4156            self.te_id(sm),
4157            self.te_float(cols as f64),
4158        );
4159        // pass 2: vs += (X[idx(i)] - mean)²  →  denom = sqrt(vs/cols + eps)
4160        let d1 = self.te_bin(
4161            BinaryOpKind::Subtract,
4162            self.te_index(self.te_id(x), idx(self, i)),
4163            self.te_id(mean),
4164        );
4165        let d2 = self.te_bin(
4166            BinaryOpKind::Subtract,
4167            self.te_index(self.te_id(x), idx(self, i)),
4168            self.te_id(mean),
4169        );
4170        let var_body = self.te_block(vec![
4171            Stmt::Set {
4172                target: vs,
4173                value: self.te_bin(
4174                    BinaryOpKind::Add,
4175                    self.te_id(vs),
4176                    self.te_bin(BinaryOpKind::Multiply, d1, d2),
4177                ),
4178            },
4179            Stmt::Set {
4180                target: i,
4181                value: self.te_bin(BinaryOpKind::Add, self.te_id(i), self.te_int(1)),
4182            },
4183        ]);
4184        let var_loop = Stmt::While {
4185            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(i), self.te_int(cols)),
4186            body: var_body,
4187            decreasing: None,
4188        };
4189        let var_mean = self.te_bin(
4190            BinaryOpKind::Divide,
4191            self.te_id(vs),
4192            self.te_float(cols as f64),
4193        );
4194        let var_eps = self.te_bin(BinaryOpKind::Add, var_mean, self.te_float(eps));
4195        let denom_expr = self.te_call(sqrt_sym, vec![var_eps]);
4196        // normalize: Push (X[idx(j)] - mean) / denom * W[j] + B[j]
4197        let centered = self.te_bin(
4198            BinaryOpKind::Subtract,
4199            self.te_index(self.te_id(x), idx(self, j)),
4200            self.te_id(mean),
4201        );
4202        let normed = self.te_bin(BinaryOpKind::Divide, centered, self.te_id(denom));
4203        let scaled = self.te_bin(
4204            BinaryOpKind::Multiply,
4205            normed,
4206            self.te_index(self.te_id(weight), self.te_id(j)),
4207        );
4208        let shifted = self.te_bin(
4209            BinaryOpKind::Add,
4210            scaled,
4211            self.te_index(self.te_id(bias), self.te_id(j)),
4212        );
4213        let n_body = self.te_block(vec![
4214            Stmt::Push {
4215                value: shifted,
4216                collection: self.te_id(var),
4217            },
4218            Stmt::Set {
4219                target: j,
4220                value: self.te_bin(BinaryOpKind::Add, self.te_id(j), self.te_int(1)),
4221            },
4222        ]);
4223        let n_loop = Stmt::While {
4224            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(j), self.te_int(cols)),
4225            body: n_body,
4226            decreasing: None,
4227        };
4228        let row_body = self.te_block(vec![
4229            Stmt::Set {
4230                target: sm,
4231                value: self.te_float(0.0),
4232            },
4233            Stmt::Set {
4234                target: i,
4235                value: self.te_int(1),
4236            },
4237            sum_loop,
4238            Stmt::Set {
4239                target: mean,
4240                value: mean_expr,
4241            },
4242            Stmt::Set {
4243                target: vs,
4244                value: self.te_float(0.0),
4245            },
4246            Stmt::Set {
4247                target: i,
4248                value: self.te_int(1),
4249            },
4250            var_loop,
4251            Stmt::Set {
4252                target: denom,
4253                value: denom_expr,
4254            },
4255            Stmt::Set {
4256                target: j,
4257                value: self.te_int(1),
4258            },
4259            n_loop,
4260            Stmt::Set {
4261                target: r,
4262                value: self.te_bin(BinaryOpKind::Add, self.te_id(r), self.te_int(1)),
4263            },
4264        ]);
4265        let row_loop = Stmt::While {
4266            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(r), self.te_int(rows)),
4267            body: row_body,
4268            decreasing: None,
4269        };
4270        let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
4271            type_name: seq_sym,
4272            type_args: vec![TypeExpr::Named(real_sym)],
4273            init_fields: vec![],
4274        });
4275        let body = self.te_block(vec![
4276            Stmt::Let {
4277                var,
4278                ty: None,
4279                value: new_seq,
4280                mutable: true,
4281            },
4282            Stmt::Let {
4283                var: r,
4284                ty: None,
4285                value: self.te_int(1),
4286                mutable: true,
4287            },
4288            Stmt::Let {
4289                var: sm,
4290                ty: None,
4291                value: self.te_float(0.0),
4292                mutable: true,
4293            },
4294            Stmt::Let {
4295                var: mean,
4296                ty: None,
4297                value: self.te_float(0.0),
4298                mutable: true,
4299            },
4300            Stmt::Let {
4301                var: vs,
4302                ty: None,
4303                value: self.te_float(0.0),
4304                mutable: true,
4305            },
4306            Stmt::Let {
4307                var: denom,
4308                ty: None,
4309                value: self.te_float(0.0),
4310                mutable: true,
4311            },
4312            Stmt::Let {
4313                var: i,
4314                ty: None,
4315                value: self.te_int(1),
4316                mutable: true,
4317            },
4318            Stmt::Let {
4319                var: j,
4320                ty: None,
4321                value: self.te_int(1),
4322                mutable: true,
4323            },
4324            row_loop,
4325        ]);
4326        Stmt::Splice { body }
4327    }
4328
4329    /// `Let Y be softmax of X` → `Y[i] = e^(X[i]-max) / Σ e^(X[j]-max)`. Desugars to a `max` reduction, a
4330    /// sum-of-exponentials loop (`e` via `E ** (X[i]-max)`, powf), and a normalize loop into a `Seq of Real`.
4331    fn desugar_tensor_softmax(&mut self, var: Symbol, x: Symbol, total: i64) -> Stmt<'a> {
4332        const E: f64 = std::f64::consts::E;
4333        let max_sym = self.interner.intern("max");
4334        let seq_sym = self.interner.intern("Seq");
4335        let real_sym = self.interner.intern("Real");
4336        let m = self.fresh_desugar_temp("sm_m");
4337        let p = self.fresh_desugar_temp("sm_p");
4338        let s = self.fresh_desugar_temp("sm_s");
4339        let p2 = self.fresh_desugar_temp("sm_p2");
4340        let q = self.fresh_desugar_temp("sm_q");
4341        // max reduction: m = X[1]; for p in 2..=total: m = max(m, X[p])
4342        let max_body = self.te_block(vec![
4343            Stmt::Set {
4344                target: m,
4345                value: self.te_call(
4346                    max_sym,
4347                    vec![self.te_id(m), self.te_index(self.te_id(x), self.te_id(p))],
4348                ),
4349            },
4350            Stmt::Set {
4351                target: p,
4352                value: self.te_bin(BinaryOpKind::Add, self.te_id(p), self.te_int(1)),
4353            },
4354        ]);
4355        let max_loop = Stmt::While {
4356            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p), self.te_int(total)),
4357            body: max_body,
4358            decreasing: None,
4359        };
4360        // e^(X[i]-m)
4361        let expo = |s2: &Self, idx: Symbol| {
4362            s2.te_bin(
4363                BinaryOpKind::Pow,
4364                s2.te_float(E),
4365                s2.te_bin(
4366                    BinaryOpKind::Subtract,
4367                    s2.te_index(s2.te_id(x), s2.te_id(idx)),
4368                    s2.te_id(m),
4369                ),
4370            )
4371        };
4372        // Σ e^(X[p2]-m)
4373        let s_body = self.te_block(vec![
4374            Stmt::Set {
4375                target: s,
4376                value: self.te_bin(BinaryOpKind::Add, self.te_id(s), expo(self, p2)),
4377            },
4378            Stmt::Set {
4379                target: p2,
4380                value: self.te_bin(BinaryOpKind::Add, self.te_id(p2), self.te_int(1)),
4381            },
4382        ]);
4383        let s_loop = Stmt::While {
4384            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p2), self.te_int(total)),
4385            body: s_body,
4386            decreasing: None,
4387        };
4388        let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
4389            type_name: seq_sym,
4390            type_args: vec![TypeExpr::Named(real_sym)],
4391            init_fields: vec![],
4392        });
4393        // Y[q] = e^(X[q]-m) / s
4394        let norm = self.te_bin(BinaryOpKind::Divide, expo(self, q), self.te_id(s));
4395        let n_body = self.te_block(vec![
4396            Stmt::Push {
4397                value: norm,
4398                collection: self.te_id(var),
4399            },
4400            Stmt::Set {
4401                target: q,
4402                value: self.te_bin(BinaryOpKind::Add, self.te_id(q), self.te_int(1)),
4403            },
4404        ]);
4405        let n_loop = Stmt::While {
4406            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(q), self.te_int(total)),
4407            body: n_body,
4408            decreasing: None,
4409        };
4410        let body = self.te_block(vec![
4411            Stmt::Let {
4412                var: m,
4413                ty: None,
4414                value: self.te_index(self.te_id(x), self.te_int(1)),
4415                mutable: true,
4416            },
4417            Stmt::Let {
4418                var: p,
4419                ty: None,
4420                value: self.te_int(2),
4421                mutable: true,
4422            },
4423            max_loop,
4424            Stmt::Let {
4425                var: s,
4426                ty: None,
4427                value: self.te_float(0.0),
4428                mutable: true,
4429            },
4430            Stmt::Let {
4431                var: p2,
4432                ty: None,
4433                value: self.te_int(1),
4434                mutable: true,
4435            },
4436            s_loop,
4437            Stmt::Let {
4438                var,
4439                ty: None,
4440                value: new_seq,
4441                mutable: true,
4442            },
4443            Stmt::Let {
4444                var: q,
4445                ty: None,
4446                value: self.te_int(1),
4447                mutable: true,
4448            },
4449            n_loop,
4450        ]);
4451        Stmt::Splice { body }
4452    }
4453
4454    /// `Let Y be softmax of X` where `X` is a `rows × cols` tensor — a PER-ROW softmax, which is what the
4455    /// transformer shape means and what `FusedRowSpec` documents ("one output row per input row, each
4456    /// normalized independently over its `cols` entries").
4457    ///
4458    /// ★ The flat [`desugar_tensor_softmax`] is the `rows == 1` case. Applying it to a 2-D tensor
4459    /// normalises across the WHOLE matrix, which for attention scores is silently wrong — every existing
4460    /// test escaped it only by using single-token attention, where the score matrix has one row. It also
4461    /// made the spatial backend refuse any real 2-D softmax, because a flattened `32 × 64` asks for 64
4462    /// accumulator tiles against 4.
4463    ///
4464    /// Emitted as an outer row loop over an inner `cols` loop, so the accelerator recovers `rows`/`cols`
4465    /// from the loop bounds exactly as the weighted-RMSNorm shape does.
4466    fn desugar_tensor_softmax_rows(
4467        &mut self,
4468        var: Symbol,
4469        x: Symbol,
4470        rows: i64,
4471        cols: i64,
4472    ) -> Stmt<'a> {
4473        const E: f64 = std::f64::consts::E;
4474        let max_sym = self.interner.intern("max");
4475        let seq_sym = self.interner.intern("Seq");
4476        let real_sym = self.interner.intern("Real");
4477        let rr = self.fresh_desugar_temp("smr_r");
4478        let m = self.fresh_desugar_temp("smr_m");
4479        let p = self.fresh_desugar_temp("smr_p");
4480        let s = self.fresh_desugar_temp("smr_s");
4481        let p2 = self.fresh_desugar_temp("smr_p2");
4482        let q = self.fresh_desugar_temp("smr_q");
4483        // X[(r-1)*cols + i] in 1-based `item` terms.
4484        let xi = |s2: &Self, i: Symbol| {
4485            let base = s2.te_bin(
4486                BinaryOpKind::Multiply,
4487                s2.te_bin(BinaryOpKind::Subtract, s2.te_id(rr), s2.te_int(1)),
4488                s2.te_int(cols),
4489            );
4490            s2.te_index(s2.te_id(x), s2.te_bin(BinaryOpKind::Add, base, s2.te_id(i)))
4491        };
4492        let expo = |s2: &Self, i: Symbol| {
4493            s2.te_bin(
4494                BinaryOpKind::Pow,
4495                s2.te_float(E),
4496                s2.te_bin(BinaryOpKind::Subtract, xi(s2, i), s2.te_id(m)),
4497            )
4498        };
4499        let bump = |s2: &Self, v: Symbol| Stmt::Set {
4500            target: v,
4501            value: s2.te_bin(BinaryOpKind::Add, s2.te_id(v), s2.te_int(1)),
4502        };
4503        let max_body = self.te_block(vec![
4504            Stmt::Set {
4505                target: m,
4506                value: self.te_call(max_sym, vec![self.te_id(m), xi(self, p)]),
4507            },
4508            bump(self, p),
4509        ]);
4510        let max_loop = Stmt::While {
4511            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p), self.te_int(cols)),
4512            body: max_body,
4513            decreasing: None,
4514        };
4515        let s_body = self.te_block(vec![
4516            Stmt::Set {
4517                target: s,
4518                value: self.te_bin(BinaryOpKind::Add, self.te_id(s), expo(self, p2)),
4519            },
4520            bump(self, p2),
4521        ]);
4522        let s_loop = Stmt::While {
4523            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p2), self.te_int(cols)),
4524            body: s_body,
4525            decreasing: None,
4526        };
4527        let n_body = self.te_block(vec![
4528            Stmt::Push {
4529                value: self.te_bin(BinaryOpKind::Divide, expo(self, q), self.te_id(s)),
4530                collection: self.te_id(var),
4531            },
4532            bump(self, q),
4533        ]);
4534        let n_loop = Stmt::While {
4535            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(q), self.te_int(cols)),
4536            body: n_body,
4537            decreasing: None,
4538        };
4539        // m starts at the row's FIRST element: X[(r-1)*cols + 1].
4540        let first = {
4541            let base = self.te_bin(
4542                BinaryOpKind::Multiply,
4543                self.te_bin(BinaryOpKind::Subtract, self.te_id(rr), self.te_int(1)),
4544                self.te_int(cols),
4545            );
4546            self.te_index(
4547                self.te_id(x),
4548                self.te_bin(BinaryOpKind::Add, base, self.te_int(1)),
4549            )
4550        };
4551        let row_body = self.te_block(vec![
4552            Stmt::Let {
4553                var: m,
4554                ty: None,
4555                value: first,
4556                mutable: true,
4557            },
4558            Stmt::Let {
4559                var: p,
4560                ty: None,
4561                value: self.te_int(2),
4562                mutable: true,
4563            },
4564            max_loop,
4565            Stmt::Let {
4566                var: s,
4567                ty: None,
4568                value: self.te_float(0.0),
4569                mutable: true,
4570            },
4571            Stmt::Let {
4572                var: p2,
4573                ty: None,
4574                value: self.te_int(1),
4575                mutable: true,
4576            },
4577            s_loop,
4578            Stmt::Let {
4579                var: q,
4580                ty: None,
4581                value: self.te_int(1),
4582                mutable: true,
4583            },
4584            n_loop,
4585            bump(self, rr),
4586        ]);
4587        let row_loop = Stmt::While {
4588            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(rr), self.te_int(rows)),
4589            body: row_body,
4590            decreasing: None,
4591        };
4592        let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
4593            type_name: seq_sym,
4594            type_args: vec![TypeExpr::Named(real_sym)],
4595            init_fields: vec![],
4596        });
4597        let body = self.te_block(vec![
4598            Stmt::Let {
4599                var,
4600                ty: None,
4601                value: new_seq,
4602                mutable: true,
4603            },
4604            Stmt::Let {
4605                var: rr,
4606                ty: None,
4607                value: self.te_int(1),
4608                mutable: true,
4609            },
4610            row_loop,
4611        ]);
4612        Stmt::Splice { body }
4613    }
4614
4615    /// `Let Y be silu of X` — elementwise SiLU `Y[i] = X[i] / (1 + e^(0 - X[i]))` over all `total` entries.
4616    /// `Let Qr be rope of Q with cos RC and sin RS` — RoPE-NEOX per head. `H` (outer) and `half` (inner) are
4617    /// the loop bounds (so the accelerator recovers `hd = 2·half` from them). Per head `rh`, push the `half`
4618    /// rotated first-half entries, then the `half` rotated second-half entries — row-major so
4619    /// `Qr[rh*hd + i] = Q[rh*hd+i]·cos[i] − Q[rh*hd+i+half]·sin[i]`, `Qr[rh*hd + half + i] = Q[..i]·sin[i] +
4620    /// Q[..i+half]·cos[i]`. The heads of every row of `Q` run in turn; with `per_row` heads to a row and a table
4621    /// row per row of `Q`, head `rh` reads table row `rh / per_row`. `from = (P, cache rows)`: the tables are a cache of
4622    /// angles per position, head `rh` reading row `P + rh / per_row` (1-based) — behind the check that the block's rows
4623    /// `P … P + R − 1` all lie among the cache's.
4624    #[allow(clippy::too_many_arguments)]
4625    fn desugar_tensor_rope(
4626        &mut self,
4627        var: Symbol,
4628        q: Symbol,
4629        rc: Symbol,
4630        rs: Symbol,
4631        d_total: i64,
4632        half: i64,
4633        hd: i64,
4634        per_row: Option<i64>,
4635        from: Option<(&'a Expr<'a>, i64)>,
4636    ) -> Stmt<'a> {
4637        let h_count = if hd > 0 { d_total / hd } else { 0 };
4638        let block = h_count / per_row.unwrap_or(h_count).max(1);
4639        let checked = from.map(|(p, cache)| {
4640            if block == 1 {
4641                self.checked_row_index(p, rc, cache)
4642            } else {
4643                self.checked_row_block(p, rc, cache, block)
4644            }
4645        });
4646        let at = checked.as_ref().map(|(at, _)| *at);
4647        let seq_sym = self.interner.intern("Seq");
4648        let real_sym = self.interner.intern("Real");
4649        let rh = self.fresh_desugar_temp("rp_h");
4650        let ri = self.fresh_desugar_temp("rp_i");
4651        let rp = self.fresh_desugar_temp("rp_pt");
4652        let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
4653            type_name: seq_sym,
4654            type_args: vec![TypeExpr::Named(real_sym)],
4655            init_fields: vec![],
4656        });
4657        // index builders (1-based `item`): a = rh*hd+ri+1, b = rh*hd+ri+half+1, i = ri+1 — or, a table row per row
4658        // of Q, i = (rh/per_row)*half + (ri+1) — or, reading a cache from position `at`, i = ((at−1) + rh/per_row)*half + (ri+1)
4659        let idx_a = |s: &mut Self| {
4660            s.te_bin(
4661                BinaryOpKind::Add,
4662                s.te_bin(
4663                    BinaryOpKind::Add,
4664                    s.te_bin(BinaryOpKind::Multiply, s.te_id(rh), s.te_int(hd)),
4665                    s.te_id(ri),
4666                ),
4667                s.te_int(1),
4668            )
4669        };
4670        let idx_b = |s: &mut Self| {
4671            s.te_bin(
4672                BinaryOpKind::Add,
4673                s.te_bin(
4674                    BinaryOpKind::Add,
4675                    s.te_bin(
4676                        BinaryOpKind::Add,
4677                        s.te_bin(BinaryOpKind::Multiply, s.te_id(rh), s.te_int(hd)),
4678                        s.te_id(ri),
4679                    ),
4680                    s.te_int(half),
4681                ),
4682                s.te_int(1),
4683            )
4684        };
4685        let idx_i = |s: &mut Self| {
4686            let within = s.te_bin(BinaryOpKind::Add, s.te_id(ri), s.te_int(1));
4687            match (per_row, at) {
4688                (None, _) => within,
4689                (Some(hpr), None) => s.te_bin(
4690                    BinaryOpKind::Add,
4691                    s.te_bin(
4692                        BinaryOpKind::Multiply,
4693                        s.te_bin(BinaryOpKind::Divide, s.te_id(rh), s.te_int(hpr)),
4694                        s.te_int(half),
4695                    ),
4696                    within,
4697                ),
4698                (Some(hpr), Some(at)) => {
4699                    let row = s.te_bin(
4700                        BinaryOpKind::Add,
4701                        s.te_bin(BinaryOpKind::Subtract, s.te_id(at), s.te_int(1)),
4702                        s.te_bin(BinaryOpKind::Divide, s.te_id(rh), s.te_int(hpr)),
4703                    );
4704                    s.te_bin(
4705                        BinaryOpKind::Add,
4706                        s.te_bin(BinaryOpKind::Multiply, row, s.te_int(half)),
4707                        within,
4708                    )
4709                }
4710            }
4711        };
4712        // push1 = Q[a]*RC[i] - Q[b]*RS[i]
4713        let qa1 = idx_a(self);
4714        let ii1 = idx_i(self);
4715        let qb1 = idx_b(self);
4716        let ii2 = idx_i(self);
4717        let m1 = self.te_bin(
4718            BinaryOpKind::Multiply,
4719            self.te_index(self.te_id(q), qa1),
4720            self.te_index(self.te_id(rc), ii1),
4721        );
4722        let m2 = self.te_bin(
4723            BinaryOpKind::Multiply,
4724            self.te_index(self.te_id(q), qb1),
4725            self.te_index(self.te_id(rs), ii2),
4726        );
4727        let push1 = self.te_bin(BinaryOpKind::Subtract, m1, m2);
4728        let inner1 = self.te_block(vec![
4729            Stmt::Push {
4730                value: push1,
4731                collection: self.te_id(var),
4732            },
4733            Stmt::Set {
4734                target: ri,
4735                value: self.te_bin(BinaryOpKind::Add, self.te_id(ri), self.te_int(1)),
4736            },
4737        ]);
4738        let loop1 = Stmt::While {
4739            cond: self.te_bin(BinaryOpKind::Lt, self.te_id(ri), self.te_int(half)),
4740            body: inner1,
4741            decreasing: None,
4742        };
4743        // push2 = Q[a]*RS[i] + Q[b]*RC[i]
4744        let qa2 = idx_a(self);
4745        let ii3 = idx_i(self);
4746        let qb2 = idx_b(self);
4747        let ii4 = idx_i(self);
4748        let n1 = self.te_bin(
4749            BinaryOpKind::Multiply,
4750            self.te_index(self.te_id(q), qa2),
4751            self.te_index(self.te_id(rs), ii3),
4752        );
4753        let n2 = self.te_bin(
4754            BinaryOpKind::Multiply,
4755            self.te_index(self.te_id(q), qb2),
4756            self.te_index(self.te_id(rc), ii4),
4757        );
4758        let push2 = self.te_bin(BinaryOpKind::Add, n1, n2);
4759        let inner2 = self.te_block(vec![
4760            Stmt::Push {
4761                value: push2,
4762                collection: self.te_id(var),
4763            },
4764            Stmt::Set {
4765                target: ri,
4766                value: self.te_bin(BinaryOpKind::Add, self.te_id(ri), self.te_int(1)),
4767            },
4768        ]);
4769        let loop2 = Stmt::While {
4770            cond: self.te_bin(BinaryOpKind::Lt, self.te_id(ri), self.te_int(half)),
4771            body: inner2,
4772            decreasing: None,
4773        };
4774        // PASS-THROUGH tail: Qwen3.8 rotates only `2·half` of each head's `hd` dims (64 of 256) and copies the
4775        // rest through untouched. When `hd == 2·half` (full rotary, the classic form) this loop is empty, so
4776        // the generalization costs the existing callers nothing.
4777        let pt_idx = self.te_bin(
4778            BinaryOpKind::Add,
4779            self.te_bin(
4780                BinaryOpKind::Add,
4781                self.te_bin(BinaryOpKind::Multiply, self.te_id(rh), self.te_int(hd)),
4782                self.te_id(rp),
4783            ),
4784            self.te_int(1),
4785        );
4786        let inner3 = self.te_block(vec![
4787            Stmt::Push {
4788                value: self.te_index(self.te_id(q), pt_idx),
4789                collection: self.te_id(var),
4790            },
4791            Stmt::Set {
4792                target: rp,
4793                value: self.te_bin(BinaryOpKind::Add, self.te_id(rp), self.te_int(1)),
4794            },
4795        ]);
4796        let loop3 = Stmt::While {
4797            cond: self.te_bin(BinaryOpKind::Lt, self.te_id(rp), self.te_int(hd)),
4798            body: inner3,
4799            decreasing: None,
4800        };
4801        let outer_body = self.te_block(vec![
4802            Stmt::Let {
4803                var: ri,
4804                ty: None,
4805                value: self.te_int(0),
4806                mutable: true,
4807            },
4808            loop1,
4809            Stmt::Set {
4810                target: ri,
4811                value: self.te_int(0),
4812            },
4813            loop2,
4814            Stmt::Let {
4815                var: rp,
4816                ty: None,
4817                value: self.te_int(2 * half),
4818                mutable: true,
4819            },
4820            loop3,
4821            Stmt::Set {
4822                target: rh,
4823                value: self.te_bin(BinaryOpKind::Add, self.te_id(rh), self.te_int(1)),
4824            },
4825        ]);
4826        let outer = Stmt::While {
4827            cond: self.te_bin(BinaryOpKind::Lt, self.te_id(rh), self.te_int(h_count)),
4828            body: outer_body,
4829            decreasing: None,
4830        };
4831        let mut stmts: Vec<Stmt<'a>> = checked
4832            .map(|(_, check)| check.into_iter().collect())
4833            .unwrap_or_default();
4834        stmts.extend([
4835            Stmt::Let {
4836                var,
4837                ty: None,
4838                value: new_seq,
4839                mutable: true,
4840            },
4841            Stmt::Let {
4842                var: rh,
4843                ty: None,
4844                value: self.te_int(0),
4845                mutable: true,
4846            },
4847            outer,
4848        ]);
4849        let body = self.te_block(stmts);
4850        Stmt::Splice { body }
4851    }
4852
4853    fn desugar_tensor_silu(&mut self, var: Symbol, x: Symbol, total: i64) -> Stmt<'a> {
4854        const E: f64 = std::f64::consts::E;
4855        let seq_sym = self.interner.intern("Seq");
4856        let real_sym = self.interner.intern("Real");
4857        let i = self.fresh_desugar_temp("sl_i");
4858        let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
4859            type_name: seq_sym,
4860            type_args: vec![TypeExpr::Named(real_sym)],
4861            init_fields: vec![],
4862        });
4863        let xi_num = self.te_index(self.te_id(x), self.te_id(i));
4864        let xi_den = self.te_index(self.te_id(x), self.te_id(i));
4865        let neg = self.te_bin(BinaryOpKind::Subtract, self.te_float(0.0), xi_den);
4866        let ex = self.te_bin(BinaryOpKind::Pow, self.te_float(E), neg);
4867        let denom = self.te_bin(BinaryOpKind::Add, self.te_float(1.0), ex);
4868        let y = self.te_bin(BinaryOpKind::Divide, xi_num, denom);
4869        let body = self.te_block(vec![
4870            Stmt::Push {
4871                value: y,
4872                collection: self.te_id(var),
4873            },
4874            Stmt::Set {
4875                target: i,
4876                value: self.te_bin(BinaryOpKind::Add, self.te_id(i), self.te_int(1)),
4877            },
4878        ]);
4879        let loop_ = Stmt::While {
4880            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(i), self.te_int(total)),
4881            body,
4882            decreasing: None,
4883        };
4884        let outer = self.te_block(vec![
4885            Stmt::Let {
4886                var,
4887                ty: None,
4888                value: new_seq,
4889                mutable: true,
4890            },
4891            Stmt::Let {
4892                var: i,
4893                ty: None,
4894                value: self.te_int(1),
4895                mutable: true,
4896            },
4897            loop_,
4898        ]);
4899        Stmt::Splice { body: outer }
4900    }
4901
4902    /// `Let Y be sigmoid of X` → elementwise `Y[i] = 1/(1 + e^-x)`. Qwen3.8 needs it twice: the gated
4903    /// attention block multiplies its context by `sigmoid` of a gate projection, and Gated DeltaNet's
4904    /// delta strength is `beta = sigmoid(b)`.
4905    fn desugar_tensor_sigmoid(&mut self, var: Symbol, x: Symbol, total: i64) -> Stmt<'a> {
4906        const E: f64 = std::f64::consts::E;
4907        let seq_sym = self.interner.intern("Seq");
4908        let real_sym = self.interner.intern("Real");
4909        let i = self.fresh_desugar_temp("sg_i");
4910        let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
4911            type_name: seq_sym,
4912            type_args: vec![TypeExpr::Named(real_sym)],
4913            init_fields: vec![],
4914        });
4915        let neg = self.te_bin(
4916            BinaryOpKind::Subtract,
4917            self.te_float(0.0),
4918            self.te_index(self.te_id(x), self.te_id(i)),
4919        );
4920        let ex = self.te_bin(BinaryOpKind::Pow, self.te_float(E), neg);
4921        let denom = self.te_bin(BinaryOpKind::Add, self.te_float(1.0), ex);
4922        let y = self.te_bin(BinaryOpKind::Divide, self.te_float(1.0), denom);
4923        let body = self.te_block(vec![
4924            Stmt::Push {
4925                value: y,
4926                collection: self.te_id(var),
4927            },
4928            Stmt::Set {
4929                target: i,
4930                value: self.te_bin(BinaryOpKind::Add, self.te_id(i), self.te_int(1)),
4931            },
4932        ]);
4933        let loop_ = Stmt::While {
4934            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(i), self.te_int(total)),
4935            body,
4936            decreasing: None,
4937        };
4938        let outer = self.te_block(vec![
4939            Stmt::Let {
4940                var,
4941                ty: None,
4942                value: new_seq,
4943                mutable: true,
4944            },
4945            Stmt::Let {
4946                var: i,
4947                ty: None,
4948                value: self.te_int(1),
4949                mutable: true,
4950            },
4951            loop_,
4952        ]);
4953        Stmt::Splice { body: outer }
4954    }
4955
4956    /// `Let Y be gelu of X` → elementwise GELU, the **tanh approximation** ggml uses — so a vision tower
4957    /// authored here matches llama.cpp's `mmproj` numerically rather than approximately.
4958    ///
4959    /// The textbook form is `0.5·x·(1 + tanh(z))` with `z = √(2/π)·(x + 0.044715·x³)`. Expanding `tanh` as
4960    /// `(e^2z − 1)/(e^2z + 1)` collapses the whole thing to `x / (1 + e^-2z)` — algebraically identical, one
4961    /// exponential instead of three ops, and it is the numerically SAFE arrangement in both tails: as `x → +∞`
4962    /// the exponent goes to `-∞` so `e^-2z → 0` and `y → x`, and as `x → -∞` it goes to `+∞` so `y → 0`. The
4963    /// direct `(t−1)/(t+1)` form instead evaluates `∞/∞ = NaN` for large positive `x`.
4964    fn desugar_tensor_gelu(&mut self, var: Symbol, x: Symbol, total: i64) -> Stmt<'a> {
4965        const E: f64 = std::f64::consts::E;
4966        const SQRT_2_OVER_PI: f64 = 0.797_884_560_802_865_4;
4967        const K: f64 = 0.044_715;
4968        let seq_sym = self.interner.intern("Seq");
4969        let real_sym = self.interner.intern("Real");
4970        let i = self.fresh_desugar_temp("gl_i");
4971        let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
4972            type_name: seq_sym,
4973            type_args: vec![TypeExpr::Named(real_sym)],
4974            init_fields: vec![],
4975        });
4976        // z = √(2/π)·(x + 0.044715·x³)
4977        let x3 = self.te_bin(
4978            BinaryOpKind::Multiply,
4979            self.te_index(self.te_id(x), self.te_id(i)),
4980            self.te_bin(
4981                BinaryOpKind::Multiply,
4982                self.te_index(self.te_id(x), self.te_id(i)),
4983                self.te_index(self.te_id(x), self.te_id(i)),
4984            ),
4985        );
4986        let inner = self.te_bin(
4987            BinaryOpKind::Add,
4988            self.te_index(self.te_id(x), self.te_id(i)),
4989            self.te_bin(BinaryOpKind::Multiply, self.te_float(K), x3),
4990        );
4991        let z = self.te_bin(BinaryOpKind::Multiply, self.te_float(SQRT_2_OVER_PI), inner);
4992        // y = x / (1 + e^(0 - 2z))
4993        let neg2z = self.te_bin(
4994            BinaryOpKind::Subtract,
4995            self.te_float(0.0),
4996            self.te_bin(BinaryOpKind::Multiply, self.te_float(2.0), z),
4997        );
4998        let ex = self.te_bin(BinaryOpKind::Pow, self.te_float(E), neg2z);
4999        let denom = self.te_bin(BinaryOpKind::Add, self.te_float(1.0), ex);
5000        let y = self.te_bin(
5001            BinaryOpKind::Divide,
5002            self.te_index(self.te_id(x), self.te_id(i)),
5003            denom,
5004        );
5005        let body = self.te_block(vec![
5006            Stmt::Push {
5007                value: y,
5008                collection: self.te_id(var),
5009            },
5010            Stmt::Set {
5011                target: i,
5012                value: self.te_bin(BinaryOpKind::Add, self.te_id(i), self.te_int(1)),
5013            },
5014        ]);
5015        let loop_ = Stmt::While {
5016            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(i), self.te_int(total)),
5017            body,
5018            decreasing: None,
5019        };
5020        let outer = self.te_block(vec![
5021            Stmt::Let {
5022                var,
5023                ty: None,
5024                value: new_seq,
5025                mutable: true,
5026            },
5027            Stmt::Let {
5028                var: i,
5029                ty: None,
5030                value: self.te_int(1),
5031                mutable: true,
5032            },
5033            loop_,
5034        ]);
5035        Stmt::Splice { body: outer }
5036    }
5037
5038    /// `Let A be decay of Ar with bias Dt rate R` → Gated DeltaNet's per-head **forget gate**,
5039    /// `A[i] = (1 + e^(Ar[i] + Dt[i]))^(R[i])`, where `R` is the (negative) decay rate.
5040    ///
5041    /// The paper writes this as `exp(R · softplus(Ar + dt))`, which needs a logarithm — and the desugar layer
5042    /// has only `Pow` and `sqrt`, no `ln`. But softplus is `ln(1 + e^u)`, so the logarithm cancels against the
5043    /// outer exponential exactly:
5044    ///
5045    /// ```text
5046    ///   exp(R · ln(1 + e^u)) = (1 + e^u)^R
5047    /// ```
5048    ///
5049    /// Same value, one fewer transcendental, and expressible in the primitives every backend already has.
5050    /// That identity is why `softplus` never became an op. `R` is used AS STORED: Qwen3.8's `ssm_a` already
5051    /// holds `−exp(A_log)`, so exponentiating it again would shorten the memory by an order of magnitude.
5052    ///
5053    /// Over a block of rows (`heads < total`) every row decays by the same per-head bias and rate: a row loop around
5054    /// the head loop, `Ar` read at `(row − 1)·heads + i`. One row is the single decay, statement for statement.
5055    fn desugar_tensor_decay(
5056        &mut self,
5057        var: Symbol,
5058        ar: Symbol,
5059        dt: Symbol,
5060        alog: Symbol,
5061        total: i64,
5062        heads: i64,
5063    ) -> Stmt<'a> {
5064        const E: f64 = std::f64::consts::E;
5065        let seq_sym = self.interner.intern("Seq");
5066        let real_sym = self.interner.intern("Real");
5067        let i = self.fresh_desugar_temp("dc_i");
5068        let row = (heads < total).then(|| self.fresh_desugar_temp("dc_row"));
5069        let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
5070            type_name: seq_sym,
5071            type_args: vec![TypeExpr::Named(real_sym)],
5072            init_fields: vec![],
5073        });
5074        let at = match row {
5075            None => self.te_id(i),
5076            Some(r) => self.te_bin(
5077                BinaryOpKind::Add,
5078                self.te_bin(
5079                    BinaryOpKind::Multiply,
5080                    self.te_bin(BinaryOpKind::Subtract, self.te_id(r), self.te_int(1)),
5081                    self.te_int(heads),
5082                ),
5083                self.te_id(i),
5084            ),
5085        };
5086        // u = Ar[at] + Dt[i];  base = 1 + e^u;  A = e^(Alog[i]);  Y = base^(0 - A)
5087        let u = self.te_bin(
5088            BinaryOpKind::Add,
5089            self.te_index(self.te_id(ar), at),
5090            self.te_index(self.te_id(dt), self.te_id(i)),
5091        );
5092        let base = self.te_bin(
5093            BinaryOpKind::Add,
5094            self.te_float(1.0),
5095            self.te_bin(BinaryOpKind::Pow, self.te_float(E), u),
5096        );
5097        let rate = self.te_index(self.te_id(alog), self.te_id(i));
5098        let y = self.te_bin(BinaryOpKind::Pow, base, rate);
5099        let body = self.te_block(vec![
5100            Stmt::Push {
5101                value: y,
5102                collection: self.te_id(var),
5103            },
5104            Stmt::Set {
5105                target: i,
5106                value: self.te_bin(BinaryOpKind::Add, self.te_id(i), self.te_int(1)),
5107            },
5108        ]);
5109        let loop_ = Stmt::While {
5110            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(i), self.te_int(heads)),
5111            body,
5112            decreasing: None,
5113        };
5114        let outer = match row {
5115            None => vec![
5116                Stmt::Let {
5117                    var,
5118                    ty: None,
5119                    value: new_seq,
5120                    mutable: true,
5121                },
5122                Stmt::Let {
5123                    var: i,
5124                    ty: None,
5125                    value: self.te_int(1),
5126                    mutable: true,
5127                },
5128                loop_,
5129            ],
5130            Some(r) => {
5131                let row_body = self.te_block(vec![
5132                    Stmt::Let {
5133                        var: i,
5134                        ty: None,
5135                        value: self.te_int(1),
5136                        mutable: true,
5137                    },
5138                    loop_,
5139                    Stmt::Set {
5140                        target: r,
5141                        value: self.te_bin(BinaryOpKind::Add, self.te_id(r), self.te_int(1)),
5142                    },
5143                ]);
5144                vec![
5145                    Stmt::Let {
5146                        var,
5147                        ty: None,
5148                        value: new_seq,
5149                        mutable: true,
5150                    },
5151                    Stmt::Let {
5152                        var: r,
5153                        ty: None,
5154                        value: self.te_int(1),
5155                        mutable: true,
5156                    },
5157                    Stmt::While {
5158                        cond: self.te_bin(
5159                            BinaryOpKind::LtEq,
5160                            self.te_id(r),
5161                            self.te_int(total / heads),
5162                        ),
5163                        body: row_body,
5164                        decreasing: None,
5165                    },
5166                ]
5167            }
5168        };
5169        let outer = self.te_block(outer);
5170        Stmt::Splice { body: outer }
5171    }
5172
5173    /// `Let Y be l2norm of X` → PER-ROW L2 normalization `Y[r,i] = X[r,i] / sqrt(Σ_i X[r,i]² + eps)` over an
5174    /// `R by C` tensor (`eps` the program's, 1e-6 by default). Per-row is what makes it useful: declare a tensor as `heads by head_dim`
5175    /// and one `l2norm` normalizes every head independently — which is exactly what Gated DeltaNet does to
5176    /// `q` and `k` before the delta update. (Distinct from `rmsnorm`, which divides by the root *mean* square.)
5177    fn desugar_tensor_l2norm(&mut self, var: Symbol, x: Symbol, rows: i64, cols: i64, eps: f64) -> Stmt<'a> {
5178        let sqrt_sym = self.interner.intern("sqrt");
5179        let seq_sym = self.interner.intern("Seq");
5180        let real_sym = self.interner.intern("Real");
5181        let r = self.fresh_desugar_temp("l2_r");
5182        let base = self.fresh_desugar_temp("l2_b");
5183        let ss = self.fresh_desugar_temp("l2_ss");
5184        let p = self.fresh_desugar_temp("l2_p");
5185        let denom = self.fresh_desugar_temp("l2_d");
5186        let q = self.fresh_desugar_temp("l2_q");
5187        let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
5188            type_name: seq_sym,
5189            type_args: vec![TypeExpr::Named(real_sym)],
5190            init_fields: vec![],
5191        });
5192        // Σ_i X[base+i]²  for this row
5193        let xp = self.te_index(
5194            self.te_id(x),
5195            self.te_bin(BinaryOpKind::Add, self.te_id(base), self.te_id(p)),
5196        );
5197        let sq = self.te_bin(BinaryOpKind::Multiply, xp, xp);
5198        let ss_body = self.te_block(vec![
5199            Stmt::Set {
5200                target: ss,
5201                value: self.te_bin(BinaryOpKind::Add, self.te_id(ss), sq),
5202            },
5203            Stmt::Set {
5204                target: p,
5205                value: self.te_bin(BinaryOpKind::Add, self.te_id(p), self.te_int(1)),
5206            },
5207        ]);
5208        let ss_loop = Stmt::While {
5209            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(p), self.te_int(cols)),
5210            body: ss_body,
5211            decreasing: None,
5212        };
5213        let denom_expr = self.te_call(
5214            sqrt_sym,
5215            vec![self.te_bin(BinaryOpKind::Add, self.te_id(ss), self.te_float(eps))],
5216        );
5217        let xq = self.te_index(
5218            self.te_id(x),
5219            self.te_bin(BinaryOpKind::Add, self.te_id(base), self.te_id(q)),
5220        );
5221        let n_body = self.te_block(vec![
5222            Stmt::Push {
5223                value: self.te_bin(BinaryOpKind::Divide, xq, self.te_id(denom)),
5224                collection: self.te_id(var),
5225            },
5226            Stmt::Set {
5227                target: q,
5228                value: self.te_bin(BinaryOpKind::Add, self.te_id(q), self.te_int(1)),
5229            },
5230        ]);
5231        let n_loop = Stmt::While {
5232            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(q), self.te_int(cols)),
5233            body: n_body,
5234            decreasing: None,
5235        };
5236        let row_body = self.te_block(vec![
5237            Stmt::Let {
5238                var: base,
5239                ty: None,
5240                value: self.te_bin(
5241                    BinaryOpKind::Multiply,
5242                    self.te_bin(BinaryOpKind::Subtract, self.te_id(r), self.te_int(1)),
5243                    self.te_int(cols),
5244                ),
5245                mutable: false,
5246            },
5247            Stmt::Let {
5248                var: ss,
5249                ty: None,
5250                value: self.te_float(0.0),
5251                mutable: true,
5252            },
5253            Stmt::Let {
5254                var: p,
5255                ty: None,
5256                value: self.te_int(1),
5257                mutable: true,
5258            },
5259            ss_loop,
5260            Stmt::Let {
5261                var: denom,
5262                ty: None,
5263                value: denom_expr,
5264                mutable: false,
5265            },
5266            Stmt::Let {
5267                var: q,
5268                ty: None,
5269                value: self.te_int(1),
5270                mutable: true,
5271            },
5272            n_loop,
5273            Stmt::Set {
5274                target: r,
5275                value: self.te_bin(BinaryOpKind::Add, self.te_id(r), self.te_int(1)),
5276            },
5277        ]);
5278        let row_loop = Stmt::While {
5279            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(r), self.te_int(rows)),
5280            body: row_body,
5281            decreasing: None,
5282        };
5283        let body = self.te_block(vec![
5284            Stmt::Let {
5285                var,
5286                ty: None,
5287                value: new_seq,
5288                mutable: true,
5289            },
5290            Stmt::Let {
5291                var: r,
5292                ty: None,
5293                value: self.te_int(1),
5294                mutable: true,
5295            },
5296            row_loop,
5297        ]);
5298        Stmt::Splice { body }
5299    }
5300
5301    /// `Let Y be swiglu of Gate and Up` → elementwise `Y[i] = silu(Gate[i]) * Up[i]`, `silu(g) = g/(1 + e^-g)`,
5302    /// over all `total` entries — the gated MLP activation.
5303    fn desugar_tensor_swiglu(
5304        &mut self,
5305        var: Symbol,
5306        gate: Symbol,
5307        up: Symbol,
5308        total: i64,
5309    ) -> Stmt<'a> {
5310        const E: f64 = std::f64::consts::E;
5311        let seq_sym = self.interner.intern("Seq");
5312        let real_sym = self.interner.intern("Real");
5313        let i = self.fresh_desugar_temp("sg_i");
5314        let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
5315            type_name: seq_sym,
5316            type_args: vec![TypeExpr::Named(real_sym)],
5317            init_fields: vec![],
5318        });
5319        // silu(Gate[i]) = Gate[i] / (1 + e^(0 - Gate[i]))
5320        let g_num = self.te_index(self.te_id(gate), self.te_id(i));
5321        let g_den = self.te_index(self.te_id(gate), self.te_id(i));
5322        let neg = self.te_bin(BinaryOpKind::Subtract, self.te_float(0.0), g_den);
5323        let ex = self.te_bin(BinaryOpKind::Pow, self.te_float(E), neg);
5324        let denom = self.te_bin(BinaryOpKind::Add, self.te_float(1.0), ex);
5325        let silu = self.te_bin(BinaryOpKind::Divide, g_num, denom);
5326        let y = self.te_bin(
5327            BinaryOpKind::Multiply,
5328            silu,
5329            self.te_index(self.te_id(up), self.te_id(i)),
5330        );
5331        let body = self.te_block(vec![
5332            Stmt::Push {
5333                value: y,
5334                collection: self.te_id(var),
5335            },
5336            Stmt::Set {
5337                target: i,
5338                value: self.te_bin(BinaryOpKind::Add, self.te_id(i), self.te_int(1)),
5339            },
5340        ]);
5341        let loop_ = Stmt::While {
5342            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(i), self.te_int(total)),
5343            body,
5344            decreasing: None,
5345        };
5346        let outer = self.te_block(vec![
5347            Stmt::Let {
5348                var,
5349                ty: None,
5350                value: new_seq,
5351                mutable: true,
5352            },
5353            Stmt::Let {
5354                var: i,
5355                ty: None,
5356                value: self.te_int(1),
5357                mutable: true,
5358            },
5359            loop_,
5360        ]);
5361        Stmt::Splice { body: outer }
5362    }
5363
5364    /// `Let Y be causal conv of X with weight WC state CS over T taps` — the **depthwise causal conv1d** that
5365    /// Gated DeltaNet runs over its `q|k|v` projection (kernel 4 in Qwen3.8-27B).
5366    ///
5367    /// `X` is `1×C` (this token's channels), `WC` is `T×C` (an independent T-tap filter per channel — depthwise,
5368    /// so no cross-channel mixing), and `CS` is the `T×C` decode ring holding the last `T` inputs. Each step
5369    /// shifts `CS` up one row, writes `X` into the last row, and reduces `Y[c] = Σ_t CS[t,c]·WC[t,c]`. `CS` is
5370    /// read AND written — it is the layer's carried state, exactly like a KV cache but bounded at `T` rows.
5371    ///
5372    /// An `X` of `rows` rows (a prefill chunk's) is those steps row after row: row `r` enters the ring after rows
5373    /// `1 … r − 1` and its output is row `r` of `Y`. One row is the single step, statement for statement.
5374    #[allow(clippy::too_many_arguments)]
5375    fn desugar_tensor_causal_conv(
5376        &mut self,
5377        var: Symbol,
5378        x: Symbol,
5379        wc: Symbol,
5380        cs: Symbol,
5381        taps: i64,
5382        chan: i64,
5383        rows: i64,
5384    ) -> Stmt<'a> {
5385        let seq_sym = self.interner.intern("Seq");
5386        let real_sym = self.interner.intern("Real");
5387        let (t1, c1, c2, c3, t2) = (
5388            self.fresh_desugar_temp("cv_t1"),
5389            self.fresh_desugar_temp("cv_c1"),
5390            self.fresh_desugar_temp("cv_c2"),
5391            self.fresh_desugar_temp("cv_c3"),
5392            self.fresh_desugar_temp("cv_t2"),
5393        );
5394        let (acc, zi) = (
5395            self.fresh_desugar_temp("cv_acc"),
5396            self.fresh_desugar_temp("cv_zi"),
5397        );
5398        let row = (rows > 1).then(|| self.fresh_desugar_temp("cv_row"));
5399        let id = |sy: Symbol| self.te_id(sy);
5400        let fl = |v: f64| self.te_float(v);
5401        let it = |n: i64| self.te_int(n);
5402        let add = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Add, a, b);
5403        let sub = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Subtract, a, b);
5404        let mul = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Multiply, a, b);
5405        let item = |coll: Symbol, ix: &'a Expr<'a>| self.te_index(self.te_id(coll), ix);
5406        let nseq = || {
5407            self.ctx.alloc_imperative_expr(Expr::New {
5408                type_name: seq_sym,
5409                type_args: vec![TypeExpr::Named(real_sym)],
5410                init_fields: vec![],
5411            })
5412        };
5413        let incr = |sy: Symbol| Stmt::Set {
5414            target: sy,
5415            value: add(id(sy), it(1)),
5416        };
5417        let while_le = |vv: Symbol, bound: i64, body: Vec<Stmt<'a>>| Stmt::While {
5418            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(vv), self.te_int(bound)),
5419            body: self.te_block(body),
5420            decreasing: None,
5421        };
5422        // 1. shift the ring up one row: CS[t-1] <- CS[t] for t = 1..T-1
5423        let shift = while_le(
5424            t1,
5425            taps - 1,
5426            vec![
5427                Stmt::Let {
5428                    var: c1,
5429                    ty: None,
5430                    value: it(1),
5431                    mutable: true,
5432                },
5433                while_le(
5434                    c1,
5435                    chan,
5436                    vec![
5437                        Stmt::SetIndex {
5438                            collection: id(cs),
5439                            index: add(mul(sub(id(t1), it(1)), it(chan)), id(c1)),
5440                            value: item(cs, add(mul(id(t1), it(chan)), id(c1))),
5441                        },
5442                        incr(c1),
5443                    ],
5444                ),
5445                incr(t1),
5446            ],
5447        );
5448        // The block's row `r` sits at `(r − 1)·chan` of `X` and of `Y`.
5449        let in_row = |c: Symbol| match row {
5450            None => id(c),
5451            Some(r) => add(mul(sub(id(r), it(1)), it(chan)), id(c)),
5452        };
5453        // 2. this token becomes the newest row
5454        let append = while_le(
5455            c2,
5456            chan,
5457            vec![
5458                Stmt::SetIndex {
5459                    collection: id(cs),
5460                    index: add(it((taps - 1) * chan), id(c2)),
5461                    value: item(x, in_row(c2)),
5462                },
5463                incr(c2),
5464            ],
5465        );
5466        // 3. Y[c] = Σ_t CS[t,c]·WC[t,c]
5467        let reduce = while_le(
5468            c3,
5469            chan,
5470            vec![
5471                Stmt::Let {
5472                    var: acc,
5473                    ty: None,
5474                    value: fl(0.0),
5475                    mutable: true,
5476                },
5477                Stmt::Let {
5478                    var: t2,
5479                    ty: None,
5480                    value: it(1),
5481                    mutable: true,
5482                },
5483                while_le(
5484                    t2,
5485                    taps,
5486                    vec![
5487                        Stmt::Set {
5488                            target: acc,
5489                            value: add(
5490                                id(acc),
5491                                mul(
5492                                    item(cs, add(mul(sub(id(t2), it(1)), it(chan)), id(c3))),
5493                                    item(wc, add(mul(sub(id(t2), it(1)), it(chan)), id(c3))),
5494                                ),
5495                            ),
5496                        },
5497                        incr(t2),
5498                    ],
5499                ),
5500                Stmt::SetIndex {
5501                    collection: id(var),
5502                    index: in_row(c3),
5503                    value: id(acc),
5504                },
5505                incr(c3),
5506            ],
5507        );
5508        let step = vec![
5509            Stmt::Let {
5510                var: t1,
5511                ty: None,
5512                value: it(1),
5513                mutable: true,
5514            },
5515            shift,
5516            Stmt::Let {
5517                var: c2,
5518                ty: None,
5519                value: it(1),
5520                mutable: true,
5521            },
5522            append,
5523            Stmt::Let {
5524                var: c3,
5525                ty: None,
5526                value: it(1),
5527                mutable: true,
5528            },
5529            reduce,
5530        ];
5531        let mut body = vec![
5532            Stmt::Let {
5533                var,
5534                ty: None,
5535                value: nseq(),
5536                mutable: true,
5537            },
5538            Stmt::Let {
5539                var: zi,
5540                ty: None,
5541                value: it(1),
5542                mutable: true,
5543            },
5544            while_le(
5545                zi,
5546                chan * rows,
5547                vec![
5548                    Stmt::Push {
5549                        value: fl(0.0),
5550                        collection: id(var),
5551                    },
5552                    incr(zi),
5553                ],
5554            ),
5555        ];
5556        match row {
5557            None => body.extend(step),
5558            Some(r) => {
5559                body.push(Stmt::Let {
5560                    var: r,
5561                    ty: None,
5562                    value: it(1),
5563                    mutable: true,
5564                });
5565                body.push(while_le(
5566                    r,
5567                    rows,
5568                    step.into_iter().chain(std::iter::once(incr(r))).collect(),
5569                ));
5570            }
5571        }
5572        let body = self.te_block(body);
5573        Stmt::Splice { body }
5574    }
5575
5576    /// `Let C be full attention of Q over K and V with H heads` — **bidirectional** multi-head self-attention
5577    /// over `T` positions, the vision tower's block. `Q`/`K`/`V` are `T×(H·HD)` and `C` takes their shape.
5578    ///
5579    /// Distinct from `attention`, which is single-query decode against a growing KV cache: here every position
5580    /// attends to every other with no causal mask, which is what an image encoder needs. Softmax subtracts the
5581    /// row max before exponentiating, same as `attention`, so the two agree on shared shapes.
5582    fn desugar_tensor_full_attention(
5583        &mut self,
5584        var: Symbol,
5585        q: Symbol,
5586        k: Symbol,
5587        v: Symbol,
5588        h: i64,
5589        hd: i64,
5590        t: i64,
5591    ) -> Stmt<'a> {
5592        const EU: f64 = std::f64::consts::E;
5593        let scale = 1.0f64 / (hd as f64).sqrt();
5594        let width = h * hd;
5595        let seq_sym = self.interner.intern("Seq");
5596        let real_sym = self.interner.intern("Real");
5597        let max_sym = self.interner.intern("max");
5598        let (row, head, scores, ozi) = (
5599            self.fresh_desugar_temp("fa_row"),
5600            self.fresh_desugar_temp("fa_head"),
5601            self.fresh_desugar_temp("fa_sc"),
5602            self.fresh_desugar_temp("fa_ozi"),
5603        );
5604        let (j_sc, e_sc, acc_sc) = (
5605            self.fresh_desugar_temp("fa_jsc"),
5606            self.fresh_desugar_temp("fa_esc"),
5607            self.fresh_desugar_temp("fa_asc"),
5608        );
5609        let (mx, si_mx, si_dn, den) = (
5610            self.fresh_desugar_temp("fa_mx"),
5611            self.fresh_desugar_temp("fa_simx"),
5612            self.fresh_desugar_temp("fa_sidn"),
5613            self.fresh_desugar_temp("fa_den"),
5614        );
5615        let (e_wv, j_wv, acc_wv) = (
5616            self.fresh_desugar_temp("fa_ewv"),
5617            self.fresh_desugar_temp("fa_jwv"),
5618            self.fresh_desugar_temp("fa_awv"),
5619        );
5620        let id = |sy: Symbol| self.te_id(sy);
5621        let fl = |val: f64| self.te_float(val);
5622        let it = |n: i64| self.te_int(n);
5623        let add = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Add, a, b);
5624        let sub = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Subtract, a, b);
5625        let mul = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Multiply, a, b);
5626        let dv = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Divide, a, b);
5627        let item = |coll: Symbol, ix: &'a Expr<'a>| self.te_index(self.te_id(coll), ix);
5628        let nseq = || {
5629            self.ctx.alloc_imperative_expr(Expr::New {
5630                type_name: seq_sym,
5631                type_args: vec![TypeExpr::Named(real_sym)],
5632                init_fields: vec![],
5633            })
5634        };
5635        let expo = |arg: &'a Expr<'a>| self.te_bin(BinaryOpKind::Pow, self.te_float(EU), arg);
5636        let incr = |sy: Symbol| Stmt::Set {
5637            target: sy,
5638            value: add(id(sy), it(1)),
5639        };
5640        let while_le = |vv: Symbol, bound: i64, body: Vec<Stmt<'a>>| Stmt::While {
5641            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(vv), self.te_int(bound)),
5642            body: self.te_block(body),
5643            decreasing: None,
5644        };
5645        // row-major 1-based indices into a T×(H·HD) tensor
5646        let hbase = mul(sub(id(head), it(1)), it(hd));
5647        let q_read = add(add(mul(sub(id(row), it(1)), it(width)), hbase), id(e_sc));
5648        let k_read = add(
5649            add(
5650                mul(sub(id(j_sc), it(1)), it(width)),
5651                mul(sub(id(head), it(1)), it(hd)),
5652            ),
5653            id(e_sc),
5654        );
5655        let v_read = add(
5656            add(
5657                mul(sub(id(j_wv), it(1)), it(width)),
5658                mul(sub(id(head), it(1)), it(hd)),
5659            ),
5660            id(e_wv),
5661        );
5662        let c_write = add(
5663            add(
5664                mul(sub(id(row), it(1)), it(width)),
5665                mul(sub(id(head), it(1)), it(hd)),
5666            ),
5667            id(e_wv),
5668        );
5669        let score_loop = while_le(
5670            j_sc,
5671            t,
5672            vec![
5673                Stmt::Let {
5674                    var: acc_sc,
5675                    ty: None,
5676                    value: fl(0.0),
5677                    mutable: true,
5678                },
5679                Stmt::Let {
5680                    var: e_sc,
5681                    ty: None,
5682                    value: it(1),
5683                    mutable: true,
5684                },
5685                while_le(
5686                    e_sc,
5687                    hd,
5688                    vec![
5689                        Stmt::Set {
5690                            target: acc_sc,
5691                            value: add(id(acc_sc), mul(item(q, q_read), item(k, k_read))),
5692                        },
5693                        incr(e_sc),
5694                    ],
5695                ),
5696                Stmt::Push {
5697                    value: mul(id(acc_sc), fl(scale)),
5698                    collection: id(scores),
5699                },
5700                incr(j_sc),
5701            ],
5702        );
5703        let max_loop = while_le(
5704            si_mx,
5705            t,
5706            vec![
5707                Stmt::Set {
5708                    target: mx,
5709                    value: self.te_call(max_sym, vec![id(mx), item(scores, id(si_mx))]),
5710                },
5711                incr(si_mx),
5712            ],
5713        );
5714        let den_loop = while_le(
5715            si_dn,
5716            t,
5717            vec![
5718                Stmt::Set {
5719                    target: den,
5720                    value: add(id(den), expo(sub(item(scores, id(si_dn)), id(mx)))),
5721                },
5722                incr(si_dn),
5723            ],
5724        );
5725        let wv_loop = while_le(
5726            e_wv,
5727            hd,
5728            vec![
5729                Stmt::Let {
5730                    var: acc_wv,
5731                    ty: None,
5732                    value: fl(0.0),
5733                    mutable: true,
5734                },
5735                Stmt::Let {
5736                    var: j_wv,
5737                    ty: None,
5738                    value: it(1),
5739                    mutable: true,
5740                },
5741                while_le(
5742                    j_wv,
5743                    t,
5744                    vec![
5745                        Stmt::Set {
5746                            target: acc_wv,
5747                            value: add(
5748                                id(acc_wv),
5749                                mul(
5750                                    dv(expo(sub(item(scores, id(j_wv)), id(mx))), id(den)),
5751                                    item(v, v_read),
5752                                ),
5753                            ),
5754                        },
5755                        incr(j_wv),
5756                    ],
5757                ),
5758                Stmt::SetIndex {
5759                    collection: id(var),
5760                    index: c_write,
5761                    value: id(acc_wv),
5762                },
5763                incr(e_wv),
5764            ],
5765        );
5766        let head_loop = while_le(
5767            head,
5768            h,
5769            vec![
5770                Stmt::Let {
5771                    var: scores,
5772                    ty: None,
5773                    value: nseq(),
5774                    mutable: true,
5775                },
5776                Stmt::Let {
5777                    var: j_sc,
5778                    ty: None,
5779                    value: it(1),
5780                    mutable: true,
5781                },
5782                score_loop,
5783                Stmt::Let {
5784                    var: mx,
5785                    ty: None,
5786                    value: item(scores, it(1)),
5787                    mutable: true,
5788                },
5789                Stmt::Let {
5790                    var: si_mx,
5791                    ty: None,
5792                    value: it(2),
5793                    mutable: true,
5794                },
5795                max_loop,
5796                Stmt::Let {
5797                    var: den,
5798                    ty: None,
5799                    value: fl(0.0),
5800                    mutable: true,
5801                },
5802                Stmt::Let {
5803                    var: si_dn,
5804                    ty: None,
5805                    value: it(1),
5806                    mutable: true,
5807                },
5808                den_loop,
5809                Stmt::Let {
5810                    var: e_wv,
5811                    ty: None,
5812                    value: it(1),
5813                    mutable: true,
5814                },
5815                wv_loop,
5816                incr(head),
5817            ],
5818        );
5819        let body = self.te_block(vec![
5820            Stmt::Let {
5821                var,
5822                ty: None,
5823                value: nseq(),
5824                mutable: true,
5825            },
5826            Stmt::Let {
5827                var: ozi,
5828                ty: None,
5829                value: it(1),
5830                mutable: true,
5831            },
5832            while_le(
5833                ozi,
5834                t * width,
5835                vec![
5836                    Stmt::Push {
5837                        value: fl(0.0),
5838                        collection: id(var),
5839                    },
5840                    incr(ozi),
5841                ],
5842            ),
5843            Stmt::Let {
5844                var: row,
5845                ty: None,
5846                value: it(1),
5847                mutable: true,
5848            },
5849            while_le(
5850                row,
5851                t,
5852                vec![
5853                    Stmt::Let {
5854                        var: head,
5855                        ty: None,
5856                        value: it(1),
5857                        mutable: true,
5858                    },
5859                    head_loop,
5860                    incr(row),
5861                ],
5862            ),
5863        ]);
5864        Stmt::Splice { body }
5865    }
5866
5867    /// `Let O be gated delta of Q over K and V with gate Z beta B decay A state S norm GN with HV heads HK groups`
5868    /// — the **Gated DeltaNet** step, the mechanism behind 48 of Qwen3.8-27B's 64 layers.
5869    ///
5870    /// Linear attention with a *delta rule*: instead of a KV cache that grows with the sequence, each value head
5871    /// carries a fixed `D×D` associative memory `S` and updates it in place. Per value head `h` (reading key
5872    /// head `h mod HK` — the keys are TILED across the value heads, not repeat-interleaved), with decay
5873    /// `α = A[h]` and write strength `β = B[h]`:
5874    ///
5875    /// ```text
5876    ///   u = S·k                       what the memory currently predicts for this key
5877    ///   S = α·S + β·(v − α·u)·kᵀ      decay, then correct by the prediction error (the delta rule)
5878    ///   o = S·q                       read the UPDATED memory
5879    /// ```
5880    ///
5881    /// Then the head is RMSNorm'd and scaled by `GN`, and only THEN gated by `silu(z)` — the order
5882    /// llama.cpp's `build_norm_gated` uses for this architecture. (Gating first gives different numbers; HF's
5883    /// Qwen3-Next does gate first, but Qwen3.5 — which this checkpoint is — does not.) `with sigmoid gate Z` gates by
5884    /// `σ(z)` instead, as Qwen3.8-Flash-Next does.
5885    ///
5886    /// Shapes: `Q`,`K` are `1×(HK·D)`; `V`,`Z` are `1×(HV·D)`; `B`,`A` are `1×HV`; `GN` is `1×D`; and `S` is
5887    /// `(HV·D)×D`, head `h`'s memory living in rows `(h-1)·D+1 ..= h·D`. `O` is `1×(HV·D)`.
5888    ///
5889    /// `S` is read AND written, which is the whole point: decode cost is constant in sequence length, and this
5890    /// is why Qwen3.8 caches only 16 of its 64 layers. Desugars to plain LOGOS loops, so every tier runs it and
5891    /// the accelerators recognize the block to emit one fused kernel that loads the memory once.
5892    #[allow(clippy::too_many_arguments)]
5893    fn desugar_tensor_gated_delta(
5894        &mut self,
5895        var: Symbol,
5896        q: Symbol,
5897        k: Symbol,
5898        v: Symbol,
5899        z: Symbol,
5900        b: Symbol,
5901        a: Symbol,
5902        st: Symbol,
5903        gn: Symbol,
5904        hv: i64,
5905        hk: i64,
5906        d: i64,
5907        rows: i64,
5908        eps: f64,
5909        sigmoid_gate: bool,
5910    ) -> Stmt<'a> {
5911        const EU: f64 = std::f64::consts::E;
5912        let seq_sym = self.interner.intern("Seq");
5913        let real_sym = self.interner.intern("Real");
5914        let sqrt_sym = self.interner.intern("sqrt");
5915        let (head, kb, vb, sb, alpha, beta) = (
5916            self.fresh_desugar_temp("gd_h"),
5917            self.fresh_desugar_temp("gd_kb"),
5918            self.fresh_desugar_temp("gd_vb"),
5919            self.fresh_desugar_temp("gd_sb"),
5920            self.fresh_desugar_temp("gd_al"),
5921            self.fresh_desugar_temp("gd_be"),
5922        );
5923        let (uvec, ua, ub, uacc) = (
5924            self.fresh_desugar_temp("gd_U"),
5925            self.fresh_desugar_temp("gd_ua"),
5926            self.fresh_desugar_temp("gd_ub"),
5927            self.fresh_desugar_temp("gd_uacc"),
5928        );
5929        let (sa, sbi, coef) = (
5930            self.fresh_desugar_temp("gd_sa"),
5931            self.fresh_desugar_temp("gd_sbi"),
5932            self.fresh_desugar_temp("gd_coef"),
5933        );
5934        let (ovec, oa, ob, oacc) = (
5935            self.fresh_desugar_temp("gd_O"),
5936            self.fresh_desugar_temp("gd_oa"),
5937            self.fresh_desugar_temp("gd_ob"),
5938            self.fresh_desugar_temp("gd_oacc"),
5939        );
5940        let (ga, ssq, den, na, zi) = (
5941            self.fresh_desugar_temp("gd_ga"),
5942            self.fresh_desugar_temp("gd_ssq"),
5943            self.fresh_desugar_temp("gd_den"),
5944            self.fresh_desugar_temp("gd_na"),
5945            self.fresh_desugar_temp("gd_zi"),
5946        );
5947        // A block of rows runs the step row after row: row `r`'s token sits at `(r − 1)·HK·D` of `Q` and `K`,
5948        // `(r − 1)·HV·D` of `V`, `Z` and `O`, and `(r − 1)·HV` of `B` and `A`; the memory is the one they all update.
5949        let row = (rows > 1).then(|| {
5950            (
5951                self.fresh_desugar_temp("gd_row"),
5952                self.fresh_desugar_temp("gd_rq"),
5953                self.fresh_desugar_temp("gd_rv"),
5954                self.fresh_desugar_temp("gd_rh"),
5955            )
5956        });
5957        let id = |sy: Symbol| self.te_id(sy);
5958        let fl = |val: f64| self.te_float(val);
5959        let it = |n: i64| self.te_int(n);
5960        let add = |x: &'a Expr<'a>, y: &'a Expr<'a>| self.te_bin(BinaryOpKind::Add, x, y);
5961        let sub = |x: &'a Expr<'a>, y: &'a Expr<'a>| self.te_bin(BinaryOpKind::Subtract, x, y);
5962        let mul = |x: &'a Expr<'a>, y: &'a Expr<'a>| self.te_bin(BinaryOpKind::Multiply, x, y);
5963        let dv = |x: &'a Expr<'a>, y: &'a Expr<'a>| self.te_bin(BinaryOpKind::Divide, x, y);
5964        let item = |coll: Symbol, ix: &'a Expr<'a>| self.te_index(self.te_id(coll), ix);
5965        let nseq = || {
5966            self.ctx.alloc_imperative_expr(Expr::New {
5967                type_name: seq_sym,
5968                type_args: vec![TypeExpr::Named(real_sym)],
5969                init_fields: vec![],
5970            })
5971        };
5972        let expo = |arg: &'a Expr<'a>| self.te_bin(BinaryOpKind::Pow, self.te_float(EU), arg);
5973        let gate = |g: &'a Expr<'a>| dv(if sigmoid_gate { fl(1.0) } else { g }, add(fl(1.0), expo(sub(fl(0.0), g))));
5974        let incr = |sy: Symbol| Stmt::Set {
5975            target: sy,
5976            value: add(id(sy), it(1)),
5977        };
5978        let while_le = |vv: Symbol, bound: i64, body: Vec<Stmt<'a>>| Stmt::While {
5979            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(vv), self.te_int(bound)),
5980            body: self.te_block(body),
5981            decreasing: None,
5982        };
5983        // S[h][r][c] is flat item  sb + (r-1)·D + c,  where sb = (h-1)·D·D.
5984        let s_at =
5985            |r: &'a Expr<'a>, c: &'a Expr<'a>| add(add(id(sb), mul(sub(r, it(1)), it(d))), c);
5986
5987        // 1. u = S·k, read from the memory BEFORE it is updated.
5988        let u_loop = while_le(
5989            ua,
5990            d,
5991            vec![
5992                Stmt::Let {
5993                    var: uacc,
5994                    ty: None,
5995                    value: fl(0.0),
5996                    mutable: true,
5997                },
5998                Stmt::Let {
5999                    var: ub,
6000                    ty: None,
6001                    value: it(1),
6002                    mutable: true,
6003                },
6004                while_le(
6005                    ub,
6006                    d,
6007                    vec![
6008                        Stmt::Set {
6009                            target: uacc,
6010                            value: add(
6011                                id(uacc),
6012                                mul(item(st, s_at(id(ua), id(ub))), item(k, add(id(kb), id(ub)))),
6013                            ),
6014                        },
6015                        incr(ub),
6016                    ],
6017                ),
6018                Stmt::Push {
6019                    value: id(uacc),
6020                    collection: id(uvec),
6021                },
6022                incr(ua),
6023            ],
6024        );
6025        // 2. S = α·S + β·(v − α·u)·kᵀ — a decay plus a rank-1 correction by the prediction error.
6026        let s_loop = while_le(
6027            sa,
6028            d,
6029            vec![
6030                Stmt::Let {
6031                    var: coef,
6032                    ty: None,
6033                    value: mul(
6034                        id(beta),
6035                        sub(
6036                            item(v, add(id(vb), id(sa))),
6037                            mul(id(alpha), item(uvec, id(sa))),
6038                        ),
6039                    ),
6040                    mutable: false,
6041                },
6042                Stmt::Let {
6043                    var: sbi,
6044                    ty: None,
6045                    value: it(1),
6046                    mutable: true,
6047                },
6048                while_le(
6049                    sbi,
6050                    d,
6051                    vec![
6052                        Stmt::SetIndex {
6053                            collection: id(st),
6054                            index: s_at(id(sa), id(sbi)),
6055                            value: add(
6056                                mul(id(alpha), item(st, s_at(id(sa), id(sbi)))),
6057                                mul(id(coef), item(k, add(id(kb), id(sbi)))),
6058                            ),
6059                        },
6060                        incr(sbi),
6061                    ],
6062                ),
6063                incr(sa),
6064            ],
6065        );
6066        // 3. o = S·q, read from the memory AFTER the update.
6067        let o_loop = while_le(
6068            oa,
6069            d,
6070            vec![
6071                Stmt::Let {
6072                    var: oacc,
6073                    ty: None,
6074                    value: fl(0.0),
6075                    mutable: true,
6076                },
6077                Stmt::Let {
6078                    var: ob,
6079                    ty: None,
6080                    value: it(1),
6081                    mutable: true,
6082                },
6083                while_le(
6084                    ob,
6085                    d,
6086                    vec![
6087                        Stmt::Set {
6088                            target: oacc,
6089                            value: add(
6090                                id(oacc),
6091                                mul(item(st, s_at(id(oa), id(ob))), item(q, add(id(kb), id(ob)))),
6092                            ),
6093                        },
6094                        incr(ob),
6095                    ],
6096                ),
6097                Stmt::Push {
6098                    value: mul(id(oacc), fl(1.0 / (d as f64).sqrt())),
6099                    collection: id(ovec),
6100                },
6101                incr(oa),
6102            ],
6103        );
6104        // 4. sum of squares of the UNGATED head — the norm is taken before the gate …
6105        let gate_loop = while_le(
6106            ga,
6107            d,
6108            vec![
6109                Stmt::Set {
6110                    target: ssq,
6111                    value: add(id(ssq), mul(item(ovec, id(ga)), item(ovec, id(ga)))),
6112                },
6113                incr(ga),
6114            ],
6115        );
6116        // … then RMSNorm, scale by the shared per-dim weight, and gate by silu(z) (or σ(z)).
6117        let norm_loop = while_le(
6118            na,
6119            d,
6120            vec![
6121                Stmt::SetIndex {
6122                    collection: id(var),
6123                    index: add(id(vb), id(na)),
6124                    value: mul(
6125                        mul(item(gn, id(na)), dv(item(ovec, id(na)), id(den))),
6126                        gate(item(z, add(id(vb), id(na)))),
6127                    ),
6128                },
6129                incr(na),
6130            ],
6131        );
6132        let key_base = mul(
6133            self.te_bin(BinaryOpKind::Modulo, sub(id(head), it(1)), it(hk)),
6134            it(d),
6135        );
6136        let value_base = mul(sub(id(head), it(1)), it(d));
6137        let (key_base, value_base, head_at) = match row {
6138            None => (key_base, value_base, id(head)),
6139            Some((_, rq, rv, rh)) => (
6140                add(id(rq), key_base),
6141                add(id(rv), value_base),
6142                add(id(rh), id(head)),
6143            ),
6144        };
6145        let head_loop = while_le(
6146            head,
6147            hv,
6148            vec![
6149                Stmt::Let {
6150                    var: kb,
6151                    ty: None,
6152                    value: key_base,
6153                    mutable: false,
6154                },
6155                Stmt::Let {
6156                    var: vb,
6157                    ty: None,
6158                    value: value_base,
6159                    mutable: false,
6160                },
6161                Stmt::Let {
6162                    var: sb,
6163                    ty: None,
6164                    value: mul(sub(id(head), it(1)), it(d * d)),
6165                    mutable: false,
6166                },
6167                Stmt::Let {
6168                    var: alpha,
6169                    ty: None,
6170                    value: item(a, head_at),
6171                    mutable: false,
6172                },
6173                Stmt::Let {
6174                    var: beta,
6175                    ty: None,
6176                    value: item(b, head_at),
6177                    mutable: false,
6178                },
6179                Stmt::Let {
6180                    var: uvec,
6181                    ty: None,
6182                    value: nseq(),
6183                    mutable: true,
6184                },
6185                Stmt::Let {
6186                    var: ua,
6187                    ty: None,
6188                    value: it(1),
6189                    mutable: true,
6190                },
6191                u_loop,
6192                Stmt::Let {
6193                    var: sa,
6194                    ty: None,
6195                    value: it(1),
6196                    mutable: true,
6197                },
6198                s_loop,
6199                Stmt::Let {
6200                    var: ovec,
6201                    ty: None,
6202                    value: nseq(),
6203                    mutable: true,
6204                },
6205                Stmt::Let {
6206                    var: oa,
6207                    ty: None,
6208                    value: it(1),
6209                    mutable: true,
6210                },
6211                o_loop,
6212                Stmt::Let {
6213                    var: ssq,
6214                    ty: None,
6215                    value: fl(0.0),
6216                    mutable: true,
6217                },
6218                Stmt::Let {
6219                    var: ga,
6220                    ty: None,
6221                    value: it(1),
6222                    mutable: true,
6223                },
6224                gate_loop,
6225                Stmt::Let {
6226                    var: den,
6227                    ty: None,
6228                    value: self.te_call(sqrt_sym, vec![add(dv(id(ssq), fl(d as f64)), fl(eps))]),
6229                    mutable: false,
6230                },
6231                Stmt::Let {
6232                    var: na,
6233                    ty: None,
6234                    value: it(1),
6235                    mutable: true,
6236                },
6237                norm_loop,
6238                incr(head),
6239            ],
6240        );
6241        let mut body = vec![
6242            Stmt::Let {
6243                var,
6244                ty: None,
6245                value: nseq(),
6246                mutable: true,
6247            },
6248            Stmt::Let {
6249                var: zi,
6250                ty: None,
6251                value: it(1),
6252                mutable: true,
6253            },
6254            while_le(
6255                zi,
6256                hv * d * rows,
6257                vec![
6258                    Stmt::Push {
6259                        value: fl(0.0),
6260                        collection: id(var),
6261                    },
6262                    incr(zi),
6263                ],
6264            ),
6265        ];
6266        match row {
6267            None => body.extend([
6268                Stmt::Let {
6269                    var: head,
6270                    ty: None,
6271                    value: it(1),
6272                    mutable: true,
6273                },
6274                head_loop,
6275            ]),
6276            Some((r, rq, rv, rh)) => {
6277                let offset = |per: i64| mul(sub(id(r), it(1)), it(per));
6278                body.push(Stmt::Let {
6279                    var: r,
6280                    ty: None,
6281                    value: it(1),
6282                    mutable: true,
6283                });
6284                body.push(while_le(
6285                    r,
6286                    rows,
6287                    vec![
6288                        Stmt::Let {
6289                            var: rq,
6290                            ty: None,
6291                            value: offset(hk * d),
6292                            mutable: false,
6293                        },
6294                        Stmt::Let {
6295                            var: rv,
6296                            ty: None,
6297                            value: offset(hv * d),
6298                            mutable: false,
6299                        },
6300                        Stmt::Let {
6301                            var: rh,
6302                            ty: None,
6303                            value: offset(hv),
6304                            mutable: false,
6305                        },
6306                        Stmt::Let {
6307                            var: head,
6308                            ty: None,
6309                            value: it(1),
6310                            mutable: true,
6311                        },
6312                        head_loop,
6313                        incr(r),
6314                    ],
6315                ));
6316            }
6317        }
6318        let body = self.te_block(body);
6319        Stmt::Splice { body }
6320    }
6321
6322    /// `Let H be moe of X with router Wr gate Wg up Wu down Wd choosing K of E` — a **Mixture-of-Experts FFN**,
6323    /// the Qwen3-30B-A3B block. `X` is `1×d`; `Wr` is the router `d×E` (logits per expert); `Wg`/`Wu` are the
6324    /// `E` experts' gate/up weights STACKED row-major (`(E·dff)×d`, expert `e`'s rows at `e·dff`); `Wd` is the
6325    /// `E` down weights stacked (`(E·d)×dff`). The block routes `X` to its top-`K` experts — softmax over the
6326    /// router logits, keep the `K` largest, renormalize — runs each selected expert's SwiGLU
6327    /// (`down·(silu(gate·x) ⊙ up·x)`) and combines by the renormalized router weight. `by sigmoid` scores each expert
6328    /// by its logit's sigmoid in place of the softmax; `biased by B` chooses by each score plus `B[e]` and still weighs
6329    /// by the score; `within the top G of N groups` scores each of `N` groups of `E/N` experts by its two best selection
6330    /// scores and chooses only among the top `G` groups' experts; the renormalizing sum is clamped at 2⁻¹⁴, as
6331    /// llama.cpp's `build_moe_ffn` clamps it (a softmax's kept weights sum past `K/E`, so only a sigmoid's can reach
6332    /// it); `scaled by S` multiplies every kept weight by `S` after; `weighting the inputs` scales each kept expert's
6333    /// input by its weight — its gate and up are the weight times its unweighted products — and adds its output
6334    /// unweighted, a selection `S[e]` (one for a kept expert, zero for the rest) in the weight's place. GENERAL in
6335    /// `d, dff, E, K`
6336    /// and in the weight SIZE (the experts are indexed, not unrolled). Desugars to plain LOGOS loops so the VM
6337    /// runs it directly, the whole-program fabric engine lowers the same routing as data-dependent dataflow,
6338    /// and the CUDA orchestrator recognizes the block to emit grouped-expert kernels. (Every expert is computed
6339    /// and weighted by its router weight — `0` for the non-selected — so the routing is a data-dependent mask;
6340    /// the sparse top-`K` gather is the throughput specialization the emitters apply.)
6341    fn desugar_tensor_moe(
6342        &mut self,
6343        var: Symbol,
6344        x: Symbol,
6345        wr: Symbol,
6346        wg: Symbol,
6347        wu: Symbol,
6348        wd: Symbol,
6349        d: i64,
6350        dff: i64,
6351        ne: i64,
6352        k: i64,
6353        opts: MoeOptions,
6354    ) -> Stmt<'a> {
6355        const EU: f64 = std::f64::consts::E;
6356        let seq_sym = self.interner.intern("Seq");
6357        let real_sym = self.interner.intern("Real");
6358        let max_sym = self.interner.intern("max");
6359        let min_sym = self.interner.intern("min");
6360        // ── loop vars + accumulators (allocated once; each `Let` below is fresh per its scope) ──
6361        let (l_seq, p_seq, pm_seq, w_seq) = (
6362            self.fresh_desugar_temp("mo_L"),
6363            self.fresh_desugar_temp("mo_P"),
6364            self.fresh_desugar_temp("mo_Pm"),
6365            self.fresh_desugar_temp("mo_W"),
6366        );
6367        let (er, accr, tr) = (
6368            self.fresh_desugar_temp("mo_er"),
6369            self.fresh_desugar_temp("mo_accr"),
6370            self.fresh_desugar_temp("mo_tr"),
6371        );
6372        let (smm, smi1, sms, smi2, smi3) = (
6373            self.fresh_desugar_temp("mo_m"),
6374            self.fresh_desugar_temp("mo_mi"),
6375            self.fresh_desugar_temp("mo_s"),
6376            self.fresh_desugar_temp("mo_si"),
6377            self.fresh_desugar_temp("mo_ni"),
6378        );
6379        // Minted only when the clause asks for it: every other mixture keeps its temporaries' names.
6380        let s_seq = opts.weigh_inputs.then(|| self.fresh_desugar_temp("mo_S"));
6381        let group_temps = opts.groups.map(|_| {
6382            [
6383                "mo_Ps", "mo_Gs", "mo_gi", "mo_gj", "mo_t1", "mo_t2", "mo_gv", "mo_pi", "mo_gr", "mo_gbv", "mo_gbi", "mo_gsi",
6384                "mo_gk",
6385            ]
6386            .map(|tag| self.fresh_desugar_temp(tag))
6387        });
6388        let scale_i = opts.scale.map(|_| self.fresh_desugar_temp("mo_sc"));
6389        let (pci, wzi, wsum, kk, bv, bi, sci, rni) = (
6390            self.fresh_desugar_temp("mo_pci"),
6391            self.fresh_desugar_temp("mo_wzi"),
6392            self.fresh_desugar_temp("mo_wsum"),
6393            self.fresh_desugar_temp("mo_kk"),
6394            self.fresh_desugar_temp("mo_bv"),
6395            self.fresh_desugar_temp("mo_bi"),
6396            self.fresh_desugar_temp("mo_sci"),
6397            self.fresh_desugar_temp("mo_rni"),
6398        );
6399        let (ozi, ee, gate, up) = (
6400            self.fresh_desugar_temp("mo_ozi"),
6401            self.fresh_desugar_temp("mo_ee"),
6402            self.fresh_desugar_temp("mo_gate"),
6403            self.fresh_desugar_temp("mo_up"),
6404        );
6405        let (gig, trg, gacc, giu, tru, uacc) = (
6406            self.fresh_desugar_temp("mo_gig"),
6407            self.fresh_desugar_temp("mo_trg"),
6408            self.fresh_desugar_temp("mo_gacc"),
6409            self.fresh_desugar_temp("mo_giu"),
6410            self.fresh_desugar_temp("mo_tru"),
6411            self.fresh_desugar_temp("mo_uacc"),
6412        );
6413        let (oo, di, dacc) = (
6414            self.fresh_desugar_temp("mo_oo"),
6415            self.fresh_desugar_temp("mo_di"),
6416            self.fresh_desugar_temp("mo_dacc"),
6417        );
6418
6419        // ── expression builders (all &self / arena-allocating; freely composable) ──
6420        let id = |s: Symbol| self.te_id(s);
6421        let fl = |v: f64| self.te_float(v);
6422        let it = |n: i64| self.te_int(n);
6423        let add = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Add, a, b);
6424        let sub = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Subtract, a, b);
6425        let mul = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Multiply, a, b);
6426        let dv = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Divide, a, b);
6427        let item = |coll: Symbol, ix: &'a Expr<'a>| self.te_index(self.te_id(coll), ix);
6428        let nseq = || {
6429            self.ctx.alloc_imperative_expr(Expr::New {
6430                type_name: seq_sym,
6431                type_args: vec![TypeExpr::Named(real_sym)],
6432                init_fields: vec![],
6433            })
6434        };
6435        let expo = |arg: &'a Expr<'a>| self.te_bin(BinaryOpKind::Pow, self.te_float(EU), arg);
6436        // silu(g) = g / (1 + e^(0 - g))
6437        let silu = |g: &'a Expr<'a>| dv(g, add(fl(1.0), expo(sub(fl(0.0), g))));
6438        let incr = |s: Symbol| Stmt::Set {
6439            target: s,
6440            value: add(id(s), it(1)),
6441        };
6442        let while_le = |vv: Symbol, bound: i64, body: Vec<Stmt<'a>>| Stmt::While {
6443            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(vv), self.te_int(bound)),
6444            body: self.te_block(body),
6445            decreasing: None,
6446        };
6447
6448        // ── 1. Router logits: L[e] = Σ_{t=1..d} X[t]·Wr[(t-1)·E + e] ──
6449        let wr_ix = add(mul(sub(id(tr), it(1)), it(ne)), id(er)); // (tr-1)*E + er
6450        let router_inner = while_le(
6451            tr,
6452            d,
6453            vec![
6454                Stmt::Set {
6455                    target: accr,
6456                    value: add(id(accr), mul(item(x, id(tr)), item(wr, wr_ix))),
6457                },
6458                incr(tr),
6459            ],
6460        );
6461        let router_loop = while_le(
6462            er,
6463            ne,
6464            vec![
6465                Stmt::Let {
6466                    var: accr,
6467                    ty: None,
6468                    value: fl(0.0),
6469                    mutable: true,
6470                },
6471                Stmt::Let {
6472                    var: tr,
6473                    ty: None,
6474                    value: it(1),
6475                    mutable: true,
6476                },
6477                router_inner,
6478                Stmt::Push {
6479                    value: match opts.router_bias {
6480                        Some(br) => add(id(accr), item(br, id(er))),
6481                        None => id(accr),
6482                    },
6483                    collection: id(l_seq),
6484                },
6485                incr(er),
6486            ],
6487        );
6488
6489        // ── 2. Softmax over the E router logits → P ──
6490        let max_loop = while_le(
6491            smi1,
6492            ne,
6493            vec![
6494                Stmt::Set {
6495                    target: smm,
6496                    value: self.te_call(max_sym, vec![id(smm), item(l_seq, id(smi1))]),
6497                },
6498                incr(smi1),
6499            ],
6500        );
6501        let sum_loop = while_le(
6502            smi2,
6503            ne,
6504            vec![
6505                Stmt::Set {
6506                    target: sms,
6507                    value: add(id(sms), expo(sub(item(l_seq, id(smi2)), id(smm)))),
6508                },
6509                incr(smi2),
6510            ],
6511        );
6512        let norm_loop = while_le(
6513            smi3,
6514            ne,
6515            vec![
6516                Stmt::Push {
6517                    value: match opts.score {
6518                        MoeScore::Softmax => dv(expo(sub(item(l_seq, id(smi3)), id(smm))), id(sms)),
6519                        // σ(L[e]) = 1 / (1 + e^(0 − L[e]))
6520                        MoeScore::Sigmoid => dv(fl(1.0), add(fl(1.0), expo(sub(fl(0.0), item(l_seq, id(smi3)))))),
6521                    },
6522                    collection: id(p_seq),
6523                },
6524                incr(smi3),
6525            ],
6526        );
6527
6528        // ── 3. Top-K: the selection scores P (+ B) → Pm, zero W, then K rounds of masked argmax; renormalize the kept
6529        //    weights over their sum clamped at 2⁻¹⁴, then scale them ──
6530        let sel_seq = group_temps.map_or(pm_seq, |t| t[0]);
6531        let pm_copy = while_le(
6532            pci,
6533            ne,
6534            vec![
6535                Stmt::Push {
6536                    value: match opts.selection_bias {
6537                        Some(b) => add(item(p_seq, id(pci)), item(b, id(pci))),
6538                        None => item(p_seq, id(pci)),
6539                    },
6540                    collection: id(sel_seq),
6541                },
6542                incr(pci),
6543            ],
6544        );
6545        // ── 3a. The group limit: each group's score is the sum of its two best selection scores; `G` rounds choose the
6546        //    best group left (marked −2e30, below the scan's −1e30 seed, so never chosen twice) and copy its experts'
6547        //    selection scores into Pm, which starts at −1e30 — an expert of a group not chosen is never picked ──
6548        let group_block = group_temps.map(|[ps, gs, gi, gj, t1, t2, gv, pi, gr, gbv, gbi, gsi, gk]| {
6549            let (kept, count) = opts.groups.expect("the group temporaries are minted for a group limit");
6550            let per = ne / count;
6551            let at = |g: Symbol, j: Symbol| add(mul(sub(id(g), it(1)), it(per)), id(j));
6552            let two_best = while_le(
6553                gj,
6554                per,
6555                vec![
6556                    Stmt::Let {
6557                        var: gv,
6558                        ty: None,
6559                        value: item(ps, at(gi, gj)),
6560                        mutable: false,
6561                    },
6562                    Stmt::If {
6563                        cond: self.te_bin(BinaryOpKind::Gt, id(gv), id(t1)),
6564                        then_block: self.te_block(vec![
6565                            Stmt::Set { target: t2, value: id(t1) },
6566                            Stmt::Set { target: t1, value: id(gv) },
6567                        ]),
6568                        else_block: Some(self.te_block(vec![Stmt::If {
6569                            cond: self.te_bin(BinaryOpKind::Gt, id(gv), id(t2)),
6570                            then_block: self.te_block(vec![Stmt::Set { target: t2, value: id(gv) }]),
6571                            else_block: None,
6572                        }])),
6573                    },
6574                    incr(gj),
6575                ],
6576            );
6577            let scores = while_le(
6578                gi,
6579                count,
6580                vec![
6581                    Stmt::Let { var: t1, ty: None, value: fl(-1.0e30), mutable: true },
6582                    Stmt::Let { var: t2, ty: None, value: fl(-1.0e30), mutable: true },
6583                    Stmt::Let { var: gj, ty: None, value: it(1), mutable: true },
6584                    two_best,
6585                    // A group of one expert is scored by that expert alone.
6586                    Stmt::Push { value: if per > 1 { add(id(t1), id(t2)) } else { id(t1) }, collection: id(gs) },
6587                    incr(gi),
6588                ],
6589            );
6590            let masked = while_le(
6591                pi,
6592                ne,
6593                vec![Stmt::Push { value: fl(-1.0e30), collection: id(pm_seq) }, incr(pi)],
6594            );
6595            let scan = while_le(
6596                gsi,
6597                count,
6598                vec![
6599                    Stmt::If {
6600                        cond: self.te_bin(BinaryOpKind::Gt, item(gs, id(gsi)), id(gbv)),
6601                        then_block: self.te_block(vec![
6602                            Stmt::Set { target: gbv, value: item(gs, id(gsi)) },
6603                            Stmt::Set { target: gbi, value: id(gsi) },
6604                        ]),
6605                        else_block: None,
6606                    },
6607                    incr(gsi),
6608                ],
6609            );
6610            let copy = while_le(
6611                gk,
6612                per,
6613                vec![
6614                    Stmt::SetIndex { collection: id(pm_seq), index: at(gbi, gk), value: item(ps, at(gbi, gk)) },
6615                    incr(gk),
6616                ],
6617            );
6618            let rounds = while_le(
6619                gr,
6620                kept,
6621                vec![
6622                    Stmt::Let { var: gbv, ty: None, value: fl(-1.0e30), mutable: true },
6623                    Stmt::Let { var: gbi, ty: None, value: it(1), mutable: true },
6624                    Stmt::Let { var: gsi, ty: None, value: it(1), mutable: true },
6625                    scan,
6626                    Stmt::Let { var: gk, ty: None, value: it(1), mutable: true },
6627                    copy,
6628                    Stmt::SetIndex { collection: id(gs), index: id(gbi), value: fl(-2.0e30) },
6629                    incr(gr),
6630                ],
6631            );
6632            vec![
6633                Stmt::Let { var: gs, ty: None, value: nseq(), mutable: true },
6634                Stmt::Let { var: gi, ty: None, value: it(1), mutable: true },
6635                scores,
6636                Stmt::Let { var: pi, ty: None, value: it(1), mutable: true },
6637                masked,
6638                Stmt::Let { var: gr, ty: None, value: it(1), mutable: true },
6639                rounds,
6640            ]
6641        });
6642        let mut zeros = vec![Stmt::Push {
6643            value: fl(0.0),
6644            collection: id(w_seq),
6645        }];
6646        if let Some(s_seq) = s_seq {
6647            zeros.push(Stmt::Push {
6648                value: fl(0.0),
6649                collection: id(s_seq),
6650            });
6651        }
6652        zeros.push(incr(wzi));
6653        let w_zero = while_le(wzi, ne, zeros);
6654        let scan = while_le(
6655            sci,
6656            ne,
6657            vec![
6658                Stmt::If {
6659                    cond: self.te_bin(BinaryOpKind::Gt, item(pm_seq, id(sci)), id(bv)),
6660                    then_block: self.te_block(vec![
6661                        Stmt::Set {
6662                            target: bv,
6663                            value: item(pm_seq, id(sci)),
6664                        },
6665                        Stmt::Set {
6666                            target: bi,
6667                            value: id(sci),
6668                        },
6669                    ]),
6670                    else_block: None,
6671                },
6672                incr(sci),
6673            ],
6674        );
6675        let mut round = vec![
6676            Stmt::Let {
6677                var: bv,
6678                ty: None,
6679                value: fl(-1.0e30),
6680                mutable: true,
6681            },
6682            Stmt::Let {
6683                var: bi,
6684                ty: None,
6685                value: it(1),
6686                mutable: true,
6687            },
6688            Stmt::Let {
6689                var: sci,
6690                ty: None,
6691                value: it(1),
6692                mutable: true,
6693            },
6694            scan,
6695            Stmt::Set {
6696                target: wsum,
6697                value: add(id(wsum), item(p_seq, id(bi))),
6698            },
6699            Stmt::SetIndex {
6700                collection: id(w_seq),
6701                index: id(bi),
6702                value: item(p_seq, id(bi)),
6703            },
6704        ];
6705        if let Some(s_seq) = s_seq {
6706            round.push(Stmt::SetIndex {
6707                collection: id(s_seq),
6708                index: id(bi),
6709                value: fl(1.0),
6710            });
6711        }
6712        round.extend([
6713            Stmt::SetIndex {
6714                collection: id(pm_seq),
6715                index: id(bi),
6716                value: fl(-1.0e30),
6717            },
6718            incr(kk),
6719        ]);
6720        let round_loop = while_le(kk, k, round);
6721        let renorm = while_le(
6722            rni,
6723            ne,
6724            vec![
6725                Stmt::SetIndex {
6726                    collection: id(w_seq),
6727                    index: id(rni),
6728                    value: dv(item(w_seq, id(rni)), id(wsum)),
6729                },
6730                incr(rni),
6731            ],
6732        );
6733
6734        // ── 4. Experts: OUT (= var) starts at d zeros; each expert e adds W[e]·down(silu(gate·x)⊙up·x) ──
6735        let out_zero = while_le(
6736            ozi,
6737            d,
6738            vec![
6739                Stmt::Push {
6740                    value: fl(0.0),
6741                    collection: id(var),
6742                },
6743                incr(ozi),
6744            ],
6745        );
6746        // gate[o] = Σ_t X[t]·Wg[((e-1)·dff + (o-1))·d + t] ; up likewise with Wu
6747        let wg_ix = add(
6748            mul(
6749                add(mul(sub(id(ee), it(1)), it(dff)), sub(id(gig), it(1))),
6750                it(d),
6751            ),
6752            id(trg),
6753        );
6754        let gate_inner = while_le(
6755            trg,
6756            d,
6757            vec![
6758                Stmt::Set {
6759                    target: gacc,
6760                    value: add(id(gacc), mul(item(x, id(trg)), item(wg, wg_ix))),
6761                },
6762                incr(trg),
6763            ],
6764        );
6765        let gate_loop = while_le(
6766            gig,
6767            dff,
6768            vec![
6769                Stmt::Let {
6770                    var: gacc,
6771                    ty: None,
6772                    value: fl(0.0),
6773                    mutable: true,
6774                },
6775                Stmt::Let {
6776                    var: trg,
6777                    ty: None,
6778                    value: it(1),
6779                    mutable: true,
6780                },
6781                gate_inner,
6782                Stmt::Push {
6783                    value: {
6784                        // The input scaled by the expert's weight: its product is the weight times the unscaled one.
6785                        let product = if opts.weigh_inputs { mul(item(w_seq, id(ee)), id(gacc)) } else { id(gacc) };
6786                        match opts.biases[0] {
6787                            Some(bg) => add(product, item(bg, add(mul(sub(id(ee), it(1)), it(dff)), id(gig)))),
6788                            None => product,
6789                        }
6790                    },
6791                    collection: id(gate),
6792                },
6793                incr(gig),
6794            ],
6795        );
6796        let wu_ix = add(
6797            mul(
6798                add(mul(sub(id(ee), it(1)), it(dff)), sub(id(giu), it(1))),
6799                it(d),
6800            ),
6801            id(tru),
6802        );
6803        let up_inner = while_le(
6804            tru,
6805            d,
6806            vec![
6807                Stmt::Set {
6808                    target: uacc,
6809                    value: add(id(uacc), mul(item(x, id(tru)), item(wu, wu_ix))),
6810                },
6811                incr(tru),
6812            ],
6813        );
6814        let up_loop = while_le(
6815            giu,
6816            dff,
6817            vec![
6818                Stmt::Let {
6819                    var: uacc,
6820                    ty: None,
6821                    value: fl(0.0),
6822                    mutable: true,
6823                },
6824                Stmt::Let {
6825                    var: tru,
6826                    ty: None,
6827                    value: it(1),
6828                    mutable: true,
6829                },
6830                up_inner,
6831                Stmt::Push {
6832                    value: {
6833                        // The input scaled by the expert's weight: its product is the weight times the unscaled one.
6834                        let product = if opts.weigh_inputs { mul(item(w_seq, id(ee)), id(uacc)) } else { id(uacc) };
6835                        match opts.biases[1] {
6836                            Some(bu) => add(product, item(bu, add(mul(sub(id(ee), it(1)), it(dff)), id(giu)))),
6837                            None => product,
6838                        }
6839                    },
6840                    collection: id(up),
6841                },
6842                incr(giu),
6843            ],
6844        );
6845        // out[o] += W[e] · Σ_t (silu(gate[t])·up[t]) · Wd[((e-1)·d + (o-1))·dff + t]
6846        let wd_ix = add(
6847            mul(
6848                add(mul(sub(id(ee), it(1)), it(d)), sub(id(oo), it(1))),
6849                it(dff),
6850            ),
6851            id(di),
6852        );
6853        let he_t = match opts.act {
6854            MoeAct::Swiglu => mul(silu(item(gate, id(di))), item(up, id(di))),
6855            // min(g, L) / (1 + e^(0 − A·min(g, L))) · (max(min(u, L), −L) + 1)
6856            MoeAct::ClampedSwiglu { limit, slope } => {
6857                let gc = self.te_call(min_sym, vec![item(gate, id(di)), fl(limit)]);
6858                let uc = self.te_call(max_sym, vec![self.te_call(min_sym, vec![item(up, id(di)), fl(limit)]), fl(-limit)]);
6859                mul(dv(gc, add(fl(1.0), expo(sub(fl(0.0), mul(fl(slope), gc))))), add(uc, fl(1.0)))
6860            }
6861        };
6862        let down_inner = while_le(
6863            di,
6864            dff,
6865            vec![
6866                Stmt::Set {
6867                    target: dacc,
6868                    value: add(id(dacc), mul(he_t, item(wd, wd_ix))),
6869                },
6870                incr(di),
6871            ],
6872        );
6873        let down_loop = while_le(
6874            oo,
6875            d,
6876            vec![
6877                Stmt::Let {
6878                    var: dacc,
6879                    ty: None,
6880                    value: fl(0.0),
6881                    mutable: true,
6882                },
6883                Stmt::Let {
6884                    var: di,
6885                    ty: None,
6886                    value: it(1),
6887                    mutable: true,
6888                },
6889                down_inner,
6890                Stmt::SetIndex {
6891                    collection: id(var),
6892                    index: id(oo),
6893                    value: add(
6894                        item(var, id(oo)),
6895                        mul(
6896                            item(s_seq.unwrap_or(w_seq), id(ee)),
6897                            match opts.biases[2] {
6898                                Some(bd) => add(id(dacc), item(bd, add(mul(sub(id(ee), it(1)), it(d)), id(oo)))),
6899                                None => id(dacc),
6900                            },
6901                        ),
6902                    ),
6903                },
6904                incr(oo),
6905            ],
6906        );
6907        let expert_loop = while_le(
6908            ee,
6909            ne,
6910            vec![
6911                Stmt::Let {
6912                    var: gate,
6913                    ty: None,
6914                    value: nseq(),
6915                    mutable: true,
6916                },
6917                Stmt::Let {
6918                    var: gig,
6919                    ty: None,
6920                    value: it(1),
6921                    mutable: true,
6922                },
6923                gate_loop,
6924                Stmt::Let {
6925                    var: up,
6926                    ty: None,
6927                    value: nseq(),
6928                    mutable: true,
6929                },
6930                Stmt::Let {
6931                    var: giu,
6932                    ty: None,
6933                    value: it(1),
6934                    mutable: true,
6935                },
6936                up_loop,
6937                Stmt::Let {
6938                    var: oo,
6939                    ty: None,
6940                    value: it(1),
6941                    mutable: true,
6942                },
6943                down_loop,
6944                incr(ee),
6945            ],
6946        );
6947
6948        let mut body = vec![
6949            // router
6950            Stmt::Let {
6951                var: l_seq,
6952                ty: None,
6953                value: nseq(),
6954                mutable: true,
6955            },
6956            Stmt::Let {
6957                var: er,
6958                ty: None,
6959                value: it(1),
6960                mutable: true,
6961            },
6962            router_loop,
6963        ];
6964        // softmax → P: the largest logit and the sum of the exponentials first; a sigmoid scores each logit alone.
6965        if opts.score == MoeScore::Softmax {
6966            body.extend([
6967                Stmt::Let {
6968                    var: smm,
6969                    ty: None,
6970                    value: item(l_seq, it(1)),
6971                    mutable: true,
6972                },
6973                Stmt::Let {
6974                    var: smi1,
6975                    ty: None,
6976                    value: it(2),
6977                    mutable: true,
6978                },
6979                max_loop,
6980                Stmt::Let {
6981                    var: sms,
6982                    ty: None,
6983                    value: fl(0.0),
6984                    mutable: true,
6985                },
6986                Stmt::Let {
6987                    var: smi2,
6988                    ty: None,
6989                    value: it(1),
6990                    mutable: true,
6991                },
6992                sum_loop,
6993            ]);
6994        }
6995        body.extend([
6996            Stmt::Let {
6997                var: p_seq,
6998                ty: None,
6999                value: nseq(),
7000                mutable: true,
7001            },
7002            Stmt::Let {
7003                var: smi3,
7004                ty: None,
7005                value: it(1),
7006                mutable: true,
7007            },
7008            norm_loop,
7009            // top-K
7010            Stmt::Let {
7011                var: pm_seq,
7012                ty: None,
7013                value: nseq(),
7014                mutable: true,
7015            },
7016        ]);
7017        if sel_seq != pm_seq {
7018            body.push(Stmt::Let {
7019                var: sel_seq,
7020                ty: None,
7021                value: nseq(),
7022                mutable: true,
7023            });
7024        }
7025        body.extend([
7026            Stmt::Let {
7027                var: pci,
7028                ty: None,
7029                value: it(1),
7030                mutable: true,
7031            },
7032            pm_copy,
7033        ]);
7034        body.extend(group_block.into_iter().flatten());
7035        body.push(Stmt::Let {
7036            var: w_seq,
7037            ty: None,
7038            value: nseq(),
7039            mutable: true,
7040        });
7041        if let Some(s_seq) = s_seq {
7042            body.push(Stmt::Let {
7043                var: s_seq,
7044                ty: None,
7045                value: nseq(),
7046                mutable: true,
7047            });
7048        }
7049        body.extend([
7050            Stmt::Let {
7051                var: wzi,
7052                ty: None,
7053                value: it(1),
7054                mutable: true,
7055            },
7056            w_zero,
7057            Stmt::Let {
7058                var: wsum,
7059                ty: None,
7060                value: fl(0.0),
7061                mutable: true,
7062            },
7063            Stmt::Let {
7064                var: kk,
7065                ty: None,
7066                value: it(1),
7067                mutable: true,
7068            },
7069            round_loop,
7070        ]);
7071        if opts.renormalize {
7072            body.extend([
7073                Stmt::Set {
7074                    target: wsum,
7075                    value: self.te_call(max_sym, vec![id(wsum), fl(6.103515625e-5)]),
7076                },
7077                Stmt::Let {
7078                    var: rni,
7079                    ty: None,
7080                    value: it(1),
7081                    mutable: true,
7082                },
7083                renorm,
7084            ]);
7085        }
7086        if let (Some(scale), Some(sc)) = (opts.scale, scale_i) {
7087            body.extend([
7088                Stmt::Let {
7089                    var: sc,
7090                    ty: None,
7091                    value: it(1),
7092                    mutable: true,
7093                },
7094                while_le(
7095                    sc,
7096                    ne,
7097                    vec![
7098                        Stmt::SetIndex {
7099                            collection: id(w_seq),
7100                            index: id(sc),
7101                            value: mul(item(w_seq, id(sc)), fl(scale)),
7102                        },
7103                        incr(sc),
7104                    ],
7105                ),
7106            ]);
7107        }
7108        body.extend([
7109            // experts + weighted combine
7110            Stmt::Let {
7111                var,
7112                ty: None,
7113                value: nseq(),
7114                mutable: true,
7115            },
7116            Stmt::Let {
7117                var: ozi,
7118                ty: None,
7119                value: it(1),
7120                mutable: true,
7121            },
7122            out_zero,
7123            Stmt::Let {
7124                var: ee,
7125                ty: None,
7126                value: it(1),
7127                mutable: true,
7128            },
7129            expert_loop,
7130        ]);
7131        let body = self.te_block(body);
7132        Stmt::Splice { body }
7133    }
7134
7135    /// `Let C be attention of Q over K and V with H heads KV groups` — single-query multi-head GQA attention.
7136    /// `Q` is `1×(H·HD)`, `K` is `L×(KV·HD)` and `V` `L×(KV·VD)` (the cache). Query head `g` (1-based) attends to kv
7137    /// head `(g-1)/(H/KV)` over all `L` cached keys: `scores[j] = (Q_g·K_j)·s` (`s` the given scale, `1/√HD` by
7138    /// default), softmax, `C_g = Σ_j p_j·V_j`, `VD` wide.
7139    /// Desugars to plain loops so the VM runs it directly; the CUDA emitter recognizes the block to emit ONE
7140    /// parallel `attn_decode` kernel (block per query head). With a sliding `window` of `W` a query attending `n`
7141    /// positions keeps the last `W` of them, `max(1, n − W + 1) ..= n`; with chunks of `C`, those of its own chunk,
7142    /// `⌊(n − 1)/C⌋·C + 1 ..= n`; and the loops run over those alone. With `sinks` head `g`'s
7143    /// sink `S_g` joins the softmax's maximum and its denominator (`+ e^(S_g − max)`) and takes no value.
7144    fn desugar_tensor_attention(
7145        &mut self,
7146        var: Symbol,
7147        q: Symbol,
7148        kc: Symbol,
7149        vc: Symbol,
7150        shape: AttentionShape,
7151        upto: Option<&'a Expr<'a>>,
7152        options: AttentionOptions,
7153    ) -> Stmt<'a> {
7154        self.desugar_tensor_attention_block(var, q, kc, vc, shape, upto, options)
7155    }
7156
7157    /// [`Self::desugar_tensor_attention`] over a BLOCK of `rows` query rows — `causal attention of Q … from P`: row `r`
7158    /// (1-based) is the query at position `P + r − 1` and attends the cache's first `P + r − 1` positions, its query read
7159    /// offset by `(r − 1)·H·HD` and its output write by `(r − 1)·H·VD`. One row is the single-query form, statement for
7160    /// statement.
7161    fn desugar_tensor_attention_block(
7162        &mut self,
7163        var: Symbol,
7164        q: Symbol,
7165        kc: Symbol,
7166        vc: Symbol,
7167        shape: AttentionShape,
7168        upto: Option<&'a Expr<'a>>,
7169        options: AttentionOptions,
7170    ) -> Stmt<'a> {
7171        let AttentionShape { h, kv, hd, vd, l, rows } = shape;
7172        let AttentionOptions { window, scale, cap, sinks } = options;
7173        let group = if kv > 0 { h / kv } else { 1 };
7174        let scale = scale.unwrap_or(1.0f64 / (hd as f64).sqrt());
7175        const EU: f64 = std::f64::consts::E;
7176        let real_sym = self.interner.intern("Real");
7177        let seq_sym = self.interner.intern("Seq");
7178        let max_sym = self.interner.intern("max");
7179        // distinct temps per loop var (no re-Let of the same symbol in a scope)
7180        let (head, kvh, scores, ozi) = (
7181            self.fresh_desugar_temp("at_head"),
7182            self.fresh_desugar_temp("at_kvh"),
7183            self.fresh_desugar_temp("at_sc"),
7184            self.fresh_desugar_temp("at_ozi"),
7185        );
7186        let (j_sc, e_sc, acc_sc) = (
7187            self.fresh_desugar_temp("at_jsc"),
7188            self.fresh_desugar_temp("at_esc"),
7189            self.fresh_desugar_temp("at_asc"),
7190        );
7191        let (mx, si_mx, si_dn, den) = (
7192            self.fresh_desugar_temp("at_mx"),
7193            self.fresh_desugar_temp("at_simx"),
7194            self.fresh_desugar_temp("at_sidn"),
7195            self.fresh_desugar_temp("at_den"),
7196        );
7197        let (e_wv, j_wv, acc_wv) = (
7198            self.fresh_desugar_temp("at_ewv"),
7199            self.fresh_desugar_temp("at_jwv"),
7200            self.fresh_desugar_temp("at_awv"),
7201        );
7202        // A window's first position and the scores it keeps, bound per head.
7203        let windowed = window.map(|w| (w, self.fresh_desugar_temp("at_lo"), self.fresh_desugar_temp("at_cnt")));
7204        // The positions attended: all `L`, or the first `P` — bound once, and checked to be in `1..=L`. A block's first
7205        // row is at `P`, its last at `P + rows − 1`, and the whole span is checked; each row then binds its own length.
7206        let block = rows > 1;
7207        let (len, len_check) = match upto {
7208            Some(p) if block => {
7209                let (from, check) = self.checked_attention_block(p, kc, l, rows);
7210                (Some(from), Some(check))
7211            }
7212            Some(p) => {
7213                let (len, check) = self.checked_attention_length(p, kc, l);
7214                (Some(len), Some(check))
7215            }
7216            None => (None, None),
7217        };
7218        let (row, row_len) = if block {
7219            (
7220                Some(self.fresh_desugar_temp("at_row")),
7221                Some(self.fresh_desugar_temp("at_rlen")),
7222            )
7223        } else {
7224            (None, None)
7225        };
7226        let bound = if block { row_len } else { len };
7227        let id = |s: Symbol| self.te_id(s);
7228        let fl = |v: f64| self.te_float(v);
7229        let it = |n: i64| self.te_int(n);
7230        let add = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Add, a, b);
7231        let sub = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Subtract, a, b);
7232        let mul = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Multiply, a, b);
7233        let dv = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Divide, a, b);
7234        let item = |coll: Symbol, ix: &'a Expr<'a>| self.te_index(self.te_id(coll), ix);
7235        let nseq = || {
7236            self.ctx.alloc_imperative_expr(Expr::New {
7237                type_name: seq_sym,
7238                type_args: vec![TypeExpr::Named(real_sym)],
7239                init_fields: vec![],
7240            })
7241        };
7242        let expo = |arg: &'a Expr<'a>| self.te_bin(BinaryOpKind::Pow, self.te_float(EU), arg);
7243        let incr = |s: Symbol| Stmt::Set {
7244            target: s,
7245            value: add(id(s), it(1)),
7246        };
7247        let while_le = |vv: Symbol, bound: i64, body: Vec<Stmt<'a>>| Stmt::While {
7248            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(vv), self.te_int(bound)),
7249            body: self.te_block(body),
7250            decreasing: None,
7251        };
7252        let over_cache = |vv: Symbol, body: Vec<Stmt<'a>>| match bound {
7253            Some(n) => Stmt::While {
7254                cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(vv), self.te_id(n)),
7255                body: self.te_block(body),
7256                decreasing: None,
7257            },
7258            None => while_le(vv, l, body),
7259        };
7260        // The positions a query attends: `n` of them, or a window's last `W`; the scores hold one per position kept.
7261        let attended = match bound {
7262            Some(n) => id(n),
7263            None => it(l),
7264        };
7265        let first = match windowed {
7266            Some((_, lo, _)) => id(lo),
7267            None => it(1),
7268        };
7269        let over_scores = |vv: Symbol, body: Vec<Stmt<'a>>| match windowed {
7270            Some((_, _, cnt)) => Stmt::While {
7271                cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(vv), self.te_id(cnt)),
7272                body: self.te_block(body),
7273                decreasing: None,
7274            },
7275            None => over_cache(vv, body),
7276        };
7277        let score_of = |j: Symbol| match windowed {
7278            Some((_, lo, _)) => item(scores, add(sub(id(j), id(lo)), it(1))),
7279            None => item(scores, id(j)),
7280        };
7281        // A block row's query sits `(row−1)·H·HD` into the block, and its output `(row−1)·H·VD`.
7282        let in_row = |at: &'a Expr<'a>, width: i64| match row {
7283            Some(r) => add(mul(sub(id(r), it(1)), it(h * width)), at),
7284            None => at,
7285        };
7286        // 1-based item indices. Q read = (head-1)·HD + e, ctx write = (head-1)·VD + e. K read = (j-1)·(KV·HD) + kvh·HD
7287        // + e, V read = (j-1)·(KV·VD) + kvh·VD + e.
7288        let q_read = in_row(add(mul(sub(id(head), it(1)), it(hd)), id(e_sc)), hd);
7289        let k_read = add(
7290            add(mul(sub(id(j_sc), it(1)), it(kv * hd)), mul(id(kvh), it(hd))),
7291            id(e_sc),
7292        );
7293        let v_read = add(
7294            add(mul(sub(id(j_wv), it(1)), it(kv * vd)), mul(id(kvh), it(vd))),
7295            id(e_wv),
7296        );
7297        let ctx_write = in_row(add(mul(sub(id(head), it(1)), it(vd)), id(e_wv)), vd);
7298        // scores[j] = scale · Σ_e Q·K
7299        let score_inner = while_le(
7300            e_sc,
7301            hd,
7302            vec![
7303                Stmt::Set {
7304                    target: acc_sc,
7305                    value: add(id(acc_sc), mul(item(q, q_read), item(kc, k_read))),
7306                },
7307                incr(e_sc),
7308            ],
7309        );
7310        let score_loop = over_cache(
7311            j_sc,
7312            vec![
7313                Stmt::Let {
7314                    var: acc_sc,
7315                    ty: None,
7316                    value: fl(0.0),
7317                    mutable: true,
7318                },
7319                Stmt::Let {
7320                    var: e_sc,
7321                    ty: None,
7322                    value: it(1),
7323                    mutable: true,
7324                },
7325                score_inner,
7326                Stmt::Push {
7327                    value: match cap {
7328                        Some(c) => self.capped(mul(id(acc_sc), fl(scale)), c),
7329                        None => mul(id(acc_sc), fl(scale)),
7330                    },
7331                    collection: id(scores),
7332                },
7333                incr(j_sc),
7334            ],
7335        );
7336        // softmax max + denominator
7337        let max_loop = over_scores(
7338            si_mx,
7339            vec![
7340                Stmt::Set {
7341                    target: mx,
7342                    value: self.te_call(max_sym, vec![id(mx), item(scores, id(si_mx))]),
7343                },
7344                incr(si_mx),
7345            ],
7346        );
7347        let den_loop = over_scores(
7348            si_dn,
7349            vec![
7350                Stmt::Set {
7351                    target: den,
7352                    value: add(id(den), expo(sub(item(scores, id(si_dn)), id(mx)))),
7353                },
7354                incr(si_dn),
7355            ],
7356        );
7357        // ctx[(head-1)·HD+e] = Σ_j (exp(scores[j]-mx)/den) · V
7358        let wv_inner = over_cache(
7359            j_wv,
7360            vec![
7361                Stmt::Set {
7362                    target: acc_wv,
7363                    value: add(
7364                        id(acc_wv),
7365                        mul(
7366                            dv(expo(sub(score_of(j_wv), id(mx))), id(den)),
7367                            item(vc, v_read),
7368                        ),
7369                    ),
7370                },
7371                incr(j_wv),
7372            ],
7373        );
7374        let wv_loop = while_le(
7375            e_wv,
7376            vd,
7377            vec![
7378                Stmt::Let {
7379                    var: acc_wv,
7380                    ty: None,
7381                    value: fl(0.0),
7382                    mutable: true,
7383                },
7384                Stmt::Let {
7385                    var: j_wv,
7386                    ty: None,
7387                    value: first,
7388                    mutable: true,
7389                },
7390                wv_inner,
7391                Stmt::SetIndex {
7392                    collection: id(var),
7393                    index: ctx_write,
7394                    value: id(acc_wv),
7395                },
7396                incr(e_wv),
7397            ],
7398        );
7399        let mut head_body = vec![
7400            Stmt::Let {
7401                var: kvh,
7402                ty: None,
7403                value: dv(sub(id(head), it(1)), it(group)),
7404                mutable: false,
7405            },
7406            Stmt::Let {
7407                var: scores,
7408                ty: None,
7409                value: nseq(),
7410                mutable: true,
7411            },
7412        ];
7413        if let Some((w, lo, cnt)) = windowed {
7414            head_body.extend([
7415                Stmt::Let {
7416                    var: lo,
7417                    ty: None,
7418                    value: match w {
7419                        Window::Sliding(w) => self.te_call(max_sym, vec![it(1), sub(attended, it(w - 1))]),
7420                        Window::Chunked(c) => add(mul(dv(sub(attended, it(1)), it(c)), it(c)), it(1)),
7421                    },
7422                    mutable: false,
7423                },
7424                Stmt::Let {
7425                    var: cnt,
7426                    ty: None,
7427                    value: add(sub(attended, id(lo)), it(1)),
7428                    mutable: false,
7429                },
7430            ]);
7431        }
7432        head_body.extend([
7433            Stmt::Let {
7434                var: j_sc,
7435                ty: None,
7436                value: first,
7437                mutable: true,
7438            },
7439            score_loop,
7440            Stmt::Let {
7441                var: mx,
7442                ty: None,
7443                value: match sinks {
7444                    Some(sk) => self.te_call(max_sym, vec![item(scores, it(1)), item(sk, id(head))]),
7445                    None => item(scores, it(1)),
7446                },
7447                mutable: true,
7448            },
7449            Stmt::Let {
7450                var: si_mx,
7451                ty: None,
7452                value: it(2),
7453                mutable: true,
7454            },
7455            max_loop,
7456            Stmt::Let {
7457                var: den,
7458                ty: None,
7459                value: match sinks {
7460                    Some(sk) => expo(sub(item(sk, id(head)), id(mx))),
7461                    None => fl(0.0),
7462                },
7463                mutable: true,
7464            },
7465            Stmt::Let {
7466                var: si_dn,
7467                ty: None,
7468                value: it(1),
7469                mutable: true,
7470            },
7471            den_loop,
7472            Stmt::Let {
7473                var: e_wv,
7474                ty: None,
7475                value: it(1),
7476                mutable: true,
7477            },
7478            wv_loop,
7479            incr(head),
7480        ]);
7481        let head_loop = while_le(head, h, head_body);
7482        let out_zero = while_le(
7483            ozi,
7484            rows * h * vd,
7485            vec![
7486                Stmt::Push {
7487                    value: fl(0.0),
7488                    collection: id(var),
7489                },
7490                incr(ozi),
7491            ],
7492        );
7493        let mut stmts: Vec<Stmt<'a>> = len_check.map(Vec::from).unwrap_or_default();
7494        stmts.extend([
7495            Stmt::Let {
7496                var,
7497                ty: None,
7498                value: nseq(),
7499                mutable: true,
7500            },
7501            Stmt::Let {
7502                var: ozi,
7503                ty: None,
7504                value: it(1),
7505                mutable: true,
7506            },
7507            out_zero,
7508        ]);
7509        match (row, row_len, len) {
7510            // Each row: its length `P + row − 1`, then every head of it.
7511            (Some(r), Some(rl), Some(from)) => stmts.extend([
7512                Stmt::Let {
7513                    var: r,
7514                    ty: None,
7515                    value: it(1),
7516                    mutable: true,
7517                },
7518                while_le(
7519                    r,
7520                    rows,
7521                    vec![
7522                        Stmt::Let {
7523                            var: rl,
7524                            ty: None,
7525                            value: sub(add(id(from), id(r)), it(1)),
7526                            mutable: false,
7527                        },
7528                        Stmt::Let {
7529                            var: head,
7530                            ty: None,
7531                            value: it(1),
7532                            mutable: true,
7533                        },
7534                        head_loop,
7535                        incr(r),
7536                    ],
7537                ),
7538            ]),
7539            _ => stmts.extend([
7540                Stmt::Let {
7541                    var: head,
7542                    ty: None,
7543                    value: it(1),
7544                    mutable: true,
7545                },
7546                head_loop,
7547            ]),
7548        }
7549        let body = self.te_block(stmts);
7550        Stmt::Splice { body }
7551    }
7552
7553    /// `Let C be lut attention of Q over K and V with H heads KV groups [up to P] with table E and scale M shifted right
7554    /// by S` — an int8 model's single-query attention, every tensor of Int. Per query head `g` (kv head
7555    /// `(g − 1) / (H / KV)`), over the first `P` cached positions (all `L` without `up to`):
7556    ///
7557    /// ```text
7558    /// s_j = Σ_e Q[g, e] · K[j, kvh, e]
7559    /// mx  = max_j s_j
7560    /// w_j = E[min(max(((mx − s_j) · M) >> S, 0), n − 1)]
7561    /// C[g, e] = (Σ_j w_j · V[j, kvh, e]) // (Σ_j w_j)          (floor division, toward −∞)
7562    /// ```
7563    ///
7564    /// Integer sums are exact, so the order they run in is not part of the meaning.
7565    #[allow(clippy::too_many_arguments)]
7566    fn desugar_tensor_lut_attention(
7567        &mut self,
7568        var: Symbol,
7569        q: Symbol,
7570        kc: Symbol,
7571        vc: Symbol,
7572        table: Symbol,
7573        (h, kv, hd, l, n): (i64, i64, i64, i64, i64),
7574        upto: Option<&'a Expr<'a>>,
7575        (m, s): (i64, i64),
7576    ) -> Stmt<'a> {
7577        let group = h / kv;
7578        let max_sym = self.interner.intern("max");
7579        let min_sym = self.interner.intern("min");
7580        let seq_sym = self.interner.intern("Seq");
7581        let int_sym = self.interner.intern("Int");
7582        let (head, kvh, scores, wts, ozi) = (
7583            self.fresh_desugar_temp("la_head"),
7584            self.fresh_desugar_temp("la_kvh"),
7585            self.fresh_desugar_temp("la_sc"),
7586            self.fresh_desugar_temp("la_w"),
7587            self.fresh_desugar_temp("la_ozi"),
7588        );
7589        let (j_sc, e_sc, acc_sc) = (
7590            self.fresh_desugar_temp("la_jsc"),
7591            self.fresh_desugar_temp("la_esc"),
7592            self.fresh_desugar_temp("la_asc"),
7593        );
7594        let (mx, si_mx, j_w, w, den) = (
7595            self.fresh_desugar_temp("la_mx"),
7596            self.fresh_desugar_temp("la_simx"),
7597            self.fresh_desugar_temp("la_jw"),
7598            self.fresh_desugar_temp("la_wt"),
7599            self.fresh_desugar_temp("la_den"),
7600        );
7601        let (e_wv, j_wv, num) = (
7602            self.fresh_desugar_temp("la_ewv"),
7603            self.fresh_desugar_temp("la_jwv"),
7604            self.fresh_desugar_temp("la_num"),
7605        );
7606        let (len, len_check) = match upto {
7607            Some(p) => {
7608                let (len, check) = self.checked_attention_length(p, kc, l);
7609                (Some(len), Some(check))
7610            }
7611            None => (None, None),
7612        };
7613        let id = |s: Symbol| self.te_id(s);
7614        let it = |v: i64| self.te_int(v);
7615        let add = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Add, a, b);
7616        let sub = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Subtract, a, b);
7617        let mul = |a: &'a Expr<'a>, b: &'a Expr<'a>| self.te_bin(BinaryOpKind::Multiply, a, b);
7618        let item = |coll: Symbol, ix: &'a Expr<'a>| self.te_index(self.te_id(coll), ix);
7619        let int_seq = || {
7620            self.ctx.alloc_imperative_expr(Expr::New {
7621                type_name: seq_sym,
7622                type_args: vec![TypeExpr::Named(int_sym)],
7623                init_fields: vec![],
7624            })
7625        };
7626        let incr = |v: Symbol| Stmt::Set {
7627            target: v,
7628            value: add(id(v), it(1)),
7629        };
7630        let let_mut = |v: Symbol, value: &'a Expr<'a>| Stmt::Let {
7631            var: v,
7632            ty: None,
7633            value,
7634            mutable: true,
7635        };
7636        let while_le = |vv: Symbol, bound: i64, body: Vec<Stmt<'a>>| Stmt::While {
7637            cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(vv), self.te_int(bound)),
7638            body: self.te_block(body),
7639            decreasing: None,
7640        };
7641        let over_cache = |vv: Symbol, body: Vec<Stmt<'a>>| match len {
7642            Some(p) => Stmt::While {
7643                cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(vv), self.te_id(p)),
7644                body: self.te_block(body),
7645                decreasing: None,
7646            },
7647            None => while_le(vv, l, body),
7648        };
7649        // 1-based items. Q read / C write = (head−1)·HD + e. K/V read = (j−1)·(KV·HD) + kvh·HD + e.
7650        let q_read = add(mul(sub(id(head), it(1)), it(hd)), id(e_sc));
7651        let k_read = add(
7652            add(mul(sub(id(j_sc), it(1)), it(kv * hd)), mul(id(kvh), it(hd))),
7653            id(e_sc),
7654        );
7655        let v_read = add(
7656            add(mul(sub(id(j_wv), it(1)), it(kv * hd)), mul(id(kvh), it(hd))),
7657            id(e_wv),
7658        );
7659        let c_write = add(mul(sub(id(head), it(1)), it(hd)), id(e_wv));
7660        let score_loop = over_cache(
7661            j_sc,
7662            vec![
7663                let_mut(acc_sc, it(0)),
7664                let_mut(e_sc, it(1)),
7665                while_le(
7666                    e_sc,
7667                    hd,
7668                    vec![
7669                        Stmt::Set {
7670                            target: acc_sc,
7671                            value: add(id(acc_sc), mul(item(q, q_read), item(kc, k_read))),
7672                        },
7673                        incr(e_sc),
7674                    ],
7675                ),
7676                Stmt::Push {
7677                    value: id(acc_sc),
7678                    collection: id(scores),
7679                },
7680                incr(j_sc),
7681            ],
7682        );
7683        let max_loop = over_cache(
7684            si_mx,
7685            vec![
7686                Stmt::Set {
7687                    target: mx,
7688                    value: self.te_call(max_sym, vec![id(mx), item(scores, id(si_mx))]),
7689                },
7690                incr(si_mx),
7691            ],
7692        );
7693        // w_j = E[min(max(((mx − s_j)·M) >> S, 0), n − 1) + 1] — the table read 1-based.
7694        let lut_index = self.te_call(
7695            min_sym,
7696            vec![
7697                self.te_call(
7698                    max_sym,
7699                    vec![
7700                        self.te_bin(
7701                            BinaryOpKind::Shr,
7702                            mul(sub(id(mx), item(scores, id(j_w))), it(m)),
7703                            it(s),
7704                        ),
7705                        it(0),
7706                    ],
7707                ),
7708                it(n - 1),
7709            ],
7710        );
7711        let weight_loop = over_cache(
7712            j_w,
7713            vec![
7714                Stmt::Let {
7715                    var: w,
7716                    ty: None,
7717                    value: item(table, add(lut_index, it(1))),
7718                    mutable: false,
7719                },
7720                Stmt::Push {
7721                    value: id(w),
7722                    collection: id(wts),
7723                },
7724                Stmt::Set {
7725                    target: den,
7726                    value: add(id(den), id(w)),
7727                },
7728                incr(j_w),
7729            ],
7730        );
7731        let ctx_loop = while_le(
7732            e_wv,
7733            hd,
7734            vec![
7735                let_mut(num, it(0)),
7736                let_mut(j_wv, it(1)),
7737                over_cache(
7738                    j_wv,
7739                    vec![
7740                        Stmt::Set {
7741                            target: num,
7742                            value: add(id(num), mul(item(wts, id(j_wv)), item(vc, v_read))),
7743                        },
7744                        incr(j_wv),
7745                    ],
7746                ),
7747                Stmt::SetIndex {
7748                    collection: id(var),
7749                    index: c_write,
7750                    value: self.te_bin(BinaryOpKind::FloorDivide, id(num), id(den)),
7751                },
7752                incr(e_wv),
7753            ],
7754        );
7755        let head_loop = while_le(
7756            head,
7757            h,
7758            vec![
7759                Stmt::Let {
7760                    var: kvh,
7761                    ty: None,
7762                    value: self.te_bin(BinaryOpKind::Divide, sub(id(head), it(1)), it(group)),
7763                    mutable: false,
7764                },
7765                let_mut(scores, int_seq()),
7766                let_mut(j_sc, it(1)),
7767                score_loop,
7768                let_mut(mx, item(scores, it(1))),
7769                let_mut(si_mx, it(2)),
7770                max_loop,
7771                let_mut(wts, int_seq()),
7772                let_mut(den, it(0)),
7773                let_mut(j_w, it(1)),
7774                weight_loop,
7775                let_mut(e_wv, it(1)),
7776                ctx_loop,
7777                incr(head),
7778            ],
7779        );
7780        let mut stmts: Vec<Stmt<'a>> = len_check.map(Vec::from).unwrap_or_default();
7781        stmts.extend([
7782            let_mut(var, int_seq()),
7783            let_mut(ozi, it(1)),
7784            while_le(
7785                ozi,
7786                h * hd,
7787                vec![
7788                    Stmt::Push {
7789                        value: it(0),
7790                        collection: id(var),
7791                    },
7792                    incr(ozi),
7793                ],
7794            ),
7795            let_mut(head, it(1)),
7796            head_loop,
7797        ]);
7798        let body = self.te_block(stmts);
7799        Stmt::Splice { body }
7800    }
7801
7802    /// Bind an attention's `up to P` bound to a fresh temp and check it, once: `1 ≤ P ≤ L`, with one message on
7803    /// every tier. Zero positions has no softmax, and past `L` there is no cache.
7804    fn checked_attention_length(
7805        &mut self,
7806        bound: &'a Expr<'a>,
7807        cache: Symbol,
7808        rows: i64,
7809    ) -> (Symbol, [Stmt<'a>; 2]) {
7810        let len = self.fresh_desugar_temp("at_len");
7811        let in_range = self.te_bin(
7812            BinaryOpKind::And,
7813            self.te_bin(BinaryOpKind::GtEq, self.te_id(len), self.te_int(1)),
7814            self.te_bin(BinaryOpKind::LtEq, self.te_id(len), self.te_int(rows)),
7815        );
7816        let message = format!("attention length out of range: {} has {rows} positions, and attention needs at least 1", self.interner.resolve(cache));
7817        let message = self.interner.intern(&message);
7818        let reason = self
7819            .ctx
7820            .alloc_imperative_expr(Expr::Literal(Literal::Text(message)));
7821        (
7822            len,
7823            [
7824                Stmt::Let {
7825                    var: len,
7826                    ty: None,
7827                    value: bound,
7828                    mutable: false,
7829                },
7830                Stmt::RuntimeAssert {
7831                    condition: in_range,
7832                    hard: true,
7833                    dim: None,
7834                    reason: Some(reason),
7835                },
7836            ],
7837        )
7838    }
7839
7840    /// Bind a block attention's first position `P` to a fresh temp and check the whole block, once: `P ≥ 1` and its last
7841    /// row's `P + rows − 1 ≤ L`.
7842    fn checked_attention_block(
7843        &mut self,
7844        from: &'a Expr<'a>,
7845        cache: Symbol,
7846        l: i64,
7847        rows: i64,
7848    ) -> (Symbol, [Stmt<'a>; 2]) {
7849        let first = self.fresh_desugar_temp("at_from");
7850        let last = self.te_bin(BinaryOpKind::Add, self.te_id(first), self.te_int(rows - 1));
7851        let in_range = self.te_bin(
7852            BinaryOpKind::And,
7853            self.te_bin(BinaryOpKind::GtEq, self.te_id(first), self.te_int(1)),
7854            self.te_bin(BinaryOpKind::LtEq, last, self.te_int(l)),
7855        );
7856        let message = format!(
7857            "attention length out of range: {} has {l} positions, and a block of {rows} rows from P needs P ≥ 1 and P + {} ≤ {l}",
7858            self.interner.resolve(cache),
7859            rows - 1
7860        );
7861        let message = self.interner.intern(&message);
7862        let reason = self
7863            .ctx
7864            .alloc_imperative_expr(Expr::Literal(Literal::Text(message)));
7865        (
7866            first,
7867            [
7868                Stmt::Let {
7869                    var: first,
7870                    ty: None,
7871                    value: from,
7872                    mutable: false,
7873                },
7874                Stmt::RuntimeAssert {
7875                    condition: in_range,
7876                    hard: true,
7877                    dim: None,
7878                    reason: Some(reason),
7879                },
7880            ],
7881        )
7882    }
7883
7884    /// `Let Kt be transpose of X` for an `R by C` tensor `X`: build the `C by R` result row-major so
7885    /// `Kt[j*R + i] = X[i*C + j]` — the outer loop is the new rows (`j` over `C`), the inner the new cols
7886    /// (`i` over `R`).
7887    /// The index of a row sugar, in the forms `item` accepts before `of`: an integer literal (a `-` sign
7888    /// allowed — it is range-checked at run time like any index), a name, or a parenthesized expression.
7889    /// `None` (nothing consumed) when none of those is next.
7890    fn parse_row_index(&mut self) -> ParseResult<Option<&'a Expr<'a>>> {
7891        if let TokenType::Number(sym) = self.peek().kind {
7892            self.advance();
7893            let text = self.interner.resolve(sym).to_string();
7894            return match self.parse_i64_numeral_signed(&text, false) {
7895                Ok(n) => Ok(Some(self.te_int(n))),
7896                Err(_) => Err(ParseError {
7897                    kind: ParseErrorKind::Custom(format!(
7898                        "a row index is a whole number — got `{text}`."
7899                    )),
7900                    span: self.current_span(),
7901                }),
7902            };
7903        }
7904        if self.check(&TokenType::Minus)
7905            && matches!(
7906                self.tokens.get(self.current + 1).map(|t| &t.kind),
7907                Some(TokenType::Number(_))
7908            )
7909        {
7910            self.advance();
7911            let TokenType::Number(sym) = self.peek().kind else {
7912                unreachable!()
7913            };
7914            self.advance();
7915            let text = self.interner.resolve(sym).to_string();
7916            let n = self
7917                .parse_i64_numeral_signed(&text, true)
7918                .map_err(|_| ParseError {
7919                    kind: ParseErrorKind::Custom(format!(
7920                        "a row index is a whole number — got `-{text}`."
7921                    )),
7922                    span: self.current_span(),
7923                })?;
7924            return Ok(Some(self.te_int(n)));
7925        }
7926        if self.check(&TokenType::LParen) {
7927            self.advance();
7928            let inner = self.parse_imperative_expr()?;
7929            if !self.check(&TokenType::RParen) {
7930                return Err(ParseError {
7931                    kind: ParseErrorKind::ExpectedKeyword {
7932                        keyword: ")".to_string(),
7933                    },
7934                    span: self.current_span(),
7935                });
7936            }
7937            self.advance();
7938            return Ok(Some(inner));
7939        }
7940        if self.check_preposition_is("of") || self.is_at_end() {
7941            return Ok(None);
7942        }
7943        let sym = self.peek().lexeme;
7944        self.advance();
7945        Ok(Some(self.te_id(sym)))
7946    }
7947
7948    /// `row <index> of <T>` where `T` is a declared tensor: `(index, T, rows, cols)`. Consumes nothing and
7949    /// returns `None` unless the WHOLE shape is there — `row` stays an ordinary name everywhere else.
7950    fn try_parse_row_of(&mut self) -> ParseResult<Option<(&'a Expr<'a>, Symbol, i64, i64)>> {
7951        if !self.check_word("row") {
7952            return Ok(None);
7953        }
7954        let saved = self.backtrack_point();
7955        self.advance();
7956        let index = match self.parse_row_index() {
7957            Ok(Some(e)) => e,
7958            _ => {
7959                self.backtrack_to(&saved);
7960                return Ok(None);
7961            }
7962        };
7963        if !self.check_preposition_is("of") {
7964            self.backtrack_to(&saved);
7965            return Ok(None);
7966        }
7967        self.advance();
7968        let table = self.peek().lexeme;
7969        let Some(&(rows, cols)) = self.tensor_shapes.get(&table) else {
7970            self.backtrack_to(&saved);
7971            return Ok(None);
7972        };
7973        self.advance();
7974        Ok(Some((index, table, rows, cols)))
7975    }
7976
7977    /// `rows I of T` — `I` a declared Int tensor of row indices, `T` a declared table: `X` binds `|I|` rows of `T`'s
7978    /// width, row `k` being row `I[k]` of `T`. Consumes nothing and returns `None` unless `rows <name> of <name>` is
7979    /// there; past that the shape is committed and indices that are not Int, or a table not declared, are refused.
7980    fn try_parse_tensor_rows(&mut self, var: Symbol) -> ParseResult<Option<Stmt<'a>>> {
7981        if !(self.check_word("rows")
7982            && matches!(self.tokens.get(self.current + 2).map(|t| &t.kind), Some(TokenType::Preposition(sym)) if self.interner.resolve(*sym) == "of"))
7983        {
7984            return Ok(None);
7985        }
7986        let idx = self.tokens[self.current + 1].lexeme;
7987        let Some(&(ir, ic)) = self.tensor_shapes.get(&idx) else {
7988            return Ok(None);
7989        };
7990        self.advance();
7991        self.advance();
7992        self.advance();
7993        let idx_name = self.interner.resolve(idx).to_string();
7994        let table = self.peek().lexeme;
7995        let table_name = self.interner.resolve(table).to_string();
7996        let Some(&(rows, cols)) = self.tensor_shapes.get(&table) else {
7997            return Err(ParseError {
7998                kind: ParseErrorKind::Custom(format!(
7999                    "`rows {idx_name} of {table_name}` — {table_name} must be a declared tensor."
8000                )),
8001                span: self.current_span(),
8002            });
8003        };
8004        if !self.tensor_int.contains(&idx) {
8005            return Err(ParseError {
8006                kind: ParseErrorKind::Custom(format!(
8007                    "`rows I of T` — the row indices {idx_name} must be a tensor of Int."
8008                )),
8009                span: self.current_span(),
8010            });
8011        }
8012        self.advance();
8013        let int = self.tensor_int.contains(&table);
8014        self.user_bound.insert(var);
8015        self.bind_tensor(var, (ir * ic, cols), int);
8016        Ok(Some(self.desugar_tensor_rows(
8017            var,
8018            idx,
8019            ir * ic,
8020            table,
8021            rows,
8022            cols,
8023            int,
8024        )))
8025    }
8026
8027    fn try_parse_tensor_row(&mut self, var: Symbol) -> ParseResult<Option<Stmt<'a>>> {
8028        let Some((index, table, rows, cols)) = self.try_parse_row_of()? else {
8029            return Ok(None);
8030        };
8031        let int = self.tensor_int.contains(&table);
8032        self.user_bound.insert(var);
8033        self.bind_tensor(var, (1, cols), int);
8034        Ok(Some(
8035            self.desugar_tensor_row(var, index, table, rows, cols, int),
8036        ))
8037    }
8038
8039    /// `Set rows from <P> of <T> to <X>.` — the block append: `X`'s `R` rows written at rows `P … P + R − 1` of `T`, as
8040    /// a prefill chunk appends its keys and values to a cache. `None` (nothing consumed) unless `rows from <P> of
8041    /// <declared tensor>` is there; past that point the shape is committed and a wrong source is a parse error.
8042    fn try_parse_set_rows(&mut self) -> ParseResult<Option<Stmt<'a>>> {
8043        if !(self.check_word("rows") && self.peek_word_at(1, "from")) {
8044            return Ok(None);
8045        }
8046        let saved = self.backtrack_point();
8047        self.advance();
8048        self.advance();
8049        let index = match self.parse_row_index() {
8050            Ok(Some(e)) => e,
8051            _ => {
8052                self.backtrack_to(&saved);
8053                return Ok(None);
8054            }
8055        };
8056        if !self.check_preposition_is("of") {
8057            self.backtrack_to(&saved);
8058            return Ok(None);
8059        }
8060        self.advance();
8061        let table = self.peek().lexeme;
8062        let Some(&(rows, cols)) = self.tensor_shapes.get(&table) else {
8063            self.backtrack_to(&saved);
8064            return Ok(None);
8065        };
8066        self.advance();
8067        let is_to = self.check(&TokenType::To)
8068            || matches!(&self.peek().kind, TokenType::Preposition(sym) if self.interner.resolve(*sym) == "to");
8069        if !is_to {
8070            self.backtrack_to(&saved);
8071            return Ok(None);
8072        }
8073        self.advance();
8074        let source = self.peek().lexeme;
8075        let table_name = self.interner.resolve(table).to_string();
8076        let Some(&(sr, sc)) = self.tensor_shapes.get(&source) else {
8077            return Err(ParseError {
8078                kind: ParseErrorKind::Custom(format!("`Set rows from … of {table_name} to X` — X must be a declared tensor whose rows are {cols} wide.")),
8079                span: self.current_span(),
8080            });
8081        };
8082        let source_name = self.interner.resolve(source).to_string();
8083        if sc != cols {
8084            return Err(ParseError {
8085                kind: ParseErrorKind::Custom(format!("`Set rows from … of {table_name} to {source_name}` — a row of {table_name} holds {cols} elements, and a row of {source_name} holds {sc}.")),
8086                span: self.current_span(),
8087            });
8088        }
8089        if sr < 1 || sr > rows {
8090            return Err(ParseError {
8091                kind: ParseErrorKind::Custom(format!("`Set rows from … of {table_name} to {source_name}` — a block of {sr} rows cannot lie among {table_name}'s {rows}.")),
8092                span: self.current_span(),
8093            });
8094        }
8095        let (table_int, source_int) = (
8096            self.tensor_int.contains(&table),
8097            self.tensor_int.contains(&source),
8098        );
8099        if table_int != source_int {
8100            let kind = |int: bool| if int { "Int" } else { "Real" };
8101            return Err(ParseError {
8102                kind: ParseErrorKind::Custom(format!("`Set rows from … of {table_name} to {source_name}` — {table_name} holds {} and {source_name} holds {}.", kind(table_int), kind(source_int))),
8103                span: self.current_span(),
8104            });
8105        }
8106        self.advance();
8107        Ok(Some(self.desugar_tensor_set_row(
8108            table, index, source, rows, cols, sr,
8109        )))
8110    }
8111
8112    /// `Set row <index> of <T> to <V>.` — `None` (nothing consumed) unless `row <index> of <declared tensor>`
8113    /// is there; past that point the shape is committed and a wrong source is a parse error. `Set rows from …` is
8114    /// the block form, [`Self::try_parse_set_rows`].
8115    fn try_parse_set_row(&mut self) -> ParseResult<Option<Stmt<'a>>> {
8116        if let Some(block) = self.try_parse_set_rows()? {
8117            return Ok(Some(block));
8118        }
8119        let saved = self.backtrack_point();
8120        let Some((index, table, rows, cols)) = self.try_parse_row_of()? else {
8121            return Ok(None);
8122        };
8123        let is_to = self.check(&TokenType::To)
8124            || matches!(&self.peek().kind, TokenType::Preposition(sym) if self.interner.resolve(*sym) == "to");
8125        if !is_to {
8126            self.backtrack_to(&saved);
8127            return Ok(None);
8128        }
8129        self.advance();
8130        let source = self.peek().lexeme;
8131        let table_name = self.interner.resolve(table).to_string();
8132        let Some(&(sr, sc)) = self.tensor_shapes.get(&source) else {
8133            return Err(ParseError {
8134                kind: ParseErrorKind::Custom(format!("`Set row … of {table_name} to X` — X must be a declared tensor of {cols} elements.")),
8135                span: self.current_span(),
8136            });
8137        };
8138        let source_name = self.interner.resolve(source).to_string();
8139        if sr * sc != cols {
8140            return Err(ParseError {
8141                kind: ParseErrorKind::Custom(format!(
8142                    "`Set row … of {table_name} to {source_name}` — a row of {table_name} holds {cols} elements, and {source_name} has {}.",
8143                    sr * sc
8144                )),
8145                span: self.current_span(),
8146            });
8147        }
8148        let (table_int, source_int) = (
8149            self.tensor_int.contains(&table),
8150            self.tensor_int.contains(&source),
8151        );
8152        if table_int != source_int {
8153            let kind = |int: bool| if int { "Int" } else { "Real" };
8154            return Err(ParseError {
8155                kind: ParseErrorKind::Custom(format!(
8156                    "`Set row … of {table_name} to {source_name}` — {table_name} holds {} and {source_name} holds {}.",
8157                    kind(table_int),
8158                    kind(source_int)
8159                )),
8160                span: self.current_span(),
8161            });
8162        }
8163        self.advance();
8164        Ok(Some(self.desugar_tensor_set_row(
8165            table, index, source, rows, cols, 1,
8166        )))
8167    }
8168
8169    /// Evaluate a row index ONCE into a fresh name, then require `1 ≤ index ≤ rows`. A LOGOS index below 1
8170    /// is END-RELATIVE, so an unchecked `(index − 1)·cols + j + 1` would silently read or write the table's
8171    /// last row for `index = 0`; the check makes every tier fail with the same message instead.
8172    fn checked_row_index(
8173        &mut self,
8174        index: &'a Expr<'a>,
8175        table: Symbol,
8176        rows: i64,
8177    ) -> (Symbol, [Stmt<'a>; 2]) {
8178        let message = format!(
8179            "row index out of range: {} has {rows} rows, numbered from 1",
8180            self.interner.resolve(table)
8181        );
8182        self.checked_first_row(index, rows, &message)
8183    }
8184
8185    /// [`Self::checked_row_index`] for a block of `count` consecutive rows from `index`: `1 ≤ index ≤ rows − count + 1`,
8186    /// so every row of the block is one of the table's.
8187    fn checked_row_block(
8188        &mut self,
8189        index: &'a Expr<'a>,
8190        table: Symbol,
8191        rows: i64,
8192        count: i64,
8193    ) -> (Symbol, [Stmt<'a>; 2]) {
8194        let message = format!("row block out of range: {} has {rows} rows, numbered from 1, and a block of {count} rows must lie among them", self.interner.resolve(table));
8195        self.checked_first_row(index, rows - count + 1, &message)
8196    }
8197
8198    /// `Let at be index` and the check `1 ≤ at ≤ last`, failing with `message`.
8199    fn checked_first_row(
8200        &mut self,
8201        index: &'a Expr<'a>,
8202        last: i64,
8203        message: &str,
8204    ) -> (Symbol, [Stmt<'a>; 2]) {
8205        let at = self.fresh_desugar_temp("row_at");
8206        let in_range = self.te_bin(
8207            BinaryOpKind::And,
8208            self.te_bin(BinaryOpKind::GtEq, self.te_id(at), self.te_int(1)),
8209            self.te_bin(BinaryOpKind::LtEq, self.te_id(at), self.te_int(last)),
8210        );
8211        let message = self.interner.intern(message);
8212        let reason = self
8213            .ctx
8214            .alloc_imperative_expr(Expr::Literal(Literal::Text(message)));
8215        (
8216            at,
8217            [
8218                Stmt::Let {
8219                    var: at,
8220                    ty: None,
8221                    value: index,
8222                    mutable: false,
8223                },
8224                Stmt::RuntimeAssert {
8225                    condition: in_range,
8226                    hard: true,
8227                    dim: None,
8228                    reason: Some(reason),
8229                },
8230            ],
8231        )
8232    }
8233
8234    /// The flat 1-based item of element `j` (0-based) of row `at` (1-based) in a table `cols` wide.
8235    fn row_item(&self, at: Symbol, cols: i64, j: &'a Expr<'a>) -> &'a Expr<'a> {
8236        let row_base = self.te_bin(
8237            BinaryOpKind::Multiply,
8238            self.te_bin(BinaryOpKind::Subtract, self.te_id(at), self.te_int(1)),
8239            self.te_int(cols),
8240        );
8241        self.te_bin(
8242            BinaryOpKind::Add,
8243            self.te_bin(BinaryOpKind::Add, row_base, j),
8244            self.te_int(1),
8245        )
8246    }
8247
8248    /// `Let x be row at of T` → check `at`, then push `T[(at−1)·cols + j + 1]` for `j < cols` into a fresh
8249    /// `Seq` of `T`'s element kind — a copy, not a view.
8250    fn desugar_tensor_row(
8251        &mut self,
8252        var: Symbol,
8253        index: &'a Expr<'a>,
8254        table: Symbol,
8255        rows: i64,
8256        cols: i64,
8257        int: bool,
8258    ) -> Stmt<'a> {
8259        let (at, check) = self.checked_row_index(index, table, rows);
8260        let seq_sym = self.interner.intern("Seq");
8261        let elem_sym = self.interner.intern(if int { "Int" } else { "Real" });
8262        let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
8263            type_name: seq_sym,
8264            type_args: vec![TypeExpr::Named(elem_sym)],
8265            init_fields: vec![],
8266        });
8267        let j = self.fresh_desugar_temp("row_j");
8268        let item = self.row_item(at, cols, self.te_id(j));
8269        let loop_body = self.te_block(vec![
8270            Stmt::Push {
8271                value: self.te_index(self.te_id(table), item),
8272                collection: self.te_id(var),
8273            },
8274            Stmt::Set {
8275                target: j,
8276                value: self.te_bin(BinaryOpKind::Add, self.te_id(j), self.te_int(1)),
8277            },
8278        ]);
8279        let [bind_at, assert_at] = check;
8280        let body = self.te_block(vec![
8281            bind_at,
8282            assert_at,
8283            Stmt::Let {
8284                var,
8285                ty: None,
8286                value: new_seq,
8287                mutable: true,
8288            },
8289            Stmt::Let {
8290                var: j,
8291                ty: None,
8292                value: self.te_int(0),
8293                mutable: true,
8294            },
8295            Stmt::While {
8296                cond: self.te_bin(BinaryOpKind::Lt, self.te_id(j), self.te_int(cols)),
8297                body: loop_body,
8298                decreasing: None,
8299            },
8300        ]);
8301        Stmt::Splice { body }
8302    }
8303
8304    /// `Let X be rows I of T` → for each of `I`'s `n` indices in turn, `at = I[k]`: check `at` as a row of `T`, then push
8305    /// `T[(at−1)·cols + j + 1]` for `j < cols` into one fresh `Seq` of `T`'s element kind — row `k` of `X` is row `I[k]`.
8306    #[allow(clippy::too_many_arguments)]
8307    fn desugar_tensor_rows(
8308        &mut self,
8309        var: Symbol,
8310        idx: Symbol,
8311        n: i64,
8312        table: Symbol,
8313        rows: i64,
8314        cols: i64,
8315        int: bool,
8316    ) -> Stmt<'a> {
8317        let seq_sym = self.interner.intern("Seq");
8318        let elem_sym = self.interner.intern(if int { "Int" } else { "Real" });
8319        let new_seq = self.ctx.alloc_imperative_expr(Expr::New {
8320            type_name: seq_sym,
8321            type_args: vec![TypeExpr::Named(elem_sym)],
8322            init_fields: vec![],
8323        });
8324        let k = self.fresh_desugar_temp("rows_k");
8325        let index = self.te_index(self.te_id(idx), self.te_id(k));
8326        let (at, check) = self.checked_row_index(index, table, rows);
8327        let j = self.fresh_desugar_temp("row_j");
8328        let item = self.row_item(at, cols, self.te_id(j));
8329        let copy = self.te_block(vec![
8330            Stmt::Push {
8331                value: self.te_index(self.te_id(table), item),
8332                collection: self.te_id(var),
8333            },
8334            Stmt::Set {
8335                target: j,
8336                value: self.te_bin(BinaryOpKind::Add, self.te_id(j), self.te_int(1)),
8337            },
8338        ]);
8339        let [bind_at, assert_at] = check;
8340        let per_row = self.te_block(vec![
8341            bind_at,
8342            assert_at,
8343            Stmt::Let {
8344                var: j,
8345                ty: None,
8346                value: self.te_int(0),
8347                mutable: true,
8348            },
8349            Stmt::While {
8350                cond: self.te_bin(BinaryOpKind::Lt, self.te_id(j), self.te_int(cols)),
8351                body: copy,
8352                decreasing: None,
8353            },
8354            Stmt::Set {
8355                target: k,
8356                value: self.te_bin(BinaryOpKind::Add, self.te_id(k), self.te_int(1)),
8357            },
8358        ]);
8359        let body = self.te_block(vec![
8360            Stmt::Let {
8361                var,
8362                ty: None,
8363                value: new_seq,
8364                mutable: true,
8365            },
8366            Stmt::Let {
8367                var: k,
8368                ty: None,
8369                value: self.te_int(1),
8370                mutable: true,
8371            },
8372            Stmt::While {
8373                cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(k), self.te_int(n)),
8374                body: per_row,
8375                decreasing: None,
8376            },
8377        ]);
8378        Stmt::Splice { body }
8379    }
8380
8381    /// `Set row at of T to V` → check `at`, then `T[(at−1)·cols + j + 1] = V[j + 1]` for `j < cols`. A block of `count`
8382    /// rows (`Set rows from at of T to V`) is the same write over `count·cols` elements — the block's rows are
8383    /// contiguous in `T` from row `at` on — behind the check that its last row is one of `T`'s.
8384    fn desugar_tensor_set_row(
8385        &mut self,
8386        table: Symbol,
8387        index: &'a Expr<'a>,
8388        source: Symbol,
8389        rows: i64,
8390        cols: i64,
8391        count: i64,
8392    ) -> Stmt<'a> {
8393        let (at, check) = if count == 1 {
8394            self.checked_row_index(index, table, rows)
8395        } else {
8396            self.checked_row_block(index, table, rows, count)
8397        };
8398        let j = self.fresh_desugar_temp("row_j");
8399        let item = self.row_item(at, cols, self.te_id(j));
8400        let loop_body = self.te_block(vec![
8401            Stmt::SetIndex {
8402                collection: self.te_id(table),
8403                index: item,
8404                value: self.te_index(
8405                    self.te_id(source),
8406                    self.te_bin(BinaryOpKind::Add, self.te_id(j), self.te_int(1)),
8407                ),
8408            },
8409            Stmt::Set {
8410                target: j,
8411                value: self.te_bin(BinaryOpKind::Add, self.te_id(j), self.te_int(1)),
8412            },
8413        ]);
8414        let [bind_at, assert_at] = check;
8415        let body = self.te_block(vec![
8416            bind_at,
8417            assert_at,
8418            Stmt::Let {
8419                var: j,
8420                ty: None,
8421                value: self.te_int(0),
8422                mutable: true,
8423            },
8424            Stmt::While {
8425                cond: self.te_bin(BinaryOpKind::Lt, self.te_id(j), self.te_int(cols * count)),
8426                body: loop_body,
8427                decreasing: None,
8428            },
8429        ]);
8430        Stmt::Splice { body }
8431    }
8432
8433    /// `Let t be argmax of X` → scan `X` keeping the best value and its 1-based index, replacing only on a
8434    /// STRICTLY greater element (so the first of equal maxima wins), then bind `t` to that index.
8435    fn desugar_tensor_argmax(&mut self, var: Symbol, x: Symbol, total: i64) -> Stmt<'a> {
8436        let best = self.fresh_desugar_temp("am_best");
8437        let at = self.fresh_desugar_temp("am_at");
8438        let i = self.fresh_desugar_temp("am_i");
8439        let then_block = self.te_block(vec![
8440            Stmt::Set {
8441                target: best,
8442                value: self.te_index(self.te_id(x), self.te_id(i)),
8443            },
8444            Stmt::Set {
8445                target: at,
8446                value: self.te_id(i),
8447            },
8448        ]);
8449        let loop_body = self.te_block(vec![
8450            Stmt::If {
8451                cond: self.te_bin(
8452                    BinaryOpKind::Gt,
8453                    self.te_index(self.te_id(x), self.te_id(i)),
8454                    self.te_id(best),
8455                ),
8456                then_block,
8457                else_block: None,
8458            },
8459            Stmt::Set {
8460                target: i,
8461                value: self.te_bin(BinaryOpKind::Add, self.te_id(i), self.te_int(1)),
8462            },
8463        ]);
8464        let body = self.te_block(vec![
8465            Stmt::Let {
8466                var: best,
8467                ty: None,
8468                value: self.te_index(self.te_id(x), self.te_int(1)),
8469                mutable: true,
8470            },
8471            Stmt::Let {
8472                var: at,
8473                ty: None,
8474                value: self.te_int(1),
8475                mutable: true,
8476            },
8477            Stmt::Let {
8478                var: i,
8479                ty: None,
8480                value: self.te_int(2),
8481                mutable: true,
8482            },
8483            Stmt::While {
8484                cond: self.te_bin(BinaryOpKind::LtEq, self.te_id(i), self.te_int(total)),
8485                body: loop_body,
8486                decreasing: None,
8487            },
8488            Stmt::Let {
8489                var,
8490                ty: None,
8491                value: self.te_id(at),
8492                mutable: false,
8493            },
8494        ]);
8495        Stmt::Splice { body }
8496    }
8497
8498    fn desugar_tensor_transpose(
8499        &mut self,
8500        var: Symbol,
8501        x: Symbol,
8502        rows: i64,
8503        cols: i64,
8504        int: bool,
8505    ) -> Stmt<'a> {
8506        let tj = self.fresh_desugar_temp("tp_j");
8507        let ti = self.fresh_desugar_temp("tp_i");
8508        let new_seq = self.new_tensor_seq(int);
8509        // X[ti*cols + tj + 1] (1-based item)
8510        let idx = self.te_bin(
8511            BinaryOpKind::Add,
8512            self.te_bin(
8513                BinaryOpKind::Add,
8514                self.te_bin(BinaryOpKind::Multiply, self.te_id(ti), self.te_int(cols)),
8515                self.te_id(tj),
8516            ),
8517            self.te_int(1),
8518        );
8519        let inner_body = self.te_block(vec![
8520            Stmt::Push {
8521                value: self.te_index(self.te_id(x), idx),
8522                collection: self.te_id(var),
8523            },
8524            Stmt::Set {
8525                target: ti,
8526                value: self.te_bin(BinaryOpKind::Add, self.te_id(ti), self.te_int(1)),
8527            },
8528        ]);
8529        let inner = Stmt::While {
8530            cond: self.te_bin(BinaryOpKind::Lt, self.te_id(ti), self.te_int(rows)),
8531            body: inner_body,
8532            decreasing: None,
8533        };
8534        let outer_body = self.te_block(vec![
8535            Stmt::Let {
8536                var: ti,
8537                ty: None,
8538                value: self.te_int(0),
8539                mutable: true,
8540            },
8541            inner,
8542            Stmt::Set {
8543                target: tj,
8544                value: self.te_bin(BinaryOpKind::Add, self.te_id(tj), self.te_int(1)),
8545            },
8546        ]);
8547        let outer = Stmt::While {
8548            cond: self.te_bin(BinaryOpKind::Lt, self.te_id(tj), self.te_int(cols)),
8549            body: outer_body,
8550            decreasing: None,
8551        };
8552        let body = self.te_block(vec![
8553            Stmt::Let {
8554                var,
8555                ty: None,
8556                value: new_seq,
8557                mutable: true,
8558            },
8559            Stmt::Let {
8560                var: tj,
8561                ty: None,
8562                value: self.te_int(0),
8563                mutable: true,
8564            },
8565            outer,
8566        ]);
8567        Stmt::Splice { body }
8568    }
8569
8570    /// `Let Y be softmax/rmsnorm of X` in value position. `X` must be a tensor declared earlier; `Y` takes its
8571    /// shape. `None` if the value isn't a reduction op.
8572    fn try_parse_tensor_unary(&mut self, var: Symbol) -> ParseResult<Option<Stmt<'a>>> {
8573        // `Let H be moe of X with router Wr gate Wg up Wu down Wd choosing K of E` — a Mixture-of-Experts FFN.
8574        // X is `1×d`, Wr the router `d×E`, Wg/Wu the E experts' gate/up stacked `(E·dff)×d`, Wd the down
8575        // stacked `(E·d)×dff`. Routes X to its top-K experts and combines their SwiGLU by the renormalized
8576        // router weight (the Qwen3-30B-A3B block, general in d/dff/E/K).
8577        if self.check_word("moe") {
8578            let saved = self.backtrack_point();
8579            self.advance();
8580            if !self.check_preposition_is("of") {
8581                self.backtrack_to(&saved);
8582                return Ok(None);
8583            }
8584            self.advance();
8585            let want = |s: &mut Self, w: &str| -> ParseResult<()> {
8586                if !s.check_word(w) {
8587                    return Err(ParseError {
8588                        kind: ParseErrorKind::Custom(format!(
8589                            "`moe of X with router Wr gate Wg up Wu down Wd choosing K of E` — expected `{w}`."
8590                        )),
8591                        span: s.current_span(),
8592                    });
8593                }
8594                s.advance();
8595                Ok(())
8596            };
8597            let operand = |s: &mut Self, role: &str| -> ParseResult<(Symbol, i64, i64)> {
8598                let sym = s.peek().lexeme;
8599                let Some(&(r, c)) = s.tensor_shapes.get(&sym) else {
8600                    return Err(ParseError {
8601                        kind: ParseErrorKind::Custom(format!(
8602                            "`moe` {role} must be a declared tensor."
8603                        )),
8604                        span: s.current_span(),
8605                    });
8606                };
8607                s.advance();
8608                Ok((sym, r, c))
8609            };
8610            // `plus B` after a weight: its bias, of the shape the weight's outputs take.
8611            let bias = |s: &mut Self, role: &str, shape: (i64, i64)| -> ParseResult<Option<Symbol>> {
8612                if !s.check_word("plus") {
8613                    return Ok(None);
8614                }
8615                s.advance();
8616                let (b, r, c) = operand(s, role)?;
8617                if (r, c) != shape {
8618                    return Err(ParseError {
8619                        kind: ParseErrorKind::Custom(format!(
8620                            "`moe … {role} plus B` — B must be {}×{}, and `{}` is {r}×{c}.",
8621                            shape.0,
8622                            shape.1,
8623                            s.interner.resolve(b)
8624                        )),
8625                        span: s.current_span(),
8626                    });
8627                }
8628                Ok(Some(b))
8629            };
8630            let (x, _xr, d) = operand(self, "input X")?;
8631            want(self, "with")?;
8632            want(self, "router")?;
8633            let (wr, _wrr, ne) = operand(self, "router Wr")?; // Wr is d×E → cols = E
8634            let router_bias = bias(self, "router Wr", (1, ne))?;
8635            want(self, "gate")?;
8636            let (wg, wgr, _wgc) = operand(self, "gate Wg")?; // Wg is (E·dff)×d → rows = E·dff
8637            let fd = if ne > 0 { wgr / ne } else { 0 };
8638            let gate_bias = bias(self, "gate Wg", (ne, fd))?;
8639            want(self, "up")?;
8640            let (wu, _wur, _wuc) = operand(self, "up Wu")?;
8641            let up_bias = bias(self, "up Wu", (ne, fd))?;
8642            want(self, "down")?;
8643            let (wd, _wdr, _wdc) = operand(self, "down Wd")?;
8644            let down_bias = bias(self, "down Wd", (ne, d))?;
8645            want(self, "choosing")?;
8646            let k = self.take_int_literal().ok_or_else(|| ParseError {
8647                kind: ParseErrorKind::Custom(
8648                    "`moe … choosing K of E` — K must be an integer literal.".to_string(),
8649                ),
8650                span: self.current_span(),
8651            })?;
8652            if !self.check_preposition_is("of") {
8653                return Err(ParseError {
8654                    kind: ParseErrorKind::Custom(
8655                        "`moe … choosing K of E` — expected `of E`.".to_string(),
8656                    ),
8657                    span: self.current_span(),
8658                });
8659            }
8660            self.advance();
8661            let ne_lit = self.take_int_literal().ok_or_else(|| ParseError {
8662                kind: ParseErrorKind::Custom(
8663                    "`moe … choosing K of E` — E must be an integer literal.".to_string(),
8664                ),
8665                span: self.current_span(),
8666            })?;
8667            // `by sigmoid`: each expert scored by its own logit's sigmoid (Llama 4), not a softmax over every expert.
8668            let score = if self.check_word("by") {
8669                self.advance();
8670                if !self.check_word("sigmoid") {
8671                    return Err(ParseError {
8672                        kind: ParseErrorKind::Custom(
8673                            "`moe … choosing K of E by sigmoid` — expected `sigmoid` after `by`.".to_string(),
8674                        ),
8675                        span: self.current_span(),
8676                    });
8677                }
8678                self.advance();
8679                MoeScore::Sigmoid
8680            } else {
8681                MoeScore::Softmax
8682            };
8683            // `biased by B`: each expert chosen by its score plus `B[e]`, and weighed by its score alone (DeepSeek-V3).
8684            let selection_bias = if self.check_word("biased") {
8685                self.advance();
8686                if !self.check_word("by") {
8687                    return Err(ParseError {
8688                        kind: ParseErrorKind::Custom("`moe … biased by B` — expected `by` after `biased`.".to_string()),
8689                        span: self.current_span(),
8690                    });
8691                }
8692                self.advance();
8693                let (b, r, c) = operand(self, "selection bias B of `biased by B`")?;
8694                if (r, c) != (1, ne_lit) {
8695                    return Err(ParseError {
8696                        kind: ParseErrorKind::Custom(format!(
8697                            "`moe … biased by B` — B must be 1×{ne_lit}, and `{}` is {r}×{c}.",
8698                            self.interner.resolve(b)
8699                        )),
8700                        span: self.current_span(),
8701                    });
8702                }
8703                Some(b)
8704            } else {
8705                None
8706            };
8707            // `within the top G of N groups`: the experts in `N` groups, chosen only among the top `G` (DeepSeek-V3).
8708            let groups = if self.check_word("within") {
8709                self.advance();
8710                let clause = "`moe … within the top G of N groups`";
8711                for w in ["the", "top"] {
8712                    if !self.check_word(w) {
8713                        return Err(ParseError {
8714                            kind: ParseErrorKind::Custom(format!("{clause} — expected `{w}`.")),
8715                            span: self.current_span(),
8716                        });
8717                    }
8718                    self.advance();
8719                }
8720                let kept = self.take_int_literal().ok_or_else(|| ParseError {
8721                    kind: ParseErrorKind::Custom(format!("{clause} — G must be an integer literal.")),
8722                    span: self.current_span(),
8723                })?;
8724                if !self.check_preposition_is("of") {
8725                    return Err(ParseError {
8726                        kind: ParseErrorKind::Custom(format!("{clause} — expected `of N groups`.")),
8727                        span: self.current_span(),
8728                    });
8729                }
8730                self.advance();
8731                let count = self.take_int_literal().ok_or_else(|| ParseError {
8732                    kind: ParseErrorKind::Custom(format!("{clause} — N must be an integer literal.")),
8733                    span: self.current_span(),
8734                })?;
8735                if !self.check_word("groups") {
8736                    return Err(ParseError {
8737                        kind: ParseErrorKind::Custom(format!("{clause} — expected `groups`.")),
8738                        span: self.current_span(),
8739                    });
8740                }
8741                self.advance();
8742                Some((kept, count))
8743            } else {
8744                None
8745            };
8746            // `without renormalizing`: each kept expert weighs its softmax over every expert (DeepSeek-V2).
8747            let renormalize = !self.check_word("without");
8748            if !renormalize {
8749                self.advance();
8750                if !self.check_word("renormalizing") {
8751                    return Err(ParseError {
8752                        kind: ParseErrorKind::Custom(
8753                            "`moe … choosing K of E without renormalizing` — expected `renormalizing` after `without`.".to_string(),
8754                        ),
8755                        span: self.current_span(),
8756                    });
8757                }
8758                self.advance();
8759            }
8760            // `scaled by S`: every kept weight times `S`, after the renormalization (DeepSeek-V3's routed scale).
8761            let scale = if self.check_word("scaled") {
8762                self.advance();
8763                if !self.check_word("by") {
8764                    return Err(ParseError {
8765                        kind: ParseErrorKind::Custom("`moe … scaled by S` — expected `by` after `scaled`.".to_string()),
8766                        span: self.current_span(),
8767                    });
8768                }
8769                self.advance();
8770                let v = match self.peek().kind {
8771                    TokenType::Number(sym) => self.interner.resolve(sym).replace('_', "").parse::<f64>().ok(),
8772                    _ => None,
8773                };
8774                match v.filter(|v| v.is_finite() && *v > 0.0) {
8775                    Some(v) => {
8776                        self.advance();
8777                        Some(v)
8778                    }
8779                    None => {
8780                        return Err(ParseError {
8781                            kind: ParseErrorKind::Custom("`moe … scaled by S` — S must be a positive number.".to_string()),
8782                            span: self.current_span(),
8783                        })
8784                    }
8785                }
8786            } else {
8787                None
8788            };
8789            // `weighting the inputs`: a kept expert's weight scales the input it reads (Llama 4).
8790            let weigh_inputs = self.check_word("weighting");
8791            if weigh_inputs {
8792                self.advance();
8793                for w in ["the", "inputs"] {
8794                    if !self.check_word(w) {
8795                        return Err(ParseError {
8796                            kind: ParseErrorKind::Custom(format!(
8797                                "`moe … weighting the inputs` — expected `{w}`."
8798                            )),
8799                            span: self.current_span(),
8800                        });
8801                    }
8802                    self.advance();
8803                }
8804            }
8805            let act = self.take_moe_activation()?;
8806            if !(self.check(&TokenType::Period) || self.check(&TokenType::EOF)) {
8807                return Err(ParseError {
8808                    kind: ParseErrorKind::Custom(format!(
8809                        "`moe … choosing K of E` — `{}` is no clause here; the clauses come in this order: `by sigmoid`, \
8810                         `biased by B`, `within the top G of N groups`, `without renormalizing`, `scaled by S`, \
8811                         `weighting the inputs`, `using a swiglu clamped at L with slope A`.",
8812                        self.interner.resolve(self.peek().lexeme)
8813                    )),
8814                    span: self.current_span(),
8815                });
8816            }
8817            if ne_lit != ne {
8818                return Err(ParseError {
8819                    kind: ParseErrorKind::Custom(format!(
8820                        "`moe … of {ne_lit}` disagrees with the router Wr's {ne} columns."
8821                    )),
8822                    span: self.current_span(),
8823                });
8824            }
8825            if ne == 0 || wgr % ne != 0 {
8826                return Err(ParseError {
8827                    kind: ParseErrorKind::Custom(format!(
8828                        "`moe` gate Wg rows ({wgr}) must be E·dff (a multiple of E={ne})."
8829                    )),
8830                    span: self.current_span(),
8831                });
8832            }
8833            let dff = wgr / ne;
8834            if !(1..=ne).contains(&k) {
8835                return Err(ParseError {
8836                    kind: ParseErrorKind::Custom(format!(
8837                        "`moe … choosing {k} of {ne}` — K must be in 1..=E."
8838                    )),
8839                    span: self.current_span(),
8840                });
8841            }
8842            if let Some((kept, count)) = groups {
8843                let why = if count < 1 || ne % count != 0 {
8844                    Some(format!("N must divide the {ne} experts into groups of one size"))
8845                } else if !(1..=count).contains(&kept) {
8846                    Some("G must be in 1..=N".to_string())
8847                } else if k > kept * (ne / count) {
8848                    Some(format!("the top {kept} groups hold {} experts, fewer than the {k} chosen", kept * (ne / count)))
8849                } else {
8850                    None
8851                };
8852                if let Some(why) = why {
8853                    return Err(ParseError {
8854                        kind: ParseErrorKind::Custom(format!("`moe … within the top {kept} of {count} groups` — {why}.")),
8855                        span: self.current_span(),
8856                    });
8857                }
8858            }
8859            self.user_bound.insert(var);
8860            self.bind_tensor(var, (1, d), false);
8861            return Ok(Some(self.desugar_tensor_moe(
8862                var,
8863                x,
8864                wr,
8865                wg,
8866                wu,
8867                wd,
8868                d,
8869                dff,
8870                ne,
8871                k,
8872                MoeOptions {
8873                    router_bias,
8874                    biases: [gate_bias, up_bias, down_bias],
8875                    score,
8876                    selection_bias,
8877                    groups,
8878                    renormalize,
8879                    scale,
8880                    weigh_inputs,
8881                    act,
8882                },
8883            )));
8884        }
8885        // `Let C be causal attention of Q over K and V with H heads KV groups from P` — a BLOCK of query rows against
8886        // a cache: Q is `T×(H·HD)`, K and V `L×(KV·HD)`, and row r (1-based) is the query at position `P + r − 1`,
8887        // attending the first `P + r − 1` cached positions — a prefill chunk whose rows appended their keys and values
8888        // first. Checked once at run time: `P ≥ 1` and `P + T − 1 ≤ L`. C is `T×(H·HD)`.
8889        if self.check_word("causal") && self.peek_word_at(1, "attention") {
8890            self.advance();
8891            self.advance();
8892            let want = |s: &mut Self, w: &str| -> ParseResult<()> {
8893                if !s.check_word(w) && !s.check_preposition_is(w) {
8894                    return Err(ParseError {
8895                        kind: ParseErrorKind::Custom(format!("`causal attention of Q over K and V with H heads KV groups from P` — expected `{w}`.")),
8896                        span: s.current_span(),
8897                    });
8898                }
8899                s.advance();
8900                Ok(())
8901            };
8902            let operand = |s: &mut Self, role: &str| -> ParseResult<(Symbol, i64, i64)> {
8903                let sym = s.peek().lexeme;
8904                let Some(&(r, c)) = s.tensor_shapes.get(&sym) else {
8905                    return Err(ParseError {
8906                        kind: ParseErrorKind::Custom(format!(
8907                            "`causal attention` {role} must be a declared tensor."
8908                        )),
8909                        span: s.current_span(),
8910                    });
8911                };
8912                s.advance();
8913                Ok((sym, r, c))
8914            };
8915            want(self, "of")?;
8916            let (q, qr, qc) = operand(self, "query block Q")?;
8917            want(self, "over")?;
8918            let (kc, kcr, kcc) = operand(self, "keys K")?;
8919            want(self, "and")?;
8920            let (vc, vr, vcc) = operand(self, "values V")?;
8921            want(self, "with")?;
8922            let h = self.take_int_literal().ok_or_else(|| ParseError {
8923                kind: ParseErrorKind::Custom(
8924                    "`causal attention … with H heads` — H must be an integer literal.".to_string(),
8925                ),
8926                span: self.current_span(),
8927            })?;
8928            want(self, "heads")?;
8929            let kv = self.take_int_literal().ok_or_else(|| ParseError {
8930                kind: ParseErrorKind::Custom(
8931                    "`causal attention … KV groups` — KV must be an integer literal.".to_string(),
8932                ),
8933                span: self.current_span(),
8934            })?;
8935            want(self, "groups")?;
8936            if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
8937                return Err(ParseError { kind: ParseErrorKind::Custom("`causal attention … from P` — expected `from` and the first row's position.".to_string()), span: self.current_span() });
8938            }
8939            self.advance();
8940            let from = self.parse_row_index()?.ok_or_else(|| ParseError {
8941                kind: ParseErrorKind::Custom("`causal attention … from P` — P is a whole number, a name or a parenthesized expression.".to_string()),
8942                span: self.current_span(),
8943            })?;
8944            let window = self.take_window()?;
8945            let scale = self.take_attention_scale()?;
8946            let cap = self.take_cap("attention …")?;
8947            let sinks = self.take_sinks(h)?;
8948            if h <= 0 || qc % h != 0 {
8949                return Err(ParseError { kind: ParseErrorKind::Custom(format!("`causal attention` query Q cols ({qc}) must be H·HD (a multiple of H={h}).")), span: self.current_span() });
8950            }
8951            let hd = qc / h;
8952            if kv <= 0 || h % kv != 0 || kcc != kv * hd || vr != kcr || vcc % kv != 0 || vcc == 0 {
8953                return Err(ParseError {
8954                    kind: ParseErrorKind::Custom(format!("`causal attention` keys K must be L×(KV·HD) = L×{kv}·{hd} with KV dividing H={h}, and values V L×(KV·VD) — got {kcr}×{kcc} and {vr}×{vcc}.")),
8955                    span: self.current_span(),
8956                });
8957            }
8958            let vd = vcc / kv;
8959            if qr < 1 {
8960                return Err(ParseError {
8961                    kind: ParseErrorKind::Custom(
8962                        "`causal attention` query block Q has no rows.".to_string(),
8963                    ),
8964                    span: self.current_span(),
8965                });
8966            }
8967            self.user_bound.insert(var);
8968            self.bind_tensor(var, (qr, h * vd), false);
8969            return Ok(Some(self.desugar_tensor_attention_block(
8970                var,
8971                q,
8972                kc,
8973                vc,
8974                AttentionShape { h, kv, hd, vd, l: kcr, rows: qr },
8975                Some(from),
8976                AttentionOptions { window, scale, cap, sinks },
8977            )));
8978        }
8979        // `Let Y be causal conv of X with weight WC state CS over T taps` — depthwise causal conv1d with a
8980        // carried decode ring. X is 1×C, WC and CS are T×C.
8981        if self.check_word("causal") {
8982            let saved = self.backtrack_point();
8983            self.advance();
8984            if !self.check_word("conv") {
8985                self.backtrack_to(&saved);
8986                return Ok(None);
8987            }
8988            self.advance();
8989            if !self.check_preposition_is("of") {
8990                self.backtrack_to(&saved);
8991                return Ok(None);
8992            }
8993            self.advance();
8994            let want = |sf: &mut Self, w: &str| -> ParseResult<()> {
8995                if !sf.check_word(w) {
8996                    return Err(ParseError {
8997                        kind: ParseErrorKind::Custom(format!("`causal conv of X with weight WC state CS over T taps` — expected `{w}`.")),
8998                        span: sf.current_span(),
8999                    });
9000                }
9001                sf.advance();
9002                Ok(())
9003            };
9004            let operand = |sf: &mut Self, role: &str| -> ParseResult<(Symbol, i64, i64)> {
9005                let sym = sf.peek().lexeme;
9006                let Some(&(r, c)) = sf.tensor_shapes.get(&sym) else {
9007                    return Err(ParseError {
9008                        kind: ParseErrorKind::Custom(format!(
9009                            "`causal conv` {role} must be a declared tensor."
9010                        )),
9011                        span: sf.current_span(),
9012                    });
9013                };
9014                sf.advance();
9015                Ok((sym, r, c))
9016            };
9017            let (x, rows, chan) = operand(self, "input X")?;
9018            want(self, "with")?;
9019            want(self, "weight")?;
9020            let (wc, wcr, wcc) = operand(self, "weight WC")?;
9021            want(self, "state")?;
9022            let (cs, csr, csc) = operand(self, "state CS")?;
9023            want(self, "over")?;
9024            let taps = self.take_int_literal().ok_or_else(|| ParseError {
9025                kind: ParseErrorKind::Custom(
9026                    "`causal conv … over T taps` — T must be an integer literal.".to_string(),
9027                ),
9028                span: self.current_span(),
9029            })?;
9030            want(self, "taps")?;
9031            if taps <= 0 {
9032                return Err(ParseError {
9033                    kind: ParseErrorKind::Custom(format!(
9034                        "`causal conv … over {taps} taps` — T must be positive."
9035                    )),
9036                    span: self.current_span(),
9037                });
9038            }
9039            if (wcr, wcc) != (taps, chan) {
9040                return Err(ParseError { kind: ParseErrorKind::Custom(format!("`causal conv` weight WC must be {taps} by {chan} (taps by channels) — got {wcr} by {wcc}.")), span: self.current_span() });
9041            }
9042            if (csr, csc) != (taps, chan) {
9043                return Err(ParseError { kind: ParseErrorKind::Custom(format!("`causal conv` state CS must be {taps} by {chan} (the ring of the last {taps} inputs) — got {csr} by {csc}.")), span: self.current_span() });
9044            }
9045            self.user_bound.insert(var);
9046            self.bind_tensor(var, (rows, chan), false);
9047            return Ok(Some(
9048                self.desugar_tensor_causal_conv(var, x, wc, cs, taps, chan, rows),
9049            ));
9050        }
9051        // `Let C be full attention of Q over K and V with H heads` — BIDIRECTIONAL T×T self-attention (the
9052        // vision tower). Q/K/V are T×(H·HD); no causal mask, no KV cache.
9053        if self.check_word("full") {
9054            let saved = self.backtrack_point();
9055            self.advance();
9056            if !self.check_word("attention") {
9057                self.backtrack_to(&saved);
9058                return Ok(None);
9059            }
9060            self.advance();
9061            if !self.check_preposition_is("of") {
9062                self.backtrack_to(&saved);
9063                return Ok(None);
9064            }
9065            self.advance();
9066            let want = |sf: &mut Self, w: &str| -> ParseResult<()> {
9067                if !sf.check_word(w) {
9068                    return Err(ParseError {
9069                        kind: ParseErrorKind::Custom(format!(
9070                            "`full attention of Q over K and V with H heads` — expected `{w}`."
9071                        )),
9072                        span: sf.current_span(),
9073                    });
9074                }
9075                sf.advance();
9076                Ok(())
9077            };
9078            let operand = |sf: &mut Self, role: &str| -> ParseResult<(Symbol, i64, i64)> {
9079                let sym = sf.peek().lexeme;
9080                let Some(&(r, c)) = sf.tensor_shapes.get(&sym) else {
9081                    return Err(ParseError {
9082                        kind: ParseErrorKind::Custom(format!(
9083                            "`full attention` {role} must be a declared tensor."
9084                        )),
9085                        span: sf.current_span(),
9086                    });
9087                };
9088                sf.advance();
9089                Ok((sym, r, c))
9090            };
9091            let (q, qr, qc) = operand(self, "query Q")?;
9092            want(self, "over")?;
9093            let (kk, kr, kc) = operand(self, "keys K")?;
9094            want(self, "and")?;
9095            let (vv, vr, vc) = operand(self, "values V")?;
9096            want(self, "with")?;
9097            let h = self.take_int_literal().ok_or_else(|| ParseError {
9098                kind: ParseErrorKind::Custom(
9099                    "`full attention … with H heads` — H must be an integer literal.".to_string(),
9100                ),
9101                span: self.current_span(),
9102            })?;
9103            want(self, "heads")?;
9104            if h <= 0 || qc % h != 0 {
9105                return Err(ParseError {
9106                    kind: ParseErrorKind::Custom(format!(
9107                        "`full attention` Q's {qc} columns must be H·HD (a multiple of H={h})."
9108                    )),
9109                    span: self.current_span(),
9110                });
9111            }
9112            if (kr, kc) != (qr, qc) || (vr, vc) != (qr, qc) {
9113                return Err(ParseError { kind: ParseErrorKind::Custom(format!("`full attention` K and V must match Q's shape {qr} by {qc} — got {kr} by {kc} and {vr} by {vc}.")), span: self.current_span() });
9114            }
9115            self.user_bound.insert(var);
9116            self.bind_tensor(var, (qr, qc), false);
9117            return Ok(Some(self.desugar_tensor_full_attention(
9118                var,
9119                q,
9120                kk,
9121                vv,
9122                h,
9123                qc / h,
9124                qr,
9125            )));
9126        }
9127        // `Let O be gated delta of Q over K and V with gate Z beta B decay A state S norm GN with HV heads
9128        // HK groups` — the Gated DeltaNet step (48 of Qwen3.8-27B's 64 layers).
9129        if self.check_word("gated") {
9130            let saved = self.backtrack_point();
9131            self.advance();
9132            if !self.check_word("delta") {
9133                self.backtrack_to(&saved);
9134                return Ok(None);
9135            }
9136            self.advance();
9137            if !self.check_preposition_is("of") {
9138                self.backtrack_to(&saved);
9139                return Ok(None);
9140            }
9141            self.advance();
9142            let want = |sf: &mut Self, w: &str| -> ParseResult<()> {
9143                if !sf.check_word(w) {
9144                    return Err(ParseError {
9145                        kind: ParseErrorKind::Custom(format!(
9146                            "`gated delta of Q over K and V with gate Z beta B decay A state S norm GN with HV heads HK groups` — expected `{w}`."
9147                        )),
9148                        span: sf.current_span(),
9149                    });
9150                }
9151                sf.advance();
9152                Ok(())
9153            };
9154            let operand = |sf: &mut Self, role: &str| -> ParseResult<(Symbol, i64, i64)> {
9155                let sym = sf.peek().lexeme;
9156                let Some(&(r, c)) = sf.tensor_shapes.get(&sym) else {
9157                    return Err(ParseError {
9158                        kind: ParseErrorKind::Custom(format!(
9159                            "`gated delta` {role} must be a declared tensor."
9160                        )),
9161                        span: sf.current_span(),
9162                    });
9163                };
9164                sf.advance();
9165                Ok((sym, r, c))
9166            };
9167            let (q, qr, qc) = operand(self, "query Q")?;
9168            want(self, "over")?;
9169            let (kk, kr, kc) = operand(self, "keys K")?;
9170            want(self, "and")?;
9171            let (vv, vr, vc) = operand(self, "values V")?;
9172            want(self, "with")?;
9173            let sigmoid_gate = self.check_word("sigmoid");
9174            if sigmoid_gate {
9175                self.advance();
9176            }
9177            want(self, "gate")?;
9178            let (zz, zr, zc) = operand(self, "gate Z")?;
9179            want(self, "beta")?;
9180            let (bb, br, bc) = operand(self, "beta B")?;
9181            want(self, "decay")?;
9182            let (aa, ar, ac) = operand(self, "decay A")?;
9183            // A block of rows runs the recurrence row after row, so every per-token operand has the block's rows.
9184            if [kr, vr, zr, br, ar].iter().any(|r| *r != qr) {
9185                return Err(ParseError {
9186                    kind: ParseErrorKind::Custom(format!("`gated delta` Q, K, V, gate Z, beta B and decay A must have the same rows (one per token) — got {qr}, {kr}, {vr}, {zr}, {br} and {ar}.")),
9187                    span: self.current_span(),
9188                });
9189            }
9190            want(self, "state")?;
9191            let (ss, sr, sc) = operand(self, "state S")?;
9192            want(self, "norm")?;
9193            let (gg, _gr, gc) = operand(self, "norm GN")?;
9194            want(self, "with")?;
9195            let hv = self.take_int_literal().ok_or_else(|| ParseError {
9196                kind: ParseErrorKind::Custom(
9197                    "`gated delta … with HV heads` — HV must be an integer literal.".to_string(),
9198                ),
9199                span: self.current_span(),
9200            })?;
9201            want(self, "heads")?;
9202            let hk = self.take_int_literal().ok_or_else(|| ParseError {
9203                kind: ParseErrorKind::Custom(
9204                    "`gated delta … HK groups` — HK must be an integer literal.".to_string(),
9205                ),
9206                span: self.current_span(),
9207            })?;
9208            want(self, "groups")?;
9209            if hk <= 0 || qc % hk != 0 {
9210                return Err(ParseError {
9211                    kind: ParseErrorKind::Custom(format!(
9212                        "`gated delta` Q's {qc} columns must be HK·D (a multiple of HK={hk})."
9213                    )),
9214                    span: self.current_span(),
9215                });
9216            }
9217            let d = qc / hk;
9218            if hv <= 0 || hv % hk != 0 {
9219                return Err(ParseError { kind: ParseErrorKind::Custom(format!("`gated delta … {hv} heads {hk} groups` — HV must be a positive multiple of HK.")), span: self.current_span() });
9220            }
9221            if kc != qc {
9222                return Err(ParseError {
9223                    kind: ParseErrorKind::Custom(format!(
9224                        "`gated delta` keys K must match Q's {qc} columns — got {kc}."
9225                    )),
9226                    span: self.current_span(),
9227                });
9228            }
9229            if vc != hv * d || zc != hv * d {
9230                return Err(ParseError { kind: ParseErrorKind::Custom(format!("`gated delta` V and gate Z must be HV·D = {}·{d} = {} columns — got {vc} and {zc}.", hv, hv * d)), span: self.current_span() });
9231            }
9232            if bc != hv || ac != hv {
9233                return Err(ParseError { kind: ParseErrorKind::Custom(format!("`gated delta` beta B and decay A must be one value per head (HV={hv}) — got {bc} and {ac}.")), span: self.current_span() });
9234            }
9235            if (sr, sc) != (hv * d, d) {
9236                return Err(ParseError {
9237                    kind: ParseErrorKind::Custom(format!(
9238                        "`gated delta` state S must be HV·D by D = {} by {d} — got {sr} by {sc}.",
9239                        hv * d
9240                    )),
9241                    span: self.current_span(),
9242                });
9243            }
9244            if gc != d {
9245                return Err(ParseError { kind: ParseErrorKind::Custom(format!("`gated delta` norm GN must have D={d} columns (shared across heads) — got {gc}.")), span: self.current_span() });
9246            }
9247            let eps = self.take_epsilon("gated delta")?;
9248            self.user_bound.insert(var);
9249            self.bind_tensor(var, (qr, hv * d), false);
9250            return Ok(Some(self.desugar_tensor_gated_delta(
9251                var, q, kk, vv, zz, bb, aa, ss, gg, hv, hk, d, qr, eps, sigmoid_gate,
9252            )));
9253        }
9254        // `Let C be lut attention of Q over K and V with H heads KV groups [up to P] with table E and scale M shifted
9255        // right by S` — an int8 model's single-query attention, its softmax an exp LUT: the weights are `E` at the
9256        // index `clamp(((max − s_j)·M) >> S, 0, len(E) − 1)` and the context their weighted mean, floored. Every tensor
9257        // is a tensor of Int; the shapes are the float form's.
9258        if self.check_word("lut") && self.peek_word_at(1, "attention") {
9259            self.advance();
9260            self.advance();
9261            let form = "`lut attention of Q over K and V with H heads KV groups up to P with table E and scale M shifted right by S`";
9262            let refuse = |s: &Self, why: String| ParseError {
9263                kind: ParseErrorKind::Custom(format!("{form} — {why}")),
9264                span: s.current_span(),
9265            };
9266            let want = |s: &mut Self, w: &str| -> ParseResult<()> {
9267                if !s.check_word(w) {
9268                    return Err(refuse(s, format!("expected `{w}`.")));
9269                }
9270                s.advance();
9271                Ok(())
9272            };
9273            let operand = |s: &mut Self, role: &str| -> ParseResult<(Symbol, i64, i64)> {
9274                let sym = s.peek().lexeme;
9275                let Some(&(r, c)) = s.tensor_shapes.get(&sym) else {
9276                    return Err(refuse(s, format!("{role} must be a declared tensor.")));
9277                };
9278                if !s.tensor_int.contains(&sym) {
9279                    return Err(refuse(s, format!("{role} must be a tensor of Int.")));
9280                }
9281                s.advance();
9282                Ok((sym, r, c))
9283            };
9284            want(self, "of")?;
9285            let (q, qr, qc) = operand(self, "the query Q")?;
9286            want(self, "over")?;
9287            let (kc, kcr, kcc) = operand(self, "the keys K")?;
9288            want(self, "and")?;
9289            let (vc, vcr, vcc) = operand(self, "the values V")?;
9290            want(self, "with")?;
9291            let h = self
9292                .take_int_literal()
9293                .ok_or_else(|| refuse(self, "H must be an integer literal.".into()))?;
9294            want(self, "heads")?;
9295            let kv = self
9296                .take_int_literal()
9297                .ok_or_else(|| refuse(self, "KV must be an integer literal.".into()))?;
9298            want(self, "groups")?;
9299            let upto = if self.check_word("up") {
9300                self.advance();
9301                let is_to = self.check(&TokenType::To)
9302                    || matches!(&self.peek().kind, TokenType::Preposition(sym) if self.interner.resolve(*sym) == "to");
9303                if !is_to {
9304                    return Err(refuse(self, "expected `to` after `up`.".into()));
9305                }
9306                self.advance();
9307                Some(self.parse_row_index()?.ok_or_else(|| {
9308                    refuse(self, "P is a whole number, a name or a parenthesized expression.".into())
9309                })?)
9310            } else {
9311                None
9312            };
9313            want(self, "with")?;
9314            want(self, "table")?;
9315            let (table, tr, n) = operand(self, "the table E")?;
9316            want(self, "and")?;
9317            want(self, "scale")?;
9318            let m = self
9319                .take_signed_int_literal()
9320                .ok_or_else(|| refuse(self, "M must be an integer literal.".into()))?;
9321            want(self, "shifted")?;
9322            want(self, "right")?;
9323            want(self, "by")?;
9324            let s = self
9325                .take_int_literal()
9326                .filter(|s| (0..=63).contains(s))
9327                .ok_or_else(|| refuse(self, "S must be a whole number from 0 to 63.".into()))?;
9328            if qr != 1 {
9329                return Err(refuse(self, format!("Q is one query row, and it has {qr} rows.")));
9330            }
9331            if h <= 0 || qc % h != 0 {
9332                return Err(refuse(self, format!("Q's {qc} columns must be H·HD, a multiple of H = {h}.")));
9333            }
9334            let hd = qc / h;
9335            if kv <= 0 || h % kv != 0 || kcc != kv * hd {
9336                return Err(refuse(
9337                    self,
9338                    format!("K's {kcc} columns must be KV·HD = {kv}·{hd}, and H = {h} a multiple of KV."),
9339                ));
9340            }
9341            if (vcr, vcc) != (kcr, kcc) {
9342                return Err(refuse(self, format!("V is {vcr} by {vcc}, and K is {kcr} by {kcc}.")));
9343            }
9344            if tr != 1 || n < 1 {
9345                return Err(refuse(self, format!("the table E is one row of entries, and it is {tr} by {n}.")));
9346            }
9347            self.user_bound.insert(var);
9348            self.bind_tensor(var, (1, qc), true);
9349            return Ok(Some(self.desugar_tensor_lut_attention(
9350                var,
9351                q,
9352                kc,
9353                vc,
9354                table,
9355                (h, kv, hd, kcr, n),
9356                upto,
9357                (m, s),
9358            )));
9359        }
9360        // `Let C be attention of Q over K and V with H heads KV groups` — single-query multi-head GQA attention.
9361        // Q is `1×(H·HD)`, K/V are `L×(KV·HD)` (the decode KV cache). Query head g attends to kv head (g-1)/(H/KV)
9362        // over all L cached keys. Lowers to one parallel `attn_decode` kernel (block per query head).
9363        // `… up to P` attends over the first P cached positions only — a decode step's cache holds every position
9364        // it will ever hold, and only the first P are written yet. P is checked, once, to be in `1..=L`.
9365        if self.check_word("attention") {
9366            let saved = self.backtrack_point();
9367            self.advance();
9368            if !self.check_preposition_is("of") {
9369                self.backtrack_to(&saved);
9370                return Ok(None);
9371            }
9372            self.advance();
9373            let want = |s: &mut Self, w: &str| -> ParseResult<()> {
9374                if !s.check_word(w) {
9375                    return Err(ParseError {
9376                        kind: ParseErrorKind::Custom(format!("`attention of Q over K and V with H heads KV groups` — expected `{w}`.")),
9377                        span: s.current_span(),
9378                    });
9379                }
9380                s.advance();
9381                Ok(())
9382            };
9383            let operand = |s: &mut Self, role: &str| -> ParseResult<(Symbol, i64, i64)> {
9384                let sym = s.peek().lexeme;
9385                let Some(&(r, c)) = s.tensor_shapes.get(&sym) else {
9386                    return Err(ParseError {
9387                        kind: ParseErrorKind::Custom(format!(
9388                            "`attention` {role} must be a declared tensor."
9389                        )),
9390                        span: s.current_span(),
9391                    });
9392                };
9393                s.advance();
9394                Ok((sym, r, c))
9395            };
9396            let (q, qr, qc) = operand(self, "query Q")?; // Q is 1×(H·HD)
9397            if qr != 1 {
9398                return Err(ParseError {
9399                    kind: ParseErrorKind::Custom(format!(
9400                        "`attention of Q …` is one query row, and Q has {qr} rows — a block of query rows, each attending up to its own position, is `causal attention of Q over K and V with H heads KV groups from P`."
9401                    )),
9402                    span: self.current_span(),
9403                });
9404            }
9405            want(self, "over")?;
9406            let (kc, kcr, kcc) = operand(self, "keys K")?; // K is L×(KV·HD)
9407            want(self, "and")?;
9408            let (vc, vr, vcc) = operand(self, "values V")?;
9409            want(self, "with")?;
9410            let h = self.take_int_literal().ok_or_else(|| ParseError {
9411                kind: ParseErrorKind::Custom(
9412                    "`attention … with H heads` — H must be an integer literal.".to_string(),
9413                ),
9414                span: self.current_span(),
9415            })?;
9416            want(self, "heads")?;
9417            let kv = self.take_int_literal().ok_or_else(|| ParseError {
9418                kind: ParseErrorKind::Custom(
9419                    "`attention … KV groups` — KV must be an integer literal.".to_string(),
9420                ),
9421                span: self.current_span(),
9422            })?;
9423            want(self, "groups")?;
9424            let upto = if self.check_word("up") {
9425                self.advance();
9426                let is_to = self.check(&TokenType::To)
9427                    || matches!(&self.peek().kind, TokenType::Preposition(sym) if self.interner.resolve(*sym) == "to");
9428                if !is_to {
9429                    return Err(ParseError {
9430                        kind: ParseErrorKind::Custom(
9431                            "`attention … up to P` — expected `to` after `up`.".to_string(),
9432                        ),
9433                        span: self.current_span(),
9434                    });
9435                }
9436                self.advance();
9437                Some(self.parse_row_index()?.ok_or_else(|| ParseError {
9438                    kind: ParseErrorKind::Custom("`attention … up to P` — P is a whole number, a name or a parenthesized expression.".to_string()),
9439                    span: self.current_span(),
9440                })?)
9441            } else {
9442                None
9443            };
9444            let window = self.take_window()?;
9445            let scale = self.take_attention_scale()?;
9446            let cap = self.take_cap("attention …")?;
9447            let sinks = self.take_sinks(h)?;
9448            if h <= 0 || qc % h != 0 {
9449                return Err(ParseError {
9450                    kind: ParseErrorKind::Custom(format!(
9451                        "`attention` query Q cols ({qc}) must be H·HD (a multiple of H={h})."
9452                    )),
9453                    span: self.current_span(),
9454                });
9455            }
9456            let hd = qc / h;
9457            if kv <= 0 || kcc != kv * hd {
9458                return Err(ParseError {
9459                    kind: ParseErrorKind::Custom(format!(
9460                        "`attention` keys K cols ({kcc}) must be KV·HD = {kv}·{hd}."
9461                    )),
9462                    span: self.current_span(),
9463                });
9464            }
9465            if vr != kcr || vcc == 0 || vcc % kv != 0 {
9466                return Err(ParseError {
9467                    kind: ParseErrorKind::Custom(format!(
9468                        "`attention` values V must be L×(KV·VD) — K's {kcr} rows, and columns a multiple of KV={kv} — got {vr}×{vcc}."
9469                    )),
9470                    span: self.current_span(),
9471                });
9472            }
9473            let vd = vcc / kv;
9474            let l = kcr; // cache length = K's rows
9475            self.user_bound.insert(var);
9476            self.bind_tensor(var, (1, h * vd), false);
9477            return Ok(Some(self.desugar_tensor_attention(
9478                var,
9479                q,
9480                kc,
9481                vc,
9482                AttentionShape { h, kv, hd, vd, l, rows: 1 },
9483                upto,
9484                AttentionOptions { window, scale, cap, sinks },
9485            )));
9486        }
9487        // `Let x be row tok of Emb` — the embedding lookup: row `tok` (1-based) of a declared tensor, as a
9488        // `1 × cols` tensor of the table's element kind.
9489        if let Some(stmt) = self.try_parse_tensor_row(var)? {
9490            return Ok(Some(stmt));
9491        }
9492        // `Let X be rows toks of Emb` — the block of them: row `k` of `X` is row `toks[k]` of the table.
9493        if let Some(stmt) = self.try_parse_tensor_rows(var)? {
9494            return Ok(Some(stmt));
9495        }
9496        // `Let t be argmax of X` — the 1-based index of the FIRST maximal element of a declared tensor.
9497        if self.check_word("argmax") && self.peek_word_at(1, "of") {
9498            let saved = self.backtrack_point();
9499            self.advance();
9500            self.advance();
9501            let x = self.peek().lexeme;
9502            let Some(&(r, c)) = self.tensor_shapes.get(&x) else {
9503                self.backtrack_to(&saved);
9504                return Ok(None);
9505            };
9506            self.advance();
9507            self.user_bound.insert(var);
9508            return Ok(Some(self.desugar_tensor_argmax(var, x, r * c)));
9509        }
9510        // `Let Kt be transpose of K` — the matrix transpose (an `R by C` tensor becomes `C by R`), needed for
9511        // attention's `Q · Kᵀ`. Kt[j][i] = K[i][j].
9512        if self.check_word("transpose") {
9513            let saved = self.backtrack_point();
9514            self.advance();
9515            if !self.check_preposition_is("of") {
9516                self.backtrack_to(&saved);
9517                return Ok(None);
9518            }
9519            self.advance();
9520            let x = self.peek().lexeme;
9521            let Some(&(r, c)) = self.tensor_shapes.get(&x) else {
9522                return Err(ParseError {
9523                    kind: ParseErrorKind::Custom(
9524                        "`transpose of X` needs a declared tensor X.".to_string(),
9525                    ),
9526                    span: self.current_span(),
9527                });
9528            };
9529            self.advance();
9530            self.user_bound.insert(var);
9531            // A transpose moves elements without computing on them: the result keeps X's element kind.
9532            let int = self.tensor_int.contains(&x);
9533            self.bind_tensor(var, (c, r), int);
9534            return Ok(Some(self.desugar_tensor_transpose(var, x, r, c, int)));
9535        }
9536        // `Let Qr be rope of Q with cos RC and sin RS` — RoPE-NEOX: rotate each head's pairs `(i, i+half)`
9537        // by the precomputed `(cos, sin)` at its row's position. `Q` is `R × (H·hd)`; `RC`/`RS` are `R × half`
9538        // (row `r` of `Q` turns by row `r` of the tables), `1 × half` (every row turns by the same angles), or — with
9539        // `from position P` — a cache of angles per position, row `r` turning by row `P + r − 1`;
9540        // `hd = 2·half`, `H = cols(Q)/hd`.
9541        if self.check_word("rope") {
9542            let saved = self.backtrack_point();
9543            self.advance();
9544            if !self.check_preposition_is("of") {
9545                self.backtrack_to(&saved);
9546                return Ok(None);
9547            }
9548            self.advance();
9549            let q = self.peek().lexeme;
9550            let Some(&(qr, qc)) = self.tensor_shapes.get(&q) else {
9551                return Err(ParseError {
9552                    kind: ParseErrorKind::Custom(
9553                        "`rope of Q` needs a declared tensor Q.".to_string(),
9554                    ),
9555                    span: self.current_span(),
9556                });
9557            };
9558            self.advance();
9559            if !self.check_word("with") {
9560                return Err(ParseError {
9561                    kind: ParseErrorKind::Custom(
9562                        "`rope of Q with cos RC and sin RS`: expected `with`.".to_string(),
9563                    ),
9564                    span: self.current_span(),
9565                });
9566            }
9567            self.advance();
9568            if !self.check_word("cos") {
9569                return Err(ParseError {
9570                    kind: ParseErrorKind::Custom(
9571                        "`rope … with cos RC …`: expected `cos`.".to_string(),
9572                    ),
9573                    span: self.current_span(),
9574                });
9575            }
9576            self.advance();
9577            let rc = self.peek().lexeme;
9578            let Some(&(rcr, half)) = self.tensor_shapes.get(&rc) else {
9579                return Err(ParseError {
9580                    kind: ParseErrorKind::Custom(
9581                        "`rope … with cos RC`: RC must be a declared `R by half` tensor."
9582                            .to_string(),
9583                    ),
9584                    span: self.current_span(),
9585                });
9586            };
9587            self.advance();
9588            if !self.check_word("and") {
9589                return Err(ParseError {
9590                    kind: ParseErrorKind::Custom(
9591                        "`rope … cos RC and sin RS`: expected `and`.".to_string(),
9592                    ),
9593                    span: self.current_span(),
9594                });
9595            }
9596            self.advance();
9597            if !self.check_word("sin") {
9598                return Err(ParseError {
9599                    kind: ParseErrorKind::Custom(
9600                        "`rope … and sin RS`: expected `sin`.".to_string(),
9601                    ),
9602                    span: self.current_span(),
9603                });
9604            }
9605            self.advance();
9606            let rs = self.peek().lexeme;
9607            let Some(&rs_shape) = self.tensor_shapes.get(&rs) else {
9608                return Err(ParseError {
9609                    kind: ParseErrorKind::Custom(
9610                        "`rope … and sin RS`: RS must be a declared tensor of RC's shape."
9611                            .to_string(),
9612                    ),
9613                    span: self.current_span(),
9614                });
9615            };
9616            let (q_name, rc_name, rs_name) = (
9617                self.interner.resolve(q).to_string(),
9618                self.interner.resolve(rc).to_string(),
9619                self.interner.resolve(rs).to_string(),
9620            );
9621            if rs_shape != (rcr, half) {
9622                return Err(ParseError {
9623                    kind: ParseErrorKind::Custom(format!("`rope … cos {rc_name} and sin {rs_name}` — {rs_name} must have {rc_name}'s shape ({rcr} by {half}), not {} by {}.", rs_shape.0, rs_shape.1)),
9624                    span: self.current_span(),
9625                });
9626            }
9627            self.advance();
9628            // Optional `from position P` — the tables are a CACHE of angles per position: row `r` of Q turns by row
9629            // `P + r − 1`, and the block's rows must all lie among the cache's. Without it the tables hold one row of
9630            // angles per row of Q, or one row for every row.
9631            let from = if self.check_word("from") && self.peek_word_at(1, "position") {
9632                self.advance();
9633                self.advance();
9634                let Some(p) = self.parse_row_index()? else {
9635                    return Err(ParseError { kind: ParseErrorKind::Custom(format!("`rope of {q_name} … from position P` — P must be a whole number, a name or a parenthesized expression.")), span: self.current_span() });
9636                };
9637                if rcr < qr {
9638                    return Err(ParseError {
9639                        kind: ParseErrorKind::Custom(format!("`rope of {q_name} … from position` — a cache of {rcr} rows cannot hold the {qr} rows of {q_name}; {rc_name} has {rcr} rows.")),
9640                        span: self.current_span(),
9641                    });
9642                }
9643                Some((p, rcr as i64))
9644            } else {
9645                if rcr != 1 && rcr != qr {
9646                    return Err(ParseError {
9647                        kind: ParseErrorKind::Custom(format!("`rope of {q_name}` — the tables hold one row of angles per row of {q_name} ({qr} rows) or one row for every row; {rc_name} has {rcr} rows.")),
9648                        span: self.current_span(),
9649                    });
9650                }
9651                None
9652            };
9653            // Optional `across H heads` — a PARTIAL rotary slice. Without it, head_dim is `2·half` (classic
9654            // full rotary, every dim rotated). With it, head_dim is `|Q|/H` and only the leading `2·half` dims
9655            // of each head rotate; the rest pass through. Qwen3.8-27B rotates 64 of each head's 256 dims, so
9656            // the cos/sin table width no longer determines head_dim and has to be stated.
9657            let hd = if self.check_word("across") {
9658                let saved_a = self.backtrack_point();
9659                self.advance();
9660                let Some(hcount) = self.take_int_literal() else {
9661                    self.backtrack_to(&saved_a);
9662                    return Err(ParseError {
9663                        kind: ParseErrorKind::Custom(
9664                            "`rope … across H heads` — H must be an integer literal.".to_string(),
9665                        ),
9666                        span: self.current_span(),
9667                    });
9668                };
9669                if !self.check_word("heads") {
9670                    return Err(ParseError {
9671                        kind: ParseErrorKind::Custom(
9672                            "`rope … across H heads`: expected `heads`.".to_string(),
9673                        ),
9674                        span: self.current_span(),
9675                    });
9676                }
9677                self.advance();
9678                if hcount <= 0 || qc % hcount != 0 {
9679                    return Err(ParseError {
9680                        kind: ParseErrorKind::Custom(format!(
9681                            "`rope … across {hcount} heads` — Q's {qc} columns must divide by H."
9682                        )),
9683                        span: self.current_span(),
9684                    });
9685                }
9686                let hd = qc / hcount;
9687                if 2 * half > hd {
9688                    return Err(ParseError { kind: ParseErrorKind::Custom(format!("`rope … across {hcount} heads` — the rotary slice is {} dims but head_dim is only {hd}.", 2 * half)), span: self.current_span() });
9689                }
9690                hd
9691            } else {
9692                if half <= 0 || qc % (2 * half) != 0 {
9693                    return Err(ParseError {
9694                        kind: ParseErrorKind::Custom(format!("`rope of {q_name}` — without `across H heads` a head is the tables' 2·{half} dims, and {q_name}'s {qc} columns are not whole heads.")),
9695                        span: self.current_span(),
9696                    });
9697                }
9698                2 * half
9699            };
9700            self.user_bound.insert(var);
9701            self.bind_tensor(var, (qr, qc), false);
9702            let per_row = (rcr > 1 || from.is_some()).then_some(qc / hd);
9703            return Ok(Some(self.desugar_tensor_rope(
9704                var,
9705                q,
9706                rc,
9707                rs,
9708                qr * qc,
9709                half,
9710                hd,
9711                per_row,
9712                from,
9713            )));
9714        }
9715        // `Let Y be relu of X` — `max(x, 0)`, elementwise, in X's element kind: an int8 model's FFN activation.
9716        if self.check_word("relu") && self.peek_word_at(1, "of") {
9717            self.advance();
9718            self.advance();
9719            let x = self.peek().lexeme;
9720            let Some(&(r, c)) = self.tensor_shapes.get(&x) else {
9721                return Err(ParseError {
9722                    kind: ParseErrorKind::Custom(
9723                        "`relu of X` needs a declared tensor X.".to_string(),
9724                    ),
9725                    span: self.current_span(),
9726                });
9727            };
9728            self.advance();
9729            let int = self.tensor_int.contains(&x);
9730            self.user_bound.insert(var);
9731            self.bind_tensor(var, (r, c), int);
9732            return Ok(Some(self.desugar_tensor_map(
9733                var,
9734                x,
9735                r * c,
9736                TensorElemMap::Relu,
9737                int,
9738            )));
9739        }
9740        // `Let Y be silu of X` — the SwiGLU gate activation `x·σ(x) = x / (1 + e^-x)`, elementwise.
9741        if self.check_word("silu") {
9742            let saved = self.backtrack_point();
9743            self.advance();
9744            if !self.check_preposition_is("of") {
9745                self.backtrack_to(&saved);
9746                return Ok(None);
9747            }
9748            self.advance();
9749            let x = self.peek().lexeme;
9750            let Some(&(r, c)) = self.tensor_shapes.get(&x) else {
9751                return Err(ParseError {
9752                    kind: ParseErrorKind::Custom(
9753                        "`silu of X` needs a declared tensor X.".to_string(),
9754                    ),
9755                    span: self.current_span(),
9756                });
9757            };
9758            self.advance();
9759            self.user_bound.insert(var);
9760            self.bind_tensor(var, (r, c), false);
9761            return Ok(Some(self.desugar_tensor_silu(var, x, r * c)));
9762        }
9763        // `Let Y be sigmoid of X` — `1/(1+e^-x)`, elementwise. The gated-attention output gate.
9764        if self.check_word("sigmoid") {
9765            let saved = self.backtrack_point();
9766            self.advance();
9767            if !self.check_preposition_is("of") {
9768                self.backtrack_to(&saved);
9769                return Ok(None);
9770            }
9771            self.advance();
9772            let x = self.peek().lexeme;
9773            let Some(&(r, c)) = self.tensor_shapes.get(&x) else {
9774                return Err(ParseError {
9775                    kind: ParseErrorKind::Custom(
9776                        "`sigmoid of X` needs a declared tensor X.".to_string(),
9777                    ),
9778                    span: self.current_span(),
9779                });
9780            };
9781            self.advance();
9782            self.user_bound.insert(var);
9783            self.bind_tensor(var, (r, c), false);
9784            return Ok(Some(self.desugar_tensor_sigmoid(var, x, r * c)));
9785        }
9786        // `Let Y be gelu of X` — GELU (ggml's tanh approximation), elementwise. The vision tower's MLP.
9787        if self.check_word("gelu") {
9788            let saved = self.backtrack_point();
9789            self.advance();
9790            if !self.check_preposition_is("of") {
9791                self.backtrack_to(&saved);
9792                return Ok(None);
9793            }
9794            self.advance();
9795            let x = self.peek().lexeme;
9796            let Some(&(r, c)) = self.tensor_shapes.get(&x) else {
9797                return Err(ParseError {
9798                    kind: ParseErrorKind::Custom(
9799                        "`gelu of X` needs a declared tensor X.".to_string(),
9800                    ),
9801                    span: self.current_span(),
9802                });
9803            };
9804            self.advance();
9805            self.user_bound.insert(var);
9806            self.bind_tensor(var, (r, c), false);
9807            return Ok(Some(self.desugar_tensor_gelu(var, x, r * c)));
9808        }
9809        // `Let A be decay of Ar with bias Dt rate Alog` — Gated DeltaNet's per-head forget gate.
9810        if self.check_word("decay") {
9811            let saved = self.backtrack_point();
9812            self.advance();
9813            if !self.check_preposition_is("of") {
9814                self.backtrack_to(&saved);
9815                return Ok(None);
9816            }
9817            self.advance();
9818            let want = |sf: &mut Self, w: &str| -> ParseResult<()> {
9819                if !sf.check_word(w) {
9820                    return Err(ParseError {
9821                        kind: ParseErrorKind::Custom(format!(
9822                            "`decay of Ar with bias Dt rate Alog` — expected `{w}`."
9823                        )),
9824                        span: sf.current_span(),
9825                    });
9826                }
9827                sf.advance();
9828                Ok(())
9829            };
9830            let operand = |sf: &mut Self, role: &str| -> ParseResult<(Symbol, i64)> {
9831                let sym = sf.peek().lexeme;
9832                let Some(&(r, c)) = sf.tensor_shapes.get(&sym) else {
9833                    return Err(ParseError {
9834                        kind: ParseErrorKind::Custom(format!(
9835                            "`decay` {role} must be a declared tensor."
9836                        )),
9837                        span: sf.current_span(),
9838                    });
9839                };
9840                sf.advance();
9841                Ok((sym, r * c))
9842            };
9843            let ar_shape = self.tensor_shapes.get(&self.peek().lexeme).copied();
9844            let (ar, n) = operand(self, "input Ar")?;
9845            want(self, "with")?;
9846            want(self, "bias")?;
9847            let (dt, dn) = operand(self, "bias Dt")?;
9848            want(self, "rate")?;
9849            let (alog, an) = operand(self, "rate Alog")?;
9850            // One bias and one rate per element of `Ar`, or — over a block of rows — one per head, a row being the heads.
9851            let heads = match ar_shape {
9852                Some((rows, cols)) if rows > 1 && dn == cols && an == cols => cols,
9853                _ => n,
9854            };
9855            if dn != heads || an != heads {
9856                return Err(ParseError {
9857                    kind: ParseErrorKind::Custom(format!("`decay` bias and rate must be one value per head ({heads}) — got {dn} and {an}.")),
9858                    span: self.current_span(),
9859                });
9860            }
9861            self.user_bound.insert(var);
9862            self.bind_tensor(var, ar_shape.filter(|_| heads < n).unwrap_or((1, n)), false);
9863            return Ok(Some(self.desugar_tensor_decay(var, ar, dt, alog, n, heads)));
9864        }
9865        // `Let Y be l2norm of X` — per-row `X/‖X‖`. Declare X as `heads by head_dim` to normalize each head.
9866        if self.check_word("l2norm") {
9867            let saved = self.backtrack_point();
9868            self.advance();
9869            if !self.check_preposition_is("of") {
9870                self.backtrack_to(&saved);
9871                return Ok(None);
9872            }
9873            self.advance();
9874            let x = self.peek().lexeme;
9875            let Some(&(r, c)) = self.tensor_shapes.get(&x) else {
9876                return Err(ParseError {
9877                    kind: ParseErrorKind::Custom(
9878                        "`l2norm of X` needs a declared tensor X.".to_string(),
9879                    ),
9880                    span: self.current_span(),
9881                });
9882            };
9883            self.advance();
9884            self.user_bound.insert(var);
9885            self.bind_tensor(var, (r, c), false);
9886            // `across H heads` — normalize each head independently. GDN L2-normalizes q and k per head, and
9887            // they arrive as a flat `1 by (H·D)` row, so the head count has to be stated rather than inferred.
9888            let (nr, nc) = if self.check_word("across") {
9889                self.advance();
9890                let Some(h) = self.take_int_literal() else {
9891                    return Err(ParseError {
9892                        kind: ParseErrorKind::Custom(
9893                            "`l2norm … across H heads` — H must be an integer literal.".to_string(),
9894                        ),
9895                        span: self.current_span(),
9896                    });
9897                };
9898                if !self.check_word("heads") {
9899                    return Err(ParseError {
9900                        kind: ParseErrorKind::Custom(
9901                            "`l2norm … across H heads`: expected `heads`.".to_string(),
9902                        ),
9903                        span: self.current_span(),
9904                    });
9905                }
9906                self.advance();
9907                if h <= 0 || (r * c) % h != 0 {
9908                    return Err(ParseError { kind: ParseErrorKind::Custom(format!("`l2norm … across {h} heads` — the tensor's {} entries must divide by H.", r * c)), span: self.current_span() });
9909                }
9910                (h, (r * c) / h)
9911            } else {
9912                (r, c)
9913            };
9914            let eps = self.take_epsilon("l2norm")?;
9915            self.bind_tensor(var, (r, c), false);
9916            return Ok(Some(self.desugar_tensor_l2norm(var, x, nr, nc, eps)));
9917        }
9918        // `Let Y be swiglu of Gate and Up` — gated MLP activation `silu(Gate) * Up`, elementwise (same shape).
9919        if self.check_word("swiglu") {
9920            let saved = self.backtrack_point();
9921            self.advance();
9922            if !self.check_preposition_is("of") {
9923                self.backtrack_to(&saved);
9924                return Ok(None);
9925            }
9926            self.advance();
9927            let gate = self.peek().lexeme;
9928            let Some(&(gr, gc)) = self.tensor_shapes.get(&gate) else {
9929                return Err(ParseError {
9930                    kind: ParseErrorKind::Custom(
9931                        "`swiglu of Gate and Up` needs a declared tensor Gate.".to_string(),
9932                    ),
9933                    span: self.current_span(),
9934                });
9935            };
9936            self.advance(); // Gate
9937            if !self.check_word("and") {
9938                return Err(ParseError {
9939                    kind: ParseErrorKind::Custom(
9940                        "expected `and Up` in `swiglu of Gate and Up`.".to_string(),
9941                    ),
9942                    span: self.current_span(),
9943                });
9944            }
9945            self.advance(); // "and"
9946            let up = self.peek().lexeme;
9947            let Some(&(ur, uc)) = self.tensor_shapes.get(&up) else {
9948                return Err(ParseError {
9949                    kind: ParseErrorKind::Custom(
9950                        "`swiglu of Gate and Up` needs a declared tensor Up.".to_string(),
9951                    ),
9952                    span: self.current_span(),
9953                });
9954            };
9955            if (ur, uc) != (gr, gc) {
9956                return Err(ParseError {
9957                    kind: ParseErrorKind::Custom(format!(
9958                        "swiglu Up must match Gate's shape {gr} by {gc} — got {ur} by {uc}."
9959                    )),
9960                    span: self.current_span(),
9961                });
9962            }
9963            self.advance(); // Up
9964            self.user_bound.insert(var);
9965            self.bind_tensor(var, (gr, gc), false);
9966            return Ok(Some(self.desugar_tensor_swiglu(var, gate, up, gr * gc)));
9967        }
9968        let kind = if self.check_word("softmax") {
9969            ReductionKind::Softmax
9970        } else if self.check_word("rmsnorm") {
9971            ReductionKind::Rmsnorm
9972        } else if self.check_word("layernorm") {
9973            ReductionKind::Layernorm
9974        } else {
9975            return Ok(None);
9976        };
9977        let saved = self.backtrack_point();
9978        self.advance(); // the op word
9979        if !self.check_preposition_is("of") {
9980            self.backtrack_to(&saved);
9981            return Ok(None);
9982        }
9983        self.advance(); // "of"
9984        let x = self.peek().lexeme;
9985        let Some(&(r, c)) = self.tensor_shapes.get(&x) else {
9986            return Err(ParseError {
9987                kind: ParseErrorKind::Custom(format!(
9988                    "`{}` needs a declared tensor — `Let Y be {} of X` where X is a tensor.",
9989                    kind.word(),
9990                    kind.word()
9991                )),
9992                span: self.current_span(),
9993            });
9994        };
9995        self.advance(); // consume X
9996                        // `layernorm of X with weight W and bias B` — per-row mean/variance normalize, then scale + shift.
9997        if matches!(kind, ReductionKind::Layernorm) {
9998            let want = |s: &mut Self, word: &str| -> ParseResult<()> {
9999                if !s.check_word(word) {
10000                    return Err(ParseError {
10001                        kind: ParseErrorKind::Custom(format!("layernorm needs `layernorm of X with weight W and bias B` — expected `{word}`.")),
10002                        span: s.current_span(),
10003                    });
10004                }
10005                s.advance();
10006                Ok(())
10007            };
10008            let vec_operand = |s: &mut Self, role: &str| -> ParseResult<Symbol> {
10009                let sym = s.peek().lexeme;
10010                let Some(&(vr, vc)) = s.tensor_shapes.get(&sym) else {
10011                    return Err(ParseError {
10012                        kind: ParseErrorKind::Custom(format!(
10013                            "layernorm {role} must be a declared length-{c} vector."
10014                        )),
10015                        span: s.current_span(),
10016                    });
10017                };
10018                if vr * vc != c {
10019                    return Err(ParseError {
10020                        kind: ParseErrorKind::Custom(format!(
10021                            "layernorm {role} length must equal the column count {c} — got {}.",
10022                            vr * vc
10023                        )),
10024                        span: s.current_span(),
10025                    });
10026                }
10027                s.advance();
10028                Ok(sym)
10029            };
10030            want(self, "with")?;
10031            want(self, "weight")?;
10032            let w = vec_operand(self, "weight")?;
10033            want(self, "and")?;
10034            want(self, "bias")?;
10035            let b = vec_operand(self, "bias")?;
10036            let eps = self.take_epsilon("layernorm")?;
10037            self.user_bound.insert(var);
10038            self.bind_tensor(var, (r, c), false);
10039            return Ok(Some(self.desugar_tensor_layernorm(var, x, w, b, r, c, eps)));
10040        }
10041        // Optional fused residual `plus R` and per-column `with weight W` — the (fused-add) weighted RMSNorm
10042        // shape (`Y = rmsnorm(X + R) * W`, FIB `001_fused_add_rmsnorm`). softmax takes neither.
10043        if matches!(kind, ReductionKind::Rmsnorm)
10044            && (self.check_word("plus")
10045                || (self.check_word("with") && !self.check_word_at(self.current + 1, "epsilon")))
10046        {
10047            // `plus R` — a fused residual add, if present. R must share X's shape.
10048            let residual = if self.check_word("plus") {
10049                self.advance(); // "plus"
10050                let rz = self.peek().lexeme;
10051                let Some(&(rr, rc)) = self.tensor_shapes.get(&rz) else {
10052                    return Err(ParseError {
10053                        kind: ParseErrorKind::Custom("`rmsnorm of X plus R` needs a declared residual tensor R with X's shape.".to_string()),
10054                        span: self.current_span(),
10055                    });
10056                };
10057                if (rr, rc) != (r, c) {
10058                    return Err(ParseError {
10059                        kind: ParseErrorKind::Custom(format!("fused-add residual R must match X's shape {r} by {c} — got {rr} by {rc}.")),
10060                        span: self.current_span(),
10061                    });
10062                }
10063                self.advance(); // consume R
10064                Some(rz)
10065            } else {
10066                None
10067            };
10068            // `with weight W` — required here (transformer RMSNorm always carries a weight; FIB 001 does).
10069            if !self.check_word("with") {
10070                return Err(ParseError {
10071                    kind: ParseErrorKind::Custom(
10072                        "rmsnorm here needs a weight: `rmsnorm of X [plus R] with weight W`."
10073                            .to_string(),
10074                    ),
10075                    span: self.current_span(),
10076                });
10077            }
10078            self.advance(); // "with"
10079            if !self.check_word("weight") {
10080                return Err(ParseError {
10081                    kind: ParseErrorKind::Custom(
10082                        "expected `weight` after `with` in `rmsnorm of X with weight W`."
10083                            .to_string(),
10084                    ),
10085                    span: self.current_span(),
10086                });
10087            }
10088            self.advance(); // "weight"
10089            let w = self.peek().lexeme;
10090            let Some(&(wr, wc)) = self.tensor_shapes.get(&w) else {
10091                return Err(ParseError {
10092                    kind: ParseErrorKind::Custom("`rmsnorm ... with weight W` needs a declared tensor W (a length-C weight vector).".to_string()),
10093                    span: self.current_span(),
10094                });
10095            };
10096            self.advance(); // consume W
10097                            // `across H heads` — PER-HEAD RMSNorm. Qwen3.8's QK-norm normalizes each of the 24 (or 4) heads
10098                            // independently with one shared `head_dim`-length weight, so the weight is C/H long, not C. The
10099                            // desugar already normalizes per ROW, so an `H by C/H` view is exactly per-head.
10100            // `in L lanes` — each of a hyper-connected residual's `L` lanes of every row normed on its own, each by its
10101            // own row of the weight (`L` rows of `C/L`): the per-row norm over an `L·R by C/L` reading.
10102            let mut weight_rows = 1;
10103            let (nr, nc) = if self.check_word("in") && self.peek_word_at(2, "lanes") {
10104                self.advance();
10105                let Some(l) = self.take_int_literal() else {
10106                    return Err(ParseError {
10107                        kind: ParseErrorKind::Custom("`rmsnorm … in L lanes` — L must be an integer literal.".to_string()),
10108                        span: self.current_span(),
10109                    });
10110                };
10111                self.advance(); // "lanes"
10112                if l <= 0 || c % l != 0 {
10113                    return Err(ParseError {
10114                        kind: ParseErrorKind::Custom(format!("`rmsnorm … in {l} lanes` — the tensor's {c} columns must divide by L.")),
10115                        span: self.current_span(),
10116                    });
10117                }
10118                if wr * wc != c {
10119                    return Err(ParseError {
10120                        kind: ParseErrorKind::Custom(format!("`rmsnorm … in {l} lanes` — the weight holds every lane's: {c} values, got {}.", wr * wc)),
10121                        span: self.current_span(),
10122                    });
10123                }
10124                weight_rows = l;
10125                (r * l, c / l)
10126            } else if self.check_word("across") {
10127                let saved_h = self.backtrack_point();
10128                self.advance();
10129                let Some(h) = self.take_int_literal() else {
10130                    self.backtrack_to(&saved_h);
10131                    return Err(ParseError {
10132                        kind: ParseErrorKind::Custom(
10133                            "`rmsnorm … across H heads` — H must be an integer literal."
10134                                .to_string(),
10135                        ),
10136                        span: self.current_span(),
10137                    });
10138                };
10139                if !self.check_word("heads") {
10140                    return Err(ParseError {
10141                        kind: ParseErrorKind::Custom(
10142                            "`rmsnorm … across H heads`: expected `heads`.".to_string(),
10143                        ),
10144                        span: self.current_span(),
10145                    });
10146                }
10147                self.advance();
10148                if h <= 0 || (r * c) % h != 0 {
10149                    return Err(ParseError { kind: ParseErrorKind::Custom(format!("`rmsnorm … across {h} heads` — the tensor's {} entries must divide by H.", r * c)), span: self.current_span() });
10150                }
10151                (h, (r * c) / h)
10152            } else {
10153                (r, c)
10154            };
10155            if weight_rows == 1 && wr * wc != nc {
10156                return Err(ParseError {
10157                    kind: ParseErrorKind::Custom(format!(
10158                        "rmsnorm weight length must be {nc} (the per-head width) — got {}.",
10159                        wr * wc
10160                    )),
10161                    span: self.current_span(),
10162                });
10163            }
10164            let eps = self.take_epsilon("rmsnorm")?;
10165            self.user_bound.insert(var);
10166            self.bind_tensor(var, (r, c), false);
10167            return Ok(Some(
10168                self.desugar_tensor_rmsnorm_weighted(var, x, residual, w, nr, nc, eps, weight_rows),
10169            ));
10170        }
10171        let total = r * c;
10172        let eps = if matches!(kind, ReductionKind::Rmsnorm) { self.take_epsilon("rmsnorm")? } else { 1e-6 };
10173        self.user_bound.insert(var);
10174        self.bind_tensor(var, (r, c), false);
10175        Ok(Some(match kind {
10176            ReductionKind::Rmsnorm => self.desugar_tensor_rmsnorm(var, x, total, eps),
10177            // A 2-D tensor gets the PER-ROW form; `rows == 1` keeps the flat path byte for byte.
10178            ReductionKind::Softmax if r > 1 => self.desugar_tensor_softmax_rows(var, x, r, c),
10179            ReductionKind::Softmax => self.desugar_tensor_softmax(var, x, total),
10180            ReductionKind::Layernorm => {
10181                unreachable!("layernorm is handled above (it requires weight + bias)")
10182            }
10183        }))
10184    }
10185
10186    /// `Let A be a R by C tensor of <Type>` in value position (article not yet consumed). Records the shape
10187    /// and returns the desugared declaration, or `None` (position restored) if the value isn't a tensor.
10188    fn try_parse_tensor_new(&mut self, var: Symbol) -> ParseResult<Option<Stmt<'a>>> {
10189        if !self.check_article() {
10190            return Ok(None);
10191        }
10192        let saved = self.backtrack_point();
10193        self.advance(); // article
10194        let Some(rows) = self.take_int_literal() else {
10195            self.backtrack_to(&saved);
10196            return Ok(None);
10197        };
10198        if !(self.check_word("by") || self.check_preposition_is("by")) {
10199            self.backtrack_to(&saved);
10200            return Ok(None);
10201        }
10202        self.advance(); // "by"
10203        let Some(cols) = self.take_int_literal() else {
10204            self.backtrack_to(&saved);
10205            return Ok(None);
10206        };
10207        if !self.check_word("tensor") {
10208            self.backtrack_to(&saved);
10209            return Ok(None);
10210        }
10211        self.advance(); // "tensor"
10212        if !self.check_preposition_is("of") {
10213            self.backtrack_to(&saved);
10214            return Ok(None);
10215        }
10216        self.advance(); // "of"
10217        let elem = self.interner.resolve(self.peek().lexeme).to_string();
10218        self.advance(); // element type name
10219        if rows <= 0 || cols <= 0 {
10220            return Err(ParseError {
10221                kind: ParseErrorKind::Custom(format!(
10222                    "A tensor's dimensions must be positive — got {rows} by {cols}."
10223                )),
10224                span: self.current_span(),
10225            });
10226        }
10227        let is_float = matches!(elem.as_str(), "Real" | "Float");
10228        self.user_bound.insert(var);
10229        self.bind_tensor(var, (rows, cols), !is_float);
10230        Ok(Some(self.desugar_tensor_new(var, rows, cols, is_float)))
10231    }
10232
10233    /// `Let P be the first K of every N of X` / `the last K of every N of X` — the `K` elements at the start (or the end)
10234    /// of every group of `N` consecutive elements of `X`, in order: one part of every head of a row of heads. `X` is
10235    /// `R × C` with `N` dividing `C`, and `P` is `R × (C/N)·K`. Consumes nothing unless `the first|last K of every` is
10236    /// there.
10237    fn try_parse_tensor_head_slice(&mut self, var: Symbol) -> ParseResult<Option<Stmt<'a>>> {
10238        let last = self.peek_word_at(1, "last");
10239        if !(self.check_article()
10240            && (self.peek_word_at(1, "first") || last)
10241            && matches!(self.tokens.get(self.current + 2).map(|t| &t.kind), Some(TokenType::Number(_)))
10242            && self.peek_word_at(3, "of")
10243            && self.peek_word_at(4, "every"))
10244        {
10245            return Ok(None);
10246        }
10247        let which = if last { "last" } else { "first" };
10248        self.advance();
10249        self.advance();
10250        let k = self.take_int_literal().unwrap_or(0);
10251        self.advance();
10252        self.advance();
10253        let Some(n) = self.take_int_literal() else {
10254            return Err(ParseError {
10255                kind: ParseErrorKind::Custom(format!("`the {which} K of every N of X` — N must be a whole number.")),
10256                span: self.current_span(),
10257            });
10258        };
10259        if !self.check_preposition_is("of") {
10260            return Err(ParseError {
10261                kind: ParseErrorKind::Custom(format!("`the {which} {k} of every {n} of X` — expected `of X`.")),
10262                span: self.current_span(),
10263            });
10264        }
10265        self.advance();
10266        let x = self.peek().lexeme;
10267        let Some(&(rows, cols)) = self.tensor_shapes.get(&x) else {
10268            return Err(ParseError {
10269                kind: ParseErrorKind::Custom(format!("`the {which} {k} of every {n} of X` — X must be a declared tensor.")),
10270                span: self.current_span(),
10271            });
10272        };
10273        self.advance();
10274        if k < 1 || n < 1 || k > n {
10275            return Err(ParseError {
10276                kind: ParseErrorKind::Custom(format!(
10277                    "`the {which} {k} of every {n} of {}` — a part of {k} must fit in a group of {n}.",
10278                    self.interner.resolve(x)
10279                )),
10280                span: self.current_span(),
10281            });
10282        }
10283        if cols % n != 0 {
10284            return Err(ParseError {
10285                kind: ParseErrorKind::Custom(format!(
10286                    "`the {which} {k} of every {n} of {}` — its {cols} columns are not whole groups of every {n}.",
10287                    self.interner.resolve(x)
10288                )),
10289                span: self.current_span(),
10290            });
10291        }
10292        let int = self.tensor_int.contains(&x);
10293        let from = if last { n - k } else { 0 };
10294        self.user_bound.insert(var);
10295        self.bind_tensor(var, (rows, cols / n * k), int);
10296        Ok(Some(self.desugar_tensor_head_slice(var, x, rows * cols / n, n, from, k, int)))
10297    }
10298
10299    /// An integer literal, negative when a `-` precedes it.
10300    fn take_signed_int_literal(&mut self) -> Option<i64> {
10301        if let Some(n) = self.take_int_literal() {
10302            return Some(n);
10303        }
10304        if self.check(&TokenType::Minus) {
10305            let saved = self.backtrack_point();
10306            self.advance();
10307            if let Some(n) = self.take_int_literal() {
10308                return Some(-n);
10309            }
10310            self.backtrack_to(&saved);
10311        }
10312        None
10313    }
10314
10315    /// `Let Y be X times M shifted right by S` — an int8 model's requant `(x · M) >> S`, an arithmetic shift, so a
10316    /// negative accumulator rounds toward −∞ — and `Let Y be X clamped between LO and HI`, its saturation, over a
10317    /// declared `tensor of Int`, elementwise into a fresh `tensor of Int` of `X`'s shape. `None`, with nothing
10318    /// consumed, when the value is not one of them.
10319    fn try_parse_tensor_int_map(&mut self, var: Symbol) -> ParseResult<Option<Stmt<'a>>> {
10320        let x = self.peek().lexeme;
10321        let Some(&(r, c)) = self.tensor_shapes.get(&x) else {
10322            return Ok(None);
10323        };
10324        let clamp = if self.peek_word_at(1, "times") {
10325            false
10326        } else if self.peek_word_at(1, "clamped") {
10327            true
10328        } else {
10329            return Ok(None);
10330        };
10331        let name = self.interner.resolve(x).to_string();
10332        let form = if clamp {
10333            "clamped between LO and HI"
10334        } else {
10335            "times M shifted right by S"
10336        };
10337        let refuse = |s: &Self, why: String| ParseError {
10338            kind: ParseErrorKind::Custom(why),
10339            span: s.current_span(),
10340        };
10341        if !self.tensor_int.contains(&x) {
10342            return Err(refuse(
10343                self,
10344                format!("`{name} {form}` is an integer form: `{name}` must be a tensor of Int."),
10345            ));
10346        }
10347        self.advance(); // X
10348        self.advance(); // `times` / `clamped`
10349        let want = |s: &mut Self, w: &str| -> ParseResult<()> {
10350            if !s.check_word(w) {
10351                return Err(refuse(s, format!("`{name} {form}` — expected `{w}`.")));
10352            }
10353            s.advance();
10354            Ok(())
10355        };
10356        let map = if clamp {
10357            want(self, "between")?;
10358            let lo = self.take_signed_int_literal().ok_or_else(|| {
10359                refuse(self, format!("`{name} {form}` — LO must be an integer literal."))
10360            })?;
10361            want(self, "and")?;
10362            let hi = self.take_signed_int_literal().ok_or_else(|| {
10363                refuse(self, format!("`{name} {form}` — HI must be an integer literal."))
10364            })?;
10365            if lo > hi {
10366                return Err(refuse(
10367                    self,
10368                    format!(
10369                        "`{name} clamped between {lo} and {hi}` — nothing lies between {lo} and {hi}; the lower bound comes first."
10370                    ),
10371                ));
10372            }
10373            TensorElemMap::Clamp { lo, hi }
10374        } else {
10375            let m = self.take_signed_int_literal().ok_or_else(|| {
10376                refuse(self, format!("`{name} {form}` — M must be an integer literal."))
10377            })?;
10378            want(self, "shifted")?;
10379            want(self, "right")?;
10380            want(self, "by")?;
10381            let s = self
10382                .take_int_literal()
10383                .filter(|s| (0..=63).contains(s))
10384                .ok_or_else(|| {
10385                    refuse(self, format!("`{name} {form}` — S must be a whole number from 0 to 63."))
10386                })?;
10387            TensorElemMap::Requant { m, s }
10388        };
10389        self.user_bound.insert(var);
10390        self.bind_tensor(var, (r, c), true);
10391        Ok(Some(self.desugar_tensor_map(var, x, r * c, map, true)))
10392    }
10393
10394    /// `Let C be A matmul B` in value position. Both operands must be tensors declared earlier; their shapes
10395    /// are checked (inner dims must agree) and `C`'s shape recorded. `None` (restored) if not a `matmul`.
10396    fn try_parse_tensor_binop(&mut self, var: Symbol) -> ParseResult<Option<Stmt<'a>>> {
10397        // Cheap guard: only a value that STARTS with a known tensor name can be a tensor binop.
10398        if !self.tensor_shapes.contains_key(&self.peek().lexeme) {
10399            return Ok(None);
10400        }
10401        let saved = self.backtrack_point();
10402        let Ok(lhs) = self.parse_primary_expr() else {
10403            self.backtrack_to(&saved);
10404            return Ok(None);
10405        };
10406        // `Let Y be X scaled by C` — every element of `X` times the number `C`, a tensor of Reals of `X`'s shape.
10407        if self.check_word("scaled") {
10408            let Some((x, (rows, cols))) = (match lhs {
10409                Expr::Identifier(x) => self.tensor_shapes.get(x).map(|&shape| (*x, shape)),
10410                _ => None,
10411            }) else {
10412                self.backtrack_to(&saved);
10413                return Ok(None);
10414            };
10415            self.advance();
10416            if !self.check_word("by") {
10417                return Err(ParseError {
10418                    kind: ParseErrorKind::Custom("`X scaled by C` — expected `by` after `scaled`.".to_string()),
10419                    span: self.current_span(),
10420                });
10421            }
10422            self.advance();
10423            let negative = self.check(&TokenType::Minus);
10424            if negative {
10425                self.advance();
10426            }
10427            let factor = match self.peek().kind {
10428                TokenType::Number(sym) => {
10429                    self.interner.resolve(sym).replace('_', "").parse::<f64>().ok().map(|c| if negative { -c } else { c })
10430                }
10431                _ => None,
10432            };
10433            let Some(factor) = factor.filter(|c| c.is_finite()) else {
10434                return Err(ParseError {
10435                    kind: ParseErrorKind::Custom("`X scaled by C` — C must be a number written out.".to_string()),
10436                    span: self.current_span(),
10437                });
10438            };
10439            self.advance();
10440            self.user_bound.insert(var);
10441            self.bind_tensor(var, (rows, cols), false);
10442            return Ok(Some(self.desugar_tensor_scale(var, x, rows * cols, factor)));
10443        }
10444        // `Let Y be X capped at C` — every element `x` of `X` as `C·tanh(x/C)`, a tensor of Reals of `X`'s shape.
10445        if self.check_word("capped") {
10446            let Some((x, (rows, cols))) = (match lhs {
10447                Expr::Identifier(x) => self.tensor_shapes.get(x).map(|&shape| (*x, shape)),
10448                _ => None,
10449            }) else {
10450                self.backtrack_to(&saved);
10451                return Ok(None);
10452            };
10453            let cap = self.take_cap("X")?.expect("`capped` was seen");
10454            self.user_bound.insert(var);
10455            self.bind_tensor(var, (rows, cols), false);
10456            return Ok(Some(self.desugar_tensor_cap(var, x, rows * cols, cap)));
10457        }
10458        // `Let J be A joined with B [across H heads]` — every row of `A` then the same row of `B`; across `H` heads, every
10459        // head's part of `A` then its part of `B`.
10460        if self.check_word("joined") {
10461            let Some((a, (ra, ca))) = (match lhs {
10462                Expr::Identifier(a) => self.tensor_shapes.get(a).map(|&shape| (*a, shape)),
10463                _ => None,
10464            }) else {
10465                self.backtrack_to(&saved);
10466                return Ok(None);
10467            };
10468            self.advance();
10469            if !self.check_word("with") {
10470                return Err(ParseError {
10471                    kind: ParseErrorKind::Custom("`A joined with B` — expected `with` after `joined`.".to_string()),
10472                    span: self.current_span(),
10473                });
10474            }
10475            self.advance();
10476            let b = self.peek().lexeme;
10477            let Some(&(rb, cb)) = self.tensor_shapes.get(&b) else {
10478                return Err(ParseError {
10479                    kind: ParseErrorKind::Custom("`A joined with B` — B must be a declared tensor.".to_string()),
10480                    span: self.current_span(),
10481                });
10482            };
10483            self.advance();
10484            let heads = self.take_across_heads("A joined with B")?.unwrap_or(1);
10485            if ra != rb {
10486                return Err(ParseError {
10487                    kind: ParseErrorKind::Custom(format!(
10488                        "`A joined with B` — A and B must have the same rows, and `{}` is {ra}×{ca} and `{}` {rb}×{cb}.",
10489                        self.interner.resolve(a),
10490                        self.interner.resolve(b)
10491                    )),
10492                    span: self.current_span(),
10493                });
10494            }
10495            if ca % heads != 0 || cb % heads != 0 {
10496                return Err(ParseError {
10497                    kind: ParseErrorKind::Custom(format!(
10498                        "`A joined with B across {heads} heads` — A's {ca} columns and B's {cb} must each divide among the {heads} heads."
10499                    )),
10500                    span: self.current_span(),
10501                });
10502            }
10503            let int = self.tensor_int.contains(&a) && self.tensor_int.contains(&b);
10504            self.user_bound.insert(var);
10505            self.bind_tensor(var, (ra, ca + cb), int);
10506            return Ok(Some(self.desugar_tensor_join(var, a, b, ra, heads, ca / heads, cb / heads, int)));
10507        }
10508        let op = if self.check_word("matmul") {
10509            TensorBinop::Matmul
10510        } else if self.check_word("plus") {
10511            TensorBinop::Add
10512        } else if self.check_word("hadamard") {
10513            TensorBinop::Hadamard
10514        } else {
10515            self.backtrack_to(&saved);
10516            return Ok(None);
10517        };
10518        self.advance(); // the operator word
10519        let rhs = self.parse_primary_expr()?;
10520        // `X matmul W across H heads` — each head's slice of `X`'s rows times its own block of `W`'s rows.
10521        let heads = match op {
10522            TensorBinop::Matmul => self.take_across_heads("X matmul W")?,
10523            _ => None,
10524        };
10525        let (Expr::Identifier(a), Expr::Identifier(b)) = (lhs, rhs) else {
10526            return Err(ParseError {
10527                kind: ParseErrorKind::Custom(format!(
10528                    "`{op}` takes two tensor variables — `Let C be A {op} B`.",
10529                    op = op.word()
10530                )),
10531                span: self.current_span(),
10532            });
10533        };
10534        let (a, b) = (*a, *b);
10535        // The result holds Int exactly when both operands do; one Real operand makes every element Real.
10536        let int = self.tensor_int.contains(&a) && self.tensor_int.contains(&b);
10537        let (m, ka) = *self.tensor_shapes.get(&a).ok_or_else(|| ParseError {
10538            kind: ParseErrorKind::Custom(format!(
10539                "`{}`'s left operand `{}` is not a declared tensor.",
10540                op.word(),
10541                self.interner.resolve(a)
10542            )),
10543            span: self.current_span(),
10544        })?;
10545        let (kb, n) = *self.tensor_shapes.get(&b).ok_or_else(|| ParseError {
10546            kind: ParseErrorKind::Custom(format!(
10547                "`{}`'s right operand `{}` is not a declared tensor.",
10548                op.word(),
10549                self.interner.resolve(b)
10550            )),
10551            span: self.current_span(),
10552        })?;
10553        match op {
10554            TensorBinop::Matmul if heads.is_some_and(|h| h > 1) => {
10555                let h = heads.unwrap_or(1);
10556                if ka % h != 0 || kb != ka {
10557                    return Err(ParseError {
10558                        kind: ParseErrorKind::Custom(format!(
10559                            "`X matmul W across {h} heads` — X's {ka} columns must divide among the {h} heads, and W must \
10560                             have a row for each of them; W is {kb}×{n}."
10561                        )),
10562                        span: self.current_span(),
10563                    });
10564                }
10565                self.user_bound.insert(var);
10566                self.bind_tensor(var, (m, h * n), int);
10567                Ok(Some(self.desugar_tensor_head_matmul(var, a, b, m, h, ka / h, n, int)))
10568            }
10569            TensorBinop::Matmul => {
10570                if ka != kb {
10571                    return Err(ParseError {
10572                        kind: ParseErrorKind::Custom(format!(
10573                            "Shape mismatch in `matmul`: the left tensor is {m}×{ka} and the right is {kb}×{n} — \
10574                             the inner dimensions ({ka} and {kb}) must agree."
10575                        )),
10576                        span: self.current_span(),
10577                    });
10578                }
10579                self.user_bound.insert(var);
10580                self.bind_tensor(var, (m, n), int);
10581                Ok(Some(self.desugar_tensor_matmul(var, a, b, m, ka, n, int)))
10582            }
10583            TensorBinop::Add | TensorBinop::Hadamard => {
10584                // `B` of A's shape, or one row as wide as A's rows — combined with every row of `A`.
10585                let row = (kb == 1 && n == ka && m > 1).then_some(ka);
10586                if (m, ka) != (kb, n) && row.is_none() {
10587                    return Err(ParseError {
10588                        kind: ParseErrorKind::Custom(format!(
10589                            "Shape mismatch in `{}`: the tensors are {m}×{ka} and {kb}×{n} — an elementwise op \
10590                             needs identical shapes, or a right operand of one row as wide as the left's rows.",
10591                            op.word()
10592                        )),
10593                        span: self.current_span(),
10594                    });
10595                }
10596                let kind = if matches!(op, TensorBinop::Add) {
10597                    BinaryOpKind::Add
10598                } else {
10599                    BinaryOpKind::Multiply
10600                };
10601                self.user_bound.insert(var);
10602                self.bind_tensor(var, (m, ka), int);
10603                Ok(Some(self.desugar_tensor_elementwise(
10604                    var,
10605                    a,
10606                    b,
10607                    m * ka,
10608                    kind,
10609                    int,
10610                    row,
10611                )))
10612            }
10613        }
10614    }
10615
10616    /// Parses the token stream into a logical expression.
10617    ///
10618    /// This is the main entry point for declarative/FOL parsing. It handles
10619    /// multi-sentence inputs by conjoining them with logical AND, and processes
10620    /// various sentence types including declaratives, questions, and imperatives.
10621    ///
10622    /// # Returns
10623    ///
10624    /// An arena-allocated [`LogicExpr`] representing the parsed input, or
10625    /// a [`ParseError`] with source location and Socratic explanation.
10626    ///
10627    /// # Discourse State
10628    ///
10629    /// The parser maintains discourse state across sentences, enabling
10630    /// anaphora resolution ("he", "she", "they" refer to prior entities)
10631    /// and temporal coherence (tense interpretation relative to reference time).
10632    pub fn parse(&mut self) -> ParseResult<&'a LogicExpr<'a>> {
10633        let mut result = self.parse_prefix()?;
10634
10635        // Postposed "while": "Y while X." mirrors the fronted "While X, Y."
10636        // — the condition restricts the main clause. A bare-predicate
10637        // condition ("… while idle") predicates over the main clause's
10638        // agent ("the transmitter shall not send data while [it is] idle").
10639        while self.check(&TokenType::While) {
10640            self.advance();
10641            let condition = if matches!(self.peek().kind, TokenType::Adjective(_))
10642                && matches!(
10643                    self.tokens.get(self.current + 1).map(|t| &t.kind),
10644                    Some(TokenType::Period) | Some(TokenType::EOF) | None
10645                ) {
10646                let adj = match self.advance().kind {
10647                    TokenType::Adjective(a) => a,
10648                    _ => unreachable!("guarded by the match above"),
10649                };
10650                let subject = Self::find_agent_term(result)
10651                    .unwrap_or(Term::Constant(self.interner.intern("System")));
10652                &*self.ctx.exprs.alloc(LogicExpr::Predicate {
10653                    name: adj,
10654                    args: self.ctx.terms.alloc_slice([subject]),
10655                    world: None,
10656                })
10657            } else {
10658                self.parse_sentence()?
10659            };
10660            if self.check(&TokenType::Period) {
10661                self.advance();
10662            }
10663            result = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
10664                left: condition,
10665                op: TokenType::Implies,
10666                right: result,
10667            });
10668        }
10669
10670        // Postposed "at any time": a universal temporal adverbial — the
10671        // clause is an invariant ("At most one grant is asserted at any
10672        // time." → G(…)).
10673        let at_any_time_from = if matches!(&self.peek().kind, TokenType::Preposition(p) if self.interner.resolve(*p).eq_ignore_ascii_case("at"))
10674            && matches!(
10675                self.tokens.get(self.current + 1).map(|t| &t.kind),
10676                Some(TokenType::Any)
10677            )
10678            && self
10679                .tokens
10680                .get(self.current + 2)
10681                .map(|t| self.interner.resolve(t.lexeme).eq_ignore_ascii_case("time"))
10682                .unwrap_or(false)
10683        {
10684            // "… at any time."
10685            Some(3)
10686        } else if matches!(self.peek().kind, TokenType::Any)
10687            && self
10688                .tokens
10689                .get(self.current + 1)
10690                .map(|t| self.interner.resolve(t.lexeme).eq_ignore_ascii_case("time"))
10691                .unwrap_or(false)
10692        {
10693            // The clause parse consumed the bare "at" as a dangling PP head;
10694            // "any time" remains.
10695            Some(2)
10696        } else {
10697            None
10698        };
10699        if let Some(n) = at_any_time_from {
10700            for _ in 0..n {
10701                self.advance();
10702            }
10703            if self.check(&TokenType::Period) {
10704                self.advance();
10705            }
10706            result = self.ctx.exprs.alloc(LogicExpr::Temporal {
10707                operator: TemporalOperator::Always,
10708                body: result,
10709            });
10710        }
10711
10712        // A parse that stops mid-sentence is not a parse: accepting it would
10713        // silently drop the remainder's meaning.
10714        if !self.is_at_end() {
10715            return Err(ParseError {
10716                kind: crate::error::ParseErrorKind::TrailingTokens {
10717                    found: self.peek().kind.clone(),
10718                },
10719                span: self.current_span(),
10720            });
10721        }
10722
10723        Ok(result)
10724    }
10725
10726    /// The Agent term of the first event in an expression (used to supply
10727    /// the understood subject of a bare postposed condition).
10728    fn find_agent_term(expr: &'a LogicExpr<'a>) -> Option<Term<'a>> {
10729        match expr {
10730            LogicExpr::NeoEvent(data) => data
10731                .roles
10732                .iter()
10733                .find(|(role, _)| matches!(role, crate::ast::ThematicRole::Agent))
10734                .map(|(_, term)| term.clone()),
10735            LogicExpr::Modal { operand, .. } => Self::find_agent_term(operand),
10736            LogicExpr::UnaryOp { operand, .. } => Self::find_agent_term(operand),
10737            LogicExpr::Temporal { body, .. } => Self::find_agent_term(body),
10738            LogicExpr::Aspectual { body, .. } => Self::find_agent_term(body),
10739            LogicExpr::Quantifier { body, .. } => Self::find_agent_term(body),
10740            LogicExpr::BinaryOp { left, right, .. } => {
10741                Self::find_agent_term(left).or_else(|| Self::find_agent_term(right))
10742            }
10743            _ => None,
10744        }
10745    }
10746
10747    /// Parses a maximal declarative prefix WITHOUT requiring end-of-input.
10748    ///
10749    /// For embedded sub-parses whose end is marked by a host-grammar keyword
10750    /// the caller checks itself ("Trust ⟨proposition⟩ because …"). Whole-input
10751    /// parsing must use [`Self::parse`], which rejects trailing tokens.
10752    pub(super) fn parse_prefix(&mut self) -> ParseResult<&'a LogicExpr<'a>> {
10753        let mut result = self.parse_sentence()?;
10754
10755        // Loop: handle ANY number of additional sentences (unlimited)
10756        // Handle all sentence terminators: . ? !
10757        while self.check(&TokenType::Period) || self.check(&TokenType::Exclamation) {
10758            self.advance(); // consume terminator
10759            if !self.is_at_end() {
10760                let next = self.parse_sentence()?;
10761                result = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
10762                    left: result,
10763                    op: TokenType::And,
10764                    right: next,
10765                });
10766            }
10767        }
10768
10769        Ok(result)
10770    }
10771
10772    /// Parses with pragmatic enrichment: the truth-conditional parse PLUS scalar
10773    /// implicature (§8.7). The literal `parse()` is unchanged; here a leading weak
10774    /// scalar "some" is strengthened to `∃… +> ¬∀…`. Used by the pragmatic compile
10775    /// path so the implicature appears as a separate labeled component.
10776    pub fn parse_pragmatic(&mut self) -> ParseResult<&'a LogicExpr<'a>> {
10777        self.pragmatic = true;
10778        // A leading weak scalar quantifier (some / most / many) is the Horn-scale trigger;
10779        // its stronger "all" alternative is implicated. "all" is not a trigger.
10780        let scalar_trigger = matches!(
10781            self.peek().kind,
10782            TokenType::Some | TokenType::Most | TokenType::Many
10783        );
10784        let result = self.parse()?;
10785        if scalar_trigger {
10786            if let Some(strengthened) = self.scalar_implicature(result) {
10787                return Ok(strengthened);
10788            }
10789        }
10790        Ok(result)
10791    }
10792
10793    /// Parses a LOGOS program into a list of statements.
10794    ///
10795    /// This is the main entry point for imperative/executable LOGOS code.
10796    /// It handles block structures (Definition, Policy, Procedure, Theorem),
10797    /// function definitions, type definitions, and executable statements.
10798    ///
10799    /// # Returns
10800    ///
10801    /// A vector of arena-allocated [`Stmt`] representing the program, or
10802    /// a [`ParseError`] with source location and Socratic explanation.
10803    ///
10804    /// # Block Types
10805    ///
10806    /// - **Definition**: Type definitions (structs, enums, generics)
10807    /// - **Policy**: Security predicates and capability rules
10808    /// - **Procedure**: Executable code blocks
10809    /// - **Theorem**: Logical propositions with proof strategies
10810    /// The view IR trees this parse built, in source order (see `views` field).
10811    pub fn views(&self) -> &[crate::ast::view::ViewDef<'a>] {
10812        &self.views
10813    }
10814
10815    /// Move the view IR trees out of the parser (they borrow only the AST arenas, never
10816    /// the parser), so a caller can release the parser's interner borrow and keep them.
10817    pub fn take_views(&mut self) -> Vec<crate::ast::view::ViewDef<'a>> {
10818        std::mem::take(&mut self.views)
10819    }
10820
10821    /// The type registry as this parse left it: every type the discovery pass read off the tokens,
10822    /// plus every record the parse itself declared — a `## Store`'s record (`lgx_lower::stores`),
10823    /// typed by the rule that builds it. An engine lays a record out from this registry, the one
10824    /// that holds what the parse declared, and from no copy taken before the parse ran.
10825    pub fn into_type_registry(self) -> TypeRegistry {
10826        self.type_registry.unwrap_or_default()
10827    }
10828
10829    /// The template WARNINGS this parse collected, in source order — accessibility lint and
10830    /// anything else worth saying that must not stop a page from running. Errors never appear
10831    /// here: they stop the parse at the point they are found, which is what makes them errors.
10832    pub fn lgx_findings(&self) -> &[crate::lgx::check::Finding] {
10833        &self.lgx_findings
10834    }
10835
10836    /// The value-semantics copy lint's own WARNINGS this parse collected, in source order — see
10837    /// [`crate::lint`].
10838    pub fn value_copy_findings(&self) -> &[crate::lint::Finding] {
10839        &self.value_copy_findings
10840    }
10841
10842    /// `Given that METHOD "/url" answers STATUS with BODY.`
10843    ///
10844    /// `None` when the words after `Given that` are not a stub, so an ordinary sentence starting
10845    /// "Given that …" keeps whatever meaning it had.
10846    fn parse_given_fetch(&mut self) -> ParseResult<Option<Stmt<'a>>> {
10847        const METHODS: &[&str] = &["GET", "POST", "PUT", "PATCH", "DELETE"];
10848        let Some(method_word) = self
10849            .tokens
10850            .get(self.current + 2)
10851            .map(|t| self.interner.resolve(t.lexeme).to_ascii_uppercase())
10852        else {
10853            return Ok(None);
10854        };
10855        if !METHODS.contains(&method_word.as_str()) {
10856            return Ok(None);
10857        }
10858        self.advance(); // "Given"
10859        self.advance(); // "that"
10860        let method = self.interner.intern(&method_word);
10861        self.advance(); // the method
10862        let url = self.parse_imperative_expr()?;
10863        if !self.check_word("answers") {
10864            return Err(ParseError {
10865                kind: ParseErrorKind::Custom(
10866                    "`Given that METHOD \"/url\" answers STATUS with BODY.` — say what the \
10867                     request answers with, so the page has something to read."
10868                        .to_string(),
10869                ),
10870                span: self.current_span(),
10871            });
10872        }
10873        self.advance(); // "answers"
10874        let status = self.parse_imperative_expr()?;
10875        if !self.check_word("with") {
10876            return Err(ParseError {
10877                kind: ParseErrorKind::Custom(
10878                    "`Given that METHOD \"/url\" answers STATUS with BODY.` — the status needs a \
10879                     body after `with` (write `with \"\"` for an empty one)."
10880                        .to_string(),
10881                ),
10882                span: self.current_span(),
10883            });
10884        }
10885        self.advance(); // "with"
10886        let body = self.parse_imperative_expr()?;
10887        Ok(Some(Stmt::GivenFetch {
10888            method,
10889            url,
10890            status,
10891            body,
10892        }))
10893    }
10894
10895    /// One action name in `the journal shows A then B` or `Replay these actions: …`.
10896    ///
10897    /// Written BARE, the way the plan's examples read (`the journal shows loaded then failed`) —
10898    /// an action is named, not quoted, everywhere else in LGX (`on-click={loaded}`), so quoting
10899    /// it here would be the odd one out. A text literal is accepted too, for a name that is not
10900    /// a legal identifier.
10901    ///
10902    /// ★ ONE NAME RULE. A bare name is read by the very rule an `Actions:` header's `To <name>:`
10903    /// declares it with ([`Self::expect_identifier`]), so every word that can declare an action — a
10904    /// keyword (`add`, `push`, `show`), an adjective (`double`), a particle (`up`), `no` — names it
10905    /// here too, as the same symbol.
10906    fn parse_journal_action_name(&mut self) -> ParseResult<Symbol> {
10907        let span = self.current_span();
10908        if let TokenType::StringLiteral(sym) = self.peek().kind {
10909            self.advance();
10910            self.journal_action_sites.push((sym, span));
10911            return Ok(sym);
10912        }
10913        match self.expect_identifier() {
10914            Ok(sym) => {
10915                self.journal_action_sites.push((sym, span));
10916                Ok(sym)
10917            }
10918            Err(_) => Err(ParseError {
10919                kind: ParseErrorKind::Custom(
10920                    "An action is named here the way its `To <name>:` declares it — \
10921                     `Replay these actions: add, remove.`, `the journal shows loaded then \
10922                     failed`."
10923                        .to_string(),
10924                ),
10925                span,
10926            }),
10927        }
10928    }
10929
10930    /// A `##` header's own line may end in an optional, meaningless `:` — the one habit the
10931    /// retired reactive dialect left behind (`## Component Name:`). ONE rule for every
10932    /// `BlockType`: `self.current` names the `BlockHeader` token; if the token immediately
10933    /// before the `Newline` ending its line is a bare `Colon`, it is excised from the token
10934    /// stream before any header-specific parsing runs, so `## Component Counter:` is simply
10935    /// `## Component Counter`. Never touches a colon that is not the LAST token of the line —
10936    /// a header whose own grammar legitimately uses one mid-line (`## Page /post/:id`) keeps it.
10937    fn skip_optional_header_colon(&mut self) {
10938        let mut i = self.current;
10939        while let Some(tok) = self.tokens.get(i) {
10940            if matches!(tok.kind, TokenType::Newline) {
10941                break;
10942            }
10943            i += 1;
10944        }
10945        if i > self.current
10946            && matches!(
10947                self.tokens.get(i.wrapping_sub(1)).map(|t| &t.kind),
10948                Some(TokenType::Colon)
10949            )
10950        {
10951            self.tokens.remove(i - 1);
10952        }
10953    }
10954
10955    pub fn parse_program(&mut self) -> ParseResult<Vec<Stmt<'a>>> {
10956        let mut statements = Vec::new();
10957        self.stmt_spans.clear();
10958        self.nested_stmt_spans = NestedStmtSpans::default();
10959        self.moves.clear();
10960        self.declare_component_names();
10961        self.declare_function_names();
10962        let mut in_definition_block = false;
10963        let mut pending_opt_flags = OptimizationConfig::all_on();
10964
10965        // Check if we started in a Definition block (from process_block_headers)
10966        if self.mode == ParserMode::Declarative {
10967            // Check if the previous token was a Definition header
10968            // For now, assume Definition blocks should be skipped
10969            // We'll detect them by checking the content pattern
10970        }
10971
10972        while !self.is_at_end() {
10973            // A new block is a new scope: a move made in the one before does not reach it.
10974            if matches!(self.peek().kind, TokenType::BlockHeader { .. }) {
10975                self.release_moves_since(0);
10976            }
10977            // Handle block headers
10978            if let Some(Token {
10979                kind: TokenType::BlockHeader { block_type },
10980                ..
10981            }) = self.tokens.get(self.current)
10982            {
10983                let block_type = *block_type;
10984                self.skip_optional_header_colon();
10985                match &block_type {
10986                    BlockType::SuspectedTypo { found, suggestion } => {
10987                        let found = self.interner.resolve(*found).to_string();
10988                        let suggestion_str = self.interner.resolve(*suggestion);
10989                        let mut canonical: String = suggestion_str.to_string();
10990                        if let Some(c) = canonical.get_mut(0..1) {
10991                            c.make_ascii_uppercase();
10992                        }
10993                        return Err(ParseError {
10994                            kind: ParseErrorKind::ExpectedKeyword {
10995                                keyword: format!(
10996                                    "a known header — `## {}` looks like a typo of `## {}` (if it is a prose heading, pick a name further from the code headers)",
10997                                    found, canonical
10998                                ),
10999                            },
11000                            span: self.current_span(),
11001                        });
11002                    }
11003                    BlockType::Definition => {
11004                        in_definition_block = true;
11005                        self.mode = ParserMode::Declarative;
11006                        self.advance();
11007                        continue;
11008                    }
11009                    BlockType::Main => {
11010                        // A `## No <X>` before `## Main` (not attached to a function)
11011                        // is a FILE-LEVEL decorator: fold it into the program config.
11012                        self.program_opt_flags = self.program_opt_flags.merged(&pending_opt_flags);
11013                        pending_opt_flags = OptimizationConfig::all_on();
11014                        in_definition_block = false;
11015                        self.mode = ParserMode::Imperative;
11016                        self.advance();
11017                        continue;
11018                    }
11019                    BlockType::No => {
11020                        // Optimization annotation: `## No <Keyword>` clears that
11021                        // optimization for the following function; `## No Optimize`
11022                        // clears all of them. A `## No` immediately before `## Main`
11023                        // (not attached to a function) is program-wide — folded into
11024                        // `program_opt_flags` at the Main block. Keywords come from the
11025                        // registry.
11026                        self.advance(); // consume the `## No` header
11027                        if let Some(token) = self.tokens.get(self.current) {
11028                            let word = self.interner.resolve(token.lexeme).to_lowercase();
11029                            if word == "optimize" {
11030                                pending_opt_flags = OptimizationConfig::all_off();
11031                            } else if let Some(opt) = by_keyword(&word) {
11032                                pending_opt_flags.set(opt, false);
11033                            }
11034                            // Unknown annotation — ignore silently.
11035                            self.advance(); // consume the flag word
11036                        }
11037                        while self.check(&TokenType::Newline) {
11038                            self.advance();
11039                        }
11040                        continue;
11041                    }
11042                    BlockType::Require => {
11043                        // Optimization DEMAND: `## Require <Keyword>` records that the
11044                        // named optimization must fire for this program, or the compile
11045                        // fails (the inverse of `## No`). Program-wide, collected
11046                        // directly. Answers Ray Myers' wish to declare a dependence on
11047                        // an optimization so a missing one becomes an error.
11048                        self.advance(); // consume the `## Require` header
11049                        if let Some(token) = self.tokens.get(self.current) {
11050                            let word = self.interner.resolve(token.lexeme).to_lowercase();
11051                            if let Some(opt) = by_keyword(&word) {
11052                                self.required_opts.push(opt);
11053                            }
11054                            // Unknown keyword — ignore silently, like `## No`.
11055                            self.advance(); // consume the keyword
11056                        }
11057                        while self.check(&TokenType::Newline) {
11058                            self.advance();
11059                        }
11060                        continue;
11061                    }
11062                    BlockType::Tier => {
11063                        // Tiered-optimizer pin: `## Tier <opt> <eager|t1|t2|t3|never>`
11064                        // overrides the hotness tier at which that optimization runs.
11065                        // Program-wide (governs the whole program's tiering ladder),
11066                        // collected directly — no per-function pending state.
11067                        self.advance(); // consume the `## Tier` header
11068                        let kw = self
11069                            .tokens
11070                            .get(self.current)
11071                            .map(|t| self.interner.resolve(t.lexeme).to_lowercase());
11072                        if kw.is_some() {
11073                            self.advance(); // consume the optimization keyword
11074                        }
11075                        let val = self
11076                            .tokens
11077                            .get(self.current)
11078                            .map(|t| self.interner.resolve(t.lexeme).to_lowercase());
11079                        if val.is_some() {
11080                            self.advance(); // consume the tier value
11081                        }
11082                        if let (Some(kw), Some(val)) = (kw, val) {
11083                            if let (Some(opt), Some(pin)) = (by_keyword(&kw), pin_from_str(&val)) {
11084                                self.program_tier_pins.set(opt, pin);
11085                            }
11086                            // Unknown opt / value — ignore silently.
11087                        }
11088                        while self.check(&TokenType::Newline) {
11089                            self.advance();
11090                        }
11091                        continue;
11092                    }
11093                    BlockType::Function => {
11094                        in_definition_block = false;
11095                        self.mode = ParserMode::Imperative;
11096                        let header_tok = self.current;
11097                        self.advance();
11098                        // Parse function definition, passing pending annotations
11099                        let flags = std::mem::take(&mut pending_opt_flags);
11100                        let func_def = self.parse_function_def_with_flags(flags)?;
11101                        statements.push(func_def);
11102                        self.record_stmt_span(header_tok);
11103                        continue;
11104                    }
11105                    BlockType::Component => {
11106                        in_definition_block = false;
11107                        self.mode = ParserMode::Imperative;
11108                        let header_tok = self.current;
11109                        self.advance();
11110                        let comp_def = self.parse_lgx_component_block()?;
11111                        statements.push(comp_def);
11112                        self.record_stmt_span(header_tok);
11113                        continue;
11114                    }
11115                    BlockType::Page { path } => {
11116                        in_definition_block = false;
11117                        self.mode = ParserMode::Imperative;
11118                        let header_tok = self.current;
11119                        let path = *path;
11120                        let def = self.parse_page_block(path)?;
11121                        statements.push(def);
11122                        self.record_stmt_span(header_tok);
11123                        continue;
11124                    }
11125                    BlockType::Store => {
11126                        in_definition_block = false;
11127                        self.mode = ParserMode::Imperative;
11128                        let header_tok = self.current;
11129                        let def = self.parse_store_block()?;
11130                        statements.push(def);
11131                        self.record_stmt_span(header_tok);
11132                        continue;
11133                    }
11134                    BlockType::Directive => {
11135                        in_definition_block = false;
11136                        self.mode = ParserMode::Imperative;
11137                        let header_tok = self.current;
11138                        let def = self.parse_directive_block()?;
11139                        statements.push(def);
11140                        self.record_stmt_span(header_tok);
11141                        continue;
11142                    }
11143                    BlockType::Route { method, path } => {
11144                        in_definition_block = false;
11145                        self.mode = ParserMode::Imperative;
11146                        let header_tok = self.current;
11147                        let (method, path) = (*method, *path);
11148                        let def = self.parse_route_block(method, path)?;
11149                        statements.push(def);
11150                        self.record_stmt_span(header_tok);
11151                        continue;
11152                    }
11153                    BlockType::Middleware => {
11154                        in_definition_block = false;
11155                        self.mode = ParserMode::Imperative;
11156                        let header_tok = self.current;
11157                        let def = self.parse_middleware_block()?;
11158                        statements.push(def);
11159                        self.record_stmt_span(header_tok);
11160                        continue;
11161                    }
11162                    BlockType::Redirect { from, to } => {
11163                        in_definition_block = false;
11164                        self.mode = ParserMode::Imperative;
11165                        let header_tok = self.current;
11166                        let (from, to) = (*from, *to);
11167                        let def = self.parse_redirect_block(from, to)?;
11168                        statements.push(def);
11169                        self.record_stmt_span(header_tok);
11170                        continue;
11171                    }
11172                    BlockType::ErrorPage { status } => {
11173                        in_definition_block = false;
11174                        self.mode = ParserMode::Imperative;
11175                        let header_tok = self.current;
11176                        let status = *status;
11177                        let def = self.parse_error_page_block(status)?;
11178                        statements.push(def);
11179                        self.record_stmt_span(header_tok);
11180                        continue;
11181                    }
11182                    BlockType::TypeDef => {
11183                        // Type definitions are handled by DiscoveryPass
11184                        // Skip content until next block header
11185                        self.advance();
11186                        self.skip_type_def_content();
11187                        continue;
11188                    }
11189                    BlockType::Session => {
11190                        // ★ THE FIELDS ARE READ ONCE, BY THE DISCOVERY PASS, which registers them
11191                        // as the record `Session` — the same route `## Store` takes, and for the
11192                        // same reason: the registry is cloned before parsing, so a type
11193                        // synthesized later reaches the statement list and never the passes that
11194                        // resolve a field to a static slot. The parser only has to remember that
11195                        // a session was DECLARED, because a program that happens to define a
11196                        // record called `Session` has not asked for one.
11197                        self.advance();
11198                        self.session_declared = true;
11199                        self.skip_type_def_content();
11200                        continue;
11201                    }
11202                    BlockType::Policy => {
11203                        // Phase 50: Policy definitions are handled by DiscoveryPass
11204                        // Skip content until next block header
11205                        in_definition_block = true; // Reuse flag to skip content
11206                        self.mode = ParserMode::Declarative;
11207                        self.advance();
11208                        continue;
11209                    }
11210                    BlockType::Hardware | BlockType::Property => {
11211                        // Hardware signal declarations and temporal property assertions
11212                        // Skip content until next block header
11213                        in_definition_block = true;
11214                        self.mode = ParserMode::Declarative;
11215                        self.advance();
11216                        continue;
11217                    }
11218                    BlockType::Theorem => {
11219                        // Phase 63: Parse theorem block
11220                        in_definition_block = false;
11221                        self.mode = ParserMode::Declarative;
11222                        let header_tok = self.current;
11223                        self.advance();
11224                        let theorem = self.parse_theorem_block()?;
11225                        statements.push(theorem);
11226                        self.record_stmt_span(header_tok);
11227                        continue;
11228                    }
11229                    BlockType::Define => {
11230                        // Rung 0a: vernacular-logic predicate definition.
11231                        in_definition_block = false;
11232                        self.mode = ParserMode::Declarative;
11233                        let header_tok = self.current;
11234                        self.advance();
11235                        let definition = self.parse_define_block()?;
11236                        statements.push(definition);
11237                        self.record_stmt_span(header_tok);
11238                        continue;
11239                    }
11240                    BlockType::Axiom => {
11241                        // A named formal first-order axiom (the seam for Tarski).
11242                        in_definition_block = false;
11243                        self.mode = ParserMode::Declarative;
11244                        let header_tok = self.current;
11245                        self.advance();
11246                        let axiom = self.parse_axiom_block()?;
11247                        statements.push(axiom);
11248                        self.record_stmt_span(header_tok);
11249                        continue;
11250                    }
11251                    BlockType::Theory => {
11252                        // A named development grouping formal axioms and theorems.
11253                        in_definition_block = false;
11254                        self.mode = ParserMode::Declarative;
11255                        let header_tok = self.current;
11256                        self.advance();
11257                        let theory = self.parse_theory_block()?;
11258                        statements.push(theory);
11259                        self.record_stmt_span(header_tok);
11260                        continue;
11261                    }
11262                    BlockType::Requires => {
11263                        in_definition_block = false;
11264                        self.mode = ParserMode::Declarative;
11265                        let header_tok = self.current;
11266                        self.advance();
11267                        let deps = self.parse_requires_block()?;
11268                        for _ in 0..deps.len() {
11269                            self.record_stmt_span(header_tok);
11270                        }
11271                        statements.extend(deps);
11272                        continue;
11273                    }
11274                    BlockType::Test { name } => {
11275                        // `## Test: <prose>` — an independent test entry point
11276                        // with an imperative body (the in-language test
11277                        // framework).
11278                        let name = *name;
11279                        in_definition_block = false;
11280                        self.mode = ParserMode::Imperative;
11281                        let header_tok = self.current;
11282                        self.advance();
11283                        let test_def = self.parse_test_block(name)?;
11284                        statements.push(test_def);
11285                        self.record_stmt_span(header_tok);
11286                        continue;
11287                    }
11288                    _ => {
11289                        // Skip other declarative blocks (Proof, Example, Logic, Note)
11290                        in_definition_block = false;
11291                        self.mode = ParserMode::Declarative;
11292                        self.advance();
11293                        continue;
11294                    }
11295                }
11296            }
11297
11298            // Skip Definition block content - handled by DiscoveryPass
11299            if in_definition_block {
11300                self.advance();
11301                continue;
11302            }
11303
11304            // Skip indent/dedent/newline tokens at program level
11305            if self.check(&TokenType::Indent)
11306                || self.check(&TokenType::Dedent)
11307                || self.check(&TokenType::Newline)
11308            {
11309                self.advance();
11310                continue;
11311            }
11312
11313            // In imperative mode, parse statements
11314            if self.mode == ParserMode::Imperative {
11315                // `Accept computed <Name> where <param> is an Int from <lo> to <hi>` is a
11316                // pure parse-time contract declaration — it records the named bounds and
11317                // emits NO runtime statement (a later `Run … under <Name>` inlines them).
11318                if self.check_word("Accept") {
11319                    self.parse_accept_contract()?;
11320                    continue;
11321                }
11322                let start_tok = self.current;
11323                let stmt = self.parse_moving_statement()?;
11324                statements.push(stmt);
11325
11326                if self.check(&TokenType::Period) {
11327                    self.advance();
11328                }
11329                self.record_stmt_span(start_tok);
11330            } else {
11331                // In declarative mode (Theorem, etc.), skip for now
11332                self.advance();
11333            }
11334        }
11335
11336        // Any decorators not consumed by a function (trailing, or a library with
11337        // no `## Main`) are file-level.
11338        self.program_opt_flags = self.program_opt_flags.merged(&pending_opt_flags);
11339
11340        // ★ FOUR CHECKS THAT ONLY MEAN SOMETHING ONCE EVERY `##` BLOCK IN THE FILE HAS BEEN SEEN
11341        // (L11). A single re-parsed block (`self.fragment_mode`, set by the editor's per-block
11342        // recovery reparse — see the field's own doc comment) has no OTHER block to check `Mount`/
11343        // routes/stores against: running these against only this block's own statements would
11344        // report false refusals for names the rest of the file declares. Skipped rather than run
11345        // partially — see `fragment_mode`'s doc comment for exactly what a fragment reparse still
11346        // does not get from this, and why.
11347        if !self.fragment_mode {
11348            // ★ A `Mount` NAMES SOMETHING THAT EXISTS. The legacy dialect desugars an unknown name
11349            // into a call to `__render_<Name>` — a symbol the author never wrote — so `Mount App.`
11350            // beside a page compiled clean, built clean, and failed in the BROWSER as
11351            // "Unknown function: __render_App". Checked here, at end-of-parse, because a `## Main`
11352            // may sit above the `## Component` it mounts.
11353            self.check_mounts_name_something()?;
11354            self.check_mounted_components_need_no_props(&statements)?;
11355
11356            // ★ A PAGE MOUNTS ITSELF. `## Page /about` owns a URL and is the root of that URL's
11357            // document — there is nothing else for it to be, and no sentence its author could write
11358            // instead: its component is interned as `__page__about`, a name nowhere in the source.
11359            // Without this, a page parsed, decided its URL, served a 200, and the document was empty.
11360            self.mount_the_declared_page(&mut statements)?;
11361
11362            // The whole-file LGX resolution, BEFORE the synthesized functions are folded in: every
11363            // `Call <Store>'s <action>` names a store that exists and an action it declares, every
11364            // attribute whose name is a declared `## Directive` becomes one, every `When "<sel>"`
11365            // names an element some view renders, and every renderable component gains the
11366            // `__render_<Name>` function the bytecode tier compiles. Done at PARSE time, so every
11367            // tier and every tool inherits it — and done here, because the lowering ADDS to the
11368            // deferred list the block below consumes.
11369            self.resolve_lgx(&mut statements)?;
11370
11371            // A `shared on` store's replicas join their topic on first access, publish what they
11372            // move and merge what other pages publish (`lgx_lower::shared`): a pass over the whole
11373            // lowered program, so it runs once every lowered function exists.
11374            self.lower_shared_stores(&mut statements);
11375        }
11376
11377        // Prepend any functions synthesized during statement parsing (the reactive
11378        // `render` entry a `Mount <Component>` needs). They must be top-level (the
11379        // VM/wasm backends collect functions only from this list) and precede Main (the
11380        // interpreter registers functions as it walks, so `render` must be defined
11381        // before Main calls it). Spans stay 1:1 with a synthetic span each.
11382        if !self.deferred_functions.is_empty() {
11383            let deferred = std::mem::take(&mut self.deferred_functions);
11384            let span = self
11385                .stmt_spans
11386                .first()
11387                .copied()
11388                .unwrap_or_else(|| self.current_span());
11389            let mut combined = deferred;
11390            for _ in 0..combined.len() {
11391                self.stmt_spans.insert(0, span);
11392            }
11393            combined.extend(statements);
11394            statements = combined;
11395        }
11396
11397        // ★ THE LAW OF EXCLUSIVITY, over the whole file as every tier will run it — the author's
11398        // statements and everything the parse lowered them into: a binding lent to a `mutable`
11399        // parameter is the callee's alone until the call returns.
11400        if !self.fragment_mode {
11401            self.check_lends_are_exclusive(&statements)?;
11402        }
11403
11404        debug_assert_eq!(
11405            self.stmt_spans.len(),
11406            statements.len(),
11407            "stmt_spans must stay aligned 1:1 with the statement list"
11408        );
11409
11410        // The AST depth gate: "parsed ⇒ bounded". Any tree this function
11411        // returns is safe for every downstream recursive walker — deep
11412        // chains built iteratively above are caught here; deep parse-time
11413        // recursion (parenthesis towers) is caught by `recursion_depth`.
11414        crate::ast_depth::validate_program_depth(
11415            &statements,
11416            self.stmt_spans
11417                .first()
11418                .copied()
11419                .unwrap_or_else(|| self.current_span()),
11420        )?;
11421
11422        Ok(statements)
11423    }
11424
11425    /// Record every `## Component <Name>` header in the token stream before any statement is
11426    /// read, so a sentence that names a component (`Mount App.`, a legacy view's `Counter`)
11427    /// resolves it wherever in the file it is declared. `## Component Name:` (a colon after the
11428    /// name) is the legacy dialect; `## Component Name (props)` is LGX — the same rule the header
11429    /// dispatch in `parse_program` applies.
11430    fn declare_component_names(&mut self) {
11431        for (i, token) in self.tokens.iter().enumerate() {
11432            if !matches!(
11433                token.kind,
11434                TokenType::BlockHeader {
11435                    block_type: BlockType::Component
11436                }
11437            ) {
11438                continue;
11439            }
11440            let Some(name) = self.tokens.get(i + 1) else {
11441                continue;
11442            };
11443            let legacy = self
11444                .tokens
11445                .get(i + 2)
11446                .is_some_and(|t| matches!(t.kind, TokenType::Colon));
11447            self.components.insert(name.lexeme);
11448            if !legacy {
11449                self.lgx_components.insert(name.lexeme);
11450            }
11451        }
11452    }
11453
11454    /// Record every `## To <name> […]:` header's OWN name in the token stream before any
11455    /// statement is read — the same pre-scan `declare_component_names` runs, so `Call <name>.`
11456    /// (`parse_call_statement`) can recognize a declared function's name whatever lexical class
11457    /// the word carries, matching what `## To` itself already accepts (`expect_identifier`, which
11458    /// takes any word-like token). Skips an optional `native` modifier the same way
11459    /// `parse_function_def_with_flags` does, so `## To native <name> …` is covered too.
11460    fn declare_function_names(&mut self) {
11461        for (i, token) in self.tokens.iter().enumerate() {
11462            if !matches!(
11463                token.kind,
11464                TokenType::BlockHeader {
11465                    block_type: BlockType::Function
11466                }
11467            ) {
11468                continue;
11469            }
11470            let mut j = i + 1;
11471            if self
11472                .tokens
11473                .get(j)
11474                .is_some_and(|t| matches!(t.kind, TokenType::Native))
11475            {
11476                j += 1;
11477            }
11478            let Some(name) = self.tokens.get(j) else {
11479                continue;
11480            };
11481            self.declared_function_names.insert(name.lexeme);
11482        }
11483    }
11484
11485    /// Enter one level of imperative parse recursion, erroring gracefully at
11486    /// the depth limit (the parse-time half of the AST depth gate). Pair
11487    /// every call with [`Parser::leave_recursion`].
11488    pub(super) fn enter_recursion(&mut self) -> ParseResult<()> {
11489        self.recursion_depth += 1;
11490        let limit = crate::ast_depth::max_parse_recursion();
11491        if self.recursion_depth > limit {
11492            self.recursion_depth -= 1;
11493            return Err(ParseError {
11494                kind: ParseErrorKind::AstTooDeep {
11495                    depth: limit + 1,
11496                    max_depth: limit,
11497                },
11498                span: self.current_span(),
11499            });
11500        }
11501        Ok(())
11502    }
11503
11504    /// Leave one level of imperative parse recursion.
11505    pub(super) fn leave_recursion(&mut self) {
11506        self.recursion_depth = self.recursion_depth.saturating_sub(1);
11507    }
11508
11509    /// The program-wide optimization config from file-level `## No <X>` decorators
11510    /// (all-on minus the file-level disables). The compile entry combines this with
11511    /// `from_env` and per-function flags.
11512    pub fn program_opt_flags(&self) -> OptimizationConfig {
11513        self.program_opt_flags
11514    }
11515
11516    /// The program-wide tiered-optimizer pins from file-level `## Tier <opt> <value>`
11517    /// decorators (HOTSWAP §8). The run-path engine overlays these onto the env
11518    /// [`crate::optimization::HotswapConfig`] before optimizing.
11519    pub fn program_tier_pins(&self) -> PinSet {
11520        self.program_tier_pins
11521    }
11522
11523    /// Optimizations demanded via `## Require <opt>` — each must have fired for
11524    /// the compile, or it fails. Read after `parse_program`.
11525    pub fn required_optimizations(&self) -> Vec<Opt> {
11526        self.required_opts.clone()
11527    }
11528
11529    /// Every tensor the program binds — declared or computed by a sugar — as `(rows, cols)`, by symbol. The
11530    /// desugared statements carry only flat lengths; this is where a tensor's two dimensions survive parsing.
11531    pub fn tensor_shapes(&self) -> &std::collections::HashMap<Symbol, (i64, i64)> {
11532        &self.tensor_shapes
11533    }
11534
11535    /// Depth-guarded wrapper: every nested construct (If/While/Repeat/Zone
11536    /// bodies, however each parses its block) recurses through here, so one
11537    /// guard covers all statement nesting. Flat statement lists enter and
11538    /// leave pairwise — sequential code never accumulates depth.
11539    fn parse_statement(&mut self) -> ParseResult<Stmt<'a>> {
11540        self.enter_recursion()?;
11541        let start_tok = self.current;
11542        let result = self.parse_statement_inner();
11543        self.leave_recursion();
11544        if let Ok(stmt) = &result {
11545            if self.copy_lint_scope.is_some() {
11546                let span = self.span_from(start_tok);
11547                let Self {
11548                    copy_lint_scope,
11549                    interner,
11550                    ..
11551                } = self;
11552                copy_lint_scope
11553                    .as_mut()
11554                    .unwrap()
11555                    .observe(span, stmt, &**interner);
11556            }
11557        }
11558        result
11559    }
11560
11561    fn parse_statement_inner(&mut self) -> ParseResult<Stmt<'a>> {
11562        // ★ A FUNCTION THE PROGRAM DECLARES IS CALLED BY ITS NAME, WHATEVER WORD THE NAME IS. A
11563        // `## To` header accepts any word as a name, so `note(3).` calls `## To note` before
11564        // `Note` can begin an observation, `show(3).` before `Show` a display, and so for every
11565        // statement word below.
11566        if self.declared_function_names.contains(&self.peek().lexeme)
11567            && self.next_is_argument_list()
11568        {
11569            return self.parse_bare_call_statement();
11570        }
11571        // `Render:` / `Render into <el>:` was the retired reactive dialect's own view section —
11572        // a view is `View:` inside a `## Page` or `## Component` now, so name that rather than
11573        // failing on whatever token happened to follow the colon.
11574        if self.lexeme_is(0, "Render")
11575            && (matches!(
11576                self.tokens.get(self.current + 1).map(|t| &t.kind),
11577                Some(TokenType::Colon)
11578            ) || self.lexeme_is(1, "into"))
11579        {
11580            return Err(ParseError {
11581                kind: ParseErrorKind::Custom(
11582                    "`Render:` is retired — a view is a `View:` section of a `## Page` or \
11583                     `## Component`, with `State:`/`Actions:`/`Style:` beside it."
11584                        .to_string(),
11585                ),
11586                span: self.current_span(),
11587            });
11588        }
11589        // `State:` / `Actions:` / `View:` / `Style:` are the sections of a component. Outside
11590        // one they are a misplaced section, not a statement — say so instead of failing on
11591        // whatever the next token happens to be.
11592        if !self.in_component && self.next_is_section_header() {
11593            let word = self.interner.resolve(self.peek().lexeme).to_string();
11594            return Err(ParseError {
11595                kind: ParseErrorKind::Custom(format!(
11596                    "`{word}:` is a component section, but there is no component here. Start one with \
11597                     `## Component <Name>`, `## Page /<path>` or `## Store <Name>`, then its `State:`, \
11598                     `Actions:`, `View:` and `Style:` sections follow."
11599                )),
11600                span: self.current_span(),
11601            });
11602        }
11603        // Inside a component, the action vocabulary (`Focus …`, `Navigate to …`, `Get … as …,
11604        // then …`) is offered BEFORE the ordinary statement forms; outside one it is not
11605        // offered at all, so an existing program's sentences keep their meaning.
11606        // `Click …` / `Type … into …` drive a mounted component from a program or a test, and
11607        // `Reload the page.` restarts one, so those are available anywhere; the rest of the
11608        // vocabulary is component-only.
11609        let reloads_the_page =
11610            self.lexeme_is(0, "Reload") && self.lexeme_is(1, "the") && self.lexeme_is(2, "page");
11611        // Outside a component, UI verbs are allowed BY NAME rather than wholesale: `Save`,
11612        // `Focus`, `Load` and friends are ordinary English, and an ordinary program that starts a
11613        // sentence with one must keep whatever meaning it had. The names below are the DRIVING
11614        // verbs — the ones a `## Test` needs to work a page — and each is specific enough that no
11615        // ordinary sentence begins with it by accident. `Check` is deliberately NOT here: it is
11616        // already the policy statement `Check … can/is …`.
11617        // The openers come from ONE table — see `UI_STATEMENT_OPENERS`. This gate and the verb
11618        // parser were two hand-written lists of the same thing and had drifted in both directions.
11619        // `Press` and `Type` keep their own line because both are ordinary English at the start of
11620        // a sentence and need the "not followed by a colon" guard.
11621        if self.in_component
11622            || reloads_the_page
11623            || crate::parser::component::UI_STATEMENT_OPENERS
11624                .iter()
11625                .any(|w| self.lexeme_is(0, w))
11626            || (self.lexeme_is(0, "Press") && !self.next_is_colon_like())
11627            || (self.lexeme_is(0, "Type") && !self.next_is_colon_like())
11628        {
11629            if let Some(stmt) = self.parse_ui_statement()? {
11630                return Ok(stmt);
11631            }
11632        }
11633        // `Given that METHOD "/url" answers STATUS with BODY.` — how a test answers a fetch
11634        // without a network. Recognised by the WHOLE SHAPE rather than by its first word, since
11635        // "given" is ordinary English; `Given that GET …` is not a sentence anyone writes by
11636        // accident.
11637        // `Replay these actions: a, b, c.` — recognised by the whole shape; "replay" on its own
11638        // is ordinary English.
11639        if self.lexeme_is(0, "Replay") && self.lexeme_is(1, "these") && self.lexeme_is(2, "actions")
11640        {
11641            for _ in 0..3 {
11642                self.advance();
11643            }
11644            if self.check(&TokenType::Colon) {
11645                self.advance();
11646            }
11647            let mut actions = vec![self.parse_journal_action_name()?];
11648            while self.check(&TokenType::Comma) || self.check_word("and") {
11649                self.advance();
11650                actions.push(self.parse_journal_action_name()?);
11651            }
11652            return Ok(Stmt::Replay { actions });
11653        }
11654        // `Serve fetches with routes.` — recognised by the whole shape, since `Serve` already
11655        // starts the server statement (`Serve on port 8080.`) and must keep that meaning.
11656        if self.lexeme_is(0, "Serve")
11657            && self.lexeme_is(1, "fetches")
11658            && self.lexeme_is(2, "with")
11659            && self.lexeme_is(3, "routes")
11660        {
11661            for _ in 0..4 {
11662                self.advance();
11663            }
11664            return Ok(Stmt::ServeFetches);
11665        }
11666        if self.lexeme_is(0, "Given") && self.lexeme_is(1, "that") {
11667            if let Some(stmt) = self.parse_given_fetch()? {
11668                return Ok(stmt);
11669            }
11670        }
11671        if self.at_limit_rule() {
11672            return self.parse_limit_rule();
11673        }
11674        // Phase 32: Function definitions can appear inside Main block
11675        // Handle both TokenType::To and Preposition("to")
11676        if self.check(&TokenType::To) || self.check_preposition_is("to") {
11677            return self.parse_function_def();
11678        }
11679        if self.check(&TokenType::Let) {
11680            return self.parse_let_statement();
11681        }
11682        // The declarative view DSL: `Render:` + an indented element tree. Checked
11683        // before the `x: Type = value` assignment path (which would otherwise grab
11684        // `Render:`); the `Colon` + `Indent` lookahead keeps a variable named
11685        // `Render` free for ordinary use.
11686        // Phase 23b: Equals-style assignment with explicit `mut` keyword
11687        // Syntax: `mut x = 5` or `mut x: Int = 5`
11688        if self.check(&TokenType::Mut) {
11689            return self.parse_equals_assignment(true);
11690        }
11691        // Phase 23b: Equals-style assignment (identifier = value)
11692        // Syntax: `x = 5` or `x: Int = 5`
11693        if self.peek_equals_assignment() {
11694            return self.parse_equals_assignment(false);
11695        }
11696        // Compound assignment: `x += e`, `grid[i] *= 2`, `p.count += 1`.
11697        if self.peek_compound_assignment() {
11698            return self.parse_compound_assignment();
11699        }
11700        if self.check(&TokenType::Set) {
11701            return self.parse_set_statement();
11702        }
11703        // Statement-level async event handler: `On <event> from <source>:` — the media/peer
11704        // event form (`On chunk from encoder:`, `On track from peer:`). Gated on the `from`
11705        // two tokens ahead (`On` <event> `from`) so a stray `On` identifier is left untouched.
11706        if self.check_word("On")
11707            && self
11708                .tokens
11709                .get(self.current + 2)
11710                .is_some_and(|t| matches!(t.kind, TokenType::From))
11711        {
11712            return self.parse_on_statement();
11713        }
11714        if self.check(&TokenType::Return) {
11715            return self.parse_return_statement();
11716        }
11717        if self.check(&TokenType::Break) {
11718            return self.parse_break_statement();
11719        }
11720        if self.check(&TokenType::If) {
11721            return self.parse_if_statement();
11722        }
11723        if self.check(&TokenType::Assert) {
11724            return self.parse_assert_statement();
11725        }
11726        if self.check(&TokenType::Require) {
11727            return self.parse_require_statement();
11728        }
11729        if self.check_word("Assume") {
11730            return self.parse_assume_statement();
11731        }
11732        if self.check_word("Fail") && self.check_word_at(self.current + 1, "with") {
11733            return self.parse_fail_statement();
11734        }
11735        if self.check_word("Solve") {
11736            return self.parse_solve_statement();
11737        }
11738        if self.check_word("Minimize") {
11739            return self.parse_minimize_statement();
11740        }
11741        // Phase 35: Trust statement
11742        if self.check(&TokenType::Trust) {
11743            return self.parse_trust_statement();
11744        }
11745        // The test framework's statement family — each parser gates itself to `## Test` bodies with a teaching error.
11746        if self.check(&TokenType::Expect) {
11747            return self.parse_expect_statement();
11748        }
11749        if self.check(&TokenType::Note) {
11750            return self.parse_observe_statement(crate::ast::stmt::ObsCategory::Note);
11751        }
11752        if self.check(&TokenType::Warn) {
11753            return self.parse_observe_statement(crate::ast::stmt::ObsCategory::Warn);
11754        }
11755        if self.check(&TokenType::Record) {
11756            return self.parse_observe_statement(crate::ast::stmt::ObsCategory::Record);
11757        }
11758        if self.check(&TokenType::During) {
11759            return self.parse_during_statement();
11760        }
11761        if self.check(&TokenType::Advance) {
11762            return self.parse_advance_time_statement();
11763        }
11764        if self.check(&TokenType::Given) && self.mode == ParserMode::Imperative {
11765            return self.parse_given_file_statement();
11766        }
11767        if self.check(&TokenType::Skip) {
11768            return Err(ParseError {
11769                kind: ParseErrorKind::Custom(
11770                    "`Skip this test because \"…\".` belongs in a test's leading \
11771                     paragraph, before any other statement"
11772                        .to_string(),
11773                ),
11774                span: self.current_span(),
11775            });
11776        }
11777        // Phase 50: Security Check statement
11778        if self.check(&TokenType::Check) {
11779            return self.parse_check_statement();
11780        }
11781        // Phase 51: P2P Networking statements
11782        if self.check(&TokenType::Listen) {
11783            return self.parse_listen_statement();
11784        }
11785        // HTTP server: `Serve on port <expr> with <handler>.`
11786        if self.check_word("Serve") {
11787            return self.parse_serve_statement();
11788        }
11789        if self.check(&TokenType::NetConnect) {
11790            return self.parse_connect_statement();
11791        }
11792        if self.check(&TokenType::Sleep) {
11793            return self.parse_sleep_statement();
11794        }
11795        // `Run <f> on <arg> under <Name> into <var>` — invoke a shipped computation through a
11796        // named acceptance contract; desugars to `Let <var> be run_accepted(<f>, <arg>, lo, hi)`.
11797        if self.check_word("Run") {
11798            return self.parse_run_under_contract();
11799        }
11800        // Phase 52: GossipSub sync statement
11801        if self.check(&TokenType::Sync) {
11802            return self.parse_sync_statement();
11803        }
11804        // Phase 53: Persistent storage mount statement
11805        if self.check(&TokenType::Mount) {
11806            return self.parse_mount_statement();
11807        }
11808        if self.check(&TokenType::While) {
11809            return self.parse_while_statement();
11810        }
11811        if self.check(&TokenType::Repeat) {
11812            return self.parse_repeat_statement();
11813        }
11814        // `For all <domain> <vars>:` inside a test block is a PROPERTY
11815        // (constitution §1.6), distinct from the `For x in xs:` loop.
11816        if self.check(&TokenType::For)
11817            && self.in_test_block
11818            && self.peek_lexeme_lower(1).as_deref() == Some("all")
11819        {
11820            return self.parse_forall_statement();
11821        }
11822        // Phase 30b: Allow "for" without "Repeat" keyword
11823        if self.check(&TokenType::For) {
11824            return self.parse_for_statement();
11825        }
11826        if self.check(&TokenType::Call) {
11827            return self.parse_call_statement();
11828        }
11829        if self.check(&TokenType::Give) {
11830            return self.parse_give_statement();
11831        }
11832        if self.check(&TokenType::Show) {
11833            return self.parse_show_statement();
11834        }
11835        // Phase 33: Pattern matching on sum types
11836        if self.check(&TokenType::Inspect) {
11837            return self.parse_inspect_statement();
11838        }
11839
11840        // Phase 43D: Collection operations
11841        if self.check(&TokenType::Push) {
11842            return self.parse_push_statement();
11843        }
11844        if self.check(&TokenType::Pop) {
11845            return self.parse_pop_statement();
11846        }
11847        // Set operations
11848        if self.check(&TokenType::Add) {
11849            return self.parse_add_statement();
11850        }
11851        if self.check(&TokenType::Remove) {
11852            return self.parse_remove_statement();
11853        }
11854
11855        // Phase 8.5: Memory zone block
11856        if self.check(&TokenType::Inside) {
11857            return self.parse_zone_statement();
11858        }
11859
11860        // Phase 9: Structured Concurrency blocks
11861        if self.check(&TokenType::Attempt) {
11862            return self.parse_concurrent_block();
11863        }
11864        if self.check(&TokenType::Simultaneously) {
11865            return self.parse_parallel_block();
11866        }
11867
11868        // Reversible / uncompute block: `Reversibly:` (alias `Uncompute:`) — an
11869        // output-transparent body whose in-place abelian scalar updates lower to
11870        // the zero-garbage reversible ISA (`Op::RevAdd`/`RevSub`/`RevXor`/`RevNeg`).
11871        if self.check_word("Reversibly") || self.check_word("Uncompute") {
11872            return self.parse_reversibly_block();
11873        }
11874
11875        // Phase 10: IO statements
11876        if self.check(&TokenType::Read) {
11877            return self.parse_read_statement();
11878        }
11879        if self.check(&TokenType::Write) {
11880            return self.parse_write_statement();
11881        }
11882
11883        // Phase 46: Agent System statements
11884        if self.check(&TokenType::Spawn) {
11885            return self.parse_spawn_statement();
11886        }
11887        if self.check_word("Stream") {
11888            return self.parse_stream_statement();
11889        }
11890        if self.check(&TokenType::Send) {
11891            // Phase 54: Disambiguate "Send x into pipe" vs "Send x to agent"
11892            if self.lookahead_contains_into() {
11893                return self.parse_send_pipe_statement();
11894            }
11895            return self.parse_send_statement();
11896        }
11897        if self.check(&TokenType::Await) {
11898            // Phase 54: Disambiguate "Await the first of:" vs "Await response from agent"
11899            if self.lookahead_is_first_of() {
11900                return self.parse_select_statement();
11901            }
11902            // Task-handle join: `Await h` / `Await h into r` (no `response`/`view`/`stream`
11903            // knob and no `from`) — distinct from the relay `Await response from … into …`.
11904            if self.lookahead_is_task_await() {
11905                return self.parse_task_await_statement();
11906            }
11907            return self.parse_await_statement();
11908        }
11909        // Task-handle join spelled `Wait for h` (+ optional `into r`).
11910        if self.check_word("Wait") {
11911            return self.parse_wait_statement();
11912        }
11913        // Close a channel (pipe): `Close ch` / `Close the pipe ch`.
11914        if self.check_word("Close") {
11915            return self.parse_close_statement();
11916        }
11917
11918        // Phase 49: CRDT statements
11919        if self.check(&TokenType::Merge) {
11920            return self.parse_merge_statement();
11921        }
11922        if self.check(&TokenType::Converge) {
11923            return self.parse_converge_statement();
11924        }
11925        if self.check(&TokenType::Swap) {
11926            return self.parse_swap_statement();
11927        }
11928        if self.check(&TokenType::Increase) {
11929            return self.parse_increase_statement();
11930        }
11931        // Phase 49b: Extended CRDT statements
11932        if self.check(&TokenType::Decrease) {
11933            return self.parse_decrease_statement();
11934        }
11935        if self.check(&TokenType::Append) {
11936            return self.parse_append_statement();
11937        }
11938        if self.check(&TokenType::Resolve) {
11939            return self.parse_resolve_statement();
11940        }
11941
11942        // Phase 54: Go-like Concurrency statements
11943        if self.check(&TokenType::Launch) {
11944            return self.parse_launch_statement();
11945        }
11946        if self.check(&TokenType::Stop) {
11947            return self.parse_stop_statement();
11948        }
11949        if self.check(&TokenType::Try) {
11950            return self.parse_try_statement();
11951        }
11952        if self.check(&TokenType::Receive) {
11953            return self.parse_receive_pipe_statement();
11954        }
11955
11956        // Escape hatch: raw foreign code blocks
11957        if self.check(&TokenType::Escape) {
11958            return self.parse_escape_statement();
11959        }
11960
11961        // Expression-statement: function call without "Call" keyword
11962        // e.g., `greet("Alice").` instead of `Call greet with "Alice".`
11963        if self.next_is_argument_list() {
11964            return self.parse_bare_call_statement();
11965        }
11966
11967        // `x is 5.` at statement position: value comparison spelled with the
11968        // predicate copula. Diagnose it specifically — a generic
11969        // ExpectedStatement would bury the actual lesson (say `equals`).
11970        // Only literal values qualify: `x is even.` is predication, not a
11971        // botched comparison.
11972        if self
11973            .tokens
11974            .get(self.current + 1)
11975            .is_some_and(|t| matches!(t.kind, TokenType::Is))
11976        {
11977            if let Some(value_token) = self.tokens.get(self.current + 2) {
11978                if matches!(
11979                    value_token.kind,
11980                    TokenType::Number(_)
11981                        | TokenType::StringLiteral(_)
11982                        | TokenType::InterpolatedString(_)
11983                ) {
11984                    let variable = self.interner.resolve(self.peek().lexeme).to_string();
11985                    let value = self.interner.resolve(value_token.lexeme).to_string();
11986                    let span = Span::new(self.peek().span.start, value_token.span.end);
11987                    return Err(ParseError {
11988                        kind: ParseErrorKind::IsValueEquality { variable, value },
11989                        span,
11990                    });
11991                }
11992            }
11993        }
11994
11995        Err(ParseError {
11996            kind: ParseErrorKind::ExpectedStatement,
11997            span: self.current_span(),
11998        })
11999    }
12000
12001    fn parse_if_statement(&mut self) -> ParseResult<Stmt<'a>> {
12002        self.advance(); // consume "If"
12003
12004        // Parse condition expression (simple: identifier equals value)
12005        let cond = self.parse_condition()?;
12006
12007        // Optionally consume "then" before the colon (supports "If x = 5 then:" syntax)
12008        if self.check(&TokenType::Then) {
12009            self.advance();
12010        }
12011
12012        self.parse_if_branches(cond)
12013    }
12014
12015    /// Parse an elif clause as an if statement.
12016    /// Called after "elif" has been consumed - parses condition and body directly.
12017    fn parse_elif_as_if(&mut self) -> ParseResult<Stmt<'a>> {
12018        // Parse condition expression (elif is already consumed)
12019        let cond = self.parse_condition()?;
12020        self.parse_if_branches(cond)
12021    }
12022
12023    /// The rest of an `If` once its condition is read: the colon, the then
12024    /// body, and the else clause — `Otherwise:`, an `Otherwise If` / `Else If`
12025    /// / `elif` chain, or none.
12026    fn parse_if_branches(&mut self, cond: &'a Expr<'a>) -> ParseResult<Stmt<'a>> {
12027        // Expect colon
12028        if !self.check(&TokenType::Colon) {
12029            return Err(ParseError {
12030                kind: ParseErrorKind::ExpectedKeyword {
12031                    keyword: ":".to_string(),
12032                },
12033                span: self.current_span(),
12034            });
12035        }
12036        self.advance(); // consume ":"
12037
12038        // Parse the then body — an indented block, OR (inline guard) a single
12039        // statement on the same line: `If c: Return 1.`
12040        let then_block = self.read_branch_body()?;
12041
12042        // Check for else clause: Otherwise/Else/Otherwise If/Else If/elif
12043        let else_block = if self.check(&TokenType::Otherwise) || self.check(&TokenType::Else) {
12044            self.advance(); // consume "Otherwise" or "Else"
12045
12046            // Check for "Otherwise If" / "Else If" chain
12047            if self.check(&TokenType::If) {
12048                // Parse as else-if: create single-statement else block containing nested If
12049                let link_tok = self.current;
12050                let nested_if = self.parse_if_statement()?;
12051                Some(self.read_link_block(nested_if, link_tok))
12052            } else {
12053                // Regular else block - expect colon and indent
12054                if !self.check(&TokenType::Colon) {
12055                    return Err(ParseError {
12056                        kind: ParseErrorKind::ExpectedKeyword {
12057                            keyword: ":".to_string(),
12058                        },
12059                        span: self.current_span(),
12060                    });
12061                }
12062                self.advance(); // consume ":"
12063
12064                // Indented block OR inline: `Otherwise: Show "x".`
12065                Some(self.read_branch_body()?)
12066            }
12067        } else if self.check(&TokenType::Elif) {
12068            // Python-style elif: equivalent to "Else If"
12069            let link_tok = self.current;
12070            self.advance(); // consume "elif"
12071                            // Parse the condition and body directly (elif acts like "Else If" without the separate If token)
12072            let nested_if = self.parse_elif_as_if()?;
12073            Some(self.read_link_block(nested_if, link_tok))
12074        } else {
12075            None
12076        };
12077
12078        Ok(Stmt::If {
12079            cond,
12080            then_block,
12081            else_block,
12082        })
12083    }
12084
12085    fn parse_while_statement(&mut self) -> ParseResult<Stmt<'a>> {
12086        self.advance(); // consume "While"
12087
12088        let cond = self.parse_condition()?;
12089
12090        // Phase 44: Parse optional (decreasing expr)
12091        let decreasing = if self.check(&TokenType::LParen) {
12092            self.advance(); // consume '('
12093
12094            // Expect "decreasing" keyword
12095            if !self.check_word("decreasing") {
12096                return Err(ParseError {
12097                    kind: ParseErrorKind::ExpectedKeyword {
12098                        keyword: "decreasing".to_string(),
12099                    },
12100                    span: self.current_span(),
12101                });
12102            }
12103            self.advance(); // consume "decreasing"
12104
12105            let variant = self.parse_imperative_expr()?;
12106
12107            if !self.check(&TokenType::RParen) {
12108                return Err(ParseError {
12109                    kind: ParseErrorKind::ExpectedKeyword {
12110                        keyword: ")".to_string(),
12111                    },
12112                    span: self.current_span(),
12113                });
12114            }
12115            self.advance(); // consume ')'
12116
12117            Some(variant)
12118        } else {
12119            None
12120        };
12121
12122        if !self.check(&TokenType::Colon) {
12123            return Err(ParseError {
12124                kind: ParseErrorKind::ExpectedKeyword {
12125                    keyword: ":".to_string(),
12126                },
12127                span: self.current_span(),
12128            });
12129        }
12130        self.advance(); // consume ":"
12131
12132        let body = self.read_indented_block()?;
12133
12134        Ok(Stmt::While {
12135            cond,
12136            body,
12137            decreasing,
12138        })
12139    }
12140
12141    /// Parse a loop pattern: single identifier or tuple destructuring.
12142    /// Examples: `x` or `(k, v)` or `(a, b, c)`
12143    fn parse_loop_pattern(&mut self) -> ParseResult<Pattern> {
12144        use crate::ast::stmt::Pattern;
12145
12146        // Check for tuple pattern: (x, y, ...)
12147        if self.check(&TokenType::LParen) {
12148            self.advance(); // consume "("
12149
12150            let mut identifiers = Vec::new();
12151            loop {
12152                let id = self.expect_identifier()?;
12153                identifiers.push(id);
12154
12155                // Check for comma to continue
12156                if self.check(&TokenType::Comma) {
12157                    self.advance(); // consume ","
12158                    continue;
12159                }
12160                break;
12161            }
12162
12163            // Expect closing paren
12164            if !self.check(&TokenType::RParen) {
12165                return Err(ParseError {
12166                    kind: ParseErrorKind::ExpectedKeyword {
12167                        keyword: ")".to_string(),
12168                    },
12169                    span: self.current_span(),
12170                });
12171            }
12172            self.advance(); // consume ")"
12173
12174            Ok(Pattern::Tuple(identifiers))
12175        } else {
12176            // Single identifier pattern
12177            let id = self.expect_identifier()?;
12178            Ok(Pattern::Identifier(id))
12179        }
12180    }
12181
12182    fn parse_repeat_statement(&mut self) -> ParseResult<Stmt<'a>> {
12183        self.advance(); // consume "Repeat"
12184
12185        // `Repeat forever:` — an infinite loop (exit via `Break`), lowered to `While true:`.
12186        if self.check_word("forever") {
12187            self.advance(); // consume "forever"
12188            if !self.check(&TokenType::Colon) {
12189                return Err(ParseError {
12190                    kind: ParseErrorKind::ExpectedKeyword {
12191                        keyword: ":".to_string(),
12192                    },
12193                    span: self.current_span(),
12194                });
12195            }
12196            self.advance(); // consume ":"
12197            let body = self.read_indented_block()?;
12198            let cond = self
12199                .ctx
12200                .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Boolean(true)));
12201            return Ok(Stmt::While {
12202                cond,
12203                body,
12204                decreasing: None,
12205            });
12206        }
12207
12208        // Optional "for"
12209        if self.check(&TokenType::For) {
12210            self.advance();
12211        }
12212        // Optional "each" — `Repeat for each t in xs` reads exactly like the view's
12213        // `for each …`, so the statement and view loop surfaces stay consistent.
12214        if self.check_word("each") {
12215            self.advance();
12216        }
12217        if let Some(stmt) = self.try_parse_every_natural_loop()? {
12218            return Ok(stmt);
12219        }
12220
12221        // Parse loop pattern: single identifier or tuple destructuring
12222        let pattern = self.parse_loop_pattern()?;
12223
12224        // Determine iteration type: "in" for collection, "from" for range
12225        let iterable = if self.check(&TokenType::From) || self.check_preposition_is("from") {
12226            self.advance(); // consume "from"
12227            let start = self.parse_imperative_expr()?;
12228
12229            // Expect "to" (can be keyword or preposition)
12230            if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
12231                return Err(ParseError {
12232                    kind: ParseErrorKind::ExpectedKeyword {
12233                        keyword: "to".to_string(),
12234                    },
12235                    span: self.current_span(),
12236                });
12237            }
12238            self.advance();
12239
12240            let end = self.parse_imperative_expr()?;
12241            self.ctx.alloc_imperative_expr(Expr::Range { start, end })
12242        } else if self.check(&TokenType::In) || self.check_preposition_is("in") {
12243            self.advance(); // consume "in"
12244            self.parse_imperative_expr()?
12245        } else {
12246            return Err(ParseError {
12247                kind: ParseErrorKind::ExpectedKeyword {
12248                    keyword: "in or from".to_string(),
12249                },
12250                span: self.current_span(),
12251            });
12252        };
12253
12254        // Expect colon
12255        if !self.check(&TokenType::Colon) {
12256            return Err(ParseError {
12257                kind: ParseErrorKind::ExpectedKeyword {
12258                    keyword: ":".to_string(),
12259                },
12260                span: self.current_span(),
12261            });
12262        }
12263        self.advance();
12264
12265        let body = self.read_indented_block()?;
12266
12267        Ok(Stmt::Repeat {
12268            pattern,
12269            iterable,
12270            body,
12271        })
12272    }
12273
12274    /// Parse a for-loop without the "Repeat" keyword prefix.
12275    /// Syntax: `for <var> from <start> to <end>:` or `for [each] <var> in <collection>:`
12276    fn parse_for_statement(&mut self) -> ParseResult<Stmt<'a>> {
12277        self.advance(); // consume "for"
12278                        // Optional "each" — keeps `for each t in xs` consistent with the view + `Repeat` forms.
12279        if self.check_word("each") {
12280            self.advance();
12281        }
12282        if let Some(stmt) = self.try_parse_every_natural_loop()? {
12283            return Ok(stmt);
12284        }
12285
12286        // Parse loop pattern: single identifier or tuple destructuring
12287        let pattern = self.parse_loop_pattern()?;
12288
12289        // Determine iteration type: "in" for collection, "from" for range
12290        let iterable = if self.check(&TokenType::From) || self.check_preposition_is("from") {
12291            self.advance(); // consume "from"
12292            let start = self.parse_imperative_expr()?;
12293
12294            // Expect "to" (can be keyword or preposition)
12295            if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
12296                return Err(ParseError {
12297                    kind: ParseErrorKind::ExpectedKeyword {
12298                        keyword: "to".to_string(),
12299                    },
12300                    span: self.current_span(),
12301                });
12302            }
12303            self.advance();
12304
12305            let end = self.parse_imperative_expr()?;
12306            self.ctx.alloc_imperative_expr(Expr::Range { start, end })
12307        } else if self.check(&TokenType::In) || self.check_preposition_is("in") {
12308            self.advance(); // consume "in"
12309            self.parse_imperative_expr()?
12310        } else {
12311            return Err(ParseError {
12312                kind: ParseErrorKind::ExpectedKeyword {
12313                    keyword: "in or from".to_string(),
12314                },
12315                span: self.current_span(),
12316            });
12317        };
12318
12319        // Expect colon
12320        if !self.check(&TokenType::Colon) {
12321            return Err(ParseError {
12322                kind: ParseErrorKind::ExpectedKeyword {
12323                    keyword: ":".to_string(),
12324                },
12325                span: self.current_span(),
12326            });
12327        }
12328        self.advance();
12329
12330        let body = self.read_indented_block()?;
12331
12332        Ok(Stmt::Repeat {
12333            pattern,
12334            iterable,
12335            body,
12336        })
12337    }
12338
12339    fn parse_call_statement(&mut self) -> ParseResult<Stmt<'a>> {
12340        let start_tok = self.current;
12341        self.advance(); // consume "Call"
12342
12343        // The callee accepts any word this program's own `## To` declared, whatever lexical
12344        // class the lexer gave it (a speech-act verb like `announce`, a focus particle like
12345        // `only`, …) — `declared_function_names` is pre-scanned once, before any statement is
12346        // read, the same way `declare_component_names` covers `Mount <Component>.`. A name this
12347        // program never declared (never reached by that pre-scan — an imported/native binding
12348        // outside this file's own token stream, say) falls back to the original, narrower token-
12349        // kind match instead of refusing outright.
12350        let function = if self.declared_function_names.contains(&self.peek().lexeme) {
12351            let s = self.peek().lexeme;
12352            self.advance();
12353            s
12354        } else {
12355            // Function names can be nouns, adjectives, or verbs (e.g., "work", "process").
12356            // Use the token's lexeme to match function definition casing.
12357            match &self.peek().kind {
12358                TokenType::Noun(sym) | TokenType::Adjective(sym) => {
12359                    let s = *sym;
12360                    self.advance();
12361                    s
12362                }
12363                TokenType::Verb { .. } | TokenType::Ambiguous { .. } => {
12364                    // Use lexeme (actual text) not lemma to preserve casing
12365                    let s = self.peek().lexeme;
12366                    self.advance();
12367                    s
12368                }
12369                _ => {
12370                    return Err(ParseError {
12371                        kind: ParseErrorKind::ExpectedIdentifier,
12372                        span: self.current_span(),
12373                    });
12374                }
12375            }
12376        };
12377
12378        // Expect "with" followed by arguments
12379        let args = if self.check_preposition_is("with") {
12380            self.advance(); // consume "with"
12381            self.parse_call_arguments()?
12382        } else {
12383            Vec::new()
12384        };
12385
12386        self.record_call_span(&args, start_tok);
12387        Ok(Stmt::Call { function, args })
12388    }
12389
12390    fn parse_call_arguments(&mut self) -> ParseResult<Vec<&'a Expr<'a>>> {
12391        let mut args = Vec::new();
12392
12393        // Parse first argument (may have Give keyword)
12394        let arg = self.parse_call_arg()?;
12395        args.push(arg);
12396
12397        // Parse additional arguments separated by "and" or ","
12398        while self.check(&TokenType::And) || self.check(&TokenType::Comma) {
12399            self.advance(); // consume "and" or ","
12400                            // A trailing comma (`f(1, 2,)`) ends the argument list.
12401            if self.check(&TokenType::RParen) {
12402                break;
12403            }
12404            let arg = self.parse_call_arg()?;
12405            args.push(arg);
12406        }
12407
12408        Ok(args)
12409    }
12410
12411    fn parse_call_arg(&mut self) -> ParseResult<&'a Expr<'a>> {
12412        // Check for Give keyword to mark ownership transfer
12413        if self.check(&TokenType::Give) {
12414            self.advance(); // consume "Give"
12415            let value = self.parse_comparison()?;
12416            return Ok(self.ctx.alloc_imperative_expr(Expr::Give { value }));
12417        }
12418
12419        // Otherwise parse normal expression — use parse_comparison() so that
12420        // "and" remains available as an argument separator in parse_call_arguments()
12421        self.parse_comparison()
12422    }
12423
12424    /// The operand of a page query: a selector string, or a name with its field-access chain.
12425    ///
12426    /// Deliberately NOT the general expression parser. This is reached from
12427    /// `parse_primary_expr_inner`, and calling back into that descent makes the two mutually
12428    /// recursive — stack the parse-recursion budget is measured against, spent on every
12429    /// expression rather than only on page queries. A page query's operand is a selector or a
12430    /// name, so parsing exactly that is both cheaper and truer to the form.
12431    #[inline(never)]
12432    fn parse_page_query_operand(&mut self) -> ParseResult<&'a Expr<'a>> {
12433        use crate::ast::{Expr, Literal};
12434        // `the count of the journal` — the operand is another page question, not a selector or a
12435        // binding. `the count of <collection>` has always meant its length, and the journal is a
12436        // collection; without this the operand parser demands an identifier and the sentence
12437        // fails to parse at all.
12438        if self.check_word("the") && self.peek_word_at(1, "journal") {
12439            self.advance(); // "the"
12440            self.advance(); // "journal"
12441            return Ok(self
12442                .ctx
12443                .alloc_imperative_expr(Expr::Dom(crate::ast::stmt::DomQuery::Journal)));
12444        }
12445        match self.peek().kind {
12446            TokenType::StringLiteral(sym) => {
12447                self.advance();
12448                Ok(self
12449                    .ctx
12450                    .alloc_imperative_expr(Expr::Literal(Literal::Text(sym))))
12451            }
12452            _ => {
12453                let name = self.expect_identifier()?;
12454                let base = self.ctx.alloc_imperative_expr(Expr::Identifier(name));
12455                self.parse_field_access_chain(base)
12456            }
12457        }
12458    }
12459
12460    fn parse_condition(&mut self) -> ParseResult<&'a Expr<'a>> {
12461        // Grand Challenge: Parse compound conditions with "and" and "or"
12462        // "or" has lower precedence than "and"
12463        let expr = self.parse_or_condition()?;
12464        // Postfix unit conversion: `<expr> in <unit>` → `convert(expr, "unit")`, e.g.
12465        // `2 inches + 5 centimeters in feet`. The lowest-precedence postfix, so `in` applies to the
12466        // whole expression. Gated on the token after `in` being a UNIT word, so `for each x in list`
12467        // (loop var via expect_identifier; collection is not a unit) and `chunk at i in zone` are
12468        // untouched — neither places a unit word right after `in`.
12469        if self.check(&TokenType::In) || self.check_preposition_is("in") {
12470            // Zoned time: `<moment> in "America/New_York"` → the local time in that zone. The token
12471            // after `in` is a STRING (a zone name), which distinguishes it from `in <unit>` below.
12472            let zone_sym = match self.tokens.get(self.current + 1).map(|t| &t.kind) {
12473                Some(TokenType::StringLiteral(s)) => Some(*s),
12474                _ => None,
12475            };
12476            if let Some(zone_sym) = zone_sym {
12477                self.advance(); // "in"
12478                self.advance(); // the zone string
12479                let zone = self
12480                    .ctx
12481                    .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Text(zone_sym)));
12482                let func = self.interner.intern("in_zone");
12483                return Ok(self.ctx.alloc_imperative_expr(Expr::Call {
12484                    function: func,
12485                    args: vec![expr, zone],
12486                }));
12487            }
12488            let unit_lexeme = self.tokens.get(self.current + 1).and_then(|t| {
12489                if matches!(
12490                    t.kind,
12491                    TokenType::Identifier | TokenType::Noun(_) | TokenType::Adjective(_)
12492                ) {
12493                    Some(t.lexeme)
12494                } else {
12495                    None
12496                }
12497            });
12498            if let Some(unit_lexeme) = unit_lexeme {
12499                if logicaffeine_base::quantity::units::by_name(self.interner.resolve(unit_lexeme))
12500                    .is_some()
12501                {
12502                    self.advance(); // consume "in"
12503                    self.advance(); // consume the unit word
12504                    let unit_expr = self.ctx.alloc_imperative_expr(crate::ast::Expr::Literal(
12505                        crate::ast::Literal::Text(unit_lexeme),
12506                    ));
12507                    let func = self.interner.intern("convert");
12508                    return Ok(self.ctx.alloc_imperative_expr(crate::ast::Expr::Call {
12509                        function: func,
12510                        args: vec![expr, unit_expr],
12511                    }));
12512                }
12513            }
12514            // Currency conversion: `<money> in EUR` → `to_currency(money, "EUR")` via the ambient
12515            // rate context. Checked AFTER units (a unit word never reads as a currency); gated on a
12516            // real ISO-4217 code, so `for each x in list` and friends are untouched.
12517            let code_lexeme = self.tokens.get(self.current + 1).and_then(|t| {
12518                if matches!(
12519                    t.kind,
12520                    TokenType::Identifier
12521                        | TokenType::Noun(_)
12522                        | TokenType::ProperName(_)
12523                        | TokenType::Adjective(_)
12524                ) {
12525                    Some(t.lexeme)
12526                } else {
12527                    None
12528                }
12529            });
12530            if let Some(code_lexeme) = code_lexeme {
12531                if logicaffeine_base::money::currency::by_code(self.interner.resolve(code_lexeme))
12532                    .is_some()
12533                {
12534                    self.advance(); // consume "in"
12535                    self.advance(); // consume the currency code
12536                    let code_expr = self.ctx.alloc_imperative_expr(crate::ast::Expr::Literal(
12537                        crate::ast::Literal::Text(code_lexeme),
12538                    ));
12539                    let func = self.interner.intern("to_currency");
12540                    return Ok(self.ctx.alloc_imperative_expr(crate::ast::Expr::Call {
12541                        function: func,
12542                        args: vec![expr, code_expr],
12543                    }));
12544                }
12545            }
12546        }
12547        Ok(expr)
12548    }
12549
12550    /// Parse "or" conditions (lower precedence than "and")
12551    fn parse_or_condition(&mut self) -> ParseResult<&'a Expr<'a>> {
12552        let mut left = self.parse_and_condition()?;
12553
12554        while self.check(&TokenType::Or) || self.check_word("or") {
12555            self.advance();
12556            let right = self.parse_and_condition()?;
12557            left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12558                op: BinaryOpKind::Or,
12559                left,
12560                right,
12561            });
12562        }
12563
12564        Ok(left)
12565    }
12566
12567    /// Parse "and" conditions (higher precedence than "or")
12568    fn parse_and_condition(&mut self) -> ParseResult<&'a Expr<'a>> {
12569        let mut left = self.parse_comparison()?;
12570
12571        while self.check(&TokenType::And) || self.check_word("and") {
12572            self.advance();
12573            let right = self.parse_comparison()?;
12574            left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12575                op: BinaryOpKind::And,
12576                left,
12577                right,
12578            });
12579        }
12580
12581        Ok(left)
12582    }
12583
12584    /// Whether the `in` at the current position introduces a postfix unit / currency / zone
12585    /// CONVERSION (handled by [`parse_condition`]) rather than Pythonic membership. Mirrors
12586    /// `parse_condition`'s exact gating: a zone STRING, or a unit / ISO-4217 currency word, right
12587    /// after `in`. Keeps `x in xs` membership working while restoring `<expr> in <unit|currency>`.
12588    fn in_introduces_conversion(&self) -> bool {
12589        match self.tokens.get(self.current + 1) {
12590            Some(t) => match &t.kind {
12591                TokenType::StringLiteral(_) => true,
12592                TokenType::Identifier
12593                | TokenType::Noun(_)
12594                | TokenType::ProperName(_)
12595                | TokenType::Adjective(_) => {
12596                    // A declared variable shadows a unit/currency abbreviation (declarer-wins):
12597                    // `20 in s`, where `s` is a bound set, is membership — NOT "convert 20 to
12598                    // seconds" (`s` = the seconds unit). A real conversion (`… in feet`, `… in USD`)
12599                    // never names a bound variable, so those are unaffected.
12600                    if self.user_bound.contains(&t.lexeme) {
12601                        return false;
12602                    }
12603                    let name = self.interner.resolve(t.lexeme);
12604                    logicaffeine_base::quantity::units::by_name(name).is_some()
12605                        || logicaffeine_base::money::currency::by_code(name).is_some()
12606                }
12607                _ => false,
12608            },
12609            None => false,
12610        }
12611    }
12612
12613    /// Grand Challenge: Parse a single comparison expression
12614    fn parse_comparison(&mut self) -> ParseResult<&'a Expr<'a>> {
12615        // Handle unary "not" operator: "not x" or "not (expr)"
12616        if self.check(&TokenType::Not) || self.check_word("not") {
12617            self.advance(); // consume "not"
12618            let operand = self.parse_comparison()?; // recursive for "not not x"
12619            return Ok(self.ctx.alloc_imperative_expr(Expr::Not { operand }));
12620        }
12621
12622        let left = self.parse_xor_expr()?;
12623
12624        // Key-first membership: `x in xs` → `xs contains x` (Pythonic); `x not in
12625        // xs` → its negation. Reuses `Expr::Contains` (operands reversed). The
12626        // `Repeat for x in xs` header consumes its own `in` before the iterable,
12627        // so it never reaches here.
12628        //
12629        // BUT decline when `in` introduces a POSTFIX CONVERSION — `<expr> in <unit|currency>` or
12630        // `<moment> in "<zone>"` — which `parse_condition` handles at a higher level and must see the
12631        // `in` for. Without this, `10.00 EUR in USD` parses as membership (`USD contains …`) and
12632        // fails with "Undefined variable: USD"; likewise `<moment> in "America/New_York"`.
12633        if self.check(&TokenType::In) && !self.in_introduces_conversion() {
12634            self.advance(); // consume "in"
12635            let collection = self.parse_xor_expr()?;
12636            return Ok(self.ctx.alloc_imperative_expr(Expr::Contains {
12637                collection,
12638                value: left,
12639            }));
12640        }
12641        if (self.check(&TokenType::Not) || self.check_word("not"))
12642            && matches!(
12643                self.tokens.get(self.current + 1).map(|t| &t.kind),
12644                Some(TokenType::In)
12645            )
12646        {
12647            self.advance(); // consume "not"
12648            self.advance(); // consume "in"
12649            let collection = self.parse_xor_expr()?;
12650            let contains = self.ctx.alloc_imperative_expr(Expr::Contains {
12651                collection,
12652                value: left,
12653            });
12654            return Ok(self
12655                .ctx
12656                .alloc_imperative_expr(Expr::Not { operand: contains }));
12657        }
12658
12659        // Check for comparison operators
12660        let op = if self.check(&TokenType::Equals) {
12661            self.advance();
12662            Some(BinaryOpKind::Eq)
12663        } else if self.check(&TokenType::Identity) {
12664            // "is equal to" was tokenized as TokenType::Identity
12665            self.advance();
12666            Some(BinaryOpKind::Eq)
12667        } else if self.check_word("is") {
12668            // Peek ahead to determine which comparison
12669            let saved_pos = self.backtrack_point();
12670            self.advance(); // consume "is"
12671
12672            // Number predicates desugar to a complete modulo test (no right
12673            // operand to combine) — return the whole Bool expression directly:
12674            // `x is even` → `x % 2 == 0`, `x is odd` → `x % 2 == 1`,
12675            // `x is divisible by n` → `x % n == 0`.
12676            if self.check_word("even") || self.check_word("odd") {
12677                let is_odd = self.check_word("odd");
12678                self.advance(); // consume "even"/"odd"
12679                let two = self
12680                    .ctx
12681                    .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Number(2)));
12682                let rem = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12683                    op: BinaryOpKind::Modulo,
12684                    left,
12685                    right: two,
12686                });
12687                let want =
12688                    self.ctx
12689                        .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Number(
12690                            if is_odd { 1 } else { 0 },
12691                        )));
12692                return Ok(self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12693                    op: BinaryOpKind::Eq,
12694                    left: rem,
12695                    right: want,
12696                }));
12697            }
12698            if let Some(predicate) = self.try_parse_nonstandard_predicate(left)? {
12699                return Ok(predicate);
12700            }
12701            // ★ `x is none` / `x is not none` — the plainest way to ask whether an optional
12702            // value is there, and the spelling this project's own design docs use
12703            // (`currentUser(request) is none`). It did not parse AT ALL: the only forms were
12704            // `is equal to none` and an `Inspect` with a `When None:` arm, and what an author got
12705            // for the natural sentence was "This construction needs the word ':' here" — an error
12706            // about punctuation for a phrase that was never a form, which sends the reader
12707            // hunting for a missing colon that is not missing.
12708            //
12709            // A live user binding wins, exactly as it does for the `none` LITERAL: a program that
12710            // wrote `Let none be a new Seq of Int.` means its own variable, and `x is none` there
12711            // is the ordinary equality it has always been.
12712            {
12713                let negated = self.check_word("not");
12714                let none_at = if negated {
12715                    self.current + 1
12716                } else {
12717                    self.current
12718                };
12719                let is_none_word = self.tokens.get(none_at).is_some_and(|t| {
12720                    self.interner.resolve(t.lexeme).eq_ignore_ascii_case("none")
12721                        && !self.user_bound.contains(&t.lexeme)
12722                });
12723                if is_none_word {
12724                    if negated {
12725                        self.advance(); // consume "not"
12726                    }
12727                    self.advance(); // consume "none"
12728                    let none_lit = self.ctx.alloc_imperative_expr(Expr::OptionNone);
12729                    let eq = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12730                        op: BinaryOpKind::Eq,
12731                        left,
12732                        right: none_lit,
12733                    });
12734                    return Ok(if negated {
12735                        self.ctx.alloc_imperative_expr(Expr::Not { operand: eq })
12736                    } else {
12737                        eq
12738                    });
12739                }
12740            }
12741            // ELEMENT-STATE page questions: `"#b" is disabled`, `"#c" is checked`, and the
12742            // visible/hidden/focused trio — and their negation, `"#b" is not disabled` (mirrors
12743            // the `is none`/`is not none` peek just above: look one word past a leading "not"
12744            // before matching, so the SAME list drives both the plain and the negated reading —
12745            // a new adjective added here can never be missing its own negation, since there is
12746            // only ever the one recognition path). They read as adjectives, and `visible`,
12747            // `hidden`, `checked` and `focused` are ordinary English words a program may well
12748            // hold a Bool under — so they fire ONLY on a LITERAL selector, and `mode is visible`
12749            // remains the equality it has always been. That is the whole disambiguation, and it
12750            // costs nothing: a page question is always asked of a selector written out.
12751            if matches!(left, Expr::Literal(crate::ast::Literal::Text(_))) {
12752                let negated = self.check_word("not");
12753                let at = usize::from(negated);
12754                let state = if self.peek_word_at(at, "visible") {
12755                    Some(0)
12756                } else if self.peek_word_at(at, "hidden") {
12757                    Some(1)
12758                } else if self.peek_word_at(at, "disabled") {
12759                    Some(2)
12760                } else if self.peek_word_at(at, "checked") {
12761                    Some(3)
12762                } else if self.peek_word_at(at, "focused") {
12763                    Some(4)
12764                } else {
12765                    None
12766                };
12767                if let Some(state) = state {
12768                    if negated {
12769                        self.advance(); // consume "not"
12770                    }
12771                    self.advance(); // the adjective
12772                                    // The `"<sel>" is …` questions take a selector too — see
12773                                    // `check_selector_literal`. A check that landed on `the count of` alone would
12774                                    // be the arbitrary-list failure this codebase keeps finding.
12775                    self.check_selector_literal(left)?;
12776                    use crate::ast::stmt::DomQuery;
12777                    let query = match state {
12778                        0 => DomQuery::Visible { selector: left },
12779                        1 => DomQuery::Hidden { selector: left },
12780                        2 => DomQuery::Disabled { selector: left },
12781                        3 => DomQuery::Checked { selector: left },
12782                        _ => DomQuery::Focused { selector: left },
12783                    };
12784                    let dom_expr = self.ctx.alloc_imperative_expr(Expr::Dom(query));
12785                    return Ok(if negated {
12786                        self.ctx
12787                            .alloc_imperative_expr(Expr::Not { operand: dom_expr })
12788                    } else {
12789                        dom_expr
12790                    });
12791                }
12792            }
12793            if self.check_word("divisible") {
12794                self.advance(); // consume "divisible"
12795                if self.check_word("by") || self.check_preposition_is("by") {
12796                    self.advance(); // consume "by"
12797                    let divisor = self.parse_xor_expr()?;
12798                    let rem = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12799                        op: BinaryOpKind::Modulo,
12800                        left,
12801                        right: divisor,
12802                    });
12803                    let zero = self
12804                        .ctx
12805                        .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Number(0)));
12806                    return Ok(self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12807                        op: BinaryOpKind::Eq,
12808                        left: rem,
12809                        right: zero,
12810                    }));
12811                }
12812                self.backtrack_to(&saved_pos);
12813                return Ok(left);
12814            }
12815            // `x is between lo and hi` → `lo <= x and x <= hi` (inclusive).
12816            if self.check_word("between") {
12817                self.advance(); // consume "between"
12818                let lo = self.parse_xor_expr()?;
12819                if self.check_word("and") || self.check(&TokenType::And) {
12820                    self.advance(); // consume "and"
12821                    let hi = self.parse_xor_expr()?;
12822                    let lower = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12823                        op: BinaryOpKind::LtEq,
12824                        left: lo,
12825                        right: left,
12826                    });
12827                    let upper = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12828                        op: BinaryOpKind::LtEq,
12829                        left,
12830                        right: hi,
12831                    });
12832                    return Ok(self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12833                        op: BinaryOpKind::And,
12834                        left: lower,
12835                        right: upper,
12836                    }));
12837                }
12838                self.backtrack_to(&saved_pos);
12839                return Ok(left);
12840            }
12841
12842            if self.check_word("greater") {
12843                self.advance(); // consume "greater"
12844                if self.check_word("than") || self.check_preposition_is("than") {
12845                    self.advance(); // consume "than"
12846                                    // "greater than or equal to X" → GtEq (the inclusive comparison, ≥); a bare
12847                                    // "greater than X" stays the strict Gt. The `or equal to` tail is optional.
12848                    if self.consume_or_equal_to() {
12849                        Some(BinaryOpKind::GtEq)
12850                    } else {
12851                        Some(BinaryOpKind::Gt)
12852                    }
12853                } else {
12854                    self.backtrack_to(&saved_pos);
12855                    None
12856                }
12857            } else if self.check_word("less") {
12858                self.advance(); // consume "less"
12859                if self.check_word("than") || self.check_preposition_is("than") {
12860                    self.advance(); // consume "than"
12861                                    // "less than or equal to X" → LtEq (≤); a bare "less than X" stays the strict Lt.
12862                    if self.consume_or_equal_to() {
12863                        Some(BinaryOpKind::LtEq)
12864                    } else {
12865                        Some(BinaryOpKind::Lt)
12866                    }
12867                } else {
12868                    self.backtrack_to(&saved_pos);
12869                    None
12870                }
12871            } else if self.check_word("at") {
12872                self.advance(); // consume "at"
12873                if self.check_word("least") {
12874                    self.advance(); // consume "least"
12875                    Some(BinaryOpKind::GtEq)
12876                } else if self.check_word("most") {
12877                    self.advance(); // consume "most"
12878                    Some(BinaryOpKind::LtEq)
12879                } else {
12880                    self.backtrack_to(&saved_pos);
12881                    None
12882                }
12883            } else if self.check_word("before") {
12884                // "is before X" → temporal `<` (a Moment/Date precedes another); generic `<`.
12885                self.advance(); // consume "before"
12886                Some(BinaryOpKind::Lt)
12887            } else if self.check_word("after") {
12888                // "is after X" → temporal `>` (a Moment/Date follows another); generic `>`.
12889                self.advance(); // consume "after"
12890                Some(BinaryOpKind::Gt)
12891            } else if self.check_word("not") || self.check(&TokenType::Not) {
12892                // "is not X" → NotEq
12893                self.advance(); // consume "not"
12894                                // ★ AND `is not equal to X`, the plain negation of `is equal to X`. It did not
12895                                // parse, and what an author got instead was "this construction needs the word
12896                                // ':'" — an error about punctuation, for the most natural way there is to write
12897                                // the opposite of a form the language already has. Whoever wrote it goes looking
12898                                // for a missing colon that was never missing.
12899                if self.check_word("equal") {
12900                    let before_equal = self.backtrack_point();
12901                    self.advance(); // consume "equal"
12902                    if self.check_preposition_is("to") || self.check_word("to") {
12903                        self.advance(); // consume "to"
12904                    } else {
12905                        // `is not equal` on its own is not a comparison — leave the word alone.
12906                        self.backtrack_to(&before_equal);
12907                    }
12908                }
12909                Some(BinaryOpKind::NotEq)
12910            } else if self.check_word("equal") {
12911                // "is equal to X" → Eq
12912                self.advance(); // consume "equal"
12913                if self.check_preposition_is("to") {
12914                    self.advance(); // consume "to"
12915                    Some(BinaryOpKind::Eq)
12916                } else {
12917                    self.backtrack_to(&saved_pos);
12918                    None
12919                }
12920            } else if self.check_word("approximately") {
12921                // "is approximately X" → the tolerant float comparison
12922                // (`==` stays IEEE bit-exact). An optional "equal to" tail
12923                // also reads naturally: "is approximately equal to X".
12924                self.advance(); // consume "approximately"
12925                if self.check_word("equal") {
12926                    self.advance(); // consume "equal"
12927                    if self.check_preposition_is("to") {
12928                        self.advance(); // consume "to"
12929                    }
12930                }
12931                // `is approximately X within eps` → (X - eps) <= v AND v <= (X + eps)
12932                // — the explicit-tolerance form (the float-equality footgun's
12933                // grammar-level answer; the tolerance is never a hidden global).
12934                let target_probe = self.backtrack_point();
12935                // The probe REWINDS on failure (the shared right-operand
12936                // parse accepts forms this level does not — the probe must
12937                // never knock an `is approximately X` program out).
12938                let target = match self.parse_xor_expr() {
12939                    Ok(t) => Some(t),
12940                    Err(_) => {
12941                        self.backtrack_to(&target_probe);
12942                        None
12943                    }
12944                };
12945                if let Some(target) = target {
12946                    if self.check_word("within") {
12947                        self.advance(); // consume "within"
12948                        let eps = self.parse_xor_expr()?;
12949                        let lo = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12950                            op: BinaryOpKind::Subtract,
12951                            left: target,
12952                            right: eps,
12953                        });
12954                        let hi = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12955                            op: BinaryOpKind::Add,
12956                            left: target,
12957                            right: eps,
12958                        });
12959                        let lower = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12960                            op: BinaryOpKind::LtEq,
12961                            left: lo,
12962                            right: left,
12963                        });
12964                        let upper = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12965                            op: BinaryOpKind::LtEq,
12966                            left,
12967                            right: hi,
12968                        });
12969                        return Ok(self.ctx.alloc_imperative_expr(Expr::BinaryOp {
12970                            op: BinaryOpKind::And,
12971                            left: lower,
12972                            right: upper,
12973                        }));
12974                    }
12975                    // No `within`: rewind and let the shared right-operand
12976                    // parse build the plain tolerant comparison.
12977                    self.backtrack_to(&target_probe);
12978                }
12979                Some(BinaryOpKind::ApproxEq)
12980            } else if matches!(self.peek().kind, TokenType::Number(_))
12981                || (self.check(&TokenType::Minus)
12982                    && self
12983                        .tokens
12984                        .get(self.current + 1)
12985                        .is_some_and(|t| matches!(t.kind, TokenType::Number(_))))
12986            {
12987                // Bare `x is <number>` is equality (`x is equal to <number>` → `x == <number>`).
12988                // Guarded to a NUMBER literal (including a negated one), so `is a`/`is an`/`is the`/
12989                // `is nothing`/`is <predicate>` and every worded `is …` comparison above stay
12990                // untouched — nothing but a numeral reaches here. The numeral is left for the
12991                // right-operand parse below.
12992                Some(BinaryOpKind::Eq)
12993            } else if matches!(self.peek().kind, TokenType::StringLiteral(_)) {
12994                // Bare `x is "text"` is equality, exactly like `x is 5`: a text literal is never a
12995                // predicate adjective, so there is nothing here to disambiguate.
12996                Some(BinaryOpKind::Eq)
12997            } else if matches!(
12998                self.peek().kind,
12999                TokenType::Identifier
13000                    | TokenType::Noun(_)
13001                    | TokenType::Adjective(_)
13002                    | TokenType::ProperName(_)
13003                    | TokenType::Verb { .. }
13004                    | TokenType::Particle(_)
13005                    | TokenType::Ambiguous { .. }
13006                    | TokenType::Pronoun { .. }
13007            ) && (self.user_bound.contains(&self.peek().lexeme)
13008                || self
13009                    .type_registry
13010                    .as_ref()
13011                    .is_some_and(|r| r.is_variant(self.peek().lexeme)))
13012            {
13013                // Declarer-wins: bare `x is <bound name>` is equality with that variable (`a plus b
13014                // is total`, where `total` is a case-table column or Let-bound name, whatever word
13015                // class the lexer gave it — a binding named `limited` or `closed` is a participle to
13016                // the lexer and a value to the program); the same for a declared enum VARIANT
13017                // (`c is Red`) — neither is ever a predicate adjective, so nothing is lost by taking
13018                // them as values. Articles are deliberately excluded so
13019                // `is a <Type>` keeps its type-check reading; unbound, non-variant words keep their
13020                // predicate reading (`x is red`).
13021                Some(BinaryOpKind::Eq)
13022            } else {
13023                self.backtrack_to(&saved_pos);
13024                None
13025            }
13026        } else if self.check(&TokenType::Lt) {
13027            self.advance();
13028            Some(BinaryOpKind::Lt)
13029        } else if self.check(&TokenType::Gt) {
13030            self.advance();
13031            Some(BinaryOpKind::Gt)
13032        } else if self.check(&TokenType::LtEq) {
13033            self.advance();
13034            Some(BinaryOpKind::LtEq)
13035        } else if self.check(&TokenType::GtEq) {
13036            self.advance();
13037            Some(BinaryOpKind::GtEq)
13038        } else if self.check(&TokenType::EqEq) || self.check(&TokenType::Assign) {
13039            self.advance();
13040            Some(BinaryOpKind::Eq)
13041        } else if self.check(&TokenType::NotEq) {
13042            self.advance();
13043            Some(BinaryOpKind::NotEq)
13044        } else {
13045            None
13046        };
13047
13048        if let Some(op) = op {
13049            let right = self.parse_xor_expr()?;
13050            let first = self
13051                .ctx
13052                .alloc_imperative_expr(Expr::BinaryOp { op, left, right });
13053            // Chained comparison: `lo <= x <= hi` → `(lo <= x) and (x <= hi)`
13054            // (math / Python reading). Only the SYMBOLIC operators chain; the
13055            // worded forms don't. `right` is the shared middle operand.
13056            if let Some(op2) = self.try_symbolic_comparison_op() {
13057                let hi = self.parse_xor_expr()?;
13058                let second = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
13059                    op: op2,
13060                    left: right,
13061                    right: hi,
13062                });
13063                return Ok(self.ctx.alloc_imperative_expr(Expr::BinaryOp {
13064                    op: BinaryOpKind::And,
13065                    left: first,
13066                    right: second,
13067                }));
13068            }
13069            Ok(first)
13070        } else {
13071            Ok(left)
13072        }
13073    }
13074
13075    /// Consume a SYMBOLIC comparison operator (`< <= > >= == !=`) if one is
13076    /// next, for chained comparisons. Worded forms (`is greater than`) are not
13077    /// chained. Returns `None` (consuming nothing) otherwise.
13078    fn try_symbolic_comparison_op(&mut self) -> Option<BinaryOpKind> {
13079        let op = if self.check(&TokenType::LtEq) {
13080            BinaryOpKind::LtEq
13081        } else if self.check(&TokenType::GtEq) {
13082            BinaryOpKind::GtEq
13083        } else if self.check(&TokenType::Lt) {
13084            BinaryOpKind::Lt
13085        } else if self.check(&TokenType::Gt) {
13086            BinaryOpKind::Gt
13087        } else if self.check(&TokenType::EqEq) {
13088            BinaryOpKind::Eq
13089        } else if self.check(&TokenType::NotEq) {
13090            BinaryOpKind::NotEq
13091        } else {
13092            return None;
13093        };
13094        self.advance();
13095        Some(op)
13096    }
13097
13098    /// `Let (a, b, …) be <expr>` — positional destructuring. Binds a fresh temporary to `<expr>`, then each
13099    /// name to `item <i+1> of <temp>` (1-based, reusing tuple index access). Emitted as a `Stmt::Splice`, so
13100    /// no `Stmt::Let` AST change and it works on every tier that already indexes tuples.
13101    fn parse_destructuring_let(&mut self, mutable: bool) -> ParseResult<Stmt<'a>> {
13102        self.advance(); // consume "("
13103        let mut binders = Vec::new();
13104        loop {
13105            binders.push(self.expect_identifier()?);
13106            if self.check(&TokenType::Comma) {
13107                self.advance(); // consume ","
13108                continue;
13109            }
13110            break;
13111        }
13112        if !self.check(&TokenType::RParen) {
13113            return Err(ParseError {
13114                kind: ParseErrorKind::ExpectedKeyword {
13115                    keyword: ")".to_string(),
13116                },
13117                span: self.current_span(),
13118            });
13119        }
13120        self.advance(); // consume ")"
13121        if !self.check(&TokenType::Be) && !self.check(&TokenType::Assign) {
13122            return Err(ParseError {
13123                kind: ParseErrorKind::ExpectedKeyword {
13124                    keyword: "be or =".to_string(),
13125                },
13126                span: self.current_span(),
13127            });
13128        }
13129        self.advance(); // consume "be" / "="
13130        let value = self.parse_imperative_expr()?;
13131
13132        let tmp = self.fresh_desugar_temp("destructure");
13133        let tmp_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(tmp));
13134        let mut body: Vec<Stmt<'a>> = Vec::with_capacity(binders.len() + 1);
13135        body.push(Stmt::Let {
13136            var: tmp,
13137            ty: None,
13138            value,
13139            mutable: false,
13140        });
13141        for (i, binder) in binders.iter().enumerate() {
13142            let idx = self
13143                .ctx
13144                .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Number((i + 1) as i64)));
13145            let read = self.ctx.alloc_imperative_expr(Expr::Index {
13146                collection: tmp_ref,
13147                index: idx,
13148            });
13149            body.push(Stmt::Let {
13150                var: *binder,
13151                ty: None,
13152                value: read,
13153                mutable,
13154            });
13155        }
13156        let body = self.alloc_block(body);
13157        Ok(Stmt::Splice { body })
13158    }
13159
13160    fn parse_let_statement(&mut self) -> ParseResult<Stmt<'a>> {
13161        self.advance(); // consume "Let"
13162
13163        // Check for "mutable" keyword
13164        let mutable = if self.check_mutable_keyword() {
13165            self.advance();
13166            true
13167        } else {
13168            false
13169        };
13170
13171        // Destructuring bind: `Let (a, b) be <expr>` binds each name positionally. Desugars to a fresh
13172        // temporary plus one `Let <name> be item <i+1> of <temp>` per binder — so it reuses tuple index
13173        // access on every tier with no `Stmt::Let` AST change.
13174        if self.check(&TokenType::LParen) {
13175            return self.parse_destructuring_let(mutable);
13176        }
13177
13178        // Get identifier
13179        let var = self.expect_identifier()?;
13180
13181        // Check for optional type annotation: `: Type`
13182        let ty = if self.check(&TokenType::Colon) {
13183            self.advance(); // consume ":"
13184            let type_expr = self.parse_type_expression()?;
13185            Some(self.ctx.alloc_type_expr(type_expr))
13186        } else {
13187            None
13188        };
13189
13190        // Expect "be" or "="
13191        if !self.check(&TokenType::Be) && !self.check(&TokenType::Assign) {
13192            return Err(ParseError {
13193                kind: ParseErrorKind::ExpectedKeyword {
13194                    keyword: "be or =".to_string(),
13195                },
13196                span: self.current_span(),
13197            });
13198        }
13199        self.advance(); // consume "be" or "="
13200
13201        // Tensor sugar (Phase 2): `Let A be a R by C tensor of Int` (a shaped, flat-backed tensor) and
13202        // `Let C be A matmul B` (the shape-checked product). Both desugar to ordinary Seq + loop statements
13203        // (a transparent `Stmt::Splice`) so every tier runs them ≡ the VM; the matmul is the exact idiom
13204        // `recognize_matmul` lifts to the optimized backends. Guarded + backtracking → zero effect otherwise.
13205        if let Some(stmt) = self.try_parse_tensor_new(var)? {
13206            return Ok(stmt);
13207        }
13208        if let Some(stmt) = self.try_parse_tensor_int_map(var)? {
13209            return Ok(stmt);
13210        }
13211        if let Some(stmt) = self.try_parse_tensor_binop(var)? {
13212            return Ok(stmt);
13213        }
13214        if let Some(stmt) = self.try_parse_tensor_head_slice(var)? {
13215            return Ok(stmt);
13216        }
13217        if let Some(stmt) = self.try_parse_tensor_unary(var)? {
13218            return Ok(stmt);
13219        }
13220        // `Let <name> be every <x> in <list> where <condition>.` — the items the condition holds
13221        // for, in order, as a new list; `<list>` is untouched.
13222        if let Some((binder, list, condition)) = self.try_parse_every_where()? {
13223            let (kept, mut body) = self.desugar_keep_where(binder, list, condition, true);
13224            let value = self.ctx.alloc_imperative_expr(Expr::Identifier(kept));
13225            body.push(Stmt::Let {
13226                var,
13227                ty,
13228                value,
13229                mutable,
13230            });
13231            let body = self.alloc_block(body);
13232            return Ok(Stmt::Splice { body });
13233        }
13234
13235        // Phase 53: Check for "mounted at [path]" pattern (for Persistent types)
13236        if self.check_word("mounted") {
13237            self.advance(); // consume "mounted"
13238            if !self.check(&TokenType::At) && !self.check_preposition_is("at") {
13239                return Err(ParseError {
13240                    kind: ParseErrorKind::ExpectedKeyword {
13241                        keyword: "at".to_string(),
13242                    },
13243                    span: self.current_span(),
13244                });
13245            }
13246            self.advance(); // consume "at"
13247            let path = self.parse_imperative_expr()?;
13248            return Ok(Stmt::Mount { var, path });
13249        }
13250
13251        // Phase 51: Check for "a PeerAgent at [addr]" pattern
13252        if self.check_article() {
13253            let saved_pos = self.backtrack_point();
13254            self.advance(); // consume article
13255
13256            // Check if next word is "PeerAgent" (case insensitive)
13257            if let TokenType::Noun(sym) | TokenType::ProperName(sym) = self.peek().kind {
13258                let word = self.interner.resolve(sym).to_lowercase();
13259                if word == "peeragent" {
13260                    self.advance(); // consume "PeerAgent"
13261
13262                    // Check for "at" keyword
13263                    if self.check(&TokenType::At) || self.check_preposition_is("at") {
13264                        self.advance(); // consume "at"
13265
13266                        // Parse address expression
13267                        let address = self.parse_imperative_expr()?;
13268
13269                        return Ok(Stmt::LetPeerAgent { var, address });
13270                    }
13271                }
13272            }
13273            // Not a PeerAgent, backtrack
13274            self.backtrack_to(&saved_pos);
13275        }
13276
13277        // Phase 54: Check for "a [new] Pipe of Type" pattern. The optional `new`
13278        // matches the `a new X` idiom every other collection construction uses
13279        // (`a new Map`, `a new Set`); the guide writes `a new Pipe of Int`.
13280        if self.check_article() {
13281            let saved_pos = self.backtrack_point();
13282            self.advance(); // consume article
13283            if self.check(&TokenType::New) {
13284                self.advance(); // consume optional "new"
13285            }
13286
13287            if self.check(&TokenType::Pipe) {
13288                self.advance(); // consume "Pipe"
13289
13290                // Expect "of"
13291                if !self.check_word("of") {
13292                    return Err(ParseError {
13293                        kind: ParseErrorKind::ExpectedKeyword {
13294                            keyword: "of".to_string(),
13295                        },
13296                        span: self.current_span(),
13297                    });
13298                }
13299                self.advance(); // consume "of"
13300
13301                // Parse element type
13302                let element_type = self.expect_identifier()?;
13303
13304                // Variable registration now handled by DRS
13305
13306                return Ok(Stmt::CreatePipe {
13307                    var,
13308                    element_type,
13309                    capacity: None,
13310                });
13311            }
13312            // Not a Pipe, backtrack
13313            self.backtrack_to(&saved_pos);
13314        }
13315
13316        // Phase 54: Check for "Launch a task to..." pattern (for task handles).
13317        // The article lookahead keeps a VARIABLE named `launch` usable as a Let
13318        // value (`Let x be launch + 1.`) — only `launch a/an …` is the statement.
13319        if self.check(&TokenType::Launch)
13320            && matches!(
13321                self.tokens.get(self.current + 1).map(|t| &t.kind),
13322                Some(TokenType::Article(_))
13323            )
13324        {
13325            self.advance(); // consume "Launch"
13326
13327            // Expect "a"
13328            if !self.check_article() {
13329                return Err(ParseError {
13330                    kind: ParseErrorKind::ExpectedKeyword {
13331                        keyword: "a".to_string(),
13332                    },
13333                    span: self.current_span(),
13334                });
13335            }
13336            self.advance();
13337
13338            // Expect "task"
13339            if !self.check(&TokenType::Task) {
13340                return Err(ParseError {
13341                    kind: ParseErrorKind::ExpectedKeyword {
13342                        keyword: "task".to_string(),
13343                    },
13344                    span: self.current_span(),
13345                });
13346            }
13347            self.advance();
13348
13349            // Expect "to"
13350            if !self.check(&TokenType::To) && !self.check_word("to") {
13351                return Err(ParseError {
13352                    kind: ParseErrorKind::ExpectedKeyword {
13353                        keyword: "to".to_string(),
13354                    },
13355                    span: self.current_span(),
13356                });
13357            }
13358            self.advance();
13359
13360            let function = self.parse_task_function()?;
13361
13362            // Parse optional arguments: "with arg1, arg2"
13363            let args = if self.check_word("with") {
13364                self.advance();
13365                self.parse_call_arguments()?
13366            } else {
13367                vec![]
13368            };
13369
13370            return Ok(Stmt::LaunchTaskWithHandle {
13371                handle: var,
13372                function,
13373                args,
13374            });
13375        }
13376
13377        let value_tok = self.current;
13378        let value = match self.try_parse_infinitesimal(var)? {
13379            Some(value) => value,
13380            None => self.parse_imperative_expr()?,
13381        };
13382
13383        // A declared type must agree with the value beside it — and, for a Real slot given a
13384        // whole number, the declaration decides what is stored.
13385        let value = match &ty {
13386            Some(declared_ty) => {
13387                self.check_declared_type(declared_ty, value, self.span_since(value_tok))?;
13388                self.widen_literal_to_declared(declared_ty, value)
13389            }
13390            None => value,
13391        };
13392
13393        // Check for optional "with capacity <expr>" — wraps value in Expr::WithCapacity
13394        let value = if self.check_word("with") {
13395            let saved = self.backtrack_point();
13396            self.advance(); // consume "with"
13397            if self.check_word("capacity") {
13398                self.advance(); // consume "capacity"
13399                let cap_expr = self.parse_imperative_expr()?;
13400                self.ctx.alloc_imperative_expr(Expr::WithCapacity {
13401                    value,
13402                    capacity: cap_expr,
13403                })
13404            } else {
13405                self.backtrack_to(&saved); // backtrack — "with" belongs to something else
13406                value
13407            }
13408        } else {
13409            value
13410        };
13411
13412        // Register variable in WorldState's DRS with Owned state for ownership tracking
13413        self.world_state.drs.introduce_referent(
13414            var,
13415            var,
13416            crate::drs::Gender::Unknown,
13417            crate::drs::Number::Singular,
13418        );
13419
13420        self.user_bound.insert(var);
13421        Ok(Stmt::Let {
13422            var,
13423            ty,
13424            value,
13425            mutable,
13426        })
13427    }
13428
13429    fn check_mutable_keyword(&self) -> bool {
13430        // Check for TokenType::Mut (Phase 23b keyword)
13431        if matches!(self.peek().kind, TokenType::Mut) {
13432            return true;
13433        }
13434        // Check for "mutable" or "mut" as Noun/Adjective (backward compatibility)
13435        if let TokenType::Noun(sym) | TokenType::Adjective(sym) = self.peek().kind {
13436            let word = self.interner.resolve(sym).to_lowercase();
13437            word == "mutable" || word == "mut"
13438        } else {
13439            false
13440        }
13441    }
13442
13443    /// Phase 43B: Infer the type of a literal expression
13444    /// A declared type must agree with the value beside it — the ONE check, shared by every place
13445    /// a declaration meets an initializer.
13446    ///
13447    /// `Let` had it inline, which meant an LGX `State:` field did not: `Let rate: Real be 3.` was
13448    /// refused at the top level and SILENTLY ACCEPTED inside a component, where the annotation was
13449    /// then ignored and the field behaved as an Int. A declared type that is quietly disregarded
13450    /// is worse than no annotation at all, because the author has been told their intent was
13451    /// understood. Sharing the check is also what keeps ONE message: a second copy would drift.
13452    /// A `Float`/`Real` slot holds a whole-number initializer AS A REAL.
13453    ///
13454    /// Accepting `Let r: Float be 1.` and then storing an Int is the annotation being ignored in
13455    /// a quieter way: the slot parses, and `r / 2` is 0 instead of 0.5 because the division is
13456    /// integer division. The declaration says what the slot HOLDS, so it is the declaration that
13457    /// decides — the literal is widened here, once, where both the type and the value are in hand.
13458    pub(super) fn widen_literal_to_declared(
13459        &self,
13460        declared: &TypeExpr<'_>,
13461        value: &'a Expr<'a>,
13462    ) -> &'a Expr<'a> {
13463        let (TypeExpr::Primitive(sym) | TypeExpr::Named(sym)) = declared else {
13464            return value;
13465        };
13466        let name = self.interner.resolve(*sym);
13467        if !name.eq_ignore_ascii_case("Float") && !name.eq_ignore_ascii_case("Real") {
13468            return value;
13469        }
13470        match value {
13471            Expr::Literal(Literal::Number(n)) => self
13472                .ctx
13473                .alloc_imperative_expr(Expr::Literal(Literal::Float(*n as f64))),
13474            _ => value,
13475        }
13476    }
13477
13478    /// A declared type agrees with the literal value beside it; a mismatch
13479    /// points at `value_span`, the value that disagrees.
13480    pub(super) fn check_declared_type(
13481        &self,
13482        declared: &TypeExpr<'_>,
13483        value: &Expr<'_>,
13484        value_span: Span,
13485    ) -> ParseResult<()> {
13486        let Some(inferred) = self.infer_literal_type(value) else {
13487            return Ok(());
13488        };
13489        if self.check_type_compatibility(declared, inferred) {
13490            return Ok(());
13491        }
13492        let expected = match declared {
13493            TypeExpr::Primitive(sym) | TypeExpr::Named(sym) => {
13494                self.interner.resolve(*sym).to_string()
13495            }
13496            _ => "unknown".to_string(),
13497        };
13498        Err(ParseError {
13499            kind: ParseErrorKind::TypeMismatch {
13500                expected,
13501                found: inferred.to_string(),
13502            },
13503            span: value_span,
13504        })
13505    }
13506
13507    fn infer_literal_type(&self, expr: &Expr<'_>) -> Option<&'static str> {
13508        match expr {
13509            Expr::Literal(lit) => match lit {
13510                crate::ast::Literal::Number(_) => Some("Int"),
13511                crate::ast::Literal::Float(_) => Some("Real"),
13512                crate::ast::Literal::Text(_) => Some("Text"),
13513                crate::ast::Literal::Boolean(_) => Some("Bool"),
13514                crate::ast::Literal::Nothing => Some("Unit"),
13515                crate::ast::Literal::Char(_) => Some("Char"),
13516                crate::ast::Literal::Duration(_) => Some("Duration"),
13517                crate::ast::Literal::Date(_) => Some("Date"),
13518                crate::ast::Literal::Moment(_) => Some("Moment"),
13519                crate::ast::Literal::Span { .. } => Some("Span"),
13520                crate::ast::Literal::Time(_) => Some("Time"),
13521            },
13522            _ => None, // Can't infer type for non-literals yet
13523        }
13524    }
13525
13526    /// Phase 43B: Check if declared type matches inferred type
13527    fn check_type_compatibility(&self, declared: &TypeExpr<'_>, inferred: &str) -> bool {
13528        match declared {
13529            TypeExpr::Primitive(sym) | TypeExpr::Named(sym) => {
13530                let declared_name = self.interner.resolve(*sym);
13531                // Nat and Byte are compatible with Int literals; an integer literal is
13532                // also a valid Rational (`Let x: Rational be 5` → exact 5/1). `Float`
13533                // is the same type as `Real` — a decimal literal satisfies both names.
13534                declared_name.eq_ignore_ascii_case(inferred)
13535                    || (declared_name.eq_ignore_ascii_case("Nat") && inferred == "Int")
13536                    || (declared_name.eq_ignore_ascii_case("Byte") && inferred == "Int")
13537                    || (declared_name.eq_ignore_ascii_case("Rational") && inferred == "Int")
13538                    || (declared_name.eq_ignore_ascii_case("Float") && inferred == "Real")
13539                    // An integer literal is a perfectly good Real: `Let r: Float be 1.` is the
13540                    // canonical explicit annotation, and `Let ratio: Float be 1.` is written that
13541                    // way in the fixtures. It is the SAME widening already allowed for Rational
13542                    // (`Let x: Rational be 5` → exact 5/1) — the annotation says which type the
13543                    // slot holds, and a whole number is a value that type has.
13544                    || (declared_name.eq_ignore_ascii_case("Float") && inferred == "Int")
13545                    || (declared_name.eq_ignore_ascii_case("Real") && inferred == "Int")
13546                    // The exact number types take a whole number as the exact value it is, as
13547                    // `Rational` does; a decimal literal is refused there too, since `0.1` written
13548                    // into an exact slot would have to mean either the double or the tenth.
13549                    || (inferred == "Int"
13550                        && ["Algebraic", "ExactReal", "Hyperreal", "Hypernatural", "Ordinal"]
13551                            .iter()
13552                            .any(|t| declared_name.eq_ignore_ascii_case(t)))
13553            }
13554            _ => true, // For generics/functions, skip check for now
13555        }
13556    }
13557
13558    // =========================================================================
13559    // Phase 23b: Equals-style Assignment (x = 5)
13560    // =========================================================================
13561
13562    /// Check if current token starts an equals-style assignment.
13563    /// Patterns: `identifier = value` or `identifier: Type = value`
13564    /// True when the statement is a compound assignment — it starts with an
13565    /// l-value root (identifier-like) and has a `+= -= *= /= %=` token before
13566    /// the statement terminator. The compound ops appear nowhere else, so their
13567    /// presence is unambiguous.
13568    fn peek_compound_assignment(&self) -> bool {
13569        let is_identifier = matches!(
13570            self.peek().kind,
13571            TokenType::Noun(_)
13572                | TokenType::ProperName(_)
13573                | TokenType::Identifier
13574                | TokenType::Adjective(_)
13575                | TokenType::Verb { .. }
13576                | TokenType::Particle(_)
13577                | TokenType::Ambiguous { .. }
13578                | TokenType::Pronoun { .. }
13579        );
13580        if !is_identifier {
13581            return false;
13582        }
13583        let mut offset = 1;
13584        while self.current + offset < self.tokens.len() {
13585            match &self.tokens[self.current + offset].kind {
13586                TokenType::PlusEq
13587                | TokenType::MinusEq
13588                | TokenType::StarEq
13589                | TokenType::SlashEq
13590                | TokenType::PercentEq => return true,
13591                TokenType::Period | TokenType::Newline | TokenType::EOF => return false,
13592                _ => offset += 1,
13593            }
13594        }
13595        false
13596    }
13597
13598    /// Parse `<lvalue> <op>= <expr>` → `Set <lvalue> to <lvalue> <op> <expr>`.
13599    /// The l-value (identifier / `p.field` / `xs[i]`) is parsed as an
13600    /// expression — it stops cleanly at the compound-op token.
13601    fn parse_compound_assignment(&mut self) -> ParseResult<Stmt<'a>> {
13602        use crate::ast::Expr;
13603        let target_from = self.current;
13604        let target = self.parse_imperative_expr()?;
13605        self.note_write(
13606            target,
13607            target_from,
13608            self.current,
13609            component::WriteVerb::Compound,
13610        );
13611        let op = match self.peek().kind {
13612            TokenType::PlusEq => BinaryOpKind::Add,
13613            TokenType::MinusEq => BinaryOpKind::Subtract,
13614            TokenType::StarEq => BinaryOpKind::Multiply,
13615            TokenType::SlashEq => BinaryOpKind::Divide,
13616            TokenType::PercentEq => BinaryOpKind::Modulo,
13617            _ => {
13618                return Err(ParseError {
13619                    kind: ParseErrorKind::ExpectedKeyword {
13620                        keyword: "+= -= *= /= %=".to_string(),
13621                    },
13622                    span: self.current_span(),
13623                });
13624            }
13625        };
13626        self.advance(); // consume the compound op
13627        let rhs = self.parse_imperative_expr()?;
13628        let combined = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
13629            op,
13630            left: target,
13631            right: rhs,
13632        });
13633        // Desugar by target shape (mirrors `Set`): identifier → Set (auto-mutable
13634        // via collect_mutable_vars), field → SetField, index → the place desugar.
13635        match target {
13636            Expr::Identifier(sym) => Ok(Stmt::Set {
13637                target: *sym,
13638                value: combined,
13639            }),
13640            Expr::FieldAccess { object, field } => Ok(Stmt::SetField {
13641                object,
13642                field: *field,
13643                value: combined,
13644            }),
13645            Expr::Index { collection, index } => {
13646                Ok(self.desugar_place_set_index(collection, index, combined))
13647            }
13648            _ => Err(ParseError {
13649                kind: ParseErrorKind::ExpectedIdentifier,
13650                span: self.current_span(),
13651            }),
13652        }
13653    }
13654
13655    fn peek_equals_assignment(&self) -> bool {
13656        // Must start with an identifier-like token
13657        // Note: Unknown words default to Adjective in the lexer
13658        // Verbs, Particles, and Ambiguous can also be variable names
13659        let is_identifier = matches!(
13660            self.peek().kind,
13661            TokenType::Noun(_)
13662                | TokenType::ProperName(_)
13663                | TokenType::Identifier
13664                | TokenType::Adjective(_)
13665                | TokenType::Verb { .. }
13666                | TokenType::Particle(_)
13667                | TokenType::Ambiguous { .. }
13668                | TokenType::Pronoun { .. }
13669        );
13670        if !is_identifier {
13671            return false;
13672        }
13673
13674        // Check what follows the identifier
13675        if self.current + 1 >= self.tokens.len() {
13676            return false;
13677        }
13678
13679        let next = &self.tokens[self.current + 1].kind;
13680
13681        // Direct assignment: identifier = value
13682        if matches!(next, TokenType::Assign) {
13683            return true;
13684        }
13685
13686        // Type-annotated assignment: identifier: Type = value
13687        // Check for colon, then scan for = before Period/Newline
13688        if matches!(next, TokenType::Colon) {
13689            let mut offset = 2;
13690            while self.current + offset < self.tokens.len() {
13691                let tok = &self.tokens[self.current + offset].kind;
13692                if matches!(tok, TokenType::Assign) {
13693                    return true;
13694                }
13695                if matches!(tok, TokenType::Period | TokenType::Newline | TokenType::EOF) {
13696                    return false;
13697                }
13698                offset += 1;
13699            }
13700        }
13701
13702        false
13703    }
13704
13705    /// Parse equals-style assignment: `x = 5` or `x: Int = 5` or `mut x = 5`
13706    fn parse_equals_assignment(&mut self, explicit_mutable: bool) -> ParseResult<Stmt<'a>> {
13707        // If explicit_mutable is true, we've already checked for Mut token
13708        if explicit_mutable {
13709            self.advance(); // consume "mut"
13710        }
13711
13712        // Get variable name
13713        let var = self.expect_identifier()?;
13714
13715        // Check for optional type annotation: `: Type`
13716        let ty = if self.check(&TokenType::Colon) {
13717            self.advance(); // consume ":"
13718            let type_expr = self.parse_type_expression()?;
13719            Some(self.ctx.alloc_type_expr(type_expr))
13720        } else {
13721            None
13722        };
13723
13724        // Expect '='
13725        if !self.check(&TokenType::Assign) {
13726            return Err(ParseError {
13727                kind: ParseErrorKind::ExpectedKeyword {
13728                    keyword: "=".to_string(),
13729                },
13730                span: self.current_span(),
13731            });
13732        }
13733        self.advance(); // consume '='
13734
13735        // Parse value expression
13736        let value = self.parse_imperative_expr()?;
13737
13738        // Check for optional "with capacity <expr>" — wraps value in Expr::WithCapacity
13739        let value = if self.check_word("with") {
13740            let saved = self.backtrack_point();
13741            self.advance(); // consume "with"
13742            if self.check_word("capacity") {
13743                self.advance(); // consume "capacity"
13744                let cap_expr = self.parse_imperative_expr()?;
13745                self.ctx.alloc_imperative_expr(Expr::WithCapacity {
13746                    value,
13747                    capacity: cap_expr,
13748                })
13749            } else {
13750                self.backtrack_to(&saved); // backtrack
13751                value
13752            }
13753        } else {
13754            value
13755        };
13756
13757        // `x = e` on an EXISTING binding MUTATES it (no silent shadow — the
13758        // loop-body `total = total + i` footgun updated a fresh ghost while
13759        // the outer variable never moved). A `mut`/type-annotated form or a
13760        // fresh name is a new binding. The mutation auto-marks the target
13761        // mutable downstream (codegen derives `let mut` from the `Set`).
13762        if !explicit_mutable && ty.is_none() && self.user_bound.contains(&var) {
13763            return Ok(Stmt::Set { target: var, value });
13764        }
13765
13766        // Register variable in WorldState's DRS
13767        self.world_state.drs.introduce_referent(
13768            var,
13769            var,
13770            crate::drs::Gender::Unknown,
13771            crate::drs::Number::Singular,
13772        );
13773
13774        self.user_bound.insert(var);
13775        Ok(Stmt::Let {
13776            var,
13777            ty,
13778            value,
13779            mutable: explicit_mutable,
13780        })
13781    }
13782
13783    fn parse_set_statement(&mut self) -> ParseResult<Stmt<'a>> {
13784        use crate::ast::Expr;
13785        self.advance(); // consume "Set"
13786
13787        // `Set the todo's id to 9.` — the article in front of the ASSIGNED possessive. The target
13788        // is not parsed through `parse_primary_expr`, so the skip is needed here too; see
13789        // `eat_article_before_possessive`. It runs after the DOM sugar below, which claims its own
13790        // `the` (`the text of …`) and is told apart by the same two-token lookahead.
13791        self.eat_article_before_possessive();
13792
13793        // `Set row p of Kc to kp.` — the KV-cache append: row `p` of a declared tensor overwritten by a
13794        // tensor of exactly its row width and element kind. Backtracks unless the whole shape is there, so
13795        // a variable named `row` is still assignable.
13796        if let Some(stmt) = self.try_parse_set_row()? {
13797            return Ok(stmt);
13798        }
13799
13800        // DOM sugar: `Set the text of <element> to <value>.` → `setText(element, value)`.
13801        if self.check_word("the") && self.peek_word_at(1, "text") && self.peek_word_at(2, "of") {
13802            self.advance(); // the
13803            self.advance(); // text
13804            self.advance(); // of
13805            let element = self.parse_imperative_expr()?;
13806            let is_to = self.check(&TokenType::To)
13807                || matches!(&self.peek().kind, TokenType::Preposition(sym) if self.interner.resolve(*sym) == "to");
13808            if !is_to {
13809                return Err(ParseError {
13810                    kind: ParseErrorKind::ExpectedKeyword {
13811                        keyword: "to".to_string(),
13812                    },
13813                    span: self.current_span(),
13814                });
13815            }
13816            self.advance(); // consume "to"
13817            let value = self.parse_imperative_expr()?;
13818            let function = self.interner.intern("setText");
13819            return Ok(Stmt::Call {
13820                function,
13821                args: vec![element, value],
13822            });
13823        }
13824
13825        // Parse target - can be identifier or field access expression
13826        let target_from = self.current;
13827        let target_expr = self.parse_imperative_expr()?;
13828        let target_to = self.current;
13829
13830        // Support "Set X at KEY to VALUE" syntax for map insertion
13831        let target_expr = if self.check(&TokenType::At) {
13832            self.advance(); // consume "at"
13833            let key = self.parse_imperative_expr()?;
13834            self.ctx.alloc_imperative_expr(Expr::Index {
13835                collection: target_expr,
13836                index: key,
13837            })
13838        } else {
13839            target_expr
13840        };
13841
13842        // Expect "to" - can be TokenType::To or Preposition("to")
13843        let is_to = self.check(&TokenType::To)
13844            || matches!(
13845                &self.peek().kind,
13846                TokenType::Preposition(sym) if self.interner.resolve(*sym) == "to"
13847            );
13848        if !is_to {
13849            return Err(ParseError {
13850                kind: ParseErrorKind::ExpectedKeyword {
13851                    keyword: "to".to_string(),
13852                },
13853                span: self.current_span(),
13854            });
13855        }
13856        self.advance(); // consume "to"
13857
13858        // Parse expression value — or `every <x> in <list> where <condition>`, the kept items
13859        // gathered first and assigned as one list.
13860        let (gathered, value) = match self.try_parse_every_where()? {
13861            Some((binder, list, condition)) => {
13862                let (kept, body) = self.desugar_keep_where(binder, list, condition, true);
13863                (body, self.ctx.alloc_imperative_expr(Expr::Identifier(kept)))
13864            }
13865            None => (Vec::new(), self.parse_imperative_expr()?),
13866        };
13867
13868        // Phase 31: Handle field access targets
13869        // Also handle index targets: Set item N of X to Y
13870        let assign = match target_expr {
13871            Expr::FieldAccess { object, field } => {
13872                self.note_write(
13873                    target_expr,
13874                    target_from,
13875                    target_to,
13876                    component::WriteVerb::SetField,
13877                );
13878                Stmt::SetField {
13879                    object: *object,
13880                    field: *field,
13881                    value,
13882                }
13883            }
13884            Expr::Identifier(target) => {
13885                self.note_write(
13886                    target_expr,
13887                    target_from,
13888                    target_to,
13889                    component::WriteVerb::Set,
13890                );
13891                Stmt::Set {
13892                    target: *target,
13893                    value,
13894                }
13895            }
13896            Expr::Index { collection, index } => {
13897                self.note_write(
13898                    target_expr,
13899                    target_from,
13900                    target_to,
13901                    component::WriteVerb::SetItem,
13902                );
13903                self.desugar_place_set_index(*collection, *index, value)
13904            }
13905            _ => {
13906                return Err(ParseError {
13907                    kind: ParseErrorKind::ExpectedIdentifier,
13908                    span: self.current_span(),
13909                })
13910            }
13911        };
13912        if gathered.is_empty() {
13913            return Ok(assign);
13914        }
13915        let mut body = gathered;
13916        body.push(assign);
13917        let body = self.alloc_block(body);
13918        Ok(Stmt::Splice { body })
13919    }
13920
13921    fn parse_return_statement(&mut self) -> ParseResult<Stmt<'a>> {
13922        self.advance(); // consume "Return"
13923
13924        // Check if there's a value or just "Return."
13925        if self.check(&TokenType::Period) || self.is_at_end() {
13926            return Ok(Stmt::Return { value: None });
13927        }
13928
13929        // Use parse_condition (the full value grammar: and/or, comparisons, AND the `in <unit>` /
13930        // `in "<zone>"` postfix) so a Return is as expressive as a Show/Let — `Return q in feet`,
13931        // `Return m in "America/New_York"`, `Return a is before b`.
13932        let value = self.parse_condition()?;
13933        // Trailing-condition guard: `Return X if c.` → `If c: Return X.`
13934        if self.check(&TokenType::If) {
13935            self.advance(); // consume "if"
13936            let cond = self.parse_condition()?;
13937            let ret = Stmt::Return { value: Some(value) };
13938            let then_block = self.alloc_block(vec![ret]);
13939            return Ok(Stmt::If {
13940                cond,
13941                then_block,
13942                else_block: None,
13943            });
13944        }
13945        Ok(Stmt::Return { value: Some(value) })
13946    }
13947
13948    fn parse_break_statement(&mut self) -> ParseResult<Stmt<'a>> {
13949        self.advance(); // consume "Break"
13950        Ok(Stmt::Break)
13951    }
13952
13953    fn parse_assert_statement(&mut self) -> ParseResult<Stmt<'a>> {
13954        self.advance(); // consume "Assert"
13955
13956        // Optionally consume "that" (may be tokenized as That or Article(Distal))
13957        if self.check(&TokenType::That)
13958            || matches!(self.peek().kind, TokenType::Article(Definiteness::Distal))
13959        {
13960            self.advance();
13961        }
13962
13963        // Parse condition using imperative expression parser
13964        // This allows syntax like "Assert that b is not 0."
13965        let condition = self.parse_condition()?;
13966        let reason = self.parse_contract_reason()?;
13967
13968        Ok(Stmt::RuntimeAssert {
13969            condition,
13970            hard: false,
13971            dim: None,
13972            reason,
13973        })
13974    }
13975
13976    /// The optional `, otherwise <reason>` clause of `Assert`/`Require`/`Assume`: the text the
13977    /// program stops with when the contract breaks. Evaluated only on failure.
13978    fn parse_contract_reason(&mut self) -> ParseResult<Option<&'a Expr<'a>>> {
13979        if self.check(&TokenType::Comma)
13980            && matches!(
13981                self.tokens.get(self.current + 1).map(|t| &t.kind),
13982                Some(TokenType::Otherwise)
13983            )
13984        {
13985            self.advance();
13986            self.advance();
13987            return Ok(Some(self.parse_imperative_expr()?));
13988        }
13989        Ok(None)
13990    }
13991
13992    /// `Fail with <reason>.` — raise an error.
13993    fn parse_fail_statement(&mut self) -> ParseResult<Stmt<'a>> {
13994        self.advance(); // consume "Fail"
13995        self.advance(); // consume "with"
13996        let reason = self.parse_imperative_expr()?;
13997        Ok(Stmt::Fail { reason })
13998    }
13999
14000    /// `Require that <cond>.` — an ENFORCED runtime invariant lowering to a hard
14001    /// `assert!` (survives release). Same surface as `Assert that` but `hard: true`;
14002    /// this is the form a proven property check (`Require that check_thm(args).`) uses.
14003    fn parse_require_statement(&mut self) -> ParseResult<Stmt<'a>> {
14004        self.advance(); // consume "Require"
14005
14006        // Optionally consume "that" (may be tokenized as That or Article(Distal)).
14007        if self.check(&TokenType::That)
14008            || matches!(self.peek().kind, TokenType::Article(Definiteness::Distal))
14009        {
14010            self.advance();
14011        }
14012
14013        let condition = self.parse_condition()?;
14014        let (condition, dim) = self.fold_contract_dimension(condition)?;
14015        let reason = self.parse_contract_reason()?;
14016        Ok(Stmt::RuntimeAssert {
14017            condition,
14018            hard: true,
14019            dim,
14020            reason,
14021        })
14022    }
14023
14024    /// `Assume [that] <cond>.` — a declared INPUT CONTRACT: the same enforced runtime invariant as
14025    /// `Require that` (a hard `RuntimeAssert`), spelled as an assumption. The canonical form is a
14026    /// range declaration (`Assume x is between -1.0 and 1.0.`, via the existing `between` desugar),
14027    /// which static consumers — the analog lane's gain scaling — recover from the produced shape.
14028    fn parse_assume_statement(&mut self) -> ParseResult<Stmt<'a>> {
14029        self.advance(); // consume "Assume"
14030
14031        // Optionally consume "that" (may be tokenized as That or Article(Distal)).
14032        if self.check(&TokenType::That)
14033            || matches!(self.peek().kind, TokenType::Article(Definiteness::Distal))
14034        {
14035            self.advance();
14036        }
14037
14038        let condition = self.parse_condition()?;
14039        let (condition, dim) = self.fold_contract_dimension(condition)?;
14040        let reason = self.parse_contract_reason()?;
14041        Ok(Stmt::RuntimeAssert {
14042            condition,
14043            hard: true,
14044            dim,
14045            reason,
14046        })
14047    }
14048
14049    /// `Solve M times x equals v for x.` — the four-fabric equation statement (R4). The
14050    /// DIGITAL semantics are exact (`Let x be solveLinear(M, v).`, pure stdlib LOGOS, every
14051    /// tier for free); the relaxation fabric's settling solver is the certificate-gated
14052    /// accelerator for the same equation. The unknown must appear as the right factor of the
14053    /// product and match the `for` binder — anything else is refused with the shape named.
14054    fn parse_solve_statement(&mut self) -> ParseResult<Stmt<'a>> {
14055        self.advance(); // consume "Solve"
14056        let err = |msg: &str| ParseError {
14057            kind: ParseErrorKind::Custom(msg.to_string()),
14058            span: Span::default(),
14059        };
14060        let lhs = self.parse_xor_expr()?;
14061        if !self.check_word("equals") {
14062            return Err(err(
14063                "a Solve statement reads `Solve M times x equals v for x.` — I expected                  `equals` after the product",
14064            ));
14065        }
14066        self.advance(); // consume "equals"
14067        let rhs = self.parse_xor_expr()?;
14068        if !self.check_word("for") && !matches!(self.peek().kind, TokenType::Preposition(_)) {
14069            return Err(err("a Solve statement ends `… for x.` — name the unknown"));
14070        }
14071        self.advance(); // consume "for"
14072        let binder = match self.peek().kind {
14073            TokenType::Identifier | TokenType::Noun(_) => {
14074                let sym = self.peek().lexeme;
14075                self.advance();
14076                sym
14077            }
14078            _ => return Err(err("a Solve statement ends `… for x.` — name the unknown")),
14079        };
14080        // The product must be `matrix × unknown`, with the unknown matching the binder.
14081        let Expr::BinaryOp {
14082            op: BinaryOpKind::Multiply,
14083            left: matrix,
14084            right: unknown,
14085        } = lhs
14086        else {
14087            return Err(err(
14088                "a Solve statement reads `Solve M times x equals v for x.` — the left side                  must be the product `M times x`",
14089            ));
14090        };
14091        let Expr::Identifier(x) = unknown else {
14092            return Err(err("the unknown in `M times x` must be a plain name"));
14093        };
14094        if *x != binder {
14095            return Err(err(
14096                "the unknown in `M times x` and the `for` binder must be the SAME name",
14097            ));
14098        }
14099        let func = self.interner.intern("solveLinear");
14100        let call = self.ctx.alloc_imperative_expr(Expr::Call {
14101            function: func,
14102            args: vec![matrix, rhs],
14103        });
14104        Ok(Stmt::Let {
14105            var: binder,
14106            ty: None,
14107            value: call,
14108            mutable: false,
14109        })
14110    }
14111
14112    /// `Minimize the quadratic Q with linear b over x.` — the relaxation-fabric statement
14113    /// (R4). Digital semantics: the strongly convex quadratic's gradient vanishes at
14114    /// `Q·x = −b`, so the exact minimizer is `Let x be minimizeQuadratic(Q, b).` (stdlib
14115    /// LOGOS). The settling/soft-spin fabrics accelerate with Candidate semantics.
14116    fn parse_minimize_statement(&mut self) -> ParseResult<Stmt<'a>> {
14117        self.advance(); // consume "Minimize"
14118        let err = |msg: &str| ParseError {
14119            kind: ParseErrorKind::Custom(msg.to_string()),
14120            span: Span::default(),
14121        };
14122        if matches!(self.peek().kind, TokenType::Article(_)) {
14123            self.advance(); // optional "the"
14124        }
14125        if !self.check_word("quadratic") {
14126            return Err(err(
14127                "a Minimize statement reads `Minimize the quadratic Q with linear b over x.`",
14128            ));
14129        }
14130        self.advance(); // consume "quadratic"
14131        let q = self.parse_xor_expr()?;
14132        if !self.check_word("with") && !matches!(self.peek().kind, TokenType::Preposition(_)) {
14133            return Err(err("I expected `with linear b` after the quadratic"));
14134        }
14135        self.advance(); // consume "with"
14136        if !self.check_word("linear") {
14137            return Err(err("I expected `with linear b` after the quadratic"));
14138        }
14139        self.advance(); // consume "linear"
14140        let b = self.parse_xor_expr()?;
14141        if !self.check_word("over") && !matches!(self.peek().kind, TokenType::Preposition(_)) {
14142            return Err(err(
14143                "a Minimize statement ends `… over x.` — name the unknown",
14144            ));
14145        }
14146        self.advance(); // consume "over"
14147        let binder = match self.peek().kind {
14148            TokenType::Identifier | TokenType::Noun(_) => {
14149                let sym = self.peek().lexeme;
14150                self.advance();
14151                sym
14152            }
14153            _ => {
14154                return Err(err(
14155                    "a Minimize statement ends `… over x.` — name the unknown",
14156                ))
14157            }
14158        };
14159        let func = self.interner.intern("minimizeQuadratic");
14160        let call = self.ctx.alloc_imperative_expr(Expr::Call {
14161            function: func,
14162            args: vec![q, b],
14163        });
14164        Ok(Stmt::Let {
14165            var: binder,
14166            ty: None,
14167            value: call,
14168            mutable: false,
14169        })
14170    }
14171
14172    /// The `Assume`/`Require` range-contract post-pass: literal QUANTITY bounds in the
14173    /// `between` desugar shape (`lo <= x and x <= hi`) fold to their exact SI magnitudes —
14174    /// exact-rational unit conversion, then the one nearest-f64 the bare-float twin would
14175    /// carry — so the runtime condition compares plain floats on every tier, and the
14176    /// contract's physical dimension is returned for the statement to carry as metadata.
14177    ///
14178    /// A unit stated on one bound distributes over the coordination (`between 200 and 700
14179    /// millivolts`). Two stated units must agree in dimension — a voltage bounded by seconds
14180    /// is analytically meaningless and is refused HERE, at parse, before anything runs. A
14181    /// dimensioned contract with a non-literal bound is likewise refused (the fold cannot
14182    /// preserve its meaning). Conditions in any other shape, and unit-free ranges, pass
14183    /// through untouched — in particular, Quantity↔Quantity comparisons in general `If`/
14184    /// `While` conditions never reach this pass.
14185    fn fold_contract_dimension(
14186        &mut self,
14187        condition: &'a Expr<'a>,
14188    ) -> ParseResult<(
14189        &'a Expr<'a>,
14190        Option<logicaffeine_base::dimension::Dimension>,
14191    )> {
14192        use logicaffeine_base::numeric::{rational_from_f64_exact, Decimal, Rational};
14193        use logicaffeine_base::quantity::{units, Unit};
14194
14195        enum CBound {
14196            Bare(Rational),
14197            Stated(Rational, Unit, String),
14198            Opaque,
14199        }
14200
14201        let Expr::BinaryOp {
14202            op: BinaryOpKind::And,
14203            left: lower,
14204            right: upper,
14205        } = condition
14206        else {
14207            return Ok((condition, None));
14208        };
14209        let Expr::BinaryOp {
14210            op: BinaryOpKind::LtEq,
14211            left: lo,
14212            right: v1,
14213        } = lower
14214        else {
14215            return Ok((condition, None));
14216        };
14217        let Expr::BinaryOp {
14218            op: BinaryOpKind::LtEq,
14219            left: v2,
14220            right: hi,
14221        } = upper
14222        else {
14223            return Ok((condition, None));
14224        };
14225        let (Expr::Identifier(a), Expr::Identifier(b)) = (v1, v2) else {
14226            return Ok((condition, None));
14227        };
14228        if a != b {
14229            return Ok((condition, None));
14230        }
14231
14232        let classify = |parser: &Self, e: &Expr<'a>| -> CBound {
14233            // One level of the zero-minus-literal negation spelling.
14234            let (neg, e) = match e {
14235                Expr::BinaryOp {
14236                    op: BinaryOpKind::Subtract,
14237                    left,
14238                    right,
14239                } => match left {
14240                    Expr::Literal(Literal::Number(0)) => (true, *right),
14241                    Expr::Literal(Literal::Float(z)) if *z == 0.0 => (true, *right),
14242                    _ => (false, e),
14243                },
14244                _ => (false, e),
14245            };
14246            let signed = |r: Rational| if neg { r.negated() } else { r };
14247            match e {
14248                Expr::Literal(Literal::Number(n)) => CBound::Bare(signed(Rational::from_i64(*n))),
14249                Expr::Literal(Literal::Float(f)) => match rational_from_f64_exact(*f) {
14250                    Some(r) => CBound::Bare(signed(r)),
14251                    None => CBound::Opaque,
14252                },
14253                Expr::Call { function, args }
14254                    if parser.interner.resolve(*function) == "quantity" && args.len() == 2 =>
14255                {
14256                    let Expr::Literal(Literal::Text(unit_sym)) = args[1] else {
14257                        return CBound::Opaque;
14258                    };
14259                    let word = parser.interner.resolve(*unit_sym).to_string();
14260                    let Some(unit) = units::by_name(&word) else {
14261                        return CBound::Opaque;
14262                    };
14263                    let mag = match args[0] {
14264                        Expr::Literal(Literal::Number(n)) => Some(Rational::from_i64(*n)),
14265                        Expr::Call {
14266                            function: df,
14267                            args: dargs,
14268                        } if parser.interner.resolve(*df) == "decimal" && dargs.len() == 1 => {
14269                            match dargs[0] {
14270                                Expr::Literal(Literal::Text(t)) => {
14271                                    Decimal::parse(parser.interner.resolve(*t))
14272                                        .map(|d| d.to_rational())
14273                                }
14274                                _ => None,
14275                            }
14276                        }
14277                        _ => None,
14278                    };
14279                    match mag {
14280                        Some(m) => CBound::Stated(signed(m), unit, word),
14281                        None => CBound::Opaque,
14282                    }
14283                }
14284                _ => CBound::Opaque,
14285            }
14286        };
14287
14288        let (blo, bhi) = (classify(self, lo), classify(self, hi));
14289        // No unit anywhere → not a dimensioned contract; leave every bit untouched.
14290        if !matches!(blo, CBound::Stated(..)) && !matches!(bhi, CBound::Stated(..)) {
14291            return Ok((condition, None));
14292        }
14293        let err = |msg: String| ParseError {
14294            kind: ParseErrorKind::Custom(msg),
14295            span: Span::default(),
14296        };
14297        let dim_err = |wl: &str, wh: &str| {
14298            err(format!(
14299                "this contract bounds one variable with `{wl}` and `{wh}` — two different \
14300                 dimensions in one range is analytically meaningless"
14301            ))
14302        };
14303        let (si_lo, si_hi, unit) = match (blo, bhi) {
14304            (CBound::Stated(ml, ul, wl), CBound::Stated(mh, uh, wh)) => {
14305                if ul.dimension != uh.dimension {
14306                    return Err(dim_err(&wl, &wh));
14307                }
14308                let l = ml.mul(&ul.scale).add(&ul.offset);
14309                let h = mh.mul(&uh.scale).add(&uh.offset);
14310                (l, h, ul)
14311            }
14312            // The stated unit distributes over the coordination onto the bare bound.
14313            (CBound::Bare(ml), CBound::Stated(mh, uh, _)) => {
14314                let l = ml.mul(&uh.scale).add(&uh.offset);
14315                let h = mh.mul(&uh.scale).add(&uh.offset);
14316                (l, h, uh)
14317            }
14318            (CBound::Stated(ml, ul, _), CBound::Bare(mh)) => {
14319                let l = ml.mul(&ul.scale).add(&ul.offset);
14320                let h = mh.mul(&ul.scale).add(&ul.offset);
14321                (l, h, ul)
14322            }
14323            (CBound::Opaque, _) | (_, CBound::Opaque) => {
14324                return Err(err(
14325                    "a dimensioned range contract needs literal bounds — the unit fold cannot \
14326                     preserve a computed bound"
14327                        .to_string(),
14328                ));
14329            }
14330            (CBound::Bare(_), CBound::Bare(_)) => unreachable!("guarded above"),
14331        };
14332        let var = self.ctx.alloc_imperative_expr(Expr::Identifier(*a));
14333        let var2 = self.ctx.alloc_imperative_expr(Expr::Identifier(*a));
14334        let flo = self
14335            .ctx
14336            .alloc_imperative_expr(Expr::Literal(Literal::Float(si_lo.to_f64())));
14337        let fhi = self
14338            .ctx
14339            .alloc_imperative_expr(Expr::Literal(Literal::Float(si_hi.to_f64())));
14340        let lower = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
14341            op: BinaryOpKind::LtEq,
14342            left: flo,
14343            right: var,
14344        });
14345        let upper = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
14346            op: BinaryOpKind::LtEq,
14347            left: var2,
14348            right: fhi,
14349        });
14350        let folded = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
14351            op: BinaryOpKind::And,
14352            left: lower,
14353            right: upper,
14354        });
14355        Ok((folded, Some(unit.dimension)))
14356    }
14357
14358    /// Phase 35: Parse Trust statement
14359    /// Syntax: Trust [that] [proposition] because [justification].
14360    fn parse_trust_statement(&mut self) -> ParseResult<Stmt<'a>> {
14361        self.advance(); // consume "Trust"
14362
14363        // Optionally consume "that" (may be tokenized as That or Article(Distal))
14364        if self.check(&TokenType::That)
14365            || matches!(self.peek().kind, TokenType::Article(Definiteness::Distal))
14366        {
14367            self.advance();
14368        }
14369
14370        // Save current mode and switch to declarative for proposition parsing
14371        let saved_mode = self.mode;
14372        self.mode = ParserMode::Declarative;
14373
14374        // Parse the proposition using the Logic Kernel. The proposition is a
14375        // PREFIX of the statement — it ends where the host keyword "because"
14376        // begins, which is checked just below.
14377        let proposition = self.parse_prefix()?;
14378
14379        // Restore mode
14380        self.mode = saved_mode;
14381
14382        // Expect "because"
14383        if !self.check(&TokenType::Because) {
14384            return Err(ParseError {
14385                kind: ParseErrorKind::UnexpectedToken {
14386                    expected: TokenType::Because,
14387                    found: self.peek().kind.clone(),
14388                },
14389                span: self.current_span(),
14390            });
14391        }
14392        self.advance(); // consume "because"
14393
14394        // Parse justification (string literal)
14395        let justification = match &self.peek().kind {
14396            TokenType::StringLiteral(sym) => {
14397                let s = *sym;
14398                self.advance();
14399                s
14400            }
14401            _ => {
14402                return Err(ParseError {
14403                    kind: ParseErrorKind::UnexpectedToken {
14404                        expected: TokenType::StringLiteral(self.interner.intern("")),
14405                        found: self.peek().kind.clone(),
14406                    },
14407                    span: self.current_span(),
14408                });
14409            }
14410        };
14411
14412        Ok(Stmt::Trust {
14413            proposition,
14414            justification,
14415        })
14416    }
14417
14418    /// The lowercased lexeme `k` tokens ahead, for multi-word lookahead
14419    /// (test-metadata sentences like `This test may use the real filesystem.`).
14420    fn peek_lexeme_lower(&self, k: usize) -> Option<String> {
14421        self.tokens
14422            .get(self.current + k)
14423            .map(|t| self.interner.resolve(t.lexeme).to_lowercase())
14424    }
14425
14426    /// Parse a `## Test: <prose sentence>` block. The header token (which
14427    /// carries the RAW prose name, captured at the lexer) is already consumed.
14428    /// Shape:
14429    ///
14430    /// - the prose name is the test's identity (duplicates in a module are a
14431    ///   parse error);
14432    /// - leading metadata sentences: `Skip this test because "…".`, capability
14433    ///   grants (`This test may use the real filesystem.`), and the op-budget
14434    ///   escalation (`This test may run up to N steps.`);
14435    /// - the rest is an ordinary imperative body, in which the test-only
14436    ///   statements (`Expect …`, `Advance time`, `Given a file`) are legal.
14437    fn parse_test_block(&mut self, name: Symbol) -> ParseResult<Stmt<'a>> {
14438        use crate::ast::stmt::TestCapabilities;
14439
14440        let name_str = self.interner.resolve(name).to_string();
14441        if name_str.is_empty() {
14442            return Err(ParseError {
14443                kind: ParseErrorKind::Custom(
14444                    "a test needs a prose name: `## Test: <what this test shows>`".to_string(),
14445                ),
14446                span: self.current_span(),
14447            });
14448        }
14449        if !self.test_names.insert(name_str.clone()) {
14450            return Err(ParseError {
14451                kind: ParseErrorKind::Custom(format!(
14452                    "duplicate test name \"{name_str}\" — a test's prose name is its identity; \
14453                     give each ## Test its own sentence"
14454                )),
14455                span: self.current_span(),
14456            });
14457        }
14458
14459        // ── metadata sentences (leading paragraph only) ──
14460        let mut skip: Option<Symbol> = None;
14461        let mut capabilities = TestCapabilities::default();
14462        let mut budget: Option<u64> = None;
14463        loop {
14464            if self.check(&TokenType::Newline) || self.check(&TokenType::Indent) {
14465                self.advance();
14466                continue;
14467            }
14468            // `Skip this test because "<reason>".`
14469            if self.check(&TokenType::Skip) {
14470                self.advance(); // Skip
14471                                // "this test because" — accept the words loosely, require "because"
14472                while self
14473                    .peek_lexeme_lower(0)
14474                    .is_some_and(|w| w == "this" || w == "test")
14475                {
14476                    self.advance();
14477                }
14478                if !self.check(&TokenType::Because)
14479                    && self.peek_lexeme_lower(0).as_deref() != Some("because")
14480                {
14481                    return Err(ParseError {
14482                        kind: ParseErrorKind::Custom(
14483                            "a skip needs its reason: `Skip this test because \"<why>\".` \
14484                             — an unexplained skip is a silent debt"
14485                                .to_string(),
14486                        ),
14487                        span: self.current_span(),
14488                    });
14489                }
14490                self.advance(); // because
14491                let TokenType::StringLiteral(reason) = self.peek().kind else {
14492                    return Err(ParseError {
14493                        kind: ParseErrorKind::Custom(
14494                            "the skip reason is a quoted sentence: \
14495                             `Skip this test because \"the flag has not shipped\".`"
14496                                .to_string(),
14497                        ),
14498                        span: self.current_span(),
14499                    });
14500                };
14501                self.advance();
14502                if self.check(&TokenType::Period) {
14503                    self.advance();
14504                }
14505                skip = Some(reason);
14506                continue;
14507            }
14508            // `This test may use the real <resource>.` / `This test may run up to <N> steps.`
14509            if self.peek_lexeme_lower(0).as_deref() == Some("this")
14510                && self.peek_lexeme_lower(1).as_deref() == Some("test")
14511                && self.peek_lexeme_lower(2).as_deref() == Some("may")
14512            {
14513                self.advance(); // this
14514                self.advance(); // test
14515                self.advance(); // may
14516                match self.peek_lexeme_lower(0).as_deref() {
14517                    Some("use") => {
14518                        self.advance(); // use
14519                                        // "the real <resource>"
14520                        while self
14521                            .peek_lexeme_lower(0)
14522                            .is_some_and(|w| w == "the" || w == "real")
14523                        {
14524                            self.advance();
14525                        }
14526                        match self.peek_lexeme_lower(0).as_deref() {
14527                            Some("filesystem") => capabilities.filesystem = true,
14528                            Some("network") => capabilities.network = true,
14529                            Some("environment") => capabilities.environment = true,
14530                            Some("clock") => capabilities.clock = true,
14531                            other => {
14532                                return Err(ParseError {
14533                                    kind: ParseErrorKind::Custom(format!(
14534                                        "unknown capability {:?} — a test may use the real \
14535                                         filesystem, network, environment, or clock",
14536                                        other.unwrap_or_default()
14537                                    )),
14538                                    span: self.current_span(),
14539                                });
14540                            }
14541                        }
14542                        self.advance(); // the resource word
14543                    }
14544                    Some("run") => {
14545                        self.advance(); // run
14546                                        // "up to <N> steps"
14547                        while self
14548                            .peek_lexeme_lower(0)
14549                            .is_some_and(|w| w == "up" || w == "to")
14550                        {
14551                            self.advance();
14552                        }
14553                        let lexeme = self
14554                            .peek_lexeme_lower(0)
14555                            .unwrap_or_default()
14556                            .replace('_', "");
14557                        let Ok(steps) = lexeme.parse::<u64>() else {
14558                            return Err(ParseError {
14559                                kind: ParseErrorKind::Custom(
14560                                    "the op budget is a number: \
14561                                     `This test may run up to 5000000 steps.`"
14562                                        .to_string(),
14563                                ),
14564                                span: self.current_span(),
14565                            });
14566                        };
14567                        self.advance(); // the number
14568                        if self.peek_lexeme_lower(0).as_deref() == Some("steps") {
14569                            self.advance();
14570                        }
14571                        budget = Some(steps);
14572                    }
14573                    _ => {
14574                        return Err(ParseError {
14575                            kind: ParseErrorKind::Custom(
14576                                "a test grant is `This test may use the real <resource>.` \
14577                                 or `This test may run up to <N> steps.`"
14578                                    .to_string(),
14579                            ),
14580                            span: self.current_span(),
14581                        });
14582                    }
14583                }
14584                if self.check(&TokenType::Period) {
14585                    self.advance();
14586                }
14587                continue;
14588            }
14589            break;
14590        }
14591
14592        // ── the body: ordinary imperative statements, test-only surface armed ──
14593        let saved_in_test = self.in_test_block;
14594        self.in_test_block = true;
14595        let mut body_stmts = StmtList::default();
14596        let mut cases: Option<crate::ast::stmt::CaseTable<'a>> = None;
14597        let result = (|| -> ParseResult<()> {
14598            while !self.is_at_end() {
14599                if self.check(&TokenType::Newline)
14600                    || self.check(&TokenType::Indent)
14601                    || self.check(&TokenType::Dedent)
14602                {
14603                    self.advance();
14604                    continue;
14605                }
14606                if matches!(self.peek().kind, TokenType::BlockHeader { .. }) {
14607                    break;
14608                }
14609                // `For each of these cases:` + a markdown pipe table — the
14610                // test's case fixture (columns bind names; each row is a
14611                // named sub-test).
14612                if self.peek_lexeme_lower(0).as_deref() == Some("for")
14613                    && self.peek_lexeme_lower(1).as_deref() == Some("each")
14614                    && self.peek_lexeme_lower(2).as_deref() == Some("of")
14615                {
14616                    if cases.is_some() {
14617                        return Err(ParseError {
14618                            kind: ParseErrorKind::Custom(
14619                                "a test carries one case table — merge the rows".to_string(),
14620                            ),
14621                            span: self.current_span(),
14622                        });
14623                    }
14624                    cases = Some(self.parse_case_table()?);
14625                    continue;
14626                }
14627                self.read_block_statement(&mut body_stmts)?;
14628            }
14629            Ok(())
14630        })();
14631        self.in_test_block = saved_in_test;
14632        result?;
14633
14634        let body = self.alloc_read_block(body_stmts);
14635        Ok(Stmt::TestDef {
14636            name,
14637            skip,
14638            capabilities,
14639            budget,
14640            cases,
14641            body,
14642        })
14643    }
14644
14645    /// Is this token a table-line boundary?
14646    fn at_table_line_end(&self) -> bool {
14647        matches!(
14648            self.peek().kind,
14649            TokenType::Newline | TokenType::Indent | TokenType::Dedent | TokenType::EOF
14650        ) || matches!(self.peek().kind, TokenType::BlockHeader { .. })
14651    }
14652
14653    /// Parse `For each of these cases:` + a markdown pipe table into the
14654    /// test's case fixture. Row 1 is the header (column names), an optional
14655    /// all-dashes separator row is skipped, and every following `|`-led row is
14656    /// one data row of expressions (a named sub-test).
14657    fn parse_case_table(&mut self) -> ParseResult<crate::ast::stmt::CaseTable<'a>> {
14658        use crate::ast::stmt::CaseTable;
14659
14660        // consume "For each of these cases:"
14661        for expected in ["for", "each", "of", "these", "cases"] {
14662            if self.peek_lexeme_lower(0).as_deref() == Some(expected) {
14663                self.advance();
14664            }
14665        }
14666        if self.check(&TokenType::Colon) {
14667            self.advance();
14668        }
14669        // layout to the first table line
14670        while matches!(
14671            self.peek().kind,
14672            TokenType::Newline | TokenType::Indent | TokenType::Dedent
14673        ) {
14674            self.advance();
14675        }
14676        if !self.check(&TokenType::VBar) {
14677            return Err(ParseError {
14678                kind: ParseErrorKind::Custom(
14679                    "a case table follows: a markdown table whose header row names \
14680                     the columns (`| a | b | total |`)"
14681                        .to_string(),
14682                ),
14683                span: self.current_span(),
14684            });
14685        }
14686
14687        // ── header row: column names as raw words ──
14688        let mut columns: Vec<Symbol> = Vec::new();
14689        self.advance(); // leading VBar
14690        while !self.at_table_line_end() {
14691            let mut cell_words: Vec<String> = Vec::new();
14692            while !self.check(&TokenType::VBar) && !self.at_table_line_end() {
14693                cell_words.push(self.interner.resolve(self.peek().lexeme).to_string());
14694                self.advance();
14695            }
14696            if !cell_words.is_empty() {
14697                columns.push(self.interner.intern(&cell_words.join(" ")));
14698            }
14699            if self.check(&TokenType::VBar) {
14700                self.advance();
14701            }
14702        }
14703        if columns.is_empty() {
14704            return Err(ParseError {
14705                kind: ParseErrorKind::Custom(
14706                    "the case table's header row names its columns: `| a | b | total |`"
14707                        .to_string(),
14708                ),
14709                span: self.current_span(),
14710            });
14711        }
14712        // Columns BIND names in the test body's scope (each row runs the body
14713        // with its cells bound) — seed them exactly as `Let` and function
14714        // params do, so `a`/`b` resolve as variables, not articles.
14715        for column in &columns {
14716            self.user_bound.insert(*column);
14717        }
14718
14719        // ── rows: optional all-dashes separator, then expression rows ──
14720        let mut rows: Vec<Vec<&'a Expr<'a>>> = Vec::new();
14721        let mut separator_checked = false;
14722        loop {
14723            while matches!(
14724                self.peek().kind,
14725                TokenType::Newline | TokenType::Indent | TokenType::Dedent
14726            ) {
14727                self.advance();
14728            }
14729            if !self.check(&TokenType::VBar) {
14730                break; // the table ends at the first non-`|` line
14731            }
14732            // The conventional `|---|---|` separator sits right under the
14733            // header: a line of nothing but dashes and pipes. Scan ahead once.
14734            if !separator_checked {
14735                separator_checked = true;
14736                let mut k = 0;
14737                let mut only_dashes = true;
14738                loop {
14739                    let Some(tok) = self.tokens.get(self.current + k) else {
14740                        break;
14741                    };
14742                    match &tok.kind {
14743                        TokenType::VBar | TokenType::Minus => k += 1,
14744                        TokenType::Newline
14745                        | TokenType::Indent
14746                        | TokenType::Dedent
14747                        | TokenType::EOF => break,
14748                        _ => {
14749                            // A dashes-only word (`----`) is still separator ink.
14750                            let lex = self.interner.resolve(tok.lexeme);
14751                            if !lex.is_empty() && lex.chars().all(|c| c == '-') {
14752                                k += 1;
14753                            } else {
14754                                only_dashes = false;
14755                                break;
14756                            }
14757                        }
14758                    }
14759                }
14760                if only_dashes {
14761                    for _ in 0..k {
14762                        self.advance();
14763                    }
14764                    continue;
14765                }
14766            }
14767            // one data row: `| <expr> | <expr> | … |`. Cells parse one
14768            // precedence level BELOW bitwise-or — `|` is the cell delimiter
14769            // here, never the operator (parenthesize a bitwise-or cell).
14770            self.advance(); // leading VBar
14771            let mut row: Vec<&'a Expr<'a>> = Vec::new();
14772            while !self.at_table_line_end() {
14773                let cell = self.parse_bitxor_expr()?;
14774                row.push(cell);
14775                if self.check(&TokenType::VBar) {
14776                    self.advance();
14777                }
14778            }
14779            if row.len() != columns.len() {
14780                return Err(ParseError {
14781                    kind: ParseErrorKind::Custom(format!(
14782                        "this case row has {} cells but the header names {} columns",
14783                        row.len(),
14784                        columns.len()
14785                    )),
14786                    span: self.current_span(),
14787                });
14788            }
14789            rows.push(row);
14790        }
14791        if rows.is_empty() {
14792            return Err(ParseError {
14793                kind: ParseErrorKind::Custom(
14794                    "the case table needs at least one data row under the header".to_string(),
14795                ),
14796                span: self.current_span(),
14797            });
14798        }
14799        Ok(CaseTable { columns, rows })
14800    }
14801
14802    /// The `Expect` statement family (test-only; parse dispatch on
14803    /// [`TokenType::Expect`]):
14804    ///
14805    /// - `Expect that <condition>.` → records-and-continues assertion;
14806    /// - `Expect this to fail [with "<message>"]:` + indented body;
14807    /// - `Expect output:` + indented snapshot, or `Expect output as in "<file>".`
14808    fn parse_expect_statement(&mut self) -> ParseResult<Stmt<'a>> {
14809        use crate::ast::stmt::ExpectedOutput;
14810
14811        let gate_span = self.current_span();
14812        self.advance(); // consume "Expect"
14813        if !self.in_test_block {
14814            return Err(ParseError {
14815                kind: ParseErrorKind::Custom(
14816                    "`Expect` lives in `## Test` blocks — for a runtime guard in ordinary \
14817                     code, use `Require that <condition>.`"
14818                        .to_string(),
14819                ),
14820                span: gate_span,
14821            });
14822        }
14823
14824        // `Expect output:` / `Expect output as in "<file>".`
14825        if self.peek_lexeme_lower(0).as_deref() == Some("output") {
14826            self.advance(); // output
14827            if self.peek_lexeme_lower(0).as_deref() == Some("as") {
14828                self.advance(); // as
14829                if self.peek_lexeme_lower(0).as_deref() == Some("in") {
14830                    self.advance(); // in
14831                }
14832                let TokenType::StringLiteral(path) = self.peek().kind else {
14833                    return Err(ParseError {
14834                        kind: ParseErrorKind::Custom(
14835                            "the golden file is a quoted path: \
14836                             `Expect output as in \"golden/report.txt\".`"
14837                                .to_string(),
14838                        ),
14839                        span: self.current_span(),
14840                    });
14841                };
14842                self.advance();
14843                return Ok(Stmt::ExpectOutput {
14844                    expected: ExpectedOutput::File(path),
14845                });
14846            }
14847            if self.check(&TokenType::Colon) {
14848                self.advance();
14849            }
14850            // Capture the indented snapshot: lexemes joined per line, lines
14851            // joined with newlines, until the indent closes.
14852            while self.check(&TokenType::Newline) {
14853                self.advance();
14854            }
14855            let mut depth = 0usize;
14856            while self.check(&TokenType::Indent) {
14857                depth += 1;
14858                self.advance();
14859            }
14860            let mut lines: Vec<String> = Vec::new();
14861            let mut line: Vec<String> = Vec::new();
14862            while depth > 0 && !self.is_at_end() {
14863                match self.peek().kind {
14864                    TokenType::Indent => {
14865                        depth += 1;
14866                        self.advance();
14867                    }
14868                    TokenType::Dedent => {
14869                        depth -= 1;
14870                        self.advance();
14871                    }
14872                    TokenType::Newline => {
14873                        if !line.is_empty() {
14874                            lines.push(line.join(" "));
14875                            line.clear();
14876                        }
14877                        self.advance();
14878                    }
14879                    TokenType::EOF => break,
14880                    _ => {
14881                        line.push(self.interner.resolve(self.peek().lexeme).to_string());
14882                        self.advance();
14883                    }
14884                }
14885            }
14886            if !line.is_empty() {
14887                lines.push(line.join(" "));
14888            }
14889            let text = self.interner.intern(&lines.join("\n"));
14890            return Ok(Stmt::ExpectOutput {
14891                expected: ExpectedOutput::Inline(text),
14892            });
14893        }
14894
14895        // `Expect this to fail [with "<message>"]:` + indented body.
14896        if self.peek_lexeme_lower(0).as_deref() == Some("this") {
14897            self.advance(); // this
14898            while self
14899                .peek_lexeme_lower(0)
14900                .is_some_and(|w| w == "to" || w == "fail")
14901            {
14902                self.advance();
14903            }
14904            let mut message: Option<Symbol> = None;
14905            if self.peek_lexeme_lower(0).as_deref() == Some("with") {
14906                self.advance(); // with
14907                let TokenType::StringLiteral(msg) = self.peek().kind else {
14908                    return Err(ParseError {
14909                        kind: ParseErrorKind::Custom(
14910                            "the expected failure is a quoted message: \
14911                             `Expect this to fail with \"Division by zero\":`"
14912                                .to_string(),
14913                        ),
14914                        span: self.current_span(),
14915                    });
14916                };
14917                self.advance();
14918                message = Some(msg);
14919            }
14920            if self.check(&TokenType::Colon) {
14921                self.advance();
14922            }
14923            let body = self.parse_indented_block()?;
14924            return Ok(Stmt::ExpectFail { message, body });
14925        }
14926
14927        // `Expect that <condition>.`
14928        if self.check(&TokenType::That)
14929            || matches!(self.peek().kind, TokenType::Article(Definiteness::Distal))
14930        {
14931            self.advance();
14932        }
14933        let condition = self.parse_condition()?;
14934        Ok(Stmt::Expect { condition })
14935    }
14936
14937    /// `Advance time by <n> <unit>.` — moves the sealed world's virtual clock
14938    /// (test-only; execution lands with the sealed world, Wave 4).
14939    fn parse_advance_time_statement(&mut self) -> ParseResult<Stmt<'a>> {
14940        let gate_span = self.current_span();
14941        self.advance(); // consume "Advance"
14942        if !self.in_test_block {
14943            return Err(ParseError {
14944                kind: ParseErrorKind::Custom(
14945                    "`Advance time` moves a test's virtual clock — it lives in \
14946                     `## Test` blocks only"
14947                        .to_string(),
14948                ),
14949                span: gate_span,
14950            });
14951        }
14952        if self.peek_lexeme_lower(0).as_deref() == Some("time") {
14953            self.advance();
14954        }
14955        if self.peek_lexeme_lower(0).as_deref() == Some("by") {
14956            self.advance();
14957        }
14958        let amount = self.parse_xor_expr()?;
14959        let unit_ms: i64 = match self.peek_lexeme_lower(0).as_deref() {
14960            Some("millisecond") | Some("milliseconds") | Some("ms") => {
14961                self.advance();
14962                1
14963            }
14964            Some("second") | Some("seconds") => {
14965                self.advance();
14966                1000
14967            }
14968            Some("minute") | Some("minutes") => {
14969                self.advance();
14970                60_000
14971            }
14972            _ => 1, // bare number = milliseconds
14973        };
14974        let millis = if unit_ms == 1 {
14975            // ★ `25 seconds` LEXES AS ONE DURATION TOKEN, so the unit is already inside the value
14976            // and no factor is applied — which left a Duration in a field documented as "the
14977            // advance in MILLISECONDS". The interpreter converted it at run time and the bytecode
14978            // tier did not, so a page with a timer ran on one tier and answered
14979            // "Cannot compare Duration and Int" on the other: a TIER DIVERGENCE produced by an
14980            // AST node that did not hold what its own field said it held.
14981            //
14982            // Folded here, where the value is known, so every consumer downstream gets the
14983            // milliseconds it was promised and none of them has to know that a duration literal
14984            // exists. A Duration that arrives as a VARIABLE is still converted by the readers.
14985            match amount {
14986                Expr::Literal(Literal::Duration(ns)) => {
14987                    self.ctx
14988                        .alloc_imperative_expr(Expr::Literal(Literal::Number(
14989                            (*ns).max(0) / 1_000_000,
14990                        )))
14991                }
14992                other => other,
14993            }
14994        } else {
14995            let factor = self
14996                .ctx
14997                .alloc_imperative_expr(Expr::Literal(Literal::Number(unit_ms)));
14998            self.ctx.alloc_imperative_expr(Expr::BinaryOp {
14999                op: BinaryOpKind::Multiply,
15000                left: amount,
15001                right: factor,
15002            })
15003        };
15004        Ok(Stmt::AdvanceTime { millis })
15005    }
15006
15007    /// `Given a file "<path>" containing "<content>".` — pre-seeds the sealed
15008    /// world's in-memory filesystem (test-only; execution lands in Wave 4).
15009    fn parse_given_file_statement(&mut self) -> ParseResult<Stmt<'a>> {
15010        let gate_span = self.current_span();
15011        self.advance(); // consume "Given"
15012        if !self.in_test_block {
15013            return Err(ParseError {
15014                kind: ParseErrorKind::Custom(
15015                    "`Given a file …` seeds a test's sealed filesystem — it lives in \
15016                     `## Test` blocks only"
15017                        .to_string(),
15018                ),
15019                span: gate_span,
15020            });
15021        }
15022        while self
15023            .peek_lexeme_lower(0)
15024            .is_some_and(|w| w == "a" || w == "an" || w == "file")
15025        {
15026            self.advance();
15027        }
15028        let TokenType::StringLiteral(path) = self.peek().kind else {
15029            return Err(ParseError {
15030                kind: ParseErrorKind::Custom(
15031                    "the fixture path is a quoted string: \
15032                     `Given a file \"data.txt\" containing \"hello\".`"
15033                        .to_string(),
15034                ),
15035                span: self.current_span(),
15036            });
15037        };
15038        self.advance();
15039        if self.peek_lexeme_lower(0).as_deref() == Some("containing") {
15040            self.advance();
15041        }
15042        let TokenType::StringLiteral(content) = self.peek().kind else {
15043            return Err(ParseError {
15044                kind: ParseErrorKind::Custom(
15045                    "the fixture content is a quoted string: \
15046                     `Given a file \"data.txt\" containing \"hello\".`"
15047                        .to_string(),
15048                ),
15049                span: self.current_span(),
15050            });
15051        };
15052        self.advance();
15053        Ok(Stmt::GivenFile { path, content })
15054    }
15055
15056    /// Whether the token after the current one opens an argument list: the current word is called.
15057    fn next_is_argument_list(&self) -> bool {
15058        self.tokens
15059            .get(self.current + 1)
15060            .is_some_and(|t| matches!(t.kind, TokenType::LParen))
15061    }
15062
15063    /// `name(args).` — a call standing as a statement.
15064    fn parse_bare_call_statement(&mut self) -> ParseResult<Stmt<'a>> {
15065        let function = self.peek().lexeme;
15066        self.advance();
15067        let expr = self.parse_call_expr(function)?;
15068        if let Expr::Call { function, args } = expr {
15069            return Ok(Stmt::Call {
15070                function: *function,
15071                args: args.clone(),
15072            });
15073        }
15074        // A bare call carrying a trailing *field* access (`f(x)'s g.`) is a no-op the language
15075        // does not model as a statement (a UFCS chain like `f(x).m(y)` still lowers to a `Call`
15076        // above). The tokens are already consumed, so diagnose rather than fall through on a
15077        // stale position.
15078        Err(ParseError {
15079            kind: ParseErrorKind::ExpectedStatement,
15080            span: self.current_span(),
15081        })
15082    }
15083
15084    /// `Note/Warn/Record that <clause> [with <payload> and <payload>]`.
15085    /// The that-clause becomes
15086    /// the event message; prepositional payloads travel as named values.
15087    fn parse_observe_statement(
15088        &mut self,
15089        category: crate::ast::stmt::ObsCategory,
15090    ) -> ParseResult<Stmt<'a>> {
15091        self.advance(); // Note / Warn / Record
15092
15093        // Optional sampling: `once in every <N> times`.
15094        let mut sample: Option<u64> = None;
15095        if self.peek_lexeme_lower(0).as_deref() == Some("once") {
15096            self.advance(); // once
15097            while self
15098                .peek_lexeme_lower(0)
15099                .is_some_and(|w| w == "in" || w == "every")
15100            {
15101                self.advance();
15102            }
15103            let n = self
15104                .peek_lexeme_lower(0)
15105                .unwrap_or_default()
15106                .replace('_', "");
15107            if let Ok(rate) = n.parse::<u64>() {
15108                self.advance(); // the number
15109                if self.peek_lexeme_lower(0).as_deref() == Some("times") {
15110                    self.advance();
15111                }
15112                sample = Some(rate.max(1));
15113            }
15114        }
15115
15116        if self.check(&TokenType::That)
15117            || matches!(self.peek().kind, TokenType::Article(Definiteness::Distal))
15118        {
15119            self.advance();
15120        }
15121        // The that-clause: words up to `with`, the period, or end of line.
15122        let mut words: Vec<String> = Vec::new();
15123        while let Some(tok) = self.tokens.get(self.current) {
15124            if self.peek_lexeme_lower(0).as_deref() == Some("with") {
15125                break;
15126            }
15127            match tok.kind {
15128                TokenType::Period
15129                | TokenType::Newline
15130                | TokenType::Indent
15131                | TokenType::Dedent
15132                | TokenType::Colon
15133                | TokenType::EOF
15134                | TokenType::BlockHeader { .. } => break,
15135                _ => {
15136                    words.push(self.interner.resolve(tok.lexeme).to_string());
15137                    self.advance();
15138                }
15139            }
15140        }
15141        let message = self.interner.intern(words.join(" ").trim());
15142
15143        // Prepositional payloads: `with <expr> and <expr> …`.
15144        let mut payloads: Vec<(Symbol, &'a Expr<'a>)> = Vec::new();
15145        if self.peek_lexeme_lower(0).as_deref() == Some("with") {
15146            self.advance(); // with
15147            loop {
15148                let expr = self.parse_xor_expr()?;
15149                let name = match expr {
15150                    Expr::Identifier(s) => *s,
15151                    other => self
15152                        .interner
15153                        .intern(&crate::printer::render_expr(other, self.interner)),
15154                };
15155                payloads.push((name, expr));
15156                if self.check_word("and") || self.check(&TokenType::And) {
15157                    self.advance();
15158                } else {
15159                    break;
15160                }
15161            }
15162        }
15163        Ok(Stmt::Observe {
15164            category,
15165            message,
15166            payloads,
15167            sample,
15168        })
15169    }
15170
15171    /// `During <gerund>:` — a lexical trace span whose body is ordinary code.
15172    fn parse_during_statement(&mut self) -> ParseResult<Stmt<'a>> {
15173        self.advance(); // During
15174        let mut words: Vec<String> = Vec::new();
15175        while let Some(tok) = self.tokens.get(self.current) {
15176            match tok.kind {
15177                TokenType::Colon | TokenType::Newline | TokenType::Indent | TokenType::EOF => break,
15178                TokenType::StringLiteral(s) => {
15179                    words.push(self.interner.resolve(s).to_string());
15180                    self.advance();
15181                }
15182                _ => {
15183                    words.push(self.interner.resolve(tok.lexeme).to_string());
15184                    self.advance();
15185                }
15186            }
15187        }
15188        let label = self.interner.intern(words.join(" ").trim());
15189        if self.check(&TokenType::Colon) {
15190            self.advance();
15191        }
15192        // Skip the newline(s) between the header line and the indented body.
15193        while self.check(&TokenType::Newline) {
15194            self.advance();
15195        }
15196        let body = self.parse_indented_block()?;
15197        Ok(Stmt::During { label, body })
15198    }
15199
15200    /// `For all <domain> <vars> [where <cond>]:` — a property test
15201    /// (constitution §1.6). The domain word names the value space; the vars are
15202    /// bound fresh per generated case in the runner.
15203    fn parse_forall_statement(&mut self) -> ParseResult<Stmt<'a>> {
15204        use crate::ast::stmt::PropDomain;
15205        self.advance(); // For
15206        self.advance(); // all
15207
15208        // The domain: integers / naturals / booleans (singular or plural).
15209        let domain = match self.peek_lexeme_lower(0).as_deref() {
15210            Some("integers") | Some("integer") => PropDomain::Integers,
15211            Some("naturals") | Some("natural") => PropDomain::Naturals,
15212            Some("booleans") | Some("boolean") | Some("bools") | Some("bool") => {
15213                PropDomain::Booleans
15214            }
15215            other => {
15216                return Err(ParseError {
15217                    kind: ParseErrorKind::Custom(format!(
15218                        "a property quantifies over a domain — `integers`, `naturals`, or \
15219                         `booleans` (got {:?})",
15220                        other.unwrap_or_default()
15221                    )),
15222                    span: self.current_span(),
15223                });
15224            }
15225        };
15226        self.advance(); // the domain word
15227
15228        // The bound variables: `a and b` / `a, b` / `n`. Each becomes a
15229        // user-bound name for the body's scope.
15230        let mut vars: Vec<Symbol> = Vec::new();
15231        loop {
15232            let name = self.expect_identifier()?;
15233            self.user_bound.insert(name);
15234            vars.push(name);
15235            if self.check_word("and")
15236                || self.check(&TokenType::And)
15237                || self.check(&TokenType::Comma)
15238            {
15239                self.advance();
15240            } else {
15241                break;
15242            }
15243        }
15244        if vars.is_empty() {
15245            return Err(ParseError {
15246                kind: ParseErrorKind::Custom(
15247                    "a property needs at least one bound variable: `For all integers n:`"
15248                        .to_string(),
15249                ),
15250                span: self.current_span(),
15251            });
15252        }
15253
15254        // Optional refinement filter.
15255        let constraint = if self.check_word("where") {
15256            self.advance(); // where
15257            Some(self.parse_condition()?)
15258        } else {
15259            None
15260        };
15261
15262        if self.check(&TokenType::Colon) {
15263            self.advance();
15264        }
15265        while self.check(&TokenType::Newline) {
15266            self.advance();
15267        }
15268        let body = self.parse_indented_block()?;
15269        Ok(Stmt::ForAll {
15270            vars,
15271            domain,
15272            constraint,
15273            body,
15274        })
15275    }
15276
15277    /// Phase 50: Parse Check statement - mandatory security guard
15278    /// Syntax: Check that [subject] is [predicate].
15279    /// Syntax: Check that [subject] can [action] the [object].
15280    fn parse_check_statement(&mut self) -> ParseResult<Stmt<'a>> {
15281        let start_span = self.current_span();
15282        self.advance(); // consume "Check"
15283
15284        // Optionally consume "that"
15285        if self.check(&TokenType::That) {
15286            self.advance();
15287        }
15288
15289        // Consume optional "the"
15290        if matches!(self.peek().kind, TokenType::Article(_)) {
15291            self.advance();
15292        }
15293
15294        // Parse subject identifier (e.g., "user")
15295        let subject = match &self.peek().kind {
15296            TokenType::Noun(sym) | TokenType::Adjective(sym) | TokenType::ProperName(sym) => {
15297                let s = *sym;
15298                self.advance();
15299                s
15300            }
15301            _ => {
15302                // Try to get an identifier
15303                let tok = self.peek();
15304                let s = tok.lexeme;
15305                self.advance();
15306                s
15307            }
15308        };
15309
15310        // Determine if this is a predicate check ("is admin") or capability check ("can publish")
15311        let is_capability;
15312        let predicate;
15313        let object;
15314
15315        if self.check(&TokenType::Is) || self.check(&TokenType::Are) {
15316            // Predicate check: "user is admin"
15317            is_capability = false;
15318            self.advance(); // consume "is" / "are"
15319
15320            // Parse predicate name (e.g., "admin")
15321            predicate = match &self.peek().kind {
15322                TokenType::Noun(sym) | TokenType::Adjective(sym) | TokenType::ProperName(sym) => {
15323                    let s = *sym;
15324                    self.advance();
15325                    s
15326                }
15327                _ => {
15328                    let tok = self.peek();
15329                    let s = tok.lexeme;
15330                    self.advance();
15331                    s
15332                }
15333            };
15334            object = None;
15335        } else if self.check(&TokenType::Can) {
15336            // Capability check: "user can publish the document"
15337            is_capability = true;
15338            self.advance(); // consume "can"
15339
15340            // Parse action (e.g., "publish", "edit", "delete")
15341            predicate = match &self.peek().kind {
15342                TokenType::Verb { lemma, .. } => {
15343                    let s = *lemma;
15344                    self.advance();
15345                    s
15346                }
15347                TokenType::Noun(sym) | TokenType::Adjective(sym) | TokenType::ProperName(sym) => {
15348                    let s = *sym;
15349                    self.advance();
15350                    s
15351                }
15352                _ => {
15353                    let tok = self.peek();
15354                    let s = tok.lexeme;
15355                    self.advance();
15356                    s
15357                }
15358            };
15359
15360            // Consume optional "the"
15361            if matches!(self.peek().kind, TokenType::Article(_)) {
15362                self.advance();
15363            }
15364
15365            // Parse object (e.g., "document")
15366            let obj = match &self.peek().kind {
15367                TokenType::Noun(sym) | TokenType::Adjective(sym) | TokenType::ProperName(sym) => {
15368                    let s = *sym;
15369                    self.advance();
15370                    s
15371                }
15372                _ => {
15373                    let tok = self.peek();
15374                    let s = tok.lexeme;
15375                    self.advance();
15376                    s
15377                }
15378            };
15379            object = Some(obj);
15380        } else {
15381            return Err(ParseError {
15382                kind: ParseErrorKind::ExpectedKeyword {
15383                    keyword: "is/can".to_string(),
15384                },
15385                span: self.current_span(),
15386            });
15387        }
15388
15389        // Build source text for error message
15390        let source_text = if is_capability {
15391            let obj_name = self.interner.resolve(object.unwrap());
15392            let pred_name = self.interner.resolve(predicate);
15393            let subj_name = self.interner.resolve(subject);
15394            format!("{} can {} the {}", subj_name, pred_name, obj_name)
15395        } else {
15396            let pred_name = self.interner.resolve(predicate);
15397            let subj_name = self.interner.resolve(subject);
15398            format!("{} is {}", subj_name, pred_name)
15399        };
15400
15401        Ok(Stmt::Check {
15402            subject,
15403            predicate,
15404            is_capability,
15405            object,
15406            source_text,
15407            span: start_span,
15408        })
15409    }
15410
15411    /// Read the current token's lexeme as an owned `String` and advance.
15412    fn read_word_string(&mut self) -> String {
15413        let s = self.interner.resolve(self.peek().lexeme).to_string();
15414        self.advance();
15415        s
15416    }
15417
15418    /// Expect a specific keyword (case-insensitive) and advance; else a `Custom` error.
15419    fn expect_keyword(&mut self, word: &str) -> ParseResult<()> {
15420        if self.check_word(word) {
15421            self.advance();
15422            Ok(())
15423        } else {
15424            Err(ParseError {
15425                kind: ParseErrorKind::Custom(format!("expected `{}` here", word)),
15426                span: self.current_span(),
15427            })
15428        }
15429    }
15430
15431    /// Read an integer literal token and advance; else a `Custom` error.
15432    fn read_int_literal(&mut self) -> ParseResult<i64> {
15433        if let TokenType::Number(sym) = &self.peek().kind {
15434            let sym = *sym;
15435            self.advance();
15436            self.interner
15437                .resolve(sym)
15438                .parse::<i64>()
15439                .map_err(|_| ParseError {
15440                    kind: ParseErrorKind::Custom("expected an integer bound".to_string()),
15441                    span: self.current_span(),
15442                })
15443        } else {
15444            Err(ParseError {
15445                kind: ParseErrorKind::Custom("expected an integer bound".to_string()),
15446                span: self.current_span(),
15447            })
15448        }
15449    }
15450
15451    /// `Accept computed <Name> where <param> is an Int from <lo> to <hi>` — a parse-time
15452    /// acceptance-contract declaration. Records the named integer bounds (consumed by a later
15453    /// `Run … under <Name>`) and emits NO runtime statement.
15454    fn parse_accept_contract(&mut self) -> ParseResult<()> {
15455        self.advance(); // "Accept"
15456        self.expect_keyword("computed")?;
15457        let name = self.read_word_string();
15458        self.expect_keyword("where")?;
15459        let _param = self.read_word_string(); // the parameter name is documentation only
15460        self.expect_keyword("is")?;
15461        if self.check_word("a") || self.check_word("an") {
15462            self.advance();
15463        }
15464        self.expect_keyword("Int")?;
15465        self.expect_keyword("from")?;
15466        let lo = self.read_int_literal()?;
15467        // "to" can be the `To` token or the bare word.
15468        if self.check(&TokenType::To) || self.check_word("to") {
15469            self.advance();
15470        } else {
15471            return Err(ParseError {
15472                kind: ParseErrorKind::Custom(
15473                    "expected `to` between the contract bounds".to_string(),
15474                ),
15475                span: self.current_span(),
15476            });
15477        }
15478        let hi = self.read_int_literal()?;
15479        if self.check(&TokenType::Period) {
15480            self.advance(); // this declaration `continue`s in parse_program, so eat its own `.`
15481        }
15482        self.contracts.insert(name, (lo, hi));
15483        Ok(())
15484    }
15485
15486    /// `Run <f> on <arg> under <Name> into <var>` → `Let <var> be run_accepted(<f>, <arg>, lo, hi)`,
15487    /// inlining the named acceptance contract's bounds. The trailing `.` is consumed by
15488    /// `parse_program` (this returns a real statement).
15489    fn parse_run_under_contract(&mut self) -> ParseResult<Stmt<'a>> {
15490        self.advance(); // "Run"
15491        let f_expr = self.parse_imperative_expr()?;
15492        self.expect_keyword("on")?;
15493        let arg_expr = self.parse_imperative_expr()?;
15494        self.expect_keyword("under")?;
15495        let name = self.read_word_string();
15496        let (lo, hi) = *self.contracts.get(&name).ok_or_else(|| ParseError {
15497            kind: ParseErrorKind::Custom(format!(
15498                "unknown acceptance contract `{name}` — declare it with `Accept computed {name} where …`"
15499            )),
15500            span: self.current_span(),
15501        })?;
15502        self.expect_keyword("into")?;
15503        let var = self.peek().lexeme;
15504        self.advance();
15505        let lo_e = self
15506            .ctx
15507            .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Number(lo)));
15508        let hi_e = self
15509            .ctx
15510            .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Number(hi)));
15511        let run_sym = self.interner.intern("run_accepted");
15512        let call = self.ctx.alloc_imperative_expr(Expr::Call {
15513            function: run_sym,
15514            args: vec![f_expr, arg_expr, lo_e, hi_e],
15515        });
15516        Ok(Stmt::Let {
15517            var,
15518            ty: None,
15519            value: call,
15520            mutable: false,
15521        })
15522    }
15523
15524    /// Phase 51: Parse Listen statement - bind to network address
15525    /// Syntax: Listen on [address].
15526    fn parse_listen_statement(&mut self) -> ParseResult<Stmt<'a>> {
15527        self.advance(); // consume "Listen"
15528
15529        // Expect "on" preposition
15530        if !self.check_preposition_is("on") {
15531            return Err(ParseError {
15532                kind: ParseErrorKind::ExpectedKeyword {
15533                    keyword: "on".to_string(),
15534                },
15535                span: self.current_span(),
15536            });
15537        }
15538        self.advance(); // consume "on"
15539
15540        // Parse address expression (string literal or variable)
15541        let address = self.parse_imperative_expr()?;
15542
15543        let secure = self.parse_secure_clause()?;
15544        Ok(Stmt::Listen { address, secure })
15545    }
15546
15547    /// Parse an HTTP `Serve` statement.
15548    /// Syntax: `Serve on port <expr> with <handler>.`
15549    fn parse_serve_statement(&mut self) -> ParseResult<Stmt<'a>> {
15550        self.advance(); // consume "Serve"
15551
15552        // Expect "on".
15553        if !self.check_preposition_is("on") && !self.check_word("on") {
15554            return Err(ParseError {
15555                kind: ParseErrorKind::ExpectedKeyword {
15556                    keyword: "on".to_string(),
15557                },
15558                span: self.current_span(),
15559            });
15560        }
15561        self.advance(); // consume "on"
15562
15563        // Expect "port".
15564        if !self.check_word("port") {
15565            return Err(ParseError {
15566                kind: ParseErrorKind::ExpectedKeyword {
15567                    keyword: "port".to_string(),
15568                },
15569                span: self.current_span(),
15570            });
15571        }
15572        self.advance(); // consume "port"
15573
15574        // The port expression (an Int literal or variable).
15575        let port = self.parse_imperative_expr()?;
15576
15577        // ★ `with <handler>` IS OPTIONAL. A file that declares `## Route` blocks has already
15578        // said everything a request needs — which function answers, with what status, and what
15579        // the answer is — so `Serve on port 8080.` serves the table. Making the author then
15580        // hand-write a `Request -> Response` that calls `route(…)` is ceremony whose only
15581        // possible outcome is getting it subtly wrong: a forgotten content type shows JSON as
15582        // text, a forgotten `Location` makes a redirect a dead end.
15583        //
15584        // `with` stays for the programs that want the whole request in their own hands.
15585        if !self.check_preposition_is("with") && !self.check_word("with") {
15586            let handler = self.ctx.alloc_imperative_expr(Expr::Identifier(
15587                self.interner
15588                    .intern(crate::parser::lgx_lower::ROUTE_HANDLER),
15589            ));
15590            return Ok(Stmt::Serve { port, handler });
15591        }
15592        self.advance(); // consume "with"
15593
15594        // The handler function (a `Request -> Response`), named or first-class.
15595        let handler = self.parse_imperative_expr()?;
15596
15597        Ok(Stmt::Serve { port, handler })
15598    }
15599
15600    /// `On <event> from <source>:` — the statement-level async event handler. Unlike the
15601    /// view-child `on <event>:` (which attaches to the enclosing element), this takes an
15602    /// EXPLICIT source, the shape media/peer orchestration needs (`On chunk from encoder:`,
15603    /// `On track from peer:`). It desugars to the same lowering the view handler uses:
15604    /// `addEventListener(source, "<event>", (event: Int) ->: <block>)`.
15605    fn parse_on_statement(&mut self) -> ParseResult<Stmt<'a>> {
15606        use crate::ast::stmt::ClosureBody;
15607        use crate::ast::{Expr, Literal, TypeExpr};
15608
15609        self.advance(); // consume "On"
15610        let event_sym = self.peek().lexeme;
15611        self.advance(); // consume the event name
15612
15613        if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
15614            return Err(ParseError {
15615                kind: ParseErrorKind::ExpectedKeyword {
15616                    keyword: "from".to_string(),
15617                },
15618                span: self.current_span(),
15619            });
15620        }
15621        self.advance(); // consume "from"
15622
15623        let source = self.parse_imperative_expr()?;
15624
15625        if !self.check(&TokenType::Colon) {
15626            return Err(ParseError {
15627                kind: ParseErrorKind::ExpectedKeyword {
15628                    keyword: ":".to_string(),
15629                },
15630                span: self.current_span(),
15631            });
15632        }
15633        self.advance(); // consume ':'
15634
15635        // Inline `On e from s: <one statement>` or an indented block.
15636        let mut block_stmts = StmtList::default();
15637        if !self.check(&TokenType::Indent) {
15638            self.read_block_statement(&mut block_stmts)?;
15639        } else {
15640            self.advance(); // consume Indent
15641            while !self.check(&TokenType::Dedent) && !self.is_at_end() {
15642                if self.check(&TokenType::Newline) {
15643                    self.advance();
15644                    continue;
15645                }
15646                self.read_block_statement(&mut block_stmts)?;
15647            }
15648            if self.check(&TokenType::Dedent) {
15649                self.advance();
15650            }
15651        }
15652        let block = self.alloc_read_block(block_stmts);
15653        // `(event: Int) ->: <block>` — Event is the Int host-handle alias, as in the view handler.
15654        let int_ty = self
15655            .ctx
15656            .alloc_type_expr(TypeExpr::Primitive(self.interner.intern("Int")));
15657        let param = self.interner.intern("event");
15658        let closure = self.ctx.alloc_imperative_expr(Expr::Closure {
15659            params: vec![(param, int_ty)],
15660            body: ClosureBody::Block(block),
15661            return_type: None,
15662        });
15663        let event_name = self
15664            .ctx
15665            .alloc_imperative_expr(Expr::Literal(Literal::Text(event_sym)));
15666        let add_listener = self.interner.intern("addEventListener");
15667        Ok(Stmt::Call {
15668            function: add_listener,
15669            args: vec![source, event_name, closure],
15670        })
15671    }
15672
15673    /// Phase 51: Parse Connect statement - dial remote peer
15674    /// Syntax: Connect to [address].
15675    fn parse_connect_statement(&mut self) -> ParseResult<Stmt<'a>> {
15676        self.advance(); // consume "Connect"
15677
15678        // Expect "to" (can be TokenType::To or preposition)
15679        if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
15680            return Err(ParseError {
15681                kind: ParseErrorKind::ExpectedKeyword {
15682                    keyword: "to".to_string(),
15683                },
15684                span: self.current_span(),
15685            });
15686        }
15687        self.advance(); // consume "to"
15688
15689        // Parse address expression
15690        let address = self.parse_imperative_expr()?;
15691
15692        let secure = self.parse_secure_clause()?;
15693        Ok(Stmt::ConnectTo { address, secure })
15694    }
15695
15696    /// Parse the optional PNP one-time-pad clause `with pad "<path>" as initiator|responder` that may
15697    /// trail a `Connect`/`Listen`. Returns `None` when the clause is absent (a bare statement), so the
15698    /// wire/behavior is byte-identical for programs that don't opt in.
15699    fn parse_secure_clause(&mut self) -> ParseResult<Option<crate::ast::SecurePad<'a>>> {
15700        // Only engage on the two-word lead `with pad`, so a stray `with` elsewhere is left alone.
15701        if !(self.check_word("with") && self.peek_word_at(1, "pad")) {
15702            return Ok(None);
15703        }
15704        self.advance(); // "with"
15705        self.advance(); // "pad"
15706        let pad = self.parse_imperative_expr()?;
15707        if !self.check_word("as") {
15708            return Err(ParseError {
15709                kind: ParseErrorKind::ExpectedKeyword {
15710                    keyword: "as".to_string(),
15711                },
15712                span: self.current_span(),
15713            });
15714        }
15715        self.advance(); // "as"
15716        let role = if self.check_word("initiator") {
15717            self.advance();
15718            crate::ast::SecureRole::Initiator
15719        } else if self.check_word("responder") {
15720            self.advance();
15721            crate::ast::SecureRole::Responder
15722        } else {
15723            return Err(ParseError {
15724                kind: ParseErrorKind::ExpectedKeyword {
15725                    keyword: "initiator or responder".to_string(),
15726                },
15727                span: self.current_span(),
15728            });
15729        };
15730        Ok(Some(crate::ast::SecurePad { pad, role }))
15731    }
15732
15733    /// Phase 51: Parse Sleep statement - pause execution
15734    /// Syntax: Sleep [milliseconds].
15735    fn parse_sleep_statement(&mut self) -> ParseResult<Stmt<'a>> {
15736        self.advance(); // consume "Sleep"
15737
15738        // Parse milliseconds expression (number or variable)
15739        let milliseconds = self.parse_imperative_expr()?;
15740
15741        Ok(Stmt::Sleep { milliseconds })
15742    }
15743
15744    /// Phase 52: Parse Sync statement - automatic CRDT replication
15745    /// Syntax: Sync [var] on [topic].
15746    fn parse_sync_statement(&mut self) -> ParseResult<Stmt<'a>> {
15747        self.advance(); // consume "Sync"
15748
15749        // Parse variable name (must be an identifier)
15750        // Phase 49: Also handle Verb and Ambiguous tokens (e.g., "state" can be verb or noun)
15751        let var = match &self.tokens[self.current].kind {
15752            TokenType::ProperName(sym) | TokenType::Noun(sym) | TokenType::Adjective(sym) => {
15753                let s = *sym;
15754                self.advance();
15755                s
15756            }
15757            TokenType::Verb { .. } | TokenType::Ambiguous { .. } => {
15758                let s = self.tokens[self.current].lexeme;
15759                self.advance();
15760                s
15761            }
15762            _ => {
15763                return Err(ParseError {
15764                    kind: ParseErrorKind::ExpectedKeyword {
15765                        keyword: "variable name".to_string(),
15766                    },
15767                    span: self.current_span(),
15768                });
15769            }
15770        };
15771
15772        // Expect "on" preposition
15773        if !self.check_preposition_is("on") {
15774            return Err(ParseError {
15775                kind: ParseErrorKind::ExpectedKeyword {
15776                    keyword: "on".to_string(),
15777                },
15778                span: self.current_span(),
15779            });
15780        }
15781        self.advance(); // consume "on"
15782
15783        // Parse topic expression (string literal or variable)
15784        let topic = self.parse_imperative_expr()?;
15785
15786        Ok(Stmt::Sync { var, topic })
15787    }
15788
15789    /// Phase 53: Parse Mount statement
15790    /// Syntax: Mount [var] at [path].
15791    /// Example: Mount counter at "data/counter.journal".
15792    fn parse_mount_statement(&mut self) -> ParseResult<Stmt<'a>> {
15793        self.advance(); // consume "Mount"
15794
15795        // A UI mount and the CRDT-journal `Mount <var> at "path"` share a sentence; the SHAPE
15796        // decides. It is a UI mount when the name is a declared component (every `## Component`
15797        // in the file is known before the first statement is read), when the name is capitalised
15798        // (components are; journal variables are not), when there is no `at` (a journal always
15799        // names a path), or when the target reads as a selector (`#root`, `.app`, `main`).
15800        // Whether the component EXISTS is checked at end-of-parse, with the names the file has.
15801        let name = self.tokens[self.current].lexeme;
15802        let capitalised = self
15803            .interner
15804            .resolve(name)
15805            .chars()
15806            .next()
15807            .is_some_and(char::is_uppercase);
15808        let no_target = matches!(
15809            self.tokens.get(self.current + 1).map(|t| &t.kind),
15810            Some(TokenType::Period)
15811        );
15812        let selector_target = matches!(
15813            self.tokens.get(self.current + 1).map(|t| &t.kind),
15814            Some(TokenType::At)
15815        ) && matches!(
15816            self.tokens.get(self.current + 2).map(|t| &t.kind),
15817            Some(TokenType::StringLiteral(s)) if crate::lgx::html::is_selector_shaped(self.interner.resolve(*s))
15818        );
15819        if self.components.contains(&name) || capitalised || no_target || selector_target {
15820            return self.parse_ui_mount_statement();
15821        }
15822
15823        // Parse variable name (must be an identifier)
15824        // Phase 49: Also handle Verb and Ambiguous tokens
15825        let var = match &self.tokens[self.current].kind {
15826            TokenType::ProperName(sym) | TokenType::Noun(sym) | TokenType::Adjective(sym) => {
15827                let s = *sym;
15828                self.advance();
15829                s
15830            }
15831            TokenType::Verb { .. } | TokenType::Ambiguous { .. } => {
15832                let s = self.tokens[self.current].lexeme;
15833                self.advance();
15834                s
15835            }
15836            _ => {
15837                return Err(ParseError {
15838                    kind: ParseErrorKind::ExpectedKeyword {
15839                        keyword: "variable name".to_string(),
15840                    },
15841                    span: self.current_span(),
15842                });
15843            }
15844        };
15845
15846        // Expect "at" keyword (TokenType::At in imperative mode)
15847        if !self.check(&TokenType::At) {
15848            return Err(ParseError {
15849                kind: ParseErrorKind::ExpectedKeyword {
15850                    keyword: "at".to_string(),
15851                },
15852                span: self.current_span(),
15853            });
15854        }
15855        self.advance(); // consume "at"
15856
15857        // Parse path expression (string literal or variable)
15858        let path = self.parse_imperative_expr()?;
15859
15860        Ok(Stmt::Mount { var, path })
15861    }
15862
15863    /// `Mount <Component> at "#sel"` — the ceremony killer. Desugars to the reactive
15864    /// root ceremony: a synthesized top-level `render` function (`beginRender`, clear
15865    /// the root, `enterComponent`/`__render_<Component>`/`exitComponent`) that the
15866    /// wasm backend exports as `__logos_render` for the settle loop, plus a mount
15867    /// statement that finds-or-creates the `#sel` element and does the initial render.
15868    fn parse_ui_mount_statement(&mut self) -> ParseResult<Stmt<'a>> {
15869        use crate::ast::{BinaryOpKind, Expr, Literal};
15870
15871        let comp_sym = self.tokens[self.current].lexeme;
15872        let target = match (
15873            self.tokens.get(self.current + 1).map(|t| &t.kind),
15874            self.tokens.get(self.current + 2).map(|t| &t.kind),
15875        ) {
15876            (Some(TokenType::At), Some(TokenType::StringLiteral(s))) => {
15877                Some(self.interner.resolve(*s).to_string())
15878            }
15879            _ => None,
15880        };
15881        self.mount_sites.push(MountSite {
15882            name: comp_sym,
15883            span: self.current_span(),
15884            is_root: !self.in_test_block,
15885            target,
15886        });
15887        self.advance(); // consume the component name
15888                        // An LGX component mounts through the first-class runtime: the component model owns
15889                        // instance creation, `When the page loads`, and rendering.
15890        if self.lgx_components.contains(&comp_sym) {
15891            let selector = if self.check(&TokenType::At) {
15892                self.advance();
15893                Some(self.parse_imperative_expr()?)
15894            } else {
15895                None
15896            };
15897            return Ok(Stmt::Ui(crate::ast::stmt::UiStmt::Mount {
15898                component: comp_sym,
15899                selector,
15900            }));
15901        }
15902        // `Mount App` — the least-ceremony form — defaults to the `#root` mount point.
15903        // `Mount App at "#sel"` names an explicit one (a string literal, whose `#id`
15904        // is the mount point created if absent).
15905        let sel_str = if self.check(&TokenType::At) {
15906            self.advance(); // consume "at"
15907            let sel_expr = self.parse_imperative_expr()?;
15908            match sel_expr {
15909                Expr::Literal(Literal::Text(s)) => self.interner.resolve(*s).to_string(),
15910                _ => {
15911                    return Err(ParseError {
15912                        kind: ParseErrorKind::Custom(
15913                            "Mount's selector must be a string literal, e.g. `Mount App at \"#sidebar\"` \
15914                             — or just `Mount App` for the default `#root`."
15915                                .to_string(),
15916                        ),
15917                        span: self.current_span(),
15918                    });
15919                }
15920            }
15921        } else {
15922            "#root".to_string()
15923        };
15924        let id_str = sel_str.strip_prefix('#').unwrap_or(&sel_str).to_string();
15925
15926        let render_entry = self.interner.intern("render");
15927        let render_name = self
15928            .interner
15929            .intern(&format!("__render_{}", self.interner.resolve(comp_sym)));
15930        let query = self.interner.intern("querySelector");
15931        let sel_sym = self.interner.intern(&sel_str);
15932
15933        // ---- the synthesized top-level `render` function ----
15934        let mut render_body: Vec<Stmt<'a>> = Vec::new();
15935        let begin = self.interner.intern("beginRender");
15936        render_body.push(Stmt::Call {
15937            function: begin,
15938            args: vec![],
15939        });
15940        let root_sym = self.interner.intern("__root");
15941        let sel_lit = self
15942            .ctx
15943            .alloc_imperative_expr(Expr::Literal(Literal::Text(sel_sym)));
15944        let qs_call = self.ctx.alloc_imperative_expr(Expr::Call {
15945            function: query,
15946            args: vec![sel_lit],
15947        });
15948        render_body.push(Stmt::Let {
15949            var: root_sym,
15950            ty: None,
15951            value: qs_call,
15952            mutable: false,
15953        });
15954        let root_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(root_sym));
15955        let clear = self.interner.intern("clearChildren");
15956        render_body.push(Stmt::Call {
15957            function: clear,
15958            args: vec![root_ref],
15959        });
15960        let enter = self.interner.intern("enterComponent");
15961        let key_lit = self
15962            .ctx
15963            .alloc_imperative_expr(Expr::Literal(Literal::Text(comp_sym)));
15964        render_body.push(Stmt::Call {
15965            function: enter,
15966            args: vec![key_lit],
15967        });
15968        render_body.push(Stmt::Call {
15969            function: render_name,
15970            args: vec![root_ref],
15971        });
15972        let exit = self.interner.intern("exitComponent");
15973        render_body.push(Stmt::Call {
15974            function: exit,
15975            args: vec![],
15976        });
15977        let render_block = self.alloc_block(render_body);
15978        self.deferred_functions.push(Stmt::FunctionDef {
15979            name: render_entry,
15980            generics: Vec::new(),
15981            params: Vec::new(),
15982            body: render_block,
15983            return_type: None,
15984            is_native: false,
15985            native_path: None,
15986            is_exported: false,
15987            export_target: None,
15988            opt_flags: OptimizationConfig::all_on(),
15989        });
15990
15991        // ---- the mount statement returned into Main: find-or-create #sel, then render ----
15992        let mut mount_body: Vec<Stmt<'a>> = Vec::new();
15993        // `If querySelector("#sel") is less than 0:` — create the mount point when absent.
15994        let sel_lit2 = self
15995            .ctx
15996            .alloc_imperative_expr(Expr::Literal(Literal::Text(sel_sym)));
15997        let qs_call2 = self.ctx.alloc_imperative_expr(Expr::Call {
15998            function: query,
15999            args: vec![sel_lit2],
16000        });
16001        let zero = self
16002            .ctx
16003            .alloc_imperative_expr(Expr::Literal(Literal::Number(0)));
16004        let cond = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
16005            op: BinaryOpKind::Lt,
16006            left: qs_call2,
16007            right: zero,
16008        });
16009        let mut then_body: Vec<Stmt<'a>> = Vec::new();
16010        let create = self.interner.intern("createElement");
16011        let div_lit = self
16012            .ctx
16013            .alloc_imperative_expr(Expr::Literal(Literal::Text(self.interner.intern("div"))));
16014        let create_call = self.ctx.alloc_imperative_expr(Expr::Call {
16015            function: create,
16016            args: vec![div_lit],
16017        });
16018        let mount_sym = self.interner.intern("__mount");
16019        then_body.push(Stmt::Let {
16020            var: mount_sym,
16021            ty: None,
16022            value: create_call,
16023            mutable: false,
16024        });
16025        let mount_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(mount_sym));
16026        let set_attr = self.interner.intern("setAttribute");
16027        let id_key = self
16028            .ctx
16029            .alloc_imperative_expr(Expr::Literal(Literal::Text(self.interner.intern("id"))));
16030        let id_val = self
16031            .ctx
16032            .alloc_imperative_expr(Expr::Literal(Literal::Text(self.interner.intern(&id_str))));
16033        then_body.push(Stmt::Call {
16034            function: set_attr,
16035            args: vec![mount_ref, id_key, id_val],
16036        });
16037        let body_fn = self.interner.intern("body");
16038        let body_call = self.ctx.alloc_imperative_expr(Expr::Call {
16039            function: body_fn,
16040            args: vec![],
16041        });
16042        let append = self.interner.intern("appendChild");
16043        then_body.push(Stmt::Call {
16044            function: append,
16045            args: vec![body_call, mount_ref],
16046        });
16047        let then_block = self.alloc_block(then_body);
16048        mount_body.push(Stmt::If {
16049            cond,
16050            then_block,
16051            else_block: None,
16052        });
16053        mount_body.push(Stmt::Call {
16054            function: render_entry,
16055            args: vec![],
16056        });
16057        let mount_block = self.alloc_block(mount_body);
16058        Ok(Stmt::Splice { body: mount_block })
16059    }
16060
16061    // =========================================================================
16062    // Phase 54: Go-like Concurrency Parser Methods
16063    // =========================================================================
16064
16065    /// Helper: Check if lookahead contains "into" (for Send...into pipe disambiguation)
16066    fn lookahead_contains_into(&self) -> bool {
16067        for i in self.current..std::cmp::min(self.current + 5, self.tokens.len()) {
16068            if matches!(self.tokens[i].kind, TokenType::Into) {
16069                return true;
16070            }
16071        }
16072        false
16073    }
16074
16075    /// Helper: Check if lookahead is "the first of" (for Await select disambiguation)
16076    fn lookahead_is_first_of(&self) -> bool {
16077        // Check for "Await the first of:"
16078        self.current + 3 < self.tokens.len()
16079            && matches!(self.tokens.get(self.current + 1), Some(t) if matches!(t.kind, TokenType::Article(_)))
16080            && self
16081                .tokens
16082                .get(self.current + 2)
16083                .map(|t| self.interner.resolve(t.lexeme).to_lowercase() == "first")
16084                .unwrap_or(false)
16085    }
16086
16087    /// Phase 54: Parse Launch statement - spawn a task
16088    /// Syntax: Launch a task to verb(args).
16089    fn parse_launch_statement(&mut self) -> ParseResult<Stmt<'a>> {
16090        self.advance(); // consume "Launch"
16091
16092        // Expect "a"
16093        if !self.check_article() {
16094            return Err(ParseError {
16095                kind: ParseErrorKind::ExpectedKeyword {
16096                    keyword: "a".to_string(),
16097                },
16098                span: self.current_span(),
16099            });
16100        }
16101        self.advance();
16102
16103        // Expect "task"
16104        if !self.check(&TokenType::Task) {
16105            return Err(ParseError {
16106                kind: ParseErrorKind::ExpectedKeyword {
16107                    keyword: "task".to_string(),
16108                },
16109                span: self.current_span(),
16110            });
16111        }
16112        self.advance();
16113
16114        // Expect "to"
16115        if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
16116            return Err(ParseError {
16117                kind: ParseErrorKind::ExpectedKeyword {
16118                    keyword: "to".to_string(),
16119                },
16120                span: self.current_span(),
16121            });
16122        }
16123        self.advance();
16124
16125        let function = self.parse_task_function()?;
16126
16127        // Optional arguments in parentheses or with "with" keyword
16128        let args = if self.check(&TokenType::LParen) {
16129            self.parse_call_arguments()?
16130        } else if self.check_word("with") {
16131            self.advance(); // consume "with"
16132            self.parse_call_arguments()?
16133        } else {
16134            Vec::new()
16135        };
16136
16137        Ok(Stmt::LaunchTask { function, args })
16138    }
16139
16140    /// A launched task's function: any name this program's own `## To` declared, whatever lexical
16141    /// class the lexer gave it — the rule `Call` follows — else any identifier. Both launch forms
16142    /// read it here, and read the arguments after `with` as `Call` does, so `and` separates them.
16143    fn parse_task_function(&mut self) -> ParseResult<Symbol> {
16144        if self.declared_function_names.contains(&self.peek().lexeme) {
16145            let name = self.peek().lexeme;
16146            self.advance();
16147            return Ok(name);
16148        }
16149        self.expect_identifier()
16150    }
16151
16152    /// Phase 54: Parse Send into pipe statement
16153    /// Syntax: Send value into pipe.
16154    fn parse_send_pipe_statement(&mut self) -> ParseResult<Stmt<'a>> {
16155        self.advance(); // consume "Send"
16156
16157        // Parse value expression
16158        let value = self.parse_imperative_expr()?;
16159
16160        // Expect "into"
16161        if !self.check(&TokenType::Into) {
16162            return Err(ParseError {
16163                kind: ParseErrorKind::ExpectedKeyword {
16164                    keyword: "into".to_string(),
16165                },
16166                span: self.current_span(),
16167            });
16168        }
16169        self.advance();
16170
16171        // Parse pipe expression
16172        let pipe = self.parse_imperative_expr()?;
16173
16174        Ok(Stmt::SendPipe { value, pipe })
16175    }
16176
16177    /// Phase 54: Parse Receive from pipe statement
16178    /// Syntax: Receive x from pipe.
16179    fn parse_receive_pipe_statement(&mut self) -> ParseResult<Stmt<'a>> {
16180        self.advance(); // consume "Receive"
16181
16182        // Get variable name - use expect_identifier which handles various token types
16183        let var = self.expect_identifier()?;
16184
16185        // Expect "from"
16186        if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
16187            return Err(ParseError {
16188                kind: ParseErrorKind::ExpectedKeyword {
16189                    keyword: "from".to_string(),
16190                },
16191                span: self.current_span(),
16192            });
16193        }
16194        self.advance();
16195
16196        // Parse pipe expression
16197        let pipe = self.parse_imperative_expr()?;
16198
16199        Ok(Stmt::ReceivePipe { var, pipe })
16200    }
16201
16202    /// Phase 54: Parse Try statement (non-blocking send/receive)
16203    /// Syntax: Try to send x into pipe. OR Try to receive x from pipe.
16204    fn parse_try_statement(&mut self) -> ParseResult<Stmt<'a>> {
16205        self.advance(); // consume "Try"
16206
16207        // Expect "to"
16208        if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
16209            return Err(ParseError {
16210                kind: ParseErrorKind::ExpectedKeyword {
16211                    keyword: "to".to_string(),
16212                },
16213                span: self.current_span(),
16214            });
16215        }
16216        self.advance();
16217
16218        // Check if send or receive
16219        if self.check(&TokenType::Send) {
16220            self.advance(); // consume "Send"
16221            let value = self.parse_imperative_expr()?;
16222
16223            if !self.check(&TokenType::Into) {
16224                return Err(ParseError {
16225                    kind: ParseErrorKind::ExpectedKeyword {
16226                        keyword: "into".to_string(),
16227                    },
16228                    span: self.current_span(),
16229                });
16230            }
16231            self.advance();
16232
16233            let pipe = self.parse_imperative_expr()?;
16234            Ok(Stmt::TrySendPipe {
16235                value,
16236                pipe,
16237                result: None,
16238            })
16239        } else if self.check(&TokenType::Receive) {
16240            self.advance(); // consume "Receive"
16241
16242            let var = self.expect_identifier()?;
16243
16244            if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
16245                return Err(ParseError {
16246                    kind: ParseErrorKind::ExpectedKeyword {
16247                        keyword: "from".to_string(),
16248                    },
16249                    span: self.current_span(),
16250                });
16251            }
16252            self.advance();
16253
16254            let pipe = self.parse_imperative_expr()?;
16255            Ok(Stmt::TryReceivePipe { var, pipe })
16256        } else {
16257            Err(ParseError {
16258                kind: ParseErrorKind::ExpectedKeyword {
16259                    keyword: "send or receive".to_string(),
16260                },
16261                span: self.current_span(),
16262            })
16263        }
16264    }
16265
16266    /// Shared helper: consume `Escape to <Language>: Indent EscapeBlock Dedent`.
16267    /// Returns (language, code, span).
16268    fn parse_escape_body(
16269        &mut self,
16270    ) -> ParseResult<(
16271        crate::intern::Symbol,
16272        crate::intern::Symbol,
16273        crate::token::Span,
16274    )> {
16275        let start_span = self.current_span();
16276        self.advance(); // consume "Escape"
16277
16278        // Expect "to"
16279        if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
16280            return Err(ParseError {
16281                kind: ParseErrorKind::Custom(
16282                    "Expected 'to' after 'Escape'. Syntax: Escape to Rust:".to_string(),
16283                ),
16284                span: self.current_span(),
16285            });
16286        }
16287        self.advance(); // consume "to"
16288
16289        // Parse language name — "Rust" will be tokenized as ProperName
16290        let language = match &self.peek().kind {
16291            TokenType::ProperName(sym) => {
16292                let s = *sym;
16293                self.advance();
16294                s
16295            }
16296            TokenType::Noun(sym) | TokenType::Adjective(sym) => {
16297                let s = *sym;
16298                self.advance();
16299                s
16300            }
16301            _ => {
16302                return Err(ParseError {
16303                    kind: ParseErrorKind::Custom(
16304                        "Expected language name after 'Escape to'. Currently only 'Rust' is supported.".to_string()
16305                    ),
16306                    span: self.current_span(),
16307                });
16308            }
16309        };
16310
16311        // Validate: only "Rust" is supported for now
16312        if !language.is(self.interner, "Rust") {
16313            let lang_str = self.interner.resolve(language);
16314            return Err(ParseError {
16315                kind: ParseErrorKind::Custom(format!(
16316                    "Unsupported escape target '{}'. Only 'Rust' is supported.",
16317                    lang_str
16318                )),
16319                span: self.current_span(),
16320            });
16321        }
16322
16323        // Expect colon
16324        if !self.check(&TokenType::Colon) {
16325            return Err(ParseError {
16326                kind: ParseErrorKind::Custom(
16327                    "Expected ':' after 'Escape to Rust'. Syntax: Escape to Rust:".to_string(),
16328                ),
16329                span: self.current_span(),
16330            });
16331        }
16332        self.advance(); // consume ":"
16333
16334        // Expect Indent (the indented block follows)
16335        if !self.check(&TokenType::Indent) {
16336            return Err(ParseError {
16337                kind: ParseErrorKind::Custom(
16338                    "Expected indented block after 'Escape to Rust:'.".to_string(),
16339                ),
16340                span: self.current_span(),
16341            });
16342        }
16343        self.advance(); // consume Indent
16344
16345        // Expect the raw EscapeBlock token
16346        let code = match &self.peek().kind {
16347            TokenType::EscapeBlock(sym) => {
16348                let s = *sym;
16349                self.advance();
16350                s
16351            }
16352            _ => {
16353                return Err(ParseError {
16354                    kind: ParseErrorKind::Custom(
16355                        "Escape block body is empty or malformed.".to_string(),
16356                    ),
16357                    span: self.current_span(),
16358                });
16359            }
16360        };
16361
16362        // Expect Dedent (block ends)
16363        if self.check(&TokenType::Dedent) {
16364            self.advance();
16365        }
16366
16367        let end_span = self.previous().span;
16368        Ok((
16369            language,
16370            code,
16371            crate::token::Span::new(start_span.start, end_span.end),
16372        ))
16373    }
16374
16375    /// Parse an escape hatch block: `Escape to Rust: <indented raw code>`
16376    fn parse_escape_statement(&mut self) -> ParseResult<Stmt<'a>> {
16377        let (language, code, span) = self.parse_escape_body()?;
16378        Ok(Stmt::Escape {
16379            language,
16380            code,
16381            span,
16382        })
16383    }
16384
16385    /// Parse an escape hatch expression: `Escape to Rust: <indented raw code>`
16386    /// Used in expression position: `Let x: Int be Escape to Rust:`
16387    fn parse_escape_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
16388        let (language, code, _span) = self.parse_escape_body()?;
16389        Ok(self
16390            .ctx
16391            .alloc_imperative_expr(Expr::Escape { language, code }))
16392    }
16393
16394    /// Parse a `## Requires` block into a list of `Stmt::Require` nodes.
16395    /// Loops until the next block header or EOF, parsing one dependency per line.
16396    fn parse_requires_block(&mut self) -> ParseResult<Vec<Stmt<'a>>> {
16397        let mut deps = Vec::new();
16398
16399        loop {
16400            // Stop at next block header or EOF
16401            if self.is_at_end() {
16402                break;
16403            }
16404            if matches!(self.peek().kind, TokenType::BlockHeader { .. }) {
16405                break;
16406            }
16407
16408            // Skip whitespace tokens
16409            if self.check(&TokenType::Indent)
16410                || self.check(&TokenType::Dedent)
16411                || self.check(&TokenType::Newline)
16412            {
16413                self.advance();
16414                continue;
16415            }
16416
16417            // Each dependency line starts with an article ("The")
16418            if matches!(self.peek().kind, TokenType::Article(_)) {
16419                let dep = self.parse_require_line()?;
16420                deps.push(dep);
16421                continue;
16422            }
16423
16424            // Skip unexpected tokens (defensive)
16425            self.advance();
16426        }
16427
16428        Ok(deps)
16429    }
16430
16431    /// Parse a single dependency line:
16432    /// `The "serde" crate version "1.0" with features "derive" and "std" for serialization.`
16433    fn parse_require_line(&mut self) -> ParseResult<Stmt<'a>> {
16434        let start_span = self.current_span();
16435
16436        // Expect article "The"
16437        if !matches!(self.peek().kind, TokenType::Article(_)) {
16438            return Err(crate::error::ParseError {
16439                kind: crate::error::ParseErrorKind::Custom(
16440                    "Expected 'The' to begin a dependency declaration.".to_string(),
16441                ),
16442                span: self.current_span(),
16443            });
16444        }
16445        self.advance(); // consume "The"
16446
16447        // Expect string literal for crate name
16448        let crate_name = if let TokenType::StringLiteral(sym) = self.peek().kind {
16449            let s = sym;
16450            self.advance();
16451            s
16452        } else {
16453            return Err(crate::error::ParseError {
16454                kind: crate::error::ParseErrorKind::Custom(
16455                    "Expected a string literal for the crate name, e.g. \"serde\".".to_string(),
16456                ),
16457                span: self.current_span(),
16458            });
16459        };
16460
16461        // Expect word "crate"
16462        if !self.check_word("crate") {
16463            return Err(crate::error::ParseError {
16464                kind: crate::error::ParseErrorKind::Custom(
16465                    "Expected the word 'crate' after the crate name.".to_string(),
16466                ),
16467                span: self.current_span(),
16468            });
16469        }
16470        self.advance(); // consume "crate"
16471
16472        // Expect word "version"
16473        if !self.check_word("version") {
16474            return Err(crate::error::ParseError {
16475                kind: crate::error::ParseErrorKind::Custom(
16476                    "Expected 'version' after 'crate'.".to_string(),
16477                ),
16478                span: self.current_span(),
16479            });
16480        }
16481        self.advance(); // consume "version"
16482
16483        // Expect string literal for version
16484        let version = if let TokenType::StringLiteral(sym) = self.peek().kind {
16485            let s = sym;
16486            self.advance();
16487            s
16488        } else {
16489            return Err(crate::error::ParseError {
16490                kind: crate::error::ParseErrorKind::Custom(
16491                    "Expected a string literal for the version, e.g. \"1.0\".".to_string(),
16492                ),
16493                span: self.current_span(),
16494            });
16495        };
16496
16497        // Optional: "with features ..."
16498        let mut features = Vec::new();
16499        if self.check_preposition_is("with") {
16500            self.advance(); // consume "with"
16501
16502            // Expect word "features"
16503            if !self.check_word("features") {
16504                return Err(crate::error::ParseError {
16505                    kind: crate::error::ParseErrorKind::Custom(
16506                        "Expected 'features' after 'with'.".to_string(),
16507                    ),
16508                    span: self.current_span(),
16509                });
16510            }
16511            self.advance(); // consume "features"
16512
16513            // Parse first feature string
16514            if let TokenType::StringLiteral(sym) = self.peek().kind {
16515                features.push(sym);
16516                self.advance();
16517            } else {
16518                return Err(crate::error::ParseError {
16519                    kind: crate::error::ParseErrorKind::Custom(
16520                        "Expected a string literal for a feature name.".to_string(),
16521                    ),
16522                    span: self.current_span(),
16523                });
16524            }
16525
16526            // Parse additional features separated by "and"
16527            while self.check(&TokenType::And) {
16528                self.advance(); // consume "and"
16529                if let TokenType::StringLiteral(sym) = self.peek().kind {
16530                    features.push(sym);
16531                    self.advance();
16532                } else {
16533                    return Err(crate::error::ParseError {
16534                        kind: crate::error::ParseErrorKind::Custom(
16535                            "Expected a string literal for a feature name after 'and'.".to_string(),
16536                        ),
16537                        span: self.current_span(),
16538                    });
16539                }
16540            }
16541        }
16542
16543        // Optional: "for <description...>" — consume until period
16544        if self.check(&TokenType::For) {
16545            self.advance(); // consume "for"
16546            while !self.check(&TokenType::Period)
16547                && !self.check(&TokenType::EOF)
16548                && !self.check(&TokenType::Newline)
16549                && !matches!(self.peek().kind, TokenType::BlockHeader { .. })
16550            {
16551                self.advance();
16552            }
16553        }
16554
16555        // Consume trailing period
16556        if self.check(&TokenType::Period) {
16557            self.advance();
16558        }
16559
16560        let end_span = self.previous().span;
16561
16562        Ok(Stmt::Require {
16563            crate_name,
16564            version,
16565            features,
16566            span: crate::token::Span::new(start_span.start, end_span.end),
16567        })
16568    }
16569
16570    /// Phase 54: Parse Stop statement
16571    /// Syntax: Stop handle.
16572    fn parse_stop_statement(&mut self) -> ParseResult<Stmt<'a>> {
16573        self.advance(); // consume "Stop"
16574
16575        let handle = self.parse_imperative_expr()?;
16576
16577        Ok(Stmt::StopTask { handle })
16578    }
16579
16580    /// Is this `Await` a task-handle join (`Await h` / `Await h into r`) rather than the relay
16581    /// receive (`Await response|view|stream … from …`) or a select (`Await the first of:`)?
16582    /// A task join is the bare form: no receive knob word right after `Await`, and no `from`
16583    /// clause (a relay receive always names its `from` source).
16584    fn lookahead_is_task_await(&self) -> bool {
16585        // A knob word (`response`/`view`/`viewed`/`stream`) or an article (`the first of`)
16586        // means this is NOT a bare handle join.
16587        if self.check_word_at(self.current + 1, "response")
16588            || self.check_word_at(self.current + 1, "view")
16589            || self.check_word_at(self.current + 1, "viewed")
16590            || self.check_word_at(self.current + 1, "stream")
16591        {
16592            return false;
16593        }
16594        // A `from` anywhere in the immediate window marks a relay receive.
16595        for i in (self.current + 1)..std::cmp::min(self.current + 5, self.tokens.len()) {
16596            let t = &self.tokens[i];
16597            if matches!(t.kind, TokenType::From)
16598                || self.interner.resolve(t.lexeme).eq_ignore_ascii_case("from")
16599            {
16600                return false;
16601            }
16602        }
16603        true
16604    }
16605
16606    /// Helper: case-insensitive lexeme check at an absolute token index.
16607    fn check_word_at(&self, idx: usize, word: &str) -> bool {
16608        self.tokens
16609            .get(idx)
16610            .map(|t| self.interner.resolve(t.lexeme).eq_ignore_ascii_case(word))
16611            .unwrap_or(false)
16612    }
16613
16614    /// Parse the task-handle join spelled with `Await`: `Await h.` / `Await h into r.`
16615    fn parse_task_await_statement(&mut self) -> ParseResult<Stmt<'a>> {
16616        self.advance(); // consume "Await"
16617        self.finish_task_await()
16618    }
16619
16620    /// Parse the task-handle join spelled with `Wait`: `Wait for h.` / `Wait for h into r.`
16621    /// The `for` is optional (`Wait h` also joins).
16622    fn parse_wait_statement(&mut self) -> ParseResult<Stmt<'a>> {
16623        self.advance(); // consume "Wait"
16624        if self.check(&TokenType::For) || self.check_preposition_is("for") || self.check_word("for")
16625        {
16626            self.advance(); // consume optional "for"
16627        }
16628        self.finish_task_await()
16629    }
16630
16631    /// Shared tail for `Await h`/`Wait for h`: the handle expression plus an optional
16632    /// `into r` result binding.
16633    fn finish_task_await(&mut self) -> ParseResult<Stmt<'a>> {
16634        let handle = self.parse_imperative_expr()?;
16635        let result = if self.check(&TokenType::Into) || self.check_word("into") {
16636            self.advance(); // consume "into"
16637            Some(self.expect_identifier()?)
16638        } else {
16639            None
16640        };
16641        Ok(Stmt::AwaitTask { handle, result })
16642    }
16643
16644    /// Parse a channel-close statement: `Close ch.` or `Close the pipe ch.`
16645    fn parse_close_statement(&mut self) -> ParseResult<Stmt<'a>> {
16646        self.advance(); // consume "Close"
16647                        // Optional `the`/`a` article and the `pipe` noun: `Close the pipe ch`.
16648        if self.check_article() {
16649            self.advance();
16650        }
16651        if self.check(&TokenType::Pipe) || self.check_word("channel") {
16652            self.advance();
16653        }
16654        let channel = self.parse_imperative_expr()?;
16655        Ok(Stmt::CloseChannel { channel })
16656    }
16657
16658    /// Phase 54: Parse Select statement
16659    /// Syntax:
16660    /// Await the first of:
16661    ///     Receive x from pipe:
16662    ///         ...
16663    ///     After N seconds:
16664    ///         ...
16665    fn parse_select_statement(&mut self) -> ParseResult<Stmt<'a>> {
16666        use crate::ast::stmt::SelectBranch;
16667
16668        self.advance(); // consume "Await"
16669
16670        // Expect "the"
16671        if !self.check_article() {
16672            return Err(ParseError {
16673                kind: ParseErrorKind::ExpectedKeyword {
16674                    keyword: "the".to_string(),
16675                },
16676                span: self.current_span(),
16677            });
16678        }
16679        self.advance();
16680
16681        // Expect "first"
16682        if !self.check_word("first") {
16683            return Err(ParseError {
16684                kind: ParseErrorKind::ExpectedKeyword {
16685                    keyword: "first".to_string(),
16686                },
16687                span: self.current_span(),
16688            });
16689        }
16690        self.advance();
16691
16692        // Expect "of"
16693        if !self.check_preposition_is("of") {
16694            return Err(ParseError {
16695                kind: ParseErrorKind::ExpectedKeyword {
16696                    keyword: "of".to_string(),
16697                },
16698                span: self.current_span(),
16699            });
16700        }
16701        self.advance();
16702
16703        // Expect colon
16704        if !self.check(&TokenType::Colon) {
16705            return Err(ParseError {
16706                kind: ParseErrorKind::ExpectedKeyword {
16707                    keyword: ":".to_string(),
16708                },
16709                span: self.current_span(),
16710            });
16711        }
16712        self.advance();
16713
16714        // Expect indent
16715        if !self.check(&TokenType::Indent) {
16716            return Err(ParseError {
16717                kind: ParseErrorKind::ExpectedStatement,
16718                span: self.current_span(),
16719            });
16720        }
16721        self.advance();
16722
16723        // Parse branches
16724        let mut branches = Vec::new();
16725        while !self.check(&TokenType::Dedent) && !self.is_at_end() {
16726            let branch = self.parse_select_branch()?;
16727            branches.push(branch);
16728        }
16729
16730        // Consume dedent
16731        if self.check(&TokenType::Dedent) {
16732            self.advance();
16733        }
16734
16735        Ok(Stmt::Select { branches })
16736    }
16737
16738    /// Phase 54: Parse a single select branch
16739    fn parse_select_branch(&mut self) -> ParseResult<crate::ast::stmt::SelectBranch<'a>> {
16740        use crate::ast::stmt::SelectBranch;
16741
16742        if self.check(&TokenType::Receive) {
16743            self.advance(); // consume "Receive"
16744
16745            let var = match &self.tokens[self.current].kind {
16746                TokenType::ProperName(sym) | TokenType::Noun(sym) | TokenType::Adjective(sym) => {
16747                    let s = *sym;
16748                    self.advance();
16749                    s
16750                }
16751                _ => {
16752                    return Err(ParseError {
16753                        kind: ParseErrorKind::ExpectedKeyword {
16754                            keyword: "variable name".to_string(),
16755                        },
16756                        span: self.current_span(),
16757                    });
16758                }
16759            };
16760
16761            if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
16762                return Err(ParseError {
16763                    kind: ParseErrorKind::ExpectedKeyword {
16764                        keyword: "from".to_string(),
16765                    },
16766                    span: self.current_span(),
16767                });
16768            }
16769            self.advance();
16770
16771            let pipe = self.parse_imperative_expr()?;
16772
16773            // Expect colon
16774            if !self.check(&TokenType::Colon) {
16775                return Err(ParseError {
16776                    kind: ParseErrorKind::ExpectedKeyword {
16777                        keyword: ":".to_string(),
16778                    },
16779                    span: self.current_span(),
16780                });
16781            }
16782            self.advance();
16783
16784            // Parse body
16785            let body = self.parse_indented_block()?;
16786
16787            Ok(SelectBranch::Receive { var, pipe, body })
16788        } else if self.check_word("after") {
16789            self.advance(); // consume "After"
16790
16791            let milliseconds = self.parse_imperative_expr()?;
16792
16793            // Skip "seconds" or "milliseconds" if present
16794            if self.check_word("seconds") || self.check_word("milliseconds") {
16795                self.advance();
16796            }
16797
16798            // Expect colon
16799            if !self.check(&TokenType::Colon) {
16800                return Err(ParseError {
16801                    kind: ParseErrorKind::ExpectedKeyword {
16802                        keyword: ":".to_string(),
16803                    },
16804                    span: self.current_span(),
16805                });
16806            }
16807            self.advance();
16808
16809            // Parse body
16810            let body = self.parse_indented_block()?;
16811
16812            Ok(SelectBranch::Timeout { milliseconds, body })
16813        } else {
16814            Err(ParseError {
16815                kind: ParseErrorKind::ExpectedKeyword {
16816                    keyword: "Receive or After".to_string(),
16817                },
16818                span: self.current_span(),
16819            })
16820        }
16821    }
16822
16823    /// Phase 54: Parse an indented block of statements
16824    /// Depth-guarded wrapper: nested blocks recurse here once per level, so
16825    /// a block pyramid errors gracefully instead of overflowing the parser.
16826    fn parse_indented_block(&mut self) -> ParseResult<crate::ast::stmt::Block<'a>> {
16827        self.enter_recursion()?;
16828        let result = self.parse_indented_block_inner();
16829        self.leave_recursion();
16830        result
16831    }
16832
16833    fn parse_indented_block_inner(&mut self) -> ParseResult<crate::ast::stmt::Block<'a>> {
16834        // Expect indent
16835        if !self.check(&TokenType::Indent) {
16836            return Err(ParseError {
16837                kind: ParseErrorKind::ExpectedStatement,
16838                span: self.current_span(),
16839            });
16840        }
16841        self.advance();
16842
16843        let list = self.read_scoped(|parser| {
16844            let mut stmts = StmtList::default();
16845            while !parser.check(&TokenType::Dedent) && !parser.is_at_end() {
16846                let start_tok = parser.current;
16847                let mut read = StmtList::default();
16848                parser.read_block_statement(&mut read)?;
16849                // ★ A FAILURE-SITE MARKER, ONE PER SENTENCE, ONLY WHILE PARSING AN ACTION/HANDLER'S
16850                // OWN BODY (`site_tracking`, set by `parse_actions_section` around exactly the two
16851                // body-parsing calls it makes). Inserted BEFORE the sentence it marks, in spliced
16852                // coordinates, sized to include the terminating period just consumed — the same span
16853                // shape `record_stmt_span` uses. A nested `{if}`/`{repeat}` inside an action's body
16854                // recurses back into this same function while `site_tracking` is still set, so its own
16855                // sentences get marked too, at no extra wiring.
16856                if let Some((component, action)) = parser.site_tracking {
16857                    let span = parser.span_from(start_tok);
16858                    let id = parser.failure_sites.len() as u32;
16859                    parser.failure_sites.push(crate::ast::stmt::FailureSite {
16860                        span,
16861                        component,
16862                        action,
16863                    });
16864                    stmts.wrote(Stmt::Site(id));
16865                }
16866                stmts.append(read);
16867            }
16868
16869            // Consume dedent
16870            if parser.check(&TokenType::Dedent) {
16871                parser.advance();
16872            }
16873            Ok(stmts)
16874        })?;
16875
16876        Ok(self.alloc_read_block(list))
16877    }
16878
16879    fn parse_give_statement(&mut self) -> ParseResult<Stmt<'a>> {
16880        self.advance(); // consume "Give"
16881
16882        // Parse the object being given: "x" or "the data"
16883        let object = self.parse_imperative_expr()?;
16884
16885        // Expect "to" preposition (can be TokenType::To when followed by verb-like word)
16886        if !self.check_to_preposition() {
16887            return Err(ParseError {
16888                kind: ParseErrorKind::ExpectedKeyword {
16889                    keyword: "to".to_string(),
16890                },
16891                span: self.current_span(),
16892            });
16893        }
16894        self.advance(); // consume "to"
16895
16896        // Parse the recipient: "processor" or "the console"
16897        let recipient = self.parse_imperative_expr()?;
16898
16899        // The move itself is settled by the block reading this statement
16900        // (`parse_moving_statement`): it lasts for the rest of that block.
16901        Ok(Stmt::Give { object, recipient })
16902    }
16903
16904    fn parse_show_statement(&mut self) -> ParseResult<Stmt<'a>> {
16905        self.advance(); // consume "Show"
16906
16907        // Parse the object being shown - use parse_condition to support
16908        // comparisons (x is less than y) and boolean operators (a and b)
16909        let object = self.parse_condition()?;
16910
16911        // Optional "to" preposition - if not present, default to "show" function
16912        // Note: Use check_to_preposition() to handle both TokenType::To (when followed by verb)
16913        // and TokenType::Preposition("to")
16914        let recipient = if self.check_to_preposition() {
16915            self.advance(); // consume "to"
16916
16917            // Phase 10: "Show x to console." or "Show x to the console."
16918            // is idiomatic for printing to stdout - use default show function
16919            if self.check_article() {
16920                self.advance(); // skip "the"
16921            }
16922            if self.check(&TokenType::Console) {
16923                self.advance(); // consume "console"
16924                let show_sym = self.interner.intern("show");
16925                self.ctx.alloc_imperative_expr(Expr::Identifier(show_sym))
16926            } else {
16927                // Parse the recipient: custom function
16928                self.parse_imperative_expr()?
16929            }
16930        } else {
16931            // Default recipient: the runtime "show" function
16932            let show_sym = self.interner.intern("show");
16933            self.ctx.alloc_imperative_expr(Expr::Identifier(show_sym))
16934        };
16935
16936        // Mark variable as Borrowed after Show
16937        if let Expr::Identifier(sym) = object {
16938            self.world_state
16939                .set_ownership_by_var(*sym, crate::drs::OwnershipState::Borrowed);
16940        }
16941
16942        Ok(Stmt::Show { object, recipient })
16943    }
16944
16945    /// Phase 43D: Parse Push statement for collection operations
16946    /// Syntax: Push x to items.
16947    fn parse_push_statement(&mut self) -> ParseResult<Stmt<'a>> {
16948        self.advance(); // consume "Push"
16949
16950        // Parse the value(s) being pushed — `Push a, b, c to xs.` appends each
16951        // in order (desugars to one Push per element).
16952        let mut values = vec![self.parse_imperative_expr()?];
16953        while self.check(&TokenType::Comma) {
16954            self.advance(); // consume ","
16955            values.push(self.parse_imperative_expr()?);
16956        }
16957
16958        // Expect "to" (can be keyword or preposition)
16959        if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
16960            return Err(ParseError {
16961                kind: ParseErrorKind::ExpectedKeyword {
16962                    keyword: "to".to_string(),
16963                },
16964                span: self.current_span(),
16965            });
16966        }
16967        self.advance(); // consume "to"
16968
16969        // Parse the collection
16970        let collection_from = self.current;
16971        let collection = self.parse_imperative_expr()?;
16972        self.note_write(
16973            collection,
16974            collection_from,
16975            self.current,
16976            component::WriteVerb::Push,
16977        );
16978
16979        if values.len() == 1 {
16980            Ok(self.desugar_place_push(values[0], collection))
16981        } else {
16982            let mut pushes = Vec::with_capacity(values.len());
16983            for value in values {
16984                pushes.push(self.desugar_place_push(value, collection));
16985            }
16986            let body = self.alloc_block(pushes);
16987            Ok(Stmt::Splice { body })
16988        }
16989    }
16990
16991    /// Phase 43D: Parse Pop statement for collection operations
16992    /// Syntax: Pop from items. OR Pop from items into y.
16993    fn parse_pop_statement(&mut self) -> ParseResult<Stmt<'a>> {
16994        self.advance(); // consume "Pop"
16995
16996        // Expect "from" - can be keyword token or preposition
16997        if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
16998            return Err(ParseError {
16999                kind: ParseErrorKind::ExpectedKeyword {
17000                    keyword: "from".to_string(),
17001                },
17002                span: self.current_span(),
17003            });
17004        }
17005        self.advance(); // consume "from"
17006
17007        // Parse the collection
17008        let collection_from = self.current;
17009        let collection = self.parse_imperative_expr()?;
17010        self.note_write(
17011            collection,
17012            collection_from,
17013            self.current,
17014            component::WriteVerb::Pop,
17015        );
17016
17017        // Check for optional "into" binding (can be Into keyword or preposition)
17018        let into = if self.check(&TokenType::Into) || self.check_preposition_is("into") {
17019            self.advance(); // consume "into"
17020
17021            // Parse variable name
17022            if let TokenType::Noun(sym) | TokenType::ProperName(sym) = &self.peek().kind {
17023                let sym = *sym;
17024                self.advance();
17025                Some(sym)
17026            } else if let Some(token) = self.tokens.get(self.current) {
17027                // Also handle identifier-like tokens
17028                let sym = token.lexeme;
17029                self.advance();
17030                Some(sym)
17031            } else {
17032                return Err(ParseError {
17033                    kind: ParseErrorKind::ExpectedIdentifier,
17034                    span: self.current_span(),
17035                });
17036            }
17037        } else {
17038            None
17039        };
17040
17041        Ok(Stmt::Pop { collection, into })
17042    }
17043
17044    /// Parse Add statement for Set insertion
17045    /// Syntax: Add x to set.
17046    fn parse_add_statement(&mut self) -> ParseResult<Stmt<'a>> {
17047        self.advance(); // consume "Add"
17048
17049        // Parse the value to add
17050        let value = self.parse_imperative_expr()?;
17051
17052        // Expect "to" preposition
17053        if !self.check_preposition_is("to") && !self.check(&TokenType::To) {
17054            return Err(ParseError {
17055                kind: ParseErrorKind::ExpectedKeyword {
17056                    keyword: "to".to_string(),
17057                },
17058                span: self.current_span(),
17059            });
17060        }
17061        self.advance(); // consume "to"
17062
17063        // Parse the collection expression
17064        let collection_from = self.current;
17065        let collection = self.parse_imperative_expr()?;
17066        self.note_write(
17067            collection,
17068            collection_from,
17069            self.current,
17070            component::WriteVerb::Add,
17071        );
17072
17073        Ok(Stmt::Add { value, collection })
17074    }
17075
17076    /// Parse Remove statement for Set deletion
17077    /// Syntax: Remove x from set.
17078    fn parse_remove_statement(&mut self) -> ParseResult<Stmt<'a>> {
17079        self.advance(); // consume "Remove"
17080
17081        // `Remove every <x> from <list> where <condition>.` — every item the condition holds for
17082        // leaves the list, the rest keep their order: the kept items gathered into a copy
17083        // (`desugar_keep_where`), written back through the same place a `Set` of `<list>` writes.
17084        if self.check_word("every") && self.check_word_at(self.current + 2, "from") {
17085            let saved = self.backtrack_point();
17086            self.advance(); // every
17087            if let Ok(binder) = self.expect_identifier() {
17088                self.advance(); // from
17089                let list_from = self.current;
17090                let list = self.parse_imperative_expr()?;
17091                let list_to = self.current;
17092                self.expect_keyword("where")?;
17093                let condition = self.parse_condition()?;
17094                let (kept, mut body) = self.desugar_keep_where(binder, list, condition, false);
17095                let kept_ref = self.ctx.alloc_imperative_expr(Expr::Identifier(kept));
17096                let write_back = match list {
17097                    Expr::Identifier(target) => {
17098                        self.note_write(list, list_from, list_to, component::WriteVerb::Remove);
17099                        Stmt::Set { target: *target, value: kept_ref }
17100                    }
17101                    Expr::FieldAccess { object, field } => {
17102                        self.note_write(list, list_from, list_to, component::WriteVerb::Remove);
17103                        Stmt::SetField { object: *object, field: *field, value: kept_ref }
17104                    }
17105                    Expr::Index { collection, index } => {
17106                        self.note_write(list, list_from, list_to, component::WriteVerb::Remove);
17107                        self.desugar_place_set_index(*collection, *index, kept_ref)
17108                    }
17109                    _ => {
17110                        return Err(ParseError {
17111                            kind: ParseErrorKind::Custom(
17112                                "`Remove every … from` takes items out of a list it can write back to — \
17113                                 a name, a field (`the cart's items`) or an item (`item 2 of lists`)."
17114                                    .to_string(),
17115                            ),
17116                            span: self.tokens[list_from].span,
17117                        })
17118                    }
17119                };
17120                body.push(write_back);
17121                let body = self.alloc_block(body);
17122                return Ok(Stmt::Splice { body });
17123            }
17124            self.backtrack_to(&saved);
17125        }
17126
17127        // Parse the value to remove
17128        let value = self.parse_imperative_expr()?;
17129
17130        // Expect "from" preposition
17131        if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
17132            return Err(ParseError {
17133                kind: ParseErrorKind::ExpectedKeyword {
17134                    keyword: "from".to_string(),
17135                },
17136                span: self.current_span(),
17137            });
17138        }
17139        self.advance(); // consume "from"
17140
17141        // Parse the collection expression
17142        let collection_from = self.current;
17143        let collection = self.parse_imperative_expr()?;
17144        self.note_write(
17145            collection,
17146            collection_from,
17147            self.current,
17148            component::WriteVerb::Remove,
17149        );
17150
17151        Ok(Stmt::Remove { value, collection })
17152    }
17153
17154    /// Phase 10: Parse Read statement for console/file input
17155    /// Syntax: Read <var> from the console.
17156    ///         Read <var> from file <path>.
17157    fn parse_read_statement(&mut self) -> ParseResult<Stmt<'a>> {
17158        self.advance(); // consume "Read"
17159
17160        // Get the variable name
17161        let var = self.expect_identifier()?;
17162
17163        // Expect "from" preposition
17164        if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
17165            return Err(ParseError {
17166                kind: ParseErrorKind::ExpectedKeyword {
17167                    keyword: "from".to_string(),
17168                },
17169                span: self.current_span(),
17170            });
17171        }
17172        self.advance(); // consume "from"
17173
17174        // Skip optional article "the"
17175        if self.check_article() {
17176            self.advance();
17177        }
17178
17179        // Determine source: console or file
17180        let source = if self.check(&TokenType::Console) {
17181            self.advance(); // consume "console"
17182            ReadSource::Console
17183        } else if self.check(&TokenType::File) {
17184            self.advance(); // consume "file"
17185            let path = self.parse_imperative_expr()?;
17186            ReadSource::File(path)
17187        } else {
17188            return Err(ParseError {
17189                kind: ParseErrorKind::ExpectedKeyword {
17190                    keyword: "console or file".to_string(),
17191                },
17192                span: self.current_span(),
17193            });
17194        };
17195
17196        Ok(Stmt::ReadFrom { var, source })
17197    }
17198
17199    /// Phase 10: Parse Write statement for file output
17200    /// Syntax: Write <content> to file <path>.
17201    fn parse_write_statement(&mut self) -> ParseResult<Stmt<'a>> {
17202        self.advance(); // consume "Write"
17203
17204        // Parse the content expression
17205        let content = self.parse_imperative_expr()?;
17206
17207        // Expect "to" (can be keyword or preposition)
17208        if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
17209            return Err(ParseError {
17210                kind: ParseErrorKind::ExpectedKeyword {
17211                    keyword: "to".to_string(),
17212                },
17213                span: self.current_span(),
17214            });
17215        }
17216        self.advance(); // consume "to"
17217
17218        // Expect "file" keyword
17219        if !self.check(&TokenType::File) {
17220            return Err(ParseError {
17221                kind: ParseErrorKind::ExpectedKeyword {
17222                    keyword: "file".to_string(),
17223                },
17224                span: self.current_span(),
17225            });
17226        }
17227        self.advance(); // consume "file"
17228
17229        // Parse the path expression
17230        let path = self.parse_imperative_expr()?;
17231
17232        Ok(Stmt::WriteFile { content, path })
17233    }
17234
17235    /// Phase 8.5: Parse Zone statement for memory arena blocks
17236    /// Syntax variants:
17237    ///   - Inside a new zone called "Scratch":
17238    ///   - Inside a zone called "Buffer" of size 1 MB:
17239    ///   - Inside a zone called "Data" mapped from "file.bin":
17240    fn parse_zone_statement(&mut self) -> ParseResult<Stmt<'a>> {
17241        self.advance(); // consume "Inside"
17242
17243        // Optional article "a"
17244        if self.check_article() {
17245            self.advance();
17246        }
17247
17248        // Optional "new"
17249        if self.check(&TokenType::New) {
17250            self.advance();
17251        }
17252
17253        // Expect "zone"
17254        if !self.check(&TokenType::Zone) {
17255            return Err(ParseError {
17256                kind: ParseErrorKind::ExpectedKeyword {
17257                    keyword: "zone".to_string(),
17258                },
17259                span: self.current_span(),
17260            });
17261        }
17262        self.advance(); // consume "zone"
17263
17264        // Expect "called"
17265        if !self.check(&TokenType::Called) {
17266            return Err(ParseError {
17267                kind: ParseErrorKind::ExpectedKeyword {
17268                    keyword: "called".to_string(),
17269                },
17270                span: self.current_span(),
17271            });
17272        }
17273        self.advance(); // consume "called"
17274
17275        // Parse zone name (can be string literal or identifier)
17276        let name = match &self.peek().kind {
17277            TokenType::StringLiteral(sym) => {
17278                let s = *sym;
17279                self.advance();
17280                s
17281            }
17282            TokenType::ProperName(sym) | TokenType::Noun(sym) | TokenType::Adjective(sym) => {
17283                let s = *sym;
17284                self.advance();
17285                s
17286            }
17287            _ => {
17288                // Try to use the lexeme directly as an identifier
17289                let token = self.peek().clone();
17290                self.advance();
17291                token.lexeme
17292            }
17293        };
17294
17295        let mut capacity = None;
17296        let mut source_file = None;
17297
17298        // Check for "mapped from" (file-backed zone)
17299        if self.check(&TokenType::Mapped) {
17300            self.advance(); // consume "mapped"
17301
17302            // Expect "from"
17303            if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
17304                return Err(ParseError {
17305                    kind: ParseErrorKind::ExpectedKeyword {
17306                        keyword: "from".to_string(),
17307                    },
17308                    span: self.current_span(),
17309                });
17310            }
17311            self.advance(); // consume "from"
17312
17313            // Parse file path — a string literal, or a variable holding the path
17314            // (the path is often known only at runtime, e.g. a function parameter).
17315            if let TokenType::StringLiteral(path) = &self.peek().kind {
17316                let p = *path;
17317                self.advance();
17318                source_file = Some(crate::ast::stmt::ZoneSource::Literal(p));
17319            } else {
17320                let var = self.expect_identifier()?;
17321                source_file = Some(crate::ast::stmt::ZoneSource::Variable(var));
17322            }
17323        }
17324        // Check for "of size N Unit" (sized heap zone)
17325        else if self.check_of_preposition() {
17326            self.advance(); // consume "of"
17327
17328            // Expect "size"
17329            if !self.check(&TokenType::Size) {
17330                return Err(ParseError {
17331                    kind: ParseErrorKind::ExpectedKeyword {
17332                        keyword: "size".to_string(),
17333                    },
17334                    span: self.current_span(),
17335                });
17336            }
17337            self.advance(); // consume "size"
17338
17339            // Parse size number
17340            let size_value = match &self.peek().kind {
17341                TokenType::Number(sym) => {
17342                    let num_str = self.interner.resolve(*sym).to_string();
17343                    let val = self.parse_i64_numeral(&num_str)? as usize;
17344                    self.advance();
17345                    val
17346                }
17347                TokenType::Cardinal(n) => {
17348                    let val = *n as usize;
17349                    self.advance();
17350                    val
17351                }
17352                _ => {
17353                    return Err(ParseError {
17354                        kind: ParseErrorKind::ExpectedNumber,
17355                        span: self.current_span(),
17356                    });
17357                }
17358            };
17359
17360            // Parse unit (KB, MB, GB, or B)
17361            let unit_multiplier = self.parse_size_unit()?;
17362            capacity = Some(size_value * unit_multiplier);
17363        }
17364
17365        // Expect colon
17366        if !self.check(&TokenType::Colon) {
17367            return Err(ParseError {
17368                kind: ParseErrorKind::ExpectedKeyword {
17369                    keyword: ":".to_string(),
17370                },
17371                span: self.current_span(),
17372            });
17373        }
17374        self.advance(); // consume ":"
17375
17376        let body = self.read_indented_block()?;
17377
17378        Ok(Stmt::Zone {
17379            name,
17380            capacity,
17381            source_file,
17382            body,
17383        })
17384    }
17385
17386    /// Parse size unit (B, KB, MB, GB) and return multiplier
17387    fn parse_size_unit(&mut self) -> ParseResult<usize> {
17388        let token = self.peek().clone();
17389        let unit_str = self.interner.resolve(token.lexeme).to_uppercase();
17390        self.advance();
17391
17392        match unit_str.as_str() {
17393            "B" | "BYTES" | "BYTE" => Ok(1),
17394            "KB" | "KILOBYTE" | "KILOBYTES" => Ok(1024),
17395            "MB" | "MEGABYTE" | "MEGABYTES" => Ok(1024 * 1024),
17396            "GB" | "GIGABYTE" | "GIGABYTES" => Ok(1024 * 1024 * 1024),
17397            _ => Err(ParseError {
17398                kind: ParseErrorKind::ExpectedKeyword {
17399                    keyword: "size unit (B, KB, MB, GB)".to_string(),
17400                },
17401                span: token.span,
17402            }),
17403        }
17404    }
17405
17406    /// Phase 9: Parse concurrent execution block (async, I/O-bound)
17407    ///
17408    /// Syntax:
17409    /// ```logos
17410    /// Attempt all of the following:
17411    ///     Call fetch_user with id.
17412    ///     Call fetch_orders with id.
17413    /// ```
17414    fn parse_concurrent_block(&mut self) -> ParseResult<Stmt<'a>> {
17415        self.advance(); // consume "Attempt"
17416
17417        // Expect "all"
17418        if !self.check(&TokenType::All) {
17419            return Err(ParseError {
17420                kind: ParseErrorKind::ExpectedKeyword {
17421                    keyword: "all".to_string(),
17422                },
17423                span: self.current_span(),
17424            });
17425        }
17426        self.advance(); // consume "all"
17427
17428        // Expect "of" (preposition)
17429        if !self.check_of_preposition() {
17430            return Err(ParseError {
17431                kind: ParseErrorKind::ExpectedKeyword {
17432                    keyword: "of".to_string(),
17433                },
17434                span: self.current_span(),
17435            });
17436        }
17437        self.advance(); // consume "of"
17438
17439        // Expect "the"
17440        if !self.check_article() {
17441            return Err(ParseError {
17442                kind: ParseErrorKind::ExpectedKeyword {
17443                    keyword: "the".to_string(),
17444                },
17445                span: self.current_span(),
17446            });
17447        }
17448        self.advance(); // consume "the"
17449
17450        // Expect "following"
17451        if !self.check(&TokenType::Following) {
17452            return Err(ParseError {
17453                kind: ParseErrorKind::ExpectedKeyword {
17454                    keyword: "following".to_string(),
17455                },
17456                span: self.current_span(),
17457            });
17458        }
17459        self.advance(); // consume "following"
17460
17461        // Expect colon
17462        if !self.check(&TokenType::Colon) {
17463            return Err(ParseError {
17464                kind: ParseErrorKind::ExpectedKeyword {
17465                    keyword: ":".to_string(),
17466                },
17467                span: self.current_span(),
17468            });
17469        }
17470        self.advance(); // consume ":"
17471
17472        let tasks = self.read_indented_block()?;
17473
17474        Ok(Stmt::Concurrent { tasks })
17475    }
17476
17477    /// Phase 9: Parse parallel execution block (CPU-bound)
17478    ///
17479    /// Syntax:
17480    /// ```logos
17481    /// Simultaneously:
17482    ///     Call compute_hash with data1.
17483    ///     Call compute_hash with data2.
17484    /// ```
17485    fn parse_parallel_block(&mut self) -> ParseResult<Stmt<'a>> {
17486        self.advance(); // consume "Simultaneously"
17487
17488        // Expect colon
17489        if !self.check(&TokenType::Colon) {
17490            return Err(ParseError {
17491                kind: ParseErrorKind::ExpectedKeyword {
17492                    keyword: ":".to_string(),
17493                },
17494                span: self.current_span(),
17495            });
17496        }
17497        self.advance(); // consume ":"
17498
17499        let tasks = self.read_indented_block()?;
17500
17501        Ok(Stmt::Parallel { tasks })
17502    }
17503
17504    /// Parse a reversible / uncompute block.
17505    ///
17506    /// Syntax:
17507    /// ```logos
17508    /// Reversibly:
17509    ///     Add 3 to x.
17510    ///     Set x to x xor mask.
17511    /// ```
17512    /// (`Uncompute:` is an accepted alias.) The body runs in the enclosing scope and produces
17513    /// byte-identical OUTPUT to the same statements written outside the block — only the LOWERING
17514    /// of the in-place abelian scalar updates changes (to `Op::RevAdd`/`RevSub`/`RevXor`/`RevNeg`).
17515    fn parse_reversibly_block(&mut self) -> ParseResult<Stmt<'a>> {
17516        self.advance(); // consume "Reversibly" / "Uncompute"
17517
17518        // Expect colon.
17519        if !self.check(&TokenType::Colon) {
17520            return Err(ParseError {
17521                kind: ParseErrorKind::ExpectedKeyword {
17522                    keyword: ":".to_string(),
17523                },
17524                span: self.current_span(),
17525            });
17526        }
17527        self.advance(); // consume ":"
17528
17529        // Skip the newline(s) between the header line and the indented body.
17530        while self.check(&TokenType::Newline) {
17531            self.advance();
17532        }
17533
17534        let body = self.read_indented_block()?;
17535
17536        Ok(Stmt::Reversibly { body })
17537    }
17538
17539    /// Phase 33: Parse Inspect statement for pattern matching
17540    /// Syntax: Inspect target:
17541    ///             If it is a Variant [(bindings)]:
17542    ///                 body...
17543    ///             Otherwise:
17544    ///                 body...
17545    fn parse_inspect_statement(&mut self) -> ParseResult<Stmt<'a>> {
17546        self.advance(); // consume "Inspect"
17547
17548        // Parse target expression
17549        let target = self.parse_imperative_expr()?;
17550
17551        // Expect colon
17552        if !self.check(&TokenType::Colon) {
17553            return Err(ParseError {
17554                kind: ParseErrorKind::ExpectedKeyword {
17555                    keyword: ":".to_string(),
17556                },
17557                span: self.current_span(),
17558            });
17559        }
17560        self.advance(); // consume ":"
17561
17562        // Expect indent
17563        if !self.check(&TokenType::Indent) {
17564            return Err(ParseError {
17565                kind: ParseErrorKind::ExpectedStatement,
17566                span: self.current_span(),
17567            });
17568        }
17569        self.advance(); // consume Indent
17570
17571        let mut arms = Vec::new();
17572        let mut has_otherwise = false;
17573        let mut established_enum: Option<(Symbol, Symbol)> = None;
17574
17575        // Parse match arms until dedent
17576        while !self.check(&TokenType::Dedent) && !self.is_at_end() {
17577            if self.check(&TokenType::Otherwise) {
17578                // Parse "Otherwise:" default arm
17579                self.advance(); // consume "Otherwise"
17580
17581                if !self.check(&TokenType::Colon) {
17582                    return Err(ParseError {
17583                        kind: ParseErrorKind::ExpectedKeyword {
17584                            keyword: ":".to_string(),
17585                        },
17586                        span: self.current_span(),
17587                    });
17588                }
17589                self.advance(); // consume ":"
17590
17591                // Handle both inline (Otherwise: stmt.) and block body
17592                let body = self.read_branch_body()?;
17593
17594                arms.push(MatchArm {
17595                    enum_name: None,
17596                    variant: None,
17597                    bindings: vec![],
17598                    body,
17599                });
17600                has_otherwise = true;
17601                break;
17602            }
17603
17604            if self.check(&TokenType::If) {
17605                // Parse "If it is a VariantName [(bindings)]:"
17606                let arm_span = self.peek().span;
17607                let arm = self.parse_match_arm(established_enum.map(|(e, _)| e))?;
17608                self.check_inspect_arm_enum(&arm, arm_span, &mut established_enum)?;
17609                arms.push(arm);
17610            } else if self.check(&TokenType::When) || self.check_word("When") {
17611                // Parse "When Variant [(bindings)]:" (concise syntax)
17612                let arm_span = self.peek().span;
17613                let arm = self.parse_when_arm(established_enum.map(|(e, _)| e))?;
17614                self.check_inspect_arm_enum(&arm, arm_span, &mut established_enum)?;
17615                arms.push(arm);
17616            } else if self.check(&TokenType::Newline) {
17617                // Skip newlines between arms
17618                self.advance();
17619            } else {
17620                // Skip unexpected tokens
17621                self.advance();
17622            }
17623        }
17624
17625        // Consume final dedent
17626        if self.check(&TokenType::Dedent) {
17627            self.advance();
17628        }
17629
17630        Ok(Stmt::Inspect {
17631            target,
17632            arms,
17633            has_otherwise,
17634        })
17635    }
17636
17637    /// An `Inspect`'s arms must all name variants of ONE enum — whatever the target's real type
17638    /// is, only one type's variants can ever be the right thing to match against it. `established`
17639    /// records the enum (and the variant that first named it) the FIRST arm with a resolvable
17640    /// `enum_name` settled on.
17641    ///
17642    /// By the time an arm reaches this check, `resolve_arm_variant` has ALREADY resolved its
17643    /// variant preferring the established enum over the registry's own arbitrary order — so a
17644    /// name two enums both declare (a user type shadowing a stdlib variant, or two user types
17645    /// sharing a name) reads through the established enum correctly and never reaches here as a
17646    /// false mismatch. What this still catches: a variant the established enum genuinely does NOT
17647    /// have — belonging to some OTHER real enum, or resolving to NO enum at all. The latter is the
17648    /// easiest way to reach this mistake in practice: `Option`/`Result`'s own `Some`/`None`/`Ok`/
17649    /// `Err` are engine-owned markers the registry never resolves to an enum (they are
17650    /// `TypeDef::Generic`, not `TypeDef::Enum`), so a stray `Err` beside a domain type's own
17651    /// `JOk`/`JErr` looks exactly like an unresolved name — and a plain "unknown variant" message
17652    /// would not point at the enum it should have named instead. Both are refused here, at the
17653    /// offending arm, naming the established enum and suggesting its own nearest-spelled variant.
17654    /// An Inspect whose EVERY arm is unresolved (a legitimate `Some`/`None` or `Ok`/`Err` match, or
17655    /// the FIRST arm of any Inspect) never establishes anything and is untouched — there is
17656    /// nothing yet to be inconsistent with.
17657    fn check_inspect_arm_enum(
17658        &self,
17659        arm: &MatchArm<'a>,
17660        arm_span: Span,
17661        established: &mut Option<(Symbol, Symbol)>,
17662    ) -> ParseResult<()> {
17663        let Some(variant) = arm.variant else {
17664            return Ok(());
17665        };
17666        let (expected_enum, first_variant) = match (*established, arm.enum_name) {
17667            (None, None) => return Ok(()),
17668            (None, Some(found_enum)) => {
17669                *established = Some((found_enum, variant));
17670                return Ok(());
17671            }
17672            (Some((expected_enum, _)), Some(found_enum)) if expected_enum == found_enum => {
17673                return Ok(())
17674            }
17675            (Some(pair), _) => pair,
17676        };
17677        let expected_str = self.interner.resolve(expected_enum).to_string();
17678        let variant_str = self.interner.resolve(variant).to_string();
17679        let first_variant_str = self.interner.resolve(first_variant).to_string();
17680        let suggestion = self
17681            .type_registry
17682            .as_ref()
17683            .and_then(|r| match r.get(expected_enum) {
17684                Some(crate::analysis::TypeDef::Enum { variants, .. }) => {
17685                    let names: Vec<&str> = variants
17686                        .iter()
17687                        .map(|v| self.interner.resolve(v.name))
17688                        .collect();
17689                    crate::suggest::find_similar(&variant_str, &names, 2)
17690                }
17691                _ => None,
17692            });
17693        let hint = suggestion
17694            .map(|s| format!(" Did you mean '{s}'?"))
17695            .unwrap_or_default();
17696        let belongs_to = match arm.enum_name {
17697            Some(found_enum) => format!(
17698                "is a variant of '{}', not",
17699                self.interner.resolve(found_enum)
17700            ),
17701            None => "is not a variant of".to_string(),
17702        };
17703        Err(ParseError {
17704            kind: ParseErrorKind::Custom(format!(
17705                "'{variant_str}' {belongs_to} '{expected_str}' — every arm of this Inspect must \
17706                match variants of the SAME type ('{expected_str}', established by its \
17707                '{first_variant_str}' arm).{hint}"
17708            )),
17709            span: arm_span,
17710        })
17711    }
17712
17713    /// Resolve a `When`/`If it is a` arm's variant to its enum and field names.
17714    /// `established` — the enum an enclosing `Inspect` has already committed to, from an
17715    /// earlier arm, if any — is checked FIRST: a variant name can be declared by more than
17716    /// one enum (a user's own type shadowing a same-named stdlib variant, or two user types
17717    /// sharing a name), and once an Inspect has settled on a real enum, every later arm must
17718    /// read its own name against THAT enum before anything else — never fall through to an
17719    /// unrelated enum's same-named variant merely because the registry's own internal map
17720    /// happened to iterate it first. Only when `established` has no such variant (including
17721    /// the arm that DOES the establishing, where nothing is settled yet) does this fall back
17722    /// to the registry's global, unordered search.
17723    fn resolve_arm_variant(
17724        &self,
17725        variant: Symbol,
17726        established: Option<Symbol>,
17727    ) -> (Option<Symbol>, Vec<Symbol>) {
17728        use crate::analysis::TypeDef;
17729        if let Some(enum_name) = established {
17730            if let Some(TypeDef::Enum { variants, .. }) =
17731                self.type_registry.as_ref().and_then(|r| r.get(enum_name))
17732            {
17733                if let Some(vdef) = variants.iter().find(|v| v.name == variant) {
17734                    return (
17735                        Some(enum_name),
17736                        vdef.fields.iter().map(|f| f.name).collect(),
17737                    );
17738                }
17739            }
17740        }
17741        self.type_registry
17742            .as_ref()
17743            .and_then(|r| {
17744                r.find_variant(variant).map(|(enum_name, vdef)| {
17745                    (
17746                        Some(enum_name),
17747                        vdef.fields.iter().map(|f| f.name).collect(),
17748                    )
17749                })
17750            })
17751            .unwrap_or((None, vec![]))
17752    }
17753
17754    /// Parse a single match arm: "If it is a Variant [(field: binding)]:"
17755    fn parse_match_arm(&mut self, established: Option<Symbol>) -> ParseResult<MatchArm<'a>> {
17756        self.advance(); // consume "If"
17757
17758        // Expect "it"
17759        if !self.check_word("it") {
17760            return Err(ParseError {
17761                kind: ParseErrorKind::ExpectedKeyword {
17762                    keyword: "it".to_string(),
17763                },
17764                span: self.current_span(),
17765            });
17766        }
17767        self.advance(); // consume "it"
17768
17769        // Expect "is"
17770        if !self.check(&TokenType::Is) {
17771            return Err(ParseError {
17772                kind: ParseErrorKind::ExpectedKeyword {
17773                    keyword: "is".to_string(),
17774                },
17775                span: self.current_span(),
17776            });
17777        }
17778        self.advance(); // consume "is"
17779
17780        // Consume article "a" or "an"
17781        if self.check_article() {
17782            self.advance();
17783        }
17784
17785        // Get variant name — by RAW spelling: under the dynamic lexicon a
17786        // capitalized variant like `Ok` classifies as a common word whose
17787        // canonical symbol is the lowercased lemma, which would miss the
17788        // enum registry silently.
17789        let variant_raw = self.peek().lexeme;
17790        self.expect_identifier()?;
17791        let variant = variant_raw;
17792
17793        // Look up the enum name for this variant — established first (see `resolve_arm_variant`).
17794        let (enum_name, _fields) = self.resolve_arm_variant(variant, established);
17795
17796        // Optional: "(field)" or "(field: binding)" or "(f1, f2: b2)"
17797        let bindings = if self.check(&TokenType::LParen) {
17798            self.parse_pattern_bindings()?
17799        } else {
17800            vec![]
17801        };
17802
17803        // Expect colon
17804        if !self.check(&TokenType::Colon) {
17805            return Err(ParseError {
17806                kind: ParseErrorKind::ExpectedKeyword {
17807                    keyword: ":".to_string(),
17808                },
17809                span: self.current_span(),
17810            });
17811        }
17812        self.advance(); // consume ":"
17813
17814        let body = self.read_indented_block()?;
17815
17816        Ok(MatchArm {
17817            enum_name,
17818            variant: Some(variant),
17819            bindings,
17820            body,
17821        })
17822    }
17823
17824    /// Parse a concise match arm: "When Variant [(bindings)]:" or "When Variant: stmt."
17825    fn parse_when_arm(&mut self, established: Option<Symbol>) -> ParseResult<MatchArm<'a>> {
17826        self.advance(); // consume "When"
17827
17828        // Get variant name — by RAW spelling (see parse_match_arm: the
17829        // dynamic lexicon lemma-lowercases common words like `Ok`).
17830        let variant_raw = self.peek().lexeme;
17831        self.expect_identifier()?;
17832        let variant = variant_raw;
17833
17834        // Look up the enum name and variant definition for this variant — established
17835        // first (see `resolve_arm_variant`), so this arm's positional bindings are built
17836        // from the SAME enum's own field list, not a same-named variant of a different one.
17837        let (enum_name, variant_fields) = self.resolve_arm_variant(variant, established);
17838
17839        // Optional: "(binding)" or "(b1, b2)" - positional bindings
17840        let bindings = if self.check(&TokenType::LParen) {
17841            let raw_bindings = self.parse_when_bindings()?;
17842            // Map positional bindings to actual field names
17843            raw_bindings
17844                .into_iter()
17845                .enumerate()
17846                .map(|(i, binding)| {
17847                    let field = variant_fields.get(i).copied().unwrap_or(binding);
17848                    (field, binding)
17849                })
17850                .collect()
17851        } else {
17852            vec![]
17853        };
17854
17855        // Expect colon
17856        if !self.check(&TokenType::Colon) {
17857            return Err(ParseError {
17858                kind: ParseErrorKind::ExpectedKeyword {
17859                    keyword: ":".to_string(),
17860                },
17861                span: self.current_span(),
17862            });
17863        }
17864        self.advance(); // consume ":"
17865
17866        // Handle both inline body (When Variant: stmt.) and block body
17867        let body = self.read_branch_body()?;
17868
17869        Ok(MatchArm {
17870            enum_name,
17871            variant: Some(variant),
17872            bindings,
17873            body,
17874        })
17875    }
17876
17877    /// Parse concise When bindings: "(r)" or "(w, h)" - just binding variable names
17878    fn parse_when_bindings(&mut self) -> ParseResult<Vec<Symbol>> {
17879        self.advance(); // consume '('
17880        let mut bindings = Vec::new();
17881
17882        loop {
17883            let binding = self.expect_identifier()?;
17884            bindings.push(binding);
17885
17886            if !self.check(&TokenType::Comma) {
17887                break;
17888            }
17889            self.advance(); // consume ','
17890        }
17891
17892        if !self.check(&TokenType::RParen) {
17893            return Err(ParseError {
17894                kind: ParseErrorKind::ExpectedKeyword {
17895                    keyword: ")".to_string(),
17896                },
17897                span: self.current_span(),
17898            });
17899        }
17900        self.advance(); // consume ')'
17901
17902        Ok(bindings)
17903    }
17904
17905    /// Parse pattern bindings: "(field)" or "(field: binding)" or "(f1, f2: b2)"
17906    fn parse_pattern_bindings(&mut self) -> ParseResult<Vec<(Symbol, Symbol)>> {
17907        self.advance(); // consume '('
17908        let mut bindings = Vec::new();
17909
17910        loop {
17911            let field = self.expect_identifier()?;
17912            let binding = if self.check(&TokenType::Colon) {
17913                self.advance(); // consume ":"
17914                self.expect_identifier()?
17915            } else {
17916                field // field name = binding name
17917            };
17918            bindings.push((field, binding));
17919
17920            if !self.check(&TokenType::Comma) {
17921                break;
17922            }
17923            self.advance(); // consume ','
17924        }
17925
17926        if !self.check(&TokenType::RParen) {
17927            return Err(ParseError {
17928                kind: ParseErrorKind::ExpectedKeyword {
17929                    keyword: ")".to_string(),
17930                },
17931                span: self.current_span(),
17932            });
17933        }
17934        self.advance(); // consume ')'
17935
17936        Ok(bindings)
17937    }
17938
17939    /// The declared field names of `type_name`'s own constructor, and whether it names a
17940    /// VARIANT (`true`) or a plain struct (`false`). `None` when the registry has nothing
17941    /// for it (an unresolved or built-in-wrapped name like `Some`/`None`) — the caller then
17942    /// falls back to the old permissive reading rather than refuse a construction it has no
17943    /// field list to check against.
17944    ///
17945    /// The distinction matters beyond the boundary check: a struct field left out of a
17946    /// construction is a documented, working feature — it defaults from its declared type
17947    /// (`interpreter.rs::default_for_declared_type`, `LOGOS_QUICKGUIDE.md`'s `a new Point`
17948    /// row) — while a variant's payload has no such default (an `Inspect` binding for a
17949    /// field never given would read nothing), so only a variant's missing fields are ever
17950    /// refused.
17951    ///
17952    /// `enum_hint` is the enum a construction site has ALREADY settled on (`from <Enum>`, or
17953    /// the sole candidate `resolve_construction_target` found) — checked first, so the field
17954    /// list is the one belonging to THAT enum's own variant rather than
17955    /// `TypeRegistry::find_variant`'s arbitrary-but-deterministic first candidate, which could
17956    /// be a same-named variant of a DIFFERENT enum with a different field shape entirely.
17957    fn constructor_field_names(
17958        &self,
17959        type_name: Symbol,
17960        enum_hint: Option<Symbol>,
17961    ) -> Option<(Vec<Symbol>, bool)> {
17962        use crate::analysis::TypeDef;
17963        let registry = self.type_registry.as_ref()?;
17964        if let Some(enum_name) = enum_hint {
17965            if let Some(vdef) = registry.variant_in(enum_name, type_name) {
17966                return Some((vdef.fields.iter().map(|f| f.name).collect(), true));
17967            }
17968        }
17969        if let Some((_, vdef)) = registry.find_variant(type_name) {
17970            return Some((vdef.fields.iter().map(|f| f.name).collect(), true));
17971        }
17972        match registry.get(type_name) {
17973            Some(TypeDef::Struct { fields, .. }) => {
17974                Some((fields.iter().map(|f| f.name).collect(), false))
17975            }
17976            _ => None,
17977        }
17978    }
17979
17980    /// The lexeme `offset` tokens ahead, when that token could plausibly name a field
17981    /// (never a string/char literal, matching `check_word`'s own exclusion) — lets a
17982    /// caller look one word past an `and` without consuming it.
17983    fn peek_field_name_at(&self, offset: usize) -> Option<Symbol> {
17984        let token = self.tokens.get(self.current + offset)?;
17985        if matches!(
17986            token.kind,
17987            TokenType::StringLiteral(_) | TokenType::CharLiteral(_)
17988        ) {
17989            return None;
17990        }
17991        Some(token.lexeme)
17992    }
17993
17994    /// When `value` is itself a nested `a new …` constructor whose OWN declared
17995    /// fields include `field`, the type it constructs — the hint a missing-field
17996    /// refusal uses to explain that an inner constructor may have consumed the
17997    /// `and <field> …` clause the outer one needed.
17998    fn nested_constructor_naming(&self, value: &Expr<'a>, field: Symbol) -> Option<Symbol> {
17999        use crate::ast::Expr;
18000        let (inner_type, enum_hint) = match value {
18001            Expr::New { type_name, .. } => (*type_name, None),
18002            Expr::NewVariant {
18003                enum_name, variant, ..
18004            } => (*variant, Some(*enum_name)),
18005            _ => return None,
18006        };
18007        self.constructor_field_names(inner_type, enum_hint)?
18008            .0
18009            .contains(&field)
18010            .then_some(inner_type)
18011    }
18012
18013    /// Whether an ANCESTOR constructor — one enclosing the frame at `frame_index`, i.e.
18014    /// `self.constructor_frames[..frame_index]` — still has an unassigned field named
18015    /// `field`. Consulted only when the CURRENT constructor has already assigned `field`
18016    /// itself and sees it named again: if some ancestor still wants it, that repetition is
18017    /// this constructor's own closing boundary (stop, don't consume, let it bubble up); if
18018    /// no ancestor could ever claim it, it is unclaimable by anyone else and is treated as
18019    /// a duplicate at THIS level instead of an inscrutable dangling `and` further out.
18020    fn any_enclosing_frame_wants(&self, frame_index: usize, field: Symbol) -> bool {
18021        self.constructor_frames[..frame_index]
18022            .iter()
18023            .any(|(ty, enum_hint, assigned)| {
18024                self.constructor_field_names(*ty, *enum_hint)
18025                    .is_some_and(|(names, _)| names.contains(&field) && !assigned.contains(&field))
18026            })
18027    }
18028
18029    /// Parse constructor fields: "with field1 value1 [and field2 value2]..."
18030    /// Example: "with radius 10" or "with x 10 and y 20"
18031    /// Used for both variant constructors and struct initialization.
18032    ///
18033    /// `type_name` is the constructor's own type. Its declared field set (via
18034    /// `constructor_field_names`) is what lets this tell "one more field of THIS
18035    /// constructor" apart from "the enclosing constructor's own next field" at every
18036    /// `and`: a nested `a new …` value parses its own fields the same way, so it stops
18037    /// consuming `and`s at its own type's boundary instead of reading past it into
18038    /// fields the ENCLOSING constructor still needs. A frame recording this call's own
18039    /// type and its fields assigned so far is pushed onto `self.constructor_frames` for
18040    /// the duration — see `any_enclosing_frame_wants`.
18041    fn parse_constructor_fields(
18042        &mut self,
18043        type_name: Symbol,
18044        enum_hint: Option<Symbol>,
18045    ) -> ParseResult<Vec<(Symbol, &'a Expr<'a>)>> {
18046        let with_span = self.peek().span;
18047        self.advance(); // consume "with"
18048
18049        let frame_index = self.constructor_frames.len();
18050        self.constructor_frames
18051            .push((type_name, enum_hint, Vec::new()));
18052        let result =
18053            self.parse_constructor_fields_body(type_name, enum_hint, with_span, frame_index);
18054        self.constructor_frames.truncate(frame_index);
18055        result
18056    }
18057
18058    fn parse_constructor_fields_body(
18059        &mut self,
18060        type_name: Symbol,
18061        enum_hint: Option<Symbol>,
18062        with_span: Span,
18063        frame_index: usize,
18064    ) -> ParseResult<Vec<(Symbol, &'a Expr<'a>)>> {
18065        use crate::ast::Expr;
18066        let mut fields: Vec<(Symbol, &'a Expr<'a>)> = Vec::new();
18067        let declared = self.constructor_field_names(type_name, enum_hint);
18068
18069        loop {
18070            let field_span = self.peek().span;
18071            let field_name = self.expect_identifier()?;
18072
18073            if fields.iter().any(|(n, _)| *n == field_name) {
18074                let field_str = self.interner.resolve(field_name);
18075                return Err(ParseError {
18076                    kind: ParseErrorKind::Custom(format!(
18077                        "'{}' names the field '{field_str}' twice in the same construction — a \
18078                        field is assigned once. Remove one of the two '{field_str}' clauses.",
18079                        self.interner.resolve(type_name)
18080                    )),
18081                    span: field_span,
18082                });
18083            }
18084
18085            // Parse field value expression — use parse_comparison to stop before
18086            // "and"/"or" which serve as field separators in constructor syntax
18087            let value = self.parse_comparison()?;
18088
18089            fields.push((field_name, value));
18090            self.constructor_frames[frame_index].2.push(field_name);
18091
18092            // Check for "and" to continue — only consumed when the word right after it
18093            // names a field THIS constructor still needs, OR repeats a field it already
18094            // has that no ENCLOSING constructor could still want (a genuine duplicate,
18095            // best caught here with a named error rather than left dangling for whatever
18096            // called this constructor). Otherwise it belongs to whatever constructor is
18097            // enclosing this one. Applies equally to a struct and a variant.
18098            if self.check(&TokenType::And) {
18099                let continues_this_constructor = match &declared {
18100                    Some((names, _)) => self.peek_field_name_at(1).is_some_and(|next| {
18101                        if !names.contains(&next) {
18102                            return false;
18103                        }
18104                        let already_assigned = fields.iter().any(|(n, _)| *n == next);
18105                        !already_assigned || !self.any_enclosing_frame_wants(frame_index, next)
18106                    }),
18107                    None => true,
18108                };
18109                if continues_this_constructor {
18110                    self.advance(); // consume "and"
18111                    continue;
18112                }
18113            }
18114            break;
18115        }
18116
18117        // A missing field is only ever refused for a VARIANT — a struct field left out
18118        // defaults from its declared type (see `constructor_field_names`'s doc comment).
18119        if let Some((names, true)) = &declared {
18120            let missing: Vec<Symbol> = names
18121                .iter()
18122                .copied()
18123                .filter(|f| !fields.iter().any(|(n, _)| n == f))
18124                .collect();
18125            if !missing.is_empty() {
18126                let type_str = self.interner.resolve(type_name);
18127                let parts: Vec<String> = missing
18128                    .iter()
18129                    .map(|f| {
18130                        let field_str = self.interner.resolve(*f);
18131                        match fields.iter().find_map(|(_, v)| self.nested_constructor_naming(v, *f)) {
18132                            Some(inner_type) => {
18133                                let inner_str = self.interner.resolve(inner_type);
18134                                format!(
18135                                    "'{field_str}' — a nested 'a new {inner_str}' assigned to another \
18136                                    field above also declares a field named '{field_str}' and may have \
18137                                    consumed the 'and {field_str} …' meant for '{type_str}' here; wrap \
18138                                    it in parentheses, '(a new {inner_str} with …)', to keep the two apart"
18139                                )
18140                            }
18141                            None => format!("'{field_str}'"),
18142                        }
18143                    })
18144                    .collect();
18145                let plural = if missing.len() == 1 {
18146                    "field"
18147                } else {
18148                    "fields"
18149                };
18150                return Err(ParseError {
18151                    kind: ParseErrorKind::Custom(format!(
18152                        "'{type_str}' needs a value for its {plural}: {}. Every field a constructor \
18153                        declares must be given one, here and at every other 'a new {type_str}'.",
18154                        parts.join("; ")
18155                    )),
18156                    span: with_span,
18157                });
18158            }
18159        }
18160
18161        Ok(fields)
18162    }
18163
18164    /// For variant constructors: "with radius 10" fields on `a new Circle with …`.
18165    /// `enum_hint` is the enum this variant construction already settled on (`from <Enum>`, or
18166    /// the sole candidate) — see `constructor_field_names`.
18167    fn parse_variant_constructor_fields(
18168        &mut self,
18169        type_name: Symbol,
18170        enum_hint: Option<Symbol>,
18171    ) -> ParseResult<Vec<(Symbol, &'a Expr<'a>)>> {
18172        self.parse_constructor_fields(type_name, enum_hint)
18173    }
18174
18175    /// Alias for struct initialization
18176    /// `a new Some with value <e>` / `a new None` as the Option they mean, or `None` when this is
18177    /// an ordinary record construction.
18178    ///
18179    /// The two surface forms of a present Option must be ONE value — see the call site for what
18180    /// went wrong while they were two. The match is deliberately exact: `Some` with a single field
18181    /// named `value`, or `None` with no fields at all. A user record that merely shares the name
18182    /// but not the shape keeps reading as a record, and a generic argument (`a new Some of Int`)
18183    /// is left alone because the payload type is carried by the value, not by an annotation.
18184    fn option_form_of(
18185        &self,
18186        type_name: Symbol,
18187        type_args: &[TypeExpr],
18188        init_fields: &[(Symbol, &'a Expr<'a>)],
18189    ) -> Option<Expr<'a>> {
18190        if !type_args.is_empty() {
18191            return None;
18192        }
18193        match self.interner.resolve(type_name) {
18194            "Some"
18195                if init_fields.len() == 1 && self.interner.resolve(init_fields[0].0) == "value" =>
18196            {
18197                Some(Expr::OptionSome {
18198                    value: init_fields[0].1,
18199                })
18200            }
18201            "None" if init_fields.is_empty() => Some(Expr::OptionNone),
18202            _ => None,
18203        }
18204    }
18205
18206    /// For struct initialization: "with x 10 and y 20" fields on `a new Point with …`. A struct
18207    /// is never enum-qualified, so this always parses with no enum hint.
18208    fn parse_struct_init_fields(
18209        &mut self,
18210        type_name: Symbol,
18211    ) -> ParseResult<Vec<(Symbol, &'a Expr<'a>)>> {
18212        self.parse_constructor_fields(type_name, None)
18213    }
18214
18215    /// Phase 34: Parse generic type arguments for constructor instantiation
18216    /// Parses "of Int" or "of Int and Text" after a generic type name
18217    /// Returns empty Vec for non-generic types
18218    fn parse_generic_type_args(&mut self, type_name: Symbol) -> ParseResult<Vec<TypeExpr<'a>>> {
18219        // Only parse type args if the type is a known generic
18220        if !self.is_generic_type(type_name) {
18221            return Ok(vec![]);
18222        }
18223
18224        // Expect "of" preposition
18225        if !self.check_preposition_is("of") {
18226            return Ok(vec![]); // Generic type without arguments - will use defaults
18227        }
18228        self.advance(); // consume "of"
18229
18230        let mut type_args = Vec::new();
18231        loop {
18232            // Bug fix: Parse full type expression to support nested types like "Seq of (Seq of Int)"
18233            let type_arg = self.parse_type_expression()?;
18234            type_args.push(type_arg);
18235
18236            // Check for "and" or "to" to continue (for multi-param generics like "Map of Text to Int")
18237            if self.check(&TokenType::And) || self.check_to_preposition() {
18238                self.advance(); // consume separator
18239                continue;
18240            }
18241            break;
18242        }
18243
18244        Ok(type_args)
18245    }
18246
18247    /// Skip type definition content until next block header
18248    /// Used for TypeDef blocks (## A Point has:, ## A Color is one of:)
18249    /// The actual parsing is done by DiscoveryPass
18250    fn skip_type_def_content(&mut self) {
18251        while !self.is_at_end() {
18252            // Stop at next block header
18253            if matches!(
18254                self.tokens.get(self.current),
18255                Some(Token {
18256                    kind: TokenType::BlockHeader { .. },
18257                    ..
18258                })
18259            ) {
18260                break;
18261            }
18262            self.advance();
18263        }
18264    }
18265
18266    /// Phase 63: Parse theorem block
18267    /// Syntax:
18268    ///   ## Theorem: Name
18269    ///   Given: Premise 1.
18270    ///   Given: Premise 2.
18271    ///   Prove: Goal.
18272    ///   Proof: Auto.
18273    fn parse_theorem_block(&mut self) -> ParseResult<Stmt<'a>> {
18274        use crate::ast::theorem::{ProofStrategy, TheoremBlock};
18275
18276        // Skip newlines and indentation
18277        self.skip_whitespace_tokens();
18278
18279        // Parse theorem name: expect "Name" after the ##Theorem header
18280        // The header has already been consumed; expect Colon then Identifier
18281        // Note: In some cases the colon might have been consumed with the header.
18282        // Let's be flexible and check for both.
18283        if self.check(&TokenType::Colon) {
18284            self.advance();
18285        }
18286
18287        // Skip whitespace again
18288        self.skip_whitespace_tokens();
18289
18290        // Get theorem name - should be an identifier (Noun, ProperName, etc.)
18291        let name = if let Some(token) = self.tokens.get(self.current) {
18292            match &token.kind {
18293                TokenType::Noun(_)
18294                | TokenType::ProperName(_)
18295                | TokenType::Verb { .. }
18296                | TokenType::Adjective(_) => {
18297                    let name = self.interner.resolve(token.lexeme).to_string();
18298                    self.advance();
18299                    name
18300                }
18301                _ => {
18302                    // Use the lexeme directly for unrecognized tokens
18303                    let lexeme = self.interner.resolve(token.lexeme);
18304                    if !lexeme.is_empty()
18305                        && lexeme
18306                            .chars()
18307                            .next()
18308                            .map(|c| c.is_alphanumeric())
18309                            .unwrap_or(false)
18310                    {
18311                        let name = lexeme.to_string();
18312                        self.advance();
18313                        name
18314                    } else {
18315                        "Anonymous".to_string()
18316                    }
18317                }
18318            }
18319        } else {
18320            "Anonymous".to_string()
18321        };
18322
18323        self.skip_whitespace_tokens();
18324
18325        // Skip period after name if present
18326        if self.check(&TokenType::Period) {
18327            self.advance();
18328        }
18329
18330        self.skip_whitespace_tokens();
18331
18332        // Parse premises (Given: statements)
18333        // Each premise is a sentence that can introduce referents for subsequent premises
18334        let mut premises = Vec::new();
18335        let mut premise_names: Vec<Option<String>> = Vec::new();
18336        while self.check(&TokenType::Given) {
18337            self.advance(); // consume "Given"
18338
18339            self.skip_whitespace_tokens();
18340
18341            // Optional premise NAME in parentheses: `Given (h): …` names this premise
18342            // `h`, so a `Proof:` script can say `cases h` instead of the positional `hp0`.
18343            let mut this_name: Option<String> = None;
18344            if self.check(&TokenType::LParen) {
18345                self.advance();
18346                self.skip_whitespace_tokens();
18347                if let Some(token) = self.tokens.get(self.current) {
18348                    if !matches!(token.kind, TokenType::RParen) {
18349                        this_name = Some(self.interner.resolve(token.lexeme).to_string());
18350                        self.advance();
18351                    }
18352                }
18353                self.skip_whitespace_tokens();
18354                if self.check(&TokenType::RParen) {
18355                    self.advance();
18356                }
18357                self.skip_whitespace_tokens();
18358            }
18359            premise_names.push(this_name);
18360
18361            // Expect colon
18362            if self.check(&TokenType::Colon) {
18363                self.advance();
18364            }
18365
18366            self.skip_whitespace_tokens();
18367
18368            // Parse the logic expression for this premise. In pragmatic mode
18369            // a leading weak scalar (some/most/many) carries its Horn-scale
18370            // implicature, exactly as in `parse_pragmatic`.
18371            let scalar_trigger = self.pragmatic
18372                && matches!(
18373                    self.peek().kind,
18374                    TokenType::Some | TokenType::Most | TokenType::Many
18375                );
18376            // An optative wish ("May you prosper!") is a valid premise; the
18377            // standalone path tries it before the declarative sentence parser,
18378            // so the theorem-premise path must too (it self-backtracks to None
18379            // for non-optatives, falling through to `parse_sentence`).
18380            let mut premise_expr = match self.try_parse_optative()? {
18381                Some(opt) => opt,
18382                None => self.parse_sentence()?,
18383            };
18384            if scalar_trigger {
18385                if let Some(strengthened) = self.scalar_implicature(premise_expr) {
18386                    premise_expr = strengthened;
18387                }
18388            }
18389            premises.push(premise_expr);
18390
18391            // Mark sentence boundary - this enables discourse mode and populates telescope
18392            // so pronouns in subsequent premises can resolve to referents from this premise
18393            self.world_state.end_sentence();
18394
18395            // Consume the trailing terminator if present ("!" for
18396            // exclamative/optative/imperative premises).
18397            if self.check(&TokenType::Period) || self.check(&TokenType::Exclamation) {
18398                self.advance();
18399            }
18400
18401            self.skip_whitespace_tokens();
18402        }
18403
18404        // Every premise must be citable by exactly one name, and the positional namespace is
18405        // the system's — see `premise_label_conflict`.
18406        if let Some(conflict) = premise_label_conflict(&premise_names) {
18407            return Err(ParseError {
18408                kind: ParseErrorKind::Custom(conflict),
18409                span: self.current_span(),
18410            });
18411        }
18412
18413        // Parse goal (Prove: statement)
18414        let goal = if self.check(&TokenType::Prove) {
18415            self.advance(); // consume "Prove"
18416
18417            if self.check(&TokenType::Colon) {
18418                self.advance();
18419            }
18420
18421            self.skip_whitespace_tokens();
18422
18423            // The goal may itself be an optative ("Prove: May you prosper!" —
18424            // identity through the wish encoding), same as the premise path.
18425            let goal_expr = match self.try_parse_optative()? {
18426                Some(opt) => opt,
18427                None => self.parse_sentence()?,
18428            };
18429
18430            if self.check(&TokenType::Period) || self.check(&TokenType::Exclamation) {
18431                self.advance();
18432            }
18433
18434            goal_expr
18435        } else {
18436            return Err(ParseError {
18437                kind: ParseErrorKind::ExpectedKeyword {
18438                    keyword: "Prove".to_string(),
18439                },
18440                span: self.current_span(),
18441            });
18442        };
18443
18444        self.skip_whitespace_tokens();
18445
18446        // ★ A `Given:` written BELOW the goal is not a premise, and it used to be NOTHING.
18447        // The premise loop above stops at the first token that is not `Given`, so a `Given:`
18448        // line after `Prove:` is left behind for the PROGRAM parser to read as an ordinary
18449        // sentence. The theorem is then built with one premise fewer, fails to prove, and
18450        // reports `Premises: 0` — which sends the author to the prover for a defect that is
18451        // in their LAYOUT. The premise did not fail to help; it was never there.
18452        //
18453        // The guard tests for the premise line's COLON, not for the word: `Given` is ordinary
18454        // English and a sentence that merely opens with it is prose. It also stops at the
18455        // `Proof:` line, so a tactic script may say whatever it likes.
18456        if self.stray_premise_after_goal() {
18457            return Err(ParseError {
18458                kind: ParseErrorKind::Custom(
18459                    "a `Given:` line appears BELOW the `Prove:` line. A theorem's premises \
18460                     are read above its goal, so a `Given:` written after `Prove:` is not a \
18461                     premise at all — the theorem would be built without it and then fail to \
18462                     prove, which reads as a defect in the proof rather than in the layout. \
18463                     Move every `Given:` line above the `Prove:` line."
18464                        .to_string(),
18465                ),
18466                span: self.current_span(),
18467            });
18468        }
18469
18470        // Parse proof strategy (`Proof: Auto.` or `Proof: <tactic script>`). The word
18471        // `Proof` may lex as a real `Proof` block header OR — inline, after `Prove:` —
18472        // as an ordinary capitalized name; accept both so the `Proof:` line is found.
18473        let at_proof = self.check(&TokenType::BlockHeader {
18474            block_type: crate::token::BlockType::Proof,
18475        }) || self.tokens.get(self.current).is_some_and(|t| {
18476            self.interner
18477                .resolve(t.lexeme)
18478                .eq_ignore_ascii_case("proof")
18479        });
18480        let strategy = if at_proof {
18481            self.advance();
18482            self.skip_whitespace_tokens();
18483
18484            if self.check(&TokenType::Colon) {
18485                self.advance();
18486            }
18487
18488            self.skip_whitespace_tokens();
18489
18490            if self.check(&TokenType::Auto) {
18491                self.advance();
18492                ProofStrategy::Auto
18493            } else {
18494                // Anything else is an explicit tactic SCRIPT, written in the
18495                // English-esque vernacular: collect every token's lexeme up to the end
18496                // of the block (the next `##` header, a dedent, or EOF) and join them —
18497                // the tactic-script parser re-lexes the result, treating sentence
18498                // punctuation as step separators. So a prose proof flows straight in.
18499                let mut parts: Vec<String> = Vec::new();
18500                while !self.is_at_end()
18501                    && !matches!(
18502                        self.tokens[self.current].kind,
18503                        TokenType::BlockHeader { .. }
18504                    )
18505                    && !self.check(&TokenType::Dedent)
18506                {
18507                    let lexeme = self.tokens[self.current].lexeme;
18508                    parts.push(self.interner.resolve(lexeme).to_string());
18509                    self.advance();
18510                }
18511                let script = parts.join(" ");
18512                if script.trim().is_empty() {
18513                    ProofStrategy::Auto
18514                } else if let Some(variable) = proof_line_is_induction_on(&parts) {
18515                    ProofStrategy::Induction(variable)
18516                } else if let Some(rule) = proof_line_names_one_rule(&parts) {
18517                    ProofStrategy::ByRule(rule)
18518                } else {
18519                    ProofStrategy::Script(lower_rule_applications(&parts).unwrap_or(script))
18520                }
18521            }
18522        } else {
18523            // No explicit proof strategy, default to Auto
18524            ProofStrategy::Auto
18525        };
18526
18527        // Consume trailing period if present
18528        if self.check(&TokenType::Period) {
18529            self.advance();
18530        }
18531
18532        let theorem = TheoremBlock {
18533            name,
18534            premises,
18535            premise_names,
18536            goal,
18537            strategy,
18538        };
18539
18540        Ok(Stmt::Theorem(theorem))
18541    }
18542
18543    /// Parse a `## Define` block (Rung 0a): a vernacular-logic predicate
18544    /// definition written as one biconditional sentence,
18545    /// `<subject> is <predicate> if and only if <definiens>`. The LHS predicate
18546    /// is the definiendum (its name + parameter symbols); the RHS is the
18547    /// definiens. A body that is not a top-level biconditional whose left side
18548    /// is a predicate application is a malformed definition and errors.
18549    fn parse_define_block(&mut self) -> ParseResult<Stmt<'a>> {
18550        use crate::ast::definition::DefinitionBlock;
18551
18552        self.skip_whitespace_tokens();
18553
18554        // Optional `: Label` after the header — the definiendum names itself, so
18555        // a label is decorative. Consume and ignore one if present.
18556        if self.check(&TokenType::Colon) {
18557            self.advance();
18558            self.skip_whitespace_tokens();
18559            if let Some(token) = self.tokens.get(self.current) {
18560                if matches!(
18561                    token.kind,
18562                    TokenType::Noun(_)
18563                        | TokenType::ProperName(_)
18564                        | TokenType::Adjective(_)
18565                        | TokenType::Verb { .. }
18566                ) {
18567                    self.advance();
18568                }
18569            }
18570            if self.check(&TokenType::Period) {
18571                self.advance();
18572            }
18573        }
18574
18575        self.skip_whitespace_tokens();
18576
18577        // The body is one biconditional sentence.
18578        let sentence = self.parse_sentence()?;
18579        if self.check(&TokenType::Period) || self.check(&TokenType::Exclamation) {
18580            self.advance();
18581        }
18582
18583        // Split at the top-level biconditional.
18584        let (definiendum, definiens) = match sentence {
18585            LogicExpr::BinaryOp {
18586                left,
18587                op: TokenType::Iff,
18588                right,
18589            } => (*left, *right),
18590            _ => {
18591                return Err(ParseError {
18592                    kind: ParseErrorKind::Custom(
18593                        "a `## Define` body must be a biconditional: \
18594                         `<subject> is <predicate> if and only if <definiens>`"
18595                            .to_string(),
18596                    ),
18597                    span: self.current_span(),
18598                });
18599            }
18600        };
18601
18602        // The definiendum must be a predicate applied to its parameters; the
18603        // predicate name is the new concept, its arguments are the parameters.
18604        let (name, params) = match definiendum {
18605            LogicExpr::Predicate { name, args, .. } => {
18606                let pname = self.interner.resolve(*name).to_string();
18607                let params: Vec<Symbol> = args
18608                    .iter()
18609                    .filter_map(|t| match t {
18610                        Term::Constant(s) | Term::Variable(s) => Some(*s),
18611                        _ => None,
18612                    })
18613                    .collect();
18614                (pname, params)
18615            }
18616            _ => {
18617                return Err(ParseError {
18618                    kind: ParseErrorKind::Custom(
18619                        "the left side of a `## Define` biconditional must be a \
18620                         predicate applied to its parameters (e.g. `x is a bachelor`)"
18621                            .to_string(),
18622                    ),
18623                    span: self.current_span(),
18624                });
18625            }
18626        };
18627
18628        Ok(Stmt::Definition(DefinitionBlock {
18629            name,
18630            params,
18631            definiendum,
18632            definiens,
18633        }))
18634    }
18635
18636    /// Parse a `## Axiom` block: `## Axiom <name>: <formula>`. The leading identifier is
18637    /// the axiom's name; the remainder of the block is captured as formula text and parsed
18638    /// downstream by the formal-formula parser (`logicaffeine_proof::formula`). The body is
18639    /// formal first-order logic — `for all a b, Cong(a, b, b, a)` — which the NL parser
18640    /// cannot express, so it is kept as text rather than parsed here.
18641    fn parse_axiom_block(&mut self) -> ParseResult<Stmt<'a>> {
18642        use crate::ast::axiom::AxiomBlock;
18643        self.skip_whitespace_tokens();
18644        let name = self.read_block_name();
18645        self.skip_whitespace_tokens();
18646        if self.check(&TokenType::Colon) {
18647            self.advance();
18648        }
18649        let formula = self.collect_block_text();
18650        Ok(Stmt::Axiom(AxiomBlock { name, formula }))
18651    }
18652
18653    /// Parse a `## Theory` block: `## Theory <name>` optionally followed by a development
18654    /// body (`Axiom …` / `Theorem …` declarations). The leading identifier is the theory's
18655    /// name; the remainder of the block is captured as development text, parsed downstream.
18656    fn parse_theory_block(&mut self) -> ParseResult<Stmt<'a>> {
18657        use crate::ast::axiom::TheoryBlock;
18658        self.skip_whitespace_tokens();
18659        let name = self.read_block_name();
18660        self.skip_whitespace_tokens();
18661        if self.check(&TokenType::Colon) {
18662            self.advance();
18663        }
18664        let body = self.collect_block_text();
18665        Ok(Stmt::Theory(TheoryBlock { name, body }))
18666    }
18667
18668    /// Read a block's leading name token (a proper name / noun / adjective / verb lexeme),
18669    /// returning its text. `"Anonymous"` if no name token is present.
18670    fn read_block_name(&mut self) -> String {
18671        if let Some(token) = self.tokens.get(self.current) {
18672            if matches!(
18673                token.kind,
18674                TokenType::Noun(_)
18675                    | TokenType::ProperName(_)
18676                    | TokenType::Adjective(_)
18677                    | TokenType::Verb { .. }
18678            ) {
18679                let name = self.interner.resolve(token.lexeme).to_string();
18680                self.advance();
18681                return name;
18682            }
18683        }
18684        "Anonymous".to_string()
18685    }
18686
18687    /// Collect the remaining tokens of the current block as text — each token's lexeme
18688    /// joined with spaces, up to the next `##` block header or EOF. Layout tokens
18689    /// (`Newline`/`Indent`/`Dedent`) are skipped rather than emitted, so a multi-line,
18690    /// indented development body flows through as clean whitespace-separated words for the
18691    /// downstream formal parser.
18692    fn collect_block_text(&mut self) -> String {
18693        let mut parts: Vec<String> = Vec::new();
18694        while !self.is_at_end()
18695            && !matches!(
18696                self.tokens[self.current].kind,
18697                TokenType::BlockHeader { .. }
18698            )
18699        {
18700            match &self.tokens[self.current].kind {
18701                TokenType::Newline | TokenType::Indent | TokenType::Dedent => {}
18702                _ => {
18703                    let lexeme = self.tokens[self.current].lexeme;
18704                    parts.push(self.interner.resolve(lexeme).to_string());
18705                }
18706            }
18707            self.advance();
18708        }
18709        parts.join(" ")
18710    }
18711
18712    /// Skip whitespace tokens (newlines, indentation)
18713    /// Is there a `Given …:` PREMISE LINE between here (just past the goal) and the proof?
18714    ///
18715    /// Scans forward to the end of the theorem block, stopping at the `Proof:` line so a
18716    /// tactic script's prose is never read as a premise, and requires the line's own
18717    /// punctuation — an optional `(name)` and then `:` — so an ordinary English sentence
18718    /// opening with the word `Given` is not mistaken for one.
18719    fn stray_premise_after_goal(&self) -> bool {
18720        let mut index = self.current;
18721        while let Some(token) = self.tokens.get(index) {
18722            match &token.kind {
18723                TokenType::BlockHeader { .. } | TokenType::EOF => return false,
18724                TokenType::Given if self.premise_colon_follows(index + 1) => return true,
18725                _ => {}
18726            }
18727            if self
18728                .interner
18729                .resolve(token.lexeme)
18730                .eq_ignore_ascii_case("proof")
18731            {
18732                return false;
18733            }
18734            index += 1;
18735        }
18736        false
18737    }
18738
18739    /// Does a premise line's punctuation — an optional parenthesized name, then `:` —
18740    /// begin at `start`?
18741    fn premise_colon_follows(&self, start: usize) -> bool {
18742        let mut index = start;
18743        let skip_layout = |index: &mut usize| {
18744            while matches!(
18745                self.tokens.get(*index).map(|t| &t.kind),
18746                Some(TokenType::Newline) | Some(TokenType::Indent) | Some(TokenType::Dedent)
18747            ) {
18748                *index += 1;
18749            }
18750        };
18751        skip_layout(&mut index);
18752        if matches!(
18753            self.tokens.get(index).map(|t| &t.kind),
18754            Some(TokenType::LParen)
18755        ) {
18756            while let Some(token) = self.tokens.get(index) {
18757                index += 1;
18758                if matches!(token.kind, TokenType::RParen) {
18759                    break;
18760                }
18761            }
18762            skip_layout(&mut index);
18763        }
18764        matches!(
18765            self.tokens.get(index).map(|t| &t.kind),
18766            Some(TokenType::Colon)
18767        )
18768    }
18769
18770    fn skip_whitespace_tokens(&mut self) {
18771        while self.check(&TokenType::Newline)
18772            || self.check(&TokenType::Indent)
18773            || self.check(&TokenType::Dedent)
18774        {
18775            self.advance();
18776        }
18777    }
18778
18779    /// Insert each postcondition (`checks`) as a hard `RuntimeAssert` immediately
18780    /// before every `Return` reachable in `stmts`, recursing into the control-flow
18781    /// constructs whose returns EXIT the function (`If`/`While`/`Repeat`/`Inspect`/
18782    /// `Zone`). `Concurrent`/`Parallel` are intentionally left as leaves: their
18783    /// returns are swallowed (they don't exit the function), so a postcondition must
18784    /// NOT be inserted there. The fallthrough exit is handled by the caller.
18785    /// The author's statements keep their spans through the rebuild; the
18786    /// asserts are the parser's own and claim none.
18787    fn insert_ensures_before_returns(
18788        &mut self,
18789        stmts: StmtList<'a>,
18790        checks: &[&'a Expr<'a>],
18791    ) -> StmtList<'a> {
18792        let mut out = StmtList::default();
18793        for (mut s, span) in stmts {
18794            if matches!(s, Stmt::Return { .. }) {
18795                for &c in checks {
18796                    out.wrote(Stmt::RuntimeAssert {
18797                        condition: c,
18798                        hard: true,
18799                        dim: None,
18800                        reason: None,
18801                    });
18802                }
18803                out.carry(s, span);
18804                continue;
18805            }
18806            // Rewrite only the child blocks (via `..` patterns) — no need to
18807            // re-spell every field of every block-bearing variant.
18808            match &mut s {
18809                Stmt::If {
18810                    then_block,
18811                    else_block,
18812                    ..
18813                } => {
18814                    *then_block = self.guard_returns_in(then_block, checks);
18815                    if let Some(eb) = else_block {
18816                        *eb = self.guard_returns_in(eb, checks);
18817                    }
18818                }
18819                Stmt::While { body, .. } | Stmt::Repeat { body, .. } | Stmt::Zone { body, .. } => {
18820                    *body = self.guard_returns_in(body, checks);
18821                }
18822                Stmt::Inspect { arms, .. } => {
18823                    for arm in arms.iter_mut() {
18824                        arm.body = self.guard_returns_in(arm.body, checks);
18825                    }
18826                }
18827                Stmt::Select { branches } => {
18828                    // A `Return` in a `select!` arm EXITS the function (unlike
18829                    // Concurrent/Parallel, whose returns are swallowed) — guard it too.
18830                    for br in branches.iter_mut() {
18831                        match br {
18832                            crate::ast::stmt::SelectBranch::Receive { body, .. }
18833                            | crate::ast::stmt::SelectBranch::Timeout { body, .. } => {
18834                                *body = self.guard_returns_in(body, checks);
18835                            }
18836                        }
18837                    }
18838                }
18839                // Concurrent/Parallel: their Returns are swallowed (don't exit the
18840                // function), so the postcondition must NOT be inserted there.
18841                _ => {}
18842            }
18843            out.carry(s, span);
18844        }
18845        out
18846    }
18847
18848    /// An allocated child block rebuilt with `checks` before each of its returns.
18849    fn guard_returns_in(&mut self, block: &[Stmt<'a>], checks: &[&'a Expr<'a>]) -> &'a [Stmt<'a>] {
18850        let list = self.carried(block);
18851        let guarded = self.insert_ensures_before_returns(list, checks);
18852        self.alloc_read_block(guarded)
18853    }
18854
18855    /// Allocate a block of statements the parser wrote itself — a lowering's,
18856    /// a desugaring's. They claim no source; a block read from the source goes
18857    /// through [`Parser::alloc_read_block`].
18858    fn alloc_block(&self, stmts: Vec<Stmt<'a>>) -> &'a [Stmt<'a>] {
18859        self.ctx
18860            .stmts
18861            .expect("imperative arenas not initialized")
18862            .alloc_slice(stmts.into_iter())
18863    }
18864
18865    /// Allocate a block read from the source, registering each statement's
18866    /// span under its final identity in [`NestedStmtSpans`].
18867    fn alloc_read_block(&mut self, list: StmtList<'a>) -> &'a [Stmt<'a>] {
18868        let StmtList { stmts, spans } = list;
18869        let block = self.alloc_block(stmts);
18870        for (stmt, span) in block.iter().zip(spans) {
18871            if let Some(span) = span {
18872                self.nested_stmt_spans.0.insert(StmtId::of(stmt), span);
18873            }
18874        }
18875        block
18876    }
18877
18878    /// Read one statement of a block and the full stop that ends it into
18879    /// `into`, with the span it was read from. Every statement a block reads
18880    /// comes through here, so each is findable by identity once allocated.
18881    fn read_block_statement(&mut self, into: &mut StmtList<'a>) -> ParseResult<()> {
18882        let start = self.current;
18883        let stmt = self.parse_moving_statement()?;
18884        if self.check(&TokenType::Period) {
18885            self.advance();
18886        }
18887        into.read(stmt, self.span_since(start));
18888        Ok(())
18889    }
18890
18891    /// Parse one statement of a block, then settle what it moved: a move made
18892    /// inside its own nested blocks ended with them, and its own `Give` moves
18893    /// the value for the rest of the block it is written in.
18894    fn parse_moving_statement(&mut self) -> ParseResult<Stmt<'a>> {
18895        let mark = self.moves.len();
18896        let stmt = self.parse_statement()?;
18897        self.release_moves_since(mark);
18898        if let Stmt::Give {
18899            object: Expr::Identifier(sym),
18900            ..
18901        } = &stmt
18902        {
18903            let prior = self
18904                .world_state
18905                .get_ownership_by_var(*sym)
18906                .unwrap_or_default();
18907            self.moves.push((*sym, prior));
18908            self.world_state
18909                .set_ownership_by_var(*sym, crate::drs::OwnershipState::Moved);
18910        }
18911        Ok(stmt)
18912    }
18913
18914    /// Undo the moves made since `mark`, newest first. A move is certain only
18915    /// for the rest of its own block: once a branch, a loop body or a function
18916    /// ends, whether the value is still there is flow analysis — the ownership
18917    /// checker's, which reports it as a maybe-move rather than a definite one.
18918    fn release_moves_since(&mut self, mark: usize) {
18919        while self.moves.len() > mark {
18920            let (sym, prior) = self.moves.pop().expect("more moves than the mark");
18921            self.world_state.set_ownership_by_var(sym, prior);
18922        }
18923    }
18924
18925    /// Read one block's statements with `read`, then end the moves made in
18926    /// them: a `Give` moves its value for the rest of the block it is written
18927    /// in and no further — not past the block's end, and not into a sibling
18928    /// branch or arm read after it, which runs only when this block did not.
18929    fn read_scoped<T>(&mut self, read: impl FnOnce(&mut Self) -> ParseResult<T>) -> ParseResult<T> {
18930        let mark = self.moves.len();
18931        let block = read(self);
18932        self.release_moves_since(mark);
18933        block
18934    }
18935
18936    /// An indented block: its `Indent`, its statements, its closing `Dedent`.
18937    /// Not depth-guarded — the statements inside recurse through
18938    /// `parse_statement`, which is; [`Parser::parse_indented_block`] guards a
18939    /// block entered from elsewhere.
18940    fn read_indented_block(&mut self) -> ParseResult<&'a [Stmt<'a>]> {
18941        if !self.check(&TokenType::Indent) {
18942            return Err(ParseError {
18943                kind: ParseErrorKind::ExpectedStatement,
18944                span: self.current_span(),
18945            });
18946        }
18947        self.advance(); // consume Indent
18948        let list = self.read_indented_statements()?;
18949        Ok(self.alloc_read_block(list))
18950    }
18951
18952    /// Read the statements of an indented block whose `Indent` was just
18953    /// consumed, through its closing `Dedent`.
18954    fn read_indented_statements(&mut self) -> ParseResult<StmtList<'a>> {
18955        self.read_scoped(|parser| {
18956            let mut list = StmtList::default();
18957            while !parser.check(&TokenType::Dedent) && !parser.is_at_end() {
18958                parser.read_block_statement(&mut list)?;
18959            }
18960            if parser.check(&TokenType::Dedent) {
18961                parser.advance();
18962            }
18963            Ok(list)
18964        })
18965    }
18966
18967    /// A branch's body: an indented block, or (an inline guard) one statement
18968    /// on the same line — `If c: Return 1.`, `Otherwise: Show "x".`
18969    fn read_branch_body(&mut self) -> ParseResult<&'a [Stmt<'a>]> {
18970        let list = if self.check(&TokenType::Indent) {
18971            self.advance(); // consume Indent
18972            self.read_indented_statements()?
18973        } else {
18974            self.read_scoped(|parser| {
18975                let mut one = StmtList::default();
18976                parser.read_block_statement(&mut one)?;
18977                Ok(one)
18978            })?
18979        };
18980        Ok(self.alloc_read_block(list))
18981    }
18982
18983    /// A one-statement block holding a statement read from `start_tok` — an
18984    /// `Otherwise If` link, which is its own `If` inside the chain's else.
18985    fn read_link_block(&mut self, stmt: Stmt<'a>, start_tok: usize) -> &'a [Stmt<'a>] {
18986        let mut link = StmtList::default();
18987        link.read(stmt, self.span_since(start_tok));
18988        self.alloc_read_block(link)
18989    }
18990
18991    /// An allocated block's statements as a list to rebuild from, each
18992    /// carrying the span it was registered with.
18993    fn carried(&self, block: &[Stmt<'a>]) -> StmtList<'a> {
18994        let mut list = StmtList::default();
18995        for stmt in block {
18996            list.carry(stmt.clone(), self.nested_stmt_spans.get(StmtId::of(stmt)));
18997        }
18998        list
18999    }
19000
19001    /// Phase 32: Parse function definition after `## To` header
19002    /// Phase 32/38: Parse function definition
19003    /// Syntax: [To] [native] name (a: Type) [and (b: Type)] [-> ReturnType]
19004    ///         body statements... (only if not native)
19005    fn parse_function_def(&mut self) -> ParseResult<Stmt<'a>> {
19006        self.parse_function_def_with_flags(OptimizationConfig::all_on())
19007    }
19008
19009    /// Parse function definition with optimization annotation flags.
19010    fn parse_function_def_with_flags(
19011        &mut self,
19012        opt_flags: OptimizationConfig,
19013    ) -> ParseResult<Stmt<'a>> {
19014        // Consume "To" if present (when called from parse_statement)
19015        if self.check(&TokenType::To) || self.check_preposition_is("to") {
19016            self.advance();
19017        }
19018
19019        // Phase 38: Check for native modifier (prefix style: "## To native funcname ...")
19020        let mut is_native = if self.check(&TokenType::Native) {
19021            self.advance(); // consume "native"
19022            true
19023        } else {
19024            false
19025        };
19026
19027        // Parse function name (first identifier after ## To [native])
19028        let name = self.expect_identifier()?;
19029
19030        // Parse optional generic type parameters: "of [T]" or "of [T] and [U]"
19031        let mut parsed_generics: Vec<Symbol> = Vec::new();
19032        if self.check_preposition_is("of") {
19033            self.advance(); // consume "of"
19034            loop {
19035                if !self.check(&TokenType::LBracket) {
19036                    return Err(ParseError {
19037                        kind: ParseErrorKind::Custom(
19038                            "Expected '[TypeParam]' after 'of' in generic function".to_string(),
19039                        ),
19040                        span: self.current_span(),
19041                    });
19042                }
19043                self.advance(); // consume [
19044                let type_param = self.expect_identifier()?;
19045                parsed_generics.push(type_param);
19046                if !self.check(&TokenType::RBracket) {
19047                    return Err(ParseError {
19048                        kind: ParseErrorKind::Custom(
19049                            "Expected ']' after type parameter name".to_string(),
19050                        ),
19051                        span: self.current_span(),
19052                    });
19053                }
19054                self.advance(); // consume ]
19055                if self.check_word("and") {
19056                    self.advance(); // consume "and"
19057                } else {
19058                    break;
19059                }
19060            }
19061        }
19062
19063        // Parse parameters: (name: Type) groups separated by "and", or comma-separated in one group
19064        let mut params = Vec::new();
19065        while self.check(&TokenType::LParen) {
19066            self.advance(); // consume (
19067
19068            // Handle empty parameter list: ()
19069            if self.check(&TokenType::RParen) {
19070                self.advance(); // consume )
19071                break;
19072            }
19073
19074            // Parse parameters in this group (possibly comma-separated)
19075            loop {
19076                let param_name = self.expect_identifier()?;
19077
19078                // Expect colon
19079                if !self.check(&TokenType::Colon) {
19080                    return Err(ParseError {
19081                        kind: ParseErrorKind::ExpectedKeyword {
19082                            keyword: ":".to_string(),
19083                        },
19084                        span: self.current_span(),
19085                    });
19086                }
19087                self.advance(); // consume :
19088
19089                // MVS: a `mutable` parameter passes by reference (callee mutations
19090                // visible to caller) under value semantics — carried as a
19091                // `TypeExpr::Mutable` wrapper so the parameter tuple is unchanged.
19092                let param_is_mutable = self.check_mutable_keyword();
19093                if param_is_mutable {
19094                    self.advance(); // consume the `mutable`/`mut` keyword
19095                }
19096
19097                // Phase 38: Parse full type expression instead of simple identifier
19098                let param_type_expr = self.parse_type_expression()?;
19099                let param_type_inner = self.ctx.alloc_type_expr(param_type_expr);
19100                let param_type = if param_is_mutable {
19101                    self.ctx.alloc_type_expr(TypeExpr::Mutable {
19102                        inner: param_type_inner,
19103                    })
19104                } else {
19105                    param_type_inner
19106                };
19107
19108                params.push((param_name, param_type));
19109
19110                // Check for comma (more params in this group) or ) (end of group)
19111                if self.check(&TokenType::Comma) {
19112                    self.advance(); // consume ,
19113                    continue;
19114                }
19115                break;
19116            }
19117
19118            // Expect )
19119            if !self.check(&TokenType::RParen) {
19120                return Err(ParseError {
19121                    kind: ParseErrorKind::ExpectedKeyword {
19122                        keyword: ")".to_string(),
19123                    },
19124                    span: self.current_span(),
19125                });
19126            }
19127            self.advance(); // consume )
19128
19129            // Check for "and", preposition, or "from" between parameter groups
19130            // Allows: "## To withdraw (amount: Int) from (balance: Int)"
19131            if self.check_word("and") || self.check_preposition() || self.check(&TokenType::From) {
19132                self.advance();
19133            }
19134        }
19135
19136        // Phase 38: Parse optional return type -> Type
19137        let return_type = if self.check(&TokenType::Arrow) {
19138            self.advance(); // consume ->
19139            let ret_type_expr = self.parse_type_expression()?;
19140            Some(self.ctx.alloc_type_expr(ret_type_expr))
19141        } else {
19142            None
19143        };
19144
19145        // FFI: Parse optional suffix modifiers: "is native <path>", "is exported [for <target>]"
19146        let mut native_path: Option<Symbol> = None;
19147        let mut is_exported = false;
19148        let mut export_target: Option<Symbol> = None;
19149
19150        if self.check_word("is") {
19151            self.advance(); // consume "is"
19152            if self.check(&TokenType::Native) {
19153                // "is native" suffix style with required path string
19154                self.advance(); // consume "native"
19155                is_native = true;
19156                if let TokenType::StringLiteral(sym) = self.peek().kind {
19157                    native_path = Some(sym);
19158                    self.advance(); // consume the path string
19159                } else {
19160                    return Err(ParseError {
19161                        kind: ParseErrorKind::Custom(
19162                            "Expected a string literal for native function path (e.g., is native \"reqwest::blocking::get\")".to_string()
19163                        ),
19164                        span: self.current_span(),
19165                    });
19166                }
19167            } else if self.check_word("exported") {
19168                // "is exported [for <target>]"
19169                self.advance(); // consume "exported"
19170                is_exported = true;
19171                if self.check_word("for") {
19172                    self.advance(); // consume "for"
19173                                    // "native" lexes as a keyword token, not an identifier.
19174                    let target_sym = if self.check(&TokenType::Native) {
19175                        self.advance();
19176                        self.interner.intern("native")
19177                    } else {
19178                        self.expect_identifier()?
19179                    };
19180                    let target_str = self.interner.resolve(target_sym);
19181                    if !target_str.eq_ignore_ascii_case("c")
19182                        && !target_str.eq_ignore_ascii_case("wasm")
19183                        && !target_str.eq_ignore_ascii_case("native")
19184                    {
19185                        return Err(ParseError {
19186                            kind: ParseErrorKind::Custom(
19187                                format!("Unsupported export target \"{}\". Supported targets are \"c\", \"wasm\", and \"native\".", target_str)
19188                            ),
19189                            span: self.current_span(),
19190                        });
19191                    }
19192                    export_target = Some(target_sym);
19193                }
19194            }
19195        }
19196
19197        // Phase 38: Native functions have no body
19198        if is_native {
19199            // Consume trailing period or newline if present
19200            if self.check(&TokenType::Period) {
19201                self.advance();
19202            }
19203            if self.check(&TokenType::Newline) {
19204                self.advance();
19205            }
19206
19207            // Return with empty body
19208            let empty_body = self.alloc_block(Vec::new());
19209
19210            return Ok(Stmt::FunctionDef {
19211                name,
19212                generics: parsed_generics,
19213                params,
19214                body: empty_body,
19215                return_type,
19216                is_native: true,
19217                native_path,
19218                is_exported: false,
19219                export_target: None,
19220                opt_flags,
19221            });
19222        }
19223
19224        // Non-native: expect colon after parameter list / return type
19225        if !self.check(&TokenType::Colon) {
19226            return Err(ParseError {
19227                kind: ParseErrorKind::ExpectedKeyword {
19228                    keyword: ":".to_string(),
19229                },
19230                span: self.current_span(),
19231            });
19232        }
19233        self.advance(); // consume :
19234
19235        // `## To badge (label: Text) -> View:` — a MACRO: the body is a template, and what the
19236        // function returns is the view it renders. Nothing else about it is special; it is an
19237        // ordinary function whose value happens to be a `View`, so it composes with props,
19238        // collections and `Show` like any other.
19239        if self.returns_a_view(return_type) {
19240            if let Some(raw) = self.consume_view_body() {
19241                let span = self.current_span();
19242                let view = self.view_from_raw(raw, None, span)?;
19243                let value = self.ctx.alloc_imperative_expr(Expr::View(Box::new(view)));
19244                let body = self.alloc_block(vec![Stmt::Return { value: Some(value) }]);
19245                return Ok(Stmt::FunctionDef {
19246                    name,
19247                    generics: parsed_generics,
19248                    params,
19249                    body,
19250                    return_type,
19251                    is_native: false,
19252                    native_path: None,
19253                    is_exported: false,
19254                    export_target: None,
19255                    opt_flags,
19256                });
19257            }
19258        }
19259
19260        // Expect indent for function body
19261        if !self.check(&TokenType::Indent) {
19262            return Err(ParseError {
19263                kind: ParseErrorKind::ExpectedStatement,
19264                span: self.current_span(),
19265            });
19266        }
19267        self.advance(); // consume Indent
19268
19269        // Scope the `=`-mutation decision (and the infinity/nan literal guard)
19270        // to THIS function body: seed a fresh bound-set with the params, restore
19271        // the outer set on exit so a sibling function's or Main's `x = e` never
19272        // mistakes an out-of-scope `x` for a local (which would wrongly emit a
19273        // `Set` on an unbound name).
19274        let saved_user_bound = std::mem::take(&mut self.user_bound);
19275        for (param_name, _) in &params {
19276            self.user_bound.insert(*param_name);
19277        }
19278
19279        // The value-semantics copy lint tracks one function body at a time; saved/restored the
19280        // same way as `user_bound` above, so a nested body (a closure literal parsed inside this
19281        // one) can never leak its own tracking into the outer function's.
19282        let outer_copy_lint_scope = self
19283            .copy_lint_scope
19284            .replace(crate::lint::CopyLintScope::new());
19285
19286        // Parse body statements
19287        let mut body_stmts = StmtList::default();
19288        // Contract clauses: `Requires <cond>.` (precondition, checked at entry) and
19289        // `Ensures <cond>.` (postcondition, checked before every return). Collected
19290        // here and desugared into enforced (hard) asserts below — NOT emitted as
19291        // ordinary statements.
19292        let mut requires_checks: Vec<&'a Expr<'a>> = Vec::new();
19293        let mut ensures_checks: Vec<&'a Expr<'a>> = Vec::new();
19294        while !self.check(&TokenType::Dedent) && !self.is_at_end() {
19295            // Skip newlines between statements
19296            if self.check(&TokenType::Newline) {
19297                self.advance();
19298                continue;
19299            }
19300            // Stop if we hit another block header
19301            if matches!(self.peek().kind, TokenType::BlockHeader { .. }) {
19302                break;
19303            }
19304            if self.check(&TokenType::Requires) {
19305                self.advance(); // consume "Requires"
19306                requires_checks.push(self.parse_condition()?);
19307                if self.check(&TokenType::Period) {
19308                    self.advance();
19309                }
19310                continue;
19311            }
19312            if self.check(&TokenType::Ensures) {
19313                self.advance(); // consume "Ensures"
19314                ensures_checks.push(self.parse_condition()?);
19315                if self.check(&TokenType::Period) {
19316                    self.advance();
19317                }
19318                continue;
19319            }
19320            self.read_block_statement(&mut body_stmts)?;
19321        }
19322
19323        // Consume dedent if present
19324        if self.check(&TokenType::Dedent) {
19325            self.advance();
19326        }
19327
19328        // Leave the function scope: the outer bound-set regains control.
19329        self.user_bound = saved_user_bound;
19330        if let Some(scope) = std::mem::replace(&mut self.copy_lint_scope, outer_copy_lint_scope) {
19331            self.value_copy_findings.extend(scope.finish(self.interner));
19332        }
19333
19334        // Desugar postconditions: a hard assert before EVERY return path (recursing
19335        // into nested control flow) and at fallthrough — so no exit escapes the check.
19336        if !ensures_checks.is_empty() {
19337            body_stmts = self.insert_ensures_before_returns(body_stmts, &ensures_checks);
19338            if !matches!(body_stmts.last(), Some(Stmt::Return { .. })) {
19339                for &c in &ensures_checks {
19340                    body_stmts.wrote(Stmt::RuntimeAssert {
19341                        condition: c,
19342                        hard: true,
19343                        dim: None,
19344                        reason: None,
19345                    });
19346                }
19347            }
19348        }
19349        // Desugar preconditions: hard asserts at function entry.
19350        if !requires_checks.is_empty() {
19351            let mut prefixed = StmtList::default();
19352            for &c in &requires_checks {
19353                prefixed.wrote(Stmt::RuntimeAssert {
19354                    condition: c,
19355                    hard: true,
19356                    dim: None,
19357                    reason: None,
19358                });
19359            }
19360            prefixed.append(body_stmts);
19361            body_stmts = prefixed;
19362        }
19363
19364        // Allocate body in arena
19365        let body = self.alloc_read_block(body_stmts);
19366
19367        Ok(Stmt::FunctionDef {
19368            name,
19369            generics: parsed_generics,
19370            params,
19371            body,
19372            return_type,
19373            is_native: false,
19374            native_path: None,
19375            is_exported,
19376            export_target,
19377            opt_flags,
19378        })
19379    }
19380
19381    /// Parse a primary expression (literal, identifier, index, slice, list, etc.)
19382    /// Depth-guarded wrapper: primary expressions are the re-entry point of
19383    /// the imperative expression descent (`( expr )`, prefix forms), so a
19384    /// parenthesis tower recurses here once per level — the guard turns a
19385    /// would-be parser stack overflow into a graceful `AstTooDeep`.
19386    fn parse_primary_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
19387        self.enter_recursion()?;
19388        self.eat_article_before_possessive();
19389        let result = self.parse_primary_expr_inner();
19390        self.leave_recursion();
19391        result
19392    }
19393
19394    /// Refuse a page-query selector the engine cannot evaluate.
19395    ///
19396    /// ★ REFUSED WHERE IT IS WRITTEN, because the alternative is not an error — it is the ANSWER
19397    /// `0`. `lgx/selector.rs` reads a descendant chain of tag / `#id` / `.class` / `[attr]`
19398    /// compounds and nothing else, and its `compound()` takes the leading run as a TAG NAME, so
19399    /// `ul > li` parsed as a tag called `>` and matched nothing. That is indistinguishable from a
19400    /// page which genuinely has no matches, so `Expect the count of "ul > li" to be 0.` passed and
19401    /// would have passed with five hundred items on the page.
19402    ///
19403    /// Only a LITERAL is checked, which is every selector anybody writes; a computed one cannot be
19404    /// read here and is not accused.
19405    fn check_selector_literal(&mut self, selector: &'a Expr<'a>) -> ParseResult<()> {
19406        let Expr::Literal(Literal::Text(sym)) = selector else {
19407            return Ok(());
19408        };
19409        let text = self.interner.resolve(*sym).to_string();
19410        match crate::lgx::selector::unsupported(&text) {
19411            Some(why) => Err(ParseError {
19412                kind: ParseErrorKind::Custom(why),
19413                span: self.current_span(),
19414            }),
19415            None => Ok(()),
19416        }
19417    }
19418
19419    /// Consume a leading `the` when it introduces a POSSESSIVE — `the todo's id`.
19420    ///
19421    /// LOGOS reads as English everywhere else, and English puts an article in front of a possessed
19422    /// noun. Only `todo's id` parsed, so the sentence a person actually writes was refused while
19423    /// the terser one was accepted — a position nobody had covered rather than a decision anybody
19424    /// made, and the refusal ("I expected a statement here") never mentioned the article, so the
19425    /// fix was the one thing the author would not think to try.
19426    ///
19427    /// ★ THE LOOKAHEAD IS TWO TOKENS DEEP, AND HAS TO BE. `the count of xs` is an existing form
19428    /// whose `the` belongs to the form itself, so an article cannot be skipped wherever one
19429    /// appears. What tells them apart is the token after the noun — `of` there, `'s` here — so
19430    /// this fires only for `the <name>'s`, which no other form claims. Deliberately NARROW:
19431    /// `the x` on its own stays refused, because widening the article rule to every expression is
19432    /// a separate decision with its own ambiguities and taking it silently here would be an
19433    /// unrequested change.
19434    fn eat_article_before_possessive(&mut self) {
19435        if self.check_word("the")
19436            && self
19437                .tokens
19438                .get(self.current + 2)
19439                .is_some_and(|t| matches!(t.kind, TokenType::Possessive))
19440        {
19441            self.advance();
19442        }
19443    }
19444
19445    fn parse_primary_expr_inner(&mut self) -> ParseResult<&'a Expr<'a>> {
19446        use crate::ast::{Expr, Literal};
19447
19448        // `count of xs` has no meaning of its own: the size of a collection is `length of xs`, and
19449        // `the count of "li"` asks how many page elements match a selector. Refused with both.
19450        if self.check_word("count") && self.peek_word_at(1, "of") {
19451            let start = self.current_span().start;
19452            let end = self.tokens[self.current + 1].span.end;
19453            let operand = self
19454                .tokens
19455                .get(self.current + 2)
19456                .map(|t| self.interner.resolve(t.lexeme).to_string())
19457                .filter(|w| {
19458                    w.chars()
19459                        .next()
19460                        .is_some_and(|c| c.is_alphabetic() || c == '_')
19461                })
19462                .unwrap_or_else(|| "xs".to_string());
19463            return Err(ParseError {
19464                kind: ParseErrorKind::Custom(format!(
19465                    "`count of {operand}` is not an expression — did you mean `length of {operand}`? \
19466                     The number of items a collection holds is its `length of`; `the count of \
19467                     \".row\"` (with `the`, and a selector) asks how many page elements match."
19468                )),
19469                span: Span::new(start, end),
19470            });
19471        }
19472
19473        // English prefix arithmetic: `the sum/product/difference/quotient/remainder of A and B`
19474        // → the matching binary op. Unambiguous (the distinctive `the <op-noun> of` prefix), so it
19475        // never shadows a plain variable. `A`/`B` are full sub-expressions; the `and` delimiter is
19476        // consumed here (it binds tighter than the boolean `and`, which lives far above additive).
19477        if self.check_word("the") && self.peek_word_at(2, "of") {
19478            // Questions about the rendered page share the shape `the <noun> of <x>` already used
19479            // for `the sum of a and b`, so they are answered HERE rather than in a branch of
19480            // their own: `parse_primary_expr_inner` is the fattest frame in the parser's descent
19481            // (the parse-recursion budget is measured against it), and one more early-return
19482            // path in it costs stack on every expression in every program.
19483            let page = if self.peek_word_at(1, "text") {
19484                Some("text")
19485            } else if self.peek_word_at(1, "count") {
19486                Some("count")
19487            } else if self.peek_word_at(1, "value") {
19488                Some("value")
19489            } else {
19490                None
19491            };
19492            if let Some(noun) = page {
19493                self.advance(); // "the"
19494                self.advance(); // the noun
19495                self.advance(); // "of"
19496                let selector = self.parse_page_query_operand()?;
19497                self.check_selector_literal(selector)?;
19498                // `the count of` spells two forms: how many page elements match a selector, and
19499                // how many items a collection holds (`count_of_collection_lock`). The operand
19500                // decides: a selector is written as a string (`.todo`, `#save`), and `the journal`
19501                // is the one non-literal a page query takes; anything else is a value, and its
19502                // count is its length.
19503                let is_page = matches!(selector, Expr::Literal(Literal::Text(_)) | Expr::Dom(_));
19504                if noun == "count" && !is_page {
19505                    return Ok(self.ctx.alloc_imperative_expr(Expr::Length {
19506                        collection: selector,
19507                    }));
19508                }
19509                let query = match noun {
19510                    "text" => crate::ast::stmt::DomQuery::Text { selector },
19511                    "count" => crate::ast::stmt::DomQuery::Count { selector },
19512                    _ => crate::ast::stmt::DomQuery::Value { selector },
19513                };
19514                return Ok(self.ctx.alloc_imperative_expr(Expr::Dom(query)));
19515            }
19516            let op = if self.peek_word_at(1, "sum") {
19517                Some(BinaryOpKind::Add)
19518            } else if self.peek_word_at(1, "product") {
19519                Some(BinaryOpKind::Multiply)
19520            } else if self.peek_word_at(1, "difference") {
19521                Some(BinaryOpKind::Subtract)
19522            } else if self.peek_word_at(1, "quotient") {
19523                Some(BinaryOpKind::Divide)
19524            } else if self.peek_word_at(1, "remainder") {
19525                Some(BinaryOpKind::Modulo)
19526            } else {
19527                None
19528            };
19529            if let Some(op) = op {
19530                self.advance(); // "the"
19531                self.advance(); // op-noun
19532                self.advance(); // "of"
19533                let a = self.parse_additive_expr()?;
19534                if self.check(&TokenType::And) || self.check_word("and") {
19535                    self.advance(); // "and"
19536                } else {
19537                    return Err(ParseError {
19538                        kind: ParseErrorKind::ExpectedKeyword {
19539                            keyword: "and".to_string(),
19540                        },
19541                        span: self.current_span(),
19542                    });
19543                }
19544                let b = self.parse_additive_expr()?;
19545                return Ok(self.ctx.alloc_imperative_expr(Expr::BinaryOp {
19546                    op,
19547                    left: a,
19548                    right: b,
19549                }));
19550            }
19551        }
19552        // `html of <Component>` — the component's view as SERVER-RENDERED markup. Reads as a
19553        // value, so `Show html of App.` and `Expect output of html of App:` both work with the
19554        // forms LOGOS already has.
19555        if self.check_word("html") && self.peek_word_at(1, "of") {
19556            self.advance(); // "html"
19557            self.advance(); // "of"
19558            let name = self.expect_identifier()?;
19559            let f = self
19560                .interner
19561                .intern(&crate::parser::lgx_lower::html_fn_name(
19562                    self.interner.resolve(name),
19563                ));
19564            return Ok(self.ctx.alloc_imperative_expr(Expr::Call {
19565                function: f,
19566                args: Vec::new(),
19567            }));
19568        }
19569        if self.check_word("the")
19570            && self.peek_word_at(1, "standard")
19571            && self.peek_word_at(2, "part")
19572            && self.peek_word_at(3, "of")
19573        {
19574            return self.parse_standard_part();
19575        }
19576        if let Some(real) = self.try_parse_defined_real()? {
19577            return Ok(real);
19578        }
19579        // The page questions with no selector operand.
19580        if self.check_word("the") {
19581            // `the journal shows A then B` — the ORDERING question, which is the one thing a
19582            // sequence operation cannot ask about the journal without an index-of. It has to be
19583            // recognised BEFORE bare `the journal`, or the `shows` clause would be left dangling
19584            // as a statement nobody can parse.
19585            if self.peek_word_at(1, "journal") && self.peek_word_at(2, "shows") {
19586                self.advance(); // "the"
19587                self.advance(); // "journal"
19588                self.advance(); // "shows"
19589                let first = self.parse_journal_action_name()?;
19590                if !self.check_word("then") {
19591                    return Err(ParseError {
19592                        kind: ParseErrorKind::ExpectedKeyword {
19593                            keyword: "then".to_string(),
19594                        },
19595                        span: self.current_span(),
19596                    });
19597                }
19598                self.advance(); // "then"
19599                let second = self.parse_journal_action_name()?;
19600                let query = crate::ast::stmt::DomQuery::JournalShows { first, second };
19601                return Ok(self.ctx.alloc_imperative_expr(Expr::Dom(query)));
19602            }
19603            let bare = if self.peek_word_at(1, "title") {
19604                Some(crate::ast::stmt::DomQuery::Title)
19605            } else if self.peek_word_at(1, "URL") {
19606                Some(crate::ast::stmt::DomQuery::Url)
19607            } else if self.peek_word_at(1, "journal") {
19608                Some(crate::ast::stmt::DomQuery::Journal)
19609            } else {
19610                None
19611            };
19612            if let Some(query) = bare {
19613                self.advance(); // "the"
19614                self.advance(); // the noun
19615                return Ok(self.ctx.alloc_imperative_expr(Expr::Dom(query)));
19616            }
19617        }
19618
19619        // Natural timestamp literal: `timestamp "2024-03-10T07:30:00Z"` → a Moment. Reads as a
19620        // value, not a function call; lowers to the (invisible) parse_timestamp. Only fires when an
19621        // identifier `timestamp` is immediately followed by a string, so a variable named
19622        // `timestamp` used on its own is unaffected.
19623        if matches!(self.peek().kind, TokenType::Identifier | TokenType::Noun(_))
19624            && self
19625                .interner
19626                .resolve(self.peek().lexeme)
19627                .eq_ignore_ascii_case("timestamp")
19628        {
19629            let str_sym = match self.tokens.get(self.current + 1).map(|t| &t.kind) {
19630                Some(TokenType::StringLiteral(s)) => Some(*s),
19631                _ => None,
19632            };
19633            if let Some(s) = str_sym {
19634                self.advance(); // "timestamp"
19635                self.advance(); // the string
19636                let text = self
19637                    .ctx
19638                    .alloc_imperative_expr(Expr::Literal(Literal::Text(s)));
19639                let func = self.interner.intern("parse_timestamp");
19640                return Ok(self.ctx.alloc_imperative_expr(Expr::Call {
19641                    function: func,
19642                    args: vec![text],
19643                }));
19644            }
19645        }
19646
19647        // Natural UUID literal: `uuid "550e8400-…"` → a Uuid (parses/normalizes the text). Reads as a
19648        // value; lowers to the invisible `uuid(text)` builtin. Only fires when the identifier `uuid`
19649        // is immediately followed by a string, so a variable named `uuid` is unaffected.
19650        if matches!(self.peek().kind, TokenType::Identifier | TokenType::Noun(_))
19651            && self
19652                .interner
19653                .resolve(self.peek().lexeme)
19654                .eq_ignore_ascii_case("uuid")
19655        {
19656            let str_sym = match self.tokens.get(self.current + 1).map(|t| &t.kind) {
19657                Some(TokenType::StringLiteral(s)) => Some(*s),
19658                _ => None,
19659            };
19660            if let Some(s) = str_sym {
19661                self.advance(); // "uuid"
19662                self.advance(); // the string
19663                let text = self
19664                    .ctx
19665                    .alloc_imperative_expr(Expr::Literal(Literal::Text(s)));
19666                let func = self.interner.intern("uuid");
19667                return Ok(self.ctx.alloc_imperative_expr(Expr::Call {
19668                    function: func,
19669                    args: vec![text],
19670                }));
19671            }
19672        }
19673
19674        let token = self.peek().clone();
19675        match &token.kind {
19676            // Phase 31: Constructor expression "new TypeName" or "a new TypeName"
19677            // Phase 33: Extended for variant constructors "new Circle with radius 10"
19678            // Phase 34: Extended for generic instantiation "new Box of Int"
19679            TokenType::New => {
19680                // Identifier freedom: `new` names a variable when nothing that can
19681                // start a type name follows (`{new}`, `new * 2`, `new(3)`).
19682                let next_starts_type = matches!(
19683                    self.tokens.get(self.current + 1).map(|t| &t.kind),
19684                    Some(TokenType::Noun(_)
19685                        | TokenType::ProperName(_)
19686                        | TokenType::Adjective(_)
19687                        | TokenType::Identifier
19688                        | TokenType::Verb { .. }
19689                        | TokenType::Tally
19690                        | TokenType::SharedSet
19691                        | TokenType::SharedSequence
19692                        | TokenType::CollaborativeSequence
19693                        | TokenType::Pipe
19694                        | TokenType::Task)
19695                );
19696                if !next_starts_type {
19697                    let sym = token.lexeme;
19698                    self.advance();
19699                    if self.check(&TokenType::LParen) {
19700                        return self.parse_call_expr(sym);
19701                    }
19702                    self.verify_identifier_access(sym, token.span)?;
19703                    let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
19704                    return self.parse_field_access_chain(base);
19705                }
19706                self.advance(); // consume "new"
19707                let type_name_span = self.current_span();
19708                let base_type_name = self.expect_identifier()?;
19709
19710                // Phase 33/36/qualified-variant: "from" is either an enum naming its own
19711                // variant, or Phase 36 module qualification — see `resolve_construction_target`.
19712                let type_name = match self.resolve_construction_target(base_type_name, type_name_span)? {
19713                    ConstructionTarget::Variant { enum_name, variant } => {
19714                        let fields = if self.check_word("with") {
19715                            self.parse_variant_constructor_fields(variant, Some(enum_name))?
19716                        } else {
19717                            vec![]
19718                        };
19719                        let base = self.ctx.alloc_imperative_expr(Expr::NewVariant {
19720                            enum_name,
19721                            variant,
19722                            fields,
19723                        });
19724                        return self.parse_field_access_chain(base);
19725                    }
19726                    ConstructionTarget::Type(type_name) => type_name,
19727                };
19728
19729                // Phase 34: Parse generic type arguments "of Int" or "of Int and Text"
19730                let type_args = self.parse_generic_type_args(type_name)?;
19731
19732                // Parse optional "with field value" pairs for struct initialization
19733                // But NOT "with capacity" — that's a pre-allocation hint handled by parse_let_statement
19734                let init_fields = if self.check_word("with") && !self.peek_word_at(1, "capacity") {
19735                    self.parse_struct_init_fields(type_name)?
19736                } else {
19737                    vec![]
19738                };
19739
19740                // ★ THE TWO SURFACE FORMS OF AN OPTION CONVERGE HERE, at the AST, so that no
19741                // tier below ever has to guess which one it is holding.
19742                //
19743                // `Option` is registered as a `TypeDef::Generic` rather than an enum, so
19744                // `find_variant("Some")` misses and `a new Some with value x` used to fall through
19745                // to `Expr::New` — a RECORD named "Some". The two tiers then disagreed about what
19746                // that record was: the tree-walker's `Some` arm looked for a field to bind and
19747                // errored `Undefined variable: n`, while the VM's `SOME_BINDING_MARKER` bound the
19748                // whole struct and printed `Some { value: 7 }`. Two tiers, two different wrong
19749                // answers, for a form `stdlib_result_interp` calls equivalent to `some x`
19750                // ("One Option, one shape") — and untested across tiers, which is why it survived.
19751                //
19752                // `Some`/`None` are already engine-owned names: `loader.rs`'s `builtin_backed`
19753                // lists them beside `List`/`Map` and strips any declaration of them out of the
19754                // prelude. Matching the exact Option shape (one `value` field, or none at all)
19755                // keeps a differently-shaped user record reading as a record.
19756                if let Some(expr) = self.option_form_of(type_name, &type_args, &init_fields) {
19757                    let base = self.ctx.alloc_imperative_expr(expr);
19758                    return self.parse_field_access_chain(base);
19759                }
19760
19761                let base = self.ctx.alloc_imperative_expr(Expr::New { type_name, type_args, init_fields });
19762                return self.parse_field_access_chain(base);
19763            }
19764
19765            // Phase 31: Handle "a new TypeName" pattern OR single-letter identifier
19766            // Phase 33: Extended for variant constructors "a new Circle with radius 10"
19767            // Phase 34: Extended for generic instantiation "a new Box of Int"
19768            TokenType::Article(_) => {
19769                // Natural date-component accessor: `the year|month|day|weekday of <moment>` reads as
19770                // a sentence and desugars to the extractor (which threads through every tier). The
19771                // component words lex as CalendarUnit/Noun; we match by lexeme and require `of` next.
19772                let comp_lex = self.tokens.get(self.current + 1).map(|t| t.lexeme);
19773                let of_lex = self.tokens.get(self.current + 2).map(|t| t.lexeme);
19774                if let (Some(cl), Some(ol)) = (comp_lex, of_lex) {
19775                    let extractor = match self.interner.resolve(cl).to_ascii_lowercase().as_str() {
19776                        "year" => Some("year_of"),
19777                        "month" => Some("month_of"),
19778                        "day" => Some("day_of"),
19779                        "weekday" => Some("weekday_of"),
19780                        "hour" => Some("hour_of"),
19781                        "minute" => Some("minute_of"),
19782                        "second" => Some("second_of"),
19783                        "date" => Some("date_of"),
19784                        "time" => Some("time_of"),
19785                        "week" => Some("week_of"),
19786                        "quarter" => Some("quarter_of"),
19787                        _ => None,
19788                    };
19789                    if let Some(extractor) = extractor {
19790                        if self.interner.resolve(ol).eq_ignore_ascii_case("of") {
19791                            self.advance(); // "the"
19792                            self.advance(); // component noun
19793                            self.advance(); // "of"
19794                            let mut operand = self.parse_primary_expr()?;
19795                            // Zoned read: `the hour of m in "Asia/Tokyo"` → the LOCAL component. We
19796                            // intercept the `in "<zone>"` here (before it reaches the in_zone postfix,
19797                            // which would wrongly wrap the resulting Int) and shift the operand to the
19798                            // zone's local-as-UTC instant, so the SAME extractor yields the local field.
19799                            if self.check(&TokenType::In) || self.check_preposition_is("in") {
19800                                let zone_sym = match self.tokens.get(self.current + 1).map(|t| &t.kind) {
19801                                    Some(TokenType::StringLiteral(z)) => Some(*z),
19802                                    _ => None,
19803                                };
19804                                if let Some(z) = zone_sym {
19805                                    self.advance(); // "in"
19806                                    self.advance(); // the zone string
19807                                    let zone = self
19808                                        .ctx
19809                                        .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Text(z)));
19810                                    let local = self.interner.intern("local_instant");
19811                                    operand = self.ctx.alloc_imperative_expr(Expr::Call {
19812                                        function: local,
19813                                        args: vec![operand, zone],
19814                                    });
19815                                }
19816                            }
19817                            let func = self.interner.intern(extractor);
19818                            return Ok(self
19819                                .ctx
19820                                .alloc_imperative_expr(Expr::Call { function: func, args: vec![operand] }));
19821                        }
19822                    }
19823                    // Natural elapsed-time: `the <unit> between <a> and <b>`. FIXED-width units
19824                    // (second…week) divide the second-difference; CALENDAR units (month/year) count
19825                    // complete periods (a month is 28–31 days, so it cannot be a fixed division).
19826                    let unit_lexeme = self.interner.resolve(cl).to_ascii_lowercase();
19827                    let per_unit: Option<i64> = match unit_lexeme.as_str() {
19828                        "second" | "seconds" => Some(1),
19829                        "minute" | "minutes" => Some(60),
19830                        "hour" | "hours" => Some(3600),
19831                        "day" | "days" => Some(86400),
19832                        "week" | "weeks" => Some(604800),
19833                        _ => None,
19834                    };
19835                    let cal_func: Option<&str> = match unit_lexeme.as_str() {
19836                        "month" | "months" => Some("months_between"),
19837                        "year" | "years" => Some("years_between"),
19838                        _ => None,
19839                    };
19840                    if (per_unit.is_some() || cal_func.is_some())
19841                        && self.interner.resolve(ol).eq_ignore_ascii_case("between")
19842                    {
19843                        self.advance(); // "the"
19844                        self.advance(); // unit noun
19845                        self.advance(); // "between"
19846                        let a = self.parse_primary_expr()?;
19847                        if !(self.check(&TokenType::And) || self.check_word("and")) {
19848                            return Err(ParseError {
19849                                kind: ParseErrorKind::ExpectedKeyword { keyword: "and".to_string() },
19850                                span: self.current_span(),
19851                            });
19852                        }
19853                        self.advance(); // "and"
19854                        let b = self.parse_primary_expr()?;
19855                        if let Some(cal) = cal_func {
19856                            let func = self.interner.intern(cal);
19857                            return Ok(self
19858                                .ctx
19859                                .alloc_imperative_expr(Expr::Call { function: func, args: vec![a, b] }));
19860                        }
19861                        let per_unit = per_unit.unwrap();
19862                        let func = self.interner.intern("seconds_between");
19863                        let secs = self.ctx.alloc_imperative_expr(Expr::Call { function: func, args: vec![a, b] });
19864                        return Ok(if per_unit == 1 {
19865                            secs
19866                        } else {
19867                            let d = self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Number(per_unit)));
19868                            self.ctx.alloc_imperative_expr(Expr::BinaryOp {
19869                                op: BinaryOpKind::Divide,
19870                                left: secs,
19871                                right: d,
19872                            })
19873                        });
19874                    }
19875                }
19876                // Phase 48: Check if followed by Manifest or Chunk token
19877                // Pattern: "the manifest of Zone" or "the chunk at N in Zone"
19878                if let Some(next) = self.tokens.get(self.current + 1) {
19879                    if matches!(next.kind, TokenType::Manifest) {
19880                        self.advance(); // consume "the"
19881                        // Delegate to Manifest handling
19882                        return self.parse_primary_expr();
19883                    }
19884                    if matches!(next.kind, TokenType::Chunk) {
19885                        self.advance(); // consume "the"
19886                        // Delegate to Chunk handling
19887                        return self.parse_primary_expr();
19888                    }
19889                    if matches!(next.kind, TokenType::Length) {
19890                        self.advance(); // consume "the"
19891                        return self.parse_primary_expr();
19892                    }
19893                }
19894                // Check if followed by New token
19895                if let Some(next) = self.tokens.get(self.current + 1) {
19896                    if matches!(next.kind, TokenType::New) {
19897                        self.advance(); // consume article "a"/"an"
19898                        self.advance(); // consume "new"
19899                        let type_name_span = self.current_span();
19900                        let base_type_name = self.expect_identifier()?;
19901
19902                        // Phase 33/36/qualified-variant: see `resolve_construction_target`.
19903                        let type_name = match self.resolve_construction_target(base_type_name, type_name_span)? {
19904                            ConstructionTarget::Variant { enum_name, variant } => {
19905                                let fields = if self.check_word("with") {
19906                                    self.parse_variant_constructor_fields(variant, Some(enum_name))?
19907                                } else {
19908                                    vec![]
19909                                };
19910                                let base = self.ctx.alloc_imperative_expr(Expr::NewVariant {
19911                                    enum_name,
19912                                    variant,
19913                                    fields,
19914                                });
19915                                return self.parse_field_access_chain(base);
19916                            }
19917                            ConstructionTarget::Type(type_name) => type_name,
19918                        };
19919
19920                        // Phase 34: Parse generic type arguments "of Int" or "of Int and Text"
19921                        let type_args = self.parse_generic_type_args(type_name)?;
19922
19923                        // Parse optional "with field value" pairs for struct initialization
19924                        // But NOT "with capacity" — that's a pre-allocation hint handled by parse_let_statement
19925                        let init_fields = if self.check_word("with") && !self.peek_word_at(1, "capacity") {
19926                            self.parse_struct_init_fields(type_name)?
19927                        } else {
19928                            vec![]
19929                        };
19930
19931                        // The Option forms converge here too — see `option_form_of`. This is the
19932                        // `a new <Type>` path reached through the single-letter/qualified branch;
19933                        // a fix applied to only one of the two construction sites is the shape
19934                        // this repository keeps paying for.
19935                        if let Some(expr) = self.option_form_of(type_name, &type_args, &init_fields) {
19936                            let base = self.ctx.alloc_imperative_expr(expr);
19937                            return self.parse_field_access_chain(base);
19938                        }
19939
19940                        let base = self.ctx.alloc_imperative_expr(Expr::New { type_name, type_args, init_fields });
19941                        return self.parse_field_access_chain(base);
19942                    }
19943                }
19944                // Phase 32: Treat as identifier (single-letter var like "a", "b")
19945                let sym = token.lexeme;
19946                self.advance();
19947                let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
19948                return self.parse_field_access_chain(base);
19949            }
19950
19951            // Identifier freedom: `swap` in EXPRESSION position names a user
19952            // function or variable (`Show swap(1, 2)`) — the reversible-Swap
19953            // keyword only leads statements. The `New`-arm pattern.
19954            TokenType::Swap => {
19955                let sym = token.lexeme;
19956                self.advance();
19957                if self.check(&TokenType::LParen) {
19958                    return self.parse_call_expr(sym);
19959                }
19960                let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
19961                return self.parse_field_access_chain(base);
19962            }
19963
19964            // Declarer-wins: a BOUND name whose word happens to classify as an
19965            // adjective (`total`, `left`) is still that variable in expression
19966            // position — the same rescue single-letter articles get above.
19967            // Unbound adjectives keep their predicate reading (`x is red`),
19968            // and a following `(` yields to the CALL machinery (`outer(5)` —
19969            // a bound closure named by an adjective-word stays callable).
19970            TokenType::Adjective(_)
19971                if self.user_bound.contains(&token.lexeme)
19972                    && !matches!(
19973                        self.tokens.get(self.current + 1).map(|t| &t.kind),
19974                        Some(TokenType::LParen)
19975                    ) => {
19976                let sym = token.lexeme;
19977                self.advance();
19978                let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
19979                return self.parse_field_access_chain(base);
19980            }
19981
19982            // Index access: "item N of collection" or "item i of collection".
19983            // `item` is ALSO a perfectly ordinary variable name (`for each item in items`), so
19984            // it only opens the indexing form when an index actually follows; standing alone —
19985            // at the end of a clause, or as an argument — it is that variable.
19986            TokenType::Item if !self.item_opens_an_index() => {
19987                let sym = self.peek().lexeme;
19988                self.advance();
19989                let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
19990                return self.parse_field_access_chain(base);
19991            }
19992            TokenType::Item => {
19993                self.advance(); // consume "item"
19994
19995                // Grand Challenge: Parse index as expression (number, identifier, or parenthesized)
19996                let index = if let TokenType::Number(sym) = &self.peek().kind {
19997                    // Literal numeral — 0 is guarded, floats are real keys.
19998                    let sym = *sym;
19999                    self.advance();
20000                    self.parse_index_numeral(sym)?
20001                } else if self.check(&TokenType::Minus)
20002                    && matches!(
20003                        self.tokens.get(self.current + 1).map(|t| &t.kind),
20004                        Some(TokenType::Number(_))
20005                    )
20006                {
20007                    // Negative index literal — end-relative (`item -1 of xs`
20008                    // is the last element).
20009                    self.advance(); // consume '-'
20010                    let TokenType::Number(sym) = self.peek().kind else { unreachable!() };
20011                    self.advance();
20012                    let num_str = self.interner.resolve(sym).to_string();
20013                    let n = self.parse_i64_numeral_signed(&num_str, true).map_err(|_| ParseError {
20014                        kind: ParseErrorKind::ExpectedKeyword {
20015                            keyword: format!("a numeric index (got '{}')", num_str),
20016                        },
20017                        span: self.current_span(),
20018                    })?;
20019                    self.ctx
20020                        .alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Number(n)))
20021                } else if self.check(&TokenType::LParen) {
20022                    // Parenthesized expression like (mid + 1) — or a TUPLE
20023                    // key like ("a", 1) (tuples are content-keyed map keys).
20024                    self.advance(); // consume '('
20025                    let first = self.parse_imperative_expr()?;
20026                    let inner = if self.check(&TokenType::Comma) {
20027                        let mut items = vec![first];
20028                        while self.check(&TokenType::Comma) {
20029                            self.advance(); // consume ","
20030                            items.push(self.parse_imperative_expr()?);
20031                        }
20032                        self.ctx.alloc_imperative_expr(Expr::Tuple(items))
20033                    } else {
20034                        first
20035                    };
20036                    if !self.check(&TokenType::RParen) {
20037                        return Err(ParseError {
20038                            kind: ParseErrorKind::ExpectedKeyword { keyword: ")".to_string() },
20039                            span: self.current_span(),
20040                        });
20041                    }
20042                    self.advance(); // consume ')'
20043                    inner
20044                } else if let TokenType::StringLiteral(sym) = self.peek().kind {
20045                    // Phase 57B: String literal key for Map access like item "iron" of prices
20046                    let sym = sym;
20047                    self.advance();
20048                    self.ctx.alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Text(sym)))
20049                } else if self.check(&TokenType::LBracket) {
20050                    // `item [1, 2] of m` — a List as a map key, rejected by
20051                    // design (a live List can mutate after insertion and the
20052                    // map would lose the entry). Name the key problem here;
20053                    // the generic missing-'of' error buries the lesson.
20054                    return Err(ParseError {
20055                        kind: ParseErrorKind::Custom(
20056                            "A List cannot be a map key: keys must be immutable, and a live \
20057                             List can change after insertion — the map would lose the entry. \
20058                             Which fixed value identifies this entry? Use an Int or Text key, \
20059                             or a tuple like ('a', 1) for a compound key."
20060                                .to_string(),
20061                        ),
20062                        span: self.peek().span,
20063                    });
20064                } else if !self.check_preposition_is("of") {
20065                    // Check for boolean literals before treating as variable
20066                    let word = self.interner.resolve(self.peek().lexeme);
20067                    if word == "true" {
20068                        self.advance();
20069                        self.ctx.alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Boolean(true)))
20070                    } else if word == "false" {
20071                        self.advance();
20072                        self.ctx.alloc_imperative_expr(Expr::Literal(crate::ast::Literal::Boolean(false)))
20073                    } else {
20074                        // Variable identifier like i, j, idx (any token that's not "of")
20075                        let sym = self.peek().lexeme;
20076                        self.advance();
20077                        self.ctx.alloc_imperative_expr(Expr::Identifier(sym))
20078                    }
20079                } else {
20080                    return Err(ParseError {
20081                        kind: ParseErrorKind::ExpectedExpression,
20082                        span: self.current_span(),
20083                    });
20084                };
20085
20086                // Expect "of"
20087                if !self.check_preposition_is("of") {
20088                    return Err(ParseError {
20089                        kind: ParseErrorKind::ExpectedKeyword { keyword: "of".to_string() },
20090                        span: self.current_span(),
20091                    });
20092                }
20093                self.advance(); // consume "of"
20094
20095                // Parse collection as primary expression (identifier or field chain)
20096                // Using primary_expr instead of imperative_expr prevents consuming operators
20097                let collection = self.parse_primary_expr()?;
20098
20099                Ok(self.ctx.alloc_imperative_expr(Expr::Index {
20100                    collection,
20101                    index,
20102                }))
20103            }
20104
20105            // Slice access: "items N through M of collection"
20106            // OR variable named "items" - disambiguate by checking if next token starts an expression
20107            TokenType::Items => {
20108                // Peek ahead to determine if this is slice syntax or variable usage. A slice's start is a
20109                // number, a parenthesized expression, `length of …`, or a single word followed by
20110                // `through` (`items lo through hi of xs`); anything else after `items` (an operator, a
20111                // dot, the end of the sentence) makes it a variable.
20112                let is_slice_syntax = match self.tokens.get(self.current + 1).map(|t| &t.kind) {
20113                    Some(TokenType::Number(_) | TokenType::LParen | TokenType::Length) => true,
20114                    Some(_) => self.peek_word_at(2, "through"),
20115                    None => false,
20116                };
20117
20118                if !is_slice_syntax {
20119                    // Treat "items" as a variable identifier
20120                    let sym = token.lexeme;
20121                    self.advance();
20122                    let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20123                    return self.parse_field_access_chain(base);
20124                }
20125
20126                self.advance(); // consume "items"
20127
20128                // Grand Challenge: Parse start index as expression (number, identifier, or parenthesized)
20129                let start = if let TokenType::Number(sym) = &self.peek().kind {
20130                    // Literal number - check for zero index at compile time
20131                    let sym = *sym;
20132                    self.advance();
20133                    self.parse_index_numeral(sym)?
20134                } else if self.check(&TokenType::LParen) {
20135                    // Parenthesized expression like (mid + 1)
20136                    self.advance(); // consume '('
20137                    let inner = self.parse_imperative_expr()?;
20138                    if !self.check(&TokenType::RParen) {
20139                        return Err(ParseError {
20140                            kind: ParseErrorKind::ExpectedKeyword { keyword: ")".to_string() },
20141                            span: self.current_span(),
20142                        });
20143                    }
20144                    self.advance(); // consume ')'
20145                    inner
20146                } else if self.check(&TokenType::Length) {
20147                    // "length of X" compound expression as start index
20148                    self.advance(); // consume "length"
20149                    if self.check_preposition_is("of") {
20150                        self.advance(); // consume "of"
20151                        let target = self.parse_primary_expr()?;
20152                        self.ctx.alloc_imperative_expr(Expr::Length { collection: target })
20153                    } else {
20154                        // Bare "length" as identifier
20155                        let sym = self.tokens[self.current - 1].lexeme;
20156                        self.ctx.alloc_imperative_expr(Expr::Identifier(sym))
20157                    }
20158                } else if !self.check_preposition_is("through") {
20159                    // Variable identifier like mid, idx
20160                    let sym = self.peek().lexeme;
20161                    self.advance();
20162                    self.ctx.alloc_imperative_expr(Expr::Identifier(sym))
20163                } else {
20164                    return Err(ParseError {
20165                        kind: ParseErrorKind::ExpectedExpression,
20166                        span: self.current_span(),
20167                    });
20168                };
20169
20170                // Expect "through"
20171                if !self.check_preposition_is("through") {
20172                    return Err(ParseError {
20173                        kind: ParseErrorKind::ExpectedKeyword { keyword: "through".to_string() },
20174                        span: self.current_span(),
20175                    });
20176                }
20177                self.advance(); // consume "through"
20178
20179                // Grand Challenge: Parse end index as expression (number, identifier, or parenthesized)
20180                let end = if let TokenType::Number(sym) = &self.peek().kind {
20181                    // Literal number - check for zero index at compile time
20182                    let sym = *sym;
20183                    self.advance();
20184                    self.parse_index_numeral(sym)?
20185                } else if self.check(&TokenType::LParen) {
20186                    // Parenthesized expression like (mid + 1)
20187                    self.advance(); // consume '('
20188                    let inner = self.parse_imperative_expr()?;
20189                    if !self.check(&TokenType::RParen) {
20190                        return Err(ParseError {
20191                            kind: ParseErrorKind::ExpectedKeyword { keyword: ")".to_string() },
20192                            span: self.current_span(),
20193                        });
20194                    }
20195                    self.advance(); // consume ')'
20196                    inner
20197                } else if self.check(&TokenType::Length) {
20198                    // "length of X" compound expression as end index
20199                    self.advance(); // consume "length"
20200                    if self.check_preposition_is("of") {
20201                        self.advance(); // consume "of"
20202                        let target = self.parse_primary_expr()?;
20203                        self.ctx.alloc_imperative_expr(Expr::Length { collection: target })
20204                    } else {
20205                        // Bare "length" as identifier
20206                        let sym = self.tokens[self.current - 1].lexeme;
20207                        self.ctx.alloc_imperative_expr(Expr::Identifier(sym))
20208                    }
20209                } else if !self.check_preposition_is("of") {
20210                    // Variable identifier like n, mid
20211                    let sym = self.peek().lexeme;
20212                    self.advance();
20213                    self.ctx.alloc_imperative_expr(Expr::Identifier(sym))
20214                } else {
20215                    return Err(ParseError {
20216                        kind: ParseErrorKind::ExpectedExpression,
20217                        span: self.current_span(),
20218                    });
20219                };
20220
20221                // "of collection" is now optional - collection can be inferred from context
20222                // (e.g., "items 1 through mid" when items is the local variable)
20223                let collection = if self.check_preposition_is("of") {
20224                    self.advance(); // consume "of"
20225                    self.parse_imperative_expr()?
20226                } else {
20227                    // The variable is the collection itself (already consumed as "items")
20228                    // Re-intern "items" to use as the collection identifier
20229                    let items_sym = self.interner.intern("items");
20230                    self.ctx.alloc_imperative_expr(Expr::Identifier(items_sym))
20231                };
20232
20233                Ok(self.ctx.alloc_imperative_expr(Expr::Slice {
20234                    collection,
20235                    start,
20236                    end,
20237                }))
20238            }
20239
20240            // List literal: [1, 2, 3]
20241            TokenType::LBracket => {
20242                self.advance(); // consume "["
20243
20244                let mut items = Vec::new();
20245                if !self.check(&TokenType::RBracket) {
20246                    loop {
20247                        items.push(self.parse_imperative_expr()?);
20248                        if !self.check(&TokenType::Comma) {
20249                            break;
20250                        }
20251                        self.advance(); // consume ","
20252                        // A trailing comma (`[1, 2, 3,]`) ends the list.
20253                        if self.check(&TokenType::RBracket) {
20254                            break;
20255                        }
20256                    }
20257                }
20258
20259                if !self.check(&TokenType::RBracket) {
20260                    return Err(ParseError {
20261                        kind: ParseErrorKind::ExpectedKeyword { keyword: "]".to_string() },
20262                        span: self.current_span(),
20263                    });
20264                }
20265                self.advance(); // consume "]"
20266
20267                // Check for typed empty list: [] of Int
20268                if items.is_empty() && self.check_word("of") {
20269                    self.advance(); // consume "of"
20270                    let type_name = self.expect_identifier()?;
20271                    // Generate: Seq::<Type>::default()
20272                    let seq_sym = self.interner.intern("Seq");
20273                    return Ok(self.ctx.alloc_imperative_expr(Expr::New {
20274                        type_name: seq_sym,
20275                        type_args: vec![TypeExpr::Named(type_name)],
20276                        init_fields: vec![],
20277                    }));
20278                }
20279
20280                Ok(self.ctx.alloc_imperative_expr(Expr::List(items)))
20281            }
20282
20283            // `{k: v, …}` map literal / `{a, b, …}` set literal. A `:` after
20284            // the first element decides map-ness; the empty form requires an
20285            // element type exactly like `[] of Int` — `{} of Int` (set) /
20286            // `{} of Text to Int` (map). Non-empty literals lower to the
20287            // variadic `mapOf`/`setOf` builtins, so every engine shares one
20288            // construction path.
20289            TokenType::LBrace => {
20290                self.advance(); // consume "{"
20291
20292                if self.check(&TokenType::RBrace) {
20293                    self.advance(); // consume "}"
20294                    if !self.check_word("of") {
20295                        return Err(ParseError {
20296                            kind: ParseErrorKind::ExpectedKeyword {
20297                                keyword: "of — an empty {} needs its element type: `{} of Int` (set) or `{} of Text to Int` (map)".to_string(),
20298                            },
20299                            span: self.current_span(),
20300                        });
20301                    }
20302                    self.advance(); // consume "of"
20303                    let first_ty = self.expect_identifier()?;
20304                    if self.check(&TokenType::To) || self.check_preposition_is("to") {
20305                        self.advance(); // consume "to"
20306                        let val_ty = self.expect_identifier()?;
20307                        let map_sym = self.interner.intern("Map");
20308                        return Ok(self.ctx.alloc_imperative_expr(Expr::New {
20309                            type_name: map_sym,
20310                            type_args: vec![TypeExpr::Named(first_ty), TypeExpr::Named(val_ty)],
20311                            init_fields: vec![],
20312                        }));
20313                    }
20314                    let set_sym = self.interner.intern("Set");
20315                    return Ok(self.ctx.alloc_imperative_expr(Expr::New {
20316                        type_name: set_sym,
20317                        type_args: vec![TypeExpr::Named(first_ty)],
20318                        init_fields: vec![],
20319                    }));
20320                }
20321
20322                let first = self.parse_imperative_expr()?;
20323                if self.check(&TokenType::Colon) {
20324                    // Map literal: `{k: v, …}` → `mapOf(k, v, …)` (pairs flat,
20325                    // insertion order preserved by the ordered-map contract).
20326                    self.advance(); // consume ":"
20327                    let mut args = vec![first, self.parse_imperative_expr()?];
20328                    while self.check(&TokenType::Comma) {
20329                        self.advance(); // consume ","
20330                        // A trailing comma (`{a: 1, b: 2,}`) ends the map.
20331                        if self.check(&TokenType::RBrace) {
20332                            break;
20333                        }
20334                        args.push(self.parse_imperative_expr()?);
20335                        if !self.check(&TokenType::Colon) {
20336                            return Err(ParseError {
20337                                kind: ParseErrorKind::ExpectedKeyword { keyword: ":".to_string() },
20338                                span: self.current_span(),
20339                            });
20340                        }
20341                        self.advance(); // consume ":"
20342                        args.push(self.parse_imperative_expr()?);
20343                    }
20344                    if !self.check(&TokenType::RBrace) {
20345                        return Err(ParseError {
20346                            kind: ParseErrorKind::ExpectedKeyword { keyword: "}".to_string() },
20347                            span: self.current_span(),
20348                        });
20349                    }
20350                    self.advance(); // consume "}"
20351                    let map_of = self.interner.intern("mapOf");
20352                    return Ok(self.ctx.alloc_imperative_expr(Expr::Call { function: map_of, args }));
20353                }
20354
20355                // Set literal: `{a, b, …}` → `setOf(a, b, …)`.
20356                let mut args = vec![first];
20357                while self.check(&TokenType::Comma) {
20358                    self.advance(); // consume ","
20359                    // A trailing comma (`{a, b, c,}`) ends the set.
20360                    if self.check(&TokenType::RBrace) {
20361                        break;
20362                    }
20363                    args.push(self.parse_imperative_expr()?);
20364                }
20365                if !self.check(&TokenType::RBrace) {
20366                    return Err(ParseError {
20367                        kind: ParseErrorKind::ExpectedKeyword { keyword: "}".to_string() },
20368                        span: self.current_span(),
20369                    });
20370                }
20371                self.advance(); // consume "}"
20372                let set_of = self.interner.intern("setOf");
20373                Ok(self.ctx.alloc_imperative_expr(Expr::Call { function: set_of, args }))
20374            }
20375
20376            // Currency-symbol money literal (`$19.99`, `€5`, `¥100`) desugars to `money(num, "CODE")`,
20377            // the same shape as the spelled `19.99 USD` form. An exact (decimal) magnitude rides
20378            // `decimal(..)` so it never float-drifts; an integer magnitude passes through directly.
20379            TokenType::MoneyLiteral { amount, currency } => {
20380                let amount = *amount;
20381                let currency = *currency;
20382                let num_str = self.interner.resolve(amount).to_string();
20383                self.advance();
20384                let num_expr = if num_str.contains('.') {
20385                    let text = self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Text(amount)));
20386                    let dec = self.interner.intern("decimal");
20387                    self.ctx.alloc_imperative_expr(Expr::Call { function: dec, args: vec![text] })
20388                } else {
20389                    let num = self.parse_i64_numeral(&num_str)?;
20390                    self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Number(num)))
20391                };
20392                let code_expr = self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Text(currency)));
20393                let func = self.interner.intern("money");
20394                return Ok(self
20395                    .ctx
20396                    .alloc_imperative_expr(Expr::Call { function: func, args: vec![num_expr, code_expr] }));
20397            }
20398
20399            TokenType::Number(sym) => {
20400                let num_str = self.interner.resolve(*sym).to_string();
20401                self.advance();
20402
20403                // Check if followed by CalendarUnit → Span literal
20404                if let TokenType::CalendarUnit(unit) = self.peek().kind {
20405                    return self.parse_span_literal_from_num(&num_str);
20406                }
20407
20408                // Dimensioned quantity literal: a number immediately followed by a unit word
20409                // (`2 inches`, `5 kilograms`, `20 celsius`) desugars to `quantity(num, "unit")`.
20410                // Imperative-only (this path allocs imperative exprs), so natural-language phrases
20411                // like "ran 2 miles" — parsed by the clause grammar — are untouched. An exact
20412                // (decimal) magnitude rides the `decimal(..)` constructor so it never float-drifts.
20413                if matches!(
20414                    self.peek().kind,
20415                    TokenType::Identifier | TokenType::Noun(_) | TokenType::Adjective(_)
20416                ) {
20417                    let unit_lexeme = self.peek().lexeme;
20418                    let is_unit =
20419                        logicaffeine_base::quantity::units::by_name(self.interner.resolve(unit_lexeme)).is_some();
20420                    if is_unit {
20421                        self.advance(); // consume the unit word
20422                        let num_expr = if num_str.contains('.') || num_str.contains('e') || num_str.contains('E') {
20423                            let text = self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Text(*sym)));
20424                            let dec = self.interner.intern("decimal");
20425                            self.ctx.alloc_imperative_expr(Expr::Call { function: dec, args: vec![text] })
20426                        } else {
20427                            let num = self.parse_i64_numeral(&num_str)?;
20428                            self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Number(num)))
20429                        };
20430                        let unit_expr = self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Text(unit_lexeme)));
20431                        let func = self.interner.intern("quantity");
20432                        return Ok(self
20433                            .ctx
20434                            .alloc_imperative_expr(Expr::Call { function: func, args: vec![num_expr, unit_expr] }));
20435                    }
20436                }
20437
20438                // `19.99 USD` / `5 EUR` desugars to `money(num, "CODE")` — the same shape as the unit
20439                // sugar above, but the trailing token is an ISO-4217 currency code (checked AFTER
20440                // units so a unit word is never mis-read as money, and gated on `currency::by_code` so
20441                // only a real code triggers). An exact (decimal) amount rides `decimal(..)`.
20442                if matches!(
20443                    self.peek().kind,
20444                    TokenType::Identifier
20445                        | TokenType::Noun(_)
20446                        | TokenType::ProperName(_)
20447                        | TokenType::Adjective(_)
20448                ) {
20449                    let code_lexeme = self.peek().lexeme;
20450                    if logicaffeine_base::money::currency::by_code(self.interner.resolve(code_lexeme)).is_some() {
20451                        self.advance(); // consume the currency code
20452                        let num_expr = if num_str.contains('.') || num_str.contains('e') || num_str.contains('E') {
20453                            let text = self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Text(*sym)));
20454                            let dec = self.interner.intern("decimal");
20455                            self.ctx.alloc_imperative_expr(Expr::Call { function: dec, args: vec![text] })
20456                        } else {
20457                            let num = self.parse_i64_numeral(&num_str)?;
20458                            self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Number(num)))
20459                        };
20460                        let code_expr = self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Text(code_lexeme)));
20461                        let func = self.interner.intern("money");
20462                        return Ok(self
20463                            .ctx
20464                            .alloc_imperative_expr(Expr::Call { function: func, args: vec![num_expr, code_expr] }));
20465                    }
20466                }
20467
20468                // Radix first (`0x1E` has an `E` that is a hex digit, not
20469                // scientific notation); then the float sniff; both parse
20470                // EXACTLY or fail loudly — never a silent 0.
20471                if Self::is_radix_numeral(&num_str) {
20472                    let num = self.parse_i64_numeral(&num_str)?;
20473                    Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Number(num))))
20474                } else if num_str.contains('.') || num_str.contains('e') || num_str.contains('E') {
20475                    let num = num_str.replace('_', "").parse::<f64>().map_err(|_| ParseError {
20476                        kind: ParseErrorKind::ExpectedKeyword {
20477                            keyword: format!("a Float literal (got `{}`)", num_str),
20478                        },
20479                        span: self.current_span(),
20480                    })?;
20481                    Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Float(num))))
20482                } else {
20483                    let num = self.parse_i64_numeral(&num_str)?;
20484                    Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Number(num))))
20485                }
20486            }
20487
20488            // Phase 33: String literals
20489            TokenType::StringLiteral(sym) => {
20490                self.advance();
20491                Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Text(*sym))))
20492            }
20493
20494            // String interpolation: "Hello, {name}!"
20495            TokenType::InterpolatedString(sym) => {
20496                let raw = self.interner.resolve(*sym).to_string();
20497                let span = self.advance().span;
20498                let parts = self.parse_interpolation_parts(&raw, span)?;
20499                Ok(self.ctx.alloc_imperative_expr(Expr::InterpolatedString(parts)))
20500            }
20501
20502            // Character literals
20503            TokenType::CharLiteral(sym) => {
20504                let char_str = self.interner.resolve(*sym);
20505                let ch = char_str.chars().next().unwrap_or('\0');
20506                self.advance();
20507                Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Char(ch))))
20508            }
20509
20510            // Duration literals: 500ms, 2s, 50ns
20511            TokenType::DurationLiteral { nanos, .. } => {
20512                let nanos = *nanos;
20513                self.advance();
20514                Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Duration(nanos))))
20515            }
20516
20517            // Date literals: 2026-05-20
20518            // Also handles "DATE at TIME" → Moment
20519            TokenType::DateLiteral { days } => {
20520                let days = *days;
20521                self.advance();
20522
20523                // Check for "at TIME" to create a Moment
20524                if self.check(&TokenType::At) {
20525                    self.advance(); // consume "at"
20526
20527                    // Expect a TimeLiteral
20528                    if let TokenType::TimeLiteral { nanos_from_midnight } = self.peek().kind {
20529                        let time_nanos = nanos_from_midnight;
20530                        self.advance(); // consume time literal
20531
20532                        // Convert to Moment: days * 86400 * 1e9 + time_nanos
20533                        let moment_nanos = (days as i64) * 86_400_000_000_000 + time_nanos;
20534                        return Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Moment(moment_nanos))));
20535                    } else {
20536                        return Err(ParseError {
20537                            kind: ParseErrorKind::ExpectedExpression,
20538                            span: self.current_span(),
20539                        });
20540                    }
20541                }
20542
20543                Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Date(days))))
20544            }
20545
20546            // Time-of-day literals: 4pm, 9:30am, noon, midnight
20547            TokenType::TimeLiteral { nanos_from_midnight } => {
20548                let nanos = *nanos_from_midnight;
20549                self.advance();
20550                Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Time(nanos))))
20551            }
20552
20553            // Handle 'nothing' literal. A live user binding wins over the
20554            // literal reading (`Let nothing be 5. Show nothing + 2.`) — the
20555            // infinity/nan precedent: value-literal words defer to user_bound.
20556            TokenType::Nothing => {
20557                if self.user_bound.contains(&token.lexeme) {
20558                    return self.shadowed_literal_word_expr(token.lexeme);
20559                }
20560                self.advance();
20561                Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Nothing)))
20562            }
20563
20564            // Option constructors: "some <expr>" → Some(expr), "none" → None.
20565            // A live user binding wins over the constructor reading
20566            // (`Let some be 5. Show some + 1.`); `expect_identifier` already
20567            // accepts `some` in name position, so the read side must too.
20568            TokenType::Some => {
20569                if self.user_bound.contains(&token.lexeme) {
20570                    return self.shadowed_literal_word_expr(token.lexeme);
20571                }
20572                self.advance(); // consume "some"
20573                let value = self.parse_imperative_expr()?;
20574                Ok(self.ctx.alloc_imperative_expr(Expr::OptionSome { value }))
20575            }
20576
20577            // Phase 43D: Length expression: "length of items" or "length(items)"
20578            TokenType::Length => {
20579                let func_name = self.peek().lexeme;
20580
20581                // `length(x)` is `length of x` (QUICKGUIDE §5.2): one meaning, one expression,
20582                // so no tier or analysis ever sees a call to the builtin named `length`. A
20583                // program that declares its own `length` calls it, and any other argument count
20584                // stays a call, for the builtin's arity error.
20585                if self.tokens.get(self.current + 1)
20586                    .map(|t| matches!(t.kind, TokenType::LParen))
20587                    .unwrap_or(false)
20588                {
20589                    let builtin = !self.declared_function_names.contains(&func_name);
20590                    self.advance(); // consume "length"
20591                    let call = self.parse_call_expr(func_name)?;
20592                    if let Expr::Call { args, .. } = call {
20593                        if let (true, [collection]) = (builtin, args.as_slice()) {
20594                            return Ok(self.ctx.alloc_imperative_expr(Expr::Length { collection: *collection }));
20595                        }
20596                    }
20597                    return Ok(call);
20598                }
20599
20600                self.advance(); // consume "length"
20601
20602                // Expect "of" for natural syntax
20603                if !self.check_preposition_is("of") {
20604                    return Err(ParseError {
20605                        kind: ParseErrorKind::ExpectedKeyword { keyword: "of".to_string() },
20606                        span: self.current_span(),
20607                    });
20608                }
20609                self.advance(); // consume "of"
20610
20611                // Use parse_primary_expr so "length of arr / 2" binds as
20612                // "(length of arr) / 2" rather than "length of (arr / 2)"
20613                let collection = self.parse_primary_expr()?;
20614                Ok(self.ctx.alloc_imperative_expr(Expr::Length { collection }))
20615            }
20616
20617            // Phase 43D: Copy expression: "copy of slice" or "copy(slice)"
20618            TokenType::Copy => {
20619                let func_name = self.peek().lexeme;
20620
20621                // `copy(x)` is `copy of x` (QUICKGUIDE §5.2), as `length(x)` is `length of x`,
20622                // unless the program declares its own `copy`, which the call then reaches.
20623                if self.tokens.get(self.current + 1)
20624                    .map(|t| matches!(t.kind, TokenType::LParen))
20625                    .unwrap_or(false)
20626                {
20627                    let builtin = !self.declared_function_names.contains(&func_name);
20628                    self.advance(); // consume "copy"
20629                    let call = self.parse_call_expr(func_name)?;
20630                    if let Expr::Call { args, .. } = call {
20631                        if let (true, [expr]) = (builtin, args.as_slice()) {
20632                            return Ok(self.ctx.alloc_imperative_expr(Expr::Copy { expr: *expr }));
20633                        }
20634                    }
20635                    return Ok(call);
20636                }
20637
20638                self.advance(); // consume "copy"
20639
20640                // Expect "of" for natural syntax
20641                if !self.check_preposition_is("of") {
20642                    return Err(ParseError {
20643                        kind: ParseErrorKind::ExpectedKeyword { keyword: "of".to_string() },
20644                        span: self.current_span(),
20645                    });
20646                }
20647                self.advance(); // consume "of"
20648
20649                let expr = self.parse_imperative_expr()?;
20650                Ok(self.ctx.alloc_imperative_expr(Expr::Copy { expr }))
20651            }
20652
20653            // Phase 48: Manifest expression: "manifest of Zone"
20654            TokenType::Manifest => {
20655                self.advance(); // consume "manifest"
20656
20657                // Expect "of"
20658                if !self.check_preposition_is("of") {
20659                    return Err(ParseError {
20660                        kind: ParseErrorKind::ExpectedKeyword { keyword: "of".to_string() },
20661                        span: self.current_span(),
20662                    });
20663                }
20664                self.advance(); // consume "of"
20665
20666                let zone = self.parse_imperative_expr()?;
20667                Ok(self.ctx.alloc_imperative_expr(Expr::ManifestOf { zone }))
20668            }
20669
20670            // Phase 48: Chunk expression: "chunk at N in Zone"
20671            TokenType::Chunk => {
20672                self.advance(); // consume "chunk"
20673
20674                // Expect "at"
20675                if !self.check(&TokenType::At) {
20676                    return Err(ParseError {
20677                        kind: ParseErrorKind::ExpectedKeyword { keyword: "at".to_string() },
20678                        span: self.current_span(),
20679                    });
20680                }
20681                self.advance(); // consume "at"
20682
20683                // The index is a dimensionless position, so it is parsed BELOW every construct that
20684                // consumes `in`: `parse_xor_expr`, not `parse_imperative_expr`/`parse_or_condition`.
20685                // The `in` here is the chunk construct's own keyword and must not be swallowed as an
20686                // `in "<zone>"` / `in <unit>` conversion (handled above `parse_or_condition`) nor as
20687                // Pythonic membership `<index> in <collection>` (handled in `parse_comparison`, which
20688                // `parse_or_condition` descends through).
20689                let index = self.parse_xor_expr()?;
20690
20691                // Expect "in"
20692                if !self.check_preposition_is("in") && !self.check(&TokenType::In) {
20693                    return Err(ParseError {
20694                        kind: ParseErrorKind::ExpectedKeyword { keyword: "in".to_string() },
20695                        span: self.current_span(),
20696                    });
20697                }
20698                self.advance(); // consume "in"
20699
20700                let zone = self.parse_imperative_expr()?;
20701                Ok(self.ctx.alloc_imperative_expr(Expr::ChunkAt { index, zone }))
20702            }
20703
20704            // Handle verbs in expression context:
20705            // - "empty" is a literal Nothing
20706            // - Other verbs can be function names (e.g., read, write)
20707            TokenType::Verb { lemma, .. } => {
20708                let word = self.interner.resolve(*lemma).to_lowercase();
20709                if word == "empty" && !self.user_bound.contains(&token.lexeme) {
20710                    self.advance();
20711                    return Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Nothing)));
20712                }
20713                // Phase 38: Allow verbs to be used as function calls
20714                let sym = token.lexeme;
20715                self.advance();
20716                if self.check(&TokenType::LParen) {
20717                    return self.parse_call_expr(sym);
20718                }
20719                // Treat as identifier reference
20720                self.verify_identifier_access(sym, token.span)?;
20721                let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20722                self.parse_field_access_chain(base)
20723            }
20724
20725            // Phase 38: Adverbs as identifiers (e.g., "now" for time functions)
20726            TokenType::TemporalAdverb(_) | TokenType::ScopalAdverb(_) | TokenType::Adverb(_) => {
20727                let sym = token.lexeme;
20728                self.advance();
20729                if self.check(&TokenType::LParen) {
20730                    return self.parse_call_expr(sym);
20731                }
20732                // Treat as identifier reference (e.g., "Let t be now.")
20733                self.verify_identifier_access(sym, token.span)?;
20734                let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20735                self.parse_field_access_chain(base)
20736            }
20737
20738            // Phase 10: IO keywords as function calls (e.g., "read", "write", "file")
20739            // Phase 57: Add/Remove keywords as function calls
20740            // Identifier freedom: every keyword accepted by `expect_identifier` must
20741            // also be REFERENCEABLE — a name you can bind but never read back is a
20742            // trap. These tokens have no expression-head meaning of their own (their
20743            // keyword roles are statement-initial, infix, or postfix), so a bare
20744            // occurrence here can only be a variable or function name.
20745            TokenType::Read | TokenType::Write | TokenType::File | TokenType::Console |
20746            TokenType::Add | TokenType::Remove |
20747            TokenType::Size | TokenType::Zone | TokenType::Inside | TokenType::Called |
20748            TokenType::Launch | TokenType::Task | TokenType::Pipe | TokenType::Stop |
20749            TokenType::Receive | TokenType::Send | TokenType::Spawn | TokenType::Await |
20750            TokenType::Attempt | TokenType::Following | TokenType::Listen |
20751            TokenType::NetConnect | TokenType::Sync | TokenType::Mount |
20752            TokenType::Shifted | TokenType::Union | TokenType::Intersection |
20753            TokenType::Contains | TokenType::Through | TokenType::Into |
20754            TokenType::Push | TokenType::Pop | TokenType::Try | TokenType::Inspect |
20755            TokenType::Check | TokenType::Superlative(_) | TokenType::Comparative(_) |
20756            TokenType::Performative(_) |
20757            // The test-framework (Wave 2) and observation (Wave 6) keywords and
20758            // `Sleep` are all bindable via `expect_identifier`; they must be
20759            // referenceable here too (`Show record`, `Let x be note`), or binding
20760            // them would be the "name you can never read back" trap this arm exists
20761            // to close. Their keyword roles are all statement-initial, so a bare
20762            // occurrence in expression head position can only be a name.
20763            TokenType::Expect | TokenType::Advance | TokenType::Skip | TokenType::Given |
20764            TokenType::Note | TokenType::Warn | TokenType::Record | TokenType::During |
20765            TokenType::Sleep |
20766            TokenType::Focus(_) | TokenType::Particle(_) | TokenType::CalendarUnit(_) => {
20767                let sym = token.lexeme;
20768                self.advance();
20769                if self.check(&TokenType::LParen) {
20770                    return self.parse_call_expr(sym);
20771                }
20772                // Treat as identifier reference
20773                self.verify_identifier_access(sym, token.span)?;
20774                let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20775                self.parse_field_access_chain(base)
20776            }
20777
20778            // Unified identifier handling - all identifier-like tokens get verified
20779            // First check for boolean/special literals before treating as variable
20780            TokenType::Noun(sym) | TokenType::ProperName(sym) | TokenType::Adjective(sym) => {
20781                let sym = *sym;
20782                let word = self.interner.resolve(sym);
20783
20784                // Check for boolean literals
20785                if word == "true" {
20786                    self.advance();
20787                    return Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Boolean(true))));
20788                }
20789                if word == "false" {
20790                    self.advance();
20791                    return Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Boolean(false))));
20792                }
20793                // Float word literals (the `true`/`false` precedent): the
20794                // values every float tower needs a spelling for.
20795                if word == "infinity" && !self.user_bound.contains(&sym) {
20796                    self.advance();
20797                    return Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Float(f64::INFINITY))));
20798                }
20799                if word == "nan" && !self.user_bound.contains(&sym) {
20800                    self.advance();
20801                    return Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Float(f64::NAN))));
20802                }
20803
20804                // Check for 'empty' - treat as unit value for collections
20805                // (a live user binding wins — the infinity/nan precedent)
20806                if word == "empty" && !self.user_bound.contains(&sym) {
20807                    self.advance();
20808                    return Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Nothing)));
20809                }
20810
20811                // Option None literal: "none" → None. A live user binding wins
20812                // (`Let none be a new Seq of Int. Push 1 to none.`), so the
20813                // word falls through to the ordinary identifier path below.
20814                if word == "none" && !self.user_bound.contains(&sym) {
20815                    self.advance();
20816                    return Ok(self.ctx.alloc_imperative_expr(Expr::OptionNone));
20817                }
20818
20819                // Don't verify as variable - might be a function call or enum variant
20820                let sym_span = self.current_span();
20821                self.advance();
20822
20823                // Phase 32: Check for function call: identifier(args)
20824                if self.check(&TokenType::LParen) {
20825                    return self.parse_call_expr(sym);
20826                }
20827
20828                // A bare identifier's own expression parse is also where `Remove k from m.`,
20829                // `Pop x from xs.` and `Take n from xs.` parse THEIR operand (`k`/`x`/`n`) — an
20830                // ordinary variable, never a variant, in the overwhelming common case — before
20831                // their own statement parser ever gets to see `from` as ITS separator. Only when
20832                // `sym` could not otherwise mean anything BUT a qualified variant (it is a
20833                // declared variant of some enum, and the word after `from` really does name that
20834                // qualifier) does this `from` belong here; otherwise it is left completely
20835                // untouched for whatever statement is calling this.
20836                if self.check(&TokenType::From) && !self.bare_word_from_is_a_qualifier(sym) {
20837                    self.verify_identifier_access(sym, sym_span)?;
20838                    let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20839                    return self.parse_field_access_chain(base);
20840                }
20841
20842                // Phase 33/qualified-variant: a bare enum variant (e.g., "North" for Direction),
20843                // optionally naming its own enum ("Failure from Outcome") the same way `a new
20844                // <Variant> from <Enum>` does — see `resolve_construction_target`.
20845                match self.resolve_construction_target(sym, sym_span)? {
20846                    ConstructionTarget::Variant { enum_name, variant } => {
20847                        let fields = if self.check_word("with") {
20848                            self.parse_variant_constructor_fields(variant, Some(enum_name))?
20849                        } else {
20850                            vec![]
20851                        };
20852                        let base = self.ctx.alloc_imperative_expr(Expr::NewVariant {
20853                            enum_name,
20854                            variant,
20855                            fields,
20856                        });
20857                        return self.parse_field_access_chain(base);
20858                    }
20859                    ConstructionTarget::Type(name) => {
20860                        // Centralized verification for undefined/moved checks (only for variables)
20861                        self.verify_identifier_access(name, sym_span)?;
20862                        let base = self.ctx.alloc_imperative_expr(Expr::Identifier(name));
20863                        // Phase 31: Check for field access via possessive
20864                        self.parse_field_access_chain(base)
20865                    }
20866                }
20867            }
20868
20869            // Pronouns can be variable names in code context ("i", "it")
20870            TokenType::Pronoun { .. } => {
20871                let sym = token.lexeme;
20872                self.advance();
20873                let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20874                // Phase 31: Check for field access via possessive
20875                self.parse_field_access_chain(base)
20876            }
20877
20878            // Phase 49: CRDT keywords can be function names (Merge, Increase)
20879            TokenType::Merge | TokenType::Increase => {
20880                let sym = token.lexeme;
20881                self.advance();
20882
20883                // Check for function call: Merge(args)
20884                if self.check(&TokenType::LParen) {
20885                    return self.parse_call_expr(sym);
20886                }
20887
20888                let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20889                self.parse_field_access_chain(base)
20890            }
20891
20892            // Escape hatch in expression position: `Escape to Rust:`
20893            // Lookahead: if followed by "to", parse as escape expression.
20894            // Otherwise, treat as identifier (variable named "escape").
20895            TokenType::Escape => {
20896                if self.tokens.get(self.current + 1).map_or(false, |t|
20897                    matches!(t.kind, TokenType::To) || {
20898                        if let TokenType::Preposition(sym) = t.kind {
20899                            sym.is(self.interner, "to")
20900                        } else {
20901                            false
20902                        }
20903                    }
20904                ) {
20905                    return self.parse_escape_expr();
20906                }
20907                // Fall through to identifier handling
20908                let sym = token.lexeme;
20909                self.advance();
20910                if self.check(&TokenType::LParen) {
20911                    return self.parse_call_expr(sym);
20912                }
20913                self.verify_identifier_access(sym, token.span)?;
20914                let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20915                self.parse_field_access_chain(base)
20916            }
20917
20918            // Keywords that can also be used as identifiers in expression context
20919            // These are contextual keywords - they have special meaning in specific positions
20920            // but can be used as variable names elsewhere
20921            TokenType::Values |    // "values" - can be a variable name
20922            TokenType::Both |      // correlative: "both X and Y"
20923            TokenType::Either |    // correlative: "either X or Y"
20924            TokenType::Combined |  // string concat: "combined with"
20925            TokenType::Followed |  // seq concat: "followed by" (also a valid variable name)
20926            TokenType::Shared |    // CRDT modifier
20927            TokenType::Preposition(_) |  // prepositions: from, into, etc.
20928            TokenType::All => {    // quantifier in FOL, but valid variable name in imperative code
20929                let sym = token.lexeme;
20930                self.advance();
20931
20932                // Check for function call
20933                if self.check(&TokenType::LParen) {
20934                    return self.parse_call_expr(sym);
20935                }
20936
20937                self.verify_identifier_access(sym, token.span)?;
20938                let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20939                self.parse_field_access_chain(base)
20940            }
20941
20942            // Handle ambiguous tokens that might be identifiers or function calls
20943            TokenType::Ambiguous { primary, alternatives } => {
20944                // Always use lexeme for identifier access - preserves original casing
20945                // (using verb lemma can give wrong casing like "Result" instead of "result")
20946                let sym = token.lexeme;
20947
20948                // Check if this token can be used as identifier (has Noun/Verb/etc. interpretation)
20949                let is_identifier_token = match &**primary {
20950                    TokenType::Noun(_) | TokenType::Adjective(_) | TokenType::ProperName(_) |
20951                    TokenType::Verb { .. } => true,
20952                    _ => alternatives.iter().any(|t| matches!(t,
20953                        TokenType::Noun(_) | TokenType::Adjective(_) | TokenType::ProperName(_) |
20954                        TokenType::Verb { .. }
20955                    ))
20956                };
20957
20958                if is_identifier_token {
20959                    self.advance();
20960
20961                    // Check for function call: ambiguous_name(args)
20962                    if self.check(&TokenType::LParen) {
20963                        return self.parse_call_expr(sym);
20964                    }
20965
20966                    self.verify_identifier_access(sym, token.span)?;
20967                    let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
20968                    // Phase 31: Check for field access via possessive
20969                    self.parse_field_access_chain(base)
20970                } else {
20971                    Err(ParseError {
20972                        kind: ParseErrorKind::ExpectedExpression,
20973                        span: self.current_span(),
20974                    })
20975                }
20976            }
20977
20978            // Parenthesized expression, tuple literal, or closure
20979            TokenType::LParen => {
20980                // Try closure parse first using speculative parsing.
20981                // Closure syntax: `(name: Type, ...) -> expr` or `() -> expr`
20982                if let Some(closure) = self.try_parse(|p| p.parse_closure_expr()) {
20983                    return Ok(closure);
20984                }
20985
20986                // Not a closure — parse as parenthesized expression or tuple
20987                self.advance(); // consume '('
20988                let first = self.parse_imperative_expr()?;
20989
20990                // Check if this is a tuple (has comma) or just grouping
20991                if self.check(&TokenType::Comma) {
20992                    // It's a tuple - parse remaining elements
20993                    let mut items = vec![first];
20994                    while self.check(&TokenType::Comma) {
20995                        self.advance(); // consume ","
20996                        items.push(self.parse_imperative_expr()?);
20997                    }
20998
20999                    if !self.check(&TokenType::RParen) {
21000                        return Err(ParseError {
21001                            kind: ParseErrorKind::ExpectedKeyword { keyword: ")".to_string() },
21002                            span: self.current_span(),
21003                        });
21004                    }
21005                    self.advance(); // consume ')'
21006
21007                    let base = self.ctx.alloc_imperative_expr(Expr::Tuple(items));
21008                    self.parse_field_access_chain(base)
21009                } else {
21010                    // Just a parenthesized expression
21011                    if !self.check(&TokenType::RParen) {
21012                        return Err(ParseError {
21013                            kind: ParseErrorKind::ExpectedKeyword { keyword: ")".to_string() },
21014                            span: self.current_span(),
21015                        });
21016                    }
21017                    self.advance(); // consume ')'
21018                    // A parenthesized expression supports the same postfix chain
21019                    // as any primary: `(5).double()`, `(p).x`, `(xs)[1]`.
21020                    self.parse_field_access_chain(first)
21021                }
21022            }
21023
21024            // "Call funcName with args" as an expression
21025            TokenType::Call => {
21026                let start_tok = self.current;
21027                self.advance(); // consume "Call"
21028                let function = match &self.peek().kind {
21029                    TokenType::Noun(sym) | TokenType::Adjective(sym) => {
21030                        let s = *sym;
21031                        self.advance();
21032                        s
21033                    }
21034                    TokenType::Verb { .. } | TokenType::Ambiguous { .. } => {
21035                        let s = self.peek().lexeme;
21036                        self.advance();
21037                        s
21038                    }
21039                    _ => {
21040                        return Err(ParseError {
21041                            kind: ParseErrorKind::ExpectedIdentifier,
21042                            span: self.current_span(),
21043                        });
21044                    }
21045                };
21046                let args = if self.check_preposition_is("with") {
21047                    self.advance(); // consume "with"
21048                    self.parse_call_arguments()?
21049                } else {
21050                    Vec::new()
21051                };
21052                self.record_call_span(&args, start_tok);
21053                Ok(self.ctx.alloc_imperative_expr(Expr::Call { function, args }))
21054            }
21055
21056            // Universal identifier freedom (reference position). Any remaining word-keyword
21057            // used where a value is expected is a variable reference — the counterpart of the
21058            // catch-all in `expect_identifier` (binding position). Together they guarantee a
21059            // keyword can name a thing AND be read back: `Let at be 1. Show at.` So a newly
21060            // added keyword never steals a name in either position.
21061            _ if self.token_is_word_identifier(&token) => {
21062                let sym = token.lexeme;
21063                self.advance();
21064                // Mirror the Noun arm: `name(args)` is a call, bare `name` is a reference.
21065                if self.check(&TokenType::LParen) {
21066                    self.parse_call_expr(sym)
21067                } else {
21068                    Ok(self.ctx.alloc_imperative_expr(Expr::Identifier(sym)))
21069                }
21070            }
21071
21072            _ => {
21073                Err(ParseError {
21074                    kind: ParseErrorKind::ExpectedExpression,
21075                    span: self.current_span(),
21076                })
21077            }
21078        }
21079    }
21080
21081    /// Parse a closure expression: `(params) -> expr` or `(params) ->:` block.
21082    /// Called speculatively — will fail (and rollback) if not a closure.
21083    fn parse_closure_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21084        use crate::ast::stmt::ClosureBody;
21085
21086        // Expect '('
21087        if !self.check(&TokenType::LParen) {
21088            return Err(ParseError {
21089                kind: ParseErrorKind::ExpectedExpression,
21090                span: self.current_span(),
21091            });
21092        }
21093        self.advance(); // consume '('
21094
21095        // Parse parameter list
21096        let mut params = Vec::new();
21097        if !self.check(&TokenType::RParen) {
21098            // First parameter: name: Type
21099            let name = self.expect_identifier()?;
21100            if !self.check(&TokenType::Colon) {
21101                return Err(ParseError {
21102                    kind: ParseErrorKind::ExpectedKeyword {
21103                        keyword: ":".to_string(),
21104                    },
21105                    span: self.current_span(),
21106                });
21107            }
21108            self.advance(); // consume ':'
21109            let ty = self.parse_type_expression()?;
21110            let ty_ref = self.ctx.alloc_type_expr(ty);
21111            params.push((name, ty_ref));
21112
21113            // Additional parameters
21114            while self.check(&TokenType::Comma) {
21115                self.advance(); // consume ','
21116                let name = self.expect_identifier()?;
21117                if !self.check(&TokenType::Colon) {
21118                    return Err(ParseError {
21119                        kind: ParseErrorKind::ExpectedKeyword {
21120                            keyword: ":".to_string(),
21121                        },
21122                        span: self.current_span(),
21123                    });
21124                }
21125                self.advance(); // consume ':'
21126                let ty = self.parse_type_expression()?;
21127                let ty_ref = self.ctx.alloc_type_expr(ty);
21128                params.push((name, ty_ref));
21129            }
21130        }
21131
21132        // Expect ')'
21133        if !self.check(&TokenType::RParen) {
21134            return Err(ParseError {
21135                kind: ParseErrorKind::ExpectedKeyword {
21136                    keyword: ")".to_string(),
21137                },
21138                span: self.current_span(),
21139            });
21140        }
21141        self.advance(); // consume ')'
21142
21143        // Expect '->'
21144        if !self.check(&TokenType::Arrow) {
21145            return Err(ParseError {
21146                kind: ParseErrorKind::ExpectedKeyword {
21147                    keyword: "->".to_string(),
21148                },
21149                span: self.current_span(),
21150            });
21151        }
21152        self.advance(); // consume '->'
21153
21154        // Check for block body (->:) vs expression body (-> expr)
21155        let body = if self.check(&TokenType::Colon) {
21156            self.advance(); // consume ':'
21157            ClosureBody::Block(self.read_indented_block()?)
21158        } else {
21159            // Single expression body — use parse_condition to support comparisons and boolean ops
21160            let expr = self.parse_condition()?;
21161            ClosureBody::Expression(expr)
21162        };
21163
21164        Ok(self.ctx.alloc_imperative_expr(Expr::Closure {
21165            params,
21166            body,
21167            return_type: None,
21168        }))
21169    }
21170
21171    /// Parse a complete imperative expression with full operator precedence.
21172    /// Delegates to parse_condition to support and/or/xor/shifts/comparisons
21173    /// in all expression contexts (Let, Set, function arguments, etc.).
21174    fn parse_imperative_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21175        self.parse_condition()
21176    }
21177
21178    /// The bitwise chain, C precedence: `|` lowest, then `^`/`xor`, then
21179    /// `&`, all below comparison and above additive. On Sets the same
21180    /// operators are union / symmetric-difference / intersection.
21181    fn parse_xor_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21182        let mut left = self.parse_bitxor_expr()?;
21183
21184        while self.check(&TokenType::VBar) {
21185            self.advance(); // consume "|"
21186            let right = self.parse_bitxor_expr()?;
21187            left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21188                op: BinaryOpKind::BitOr,
21189                left,
21190                right,
21191            });
21192        }
21193
21194        Ok(left)
21195    }
21196
21197    /// `^` (or the word `xor`): bitwise XOR / set symmetric difference.
21198    fn parse_bitxor_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21199        let mut left = self.parse_bitand_expr()?;
21200
21201        while self.check(&TokenType::Xor) || self.check(&TokenType::Caret) {
21202            self.advance(); // consume "xor" / "^"
21203            let right = self.parse_bitand_expr()?;
21204            left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21205                op: BinaryOpKind::BitXor,
21206                left,
21207                right,
21208            });
21209        }
21210
21211        Ok(left)
21212    }
21213
21214    /// `&`: bitwise AND / set intersection.
21215    fn parse_bitand_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21216        let mut left = self.parse_additive_expr()?;
21217
21218        while self.check(&TokenType::Amp) {
21219            self.advance(); // consume "&"
21220            let right = self.parse_additive_expr()?;
21221            left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21222                op: BinaryOpKind::BitAnd,
21223                left,
21224                right,
21225            });
21226        }
21227
21228        Ok(left)
21229    }
21230
21231    /// Parse additive expressions (+, -, combined with, union, intersection, contains) - left-to-right associative
21232    fn parse_additive_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21233        let mut left = self.parse_shift_expr()?;
21234
21235        loop {
21236            match &self.peek().kind {
21237                TokenType::Plus => {
21238                    self.advance();
21239                    let right = self.parse_shift_expr()?;
21240                    left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21241                        op: BinaryOpKind::Add,
21242                        left,
21243                        right,
21244                    });
21245                }
21246                TokenType::Minus => {
21247                    self.advance();
21248                    let right = self.parse_shift_expr()?;
21249                    left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21250                        op: BinaryOpKind::Subtract,
21251                        left,
21252                        right,
21253                    });
21254                }
21255                // Phase 53: "combined with" for string concatenation
21256                TokenType::Combined => {
21257                    self.advance(); // consume "combined"
21258                                    // Expect "with" (preposition)
21259                    if !self.check_preposition_is("with") {
21260                        return Err(ParseError {
21261                            kind: ParseErrorKind::ExpectedKeyword {
21262                                keyword: "with".to_string(),
21263                            },
21264                            span: self.current_span(),
21265                        });
21266                    }
21267                    self.advance(); // consume "with"
21268                    let right = self.parse_shift_expr()?;
21269                    left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21270                        op: BinaryOpKind::Concat,
21271                        left,
21272                        right,
21273                    });
21274                }
21275                // "followed by" for sequence concatenation (merge two sequences into one)
21276                TokenType::Followed => {
21277                    self.advance(); // consume "followed"
21278                    if !self.check_preposition_is("by") {
21279                        return Err(ParseError {
21280                            kind: ParseErrorKind::ExpectedKeyword {
21281                                keyword: "by".to_string(),
21282                            },
21283                            span: self.current_span(),
21284                        });
21285                    }
21286                    self.advance(); // consume "by"
21287                    let right = self.parse_shift_expr()?;
21288                    left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21289                        op: BinaryOpKind::SeqConcat,
21290                        left,
21291                        right,
21292                    });
21293                }
21294                // Set operations: union, intersection
21295                TokenType::Union => {
21296                    self.advance(); // consume "union"
21297                    let right = self.parse_shift_expr()?;
21298                    left = self.ctx.alloc_imperative_expr(Expr::Union { left, right });
21299                }
21300                TokenType::Intersection => {
21301                    self.advance(); // consume "intersection"
21302                    let right = self.parse_shift_expr()?;
21303                    left = self
21304                        .ctx
21305                        .alloc_imperative_expr(Expr::Intersection { left, right });
21306                }
21307                // Set membership: "set contains value"
21308                TokenType::Contains => {
21309                    self.advance(); // consume "contains"
21310                    let value = self.parse_shift_expr()?;
21311                    left = self.ctx.alloc_imperative_expr(Expr::Contains {
21312                        collection: left,
21313                        value,
21314                    });
21315                }
21316                // `a without b` — the English set difference (`-` is the
21317                // symbolic spelling). Lowers to Subtract, which dispatches on
21318                // Set operands.
21319                _ if self.check_word("without") => {
21320                    self.advance(); // consume "without"
21321                    let right = self.parse_shift_expr()?;
21322                    left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21323                        op: BinaryOpKind::Subtract,
21324                        left,
21325                        right,
21326                    });
21327                }
21328                // `n copies of x` — the English fill: a fresh sequence of n
21329                // deep-copied slots (`[x] * n` is the symbolic spelling).
21330                // Guarded on the following `of`, so a plain variable named
21331                // `copies` keeps resolving as an identifier.
21332                _ if self.check_word("copies") && self.peek_word_at(1, "of") => {
21333                    self.advance(); // consume "copies"
21334                    self.advance(); // consume "of"
21335                    let element = self.parse_shift_expr()?;
21336                    let repeat_sym = self.interner.intern("repeatSeq");
21337                    left = self.ctx.alloc_imperative_expr(Expr::Call {
21338                        function: repeat_sym,
21339                        args: vec![element, left],
21340                    });
21341                }
21342                // English infix `A plus B` / `A minus B` — the spoken spellings of `+`/`-`,
21343                // additive precedence. `check_op_word` yields to a same-named variable.
21344                _ if self.check_op_word("plus") => {
21345                    self.advance(); // consume "plus"
21346                    let right = self.parse_shift_expr()?;
21347                    left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21348                        op: BinaryOpKind::Add,
21349                        left,
21350                        right,
21351                    });
21352                }
21353                _ if self.check_op_word("minus") => {
21354                    self.advance(); // consume "minus"
21355                    let right = self.parse_shift_expr()?;
21356                    left = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21357                        op: BinaryOpKind::Subtract,
21358                        left,
21359                        right,
21360                    });
21361                }
21362                _ => break,
21363            }
21364        }
21365
21366        Ok(left)
21367    }
21368
21369    /// Parse shift expressions: "x shifted left by y" / "x shifted right by y"
21370    /// Precedence: below additive, above multiplicative.
21371    fn parse_shift_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21372        let mut left = self.parse_multiplicative_expr()?;
21373
21374        loop {
21375            if !self.check(&TokenType::Shifted) {
21376                break;
21377            }
21378            self.advance(); // consume "shifted"
21379
21380            let is_left = self.check_word("left");
21381            if is_left {
21382                self.advance(); // consume "left"
21383            } else if self.check_word("right") {
21384                self.advance(); // consume "right"
21385            } else {
21386                return Err(ParseError {
21387                    kind: ParseErrorKind::ExpectedKeyword {
21388                        keyword: "left or right".to_string(),
21389                    },
21390                    span: self.current_span(),
21391                });
21392            }
21393
21394            // Expect "by"
21395            if !self.check_preposition_is("by") && !self.check_word("by") {
21396                return Err(ParseError {
21397                    kind: ParseErrorKind::ExpectedKeyword {
21398                        keyword: "by".to_string(),
21399                    },
21400                    span: self.current_span(),
21401                });
21402            }
21403            self.advance(); // consume "by"
21404
21405            let right = self.parse_multiplicative_expr()?;
21406            let op = if is_left {
21407                BinaryOpKind::Shl
21408            } else {
21409                BinaryOpKind::Shr
21410            };
21411            left = self
21412                .ctx
21413                .alloc_imperative_expr(Expr::BinaryOp { op, left, right });
21414        }
21415
21416        Ok(left)
21417    }
21418
21419    /// Parse unary expressions (currently just unary minus)
21420    fn parse_unary_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21421        use crate::ast::{Expr, Literal};
21422
21423        if self.check(&TokenType::Tilde) {
21424            self.advance(); // consume "~"
21425            let operand = self.parse_unary_expr()?;
21426            // `~x` IS `x ^ -1` in two's complement — reuse the existing
21427            // BitXor path end to end (no new op anywhere downstream).
21428            let neg_one = self
21429                .ctx
21430                .alloc_imperative_expr(Expr::Literal(Literal::Number(-1)));
21431            return Ok(self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21432                op: BinaryOpKind::BitXor,
21433                left: operand,
21434                right: neg_one,
21435            }));
21436        }
21437        if self.check(&TokenType::Minus) {
21438            // A negative LITERAL parses as one exact literal — the sign
21439            // attaches to the digits, so `-9223372036854775808` (i64::MIN,
21440            // whose magnitude alone overflows) and `-1.5` (a real Float
21441            // literal, not `0 - 1.5`) both come out exact.
21442            if let Some(TokenType::Number(sym)) =
21443                self.tokens.get(self.current + 1).map(|t| t.kind.clone())
21444            {
21445                self.advance(); // consume '-'
21446                self.advance(); // consume the numeral
21447                let num_str = self.interner.resolve(sym).to_string();
21448                if !Self::is_radix_numeral(&num_str)
21449                    && (num_str.contains('.') || num_str.contains('e') || num_str.contains('E'))
21450                {
21451                    let f = num_str
21452                        .replace('_', "")
21453                        .parse::<f64>()
21454                        .map_err(|_| ParseError {
21455                            kind: ParseErrorKind::ExpectedKeyword {
21456                                keyword: format!("a Float literal (got `-{}`)", num_str),
21457                            },
21458                            span: self.current_span(),
21459                        })?;
21460                    return Ok(self
21461                        .ctx
21462                        .alloc_imperative_expr(Expr::Literal(Literal::Float(-f))));
21463                }
21464                let n = self.parse_i64_numeral_signed(&num_str, true)?;
21465                return Ok(self
21466                    .ctx
21467                    .alloc_imperative_expr(Expr::Literal(Literal::Number(n))));
21468            }
21469            if self.tokens.get(self.current + 1).is_some_and(|t| {
21470                self.interner.resolve(t.lexeme) == "infinity"
21471                    && !self.user_bound.contains(&t.lexeme)
21472            }) {
21473                self.advance(); // consume '-'
21474                self.advance(); // consume "infinity"
21475                return Ok(self
21476                    .ctx
21477                    .alloc_imperative_expr(Expr::Literal(Literal::Float(f64::NEG_INFINITY))));
21478            }
21479            self.advance(); // consume '-'
21480            let operand = self.parse_unary_expr()?; // recursive for --5
21481                                                    // Implement as 0 - operand (no UnaryOp variant in Expr)
21482            return Ok(self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21483                op: BinaryOpKind::Subtract,
21484                left: self
21485                    .ctx
21486                    .alloc_imperative_expr(Expr::Literal(Literal::Number(0))),
21487                right: operand,
21488            }));
21489        }
21490        self.parse_primary_expr()
21491    }
21492
21493    /// Parse multiplicative expressions (*, /, %) - left-to-right associative
21494    /// Exponentiation  — binds tighter than , RIGHT-associative
21495    /// ( = ). The base is a unary expression; the exponent
21496    /// recurses into itself for right-associativity.
21497    fn parse_power_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21498        let base = self.parse_unary_expr()?;
21499        // `**` or the English spelling `A to the power of B` — both right-associative.
21500        let symbolic = self.check(&TokenType::StarStar);
21501        let spoken = (self.check_to_preposition() || self.check_word("to"))
21502            && self.peek_word_at(1, "the")
21503            && self.peek_word_at(2, "power")
21504            && self.peek_word_at(3, "of");
21505        if symbolic || spoken {
21506            if symbolic {
21507                self.advance(); // consume "**"
21508            } else {
21509                self.advance(); // "to"
21510                self.advance(); // "the"
21511                self.advance(); // "power"
21512                self.advance(); // "of"
21513            }
21514            let exp = self.parse_power_expr()?;
21515            return Ok(self.ctx.alloc_imperative_expr(Expr::BinaryOp {
21516                op: BinaryOpKind::Pow,
21517                left: base,
21518                right: exp,
21519            }));
21520        }
21521        Ok(base)
21522    }
21523
21524    fn parse_multiplicative_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21525        let mut left = self.parse_power_expr()?;
21526
21527        loop {
21528            let op = match &self.peek().kind {
21529                TokenType::Star => {
21530                    self.advance();
21531                    BinaryOpKind::Multiply
21532                }
21533                TokenType::Slash => {
21534                    self.advance();
21535                    BinaryOpKind::Divide
21536                }
21537                TokenType::SlashSlash => {
21538                    self.advance();
21539                    BinaryOpKind::FloorDivide
21540                }
21541                TokenType::Percent => {
21542                    self.advance();
21543                    BinaryOpKind::Modulo
21544                }
21545                // English infix `A times B`, `A divided by B`, `A modulo B` (`mod`) — the spoken
21546                // spellings of `*`/`/`/`%`, multiplicative precedence. `check_op_word` yields to a
21547                // same-named variable (declarer-wins).
21548                _ if self.check_op_word("times") => {
21549                    self.advance(); // consume "times"
21550                    BinaryOpKind::Multiply
21551                }
21552                _ if self.check_op_word("divided") && self.peek_word_at(1, "by") => {
21553                    self.advance(); // consume "divided"
21554                    self.advance(); // consume "by"
21555                    BinaryOpKind::Divide
21556                }
21557                _ if self.check_op_word("modulo") || self.check_op_word("mod") => {
21558                    self.advance(); // consume "modulo"/"mod"
21559                    BinaryOpKind::Modulo
21560                }
21561                _ => break,
21562            };
21563            let right = self.parse_power_expr()?;
21564            left = self
21565                .ctx
21566                .alloc_imperative_expr(Expr::BinaryOp { op, left, right });
21567        }
21568
21569        Ok(left)
21570    }
21571
21572    /// Try to parse a binary operator (+, -, *, /)
21573    fn try_parse_binary_op(&mut self) -> Option<BinaryOpKind> {
21574        match &self.peek().kind {
21575            TokenType::Plus => {
21576                self.advance();
21577                Some(BinaryOpKind::Add)
21578            }
21579            TokenType::Minus => {
21580                self.advance();
21581                Some(BinaryOpKind::Subtract)
21582            }
21583            TokenType::Star => {
21584                self.advance();
21585                Some(BinaryOpKind::Multiply)
21586            }
21587            TokenType::Slash => {
21588                self.advance();
21589                Some(BinaryOpKind::Divide)
21590            }
21591            _ => None,
21592        }
21593    }
21594
21595    /// Parse the contents of an interpolated string into a sequence of literal/expression parts.
21596    ///
21597    /// Input is the raw string content (without quotes), e.g., `Hello, {name}! Value: {x:.2}`
21598    /// `{{` and `}}` are escape sequences for literal braces. Every error found in here —
21599    /// including one from inside a hole, whose own tokens count from the hole — points at
21600    /// `span`, the string literal it was written in.
21601    fn parse_interpolation_parts(
21602        &mut self,
21603        raw: &str,
21604        span: Span,
21605    ) -> ParseResult<Vec<crate::ast::stmt::StringPart<'a>>> {
21606        use crate::ast::stmt::StringPart;
21607
21608        let mut parts = Vec::new();
21609        let chars: Vec<char> = raw.chars().collect();
21610        let mut i = 0;
21611        let mut literal_buf = String::new();
21612
21613        while i < chars.len() {
21614            match chars[i] {
21615                '{' if i + 1 < chars.len() && chars[i + 1] == '{' => {
21616                    // Escaped brace: {{ → literal {
21617                    literal_buf.push('{');
21618                    i += 2;
21619                }
21620                '{' => {
21621                    // Flush literal buffer
21622                    if !literal_buf.is_empty() {
21623                        let sym = self.interner.intern(&literal_buf);
21624                        parts.push(StringPart::Literal(sym));
21625                        literal_buf.clear();
21626                    }
21627
21628                    // Find matching closing brace
21629                    let start = i + 1;
21630                    let mut depth = 1;
21631                    let mut j = start;
21632                    while j < chars.len() && depth > 0 {
21633                        if chars[j] == '{' {
21634                            depth += 1;
21635                        }
21636                        if chars[j] == '}' {
21637                            depth -= 1;
21638                        }
21639                        if depth > 0 {
21640                            j += 1;
21641                        }
21642                    }
21643                    if depth != 0 {
21644                        return Err(ParseError {
21645                            kind: crate::error::ParseErrorKind::Custom(
21646                                "Unclosed interpolation brace in string — a `{` opens a hole that \
21647                                 nothing closes. Add the `}`, or, if you meant the character \
21648                                 itself, double it: `{{{{` prints `{{`."
21649                                    .to_string(),
21650                            ),
21651                            span,
21652                        });
21653                    }
21654
21655                    let hole_content: String = chars[start..j].iter().collect();
21656
21657                    // Detect debug format: {var=} or {var=:.2}
21658                    // Debug `=` is always the LAST `=` in the hole content.
21659                    // Using rfind ensures comparison operators like `==` don't trigger
21660                    // false positives — `{x == 5}` has no trailing `=` after an identifier.
21661                    let (hole_after_debug, is_debug) = {
21662                        if let Some(eq_pos) = hole_content.rfind('=') {
21663                            let before_eq = hole_content[..eq_pos].trim();
21664                            // Only treat as debug if the part before = is a simple identifier
21665                            // and the `=` is not part of `==`, `<=`, `>=`, `!=`
21666                            let is_double_eq = eq_pos > 0
21667                                && hole_content.as_bytes().get(eq_pos - 1) == Some(&b'=');
21668                            let is_preceded_by_comparison = eq_pos > 0
21669                                && matches!(
21670                                    hole_content.as_bytes().get(eq_pos - 1),
21671                                    Some(b'!' | b'<' | b'>')
21672                                );
21673                            if !is_double_eq
21674                                && !is_preceded_by_comparison
21675                                && !before_eq.is_empty()
21676                                && before_eq.chars().all(|c| c.is_alphanumeric() || c == '_')
21677                            {
21678                                (
21679                                    hole_content[..eq_pos].to_string()
21680                                        + &hole_content[eq_pos + 1..],
21681                                    true,
21682                                )
21683                            } else {
21684                                (hole_content.clone(), false)
21685                            }
21686                        } else {
21687                            (hole_content.clone(), false)
21688                        }
21689                    };
21690
21691                    // Split on `:` for format specifier (but not `::`)
21692                    let (expr_str, format_spec) =
21693                        if let Some(colon_pos) = hole_after_debug.rfind(':') {
21694                            let before = &hole_after_debug[..colon_pos];
21695                            let after = &hole_after_debug[colon_pos + 1..];
21696                            // Only treat as format spec if the part after `:` looks like a format spec
21697                            // (starts with `.`, `<`, `>`, `^`, `$`, or a digit, or is a known specifier letter)
21698                            if !after.is_empty()
21699                                && (after.starts_with('.')
21700                                    || after.starts_with('<')
21701                                    || after.starts_with('>')
21702                                    || after.starts_with('^')
21703                                    || after == "$"
21704                                    || after.chars().next().map_or(false, |c| c.is_ascii_digit()))
21705                            {
21706                                // Validate the format spec matches a known pattern
21707                                let valid = if after == "$" {
21708                                    true
21709                                } else if after.starts_with('.') {
21710                                    after[1..].parse::<usize>().is_ok()
21711                                } else if after.starts_with('<')
21712                                    || after.starts_with('>')
21713                                    || after.starts_with('^')
21714                                {
21715                                    after[1..].parse::<usize>().is_ok()
21716                                } else {
21717                                    after.parse::<usize>().is_ok()
21718                                };
21719                                if !valid {
21720                                    return Err(ParseError {
21721                                        kind: crate::error::ParseErrorKind::Custom(format!(
21722                                            "Invalid format specifier `{}` in interpolation hole",
21723                                            after
21724                                        )),
21725                                        span,
21726                                    });
21727                                }
21728                                (before.to_string(), Some(after.to_string()))
21729                            } else {
21730                                (hole_after_debug.clone(), None)
21731                            }
21732                        } else {
21733                            (hole_after_debug.clone(), None)
21734                        };
21735
21736                    // Parse the expression through a sub-parser
21737                    let expr_source = expr_str.trim().to_string();
21738                    if expr_source.is_empty() {
21739                        return Err(ParseError {
21740                            kind: crate::error::ParseErrorKind::Custom(
21741                                "Empty interpolation hole in string — `{}` asks me to print a \
21742                                 value and names none. Put the value inside the braces, or, if you \
21743                                 meant the two characters themselves (an empty JSON object, a CSS \
21744                                 rule), double them: write `{{{{}}}}` to get `{}`."
21745                                    .to_string(),
21746                            ),
21747                            span,
21748                        });
21749                    }
21750
21751                    // Lex + parse the expression fragment by temporarily swapping
21752                    // the parser's token stream
21753                    let sub_expr = {
21754                        // ★ A HOLE SPEAKS THE LANGUAGE IT IS WRITTEN IN. This was a bare
21755                        // `Lexer::new`, which is DECLARATIVE mode — while every string that can
21756                        // hold a hole lives inside imperative code. So the whole imperative
21757                        // vocabulary was invisible in here: `length`, `copy`, `sum` and every
21758                        // other word the imperative lexer promotes to a keyword arrived as a bare
21759                        // identifier, and `"{length of tasks}"` failed with `Undefined variable:
21760                        // length` — naming a variable the author never wrote, for a phrase that is
21761                        // correct LOGOS three characters to the left of the quote.
21762                        //
21763                        // The fragment is part of the surrounding program, so it is read with the
21764                        // surrounding program's vocabulary. `parse_expr_snippet` in `component.rs`
21765                        // already re-lexes this way; this site is now the same shape.
21766                        let mut sub_lexer = match self.mode {
21767                            ParserMode::Imperative => {
21768                                crate::lexer::Lexer::new_imperative(&expr_source, self.interner)
21769                            }
21770                            ParserMode::Declarative => {
21771                                crate::lexer::Lexer::new(&expr_source, self.interner)
21772                            }
21773                        };
21774                        let sub_tokens = sub_lexer.tokenize();
21775
21776                        // Save and swap parser state
21777                        let saved_tokens = std::mem::replace(&mut self.tokens, sub_tokens);
21778                        let saved_current = self.backtrack_point();
21779                        self.current = 0;
21780
21781                        let result = self.parse_primary_or_binary_expr();
21782
21783                        // ★ THE HOLE MUST BE READ WHOLE. The sub-parser stops at the first thing
21784                        // it cannot fold into an expression, and its leftover tokens used to be
21785                        // DISCARDED here without anybody looking at them — so a hole that was not
21786                        // entirely an expression rendered the prefix that happened to parse and
21787                        // silently threw the rest away. `"x{5 garbage here}y"` printed `x5y`, and
21788                        // `"{"a":1}"` — an ordinary JSON object, the most common thing a web
21789                        // author ever types in a string — printed `a`.
21790                        //
21791                        // Refusing is right rather than treating unparseable braces as literal,
21792                        // because a literal brace ALREADY has a spelling: `{{` and `}}`, as in
21793                        // `format!`. Silently rendering half a hole is the only option of the
21794                        // three that leaves the page looking like it worked.
21795                        let unread = match &result {
21796                            Ok(_) if !self.is_at_end() => {
21797                                let at = self.peek().span.start.min(expr_source.len());
21798                                let read = expr_source.get(..at).unwrap_or("").trim().to_string();
21799                                let rest = expr_source.get(at..).unwrap_or("").trim().to_string();
21800                                Some((read, rest))
21801                            }
21802                            _ => None,
21803                        };
21804
21805                        // Restore parser state
21806                        self.tokens = saved_tokens;
21807                        self.backtrack_to(&saved_current);
21808
21809                        if let Some((read, rest)) = unread {
21810                            return Err(ParseError {
21811                                kind: crate::error::ParseErrorKind::Custom(format!(
21812                                    "I read `{read}` in this interpolation hole and then found \
21813                                     `{rest}`, which is not part of it. A hole holds ONE value, so \
21814                                     everything between the braces has to be that one value. If \
21815                                     you meant the braces themselves — a JSON object, a CSS rule — \
21816                                     double them: `{{{{` and `}}}}` print `{{` and `}}`."
21817                                )),
21818                                span,
21819                            });
21820                        }
21821
21822                        result.map_err(|e| ParseError { span, ..e })?
21823                    };
21824
21825                    // ★ A WIDTH SPECIFIER ON A TEXT LITERAL IS ALWAYS A MISTAKE — and it is the
21826                    // one that silently eats JSON. `{"a":1}` splits at the colon into the literal
21827                    // `"a"` and the width `1`, so an ordinary JSON object printed `a` on every
21828                    // tier, with no error. Padding a LITERAL to a width is something the author
21829                    // can simply type out, so refusing it costs nothing, while `{n:5}` and
21830                    // `{3.14:2}` — a width on a value that varies, which is the entire point of
21831                    // the feature — are untouched.
21832                    //
21833                    // This is the second half of "a hole is read whole". The first half caught a
21834                    // hole whose tokens were not all consumed; this catches one where they WERE,
21835                    // by a reading no author ever intends.
21836                    if format_spec.is_some() && matches!(sub_expr, Expr::Literal(Literal::Text(_)))
21837                    {
21838                        return Err(ParseError {
21839                            kind: crate::error::ParseErrorKind::Custom(format!(
21840                                "`{{{hole_after_debug}}}` reads as a text literal padded to a \
21841                                 width — a value you could write out directly, so this is almost \
21842                                 always a `:` that was meant literally. If you meant a JSON \
21843                                 object, a CSS rule, or anything else containing braces, double \
21844                                 them: `{{{{` and `}}}}` print `{{` and `}}`."
21845                            )),
21846                            span,
21847                        });
21848                    }
21849
21850                    let format_sym = format_spec.map(|s| self.interner.intern(&s));
21851                    parts.push(StringPart::Expr {
21852                        value: sub_expr,
21853                        format_spec: format_sym,
21854                        debug: is_debug,
21855                    });
21856
21857                    i = j + 1; // skip past closing '}'
21858                }
21859                '}' if i + 1 < chars.len() && chars[i + 1] == '}' => {
21860                    // Escaped brace: }} → literal }
21861                    literal_buf.push('}');
21862                    i += 2;
21863                }
21864                _ => {
21865                    literal_buf.push(chars[i]);
21866                    i += 1;
21867                }
21868            }
21869        }
21870
21871        // Flush remaining literal
21872        if !literal_buf.is_empty() {
21873            let sym = self.interner.intern(&literal_buf);
21874            parts.push(StringPart::Literal(sym));
21875        }
21876
21877        Ok(parts)
21878    }
21879
21880    /// Parse a primary expression or a full binary expression for interpolation holes.
21881    fn parse_primary_or_binary_expr(&mut self) -> ParseResult<&'a Expr<'a>> {
21882        self.parse_imperative_expr()
21883    }
21884
21885    /// Parse a Span literal starting from a number that was already consumed.
21886    /// Handles patterns like: "3 days", "2 months", "1 year and 3 days"
21887    fn parse_span_literal_from_num(&mut self, first_num_str: &str) -> ParseResult<&'a Expr<'a>> {
21888        use crate::ast::Literal;
21889        use crate::token::CalendarUnit;
21890
21891        let first_num = self.parse_i64_numeral(first_num_str)?;
21892
21893        // We expect a CalendarUnit after the number
21894        let unit = match self.peek().kind {
21895            TokenType::CalendarUnit(u) => u,
21896            _ => {
21897                return Err(ParseError {
21898                    kind: ParseErrorKind::ExpectedKeyword {
21899                        keyword: "calendar unit (day, week, month, year)".to_string(),
21900                    },
21901                    span: self.current_span(),
21902                });
21903            }
21904        };
21905        self.advance(); // consume the CalendarUnit
21906
21907        // Calendar fields (months/days) feed a `Span`; sub-day CLOCK units
21908        // (hour/minute/second) feed a nanosecond `Duration`. A literal built
21909        // purely from clock units IS a Duration — `After 5 seconds`,
21910        // `Sleep for 2 minutes`; a calendar (or mixed) literal stays a Span,
21911        // which models only months/days.
21912        let mut total_months: i64 = 0;
21913        let mut total_days: i64 = 0;
21914        let mut total_nanos: i64 = 0;
21915        let mut saw_calendar = false;
21916        let mut saw_clock = false;
21917
21918        fn apply(
21919            unit: CalendarUnit,
21920            n: i64,
21921            months: &mut i64,
21922            days: &mut i64,
21923            nanos: &mut i64,
21924            cal: &mut bool,
21925            clk: &mut bool,
21926        ) {
21927            match unit {
21928                CalendarUnit::Day => {
21929                    *days += n;
21930                    *cal = true;
21931                }
21932                CalendarUnit::Week => {
21933                    *days += n * 7;
21934                    *cal = true;
21935                }
21936                CalendarUnit::Month => {
21937                    *months += n;
21938                    *cal = true;
21939                }
21940                CalendarUnit::Year => {
21941                    *months += n * 12;
21942                    *cal = true;
21943                }
21944                CalendarUnit::Hour => {
21945                    *nanos += n * 3_600_000_000_000;
21946                    *clk = true;
21947                }
21948                CalendarUnit::Minute => {
21949                    *nanos += n * 60_000_000_000;
21950                    *clk = true;
21951                }
21952                CalendarUnit::Second => {
21953                    *nanos += n * 1_000_000_000;
21954                    *clk = true;
21955                }
21956                CalendarUnit::Millisecond => {
21957                    *nanos += n * 1_000_000;
21958                    *clk = true;
21959                }
21960                CalendarUnit::Microsecond => {
21961                    *nanos += n * 1_000;
21962                    *clk = true;
21963                }
21964                CalendarUnit::Nanosecond => {
21965                    *nanos += n;
21966                    *clk = true;
21967                }
21968            }
21969        }
21970        apply(
21971            unit,
21972            first_num,
21973            &mut total_months,
21974            &mut total_days,
21975            &mut total_nanos,
21976            &mut saw_calendar,
21977            &mut saw_clock,
21978        );
21979
21980        // Check for "and" followed by more Number + CalendarUnit
21981        while self.check(&TokenType::And) {
21982            self.advance(); // consume "and"
21983
21984            // Expect another Number
21985            let next_num = match &self.peek().kind {
21986                TokenType::Number(sym) => {
21987                    let num_str = self.interner.resolve(*sym).to_string();
21988                    self.advance();
21989                    self.parse_i64_numeral(&num_str)?
21990                }
21991                _ => break, // Not a number, backtrack is complex so just stop
21992            };
21993
21994            // Expect another CalendarUnit
21995            let next_unit = match self.peek().kind {
21996                TokenType::CalendarUnit(u) => {
21997                    self.advance();
21998                    u
21999                }
22000                _ => break, // Not a unit, backtrack is complex so just stop
22001            };
22002
22003            apply(
22004                next_unit,
22005                next_num,
22006                &mut total_months,
22007                &mut total_days,
22008                &mut total_nanos,
22009                &mut saw_calendar,
22010                &mut saw_clock,
22011            );
22012        }
22013
22014        if saw_clock && !saw_calendar {
22015            Ok(self
22016                .ctx
22017                .alloc_imperative_expr(Expr::Literal(Literal::Duration(total_nanos))))
22018        } else {
22019            Ok(self.ctx.alloc_imperative_expr(Expr::Literal(Literal::Span {
22020                months: total_months as i32,
22021                days: total_days as i32,
22022            })))
22023        }
22024    }
22025
22026    /// A value-literal word (`some`, `nothing`) whose spelling a live user
22027    /// binding shadows: read it as an ordinary identifier — call, reference,
22028    /// or field chain. Kept OUT of `parse_primary_expr`'s body (and never
22029    /// inlined back) so the deeply-recursing primary frame stays within the
22030    /// AST depth gate's measured per-level stack budget.
22031    #[inline(never)]
22032    fn shadowed_literal_word_expr(&mut self, sym: Symbol) -> ParseResult<&'a Expr<'a>> {
22033        use crate::ast::Expr;
22034        let span = self.advance().span;
22035        if self.check(&TokenType::LParen) {
22036            return self.parse_call_expr(sym);
22037        }
22038        self.verify_identifier_access(sym, span)?;
22039        let base = self.ctx.alloc_imperative_expr(Expr::Identifier(sym));
22040        self.parse_field_access_chain(base)
22041    }
22042
22043    /// Phase 32: Parse function call expression: f(x, y, ...)
22044    fn parse_call_expr(&mut self, function: Symbol) -> ParseResult<&'a Expr<'a>> {
22045        use crate::ast::Expr;
22046
22047        // Every caller has consumed the function's name: the call begins one token back.
22048        let start_tok = self.current.saturating_sub(1);
22049        self.advance(); // consume '('
22050
22051        let mut args = Vec::new();
22052        if !self.check(&TokenType::RParen) {
22053            loop {
22054                args.push(self.parse_imperative_expr()?);
22055                if !self.check(&TokenType::Comma) {
22056                    break;
22057                }
22058                self.advance(); // consume ','
22059            }
22060        }
22061
22062        if !self.check(&TokenType::RParen) {
22063            return Err(ParseError {
22064                kind: ParseErrorKind::ExpectedKeyword {
22065                    keyword: ")".to_string(),
22066                },
22067                span: self.current_span(),
22068            });
22069        }
22070        self.advance(); // consume ')'
22071
22072        // Chain any trailing `'s field` / `.field` / `[i]` onto the call so
22073        // `handle(x)'s field` and `getValue(input).length` parse. A terminating
22074        // sentence `Period` is not a `Dot`, so a bare call is left untouched.
22075        self.record_call_span(&args, start_tok);
22076        let call = self
22077            .ctx
22078            .alloc_imperative_expr(Expr::Call { function, args });
22079        self.parse_field_access_chain(call)
22080    }
22081
22082    /// Phase 31: Parse field access chain via possessive ('s) and bracket indexing
22083    /// Handles patterns like: p's x, p's x's y, items[1], items[i]'s field
22084    fn parse_field_access_chain(&mut self, base: &'a Expr<'a>) -> ParseResult<&'a Expr<'a>> {
22085        use crate::ast::Expr;
22086
22087        let mut result = base;
22088
22089        // Keep parsing field accesses and bracket indexing
22090        loop {
22091            if self.check(&TokenType::Dot) {
22092                // The dot: `p.x` field access (≡ `p's x`) or `xs.f(a)` UFCS
22093                // method syntax (≡ `f(xs, a)` — the RECEIVER becomes the first
22094                // argument, so every method spelling lowers to a plain call and
22095                // all engines get it free).
22096                self.advance(); // consume "."
22097                let start_tok = self.current;
22098                let name = self.expect_identifier()?;
22099                if self.check(&TokenType::LParen) {
22100                    self.advance(); // consume "("
22101                    let mut args = vec![result]; // receiver first
22102                    if !self.check(&TokenType::RParen) {
22103                        loop {
22104                            args.push(self.parse_imperative_expr()?);
22105                            if !self.check(&TokenType::Comma) {
22106                                break;
22107                            }
22108                            self.advance(); // consume ","
22109                        }
22110                    }
22111                    if !self.check(&TokenType::RParen) {
22112                        return Err(ParseError {
22113                            kind: ParseErrorKind::ExpectedKeyword {
22114                                keyword: ")".to_string(),
22115                            },
22116                            span: self.current_span(),
22117                        });
22118                    }
22119                    self.advance(); // consume ")"
22120                    self.record_call_span(&args, start_tok);
22121                    result = self.ctx.alloc_imperative_expr(Expr::Call {
22122                        function: name,
22123                        args,
22124                    });
22125                } else {
22126                    result = self.ctx.alloc_imperative_expr(Expr::FieldAccess {
22127                        object: result,
22128                        field: name,
22129                    });
22130                }
22131            } else if self.check(&TokenType::Possessive) {
22132                // Field access: p's x
22133                self.advance(); // consume "'s"
22134                let field = self.expect_identifier()?;
22135                result = self.ctx.alloc_imperative_expr(Expr::FieldAccess {
22136                    object: result,
22137                    field,
22138                });
22139            } else if self.check(&TokenType::LBracket) {
22140                // Bracket indexing: items[1], items[i]
22141                self.advance(); // consume "["
22142                let index = self.parse_imperative_expr()?;
22143
22144                if !self.check(&TokenType::RBracket) {
22145                    return Err(ParseError {
22146                        kind: ParseErrorKind::ExpectedKeyword {
22147                            keyword: "]".to_string(),
22148                        },
22149                        span: self.current_span(),
22150                    });
22151                }
22152                self.advance(); // consume "]"
22153
22154                result = self.ctx.alloc_imperative_expr(Expr::Index {
22155                    collection: result,
22156                    index,
22157                });
22158            } else {
22159                break;
22160            }
22161        }
22162
22163        Ok(result)
22164    }
22165
22166    /// Centralized verification for identifier access in imperative mode.
22167    /// Checks for use-after-move errors on known variables; `span` is the
22168    /// name's own token, so the error points at the use itself.
22169    fn verify_identifier_access(&self, sym: Symbol, span: crate::token::Span) -> ParseResult<()> {
22170        if self.mode != ParserMode::Imperative {
22171            return Ok(());
22172        }
22173
22174        // Check if variable has been moved
22175        if let Some(crate::drs::OwnershipState::Moved) = self.world_state.get_ownership_by_var(sym)
22176        {
22177            return Err(ParseError {
22178                kind: ParseErrorKind::UseAfterMove {
22179                    name: self.interner.resolve(sym).to_string(),
22180                },
22181                span,
22182            });
22183        }
22184
22185        Ok(())
22186    }
22187
22188    fn expect_identifier(&mut self) -> ParseResult<Symbol> {
22189        let token = self.peek().clone();
22190        match &token.kind {
22191            // Standard identifiers
22192            TokenType::Noun(sym) | TokenType::ProperName(sym) | TokenType::Adjective(sym) => {
22193                self.advance();
22194                Ok(*sym)
22195            }
22196            // Verbs can be variable names in code context ("empty", "run", etc.)
22197            // Use raw lexeme to preserve original casing
22198            TokenType::Verb { .. } => {
22199                let sym = token.lexeme;
22200                self.advance();
22201                Ok(sym)
22202            }
22203            // Phase 32: Articles can be single-letter identifiers (a, an)
22204            TokenType::Article(_) => {
22205                let sym = token.lexeme;
22206                self.advance();
22207                Ok(sym)
22208            }
22209            // Overloaded tokens that are valid identifiers in code context
22210            TokenType::Pronoun { .. } |  // "i", "it"
22211            TokenType::Items |           // "items"
22212            TokenType::Values |          // "values"
22213            TokenType::Item |            // "item"
22214            TokenType::Nothing |         // "nothing"
22215            // Phase 38: Adverbs can be function names (now, sleep, etc.)
22216            TokenType::TemporalAdverb(_) |
22217            TokenType::ScopalAdverb(_) |
22218            TokenType::Adverb(_) |
22219            // Phase 10: IO keywords can be function names (read, write, file, console)
22220            TokenType::Read |
22221            TokenType::Write |
22222            TokenType::File |
22223            TokenType::Console |
22224            // Phase 49: CRDT keywords can be type/function names
22225            TokenType::Merge |
22226            TokenType::Increase |
22227            TokenType::Decrease |
22228            // Phase 51: "sleep" leads the Sleep statement but also names a native
22229            // (`## To native sleep`) — in name position the declarer wins.
22230            TokenType::Sleep |
22231            // Test-framework keywords (constitution Wave 2) lead statements but
22232            // remain legal names in name position — the declarer wins, exactly
22233            // like Sleep/Read/Write.
22234            TokenType::Expect |
22235            TokenType::Advance |
22236            TokenType::Skip |
22237            TokenType::Given |
22238            // Observation keywords lead statements but also name functions and
22239            // variables (`## To record …`, `Let note be …`) — declarer wins.
22240            TokenType::Note |
22241            TokenType::Warn |
22242            TokenType::Record |
22243            TokenType::During |
22244            // "swap" leads the reversible Swap statement but also names user
22245            // functions (`## To swap …`) — the declarer wins (the Sleep lesson).
22246            TokenType::Swap |
22247            // Phase 49b: CRDT type keywords can be type names
22248            TokenType::Tally |
22249            TokenType::SharedSet |
22250            TokenType::SharedSequence |
22251            TokenType::CollaborativeSequence |
22252            // Phase 54: "first", "second", etc. can be variable names
22253            // Phase 57: "add", "remove" can be function names
22254            TokenType::Add |
22255            TokenType::Remove |
22256            TokenType::First |
22257            // Correlative conjunctions and other keywords usable as identifiers
22258            TokenType::Both |            // "both" (correlative: both X and Y)
22259            TokenType::Either |          // "either" (correlative: either X or Y)
22260            TokenType::Combined |        // "combined" (string concat: combined with)
22261            TokenType::Followed |        // "followed" (seq concat: followed by; also a variable name)
22262            TokenType::Shared |          // "shared" (CRDT type modifier)
22263            TokenType::All |             // "all" (FOL quantifier, valid variable name in imperative)
22264            TokenType::Some |            // "some" (FOL quantifier / option literal; also the prelude
22265                                         // Option's variant — `When Some (v):`, `a new Some …` — the
22266                                         // declarer wins in name position, exactly like All/Sleep)
22267            // Calendar units can be type/variable names (Day, Week, Month, Year)
22268            TokenType::CalendarUnit(_) |
22269            // Phase 103: Focus particles can be variant names (Just, Only, Even)
22270            TokenType::Focus(_) |
22271            // Phrasal verb particles can be variable names (out, up, down, etc.)
22272            TokenType::Particle(_) |
22273            // Prepositions can be variable names in code context (from, into, etc.)
22274            TokenType::Preposition(_) |
22275            // Identifier freedom (identifier_freedom.rs): everyday words that other
22276            // constructs keywordize must still name variables, fields, and params —
22277            // in name position the user wins, exactly like Sleep/Read/Write above.
22278            TokenType::Size |            // "size" (zone syntax: of size 1 MB)
22279            TokenType::Zone |            // "zone" (Inside a zone ...)
22280            TokenType::Inside |          // "inside"
22281            TokenType::Called |          // "called"
22282            TokenType::Launch |          // "launch" (Launch a task ...)
22283            TokenType::Task |            // "task"
22284            TokenType::Pipe |            // "pipe" (a Pipe of T)
22285            TokenType::Stop |            // "stop" (Stop handle.)
22286            TokenType::Receive |         // "receive" (Receive x from ch.)
22287            TokenType::Send |            // "send" (Send v into ch.)
22288            TokenType::Spawn |           // "spawn"
22289            TokenType::Await |           // "await" (Await the first of:)
22290            TokenType::Attempt |         // "attempt" (Attempt all of ...)
22291            TokenType::Following |       // "following"
22292            TokenType::Listen |          // "listen" (Listen on "addr".)
22293            TokenType::NetConnect |      // "connect" (Connect to "addr".)
22294            TokenType::Sync |            // "sync" (Sync x on "topic".)
22295            TokenType::Mount |           // "mount" (Mount x at "path".)
22296            TokenType::Shifted |         // "shifted" (x shifted left by n)
22297            TokenType::Union |           // "union" (a union b)
22298            TokenType::Intersection |    // "intersection" (a intersection b)
22299            TokenType::Contains |        // "contains" (xs contains v)
22300            TokenType::Through |         // "through" (items 2 through 4)
22301            TokenType::Into |            // "into" (Send v into ch.)
22302            TokenType::Push |            // "push" (Push v to xs.)
22303            TokenType::Pop |             // "pop" (Pop from xs.)
22304            TokenType::Try |             // "try" (Try to send ...)
22305            TokenType::New |             // "new" (a new T) — in name position the user wins
22306            TokenType::Inspect |         // "inspect" (Inspect x:)
22307            TokenType::Check |           // "check" (Check that ...)
22308            TokenType::Superlative(_) |  // "lowest", "highest", ... (degree morphology)
22309            TokenType::Comparative(_) |  // "lower", "higher", "greater", ... (degree; declarer wins)
22310            TokenType::Performative(_) | // "command", "order", ... (speech-act verbs)
22311            // Escape hatch keyword can be a variable name
22312            TokenType::Escape => {
22313                // Use the raw lexeme (interned string) as the symbol
22314                let sym = token.lexeme;
22315                self.advance();
22316                Ok(sym)
22317            }
22318            TokenType::Ambiguous { .. } => {
22319                // For ambiguous tokens, always use the raw lexeme to preserve original casing
22320                // (using verb lemma can give wrong casing like "State" instead of "state")
22321                let sym = token.lexeme;
22322                self.advance();
22323                Ok(sym)
22324            }
22325            // Universal identifier freedom (LOGOS is imperative: keywords are contextual, never
22326            // reserved). Any REMAINING word-keyword — one whose lexeme is a plain identifier word
22327            // — legally names a variable/parameter/field/function in this name position. Only
22328            // value literals and structural/operator/layout tokens fall through to the error.
22329            // This is the PERMANENT catch-all so a newly added keyword (`at`, `most`, `than`, …)
22330            // can never again silently steal a name; the explicit fast-paths above remain only for
22331            // casing/lemma nuances and readability.
22332            _ if self.token_is_word_identifier(&token) => {
22333                let sym = token.lexeme;
22334                self.advance();
22335                Ok(sym)
22336            }
22337            _ => Err(ParseError {
22338                kind: ParseErrorKind::ExpectedIdentifier,
22339                span: self.current_span(),
22340            }),
22341        }
22342    }
22343
22344    /// Whether `token` is a word-keyword usable as an identifier in a name position — the basis
22345    /// of universal identifier freedom. True iff the token is neither a value literal nor a
22346    /// layout token AND its lexeme is a plain identifier word (leading letter/underscore, then
22347    /// word characters). Operator/punctuation tokens have non-word lexemes and are excluded by
22348    /// the word check itself, so they need no explicit listing.
22349    fn token_is_word_identifier(&self, token: &crate::token::Token) -> bool {
22350        match &token.kind {
22351            // Value literals carry their CONTENT as the lexeme — never an identifier.
22352            TokenType::Number(_)
22353            | TokenType::StringLiteral(_)
22354            | TokenType::InterpolatedString(_)
22355            | TokenType::CharLiteral(_)
22356            // Layout / end tokens.
22357            | TokenType::EOF
22358            | TokenType::Colon
22359            | TokenType::Indent
22360            | TokenType::Dedent
22361            | TokenType::Newline => false,
22362            _ => {
22363                let s = self.interner.resolve(token.lexeme);
22364                let mut chars = s.chars();
22365                matches!(chars.next(), Some(c) if c.is_ascii_alphabetic() || c == '_')
22366                    && s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
22367            }
22368        }
22369    }
22370
22371    fn consume_content_word_for_relative(&mut self) -> ParseResult<Symbol> {
22372        let t = self.advance().clone();
22373        match t.kind {
22374            TokenType::Noun(s) | TokenType::Adjective(s) => Ok(s),
22375            TokenType::ProperName(s) => Ok(s),
22376            TokenType::Verb { lemma, .. } => Ok(lemma),
22377            other => Err(ParseError {
22378                kind: ParseErrorKind::ExpectedContentWord { found: other },
22379                span: self.current_span(),
22380            }),
22381        }
22382    }
22383
22384    fn check_modal(&self) -> bool {
22385        matches!(
22386            self.peek().kind,
22387            TokenType::Must
22388                | TokenType::Shall
22389                | TokenType::Should
22390                | TokenType::Can
22391                | TokenType::May
22392                | TokenType::Cannot
22393                | TokenType::Could
22394                | TokenType::Would
22395                | TokenType::Might
22396        )
22397    }
22398
22399    /// Does an NP head follow the current (possessive) token — i.e. zero or
22400    /// more adjectives ending in a noun? "his dog" / "her red dog" yes;
22401    /// "its wet" (the it's typo) no.
22402    fn possessive_np_head_follows(&self) -> bool {
22403        let mut i = self.current + 1;
22404        while let Some(token) = self.tokens.get(i) {
22405            match &token.kind {
22406                TokenType::Adjective(_) => i += 1,
22407                TokenType::Noun(_) => return true,
22408                // A noun READING suffices ("its value" — "value" lexes
22409                // Ambiguous{Verb|Noun}); the NP parse commits it to noun.
22410                TokenType::Ambiguous {
22411                    primary,
22412                    alternatives,
22413                } => {
22414                    return matches!(**primary, TokenType::Noun(_))
22415                        || alternatives.iter().any(|t| matches!(t, TokenType::Noun(_)));
22416                }
22417                _ => return false,
22418            }
22419        }
22420        false
22421    }
22422
22423    fn check_pronoun(&self) -> bool {
22424        match &self.peek().kind {
22425            TokenType::Pronoun { case, .. } => {
22426                if matches!(case, Case::Possessive) {
22427                    // In noun_priority_mode, possessive pronouns start NPs,
22428                    // not standalone objects.
22429                    if self.noun_priority_mode {
22430                        return false;
22431                    }
22432                    // A possessive followed by an NP head ("his dog",
22433                    // "her red dog") is a possessive NP, never a standalone
22434                    // pronoun. A possessive followed by a bare adjective
22435                    // ("its wet") stays on the pronoun path so the it's-typo
22436                    // grammar error still fires.
22437                    if self.possessive_np_head_follows() {
22438                        return false;
22439                    }
22440                }
22441                true
22442            }
22443            TokenType::Ambiguous {
22444                primary,
22445                alternatives,
22446            } => {
22447                let has_possessive = matches!(
22448                    **primary,
22449                    TokenType::Pronoun {
22450                        case: Case::Possessive,
22451                        ..
22452                    }
22453                ) || alternatives.iter().any(|t| {
22454                    matches!(
22455                        t,
22456                        TokenType::Pronoun {
22457                            case: Case::Possessive,
22458                            ..
22459                        }
22460                    )
22461                });
22462                // In noun_priority_mode, if there's a possessive alternative, prefer noun path
22463                if self.noun_priority_mode && has_possessive {
22464                    return false;
22465                }
22466                // An object/possessive-ambiguous pronoun ("her") stands alone
22467                // only when no NP head follows: "Mary saw her." is a pronoun
22468                // object, "Mary saw her dog." is a possessive NP.
22469                if has_possessive && self.possessive_np_head_follows() {
22470                    return false;
22471                }
22472                matches!(**primary, TokenType::Pronoun { .. })
22473                    || alternatives
22474                        .iter()
22475                        .any(|t| matches!(t, TokenType::Pronoun { .. }))
22476            }
22477            _ => false,
22478        }
22479    }
22480
22481    /// True when the cursor heads a REDUCED-RELATIVE participle — a non-auxiliary
22482    /// verb, optionally behind a single leading ordinal/temporal adverb ("FIRST
22483    /// climbed", "originally seen"). The list-subject gate uses it so a first member
22484    /// carrying a reduced relative ("The peak CLIMBED in 1845, the dog, and Mt. Quinn
22485    /// are three different mountains") routes into the list parser, which consumes
22486    /// the reduced relative per member via `try_consume_reduced_relative`.
22487    fn peek_heads_reduced_relative_participle(&self) -> bool {
22488        let idx = if matches!(
22489            self.peek().kind,
22490            TokenType::Adverb(_) | TokenType::TemporalAdverb(_)
22491        ) && self
22492            .tokens
22493            .get(self.current + 1)
22494            .map_or(false, |t| self.kind_is_verb(&t.kind))
22495        {
22496            self.current + 1
22497        } else {
22498            self.current
22499        };
22500        let Some(t) = self.tokens.get(idx) else {
22501            return false;
22502        };
22503        if !self.kind_is_verb(&t.kind) {
22504            return false;
22505        }
22506        // A perfect/passive/do auxiliary heads a finite matrix VP, not a reduced
22507        // relative ("The apple HAS been eaten").
22508        if let TokenType::Verb { lemma, .. } = &t.kind {
22509            return !matches!(
22510                self.interner.resolve(*lemma).to_lowercase().as_str(),
22511                "have" | "be" | "do"
22512            );
22513        }
22514        true
22515    }
22516
22517    /// True when a comma appears before the end of the current clause (the next
22518    /// Period/EOF). The list-subject gate requires one before speculating on a
22519    /// reduced-relative first member — an enumerated list ("A, B, and C are …")
22520    /// always has a comma, an ordinary main clause does not.
22521    fn has_comma_before_clause_end(&self) -> bool {
22522        for i in self.current..self.tokens.len() {
22523            match &self.tokens[i].kind {
22524                TokenType::Period | TokenType::EOF => return false,
22525                TokenType::Comma => return true,
22526                _ => {}
22527            }
22528        }
22529        false
22530    }
22531
22532    /// Consume a post-nominal REDUCED RELATIVE — an optional leading ordinal/temporal
22533    /// adverb ("first"), a past/-ing participle ("climbed", "seen", "going"), and its
22534    /// PP / measure / directional-"to" complements ("in 1845", "by Captain Norris",
22535    /// "to Pete's") — as restriction predicates over `term`. Returns their conjunction,
22536    /// or None when no participle heads the cursor.
22537    ///
22538    /// `parse_noun_phrase` attaches reduced relatives only when the participle is
22539    /// unambiguous; its conservative, matrix-verb-gated post-subject handler keeps
22540    /// "The team won in 1989" a main clause. In a NOMINAL COORDINATION position — a
22541    /// list member, an either/or disjunct, an of-pair member — the NP cannot be a
22542    /// main-clause subject, so a participle there IS a reduced relative. This helper
22543    /// attaches it so the member's restrictor survives, shared so every coordination
22544    /// site composes once. Callers gate it to those positions (the participle here is
22545    /// committed unconditionally, no matrix-verb lookahead).
22546    fn try_consume_reduced_relative(
22547        &mut self,
22548        term: Term<'a>,
22549    ) -> ParseResult<Option<&'a LogicExpr<'a>>> {
22550        if !self.peek_heads_reduced_relative_participle() {
22551            return Ok(None);
22552        }
22553        let mut preds: Vec<&'a LogicExpr<'a>> = Vec::new();
22554        // Leading ordinal/temporal adverb over the gap ("first climbed" → First(x)).
22555        if matches!(
22556            self.peek().kind,
22557            TokenType::Adverb(_) | TokenType::TemporalAdverb(_)
22558        ) {
22559            let adv = match self.peek().kind {
22560                TokenType::Adverb(s) | TokenType::TemporalAdverb(s) => s,
22561                _ => unreachable!(),
22562            };
22563            self.advance();
22564            preds.push(self.ctx.exprs.alloc(LogicExpr::Predicate {
22565                name: adv,
22566                args: self.ctx.terms.alloc_slice([term]),
22567                world: None,
22568            }));
22569        }
22570        let (red_verb, _t, _a, _c) = self.consume_verb_with_metadata();
22571        preds.push(self.ctx.exprs.alloc(LogicExpr::Predicate {
22572            name: red_verb,
22573            args: self.ctx.terms.alloc_slice([term]),
22574            world: None,
22575        }));
22576        // Complements: PP objects ("from a beetle", "in March"), directional
22577        // "to <NP>" ("going to Pete's"), numeric measures ("in 1845"). Mirrors the
22578        // post-subject reduced-relative handler.
22579        loop {
22580            let prep = if self.check_preposition() {
22581                match self.advance().kind {
22582                    TokenType::Preposition(s) => s,
22583                    _ => break,
22584                }
22585            } else if self.check(&TokenType::To)
22586                && matches!(
22587                    self.tokens.get(self.current + 1).map(|t| &t.kind),
22588                    Some(TokenType::Article(_))
22589                        | Some(TokenType::Noun(_))
22590                        | Some(TokenType::ProperName(_))
22591                )
22592            {
22593                self.advance();
22594                self.interner.intern("To")
22595            } else {
22596                break;
22597            };
22598            if self.check_number() {
22599                let m = self.parse_measure_phrase()?;
22600                preds.push(self.ctx.exprs.alloc(LogicExpr::Predicate {
22601                    name: prep,
22602                    args: self.ctx.terms.alloc_slice([term, *m]),
22603                    world: None,
22604                }));
22605            } else if self.check_article() || self.check_content_word() {
22606                let obj = self.parse_noun_phrase(true)?;
22607                preds.push(self.ctx.exprs.alloc(LogicExpr::Predicate {
22608                    name: prep,
22609                    args: self.ctx.terms.alloc_slice([term, Term::Constant(obj.noun)]),
22610                    world: None,
22611                }));
22612            } else {
22613                break;
22614            }
22615        }
22616        let mut result = preds[0];
22617        for p in &preds[1..] {
22618            result = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
22619                left: result,
22620                op: TokenType::And,
22621                right: p,
22622            });
22623        }
22624        Ok(Some(result))
22625    }
22626
22627    /// Lower ONE list member to a distinct entity. A DESCRIPTION (adjectives /
22628    /// possessor / PPs / relative clause — and, when `bare_definite_distinct`, a
22629    /// bare definite) becomes a fresh ∃-VARIABLE carrying its full restrictor
22630    /// (head + adjectives + possessor + PPs + relative, all over the variable);
22631    /// anything else is a referring CONSTANT. Returns `(term, ∃-var?, restrictor?)`.
22632    ///
22633    /// Shared by both list shapes so members lower IDENTICALLY: the list subject
22634    /// ("A, B and C are different X") needs every definite member distinct for the
22635    /// "different" distinctness (`bare_definite_distinct = true`); the enumerated
22636    /// identity ("The four X are A, B and C") keeps a bare named definite ("the
22637    /// hustle") a constant but still must not collapse a DESCRIBED member ("the
22638    /// person from Spain", "the winner who won") to its head (`= false`).
22639    fn parse_list_member_np(
22640        &mut self,
22641        np: &crate::ast::NounPhrase<'a>,
22642        bare_definite_distinct: bool,
22643    ) -> ParseResult<(Term<'a>, Option<Symbol>, Option<&'a LogicExpr<'a>>)> {
22644        let has_rel = self.check(&TokenType::Who)
22645            || self.check(&TokenType::That)
22646            || self.check(&TokenType::Where)
22647            || self.check(&TokenType::Whose);
22648        let is_desc = has_rel
22649            || (bare_definite_distinct && np.definiteness.is_some())
22650            || !np.adjectives.is_empty()
22651            || np.possessor.is_some()
22652            || !np.pps.is_empty();
22653        if is_desc {
22654            let var = self.next_var_name();
22655            let term = Term::Variable(var);
22656            let mut r = self.nominal_predication(term, np);
22657            if let Some(poss) = np.possessor {
22658                let possesses = self.interner.intern("Possesses");
22659                let pr = self.ctx.exprs.alloc(LogicExpr::Predicate {
22660                    name: possesses,
22661                    args: self
22662                        .ctx
22663                        .terms
22664                        .alloc_slice([Term::Constant(poss.noun), term]),
22665                    world: None,
22666                });
22667                r = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
22668                    left: r,
22669                    op: TokenType::And,
22670                    right: pr,
22671                });
22672            }
22673            for pp in np.pps {
22674                let pp_sub = self.substitute_pp_self_term(pp, term);
22675                r = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
22676                    left: r,
22677                    op: TokenType::And,
22678                    right: pp_sub,
22679                });
22680            }
22681            // who/that/where/whose attach uniformly via the shared dispatcher.
22682            if let Some(rc) = self.try_attach_relative(term)? {
22683                r = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
22684                    left: r,
22685                    op: TokenType::And,
22686                    right: rc,
22687                });
22688            }
22689            // A REDUCED relative ("the peak climbed in 1845", "the island first seen
22690            // by Captain Norris") — a participle a list member can carry but
22691            // parse_noun_phrase's conservative handler leaves stranded.
22692            if let Some(rr) = self.try_consume_reduced_relative(term)? {
22693                r = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
22694                    left: r,
22695                    op: TokenType::And,
22696                    right: rr,
22697                });
22698            }
22699            Ok((term, Some(var), Some(r)))
22700        } else {
22701            Ok((Term::Constant(np.noun), None, None))
22702        }
22703    }
22704
22705    /// Assemble an enumerated identity ("The four X are A, B and C") from an
22706    /// already-built first member. Parses the remaining comma/and-separated members
22707    /// through the shared distinct-entity builder, identifies the plural subject
22708    /// with the group of member terms, conjoins every member restrictor, and binds
22709    /// the described members' ∃ variables around the whole result.
22710    fn finish_enumerated_identity(
22711        &mut self,
22712        subject: &crate::ast::NounPhrase<'a>,
22713        is_negated: bool,
22714        first: (Term<'a>, Option<Symbol>, Option<&'a LogicExpr<'a>>),
22715    ) -> ParseResult<&'a LogicExpr<'a>> {
22716        let mut members: Vec<Term<'a>> = Vec::new();
22717        let mut member_vars: Vec<Symbol> = Vec::new();
22718        let mut member_restrictors: Vec<&'a LogicExpr<'a>> = Vec::new();
22719        let (t0, v0, r0) = first;
22720        members.push(t0);
22721        if let Some(v) = v0 {
22722            member_vars.push(v);
22723        }
22724        if let Some(r) = r0 {
22725            member_restrictors.push(r);
22726        }
22727        while self.check(&TokenType::Comma) || self.check(&TokenType::And) {
22728            // Consume separators ("," / "and" / ", and").
22729            while self.check(&TokenType::Comma) || self.check(&TokenType::And) {
22730                self.advance();
22731            }
22732            if !(self.check_article() || self.check_content_word()) {
22733                break;
22734            }
22735            let item = self.parse_noun_phrase(true)?;
22736            let (t, v, r) = self.parse_list_member_np(&item, false)?;
22737            members.push(t);
22738            if let Some(vv) = v {
22739                member_vars.push(vv);
22740            }
22741            if let Some(rr) = r {
22742                member_restrictors.push(rr);
22743            }
22744        }
22745        let group = Term::Group(self.ctx.terms.alloc_slice(members));
22746        let identity: &'a LogicExpr<'a> = self.ctx.exprs.alloc(LogicExpr::Identity {
22747            left: self.ctx.terms.alloc(Term::Constant(subject.noun)),
22748            right: self.ctx.terms.alloc(group),
22749        });
22750        let mut result = if is_negated {
22751            &*self.ctx.exprs.alloc(LogicExpr::UnaryOp {
22752                op: TokenType::Not,
22753                operand: identity,
22754            })
22755        } else {
22756            identity
22757        };
22758        for rc in member_restrictors {
22759            result = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
22760                left: result,
22761                op: TokenType::And,
22762                right: rc,
22763            });
22764        }
22765        for v in member_vars.into_iter().rev() {
22766            result = self.ctx.exprs.alloc(LogicExpr::Quantifier {
22767                kind: QuantifierKind::Existential,
22768                variable: v,
22769                body: result,
22770                island_id: self.current_island,
22771            });
22772        }
22773        self.wrap_with_definiteness_full(subject, result)
22774    }
22775
22776    /// Comma-coordinated list subject: "A, B and C are reptiles" / "A, B and C
22777    /// are all different animals". Distributes the predicate over each member;
22778    /// "(all) different" yields pairwise distinctness — the AllDifferent
22779    /// constraint the puzzle solver depends on. Returns `None` (restoring) when
22780    /// the comma does not open such a list (e.g. topicalization / apposition).
22781    fn try_parse_list_subject(
22782        &mut self,
22783        first: &crate::ast::NounPhrase<'a>,
22784    ) -> ParseResult<Option<&'a LogicExpr<'a>>> {
22785        let cp = self.checkpoint();
22786        // Each member becomes a DISTINCT entity: a DESCRIPTION (determiner /
22787        // adjectives / possessor / PPs / relative clause) is a fresh ∃-VARIABLE
22788        // carrying its restrictor (head + adjectives + possessor + PPs + rel clause,
22789        // all over the variable); a bare proper name is a referring CONSTANT
22790        // (distinct by unique-name). This makes "three different boxes" a MEANINGFUL
22791        // ¬x=y instead of the vacuous ¬Box=Box a shared head-noun constant gives.
22792        let mut member_terms: Vec<Term<'a>> = Vec::new();
22793        let mut member_vars: Vec<Symbol> = Vec::new();
22794        let mut member_restrictors: Vec<&'a LogicExpr<'a>> = Vec::new();
22795        macro_rules! add_member {
22796            ($np:expr) => {{
22797                let np = $np;
22798                match self.parse_list_member_np(&np, true) {
22799                    Ok((term, var, restrictor)) => {
22800                        member_terms.push(term);
22801                        if let Some(v) = var {
22802                            member_vars.push(v);
22803                        }
22804                        if let Some(r) = restrictor {
22805                            member_restrictors.push(r);
22806                        }
22807                    }
22808                    Err(_) => {
22809                        self.restore(cp);
22810                        return Ok(None);
22811                    }
22812                }
22813            }};
22814        }
22815        add_member!(*first);
22816        // The list gate: a comma OR "and" must follow the first member (and its
22817        // optional relative clause, "The juggler WHO USED BATONS, …"). A bare
22818        // 2-member "A and B verb"/"A and B are happy" with NO relative reaches
22819        // try_parse_plural_subject FIRST (the "and"-directly-after-subject gate),
22820        // so this path only sees DESCRIPTIVE members (a relativizer routed us
22821        // here) — those need the list parser's member restrictors + distinctness.
22822        if !self.check(&TokenType::Comma) && !self.check(&TokenType::And) {
22823            self.restore(cp);
22824            return Ok(None);
22825        }
22826        let mut saw_and = false;
22827        loop {
22828            let mut advanced = false;
22829            if self.check(&TokenType::Comma) {
22830                self.advance();
22831                advanced = true;
22832            }
22833            if self.check(&TokenType::And) {
22834                self.advance();
22835                saw_and = true;
22836                advanced = true;
22837            }
22838            if !advanced {
22839                break;
22840            }
22841            if !(self.check_article() || self.check_content_word()) {
22842                break;
22843            }
22844            let np = match self.parse_noun_phrase(true) {
22845                Ok(np) => np,
22846                Err(_) => {
22847                    self.restore(cp);
22848                    return Ok(None);
22849                }
22850            };
22851            add_member!(np);
22852        }
22853
22854        // A genuine coordination needs the final "and" and ≥2 members. The
22855        // simple 2-member "A and B" (no relative) is try_parse_plural_subject's
22856        // job and never reaches here; a 2-member list that DID reach here has a
22857        // descriptive (relative-clause) member that the plural path can't carry,
22858        // so handle it ("The cat that purrs and the dog that barks are different
22859        // animals").
22860        if !saw_and || member_terms.len() < 2 {
22861            self.restore(cp);
22862            return Ok(None);
22863        }
22864
22865        let copula_past = self.check(&TokenType::Were);
22866        if !(self.check(&TokenType::Are)
22867            || self.check(&TokenType::Were)
22868            || self.check(&TokenType::Is))
22869        {
22870            self.restore(cp);
22871            return Ok(None);
22872        }
22873        self.advance(); // copula
22874
22875        if self.check(&TokenType::All) {
22876            self.advance();
22877        }
22878
22879        // "are THREE different vegetables" — a cardinal that restates the member
22880        // count before "different" is subsumed by the pairwise distinctness, so
22881        // skip it (only when "different" actually follows, to stay conservative).
22882        if matches!(self.peek().kind, TokenType::Cardinal(_))
22883            && self
22884                .tokens
22885                .get(self.current + 1)
22886                .map(|t| {
22887                    self.interner
22888                        .resolve(t.lexeme)
22889                        .eq_ignore_ascii_case("different")
22890                })
22891                .unwrap_or(false)
22892        {
22893            self.advance();
22894        }
22895
22896        // Each member's restrictor (head + adjectives + possessor + PPs + relative
22897        // clause, over its entity term) was built during parsing — start the
22898        // conjunction with them so nothing is dropped.
22899        let mut conjuncts: Vec<&'a LogicExpr<'a>> = member_restrictors;
22900
22901        let is_different = self
22902            .interner
22903            .resolve(self.peek().lexeme)
22904            .eq_ignore_ascii_case("different");
22905        if is_different {
22906            self.advance(); // "different"
22907                            // Optional type noun ("animals"): predicate it of each member entity.
22908            if self.check_content_word() {
22909                let type_noun = self.consume_content_word()?;
22910                for t in &member_terms {
22911                    conjuncts.push(self.ctx.exprs.alloc(LogicExpr::Predicate {
22912                        name: type_noun,
22913                        args: self.ctx.terms.alloc_slice([*t]),
22914                        world: None,
22915                    }));
22916                    // A category DECLARATION ("2001, 2002, 2003, and 2004 are four
22917                    // different years.") records item→category in the shared
22918                    // discourse DRS, so a later definite LABEL ("the 2003 holiday")
22919                    // can recover the category and un-fuse to the same relation the
22920                    // prepositional-phrase form yields. Only a referring CONSTANT
22921                    // member names an item the label can match.
22922                    if let Term::Constant(item) = t {
22923                        self.drs.register_item_category(*item, type_noun);
22924                    }
22925                }
22926            }
22927            // Pairwise distinctness — the AllDifferent constraint over the DISTINCT
22928            // member entities (variables / constants), so it is non-vacuous.
22929            for i in 0..member_terms.len() {
22930                for j in (i + 1)..member_terms.len() {
22931                    let id = self.ctx.exprs.alloc(LogicExpr::Identity {
22932                        left: self.ctx.terms.alloc(member_terms[i]),
22933                        right: self.ctx.terms.alloc(member_terms[j]),
22934                    });
22935                    conjuncts.push(self.ctx.exprs.alloc(LogicExpr::UnaryOp {
22936                        op: TokenType::Not,
22937                        operand: id,
22938                    }));
22939                }
22940            }
22941        } else if self.check_article() || self.check_content_word() {
22942            // Distributive predication: each member satisfies the predicate NP.
22943            let pred_np = self.parse_noun_phrase(true)?;
22944            for t in &member_terms {
22945                conjuncts.push(self.ctx.exprs.alloc(LogicExpr::Predicate {
22946                    name: pred_np.noun,
22947                    args: self.ctx.terms.alloc_slice([*t]),
22948                    world: None,
22949                }));
22950            }
22951        } else {
22952            self.restore(cp);
22953            return Ok(None);
22954        }
22955
22956        if conjuncts.is_empty() {
22957            self.restore(cp);
22958            return Ok(None);
22959        }
22960        let mut result = conjuncts[0];
22961        for c in &conjuncts[1..] {
22962            result = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
22963                left: result,
22964                op: TokenType::And,
22965                right: c,
22966            });
22967        }
22968        if copula_past {
22969            result = self.ctx.exprs.alloc(LogicExpr::Temporal {
22970                operator: TemporalOperator::Past,
22971                body: result,
22972            });
22973        }
22974        // Bind each descriptive member's existential variable around the whole
22975        // conjunction (∃x∃y∃z(restrictors ∧ types ∧ ¬x=y ∧ …)).
22976        for &var in member_vars.iter().rev() {
22977            result = self.ctx.exprs.alloc(LogicExpr::Quantifier {
22978                kind: QuantifierKind::Existential,
22979                variable: var,
22980                body: result,
22981                island_id: self.current_island,
22982            });
22983        }
22984        Ok(Some(result))
22985    }
22986
22987    /// Parse a logic atom. Depth-guarded wrapper: `parse_atom` is the re-entry
22988    /// point of the LOGIC descent — a parenthesized sentence `( … )` recurses back
22989    /// through here once per level (`parse_atom` → `parse_sentence` → … →
22990    /// `parse_atom`), so a parenthesis tower becomes a graceful `AstTooDeep`
22991    /// instead of a parser stack overflow. Mirrors `parse_primary_expr` on the
22992    /// imperative side; both share the `recursion_depth` counter and the
22993    /// `max_parse_recursion` limit.
22994    fn parse_atom(&mut self) -> ParseResult<&'a LogicExpr<'a>> {
22995        self.enter_recursion()?;
22996        let result = self.parse_atom_inner();
22997        self.leave_recursion();
22998        result
22999    }
23000
23001    fn parse_atom_inner(&mut self) -> ParseResult<&'a LogicExpr<'a>> {
23002        // A fresh clause starts with no stashed subject restriction — guard
23003        // against a prior clause leaking one that its copula branch never
23004        // consumed.
23005        self.pending_subject_restriction = None;
23006
23007        // Handle Focus particles: "Only John loves Mary", "Even John ran"
23008        if self.check_focus() {
23009            return self.parse_focus();
23010        }
23011
23012        // Handle mass noun measure: "Much water flows", "Little time remains"
23013        if self.check_measure() {
23014            return self.parse_measure();
23015        }
23016
23017        // Gerund subject ("Flying planes can be dangerous."): a sentence-initial
23018        // progressive verb + NP names an ACTIVITY; the matrix predicate applies
23019        // to that activity as a proposition. Gated on the full shape
23020        // (gerund + noun + modal/copula) so imperatives are untouched.
23021        if let TokenType::Verb {
23022            lemma,
23023            aspect: Aspect::Progressive,
23024            time: Time::None,
23025            ..
23026        } = self.peek().kind
23027        {
23028            let noun_next = matches!(
23029                self.tokens.get(self.current + 1).map(|t| t.kind.clone()),
23030                Some(TokenType::Noun(_))
23031            );
23032            let finite_after = matches!(
23033                self.tokens.get(self.current + 2).map(|t| t.kind.clone()),
23034                Some(
23035                    TokenType::Can
23036                        | TokenType::Could
23037                        | TokenType::May
23038                        | TokenType::Must
23039                        | TokenType::Should
23040                        | TokenType::Would
23041                        | TokenType::Is
23042                        | TokenType::Are
23043                )
23044            );
23045            if noun_next && finite_after {
23046                self.advance(); // gerund
23047                let noun_sym = match self.advance().kind {
23048                    TokenType::Noun(n) => n,
23049                    _ => unreachable!("guarded by noun_next"),
23050                };
23051
23052                let event_var = self.get_event_var();
23053                let activity = self
23054                    .ctx
23055                    .exprs
23056                    .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
23057                        event_var,
23058                        verb: lemma,
23059                        roles: self
23060                            .ctx
23061                            .roles
23062                            .alloc_slice(vec![(ThematicRole::Theme, Term::Constant(noun_sym))]),
23063                        modifiers: self.ctx.syms.alloc_slice(vec![]),
23064                        suppress_existential: false,
23065                        world: None,
23066                    })));
23067
23068                let modal_tok = if matches!(
23069                    self.peek().kind,
23070                    TokenType::Can
23071                        | TokenType::Could
23072                        | TokenType::May
23073                        | TokenType::Must
23074                        | TokenType::Should
23075                        | TokenType::Would
23076                ) {
23077                    Some(self.advance().kind.clone())
23078                } else {
23079                    None
23080                };
23081                // Copula: bare "be" under a modal, or finite "is"/"are".
23082                if matches!(
23083                    self.peek().kind,
23084                    TokenType::Is | TokenType::Are | TokenType::Be
23085                ) {
23086                    self.advance();
23087                } else if matches!(self.peek().kind, TokenType::Verb { .. })
23088                    && self
23089                        .interner
23090                        .resolve(self.peek().lexeme)
23091                        .eq_ignore_ascii_case("be")
23092                {
23093                    self.advance();
23094                }
23095
23096                if let TokenType::Adjective(adj) = self.peek().kind {
23097                    self.advance();
23098                    let pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
23099                        name: adj,
23100                        args: self.ctx.terms.alloc_slice([Term::Proposition(activity)]),
23101                        world: None,
23102                    });
23103                    let result = match modal_tok {
23104                        Some(tok) => {
23105                            let force = match tok {
23106                                TokenType::Must => 1.0,
23107                                TokenType::Should | TokenType::Would => 0.6,
23108                                TokenType::Could => 0.4,
23109                                _ => 0.5,
23110                            };
23111                            &*self.ctx.exprs.alloc(LogicExpr::Modal {
23112                                vector: crate::ast::ModalVector::new(
23113                                    crate::ast::ModalDomain::Alethic,
23114                                    force,
23115                                    crate::ast::ModalFlavor::Root,
23116                                ),
23117                                operand: pred,
23118                            })
23119                        }
23120                        None => pred,
23121                    };
23122                    return Ok(result);
23123                }
23124                return Err(ParseError {
23125                    kind: ParseErrorKind::ExpectedContentWord {
23126                        found: self.peek().kind.clone(),
23127                    },
23128                    span: self.current_span(),
23129                });
23130            }
23131        }
23132
23133        if self.check_quantifier() {
23134            self.advance();
23135            return self.parse_quantified();
23136        }
23137
23138        if self.check_npi_quantifier() {
23139            return self.parse_npi_quantified();
23140        }
23141
23142        if self.check_temporal_npi() {
23143            return self.parse_temporal_npi();
23144        }
23145
23146        if self.match_token(&[TokenType::LParen]) {
23147            let expr = self.parse_sentence()?;
23148            self.consume(TokenType::RParen)?;
23149            return Ok(expr);
23150        }
23151
23152        // Handle pronoun as subject
23153        if self.check_pronoun() {
23154            let token = self.advance().clone();
23155            let (gender, number) = match &token.kind {
23156                TokenType::Pronoun { gender, number, .. } => (*gender, *number),
23157                TokenType::Ambiguous {
23158                    primary,
23159                    alternatives,
23160                } => {
23161                    if let TokenType::Pronoun { gender, number, .. } = **primary {
23162                        (gender, number)
23163                    } else {
23164                        alternatives
23165                            .iter()
23166                            .find_map(|t| {
23167                                if let TokenType::Pronoun { gender, number, .. } = t {
23168                                    Some((*gender, *number))
23169                                } else {
23170                                    None
23171                                }
23172                            })
23173                            .unwrap_or((Gender::Unknown, Number::Singular))
23174                    }
23175                }
23176                _ => (Gender::Unknown, Number::Singular),
23177            };
23178
23179            let token_text = self.interner.resolve(token.lexeme);
23180
23181            // Weather verb + expletive "it" detection: "it rains" → ∃e(Rain(e))
23182            // Must check BEFORE pronoun resolution since "it" resolves to "?"
23183            if token_text.eq_ignore_ascii_case("it") && self.check_verb() {
23184                if let TokenType::Verb { lemma, time, .. } = &self.peek().kind {
23185                    let lemma_str = self.interner.resolve(*lemma);
23186                    if Lexer::is_weather_verb(lemma_str) {
23187                        let verb = *lemma;
23188                        let verb_time = *time;
23189                        self.advance(); // consume the weather verb
23190
23191                        let event_var = self.get_event_var();
23192                        let suppress_existential = self.drs.in_conditional_antecedent();
23193                        if suppress_existential {
23194                            let event_class = self.interner.intern("Event");
23195                            self.drs.introduce_event_referent(event_var, event_class);
23196                        }
23197                        let neo_event =
23198                            self.ctx
23199                                .exprs
23200                                .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
23201                                    event_var,
23202                                    verb,
23203                                    roles: self.ctx.roles.alloc_slice(vec![]), // No thematic roles
23204                                    modifiers: self.ctx.syms.alloc_slice(vec![]),
23205                                    suppress_existential,
23206                                    world: None,
23207                                })));
23208
23209                        return Ok(match verb_time {
23210                            Time::Past => self.ctx.exprs.alloc(LogicExpr::Temporal {
23211                                operator: TemporalOperator::Past,
23212                                body: neo_event,
23213                            }),
23214                            Time::Future => self.ctx.exprs.alloc(LogicExpr::Temporal {
23215                                operator: TemporalOperator::Future,
23216                                body: neo_event,
23217                            }),
23218                            _ => neo_event,
23219                        });
23220                    }
23221                }
23222            }
23223
23224            // Handle deictic pronouns that don't need discourse resolution
23225            let resolved = if token_text.eq_ignore_ascii_case("i") {
23226                ResolvedPronoun::Constant(self.interner.intern("Speaker"))
23227            } else if token_text.eq_ignore_ascii_case("you") {
23228                ResolvedPronoun::Constant(self.interner.intern("Addressee"))
23229            } else {
23230                // Try discourse resolution for anaphoric pronouns
23231                self.resolve_pronoun(gender, number)?
23232            };
23233
23234            // Skip the performativity marker "hereby": "I hereby resign."
23235            if self
23236                .interner
23237                .resolve(self.peek().lexeme)
23238                .eq_ignore_ascii_case("hereby")
23239            {
23240                self.advance();
23241            }
23242
23243            // Check for performative: "I (hereby) promise to/that ...", "I hereby resign."
23244            if self.check_performative() {
23245                if let TokenType::Performative(act) = self.advance().kind.clone() {
23246                    let sym = match resolved {
23247                        ResolvedPronoun::Variable(s) | ResolvedPronoun::Constant(s) => s,
23248                    };
23249
23250                    // Infinitive complement: "I promise to call you." The complement
23251                    // verb may be noun-tagged ("call"), so accept any content word and
23252                    // capture its object so the hearer survives (P3 structured content).
23253                    if self.check(&TokenType::To) {
23254                        self.advance(); // consume "to"
23255
23256                        if !self.is_at_end() && !self.check(&TokenType::Period) {
23257                            // The complement verb may be lexed as a reserved keyword
23258                            // (e.g. "call" → TokenType::Call), so take the lexeme
23259                            // directly as the predicate name rather than requiring a
23260                            // content-word token.
23261                            let lexeme = self.interner.resolve(self.peek().lexeme).to_string();
23262                            let verb_name = lexeme
23263                                .chars()
23264                                .next()
23265                                .map(|c| c.to_uppercase().collect::<String>() + &lexeme[1..])
23266                                .unwrap_or(lexeme);
23267                            let infinitive_verb = self.interner.intern(&verb_name);
23268                            self.advance(); // consume the complement verb
23269                            let content = self.performative_predicate(infinitive_verb, sym);
23270                            return Ok(self.ctx.exprs.alloc(LogicExpr::SpeechAct {
23271                                performer: sym,
23272                                act_type: act,
23273                                content,
23274                            }));
23275                        }
23276                    }
23277
23278                    // Clausal complement: "I declare that S."
23279                    if self.check(&TokenType::That) {
23280                        self.advance();
23281                        let content = self.parse_sentence()?;
23282                        return Ok(self.ctx.exprs.alloc(LogicExpr::SpeechAct {
23283                            performer: sym,
23284                            act_type: act,
23285                            content,
23286                        }));
23287                    }
23288
23289                    // Bare performative — the saying is the doing — but ONLY when the
23290                    // remainder is empty ("I hereby resign.") or a lone object NP
23291                    // ("I thank you."). Object-control performatives that take a
23292                    // further complement ("I order you to leave.") fall through to the
23293                    // standard clausal parse below so the complement is not truncated.
23294                    let at_end = self.check(&TokenType::Period) || self.is_at_end();
23295                    let lone_object_then_end = matches!(
23296                        self.peek().kind,
23297                        TokenType::Pronoun { .. } | TokenType::ProperName(_)
23298                    ) && self.current + 1 < self.tokens.len()
23299                        && matches!(
23300                            self.tokens[self.current + 1].kind,
23301                            TokenType::Period | TokenType::EOF
23302                        );
23303                    if at_end || lone_object_then_end {
23304                        let content = self.performative_predicate(act, sym);
23305                        return Ok(self.ctx.exprs.alloc(LogicExpr::SpeechAct {
23306                            performer: sym,
23307                            act_type: act,
23308                            content,
23309                        }));
23310                    }
23311
23312                    // Fallback (object-control performatives, residual material):
23313                    // preserve the prior behavior of parsing the remainder as content.
23314                    let content = self.parse_sentence()?;
23315                    return Ok(self.ctx.exprs.alloc(LogicExpr::SpeechAct {
23316                        performer: sym,
23317                        act_type: act,
23318                        content,
23319                    }));
23320                }
23321            }
23322
23323            // Continue parsing verb phrase with resolved subject
23324            // Use as_var=true for bound variables, as_var=false for constants
23325            return match resolved {
23326                ResolvedPronoun::Variable(sym) => self.parse_predicate_with_subject_as_var(sym),
23327                ResolvedPronoun::Constant(sym) => self.parse_predicate_with_subject(sym),
23328            };
23329        }
23330
23331        // Consume "both" correlative marker if present: "both X and Y"
23332        // The existing try_parse_plural_subject will handle the "X and Y" pattern
23333        let _had_both = self.match_token(&[TokenType::Both]);
23334
23335        let mut subject = self.parse_noun_phrase(true)?;
23336
23337        // Floating (stranded) quantifier: "The boys all left.", "The students each
23338        // solved a problem.", "The boys both ran." A quantifier stranded after the
23339        // subject re-associates to it and distributes the predicate over its members.
23340        // Consume it so the (plural definite) subject's distributive reading applies.
23341        if self.check(&TokenType::All)
23342            || self.check(&TokenType::Both)
23343            || self
23344                .interner
23345                .resolve(self.peek().lexeme)
23346                .eq_ignore_ascii_case("each")
23347        {
23348            self.advance();
23349            self.collective_mode = false; // force the distributive (per-member) reading
23350            self.distributive_marker = true;
23351        }
23352
23353        // Introduce subject NP to DRS for cross-sentence pronoun resolution (accommodation)
23354        // This allows "A man walked. He fell." to work
23355        // Use noun as both variable and noun_class (like proper names) so pronouns resolve to it
23356        // NOTE: Definite NPs are NOT introduced here - they go through wrap_with_definiteness
23357        // where bridging anaphora can link them to prior wholes (e.g., "I bought a car. The engine smoked.")
23358        if subject.definiteness == Some(Definiteness::Indefinite)
23359            || subject.definiteness == Some(Definiteness::Distal)
23360        {
23361            let gender = Self::infer_noun_gender(self.interner.resolve(subject.noun));
23362            let number = if Self::is_plural_noun(self.interner.resolve(subject.noun)) {
23363                Number::Plural
23364            } else {
23365                Number::Singular
23366            };
23367            // Use noun as variable so pronoun resolution returns the noun name
23368            self.drs
23369                .introduce_referent(subject.noun, subject.noun, gender, number);
23370        }
23371
23372        // Handle plural subjects: "John and Mary verb"
23373        if self.check(&TokenType::And) {
23374            match self.try_parse_plural_subject(&subject) {
23375                Ok(Some(result)) => return Ok(result),
23376                Ok(None) => {}           // Not a plural subject, continue
23377                Err(e) => return Err(e), // Semantic error (e.g., respectively mismatch)
23378            }
23379        }
23380
23381        // Handle scopal adverbs: "John almost died"
23382        if self.check_scopal_adverb() {
23383            return self.parse_scopal_adverb(&subject);
23384        }
23385
23386        // Non-restrictive (appositive) RC (§3.1): "John, who loves Mary, left." — a
23387        // comma-delimited RC on a referential head is a SIDE-ASSERTION about the
23388        // head referent, conjoined with the main clause, NOT a restriction.
23389        if self.check(&TokenType::Comma)
23390            && matches!(
23391                self.tokens.get(self.current + 1).map(|t| &t.kind),
23392                Some(TokenType::Who) | Some(TokenType::That)
23393            )
23394        {
23395            self.advance(); // comma
23396            self.advance(); // who / which / that
23397                            // The RC predication, with the head referent filling the gap.
23398            let rc = self.parse_relative_clause(subject.noun)?;
23399            if self.check(&TokenType::Comma) {
23400                self.advance(); // closing comma
23401            }
23402            // The main clause predicated of the same head.
23403            let main = self.parse_predicate_with_subject(subject.noun)?;
23404            let conj = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23405                left: main,
23406                op: TokenType::And,
23407                right: rc,
23408            });
23409            return self.wrap_with_definiteness_full(&subject, conj);
23410        }
23411
23412        // Comma-coordinated list subject: "A, B and C are all different animals."
23413        // Also fire when a relativizer follows the first member ("The scent THAT
23414        // sold for $75, the white fragrance, and the purple perfume are three
23415        // different perfumes") — the list parser consumes the first member's
23416        // relative clause before the comma gate. It restores cleanly when this is
23417        // an ordinary relative-clause subject, so the normal handler still runs.
23418        if self.check(&TokenType::Comma)
23419            || self.check(&TokenType::Who)
23420            || self.check(&TokenType::That)
23421            // "and" too: try_parse_plural_subject (called above) handles the simple
23422            // "A and B verb"/"A and B are happy" and returned None here only when a
23423            // member it can't carry follows (a relative clause on the SECOND member:
23424            // "Mr. Gray's timepiece and the timepiece THAT sold for $1,800 were
23425            // different clocks"). The list parser carries descriptive members; it
23426            // restores cleanly if this is not a coordinated list.
23427            || self.check(&TokenType::And)
23428            // A reduced-relative participle on the first member ("The peak CLIMBED
23429            // in 1845, …, and X are three different mountains") — the bare NP stops
23430            // at the head, so the comma is hidden behind the participle; route in so
23431            // the list parser consumes the reduced relative per member. An enumerated
23432            // list needs a comma before the clause end, so gate on one: a plain main
23433            // clause ("The team won in 1989") then never triggers the speculation.
23434            || (self.peek_heads_reduced_relative_participle() && self.has_comma_before_clause_end())
23435        {
23436            if let Some(result) = self.try_parse_list_subject(&subject)? {
23437                return Ok(result);
23438            }
23439        }
23440
23441        // Handle topicalization: "The cake, John ate." - first NP is object, not subject
23442        if self.check(&TokenType::Comma) {
23443            let saved_pos = self.backtrack_point();
23444            self.advance(); // consume comma
23445
23446            // Check if followed by pronoun subject (e.g., "The book, he read.")
23447            if self.check_pronoun() {
23448                let topic_attempt = self.try_parse(|p| {
23449                    let token = p.peek().clone();
23450                    let pronoun_features = match &token.kind {
23451                        TokenType::Pronoun { gender, number, .. } => Some((*gender, *number)),
23452                        TokenType::Ambiguous {
23453                            primary,
23454                            alternatives,
23455                        } => {
23456                            if let TokenType::Pronoun { gender, number, .. } = **primary {
23457                                Some((gender, number))
23458                            } else {
23459                                alternatives.iter().find_map(|t| {
23460                                    if let TokenType::Pronoun { gender, number, .. } = t {
23461                                        Some((*gender, *number))
23462                                    } else {
23463                                        None
23464                                    }
23465                                })
23466                            }
23467                        }
23468                        _ => None,
23469                    };
23470
23471                    if let Some((gender, number)) = pronoun_features {
23472                        p.advance(); // consume pronoun
23473                        let resolved = p.resolve_pronoun(gender, number)?;
23474                        let resolved_term = match resolved {
23475                            ResolvedPronoun::Variable(s) => Term::Variable(s),
23476                            ResolvedPronoun::Constant(s) => Term::Constant(s),
23477                        };
23478
23479                        if p.check_verb() {
23480                            let verb = p.consume_verb();
23481                            let predicate = p.ctx.exprs.alloc(LogicExpr::Predicate {
23482                                name: verb,
23483                                args: p
23484                                    .ctx
23485                                    .terms
23486                                    .alloc_slice([resolved_term, Term::Constant(subject.noun)]),
23487                                world: None,
23488                            });
23489                            p.wrap_with_definiteness_full(&subject, predicate)
23490                        } else {
23491                            Err(ParseError {
23492                                kind: ParseErrorKind::ExpectedVerb {
23493                                    found: p.peek().kind.clone(),
23494                                },
23495                                span: p.current_span(),
23496                            })
23497                        }
23498                    } else {
23499                        Err(ParseError {
23500                            kind: ParseErrorKind::ExpectedContentWord { found: token.kind },
23501                            span: p.current_span(),
23502                        })
23503                    }
23504                });
23505
23506                if let Some(result) = topic_attempt {
23507                    return Ok(result);
23508                }
23509            }
23510
23511            // Check if followed by another NP and then a verb (topicalization pattern)
23512            if self.check_content_word() {
23513                let topic_attempt = self.try_parse(|p| {
23514                    let real_subject = p.parse_noun_phrase(true)?;
23515                    if p.check_verb() {
23516                        let verb = p.consume_verb();
23517                        let predicate = p.ctx.exprs.alloc(LogicExpr::Predicate {
23518                            name: verb,
23519                            args: p.ctx.terms.alloc_slice([
23520                                Term::Constant(real_subject.noun),
23521                                Term::Constant(subject.noun),
23522                            ]),
23523                            world: None,
23524                        });
23525                        p.wrap_with_definiteness_full(&subject, predicate)
23526                    } else {
23527                        Err(ParseError {
23528                            kind: ParseErrorKind::ExpectedVerb {
23529                                found: p.peek().kind.clone(),
23530                            },
23531                            span: p.current_span(),
23532                        })
23533                    }
23534                });
23535
23536                if let Some(result) = topic_attempt {
23537                    return Ok(result);
23538                }
23539            }
23540
23541            // Restore position if topicalization didn't match
23542            self.backtrack_to(&saved_pos);
23543        }
23544
23545        // Handle relative clause after subject: "The cat that the dog chased ran."
23546        let mut relative_clause: Option<(Symbol, &'a LogicExpr<'a>)> = None;
23547        if self.check(&TokenType::That) || self.check(&TokenType::Who) {
23548            self.advance();
23549            let var_name = self.next_var_name();
23550            let rel_pred = self.parse_relative_clause(var_name)?;
23551            relative_clause = Some((var_name, rel_pred));
23552        } else if self.check(&TokenType::Where) {
23553            // Locative/circumstance relative: "the episode WHERE the survivor brought
23554            // the knife", "the trip WHERE they saw 16 stars". Unlike who/that (an
23555            // argument gap), the clause has its OWN subject and the head is the
23556            // LOCATION/occasion of the event — so parse the embedded subject + its full
23557            // VP and conjoin In(event, head) over the gap. Without this the clue
23558            // stranded at "where" (TrailingTokens).
23559            let var_name = self.next_var_name();
23560            let rel_pred = self.parse_where_relative(var_name)?;
23561            relative_clause = Some((var_name, rel_pred));
23562        } else if matches!(self.peek().kind, TokenType::Article(_))
23563            && self.is_contact_clause_pattern()
23564        {
23565            // Contact clause (reduced relative): "The cat the dog chased ran."
23566            // NP + NP + Verb pattern indicates embedded relative without explicit "that"
23567            let var_name = self.next_var_name();
23568            let rel_pred = self.parse_relative_clause(var_name)?;
23569            relative_clause = Some((var_name, rel_pred));
23570        }
23571
23572        // Handle main verb after relative clause: "The cat that the dog chased ran."
23573        if let Some((var_name, rel_clause)) = relative_clause {
23574            // Aspectual / presupposition-trigger matrix over a relativized subject
23575            // ("the person WHO WON STARTED skydiving 2 years after Leslie") — the
23576            // SAME presupposition grammar as a bare subject, via the term-parametric
23577            // form, so the feature composes with relative-clause subjects.
23578            if self.check_presup_trigger()
23579                && !self.is_followed_by_np_object()
23580                && self.is_followed_by_gerund()
23581            {
23582                let presup_kind = self.consume_presup_trigger();
23583                let main_pred = self.parse_presupposition_for_term(
23584                    Term::Variable(var_name),
23585                    presup_kind,
23586                    false,
23587                )?;
23588                let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
23589                    name: subject.noun,
23590                    args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
23591                    world: None,
23592                });
23593                let inner = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23594                    left: type_pred,
23595                    op: TokenType::And,
23596                    right: rel_clause,
23597                });
23598                let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23599                    left: inner,
23600                    op: TokenType::And,
23601                    right: main_pred,
23602                });
23603                return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
23604                    kind: QuantifierKind::Existential,
23605                    variable: var_name,
23606                    body,
23607                    island_id: self.current_island,
23608                }));
23609            }
23610            if self.check_verb() {
23611                let (verb, verb_time, _, _) = self.consume_verb_with_metadata();
23612                let var_term = Term::Variable(var_name);
23613
23614                // A bare verb keeps the simple event; material after it
23615                // ("The farmer who saw a cat CHASED IT.") takes the full VP
23616                // grammar over the relativized variable.
23617                let main_pred = if self.at_clause_boundary() {
23618                    let event_var = self.get_event_var();
23619                    let suppress_existential = self.drs.in_conditional_antecedent();
23620                    let mut modifiers = vec![];
23621                    if verb_time == Time::Past {
23622                        modifiers.push(self.interner.intern("Past"));
23623                    }
23624                    &*self
23625                        .ctx
23626                        .exprs
23627                        .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
23628                            event_var,
23629                            verb,
23630                            roles: self
23631                                .ctx
23632                                .roles
23633                                .alloc_slice(vec![(ThematicRole::Agent, var_term)]),
23634                            modifiers: self.ctx.syms.alloc_slice(modifiers),
23635                            suppress_existential,
23636                            world: None,
23637                        })))
23638                } else {
23639                    self.current -= 1; // hand the verb back to the VP parser
23640                    self.parse_predicate_with_subject_as_var(var_name)?
23641                };
23642
23643                let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
23644                    name: subject.noun,
23645                    args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
23646                    world: None,
23647                });
23648
23649                let inner = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23650                    left: type_pred,
23651                    op: TokenType::And,
23652                    right: rel_clause,
23653                });
23654
23655                let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23656                    left: inner,
23657                    op: TokenType::And,
23658                    right: main_pred,
23659                });
23660
23661                return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
23662                    kind: QuantifierKind::Existential,
23663                    variable: var_name,
23664                    body,
23665                    island_id: self.current_island,
23666                }));
23667            }
23668
23669            // No main verb - just the relative clause: "The cat that runs" as a complete NP
23670            // Build: ∃x(Cat(x) ∧ Runs(x) ∧ ∀y(Cat(y) → y=x))
23671            if self.is_at_end() || self.check(&TokenType::Period) || self.check(&TokenType::Comma) {
23672                let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
23673                    name: subject.noun,
23674                    args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
23675                    world: None,
23676                });
23677
23678                let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23679                    left: type_pred,
23680                    op: TokenType::And,
23681                    right: rel_clause,
23682                });
23683
23684                // Add uniqueness for definite description
23685                let uniqueness_body = if subject.definiteness == Some(Definiteness::Definite) {
23686                    let y_var = self.next_var_name();
23687                    let type_pred_y = self.ctx.exprs.alloc(LogicExpr::Predicate {
23688                        name: subject.noun,
23689                        args: self.ctx.terms.alloc_slice([Term::Variable(y_var)]),
23690                        world: None,
23691                    });
23692                    let identity = self.ctx.exprs.alloc(LogicExpr::Identity {
23693                        left: self.ctx.terms.alloc(Term::Variable(y_var)),
23694                        right: self.ctx.terms.alloc(Term::Variable(var_name)),
23695                    });
23696                    let uniqueness_cond = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23697                        left: type_pred_y,
23698                        op: TokenType::Implies,
23699                        right: identity,
23700                    });
23701                    let uniqueness = self.ctx.exprs.alloc(LogicExpr::Quantifier {
23702                        kind: QuantifierKind::Universal,
23703                        variable: y_var,
23704                        body: uniqueness_cond,
23705                        island_id: self.current_island,
23706                    });
23707                    self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23708                        left: body,
23709                        op: TokenType::And,
23710                        right: uniqueness,
23711                    })
23712                } else {
23713                    body
23714                };
23715
23716                return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
23717                    kind: QuantifierKind::Existential,
23718                    variable: var_name,
23719                    body: uniqueness_body,
23720                    island_id: self.current_island,
23721                }));
23722            }
23723
23724            // Re-store for copula handling below
23725            relative_clause = Some((var_name, rel_clause));
23726        }
23727
23728        // Identity check: "Clark is equal to Superman"
23729        if self.check(&TokenType::Identity) {
23730            self.advance();
23731            let right = self.consume_content_word()?;
23732            return Ok(self.ctx.exprs.alloc(LogicExpr::Identity {
23733                left: self.ctx.terms.alloc(Term::Constant(subject.noun)),
23734                right: self.ctx.terms.alloc(Term::Constant(right)),
23735            }));
23736        }
23737
23738        if self.check_modal() {
23739            if let Some((var_name, rel_clause)) = relative_clause {
23740                let modal_pred = self.parse_aspect_chain_with_term(Term::Variable(var_name))?;
23741
23742                let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
23743                    name: subject.noun,
23744                    args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
23745                    world: None,
23746                });
23747
23748                let inner = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23749                    left: type_pred,
23750                    op: TokenType::And,
23751                    right: rel_clause,
23752                });
23753
23754                let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23755                    left: inner,
23756                    op: TokenType::And,
23757                    right: modal_pred,
23758                });
23759
23760                return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
23761                    kind: QuantifierKind::Existential,
23762                    variable: var_name,
23763                    body,
23764                    island_id: self.current_island,
23765                }));
23766            }
23767
23768            let modal_pred = self.parse_aspect_chain(subject.noun)?;
23769            return self.wrap_with_definiteness_full(&subject, modal_pred);
23770        }
23771
23772        // Reduced relative clause on the subject before a copular matrix
23773        // predicate: "The drug sourced from a beetle is X.", "The mushroom going
23774        // to Pete's is Y.", "The raptor belonging to Ivan is trained by Z." A
23775        // participle directly after the subject — followed, once its complements
23776        // are consumed, by the matrix copula — restricts the subject (it is NOT
23777        // the matrix verb). Parse it as restriction predicates over the subject
23778        // and fold them into the subject's PPs, so the existing copular path then
23779        // predicates the rest. Backtracking: commit only if a copula truly
23780        // follows, so a finite main verb ("The drug treats X") is untouched.
23781        // An auxiliary (have/be/do) heads a perfect/passive/progressive matrix
23782        // ("The apple has been eaten."), NOT a reduced relative — never treat it
23783        // as the participle.
23784        // A reduced relative may carry a leading ordinal/temporal adverb ("The peak
23785        // FIRST climbed in 1845 is tall.", "The island FIRST seen by Captain Norris
23786        // is small."). The adverb hides the participle from the verb check below; look
23787        // one token past a single leading adverb to find the participle that heads the
23788        // relative, and consume it inside the speculative parse so the token survives
23789        // as a modifier on the subject.
23790        let leading_adverb = if matches!(
23791            self.peek().kind,
23792            TokenType::Adverb(_) | TokenType::TemporalAdverb(_)
23793        ) && self
23794            .tokens
23795            .get(self.current + 1)
23796            .map_or(false, |t| self.kind_is_verb(&t.kind))
23797        {
23798            match self.peek().kind {
23799                TokenType::Adverb(s) | TokenType::TemporalAdverb(s) => Some(s),
23800                _ => None,
23801            }
23802        } else {
23803            None
23804        };
23805        let head_verb_idx = if leading_adverb.is_some() {
23806            self.current + 1
23807        } else {
23808            self.current
23809        };
23810        let head_verb_is_aux = if let Some(TokenType::Verb { lemma, .. }) =
23811            self.tokens.get(head_verb_idx).map(|t| &t.kind)
23812        {
23813            matches!(
23814                self.interner.resolve(*lemma).to_lowercase().as_str(),
23815                "have" | "be" | "do"
23816            )
23817        } else {
23818            false
23819        };
23820        let heads_reduced_rel = self
23821            .tokens
23822            .get(head_verb_idx)
23823            .map_or(false, |t| self.kind_is_verb(&t.kind));
23824        if subject.definiteness.is_some()
23825            && heads_reduced_rel
23826            && self.pending_time.is_none()
23827            && !head_verb_is_aux
23828        {
23829            if let Some(restrs) = self.try_parse(|p| {
23830                let placeholder = p.interner.intern("_PP_SELF_");
23831                let mut preds: Vec<&'a LogicExpr<'a>> = Vec::new();
23832                if let Some(adv) = leading_adverb {
23833                    p.advance();
23834                    preds.push(p.ctx.exprs.alloc(LogicExpr::Predicate {
23835                        name: adv,
23836                        args: p.ctx.terms.alloc_slice([Term::Variable(placeholder)]),
23837                        world: None,
23838                    }));
23839                }
23840                let (red_verb, _t, _a, _c) = p.consume_verb_with_metadata();
23841                preds.push(p.ctx.exprs.alloc(LogicExpr::Predicate {
23842                    name: red_verb,
23843                    args: p.ctx.terms.alloc_slice([Term::Variable(placeholder)]),
23844                    world: None,
23845                }));
23846                // Complements: PP objects ("from a beetle", "in March"),
23847                // directional "to <NP>" ("going to Pete's"), numeric measures.
23848                loop {
23849                    let prep = if p.check_preposition() {
23850                        match p.advance().kind {
23851                            TokenType::Preposition(s) => s,
23852                            _ => break,
23853                        }
23854                    } else if p.check(&TokenType::To)
23855                        && matches!(
23856                            p.tokens.get(p.current + 1).map(|t| &t.kind),
23857                            Some(TokenType::Article(_))
23858                                | Some(TokenType::Noun(_))
23859                                | Some(TokenType::ProperName(_))
23860                        )
23861                    {
23862                        p.advance(); // "to" (directional, NP object follows)
23863                        p.interner.intern("To")
23864                    } else {
23865                        break;
23866                    };
23867                    if p.check_number() {
23868                        let m = p.parse_measure_phrase()?;
23869                        preds.push(p.ctx.exprs.alloc(LogicExpr::Predicate {
23870                            name: prep,
23871                            args: p.ctx.terms.alloc_slice([Term::Variable(placeholder), *m]),
23872                            world: None,
23873                        }));
23874                    } else if p.check_article() || p.check_content_word() {
23875                        let obj = p.parse_noun_phrase(true)?;
23876                        preds.push(p.ctx.exprs.alloc(LogicExpr::Predicate {
23877                            name: prep,
23878                            args: p.ctx.terms.alloc_slice([
23879                                Term::Variable(placeholder),
23880                                Term::Constant(obj.noun),
23881                            ]),
23882                            world: None,
23883                        }));
23884                    } else {
23885                        break;
23886                    }
23887                }
23888                // Commit only if the matrix predicate follows — a copula, a FINITE
23889                // main verb, or a do-support / perfect AUXILIARY heading the matrix
23890                // VP. The "participle (+ complements) THEN a matrix predicate" pattern
23891                // is the reduced-relative signature ("the box [found on May 3] HELD the
23892                // ring", "the book [priced at $55] DOESN'T have a cover", "the well
23893                // [cut through gravel] HAD a leak"); a lone main verb ("The box found
23894                // the key") has no matrix here, so it backtracks and keeps its verb
23895                // reading. The matrix verb must be FINITE — an -ing (progressive) form
23896                // is not a finite matrix verb but a gerund modifier of the verb's
23897                // object ("used BOWLING pins"), so it must NOT trigger the reading.
23898                let matrix_finite_verb = matches!(
23899                    p.peek().kind,
23900                    TokenType::Verb { aspect, .. } if !matches!(aspect, Aspect::Progressive)
23901                );
23902                if !(p.check(&TokenType::Is)
23903                    || p.check(&TokenType::Are)
23904                    || p.check(&TokenType::Was)
23905                    || p.check(&TokenType::Were)
23906                    || p.check(&TokenType::Does)
23907                    || p.check(&TokenType::Do)
23908                    || p.check(&TokenType::Had)
23909                    || matrix_finite_verb)
23910                {
23911                    return Err(ParseError {
23912                        kind: ParseErrorKind::ExpectedCopula,
23913                        span: p.current_span(),
23914                    });
23915                }
23916                Ok(preds)
23917            }) {
23918                let mut pps: Vec<&'a LogicExpr<'a>> = subject.pps.to_vec();
23919                pps.extend(restrs);
23920                subject = crate::ast::NounPhrase {
23921                    pps: self.ctx.pps.alloc_slice(pps),
23922                    ..subject
23923                };
23924            }
23925        }
23926
23927        // Bare-plural copular subject = kind predication (§6.1):
23928        // "Penguins are birds." → Gen x(Penguin(x) → Bird(x)), never the
23929        // ordinary individual-level copular path.
23930        if (self.check(&TokenType::Are) || self.check(&TokenType::Is))
23931            && subject.definiteness.is_none()
23932            && subject.possessor.is_none()
23933        {
23934            let noun_str = self.interner.resolve(subject.noun);
23935            let lower = noun_str.to_lowercase();
23936            let is_known_plural = Self::is_plural_noun(noun_str)
23937                && (crate::lexicon::is_common_noun(&lower)
23938                    || matches!(
23939                        crate::lexicon::analyze_word(&lower),
23940                        Some(crate::lexicon::WordAnalysis::Noun(_))
23941                    ));
23942            let nominal_or_adj_follows = matches!(
23943                self.tokens.get(self.current + 1).map(|t| &t.kind),
23944                Some(TokenType::Noun(_))
23945                    | Some(TokenType::Adjective(_))
23946                    | Some(TokenType::Article(_))
23947            );
23948            if is_known_plural && nominal_or_adj_follows {
23949                self.advance(); // copula
23950                let negated = self.check(&TokenType::Not);
23951                if negated {
23952                    self.advance();
23953                }
23954                if self.check_article() {
23955                    self.advance();
23956                }
23957                let var_name = self.next_var_name();
23958                let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
23959                    name: subject.noun,
23960                    args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
23961                    world: None,
23962                });
23963                let pred_name = match self.peek().kind {
23964                    TokenType::Noun(s) | TokenType::Adjective(s) => {
23965                        self.advance();
23966                        s
23967                    }
23968                    _ => self.consume_content_word()?,
23969                };
23970                let mut nucleus: &'a LogicExpr<'a> = self.ctx.exprs.alloc(LogicExpr::Predicate {
23971                    name: pred_name,
23972                    args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
23973                    world: None,
23974                });
23975                if negated {
23976                    nucleus = self.ctx.exprs.alloc(LogicExpr::UnaryOp {
23977                        op: TokenType::Not,
23978                        operand: nucleus,
23979                    });
23980                }
23981                let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
23982                    left: type_pred,
23983                    op: TokenType::Implies,
23984                    right: nucleus,
23985                });
23986                return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
23987                    kind: QuantifierKind::Generic,
23988                    variable: var_name,
23989                    body,
23990                    island_id: self.current_island,
23991                }));
23992            }
23993        }
23994
23995        // A relative-clause subject feeding a copula ("The scent that sold for
23996        // $75 is the white fragrance.") — stash the restriction, rewritten over
23997        // the subject constant, so the copula's wrap_with_definiteness re-binds
23998        // it instead of silently dropping it. The copula branches all key their
23999        // predicate on Constant(subject.noun), so the fold unifies cleanly.
24000        if let Some((rc_var, rc_pred)) = relative_clause {
24001            if self.check(&TokenType::Is)
24002                || self.check(&TokenType::Are)
24003                || self.check(&TokenType::Was)
24004                || self.check(&TokenType::Were)
24005            {
24006                let restr =
24007                    self.substitute_variable_with_constant(rc_pred, rc_var, subject.noun)?;
24008                self.pending_subject_restriction = Some((subject.noun, restr));
24009            }
24010        }
24011
24012        if self.check(&TokenType::Is)
24013            || self.check(&TokenType::Are)
24014            || self.check(&TokenType::Was)
24015            || self.check(&TokenType::Were)
24016        {
24017            let copula_time = if self.check(&TokenType::Was) || self.check(&TokenType::Were) {
24018                Time::Past
24019            } else {
24020                Time::Present
24021            };
24022            self.advance();
24023
24024            // Check for negation: "was not caught", "is not happy"
24025            let is_negated = self.check(&TokenType::Not);
24026            if is_negated {
24027                self.advance(); // consume "not"
24028            }
24029
24030            // Check for temporal adverbs after copula: "is always Y", "is never Y"
24031            let mut copula_temporal = None;
24032            if !is_negated {
24033                if self.check(&TokenType::Never) {
24034                    self.advance();
24035                    copula_temporal = Some(CopulaTemporal::Never);
24036                } else if let TokenType::Adverb(sym)
24037                | TokenType::ScopalAdverb(sym)
24038                | TokenType::TemporalAdverb(sym) = &self.peek().kind
24039                {
24040                    let resolved = self.interner.resolve(*sym).to_string();
24041                    if resolved == "Always" || resolved == "always" {
24042                        self.advance();
24043                        copula_temporal = Some(CopulaTemporal::Always);
24044                    } else if resolved == "Eventually" || resolved == "eventually" {
24045                        self.advance();
24046                        copula_temporal = Some(CopulaTemporal::Eventually);
24047                    }
24048                }
24049            }
24050
24051            // Membership partitive: "Tom is one of the boys." → Boy(Tom).
24052            // Member-of-the-sum is atomic parthood in the Link lattice, which
24053            // for a definite plural is just the noun predicate (§5.4/§6.2).
24054            if matches!(self.peek().kind, TokenType::Cardinal(1)) {
24055                let of_follows = matches!(
24056                    self.tokens.get(self.current + 1).map(|t| &t.kind),
24057                    Some(TokenType::Preposition(p)) if self.interner.resolve(*p).eq_ignore_ascii_case("of")
24058                );
24059                let article_then_noun = matches!(
24060                    self.tokens.get(self.current + 2).map(|t| &t.kind),
24061                    Some(TokenType::Article(_))
24062                ) && matches!(
24063                    self.tokens.get(self.current + 3).map(|t| &t.kind),
24064                    Some(TokenType::Noun(_))
24065                );
24066                if of_follows && article_then_noun {
24067                    self.advance(); // "one"
24068                    self.advance(); // "of"
24069                    self.advance(); // "the"
24070                    let noun_sym = match self.advance().kind {
24071                        TokenType::Noun(s) => s,
24072                        _ => unreachable!("guarded by article_then_noun"),
24073                    };
24074                    let noun_str = self.interner.resolve(noun_sym).to_string();
24075                    let singular = Self::singularize_noun(&noun_str);
24076                    let name_sym = self.interner.intern(&singular);
24077                    let mut result: &'a LogicExpr<'a> =
24078                        self.ctx.exprs.alloc(LogicExpr::Predicate {
24079                            name: name_sym,
24080                            args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
24081                            world: None,
24082                        });
24083                    if is_negated {
24084                        result = self.ctx.exprs.alloc(LogicExpr::UnaryOp {
24085                            op: TokenType::Not,
24086                            operand: result,
24087                        });
24088                    }
24089                    return self.wrap_with_definiteness_full(&subject, result);
24090                }
24091            }
24092
24093            // A degree adverb directly before a comparative ("is somewhat shorter
24094            // than", "is slightly wider than") must be skipped here so the
24095            // comparative branch fires — otherwise it is read as a bare adjective
24096            // complement and the comparative strands. The degree-adverb class is a
24097            // closed lexical category (lexicon `degree_adverbs`), not a parser list,
24098            // so a real adjective complement ("is happy") is untouched.
24099            if matches!(
24100                self.tokens.get(self.current + 1).map(|t| &t.kind),
24101                Some(TokenType::Comparative(_))
24102            ) && lexicon::is_degree_adverb(
24103                &self.interner.resolve(self.peek().lexeme).to_lowercase(),
24104            ) {
24105                self.advance(); // degree adverb
24106            }
24107
24108            // Check for Number token (measure phrase) before comparative or adjective
24109            // "John is 2 inches taller than Mary" or "The rope is 5 meters long"
24110            if self.check_number() {
24111                let measure = self.parse_measure_phrase()?;
24112
24113                // Check if followed by comparative: "2 inches taller than"
24114                if self.check_comparative() {
24115                    return self.parse_comparative(&subject, copula_time, Some(measure));
24116                }
24117
24118                // Check for dimensional adjective: "5 meters long"
24119                if self.check_content_word() {
24120                    let adj = self.consume_content_word()?;
24121                    let result = self.ctx.exprs.alloc(LogicExpr::Predicate {
24122                        name: adj,
24123                        args: self
24124                            .ctx
24125                            .terms
24126                            .alloc_slice([Term::Constant(subject.noun), *measure]),
24127                        world: None,
24128                    });
24129                    return self.wrap_with_definiteness_full(&subject, result);
24130                }
24131
24132                // Bare measure phrase: "The temperature is 98.6 degrees."
24133                // Output: Identity(subject, measure)
24134                if self.check(&TokenType::Period) || self.is_at_end() {
24135                    // In imperative mode, reject "x is 5" - suggest "x equals 5"
24136                    if self.mode == ParserMode::Imperative {
24137                        let variable = self.interner.resolve(subject.noun).to_string();
24138                        let value = if let Term::Value { kind, .. } = measure {
24139                            format!("{:?}", kind)
24140                        } else {
24141                            "value".to_string()
24142                        };
24143                        return Err(ParseError {
24144                            kind: ParseErrorKind::IsValueEquality { variable, value },
24145                            span: self.current_span(),
24146                        });
24147                    }
24148                    let result = self.ctx.exprs.alloc(LogicExpr::Identity {
24149                        left: self.ctx.terms.alloc(Term::Constant(subject.noun)),
24150                        right: measure,
24151                    });
24152                    return self.wrap_with_definiteness_full(&subject, result);
24153                }
24154            }
24155
24156            // Check for equative: "is as tall as Y" → an at-least (≥) comparison.
24157            if self
24158                .interner
24159                .resolve(self.peek().lexeme)
24160                .eq_ignore_ascii_case("as")
24161            {
24162                return self.parse_equative(&subject);
24163            }
24164
24165            // Check for comparative: "is taller than"
24166            if self.check_comparative() {
24167                return self.parse_comparative(&subject, copula_time, None);
24168            }
24169
24170            // Check for existential "is": "God is." - bare copula followed by period/EOF
24171            if self.check(&TokenType::Period) || self.is_at_end() {
24172                let var = self.next_var_name();
24173                let body = self.ctx.exprs.alloc(LogicExpr::Identity {
24174                    left: self.ctx.terms.alloc(Term::Variable(var)),
24175                    right: self.ctx.terms.alloc(Term::Constant(subject.noun)),
24176                });
24177                return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
24178                    kind: QuantifierKind::Existential,
24179                    variable: var,
24180                    body,
24181                    island_id: self.current_island,
24182                }));
24183            }
24184
24185            // Check for superlative: "is the tallest man"
24186            if self.check(&TokenType::Article(Definiteness::Definite)) {
24187                let saved_pos = self.backtrack_point();
24188                self.advance();
24189                if self.check_superlative() {
24190                    return self.parse_superlative(&subject);
24191                }
24192                self.backtrack_to(&saved_pos);
24193            }
24194
24195            // Check for predicate NP: "Juliet is the sun" or "John is a man"
24196            if self.check_article() {
24197                // The copula is already consumed, so a verb-word in the predicate
24198                // NP can never be the matrix verb — it is a deverbal noun head
24199                // ("is the orange PACK", "is the Russell Road PROJECT").
24200                let saved_ctx = self.nominal_np_context;
24201                self.nominal_np_context = true;
24202                let predicate_np_result = self.parse_noun_phrase(true);
24203                self.nominal_np_context = saved_ctx;
24204                let predicate_np = predicate_np_result?;
24205                let predicate_noun = predicate_np.noun;
24206
24207                // Enumerated identity: "The four dances are the hustle, the
24208                // lindy, the twist, and the waltz." A genuine comma list (≥3
24209                // members) identifies the plural subject with the group; a bare
24210                // "is a doctor and wealthy" is predication conjunction, not a
24211                // group identity, so a comma is required to enter this path.
24212                // A who/that relative on the FIRST member hides the comma that
24213                // opens the list ("The four winners are the winner WHO won, the
24214                // runner-up, …"). Speculatively consume the member-with-relative and
24215                // commit only if a comma actually follows; otherwise restore exactly
24216                // so the single "X is the Y who Z" copula path below runs unchanged.
24217                if self.check(&TokenType::Who) || self.check(&TokenType::That) {
24218                    let rel_cp = self.checkpoint();
24219                    if let Ok(first) = self.parse_list_member_np(&predicate_np, false) {
24220                        if self.check(&TokenType::Comma) {
24221                            return self.finish_enumerated_identity(&subject, is_negated, first);
24222                        }
24223                    }
24224                    self.restore(rel_cp);
24225                }
24226                if self.check(&TokenType::Comma) {
24227                    let first = self.parse_list_member_np(&predicate_np, false)?;
24228                    return self.finish_enumerated_identity(&subject, is_negated, first);
24229                }
24230
24231                // Phase 41: Event adjective reading
24232                // "beautiful dancer" in event mode → ∃e(Dance(e) ∧ Agent(e, x) ∧ Beautiful(e))
24233                if self.event_reading_mode {
24234                    let noun_str = self.interner.resolve(predicate_noun);
24235                    if let Some(base_verb) = lexicon::lookup_agentive_noun(noun_str) {
24236                        // Check if any adjective can modify events
24237                        let event_adj = predicate_np.adjectives.iter().find(|adj| {
24238                            lexicon::is_event_modifier_adjective(self.interner.resolve(**adj))
24239                        });
24240
24241                        if let Some(&adj_sym) = event_adj {
24242                            // Build event reading: ∃e(Verb(e) ∧ Agent(e, subject) ∧ Adj(e))
24243                            let verb_sym = self.interner.intern(base_verb);
24244                            let event_var = self.get_event_var();
24245
24246                            let verb_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
24247                                name: verb_sym,
24248                                args: self.ctx.terms.alloc_slice([Term::Variable(event_var)]),
24249                                world: None,
24250                            });
24251
24252                            let agent_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
24253                                name: self.interner.intern("Agent"),
24254                                args: self.ctx.terms.alloc_slice([
24255                                    Term::Variable(event_var),
24256                                    Term::Constant(subject.noun),
24257                                ]),
24258                                world: None,
24259                            });
24260
24261                            let adj_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
24262                                name: adj_sym,
24263                                args: self.ctx.terms.alloc_slice([Term::Variable(event_var)]),
24264                                world: None,
24265                            });
24266
24267                            // Conjoin: Verb(e) ∧ Agent(e, x)
24268                            let verb_agent = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24269                                left: verb_pred,
24270                                op: TokenType::And,
24271                                right: agent_pred,
24272                            });
24273
24274                            // Conjoin: (Verb(e) ∧ Agent(e, x)) ∧ Adj(e)
24275                            let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24276                                left: verb_agent,
24277                                op: TokenType::And,
24278                                right: adj_pred,
24279                            });
24280
24281                            // Wrap in existential: ∃e(...)
24282                            let event_reading = self.ctx.exprs.alloc(LogicExpr::Quantifier {
24283                                kind: QuantifierKind::Existential,
24284                                variable: event_var,
24285                                body,
24286                                island_id: self.current_island,
24287                            });
24288
24289                            return self.wrap_with_definiteness_full(&subject, event_reading);
24290                        }
24291                    }
24292                }
24293
24294                let subject_sort = lexicon::lookup_sort(self.interner.resolve(subject.noun));
24295                let predicate_sort = lexicon::lookup_sort(self.interner.resolve(predicate_noun));
24296
24297                if let (Some(s_sort), Some(p_sort)) = (subject_sort, predicate_sort) {
24298                    if !s_sort.is_compatible_with(p_sort) && !p_sort.is_compatible_with(s_sort) {
24299                        let metaphor: &'a LogicExpr<'a> =
24300                            self.ctx.exprs.alloc(LogicExpr::Metaphor {
24301                                tenor: self.ctx.terms.alloc(Term::Constant(subject.noun)),
24302                                vehicle: self.ctx.terms.alloc(Term::Constant(predicate_noun)),
24303                            });
24304                        let metaphor = if is_negated {
24305                            &*self.ctx.exprs.alloc(LogicExpr::UnaryOp {
24306                                op: TokenType::Not,
24307                                operand: metaphor,
24308                            })
24309                        } else {
24310                            metaphor
24311                        };
24312                        return self.wrap_with_definiteness_full(&subject, metaphor);
24313                    }
24314                }
24315
24316                // Default: intersective reading for adjectives
24317                // Build Adj1(x) ∧ Adj2(x) ∧ ... ∧ Noun(x)
24318                let mut predicates: Vec<&'a LogicExpr<'a>> = Vec::new();
24319
24320                // Add adjective predicates
24321                for &adj_sym in predicate_np.adjectives {
24322                    let adj_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
24323                        name: adj_sym,
24324                        args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
24325                        world: None,
24326                    });
24327                    predicates.push(adj_pred);
24328                }
24329
24330                // Add noun predicate
24331                let noun_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
24332                    name: predicate_noun,
24333                    args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
24334                    world: None,
24335                });
24336                predicates.push(noun_pred);
24337
24338                // A predicate-nominal genitive ("X is the Woodard family's house",
24339                // "X is the red team's trophy") binds the possessor to the subject.
24340                // Routed through the shared possessor lowering so a MODIFIED possessor
24341                // keeps its restrictor (∃p(Team(p) ∧ Red(p) ∧ Possesses(p, X))) instead
24342                // of collapsing to a bare constant that drops "red" — a meaning-loss
24343                // parse the prover can't use.
24344                if let Some(possessor) = predicate_np.possessor {
24345                    let poss_pred =
24346                        self.possessor_predication(possessor, Term::Constant(subject.noun));
24347                    predicates.push(poss_pred);
24348                }
24349
24350                // The predicate nominal's PP restrictors ("X is the gator FROM Lynn",
24351                // "the gator CAUGHT in Lynn", "the doll 80 years OLD") are predicated of
24352                // the subject; dropping them is meaning loss. Each pp is written over the
24353                // _PP_SELF_ placeholder — rebind it to the subject. Only the pps are
24354                // merged here (not the whole block via nominal_predication_with_pps),
24355                // because the metaphor / sort-incompatibility / event-adjective readings
24356                // above are intersective-reading-specific and must stay reachable.
24357                for pp in predicate_np.pps {
24358                    let pp_sub = self.substitute_pp_self_term(pp, Term::Constant(subject.noun));
24359                    predicates.push(pp_sub);
24360                }
24361
24362                // Conjoin all predicates
24363                let mut result = if predicates.len() == 1 {
24364                    predicates[0]
24365                } else {
24366                    let mut combined = predicates[0];
24367                    for pred in &predicates[1..] {
24368                        combined = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24369                            left: combined,
24370                            op: TokenType::And,
24371                            right: *pred,
24372                        });
24373                    }
24374                    combined
24375                };
24376
24377                // A relative clause on the predicate nominal ("X is the player
24378                // WHO played", "X was the one THAT won 9 games") is predicated of
24379                // the SUBJECT — being that player entails X played. The gap binds
24380                // to the subject constant, and the restriction-VP composes its own
24381                // features (objects, counting NPs). Previously "who"/"that" here
24382                // stranded (TrailingTokens), failing every "X is the Y who/that Z".
24383                let mut result = result;
24384                if let Some(rc) = self.try_attach_relative(Term::Constant(subject.noun))? {
24385                    result = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24386                        left: result,
24387                        op: TokenType::And,
24388                        right: rc,
24389                    });
24390                }
24391
24392                // Non-parallel copula coordination: "Mary is a doctor and wealthy"
24393                // / "is a doctor and a lawyer" — each conjunct predicates the SAME
24394                // subject (a comma list ≥3 is group identity, handled above). Stop
24395                // at anything that isn't an adjective or article-NP complement so a
24396                // genuine clause coordination ("… and Bob runs") is left intact.
24397                loop {
24398                    if !self.check(&TokenType::And) {
24399                        break;
24400                    }
24401                    let save = self.backtrack_point();
24402                    self.advance(); // "and"
24403                                    // A bare predicate word (adjective, or a noun the lexicon
24404                                    // didn't mark adjectival like "wealthy") is a shared predicate;
24405                                    // a noun is only taken when a clause boundary / "and" follows,
24406                                    // so "… and Bob runs" / "… and cats sleep" stay separate clauses.
24407                    let next_is_boundary = matches!(
24408                        self.tokens.get(self.current + 1).map(|t| &t.kind),
24409                        Some(TokenType::Period)
24410                            | Some(TokenType::Comma)
24411                            | Some(TokenType::And)
24412                            | Some(TokenType::EOF)
24413                            | Some(TokenType::Exclamation)
24414                            | None
24415                    );
24416                    let conjunct: Option<&'a LogicExpr<'a>> = match self.peek().kind {
24417                        TokenType::Adjective(a) | TokenType::NonIntersectiveAdjective(a) => {
24418                            self.advance();
24419                            Some(self.ctx.exprs.alloc(LogicExpr::Predicate {
24420                                name: a,
24421                                args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
24422                                world: None,
24423                            }))
24424                        }
24425                        TokenType::Noun(a) if next_is_boundary => {
24426                            self.advance();
24427                            Some(self.ctx.exprs.alloc(LogicExpr::Predicate {
24428                                name: a,
24429                                args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
24430                                world: None,
24431                            }))
24432                        }
24433                        _ if self.check_article() => match self.parse_noun_phrase(true) {
24434                            Ok(np) => {
24435                                Some(self.nominal_predication(Term::Constant(subject.noun), &np))
24436                            }
24437                            Err(_) => None,
24438                        },
24439                        _ => None,
24440                    };
24441                    match conjunct {
24442                        Some(c) => {
24443                            result = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24444                                left: result,
24445                                op: TokenType::And,
24446                                right: c,
24447                            });
24448                        }
24449                        None => {
24450                            self.backtrack_to(&save);
24451                            break;
24452                        }
24453                    }
24454                }
24455
24456                // "is NOT a man" negates the whole nominal predication.
24457                let result = if is_negated {
24458                    &*self.ctx.exprs.alloc(LogicExpr::UnaryOp {
24459                        op: TokenType::Not,
24460                        operand: result,
24461                    })
24462                } else {
24463                    result
24464                };
24465
24466                // Use the full NP so the SUBJECT's adjectives / PPs / possessor
24467                // survive — "X is the Y" must not drop them (e.g. "Ray Ricardo's
24468                // stamp is the Yellownose" keeps the genitive, "The red stamp is
24469                // the Yellownose" keeps "red"). Mirrors the enumerated-identity
24470                // (above) and adjective-complement (below) sites.
24471                return self.wrap_with_definiteness_full(&subject, result);
24472            }
24473
24474            // After copula, prefer Adjective over simple-aspect Verb for ambiguous tokens
24475            // "is open" (Adj: state) is standard; "is open" (Verb: habitual) is ungrammatical here
24476            let prefer_adjective = if let TokenType::Ambiguous {
24477                primary,
24478                alternatives,
24479            } = &self.peek().kind
24480            {
24481                let is_simple_verb = if let TokenType::Verb { aspect, .. } = **primary {
24482                    aspect == Aspect::Simple
24483                } else {
24484                    false
24485                };
24486                let has_adj_alt = alternatives
24487                    .iter()
24488                    .any(|t| matches!(t, TokenType::Adjective(_)));
24489                is_simple_verb && has_adj_alt
24490            } else {
24491                false
24492            };
24493
24494            if !prefer_adjective && self.check_verb() {
24495                let (verb, _verb_time, verb_aspect, verb_class) = self.consume_verb_with_metadata();
24496
24497                // Stative verbs cannot be progressive
24498                if verb_class.is_stative() && verb_aspect == Aspect::Progressive {
24499                    return Err(ParseError {
24500                        kind: ParseErrorKind::StativeProgressiveConflict,
24501                        span: self.current_span(),
24502                    });
24503                }
24504
24505                // Collect any prepositional phrases before "by" (for ditransitives)
24506                // "given to Mary by John" → goal = Mary, then agent = John
24507                let mut goal_args: Vec<Term<'a>> = Vec::new();
24508                while self.check_to_preposition() {
24509                    self.advance(); // consume "to"
24510                    let goal = self.parse_noun_phrase(true)?;
24511                    goal_args.push(self.noun_phrase_to_term(&goal));
24512                }
24513
24514                // Check for passive: "was loved by John" or "was given to Mary by John"
24515                if self.check_by_preposition() {
24516                    self.advance(); // consume "by"
24517                    let agent = self.parse_noun_phrase(true)?;
24518
24519                    // A DESCRIPTIVE by-agent ("the red team", "the Woodard family")
24520                    // becomes its own restrictor-carrying entity scoping the relation
24521                    // — `∃p(Restrictor(p) ∧ Verb(p, theme))` — instead of collapsing
24522                    // to the bare head. A bare agent keeps the constant form.
24523                    let (agent_term, agent_restr) = self.possessor_entity(&agent);
24524                    let mut args = vec![agent_term, self.noun_phrase_to_term(&subject)];
24525                    args.extend(goal_args);
24526
24527                    let predicate = self.ctx.exprs.alloc(LogicExpr::Predicate {
24528                        name: verb,
24529                        args: self.ctx.terms.alloc_slice(args),
24530                        world: None,
24531                    });
24532                    let predicate = self.wrap_in_possessor_entity(agent_restr, predicate);
24533
24534                    let with_time = if copula_time == Time::Past {
24535                        self.ctx.exprs.alloc(LogicExpr::Temporal {
24536                            operator: TemporalOperator::Past,
24537                            body: predicate,
24538                        })
24539                    } else {
24540                        predicate
24541                    };
24542
24543                    return self.wrap_with_definiteness_full(&subject, with_time);
24544                }
24545
24546                // Agentless passive: "The book was read" → ∃x.Read(x, Book)
24547                // For DEFINITE subjects ("The butler was caught"), use simpler reading
24548                // without existential over implicit agent: Past(catch(butler))
24549                // This makes negation cleaner for theorem proving: ¬Past(catch(butler))
24550                //
24551                // The existential-over-type reading is correct for a bare plural /
24552                // indefinite ("Drugs were approved", "A book was read") — a known
24553                // COMMON noun. A PROPER NAME ("Pluniden was approved") is a specific
24554                // referent, not a type, so it falls through to the constant path
24555                // below (which also carries trailing PP adjuncts and offsets).
24556                let subject_is_common_noun = {
24557                    let n = self.interner.resolve(subject.noun);
24558                    lexicon::lookup_noun_db(n).is_some()
24559                        || (n.ends_with('s')
24560                            && lexicon::lookup_noun_db(&n[..n.len() - 1]).is_some())
24561                };
24562                if copula_time == Time::Past
24563                    && verb_aspect == Aspect::Simple
24564                    && subject.definiteness != Some(Definiteness::Definite)
24565                    && subject_is_common_noun
24566                {
24567                    // Indefinite agentless passive - treat as existential over implicit agent
24568                    let var_name = self.next_var_name();
24569                    let predicate = self.ctx.exprs.alloc(LogicExpr::Predicate {
24570                        name: verb,
24571                        args: self
24572                            .ctx
24573                            .terms
24574                            .alloc_slice([Term::Variable(var_name), Term::Constant(subject.noun)]),
24575                        world: None,
24576                    });
24577
24578                    let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
24579                        name: subject.noun,
24580                        args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
24581                        world: None,
24582                    });
24583
24584                    let temporal = self.ctx.exprs.alloc(LogicExpr::Temporal {
24585                        operator: TemporalOperator::Past,
24586                        body: predicate,
24587                    });
24588
24589                    let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24590                        left: type_pred,
24591                        op: TokenType::And,
24592                        right: temporal,
24593                    });
24594
24595                    let result = self.ctx.exprs.alloc(LogicExpr::Quantifier {
24596                        kind: QuantifierKind::Existential,
24597                        variable: var_name,
24598                        body,
24599                        island_id: self.current_island,
24600                    });
24601
24602                    // Apply negation if "was not caught"
24603                    if is_negated {
24604                        return Ok(self.ctx.exprs.alloc(LogicExpr::UnaryOp {
24605                            op: TokenType::Not,
24606                            operand: result,
24607                        }));
24608                    }
24609                    return Ok(result);
24610                }
24611
24612                // Check if verb is an intensional predicate (e.g., "rising", "changing")
24613                // Intensional predicates take intensions, not extensions
24614                let verb_str = self.interner.resolve(verb).to_lowercase();
24615                let subject_term = if lexicon::is_intensional_predicate(&verb_str) {
24616                    Term::Intension(subject.noun)
24617                } else {
24618                    Term::Constant(subject.noun)
24619                };
24620
24621                let mut predicate: &'a LogicExpr<'a> = self.ctx.exprs.alloc(LogicExpr::Predicate {
24622                    name: verb,
24623                    args: self.ctx.terms.alloc_slice([subject_term]),
24624                    world: None,
24625                });
24626
24627                // A PROGRESSIVE transitive ("is paying the rent", "is reading a
24628                // book", "is paying $700") takes a DIRECT OBJECT → Verb(subject,
24629                // object). A passive past-participle has none (its theme IS the
24630                // subject), so gate on Progressive. Not a preposition (that's a PP
24631                // adjunct, handled below) and not "by"/"to" (passive/dative above).
24632                if verb_aspect == Aspect::Progressive
24633                    && !self.check_preposition()
24634                    && (self.check_article() || self.check_number() || self.check_content_word())
24635                {
24636                    let obj_term = if self.check_number() {
24637                        *self.parse_measure_phrase()?
24638                    } else {
24639                        let obj_np = self.parse_noun_phrase(false)?;
24640                        self.noun_phrase_to_term(&obj_np)
24641                    };
24642                    predicate = self.ctx.exprs.alloc(LogicExpr::Predicate {
24643                        name: verb,
24644                        args: self.ctx.terms.alloc_slice([subject_term, obj_term]),
24645                        world: None,
24646                    });
24647                }
24648
24649                // Trailing locative/temporal/material PP adjuncts on the passive ("was
24650                // taken on May 9", "was found in Spain", "was made OF rosewood") —
24651                // predicated of the theme (the subject). "of" after a passive participle
24652                // is the MATERIAL complement (Of(x, rosewood)), not a possessive.
24653                while self.check_preposition() {
24654                    let prep = match self.advance().kind {
24655                        TokenType::Preposition(s) => s,
24656                        _ => break,
24657                    };
24658                    let pp = if self.check_number() {
24659                        let m = self.parse_measure_phrase()?;
24660                        self.ctx.exprs.alloc(LogicExpr::Predicate {
24661                            name: prep,
24662                            args: self.ctx.terms.alloc_slice([subject_term, *m]),
24663                            world: None,
24664                        })
24665                    } else if self.check_content_word()
24666                        || matches!(self.peek().kind, TokenType::Article(_))
24667                    {
24668                        let obj = self.parse_noun_phrase(true)?;
24669                        self.ctx.exprs.alloc(LogicExpr::Predicate {
24670                            name: prep,
24671                            args: self
24672                                .ctx
24673                                .terms
24674                                .alloc_slice([subject_term, Term::Constant(obj.noun)]),
24675                            world: None,
24676                        })
24677                    } else {
24678                        break;
24679                    };
24680                    predicate = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24681                        left: predicate,
24682                        op: TokenType::And,
24683                        right: pp,
24684                    });
24685                }
24686
24687                // A trailing calendar-unit offset ("was taken 1 month after Y",
24688                // "was found 3 days before Z") relates the theme's temporal
24689                // position to a distinct standard — solver-ready, shared with the
24690                // active VP path.
24691                if let Some(constraint) = self.parse_temporal_offset_constraint(subject_term)? {
24692                    predicate = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24693                        left: predicate,
24694                        op: TokenType::And,
24695                        right: constraint,
24696                    });
24697                }
24698                // A bare temporal ordering ("was taken sometime before the photo of
24699                // the red panda", "was bought sometime before Faye's pet") relates
24700                // the theme to a distinct standard by time.
24701                else if let Some(constraint) =
24702                    self.parse_bare_temporal_constraint(subject_term)?
24703                {
24704                    predicate = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24705                        left: predicate,
24706                        op: TokenType::And,
24707                        right: constraint,
24708                    });
24709                }
24710
24711                // Manner adverbs after the participle ("was running quickly")
24712                // modify the event under the aspect operator.
24713                let adverbs = self.collect_adverbs();
24714                let predicate = if adverbs.is_empty() {
24715                    predicate
24716                } else {
24717                    self.ctx.exprs.alloc(LogicExpr::Event {
24718                        predicate,
24719                        adverbs: self.ctx.syms.alloc_slice(adverbs),
24720                    })
24721                };
24722
24723                let with_aspect = if verb_aspect == Aspect::Progressive {
24724                    // Semelfactive + Progressive → Iterative
24725                    let operator = if verb_class == VerbClass::Semelfactive {
24726                        AspectOperator::Iterative
24727                    } else {
24728                        AspectOperator::Progressive
24729                    };
24730                    self.ctx.exprs.alloc(LogicExpr::Aspectual {
24731                        operator,
24732                        body: predicate,
24733                    })
24734                } else {
24735                    predicate
24736                };
24737
24738                let with_time = if copula_time == Time::Past {
24739                    self.ctx.exprs.alloc(LogicExpr::Temporal {
24740                        operator: TemporalOperator::Past,
24741                        body: with_aspect,
24742                    })
24743                } else {
24744                    with_aspect
24745                };
24746
24747                let final_expr = if is_negated {
24748                    self.ctx.exprs.alloc(LogicExpr::UnaryOp {
24749                        op: TokenType::Not,
24750                        operand: with_time,
24751                    })
24752                } else {
24753                    with_time
24754                };
24755
24756                // For a BARE definite subject, return directly without the
24757                // Russellian wrapper: "The butler was caught" → Past(catch(butler)),
24758                // keeping the output simple for theorem proving. But a definite
24759                // subject WITH restrictions (adjectives, PPs like "at 40.5912 N",
24760                // a possessor) must go through the full wrapper so those
24761                // constraints survive — dropping them is meaning loss.
24762                if subject.definiteness == Some(Definiteness::Definite)
24763                    && subject.adjectives.is_empty()
24764                    && subject.pps.is_empty()
24765                    && subject.possessor.is_none()
24766                {
24767                    return Ok(final_expr);
24768                }
24769
24770                return self.wrap_with_definiteness_full(&subject, final_expr);
24771            }
24772
24773            // Handle relative clause with copula: "The book that John read is good."
24774            // The complement uses the same forms as a plain copula — proper-name
24775            // identity, "either A or B" disjunction, and (the)-NP predication —
24776            // predicated of the relative-clause variable, not just a bare adjective.
24777            if let Some((var_name, rel_clause)) = relative_clause {
24778                let var_term = Term::Variable(var_name);
24779
24780                let main_pred: &'a LogicExpr<'a> = if self.check(&TokenType::Either) {
24781                    self.advance();
24782                    let np1 = self.parse_noun_phrase(true)?;
24783                    let p1 = self.nominal_predication_with_pps(var_term, &np1);
24784                    let p1 = self.conjoin_trailing_relative(p1, var_term)?;
24785                    if self.check(&TokenType::Or) {
24786                        self.advance();
24787                        let np2 = self.parse_noun_phrase(true)?;
24788                        let p2 = self.nominal_predication_with_pps(var_term, &np2);
24789                        let p2 = self.conjoin_trailing_relative(p2, var_term)?;
24790                        self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24791                            left: p1,
24792                            op: TokenType::Or,
24793                            right: p2,
24794                        })
24795                    } else {
24796                        p1
24797                    }
24798                } else if matches!(self.peek().kind, TokenType::ProperName(_))
24799                    && matches!(
24800                        self.tokens.get(self.current + 1).map(|t| &t.kind),
24801                        Some(TokenType::Possessive)
24802                    )
24803                {
24804                    // "is Kerry's project" — a possessive NP complement.
24805                    let np = self.parse_noun_phrase(true)?;
24806                    self.nominal_predication_with_pps(var_term, &np)
24807                } else if let TokenType::ProperName(pname) = self.peek().kind {
24808                    self.advance();
24809                    self.ctx.exprs.alloc(LogicExpr::Identity {
24810                        left: self.ctx.terms.alloc(var_term),
24811                        right: self.ctx.terms.alloc(Term::Constant(pname)),
24812                    })
24813                } else if self.check_article() {
24814                    let np = self.parse_noun_phrase(true)?;
24815                    let pred = self.nominal_predication_with_pps(var_term, &np);
24816                    self.conjoin_trailing_relative(pred, var_term)?
24817                } else if self.check_preposition() && !self.check_by_preposition() {
24818                    // PP predicate complement: "… is FROM Australia", "… is ON
24819                    // Holly Street", "… is AT 40 degrees" — predicated of the
24820                    // relativized variable. Mirrors the plain-copula PP path.
24821                    let prep_sym = match self.advance().kind {
24822                        TokenType::Preposition(s) => s,
24823                        _ => unreachable!("guarded by check_preposition()"),
24824                    };
24825                    if self.check_number() {
24826                        let measure = self.parse_measure_phrase()?;
24827                        self.ctx.exprs.alloc(LogicExpr::Predicate {
24828                            name: prep_sym,
24829                            args: self.ctx.terms.alloc_slice([var_term, *measure]),
24830                            world: None,
24831                        })
24832                    } else {
24833                        let obj = self.parse_noun_phrase(true)?;
24834                        self.ctx.exprs.alloc(LogicExpr::Predicate {
24835                            name: prep_sym,
24836                            args: self
24837                                .ctx
24838                                .terms
24839                                .alloc_slice([var_term, Term::Constant(obj.noun)]),
24840                            world: None,
24841                        })
24842                    }
24843                } else {
24844                    let pred_word = self.consume_content_word()?;
24845                    self.ctx.exprs.alloc(LogicExpr::Predicate {
24846                        name: pred_word,
24847                        args: self.ctx.terms.alloc_slice([var_term]),
24848                        world: None,
24849                    })
24850                };
24851                let main_pred = if is_negated {
24852                    &*self.ctx.exprs.alloc(LogicExpr::UnaryOp {
24853                        op: TokenType::Not,
24854                        operand: main_pred,
24855                    })
24856                } else {
24857                    main_pred
24858                };
24859
24860                let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
24861                    name: subject.noun,
24862                    args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
24863                    world: None,
24864                });
24865
24866                let inner = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24867                    left: type_pred,
24868                    op: TokenType::And,
24869                    right: rel_clause,
24870                });
24871
24872                let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
24873                    left: inner,
24874                    op: TokenType::And,
24875                    right: main_pred,
24876                });
24877
24878                return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
24879                    kind: QuantifierKind::Existential,
24880                    variable: var_name,
24881                    body,
24882                    island_id: self.current_island,
24883                }));
24884            }
24885
24886            // Note: is_negated was already set after copula consumption above
24887
24888            // Possessive predicate with an elided noun: "The scent is Camille's." →
24889            // Possesses(Camille, scent) — the subject BELONGS to the named
24890            // possessor. Detected by a "'s" right after the name with no possessed
24891            // noun (a clause boundary follows); "X is Camille's perfume" (explicit
24892            // noun) falls through to the nominal-predicate paths.
24893            if let TokenType::ProperName(predicate_name) = self.peek().kind {
24894                let next_is_possessive = matches!(
24895                    self.tokens.get(self.current + 1).map(|t| &t.kind),
24896                    Some(TokenType::Possessive)
24897                );
24898                let elided = matches!(
24899                    self.tokens.get(self.current + 2).map(|t| &t.kind),
24900                    Some(TokenType::Period)
24901                        | Some(TokenType::EOF)
24902                        | Some(TokenType::Comma)
24903                        | Some(TokenType::And)
24904                        | Some(TokenType::Or)
24905                        | None
24906                );
24907                if next_is_possessive && elided {
24908                    self.advance(); // proper name
24909                    self.advance(); // "'s"
24910                    let poss = self.ctx.exprs.alloc(LogicExpr::Predicate {
24911                        name: self.interner.intern("Possesses"),
24912                        args: self.ctx.terms.alloc_slice([
24913                            Term::Constant(predicate_name),
24914                            Term::Constant(subject.noun),
24915                        ]),
24916                        world: None,
24917                    });
24918                    let result = if is_negated {
24919                        self.ctx.exprs.alloc(LogicExpr::UnaryOp {
24920                            op: TokenType::Not,
24921                            operand: poss,
24922                        })
24923                    } else {
24924                        poss
24925                    };
24926                    return self.wrap_with_definiteness_full(&subject, result);
24927                }
24928            }
24929
24930            // Possessive NP predicate with an EXPLICIT noun: "The box is Kerry's
24931            // project" → Project(box) ∧ Possesses(Kerry, box). (The elided form "is
24932            // Camille's" is handled above; the bare identity below.)
24933            if matches!(self.peek().kind, TokenType::ProperName(_))
24934                && matches!(
24935                    self.tokens.get(self.current + 1).map(|t| &t.kind),
24936                    Some(TokenType::Possessive)
24937                )
24938            {
24939                let np = self.parse_noun_phrase(true)?;
24940                let pred = self.nominal_predication_with_pps(Term::Constant(subject.noun), &np);
24941                let result = if is_negated {
24942                    self.ctx.exprs.alloc(LogicExpr::UnaryOp {
24943                        op: TokenType::Not,
24944                        operand: pred,
24945                    })
24946                } else {
24947                    pred
24948                };
24949                return self.wrap_with_definiteness_full(&subject, result);
24950            }
24951
24952            // Handle identity: "Clark is Superman" - NP copula ProperName → Identity
24953            // This enables Leibniz's Law: if Clark = Superman and mortal(Clark), then mortal(Superman)
24954            if let TokenType::ProperName(predicate_name) = self.peek().kind {
24955                self.advance(); // consume the proper name
24956                                // Absorb subsequent capitalized words into one multi-word proper
24957                                // name ("Porcher Place", "Highland Drive") so the identity does
24958                                // not strand the second word.
24959                let predicate_name = self.absorb_multiword_proper_name(predicate_name);
24960
24961                // Enumerated identity whose FIRST member is a proper name: "The four
24962                // people are Anthony, the waiter, the musician and the dentist." A
24963                // comma list (≥2 members) identifies the plural subject with the
24964                // group; members may be proper names OR definite descriptions. Mirrors
24965                // the common-noun-first enumerated-identity path above so a leading
24966                // proper name no longer strands the rest of the list.
24967                if self.check(&TokenType::Comma) {
24968                    let mut members: Vec<Term<'a>> = vec![Term::Constant(predicate_name)];
24969                    let mut member_rels: Vec<&'a LogicExpr<'a>> = Vec::new();
24970                    while self.check(&TokenType::Comma) || self.check(&TokenType::And) {
24971                        while self.check(&TokenType::Comma) || self.check(&TokenType::And) {
24972                            self.advance();
24973                        }
24974                        if let TokenType::ProperName(pn) = self.peek().kind {
24975                            self.advance();
24976                            let pn = self.absorb_multiword_proper_name(pn);
24977                            members.push(Term::Constant(pn));
24978                        } else if self.check_article() || self.check_content_word() {
24979                            let item = self.parse_noun_phrase(true)?;
24980                            members.push(Term::Constant(item.noun));
24981                            if let Some(rc) = self.try_attach_relative(Term::Constant(item.noun))? {
24982                                member_rels.push(rc);
24983                            }
24984                        } else {
24985                            break;
24986                        }
24987                    }
24988                    let group = Term::Group(self.ctx.terms.alloc_slice(members));
24989                    let identity: &'a LogicExpr<'a> = self.ctx.exprs.alloc(LogicExpr::Identity {
24990                        left: self.ctx.terms.alloc(Term::Constant(subject.noun)),
24991                        right: self.ctx.terms.alloc(group),
24992                    });
24993                    let mut result = if is_negated {
24994                        &*self.ctx.exprs.alloc(LogicExpr::UnaryOp {
24995                            op: TokenType::Not,
24996                            operand: identity,
24997                        })
24998                    } else {
24999                        identity
25000                    };
25001                    for rc in member_rels {
25002                        result = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
25003                            left: result,
25004                            op: TokenType::And,
25005                            right: rc,
25006                        });
25007                    }
25008                    return self.wrap_with_definiteness_full(&subject, result);
25009                }
25010
25011                let identity = self.ctx.exprs.alloc(LogicExpr::Identity {
25012                    left: self.ctx.terms.alloc(Term::Constant(subject.noun)),
25013                    right: self.ctx.terms.alloc(Term::Constant(predicate_name)),
25014                });
25015                let result = if is_negated {
25016                    self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25017                        op: TokenType::Not,
25018                        operand: identity,
25019                    })
25020                } else {
25021                    identity
25022                };
25023                return self.wrap_with_definiteness_full(&subject, result);
25024            }
25025
25026            // Handle "X is either A or B" — disjunctive predication in copula position.
25027            // "The dancer is either Tara or Bessie" → Tara(dancer) ∨ Bessie(dancer)
25028            if self.check(&TokenType::Either) {
25029                self.advance(); // consume "either"
25030                                // Parse one disjunct: an NP optionally restricted by a relative
25031                                // clause predicated of the subject ("the player who wore number
25032                                // 29" → Player(Frank) ∧ ∃e(Wore(e) ∧ Agent(e, Frank) ∧ …)).
25033                let parse_disjunct = |p: &mut Self| -> ParseResult<&'a LogicExpr<'a>> {
25034                    // The copula is consumed, so a verb-word in the disjunct NP is a
25035                    // deverbal noun head ("either Leroy's PACK or the silver PACK").
25036                    let saved_ctx = p.nominal_np_context;
25037                    p.nominal_np_context = true;
25038                    let np_result = p.parse_noun_phrase(true);
25039                    p.nominal_np_context = saved_ctx;
25040                    let np = np_result?;
25041                    // nominal_predication keeps the disjunct NP's possessor and
25042                    // adjectives ("Elodie's perfume" → Perfume(x) ∧
25043                    // Possesses(Elodie, x)) — a bare Predicate would drop them and
25044                    // collapse "either A's perfume or B's perfume" to Perfume ∨
25045                    // Perfume (a vacuous, prover-useless disjunction).
25046                    let mut pred: &'a LogicExpr<'a> =
25047                        p.nominal_predication_with_pps(Term::Constant(subject.noun), &np);
25048                    if let Some(rel) = p.try_attach_relative(Term::Constant(subject.noun))? {
25049                        pred = p.ctx.exprs.alloc(LogicExpr::BinaryOp {
25050                            left: pred,
25051                            op: TokenType::And,
25052                            right: rel,
25053                        });
25054                    }
25055                    // A reduced relative on the disjunct ("either the mountain FIRST
25056                    // CLIMBED in 1845 or …") restricts the subject — the disjunct NP
25057                    // cannot be a main clause here.
25058                    if let Some(rr) =
25059                        p.try_consume_reduced_relative(Term::Constant(subject.noun))?
25060                    {
25061                        pred = p.ctx.exprs.alloc(LogicExpr::BinaryOp {
25062                            left: pred,
25063                            op: TokenType::And,
25064                            right: rr,
25065                        });
25066                    }
25067                    Ok(pred)
25068                };
25069                let pred1 = parse_disjunct(self)?;
25070                if self.check(&TokenType::Or) {
25071                    self.advance(); // consume "or"
25072                    let pred2 = parse_disjunct(self)?;
25073                    let disj = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
25074                        left: pred1,
25075                        op: TokenType::Or,
25076                        right: pred2,
25077                    });
25078                    let with_time = if copula_time == Time::Past {
25079                        self.ctx.exprs.alloc(LogicExpr::Temporal {
25080                            operator: TemporalOperator::Past,
25081                            body: disj,
25082                        })
25083                    } else {
25084                        disj
25085                    };
25086                    let result = if is_negated {
25087                        self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25088                            op: TokenType::Not,
25089                            operand: with_time,
25090                        })
25091                    } else {
25092                        with_time
25093                    };
25094                    // Full NP so the subject's adjectives / PPs / possessor survive
25095                    // ("the class with 15 people is either …" must keep the PP).
25096                    return self.wrap_with_definiteness_full(&subject, result);
25097                }
25098            }
25099
25100            // Handle "X is from Y" / "X is on Y" — PP predicate in copula position.
25101            // Excludes "by" which is passive-agent and handled above.
25102            if self.check_preposition() && !self.check_by_preposition() {
25103                let prep_token = self.advance().clone();
25104                let prep_sym = match prep_token.kind {
25105                    TokenType::Preposition(s) => s,
25106                    _ => unreachable!("guarded by check_preposition()"),
25107                };
25108                // A numeric PP object is a measure ("is at 40.5912 N", "is at 385
25109                // degrees") — keep the amount and its unit/direction; otherwise an
25110                // ordinary NP object.
25111                let base = if self.check_number() {
25112                    let m = self.parse_measure_phrase()?;
25113                    self.ctx.exprs.alloc(LogicExpr::Predicate {
25114                        name: prep_sym,
25115                        args: self
25116                            .ctx
25117                            .terms
25118                            .alloc_slice([Term::Constant(subject.noun), *m]),
25119                        world: None,
25120                    })
25121                } else {
25122                    // A copula + preposition rules out the matrix verb, so a
25123                    // verb-tagged head in the PP object is a deverbal noun ("was
25124                    // after the skydiving TRIP", "is from New Guinea").
25125                    let saved_ctx = self.nominal_np_context;
25126                    self.nominal_np_context = true;
25127                    let pp_obj_result = self.parse_noun_phrase(true);
25128                    self.nominal_np_context = saved_ctx;
25129                    let pp_obj = pp_obj_result?;
25130                    let pred: &'a LogicExpr<'a> = self.ctx.exprs.alloc(LogicExpr::Predicate {
25131                        name: prep_sym,
25132                        args: self.ctx.terms.alloc_slice([
25133                            Term::Constant(subject.noun),
25134                            Term::Constant(pp_obj.noun),
25135                        ]),
25136                        world: None,
25137                    });
25138                    // The PP object's own ADJECTIVES ("is on the BIG table" →
25139                    // On(x, Table) ∧ Big(Table)) and nested PPs survive via the shared
25140                    // helper, dropping them would be meaning loss.
25141                    self.attach_pp_object_modifiers(pred, &pp_obj)
25142                };
25143                // Disjunctive PP complement — "is in Florida OR in Maine" / "is in
25144                // A or B" → In(x,A) ∨ In(x,B): the domain-closure shape a logic-grid
25145                // bijection eliminates over ("not Florida ⇒ Maine"). "or in B"
25146                // repeats the preposition; "or B" reuses the first one.
25147                let mut base: &'a LogicExpr<'a> = base;
25148                while self.check(&TokenType::Or) {
25149                    let cp = self.checkpoint();
25150                    self.advance(); // "or"
25151                    let disj_prep = if self.check_preposition() && !self.check_by_preposition() {
25152                        match self.advance().kind {
25153                            TokenType::Preposition(s) => s,
25154                            _ => prep_sym,
25155                        }
25156                    } else {
25157                        prep_sym
25158                    };
25159                    if !(self.check_content_word()
25160                        || self.check_number()
25161                        || matches!(self.peek().kind, TokenType::Article(_)))
25162                    {
25163                        self.restore(cp);
25164                        break;
25165                    }
25166                    let saved_ctx = self.nominal_np_context;
25167                    self.nominal_np_context = true;
25168                    let disj_obj_res = self.parse_noun_phrase(true);
25169                    self.nominal_np_context = saved_ctx;
25170                    let disj_obj = disj_obj_res?;
25171                    let disj_pred: &'a LogicExpr<'a> = self.ctx.exprs.alloc(LogicExpr::Predicate {
25172                        name: disj_prep,
25173                        args: self.ctx.terms.alloc_slice([
25174                            Term::Constant(subject.noun),
25175                            Term::Constant(disj_obj.noun),
25176                        ]),
25177                        world: None,
25178                    });
25179                    let disj_pred = self.attach_pp_object_modifiers(disj_pred, &disj_obj);
25180                    base = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
25181                        left: base,
25182                        op: TokenType::Or,
25183                        right: disj_pred,
25184                    });
25185                }
25186                let with_time = if copula_time == Time::Past {
25187                    self.ctx.exprs.alloc(LogicExpr::Temporal {
25188                        operator: TemporalOperator::Past,
25189                        body: base,
25190                    })
25191                } else {
25192                    base
25193                };
25194                let result = if is_negated {
25195                    self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25196                        op: TokenType::Not,
25197                        operand: with_time,
25198                    })
25199                } else {
25200                    with_time
25201                };
25202                // Use the full NP so a subject genitive ("The Alvarado family's
25203                // house is on Cora Street.") keeps its possession presupposition.
25204                return self.wrap_with_definiteness_full(&subject, result);
25205            }
25206
25207            // Copula complement led by a temporal/ordinal adverb: "was FIRST", "is
25208            // NOW the leader". Shared with parse_predicate's copula path; wrapped here
25209            // with parse_atom's own tense / negation / definiteness.
25210            if let Some(base) =
25211                self.copula_temporal_adverb_complement(Term::Constant(subject.noun))?
25212            {
25213                let with_time = if copula_time == Time::Past {
25214                    self.ctx.exprs.alloc(LogicExpr::Temporal {
25215                        operator: TemporalOperator::Past,
25216                        body: base,
25217                    })
25218                } else {
25219                    base
25220                };
25221                let result = if is_negated {
25222                    self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25223                        op: TokenType::Not,
25224                        operand: with_time,
25225                    })
25226                } else {
25227                    with_time
25228                };
25229                return self.wrap_with_definiteness_full(&subject, result);
25230            }
25231
25232            // Handle "The king is bald" or "Alice is not guilty" - NP copula (not)? ADJ/NOUN
25233            // Also handles bare noun predicates like "Time is money"
25234            let predicate_name = self.consume_content_word()?;
25235
25236            // Coordinated predicate adjectives — "is black and red" → Black(x) ∧
25237            // Red(x). The copula complement must consume the whole coordination;
25238            // otherwise "and red" is mis-parsed as a separate clause. Requires an
25239            // "and" before each extra adjective (a bare juxtaposed pair is left
25240            // alone — it may be a compound shade like "dark green").
25241            {
25242                let mut coord_adjs: Vec<Symbol> = vec![predicate_name];
25243                while self.check(&TokenType::And) {
25244                    let cp = self.checkpoint();
25245                    self.advance(); // "and"
25246                    if let TokenType::Adjective(a) = self.peek().kind {
25247                        // "and ADJ is/are/verb …" — ADJ is the SUBJECT of a new
25248                        // clause ("valid is high AND ready is high"), not a
25249                        // coordinated predicate adjective. Only a clause-final
25250                        // adjective ("is black AND red.") coordinates.
25251                        if matches!(
25252                            self.tokens.get(self.current + 1).map(|t| &t.kind),
25253                            Some(TokenType::Is)
25254                                | Some(TokenType::Are)
25255                                | Some(TokenType::Was)
25256                                | Some(TokenType::Were)
25257                                | Some(TokenType::Verb { .. })
25258                        ) {
25259                            self.restore(cp);
25260                            break;
25261                        }
25262                        self.advance();
25263                        coord_adjs.push(a);
25264                    } else {
25265                        self.restore(cp); // "and X" is not adjective coordination
25266                        break;
25267                    }
25268                }
25269                if coord_adjs.len() > 1 {
25270                    let mut conj: &'a LogicExpr<'a> = self.ctx.exprs.alloc(LogicExpr::Predicate {
25271                        name: coord_adjs[0],
25272                        args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
25273                        world: None,
25274                    });
25275                    for &a in &coord_adjs[1..] {
25276                        let p = self.ctx.exprs.alloc(LogicExpr::Predicate {
25277                            name: a,
25278                            args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
25279                            world: None,
25280                        });
25281                        conj = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
25282                            left: conj,
25283                            op: TokenType::And,
25284                            right: p,
25285                        });
25286                    }
25287                    let conj = if is_negated {
25288                        self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25289                            op: TokenType::Not,
25290                            operand: conj,
25291                        })
25292                    } else {
25293                        conj
25294                    };
25295                    let conj = if copula_time == Time::Past {
25296                        self.ctx.exprs.alloc(LogicExpr::Temporal {
25297                            operator: TemporalOperator::Past,
25298                            body: conj,
25299                        })
25300                    } else {
25301                        conj
25302                    };
25303                    return self.wrap_with_definiteness_full(&subject, conj);
25304                }
25305            }
25306
25307            // A predicate adjective taking a POSTPOSED measure complement — "is
25308            // worth $26 billion", "is worth 5 dollars". The measure-BEFORE-adjective
25309            // form ("is 5 feet tall") is handled earlier; here the amount follows
25310            // the adjective and is its degree argument: Worth(subject, $26 billion).
25311            // A number after a predicate adjective is never a separate clause.
25312            if self.check_number() {
25313                let measure = self.parse_measure_phrase()?;
25314                let pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
25315                    name: predicate_name,
25316                    args: self
25317                        .ctx
25318                        .terms
25319                        .alloc_slice([Term::Constant(subject.noun), *measure]),
25320                    world: None,
25321                });
25322                let pred = if is_negated {
25323                    self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25324                        op: TokenType::Not,
25325                        operand: pred,
25326                    })
25327                } else {
25328                    pred
25329                };
25330                let pred = if copula_time == Time::Past {
25331                    self.ctx.exprs.alloc(LogicExpr::Temporal {
25332                        operator: TemporalOperator::Past,
25333                        body: pred,
25334                    })
25335                } else {
25336                    pred
25337                };
25338                return self.wrap_with_definiteness_full(&subject, pred);
25339            }
25340
25341            // Check for sort violation (metaphor detection)
25342            let subject_sort = lexicon::lookup_sort(self.interner.resolve(subject.noun));
25343            let predicate_str = self.interner.resolve(predicate_name);
25344
25345            // Check ontology's predicate sort requirements (for adjectives like "happy")
25346            if let Some(s_sort) = subject_sort {
25347                if !crate::ontology::check_sort_compatibility(predicate_str, s_sort) {
25348                    let metaphor = self.ctx.exprs.alloc(LogicExpr::Metaphor {
25349                        tenor: self.ctx.terms.alloc(Term::Constant(subject.noun)),
25350                        vehicle: self.ctx.terms.alloc(Term::Constant(predicate_name)),
25351                    });
25352                    return self.wrap_with_definiteness_full(&subject, metaphor);
25353                }
25354            }
25355
25356            // Check copular NP predicate sort compatibility (for "Time is money")
25357            let predicate_sort = lexicon::lookup_sort(predicate_str);
25358            if let (Some(s_sort), Some(p_sort)) = (subject_sort, predicate_sort) {
25359                if s_sort != p_sort
25360                    && !s_sort.is_compatible_with(p_sort)
25361                    && !p_sort.is_compatible_with(s_sort)
25362                {
25363                    let metaphor = self.ctx.exprs.alloc(LogicExpr::Metaphor {
25364                        tenor: self.ctx.terms.alloc(Term::Constant(subject.noun)),
25365                        vehicle: self.ctx.terms.alloc(Term::Constant(predicate_name)),
25366                    });
25367                    return self.wrap_with_definiteness_full(&subject, metaphor);
25368                }
25369            }
25370
25371            // Bare gradable predication (§7.2) relies on a contextual standard: a
25372            // GRADABLE adjective ("tall", "heavy" — lexicon-tagged) is `∃d(Adj(x,d)
25373            // ∧ d > θ_C)`, exceeding a context standard θ_C, not a crisp predicate.
25374            let predicate = if self.pragmatic
25375                && crate::lexicon::is_gradable_adjective(
25376                    &self.interner.resolve(predicate_name).to_lowercase(),
25377                ) {
25378                let d = self.next_var_name();
25379                let theta = self.interner.intern("θ_C");
25380                let adj_d = self.ctx.exprs.alloc(LogicExpr::Predicate {
25381                    name: predicate_name,
25382                    args: self
25383                        .ctx
25384                        .terms
25385                        .alloc_slice([Term::Constant(subject.noun), Term::Variable(d)]),
25386                    world: None,
25387                });
25388                let exceeds = self.ctx.exprs.alloc(LogicExpr::Predicate {
25389                    name: self.interner.intern(">"),
25390                    args: self
25391                        .ctx
25392                        .terms
25393                        .alloc_slice([Term::Variable(d), Term::Constant(theta)]),
25394                    world: None,
25395                });
25396                let mut body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
25397                    left: adj_d,
25398                    op: TokenType::And,
25399                    right: exceeds,
25400                });
25401                // Vagueness (§8.5): a vague adjective's threshold has a PENUMBRA —
25402                // a borderline region — marked Borderline(θ_C). Sorites-susceptible.
25403                if crate::lexicon::is_vague_adjective(
25404                    &self.interner.resolve(predicate_name).to_lowercase(),
25405                ) {
25406                    let borderline = self.ctx.exprs.alloc(LogicExpr::Predicate {
25407                        name: self.interner.intern("Borderline"),
25408                        args: self.ctx.terms.alloc_slice([Term::Constant(theta)]),
25409                        world: None,
25410                    });
25411                    body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
25412                        left: body,
25413                        op: TokenType::And,
25414                        right: borderline,
25415                    });
25416                }
25417                self.ctx.exprs.alloc(LogicExpr::Quantifier {
25418                    kind: QuantifierKind::Existential,
25419                    variable: d,
25420                    body,
25421                    island_id: self.current_island,
25422                })
25423            } else {
25424                self.ctx.exprs.alloc(LogicExpr::Predicate {
25425                    name: predicate_name,
25426                    args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
25427                    world: None,
25428                })
25429            };
25430
25431            // Apply negation if "is not"
25432            let result = if is_negated {
25433                self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25434                    op: TokenType::Not,
25435                    operand: predicate,
25436                })
25437            } else {
25438                predicate
25439            };
25440
25441            // Apply temporal wrapper for "is always Y" → G(Y(X)) or "is never Y" → G(¬Y(X))
25442            let result = match copula_temporal {
25443                Some(CopulaTemporal::Always) => self.ctx.exprs.alloc(LogicExpr::Temporal {
25444                    operator: TemporalOperator::Always,
25445                    body: result,
25446                }),
25447                Some(CopulaTemporal::Never) => {
25448                    let negated = self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25449                        op: TokenType::Not,
25450                        operand: predicate,
25451                    });
25452                    self.ctx.exprs.alloc(LogicExpr::Temporal {
25453                        operator: TemporalOperator::Always,
25454                        body: negated,
25455                    })
25456                }
25457                Some(CopulaTemporal::Eventually) => self.ctx.exprs.alloc(LogicExpr::Temporal {
25458                    operator: TemporalOperator::Eventually,
25459                    body: result,
25460                }),
25461                None => result,
25462            };
25463
25464            // Use the full NP so the subject's adjectives/PPs are applied to the
25465            // restriction — copular predication must not drop them (e.g. the
25466            // adjective in "A red car is fast." → ∃x((Car(x) ∧ Red(x)) ∧ Fast(x))).
25467            return self.wrap_with_definiteness_full(&subject, result);
25468        }
25469
25470        // "did" as a MAIN verb ("Philip did the dishes", "the diver did 49
25471        // previous jumps") — an Auxiliary(Past) followed by an object NP, not
25472        // do-support. parse_predicate_impl (verb.rs) already handles this; the
25473        // simple-clause path here did not, so proper-name-subject "did X"
25474        // stranded. (Object NPs only; "did not"/"did run" stay auxiliary.)
25475        if self.check_auxiliary_as_main_verb() {
25476            let subject_term = self.noun_phrase_to_term(&subject);
25477            let result = self.parse_do_as_main_verb(subject_term)?;
25478            return self.wrap_with_definiteness_full(&subject, result);
25479        }
25480
25481        // Handle auxiliary: set pending_time, handle negation
25482        // BUT: "did it" should be parsed as verb "do" with object "it"
25483        // We lookahead to check if this is truly an auxiliary usage
25484        if self.check_auxiliary() && self.is_true_auxiliary_usage() {
25485            let aux_time = if let TokenType::Auxiliary(time) = self.advance().kind {
25486                time
25487            } else {
25488                Time::None
25489            };
25490            self.pending_time = Some(aux_time);
25491
25492            // Handle negation: "John did not see dogs"
25493            if self.match_token(&[TokenType::Not]) {
25494                self.negative_depth += 1;
25495
25496                // Skip "ever" if present: "John did not ever run"
25497                if self.check(&TokenType::Ever) {
25498                    self.advance();
25499                }
25500
25501                // Presupposition trigger under negation: the presupposition PROJECTS
25502                // through the ¬ (Van der Sandt). "Mary does not regret lying." →
25503                // ¬Regret(Mary) [Presup: P(Lie(Mary))] — the lying survives.
25504                if self.check_presup_trigger()
25505                    && !self.is_followed_by_np_object()
25506                    && self.is_followed_by_gerund()
25507                {
25508                    let presup_kind = self.consume_presup_trigger();
25509                    self.negative_depth -= 1; // parse_presupposition handles the negation
25510                    return self.parse_presupposition(&subject, presup_kind, true);
25511                }
25512
25513                // A bare verb the lexicon ALSO lists as a performative ("didn't ORDER")
25514                // is the clause's main verb after do-support, not a speech act — re-tag
25515                // it so check_verb consumes it (tense comes from the "did" auxiliary).
25516                if let TokenType::Performative(_) = self.peek().kind {
25517                    let lemma = self
25518                        .interner
25519                        .intern(&self.interner.resolve(self.peek().lexeme).to_lowercase());
25520                    self.tokens[self.current].kind = TokenType::Verb {
25521                        lemma,
25522                        time: Time::None,
25523                        aspect: Aspect::Simple,
25524                        class: crate::lexicon::VerbClass::Activity,
25525                    };
25526                }
25527                // Check for verb or "do" (TokenType::Do is separate from TokenType::Verb)
25528                if self.check_verb() || self.check(&TokenType::Do) {
25529                    let (verb, verb_time, verb_aspect, verb_class) = if self.check(&TokenType::Do) {
25530                        self.advance(); // consume "do"
25531                        (
25532                            self.interner.intern("Do"),
25533                            Time::None,
25534                            Aspect::Simple,
25535                            crate::lexicon::VerbClass::Activity,
25536                        )
25537                    } else {
25538                        self.consume_verb_with_metadata()
25539                    };
25540                    let subject_term = self.noun_phrase_to_term(&subject);
25541
25542                    // Check for NPI object first: "John did not see anything"
25543                    if self.check_npi_object() {
25544                        let npi_token = self.advance().kind.clone();
25545                        let obj_var = self.next_var_name();
25546
25547                        let restriction_name = match npi_token {
25548                            TokenType::Anything => "Thing",
25549                            TokenType::Anyone => "Person",
25550                            _ => "Thing",
25551                        };
25552
25553                        let restriction_sym = self.interner.intern(restriction_name);
25554                        let obj_restriction = self.ctx.exprs.alloc(LogicExpr::Predicate {
25555                            name: restriction_sym,
25556                            args: self.ctx.terms.alloc_slice([Term::Variable(obj_var)]),
25557                            world: None,
25558                        });
25559
25560                        let verb_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
25561                            name: verb,
25562                            args: self
25563                                .ctx
25564                                .terms
25565                                .alloc_slice([subject_term.clone(), Term::Variable(obj_var)]),
25566                            world: None,
25567                        });
25568
25569                        let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
25570                            left: obj_restriction,
25571                            op: TokenType::And,
25572                            right: verb_pred,
25573                        });
25574
25575                        let quantified = self.ctx.exprs.alloc(LogicExpr::Quantifier {
25576                            kind: QuantifierKind::Existential,
25577                            variable: obj_var,
25578                            body,
25579                            island_id: self.current_island,
25580                        });
25581
25582                        let effective_time = self.pending_time.take().unwrap_or(Time::None);
25583                        let with_time = match effective_time {
25584                            Time::Past => self.ctx.exprs.alloc(LogicExpr::Temporal {
25585                                operator: TemporalOperator::Past,
25586                                body: quantified,
25587                            }),
25588                            Time::Future => self.ctx.exprs.alloc(LogicExpr::Temporal {
25589                                operator: TemporalOperator::Future,
25590                                body: quantified,
25591                            }),
25592                            _ => quantified,
25593                        };
25594
25595                        self.negative_depth -= 1;
25596                        return Ok(self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25597                            op: TokenType::Not,
25598                            operand: with_time,
25599                        }));
25600                    }
25601
25602                    // Check for quantifier object: "John did not see any dogs"
25603                    if self.check_quantifier() {
25604                        let quantifier_token = self.advance().kind.clone();
25605                        let object_np = self.parse_noun_phrase(false)?;
25606                        let obj_var = self.next_var_name();
25607
25608                        let obj_restriction = self.ctx.exprs.alloc(LogicExpr::Predicate {
25609                            name: object_np.noun,
25610                            args: self.ctx.terms.alloc_slice([Term::Variable(obj_var)]),
25611                            world: None,
25612                        });
25613
25614                        let verb_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
25615                            name: verb,
25616                            args: self
25617                                .ctx
25618                                .terms
25619                                .alloc_slice([subject_term.clone(), Term::Variable(obj_var)]),
25620                            world: None,
25621                        });
25622
25623                        let quantified =
25624                            self.quantify_argument(&quantifier_token, obj_var, obj_restriction, verb_pred);
25625
25626                        let effective_time = self.pending_time.take().unwrap_or(Time::None);
25627                        let with_time = match effective_time {
25628                            Time::Past => self.ctx.exprs.alloc(LogicExpr::Temporal {
25629                                operator: TemporalOperator::Past,
25630                                body: quantified,
25631                            }),
25632                            Time::Future => self.ctx.exprs.alloc(LogicExpr::Temporal {
25633                                operator: TemporalOperator::Future,
25634                                body: quantified,
25635                            }),
25636                            _ => quantified,
25637                        };
25638
25639                        self.negative_depth -= 1;
25640                        return Ok(self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25641                            op: TokenType::Not,
25642                            operand: with_time,
25643                        }));
25644                    }
25645
25646                    // A pronoun object ("did not do it") stays LITERAL, and the
25647                    // negation is returned UNWRAPPED. Resolving the pronoun or
25648                    // wrapping the definite subject in a uniqueness clause would
25649                    // desync a modus-tollens goal from the conditional antecedent it
25650                    // must refute ("if the butler did it …" keeps Theme(e, it)).
25651                    if self.check_pronoun() {
25652                        let pronoun_token = self.advance();
25653                        let pronoun_sym = pronoun_token.lexeme;
25654                        let roles = vec![
25655                            (ThematicRole::Agent, subject_term.clone()),
25656                            (ThematicRole::Theme, Term::Constant(pronoun_sym)),
25657                        ];
25658                        let effective_time = self.pending_time.take().unwrap_or(Time::None);
25659                        let mut modifiers: Vec<Symbol> = vec![];
25660                        match effective_time {
25661                            Time::Past => modifiers.push(self.interner.intern("Past")),
25662                            Time::Future => modifiers.push(self.interner.intern("Future")),
25663                            _ => {}
25664                        }
25665                        let event_var = self.get_event_var();
25666                        let suppress_existential = self.drs.in_conditional_antecedent();
25667                        let neo_event =
25668                            self.ctx
25669                                .exprs
25670                                .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
25671                                    event_var,
25672                                    verb,
25673                                    roles: self.ctx.roles.alloc_slice(roles),
25674                                    modifiers: self.ctx.syms.alloc_slice(modifiers),
25675                                    suppress_existential,
25676                                    world: None,
25677                                })));
25678                        self.negative_depth -= 1;
25679                        return Ok(self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25680                            op: TokenType::Not,
25681                            operand: neo_event,
25682                        }));
25683                    }
25684
25685                    // The remaining complement forms (NP / measure / PP / aspect)
25686                    // fold through the shared finite-verb builder so "did not VERB …"
25687                    // folds exactly like the positive path; the auxiliary tense rides
25688                    // in via pending_time. The result is returned UNWRAPPED — a
25689                    // negated definite subject is not given a uniqueness clause here,
25690                    // keeping proof goals structurally clean.
25691                    let vp = self.build_verb_vp(
25692                        subject.noun,
25693                        subject_term,
25694                        false,
25695                        verb,
25696                        verb_time,
25697                        verb_aspect,
25698                        verb_class,
25699                    )?;
25700
25701                    self.negative_depth -= 1;
25702                    return Ok(self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25703                        op: TokenType::Not,
25704                        operand: vp,
25705                    }));
25706                }
25707
25708                self.negative_depth -= 1;
25709            }
25710            // Non-negated auxiliary: pending_time is set, fall through to normal verb handling
25711        }
25712
25713        // Check for presupposition triggers: "stopped", "started", "regrets", "knows"
25714        // Factive verbs like "know" only trigger presupposition with clausal complements
25715        // "John knows that..." → presupposition, "John knows Mary" → regular verb
25716        // Only trigger presupposition if followed by a gerund (e.g., "stopped smoking")
25717        // "John stopped." alone should parse as intransitive verb, not presupposition
25718        if self.check_presup_trigger()
25719            && !self.is_followed_by_np_object()
25720            && self.is_followed_by_gerund()
25721        {
25722            let presup_kind = self.consume_presup_trigger();
25723            return self.parse_presupposition(&subject, presup_kind, false);
25724        }
25725
25726        // Handle bare plurals: "Birds fly." → Gen x. Bird(x) → Fly(x)
25727        let noun_str = self.interner.resolve(subject.noun);
25728        // Only a KNOWN plural common noun is a generic restrictor — an
25729        // unknown s-final proper name ("Opus flies.") is not a bare plural.
25730        let is_known_plural = {
25731            let lower = noun_str.to_lowercase();
25732            Self::is_plural_noun(noun_str)
25733                && (crate::lexicon::is_common_noun(&lower)
25734                    || matches!(
25735                        crate::lexicon::analyze_word(&lower),
25736                        Some(crate::lexicon::WordAnalysis::Noun(_))
25737                    ))
25738        };
25739        // GEN also covers do-negated characterizing sentences
25740        // ("Penguins do not fly."); the copular kind-predication case
25741        // ("Penguins are birds.") is intercepted at the copular dispatch.
25742        let generic_do_not = matches!(self.peek().kind, TokenType::Does | TokenType::Do)
25743            && matches!(
25744                self.tokens.get(self.current + 1).map(|t| &t.kind),
25745                Some(TokenType::Not)
25746            );
25747        let is_bare_plural = subject.definiteness.is_none()
25748            && subject.possessor.is_none()
25749            && is_known_plural
25750            && (self.check_verb() || generic_do_not);
25751
25752        if is_bare_plural && generic_do_not {
25753            // "Penguins do not fly." → Gen x(Penguin(x) → ¬∃e(Fly(e) ∧ Agent(e, x))).
25754            // The nucleus is a real event so the generic matches what an
25755            // instance sentence ("Opus does not fly.") parses to.
25756            self.advance(); // do/does
25757            self.advance(); // not
25758            let var_name = self.next_var_name();
25759            let (verb, _t, _a, _) = self.consume_verb_with_metadata();
25760            let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
25761                name: subject.noun,
25762                args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
25763                world: None,
25764            });
25765            let event_var = self.get_event_var();
25766            let verb_pred = self
25767                .ctx
25768                .exprs
25769                .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
25770                    event_var,
25771                    verb,
25772                    roles: self
25773                        .ctx
25774                        .roles
25775                        .alloc_slice(vec![(ThematicRole::Agent, Term::Variable(var_name))]),
25776                    modifiers: self.ctx.syms.alloc_slice(vec![]),
25777                    suppress_existential: false,
25778                    world: None,
25779                })));
25780            let negated = self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25781                op: TokenType::Not,
25782                operand: verb_pred,
25783            });
25784            let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
25785                left: type_pred,
25786                op: TokenType::Implies,
25787                right: negated,
25788            });
25789            return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
25790                kind: QuantifierKind::Generic,
25791                variable: var_name,
25792                body,
25793                island_id: self.current_island,
25794            }));
25795        }
25796
25797        if is_bare_plural {
25798            let var_name = self.next_var_name();
25799            let (verb, verb_time, verb_aspect, _) = self.consume_verb_with_metadata();
25800
25801            let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
25802                name: subject.noun,
25803                args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
25804                world: None,
25805            });
25806
25807            // The generic nucleus is a real event — the same shape an
25808            // instance sentence parses to, so GEN defaults are derivable for
25809            // individuals ("Birds fly." + Bird(Tweety) ⊨~ Tweety flies).
25810            let mut roles = vec![(ThematicRole::Agent, Term::Variable(var_name))];
25811            if self.check_content_word() {
25812                let object = self.parse_noun_phrase(false)?;
25813                roles.push((ThematicRole::Theme, self.noun_phrase_to_term(&object)));
25814            }
25815
25816            // A trailing manner adverb attaches to the generic nucleus's event
25817            // ("Dogs bark loudly." → Gen x(Dog(x) → ∃e(Bark(e) ∧ … ∧ Loudly(e)))),
25818            // the same event modifier an instance sentence produces.
25819            let manner_adverbs = self.collect_adverbs();
25820
25821            let event_var = self.get_event_var();
25822            let verb_pred = self
25823                .ctx
25824                .exprs
25825                .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
25826                    event_var,
25827                    verb,
25828                    roles: self.ctx.roles.alloc_slice(roles),
25829                    modifiers: self.ctx.syms.alloc_slice(manner_adverbs),
25830                    suppress_existential: false,
25831                    world: None,
25832                })));
25833
25834            let effective_time = self.pending_time.take().unwrap_or(verb_time);
25835            let with_time = match effective_time {
25836                Time::Past => self.ctx.exprs.alloc(LogicExpr::Temporal {
25837                    operator: TemporalOperator::Past,
25838                    body: verb_pred,
25839                }),
25840                Time::Future => self.ctx.exprs.alloc(LogicExpr::Temporal {
25841                    operator: TemporalOperator::Future,
25842                    body: verb_pred,
25843                }),
25844                _ => verb_pred,
25845            };
25846
25847            let with_aspect = if verb_aspect == Aspect::Progressive {
25848                self.ctx.exprs.alloc(LogicExpr::Aspectual {
25849                    operator: AspectOperator::Progressive,
25850                    body: with_time,
25851                })
25852            } else {
25853                with_time
25854            };
25855
25856            let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
25857                left: type_pred,
25858                op: TokenType::Implies,
25859                right: with_aspect,
25860            });
25861
25862            return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
25863                kind: QuantifierKind::Generic,
25864                variable: var_name,
25865                body,
25866                island_id: self.current_island,
25867            }));
25868        }
25869
25870        // Handle do-support: "John does not exist" or "John does run"
25871        if self.check(&TokenType::Does) || self.check(&TokenType::Do) {
25872            self.advance(); // consume does/do
25873            let is_negated = self.match_token(&[TokenType::Not]);
25874
25875            // Presupposition trigger under do-support negation projects (Van der
25876            // Sandt): "Mary does not regret lying." → ¬Regret(Mary) [Presup: Lie(Mary)].
25877            if self.check_presup_trigger()
25878                && !self.is_followed_by_np_object()
25879                && self.is_followed_by_gerund()
25880            {
25881                let presup_kind = self.consume_presup_trigger();
25882                return self.parse_presupposition(&subject, presup_kind, is_negated);
25883            }
25884
25885            if self.check_verb() {
25886                let (verb, verb_time, verb_aspect, verb_class) = self.consume_verb_with_metadata();
25887                let verb_lemma = self.interner.resolve(verb).to_lowercase();
25888
25889                // Check for embedded wh-clause with negation: "I don't know who"
25890                if self.check_wh_word() {
25891                    let wh_token = self.advance().kind.clone();
25892                    let is_who = matches!(wh_token, TokenType::Who);
25893                    let is_what = matches!(wh_token, TokenType::What);
25894
25895                    let is_sluicing = self.is_at_end()
25896                        || self.check(&TokenType::Period)
25897                        || self.check(&TokenType::Comma);
25898
25899                    if is_sluicing {
25900                        if let Some(template) = self.last_event_template.clone() {
25901                            let wh_var = self.next_var_name();
25902                            let subject_term = self.noun_phrase_to_term(&subject);
25903
25904                            let roles: Vec<_> = if is_who {
25905                                std::iter::once((ThematicRole::Agent, Term::Variable(wh_var)))
25906                                    .chain(template.non_agent_roles.iter().cloned())
25907                                    .collect()
25908                            } else if is_what {
25909                                vec![
25910                                    (ThematicRole::Agent, subject_term.clone()),
25911                                    (ThematicRole::Theme, Term::Variable(wh_var)),
25912                                ]
25913                            } else {
25914                                std::iter::once((ThematicRole::Agent, Term::Variable(wh_var)))
25915                                    .chain(template.non_agent_roles.iter().cloned())
25916                                    .collect()
25917                            };
25918
25919                            let event_var = self.get_event_var();
25920                            let suppress_existential = self.drs.in_conditional_antecedent();
25921                            if suppress_existential {
25922                                let event_class = self.interner.intern("Event");
25923                                self.drs.introduce_event_referent(event_var, event_class);
25924                            }
25925                            let reconstructed =
25926                                self.ctx
25927                                    .exprs
25928                                    .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
25929                                        event_var,
25930                                        verb: template.verb,
25931                                        roles: self.ctx.roles.alloc_slice(roles),
25932                                        modifiers: self
25933                                            .ctx
25934                                            .syms
25935                                            .alloc_slice(template.modifiers.clone()),
25936                                        suppress_existential,
25937                                        world: None,
25938                                    })));
25939
25940                            let question = self.ctx.exprs.alloc(LogicExpr::Question {
25941                                wh_variable: wh_var,
25942                                body: reconstructed,
25943                            });
25944
25945                            let know_event_var = self.get_event_var();
25946                            let suppress_existential2 = self.drs.in_conditional_antecedent();
25947                            if suppress_existential2 {
25948                                let event_class = self.interner.intern("Event");
25949                                self.drs
25950                                    .introduce_event_referent(know_event_var, event_class);
25951                            }
25952                            let know_event =
25953                                self.ctx
25954                                    .exprs
25955                                    .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
25956                                        event_var: know_event_var,
25957                                        verb,
25958                                        roles: self.ctx.roles.alloc_slice(vec![
25959                                            (ThematicRole::Agent, subject_term),
25960                                            (ThematicRole::Theme, Term::Proposition(question)),
25961                                        ]),
25962                                        modifiers: self.ctx.syms.alloc_slice(vec![]),
25963                                        suppress_existential: suppress_existential2,
25964                                        world: None,
25965                                    })));
25966
25967                            let result = if is_negated {
25968                                self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25969                                    op: TokenType::Not,
25970                                    operand: know_event,
25971                                })
25972                            } else {
25973                                know_event
25974                            };
25975
25976                            return self.wrap_with_definiteness_full(&subject, result);
25977                        }
25978                    }
25979                }
25980
25981                // Special handling for "exist" with negation
25982                if verb_lemma == "exist" && is_negated {
25983                    // "The King of France does not exist" -> ¬∃x(KingOfFrance(x))
25984                    let var_name = self.next_var_name();
25985                    let restriction = self.ctx.exprs.alloc(LogicExpr::Predicate {
25986                        name: subject.noun,
25987                        args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
25988                        world: None,
25989                    });
25990                    let exists = self.ctx.exprs.alloc(LogicExpr::Quantifier {
25991                        kind: QuantifierKind::Existential,
25992                        variable: var_name,
25993                        body: restriction,
25994                        island_id: self.current_island,
25995                    });
25996                    return Ok(self.ctx.exprs.alloc(LogicExpr::UnaryOp {
25997                        op: TokenType::Not,
25998                        operand: exists,
25999                    }));
26000                }
26001
26002                // Regular do-support: "John does run" or "John does not run"
26003                // Also handles transitive: "John does not shave any man"
26004                let subject_term = self.noun_phrase_to_term(&subject);
26005                let modifiers: Vec<Symbol> = vec![];
26006
26007                // Check for reflexive object
26008                if self.check(&TokenType::Reflexive) {
26009                    self.advance();
26010                    let roles = vec![
26011                        (ThematicRole::Agent, subject_term.clone()),
26012                        (ThematicRole::Theme, subject_term),
26013                    ];
26014                    let event_var = self.get_event_var();
26015                    let suppress_existential = self.drs.in_conditional_antecedent();
26016                    if suppress_existential {
26017                        let event_class = self.interner.intern("Event");
26018                        self.drs.introduce_event_referent(event_var, event_class);
26019                    }
26020                    let neo_event =
26021                        self.ctx
26022                            .exprs
26023                            .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
26024                                event_var,
26025                                verb,
26026                                roles: self.ctx.roles.alloc_slice(roles),
26027                                modifiers: self.ctx.syms.alloc_slice(modifiers),
26028                                suppress_existential,
26029                                world: None,
26030                            })));
26031
26032                    let result = if is_negated {
26033                        self.ctx.exprs.alloc(LogicExpr::UnaryOp {
26034                            op: TokenType::Not,
26035                            operand: neo_event,
26036                        })
26037                    } else {
26038                        neo_event
26039                    };
26040                    return self.wrap_with_definiteness_full(&subject, result);
26041                }
26042
26043                // Check for quantified object: "does not shave any man"
26044                if self.check_npi_quantifier() || self.check_quantifier() || self.check_article() {
26045                    let (obj_quantifier, was_definite_article) = if self.check_npi_quantifier() {
26046                        // "any" is an NPI quantifier in negative contexts
26047                        let tok = self.advance().kind.clone();
26048                        (Some(tok), false)
26049                    } else if self.check_quantifier() {
26050                        (Some(self.advance().kind.clone()), false)
26051                    } else {
26052                        let art = self.advance().kind.clone();
26053                        if let TokenType::Article(def) = art {
26054                            if def == Definiteness::Indefinite {
26055                                (Some(TokenType::Some), false)
26056                            } else {
26057                                (None, true)
26058                            }
26059                        } else {
26060                            (None, false)
26061                        }
26062                    };
26063
26064                    let object_np = self.parse_noun_phrase(false)?;
26065                    let obj_var = self.next_var_name();
26066
26067                    let mut type_pred: &'a LogicExpr<'a> =
26068                        self.ctx.exprs.alloc(LogicExpr::Predicate {
26069                            name: object_np.noun,
26070                            args: self.ctx.terms.alloc_slice([Term::Variable(obj_var)]),
26071                            world: None,
26072                        });
26073                    // A quantified object's adjectives and PP restrictors ("has a
26074                    // RED book", "has a maximum range OF 475 ft") are part of the
26075                    // object's description — dropping them is a meaning-loss parse.
26076                    for &adj in object_np.adjectives {
26077                        let adj_pred = self.adjective_restriction(adj, obj_var, object_np.noun);
26078                        type_pred = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
26079                            left: type_pred,
26080                            op: TokenType::And,
26081                            right: adj_pred,
26082                        });
26083                    }
26084                    for pp in object_np.pps {
26085                        let pp_sub = self.substitute_pp_placeholder(pp, obj_var);
26086                        type_pred = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
26087                            left: type_pred,
26088                            op: TokenType::And,
26089                            right: pp_sub,
26090                        });
26091                    }
26092
26093                    // Check for relative clause on object
26094                    let obj_restriction =
26095                        if self.check(&TokenType::That) || self.check(&TokenType::Who) {
26096                            self.advance();
26097                            let rel_clause = self.parse_relative_clause(obj_var)?;
26098                            self.ctx.exprs.alloc(LogicExpr::BinaryOp {
26099                                left: type_pred,
26100                                op: TokenType::And,
26101                                right: rel_clause,
26102                            })
26103                        } else {
26104                            type_pred
26105                        };
26106
26107                    let event_var = self.get_event_var();
26108                    let suppress_existential = self.drs.in_conditional_antecedent();
26109                    if suppress_existential {
26110                        let event_class = self.interner.intern("Event");
26111                        self.drs.introduce_event_referent(event_var, event_class);
26112                    }
26113
26114                    let roles = vec![
26115                        (ThematicRole::Agent, subject_term),
26116                        (ThematicRole::Theme, Term::Variable(obj_var)),
26117                    ];
26118
26119                    let neo_event =
26120                        self.ctx
26121                            .exprs
26122                            .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
26123                                event_var,
26124                                verb,
26125                                roles: self.ctx.roles.alloc_slice(roles),
26126                                modifiers: self.ctx.syms.alloc_slice(modifiers),
26127                                suppress_existential,
26128                                world: None,
26129                            })));
26130
26131                    // `any` reads as an indefinite under the clause's negation: "does not shave
26132                    // any man" is ¬∃x(Man(x) ∧ Shave(barber, x)).
26133                    let obj_determiner = obj_quantifier.clone().unwrap_or(TokenType::Some);
26134                    let obj_quantified =
26135                        self.quantify_argument(&obj_determiner, obj_var, obj_restriction, neo_event);
26136
26137                    // Apply negation at sentence level for "does not ... any"
26138                    let result = if is_negated && matches!(obj_quantifier, Some(TokenType::Any)) {
26139                        self.ctx.exprs.alloc(LogicExpr::UnaryOp {
26140                            op: TokenType::Not,
26141                            operand: obj_quantified,
26142                        })
26143                    } else if is_negated {
26144                        // For other quantifiers, negate the whole thing
26145                        self.ctx.exprs.alloc(LogicExpr::UnaryOp {
26146                            op: TokenType::Not,
26147                            operand: obj_quantified,
26148                        })
26149                    } else {
26150                        obj_quantified
26151                    };
26152
26153                    return self.wrap_with_definiteness_full(&subject, result);
26154                }
26155
26156                // Remaining complement forms — bare-noun object ("contain clove"),
26157                // measure/money ("cost $29.99", "measure 60 inches"), trailing PPs
26158                // ("happen in May", "go to Kansas"), aspect — are folded by the
26159                // shared finite-verb builder, exactly as in the positive path. The
26160                // negative scope stays open across the complement so NPIs inside it
26161                // are licensed; the result is wrapped in ¬ when "not" was present.
26162                if is_negated {
26163                    self.negative_depth += 1;
26164                }
26165                let vp = self.build_verb_vp(
26166                    subject.noun,
26167                    subject_term,
26168                    false,
26169                    verb,
26170                    verb_time,
26171                    verb_aspect,
26172                    verb_class,
26173                )?;
26174                if is_negated {
26175                    self.negative_depth -= 1;
26176                }
26177                // Returned UNWRAPPED: a definite subject is not given a uniqueness
26178                // clause here, keeping negated do-support goals structurally clean
26179                // for the proof engine (matches the pre-existing intransitive form).
26180                let result = if is_negated {
26181                    self.ctx.exprs.alloc(LogicExpr::UnaryOp {
26182                        op: TokenType::Not,
26183                        operand: vp,
26184                    })
26185                } else {
26186                    vp
26187                };
26188                return Ok(result);
26189            }
26190        }
26191
26192        // Garden path detection: "The horse raced past the barn fell."
26193        // If we have a definite NP + past verb + more content + another verb,
26194        // try reduced relative interpretation
26195        // Skip if pending_time is set (auxiliary like "will" was just consumed)
26196        // Skip if verb is has/have/had (perfect aspect, not reduced relative)
26197        let is_perfect_aux = if self.check_verb() {
26198            let word = self.interner.resolve(self.peek().lexeme).to_lowercase();
26199            word == "has" || word == "have" || word == "had"
26200        } else {
26201            false
26202        };
26203        if subject.definiteness == Some(Definiteness::Definite)
26204            && self.check_verb()
26205            && self.pending_time.is_none()
26206            && !is_perfect_aux
26207        {
26208            let saved_pos = self.backtrack_point();
26209
26210            // Try parsing as reduced relative: first verb is modifier, look for main verb after
26211            if let Some(garden_path_result) = self.try_parse(|p| {
26212                let (modifier_verb, _modifier_time, _, _) = p.consume_verb_with_metadata();
26213
26214                // Collect any PP modifiers on the reduced relative
26215                let mut pp_mods: Vec<&'a LogicExpr<'a>> = Vec::new();
26216                while p.check_preposition() {
26217                    let prep = if let TokenType::Preposition(prep) = p.advance().kind {
26218                        prep
26219                    } else {
26220                        break;
26221                    };
26222                    if p.check_article() || p.check_content_word() {
26223                        let pp_obj = p.parse_noun_phrase(false)?;
26224                        let pp_pred = p.ctx.exprs.alloc(LogicExpr::Predicate {
26225                            name: prep,
26226                            args: p.ctx.terms.alloc_slice([
26227                                Term::Variable(p.interner.intern("x")),
26228                                Term::Constant(pp_obj.noun),
26229                            ]),
26230                            world: None,
26231                        });
26232                        pp_mods.push(pp_pred);
26233                    }
26234                }
26235
26236                // Now check if there's ANOTHER verb (the real main verb)
26237                if !p.check_verb() {
26238                    return Err(ParseError {
26239                        kind: ParseErrorKind::ExpectedVerb {
26240                            found: p.peek().kind.clone(),
26241                        },
26242                        span: p.current_span(),
26243                    });
26244                }
26245
26246                let (main_verb, main_time, _, _) = p.consume_verb_with_metadata();
26247
26248                // Adjective complement of the main verb ("proved FALSE") is
26249                // its predicated result.
26250                let main_complement = if let TokenType::Adjective(a) = p.peek().kind {
26251                    p.advance();
26252                    Some(a)
26253                } else {
26254                    None
26255                };
26256
26257                // The reanalysis must account for the WHOLE clause, or it is
26258                // not the right reading.
26259                if !p.at_clause_boundary() {
26260                    return Err(ParseError {
26261                        kind: ParseErrorKind::TrailingTokens {
26262                            found: p.peek().kind.clone(),
26263                        },
26264                        span: p.current_span(),
26265                    });
26266                }
26267
26268                // Build: ∃x((Horse(x) ∧ ∀y(Horse(y) → y=x)) ∧ Raced(x) ∧ Past(x, Barn) ∧ Fell(x))
26269                let var = p.interner.intern("x");
26270
26271                // Type predicate
26272                let type_pred = p.ctx.exprs.alloc(LogicExpr::Predicate {
26273                    name: subject.noun,
26274                    args: p.ctx.terms.alloc_slice([Term::Variable(var)]),
26275                    world: None,
26276                });
26277
26278                // Modifier verb predicate (reduced relative)
26279                let mod_pred = p.ctx.exprs.alloc(LogicExpr::Predicate {
26280                    name: modifier_verb,
26281                    args: p.ctx.terms.alloc_slice([Term::Variable(var)]),
26282                    world: None,
26283                });
26284
26285                // Main verb predicate
26286                let main_pred = p.ctx.exprs.alloc(LogicExpr::Predicate {
26287                    name: main_verb,
26288                    args: match main_complement {
26289                        Some(adj) => p
26290                            .ctx
26291                            .terms
26292                            .alloc_slice([Term::Variable(var), Term::Constant(adj)]),
26293                        None => p.ctx.terms.alloc_slice([Term::Variable(var)]),
26294                    },
26295                    world: None,
26296                });
26297
26298                // Combine type + modifier
26299                let mut body = p.ctx.exprs.alloc(LogicExpr::BinaryOp {
26300                    left: type_pred,
26301                    op: TokenType::And,
26302                    right: mod_pred,
26303                });
26304
26305                // Add PP modifiers
26306                for pp in pp_mods {
26307                    body = p.ctx.exprs.alloc(LogicExpr::BinaryOp {
26308                        left: body,
26309                        op: TokenType::And,
26310                        right: pp,
26311                    });
26312                }
26313
26314                // Add main predicate
26315                body = p.ctx.exprs.alloc(LogicExpr::BinaryOp {
26316                    left: body,
26317                    op: TokenType::And,
26318                    right: main_pred,
26319                });
26320
26321                // Wrap with temporal if needed
26322                let with_time = match main_time {
26323                    Time::Past => p.ctx.exprs.alloc(LogicExpr::Temporal {
26324                        operator: TemporalOperator::Past,
26325                        body,
26326                    }),
26327                    Time::Future => p.ctx.exprs.alloc(LogicExpr::Temporal {
26328                        operator: TemporalOperator::Future,
26329                        body,
26330                    }),
26331                    _ => body,
26332                };
26333
26334                // Wrap in existential quantifier for definite
26335                Ok(p.ctx.exprs.alloc(LogicExpr::Quantifier {
26336                    kind: QuantifierKind::Existential,
26337                    variable: var,
26338                    body: with_time,
26339                    island_id: p.current_island,
26340                }))
26341            }) {
26342                return Ok(garden_path_result);
26343            }
26344
26345            // Restore position if garden path didn't work
26346            self.backtrack_to(&saved_pos);
26347        }
26348
26349        if self.check_modal() {
26350            return self.parse_aspect_chain(subject.noun);
26351        }
26352
26353        // Handle "has/have/had" perfect aspect: "John has run"
26354        if self.check_content_word() {
26355            let word = self.interner.resolve(self.peek().lexeme).to_lowercase();
26356            if word == "has" || word == "have" || word == "had" {
26357                // Lookahead to distinguish perfect aspect ("has eaten") from possession ("has 3 children")
26358                let is_perfect_aspect = if self.current + 1 < self.tokens.len() {
26359                    let next_token = &self.tokens[self.current + 1].kind;
26360                    matches!(next_token, TokenType::Verb { .. } | TokenType::Not)
26361                        && !matches!(next_token, TokenType::Number(_))
26362                } else {
26363                    false
26364                };
26365                if is_perfect_aspect {
26366                    return self.parse_aspect_chain(subject.noun);
26367                }
26368                // Otherwise fall through to verb parsing below
26369            }
26370        }
26371
26372        // "John had RUN" is the past perfect; "Bob had THE PORT" (an object, no
26373        // participle) is possessive HAVE in the PAST — re-tag "had" as the verb so
26374        // the normal verb+object grammar builds ∃e(Have(e) ∧ Agent ∧ Theme ∧ Past)
26375        // instead of mis-reading "the port" as a perfect participle.
26376        if self.check(&TokenType::Had) {
26377            let next_is_participle = matches!(
26378                self.tokens.get(self.current + 1).map(|t| &t.kind),
26379                Some(TokenType::Verb { .. })
26380            );
26381            if next_is_participle {
26382                return self.parse_aspect_chain(subject.noun);
26383            }
26384            let have_lemma = self.interner.intern("Have");
26385            self.tokens[self.current].kind = TokenType::Verb {
26386                lemma: have_lemma,
26387                time: Time::Past,
26388                aspect: Aspect::Simple,
26389                class: VerbClass::State,
26390            };
26391            // fall through to the normal verb+object handling
26392        }
26393
26394        // Handle "never" temporal negation: "John never runs"
26395        if self.check(&TokenType::Never) {
26396            self.advance();
26397            let verb = self.consume_verb();
26398            let subject_term = self.noun_phrase_to_term(&subject);
26399
26400            // Optional object: quantified ("two requests") raises past the
26401            // negation; a plain NP fills Theme. Bare "never V" keeps the
26402            // classical ¬Verb(subject) shape.
26403            let mut object_quant: Option<(QuantifierKind, Symbol, Symbol)> = None;
26404            let mut object_term: Option<Term<'a>> = None;
26405            if let TokenType::Cardinal(n) = self.peek().kind {
26406                self.advance();
26407                let np = self.parse_noun_phrase(false)?;
26408                let var = self.next_var_name();
26409                object_quant = Some((QuantifierKind::Cardinal(n), var, np.noun));
26410                object_term = Some(Term::Variable(var));
26411            } else if matches!(self.peek().kind, TokenType::All | TokenType::Some) {
26412                let universal = matches!(self.advance().kind, TokenType::All);
26413                let np = self.parse_noun_phrase(false)?;
26414                let var = self.next_var_name();
26415                let kind = if universal {
26416                    QuantifierKind::Universal
26417                } else {
26418                    QuantifierKind::Existential
26419                };
26420                object_quant = Some((kind, var, np.noun));
26421                object_term = Some(Term::Variable(var));
26422            } else if self.check_content_word() || self.check_article() {
26423                let np = self.parse_noun_phrase(false)?;
26424                object_term = Some(self.noun_phrase_to_term(&np));
26425            }
26426
26427            if object_term.is_none() {
26428                let verb_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
26429                    name: verb,
26430                    args: self.ctx.terms.alloc_slice([subject_term]),
26431                    world: None,
26432                });
26433                let result = self.ctx.exprs.alloc(LogicExpr::UnaryOp {
26434                    op: TokenType::Not,
26435                    operand: verb_pred,
26436                });
26437                return self.wrap_with_definiteness_full(&subject, result);
26438            }
26439
26440            let mut roles = vec![(crate::ast::ThematicRole::Agent, subject_term)];
26441            if let Some(t) = object_term {
26442                roles.push((crate::ast::ThematicRole::Theme, t));
26443            }
26444            let modifiers = self.collect_adverbs();
26445            let event_var = self.get_event_var();
26446            let suppress_existential = self.drs.in_conditional_antecedent();
26447            let event = self
26448                .ctx
26449                .exprs
26450                .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
26451                    event_var,
26452                    verb,
26453                    roles: self.ctx.roles.alloc_slice(roles),
26454                    modifiers: self.ctx.syms.alloc_slice(modifiers),
26455                    suppress_existential,
26456                    world: None,
26457                })));
26458            let mut result = self.ctx.exprs.alloc(LogicExpr::UnaryOp {
26459                op: TokenType::Not,
26460                operand: event,
26461            });
26462            if let Some((kind, var, noun)) = object_quant {
26463                let restriction = self.ctx.exprs.alloc(LogicExpr::Predicate {
26464                    name: noun,
26465                    args: self.ctx.terms.alloc_slice([Term::Variable(var)]),
26466                    world: None,
26467                });
26468                let connective = if matches!(kind, QuantifierKind::Universal) {
26469                    TokenType::Implies
26470                } else {
26471                    TokenType::And
26472                };
26473                let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
26474                    left: restriction,
26475                    op: connective,
26476                    right: result,
26477                });
26478                result = self.ctx.exprs.alloc(LogicExpr::Quantifier {
26479                    kind,
26480                    variable: var,
26481                    body,
26482                    island_id: self.current_island,
26483                });
26484            }
26485            return self.wrap_with_definiteness_full(&subject, result);
26486        }
26487
26488        // Adverbs of quantification ("usually", "often", "always", "generally", …)
26489        // before the verb mark a characterizing/habitual clause ("John usually
26490        // smokes."). Consume the adverb so the predicate parse proceeds; the
26491        // habitual operator falls out of the simple-present eventive reading. Only
26492        // consumed when a verb follows, so it never strands other constructions.
26493        if let TokenType::Adverb(sym)
26494        | TokenType::ScopalAdverb(sym)
26495        | TokenType::TemporalAdverb(sym) = self.peek().kind
26496        {
26497            // Adverbs of quantification are a lexical category (tagged in the
26498            // lexicon), not a hardcoded parser list.
26499            let w = self.interner.resolve(sym).to_lowercase();
26500            if crate::lexicon::is_quantificational_adverb(&w)
26501                && self.current + 1 < self.tokens.len()
26502                && matches!(self.tokens[self.current + 1].kind, TokenType::Verb { .. })
26503            {
26504                self.advance();
26505            }
26506        }
26507
26508        if self.check_verb() {
26509            let (mut verb, verb_time, verb_aspect, verb_class) = self.consume_verb_with_metadata();
26510
26511            // Check for verb sort violation (metaphor detection)
26512            let subject_sort = lexicon::lookup_sort(self.interner.resolve(subject.noun));
26513            let verb_str = self.interner.resolve(verb);
26514            if let Some(s_sort) = subject_sort {
26515                if !crate::ontology::check_sort_compatibility(verb_str, s_sort) {
26516                    let metaphor = self.ctx.exprs.alloc(LogicExpr::Metaphor {
26517                        tenor: self.ctx.terms.alloc(Term::Constant(subject.noun)),
26518                        vehicle: self.ctx.terms.alloc(Term::Constant(verb)),
26519                    });
26520                    return self.wrap_with_definiteness_full(&subject, metaphor);
26521                }
26522            }
26523
26524            // Arithmetic / vague verbal comparative ("Tara scored 3 points lower
26525            // than Bessie"): the same construction parse_predicate_impl handles,
26526            // shared so a proper-name / definite subject parsed inline here keeps
26527            // the offset and the comparison instead of stranding the comparative.
26528            if let Some(cmp) =
26529                self.try_arithmetic_comparative(verb, Term::Constant(subject.noun), verb_time)?
26530            {
26531                return self.wrap_with_definiteness_full(&subject, cmp);
26532            }
26533
26534            // Temporal offset ("Tara performed 2 weeks after Bessie").
26535            if let Some(off) =
26536                self.try_temporal_offset(verb, Term::Constant(subject.noun), verb_time)?
26537            {
26538                return self.wrap_with_definiteness_full(&subject, off);
26539            }
26540
26541            // Ordinal-position offset ("finished 2 places ahead of Bob").
26542            if let Some(off) =
26543                self.try_positional_offset(verb, Term::Constant(subject.noun), verb_time)?
26544            {
26545                return self.wrap_with_definiteness_full(&subject, off);
26546            }
26547
26548            // Raising / evidential verbs (seem, appear, look — tagged `Raising` in
26549            // the lexicon): "John seems happy." marks an evidence source; the
26550            // complement is NOT asserted but embedded under the evidential:
26551            // Seem(⟨Happy(John)⟩). So "seems happy" does not entail "happy". This is
26552            // checked BEFORE the control-verb path because seem/appear are raising
26553            // (subject-to-subject), not control, even if also tagged control.
26554            // The EVIDENTIAL use has a SMALL-CLAUSE complement: "seems happy",
26555            // "seems to be happy", "seems a fool". A bare intransitive ("a cat
26556            // appeared.") and a raising-to-control "seems to <verb>" ("seems to want
26557            // to stay") both fall through to the existing verb handling.
26558            let is_raising =
26559                crate::lexicon::is_raising_verb(&self.interner.resolve(verb).to_lowercase());
26560            let next_is_to_be = self.check(&TokenType::To)
26561                && self.current + 1 < self.tokens.len()
26562                && matches!(self.tokens[self.current + 1].kind, TokenType::Be);
26563            let raising_with_complement = is_raising
26564                && (matches!(self.peek().kind, TokenType::Adjective(_))
26565                    || next_is_to_be
26566                    || (self.check_content_word() && !self.check(&TokenType::To)));
26567            if raising_with_complement {
26568                // Optional "to be": "seems to be happy".
26569                if self.check(&TokenType::To) {
26570                    self.advance();
26571                    if self.check(&TokenType::Be) {
26572                        self.advance();
26573                    }
26574                }
26575                let complement = if let TokenType::Adjective(adj) = self.peek().kind {
26576                    self.advance();
26577                    self.ctx.exprs.alloc(LogicExpr::Predicate {
26578                        name: adj,
26579                        args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
26580                        world: None,
26581                    })
26582                } else if self.check_content_word() {
26583                    self.parse_predicate_with_subject(subject.noun)?
26584                } else {
26585                    self.ctx
26586                        .exprs
26587                        .alloc(LogicExpr::Atom(self.interner.intern("?")))
26588                };
26589                let evidential = self.ctx.exprs.alloc(LogicExpr::Modal {
26590                    vector: crate::ast::ModalVector::new(
26591                        crate::ast::ModalDomain::Alethic,
26592                        1.0,
26593                        crate::ast::ModalFlavor::Evidential,
26594                    )
26595                    .with_base(verb),
26596                    operand: complement,
26597                });
26598                return self.wrap_with_definiteness_full(&subject, evidential);
26599            }
26600
26601            // Perception complement (§3.2): a perception verb ("hear", "see" —
26602            // lexicon-tagged) takes an "NP bare-VP" small clause naming the PERCEIVED
26603            // EVENT: "Mary heard the bell ring." → Hear(Mary, ⟨Ring(bell)⟩). The bare
26604            // VP head is the LAST verb-capable token before the clause end (so the
26605            // perceived NP "the bell" is not greedily merged with "ring"); a clause
26606            // with no such trailing verb ("heard the bell") is an ordinary object.
26607            if crate::lexicon::is_perception_verb(&self.interner.resolve(verb).to_lowercase()) {
26608                let is_verb_capable = |kind: &TokenType| match kind {
26609                    TokenType::Verb { .. } => true,
26610                    TokenType::Ambiguous {
26611                        primary,
26612                        alternatives,
26613                    } => {
26614                        matches!(**primary, TokenType::Verb { .. })
26615                            || alternatives
26616                                .iter()
26617                                .any(|t| matches!(t, TokenType::Verb { .. }))
26618                    }
26619                    _ => false,
26620                };
26621                let mut vp_idx = None;
26622                let mut i = self.current;
26623                while i < self.tokens.len() {
26624                    // A small clause ends where its sentence does, or at a coordinator:
26625                    // in "sees Mary and Bill runs" Bill's running is a clause of its own.
26626                    if matches!(
26627                        self.tokens[i].kind,
26628                        TokenType::Period
26629                            | TokenType::EOF
26630                            | TokenType::Comma
26631                            | TokenType::And
26632                            | TokenType::Or
26633                    ) {
26634                        break;
26635                    }
26636                    // Verb-capable by token kind OR by lexicon (a word like "ring"
26637                    // may be tokenized as a noun after "the bell" yet head the VP).
26638                    let lex = self.interner.resolve(self.tokens[i].lexeme).to_lowercase();
26639                    if is_verb_capable(&self.tokens[i].kind)
26640                        || crate::lexicon::lookup_verb_db(&lex).is_some()
26641                    {
26642                        vp_idx = Some(i);
26643                    }
26644                    i += 1;
26645                }
26646                if let Some(vp_i) = vp_idx {
26647                    // The perceived subject is the noun/name immediately before the VP.
26648                    if vp_i > self.current {
26649                        let subj_kind = self.tokens[vp_i - 1].kind.clone();
26650                        let psubj = match subj_kind {
26651                            TokenType::Noun(n) | TokenType::ProperName(n) => Some(n),
26652                            // A function word cannot be the perceived subject.
26653                            TokenType::Article(_)
26654                            | TokenType::Preposition(_)
26655                            | TokenType::Period
26656                            | TokenType::Comma => None,
26657                            // An ADJECTIVE or NUMBER/CARDINAL before the candidate
26658                            // VP means this is NOT a small clause ("saw 6 previous
26659                            // JUMPS" — "previous" cannot be a clause subject); the
26660                            // verb-word is a deverbal noun head. Bail so the object
26661                            // NP path (counting NP / deverbal noun) handles it.
26662                            TokenType::Adjective(_)
26663                            | TokenType::NonIntersectiveAdjective(_)
26664                            | TokenType::Cardinal(_)
26665                            | TokenType::Number(_)
26666                            | TokenType::AtLeast(_)
26667                            | TokenType::AtMost(_) => None,
26668                            // A content word tokenized otherwise (e.g. a noun-or-verb
26669                            // ambiguity) — use its lexeme as the entity name.
26670                            _ => {
26671                                let lx = self
26672                                    .interner
26673                                    .resolve(self.tokens[vp_i - 1].lexeme)
26674                                    .to_lowercase();
26675                                let cap = lx
26676                                    .chars()
26677                                    .next()
26678                                    .map(|c| c.to_uppercase().collect::<String>() + &lx[1..])
26679                                    .unwrap_or(lx);
26680                                Some(self.interner.intern(&cap))
26681                            }
26682                        };
26683                        if let Some(psubj) = psubj {
26684                            let vlex = self
26685                                .interner
26686                                .resolve(self.tokens[vp_i].lexeme)
26687                                .to_lowercase();
26688                            let vname = vlex
26689                                .chars()
26690                                .next()
26691                                .map(|c| c.to_uppercase().collect::<String>() + &vlex[1..])
26692                                .unwrap_or(vlex);
26693                            let inner_verb = self.interner.intern(&vname);
26694                            self.current = vp_i + 1; // consume through the VP head
26695                            let perceived = self.ctx.exprs.alloc(LogicExpr::Predicate {
26696                                name: inner_verb,
26697                                args: self.ctx.terms.alloc_slice([Term::Constant(psubj)]),
26698                                world: None,
26699                            });
26700                            // A trailing adverb modifies the PERCEIVED event
26701                            // ("heard the bell ring LOUDLY").
26702                            let perceived_advs = self.collect_adverbs();
26703                            let perceived = if perceived_advs.is_empty() {
26704                                perceived
26705                            } else {
26706                                self.ctx.exprs.alloc(LogicExpr::Event {
26707                                    predicate: perceived,
26708                                    adverbs: self.ctx.syms.alloc_slice(perceived_advs),
26709                                })
26710                            };
26711                            let event_var = self.get_event_var();
26712                            let roles = vec![
26713                                (ThematicRole::Agent, Term::Constant(subject.noun)),
26714                                (ThematicRole::Theme, Term::Proposition(perceived)),
26715                            ];
26716                            let mut modifiers: Vec<Symbol> = Vec::new();
26717                            match verb_time {
26718                                Time::Past => modifiers.push(self.interner.intern("Past")),
26719                                Time::Future => modifiers.push(self.interner.intern("Future")),
26720                                _ => {}
26721                            }
26722                            let neo =
26723                                self.ctx
26724                                    .exprs
26725                                    .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
26726                                        event_var,
26727                                        verb,
26728                                        roles: self.ctx.roles.alloc_slice(roles),
26729                                        modifiers: self.ctx.syms.alloc_slice(modifiers),
26730                                        suppress_existential: false,
26731                                        world: None,
26732                                    })));
26733                            return self.wrap_with_definiteness_full(&subject, neo);
26734                        }
26735                    }
26736                }
26737            }
26738
26739            // Check for control verb + infinitive (raising verbs handled above).
26740            // A SUBJECT-control verb only takes the control structure when an
26741            // infinitival complement actually follows ("started TO run"). Used as a
26742            // plain eventive verb ("The outing started at Greektown"), it falls
26743            // through to the regular VP grammar so its PP adjuncts attach. An
26744            // OBJECT-control verb keeps the control path even without an immediate
26745            // "to" — its object comes first ("persuaded Mary to be examined").
26746            if self.is_control_verb(verb)
26747                && (self.check_to()
26748                    || lexicon::is_object_control_verb(&self.interner.resolve(verb).to_lowercase()))
26749            {
26750                return self.parse_control_structure(&subject, verb, verb_time);
26751            }
26752
26753            // If we have a relative clause, use variable binding
26754            if let Some((var_name, rel_clause)) = relative_clause {
26755                // A bare verb keeps the simple predication; material after it
26756                // ("The farmer who saw a cat CHASED IT.") takes the full VP
26757                // grammar over the relativized variable.
26758                let with_time = if self.at_clause_boundary() {
26759                    let main_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
26760                        name: verb,
26761                        args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
26762                        world: None,
26763                    });
26764
26765                    let effective_time = self.pending_time.take().unwrap_or(verb_time);
26766                    match effective_time {
26767                        Time::Past => &*self.ctx.exprs.alloc(LogicExpr::Temporal {
26768                            operator: TemporalOperator::Past,
26769                            body: main_pred,
26770                        }),
26771                        Time::Future => &*self.ctx.exprs.alloc(LogicExpr::Temporal {
26772                            operator: TemporalOperator::Future,
26773                            body: main_pred,
26774                        }),
26775                        _ => main_pred,
26776                    }
26777                } else {
26778                    self.current -= 1; // hand the verb back to the VP parser
26779                    self.parse_predicate_with_subject_as_var(var_name)?
26780                };
26781
26782                // Build: ∃x(Type(x) ∧ RelClause(x) ∧ MainPred(x))
26783                let type_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
26784                    name: subject.noun,
26785                    args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
26786                    world: None,
26787                });
26788
26789                let inner = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
26790                    left: type_pred,
26791                    op: TokenType::And,
26792                    right: rel_clause,
26793                });
26794
26795                let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
26796                    left: inner,
26797                    op: TokenType::And,
26798                    right: with_time,
26799                });
26800
26801                return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
26802                    kind: QuantifierKind::Existential,
26803                    variable: var_name,
26804                    body,
26805                    island_id: self.current_island,
26806                }));
26807            }
26808
26809            let subject_term = self.noun_phrase_to_term(&subject);
26810            let mut args = vec![subject_term.clone()];
26811
26812            let unknown = self.interner.intern("?");
26813
26814            // Check for embedded wh-clause: "I know who/what"
26815            if self.check_wh_word() {
26816                let wh_token = self.advance().kind.clone();
26817
26818                // Determine wh-type for slot matching
26819                let is_who = matches!(wh_token, TokenType::Who);
26820                let is_what = matches!(wh_token, TokenType::What);
26821
26822                // Check for sluicing: wh-word followed by terminator
26823                let is_sluicing = self.is_at_end()
26824                    || self.check(&TokenType::Period)
26825                    || self.check(&TokenType::Comma);
26826
26827                if is_sluicing {
26828                    // Reconstruct from template
26829                    if let Some(template) = self.last_event_template.clone() {
26830                        let wh_var = self.next_var_name();
26831
26832                        // Build roles with wh-variable in appropriate slot
26833                        let roles: Vec<_> = if is_who {
26834                            // "who" replaces Agent
26835                            std::iter::once((ThematicRole::Agent, Term::Variable(wh_var)))
26836                                .chain(template.non_agent_roles.iter().cloned())
26837                                .collect()
26838                        } else if is_what {
26839                            // "what" replaces Theme - use Agent from context, Theme is variable
26840                            vec![
26841                                (ThematicRole::Agent, subject_term.clone()),
26842                                (ThematicRole::Theme, Term::Variable(wh_var)),
26843                            ]
26844                        } else {
26845                            // Default: wh-variable as Agent
26846                            std::iter::once((ThematicRole::Agent, Term::Variable(wh_var)))
26847                                .chain(template.non_agent_roles.iter().cloned())
26848                                .collect()
26849                        };
26850
26851                        let event_var = self.get_event_var();
26852                        let suppress_existential = self.drs.in_conditional_antecedent();
26853                        if suppress_existential {
26854                            let event_class = self.interner.intern("Event");
26855                            self.drs.introduce_event_referent(event_var, event_class);
26856                        }
26857                        let reconstructed =
26858                            self.ctx
26859                                .exprs
26860                                .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
26861                                    event_var,
26862                                    verb: template.verb,
26863                                    roles: self.ctx.roles.alloc_slice(roles),
26864                                    modifiers: self
26865                                        .ctx
26866                                        .syms
26867                                        .alloc_slice(template.modifiers.clone()),
26868                                    suppress_existential,
26869                                    world: None,
26870                                })));
26871
26872                        let question = self.ctx.exprs.alloc(LogicExpr::Question {
26873                            wh_variable: wh_var,
26874                            body: reconstructed,
26875                        });
26876
26877                        // Build: Know(subject, question)
26878                        let know_event_var = self.get_event_var();
26879                        let suppress_existential2 = self.drs.in_conditional_antecedent();
26880                        if suppress_existential2 {
26881                            let event_class = self.interner.intern("Event");
26882                            self.drs
26883                                .introduce_event_referent(know_event_var, event_class);
26884                        }
26885                        let know_event =
26886                            self.ctx
26887                                .exprs
26888                                .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
26889                                    event_var: know_event_var,
26890                                    verb,
26891                                    roles: self.ctx.roles.alloc_slice(vec![
26892                                        (ThematicRole::Agent, subject_term),
26893                                        (ThematicRole::Theme, Term::Proposition(question)),
26894                                    ]),
26895                                    modifiers: self.ctx.syms.alloc_slice(vec![]),
26896                                    suppress_existential: suppress_existential2,
26897                                    world: None,
26898                                })));
26899
26900                        return self.wrap_with_definiteness_full(&subject, know_event);
26901                    }
26902                }
26903
26904                // Non-sluicing embedded question: "I know who runs"
26905                let embedded = self.parse_embedded_wh_clause()?;
26906                let question = self.ctx.exprs.alloc(LogicExpr::Question {
26907                    wh_variable: self.interner.intern("x"),
26908                    body: embedded,
26909                });
26910
26911                // Build: Know(subject, question)
26912                let know_event_var = self.get_event_var();
26913                let suppress_existential = self.drs.in_conditional_antecedent();
26914                if suppress_existential {
26915                    let event_class = self.interner.intern("Event");
26916                    self.drs
26917                        .introduce_event_referent(know_event_var, event_class);
26918                }
26919                let know_event =
26920                    self.ctx
26921                        .exprs
26922                        .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
26923                            event_var: know_event_var,
26924                            verb,
26925                            roles: self.ctx.roles.alloc_slice(vec![
26926                                (ThematicRole::Agent, subject_term),
26927                                (ThematicRole::Theme, Term::Proposition(question)),
26928                            ]),
26929                            modifiers: self.ctx.syms.alloc_slice(vec![]),
26930                            suppress_existential,
26931                            world: None,
26932                        })));
26933
26934                return self.wrap_with_definiteness_full(&subject, know_event);
26935            }
26936
26937            // Opaque attitude verbs take a finite clausal complement as a STRUCTURED
26938            // PROPOSITION (P3), not an extensional object: "John believes Mary left."
26939            // → Believe(John, ⟨Left(Mary)⟩). A pure-token lookahead detects an embedded
26940            // proper-name/pronoun subject directly followed by a verb (optionally after
26941            // the complementizer "that"); article-headed embedded clauses ("a spy
26942            // exists") keep their existing de re/de dicto treatment downstream.
26943            if crate::lexicon::is_opaque_verb(&self.interner.resolve(verb).to_lowercase()) {
26944                let mut i = self.current;
26945                if i < self.tokens.len() && matches!(self.tokens[i].kind, TokenType::That) {
26946                    i += 1;
26947                }
26948                let subj_ok = i < self.tokens.len()
26949                    && matches!(
26950                        self.tokens[i].kind,
26951                        TokenType::ProperName(_) | TokenType::Pronoun { .. }
26952                    );
26953                let verb_follows = subj_ok
26954                    && i + 1 < self.tokens.len()
26955                    && (matches!(
26956                        self.tokens[i + 1].kind,
26957                        TokenType::Verb { .. } | TokenType::Auxiliary(_)
26958                    )
26959                        // Noun/verb-ambiguous surface forms ("saw", "ring")
26960                        // tokenize as nouns; the irregular table identifies
26961                        // them as verb forms.
26962                        || crate::lexicon::lookup_irregular_verb(
26963                            &self
26964                                .interner
26965                                .resolve(self.tokens[i + 1].lexeme)
26966                                .to_lowercase(),
26967                        )
26968                        .is_some());
26969                if verb_follows {
26970                    if self.check(&TokenType::That) {
26971                        self.advance();
26972                    }
26973                    let embedded_subject = match self.peek().kind {
26974                        TokenType::ProperName(s) => {
26975                            self.advance();
26976                            s
26977                        }
26978                        TokenType::Pronoun { gender, number, .. } => {
26979                            self.advance();
26980                            match self.resolve_pronoun(gender, number)? {
26981                                ResolvedPronoun::Variable(s) | ResolvedPronoun::Constant(s) => s,
26982                            }
26983                        }
26984                        _ => unreachable!("guarded by subj_ok"),
26985                    };
26986                    let embedded_pred = self.parse_predicate_with_subject(embedded_subject)?;
26987                    let subject_term_for_event = self.coerce_agent(&subject);
26988                    let main_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
26989                        name: verb,
26990                        args: self.ctx.terms.alloc_slice([
26991                            subject_term_for_event,
26992                            Term::Proposition(embedded_pred),
26993                        ]),
26994                        world: None,
26995                    });
26996                    let effective_time = self.pending_time.take().unwrap_or(verb_time);
26997                    let with_time = match effective_time {
26998                        Time::Past => self.ctx.exprs.alloc(LogicExpr::Temporal {
26999                            operator: TemporalOperator::Past,
27000                            body: main_pred,
27001                        }),
27002                        Time::Future => self.ctx.exprs.alloc(LogicExpr::Temporal {
27003                            operator: TemporalOperator::Future,
27004                            body: main_pred,
27005                        }),
27006                        _ => main_pred,
27007                    };
27008                    return self.wrap_with_definiteness_full(&subject, with_time);
27009                }
27010            }
27011
27012            let mut object_term: Option<Term<'a>> = None;
27013            // A definite/constant object's own adjectives and PP restrictors ("ate
27014            // the RED apple", "the frat ON Holly Street") — predicated of the
27015            // object term in the shared event build below; dropping them loses a
27016            // constraint (meaning-loss).
27017            let mut object_adjectives: &[Symbol] = &[];
27018            let mut object_desc_pps: &[&'a LogicExpr<'a>] = &[];
27019            // Secondary predication (§3.3): a post-object adjective ("painted the
27020            // door red", "ate the meat raw") captured during object parsing and
27021            // attached as a Result (change-of-state verb) or Depictive role below.
27022            let mut secondary_adj: Option<Symbol> = None;
27023            // Resultative vs depictive is a LEXICAL property of the verb (causative
27024            // change-of-state verbs are tagged `Resultative` in the lexicon).
27025            let secondary_role = |verb_lemma: &str| -> ThematicRole {
27026                if crate::lexicon::is_resultative_verb(verb_lemma) {
27027                    ThematicRole::Result
27028                } else {
27029                    ThematicRole::Depictive
27030                }
27031            };
27032            let mut second_object_term: Option<Term<'a>> = None;
27033            let mut object_superlative: Option<(Symbol, Symbol)> = None; // (adjective, noun)
27034            if self.check(&TokenType::Reflexive) {
27035                self.advance();
27036                let term = self.noun_phrase_to_term(&subject);
27037                object_term = Some(term.clone());
27038                args.push(term);
27039
27040                // Check for distanced phrasal verb particle: "gave himself up"
27041                if let TokenType::Particle(particle_sym) = self.peek().kind {
27042                    let verb_str = self.interner.resolve(verb).to_lowercase();
27043                    let particle_str = self.interner.resolve(particle_sym).to_lowercase();
27044                    if let Some((phrasal_lemma, _class)) =
27045                        crate::lexicon::lookup_phrasal_verb(&verb_str, &particle_str)
27046                    {
27047                        self.advance();
27048                        verb = self.interner.intern(phrasal_lemma);
27049                    }
27050                }
27051            } else if self.check_pronoun() {
27052                let token = self.advance().clone();
27053                // See through an Ambiguous wrapper ("her" = object pronoun /
27054                // possessive): check_pronoun already ruled out the possessive
27055                // NP reading, so the primary pronoun analysis applies here.
27056                let pronoun_analysis = match &token.kind {
27057                    TokenType::Pronoun { gender, number, .. } => Some((*gender, *number)),
27058                    TokenType::Ambiguous { primary, .. } => match primary.as_ref() {
27059                        TokenType::Pronoun { gender, number, .. } => Some((*gender, *number)),
27060                        _ => None,
27061                    },
27062                    _ => None,
27063                };
27064                if let Some((gender, number)) = pronoun_analysis {
27065                    // Person deictics (§8.4) resolve to the discourse roles in ANY
27066                    // position: "Mary saw you." → Addressee, not a discourse referent.
27067                    let plex = self.interner.resolve(token.lexeme).to_lowercase();
27068                    let deictic_role = match plex.as_str() {
27069                        "you" | "yourself" => Some("Addressee"),
27070                        "i" | "me" | "myself" => Some("Speaker"),
27071                        _ => None,
27072                    };
27073                    if let Some(role) = deictic_role {
27074                        let term = Term::Constant(self.interner.intern(role));
27075                        object_term = Some(term.clone());
27076                        args.push(term);
27077                        // fall past the resolve-pronoun handling below
27078                    } else {
27079                        let resolved = self.resolve_pronoun(gender, number)?;
27080                        let resolved_sym = match resolved {
27081                            ResolvedPronoun::Variable(s) | ResolvedPronoun::Constant(s) => s,
27082                        };
27083                        // Binding Principle B: a plain (non-reflexive) object pronoun is
27084                        // free in its local clause — it must not corefer with the local
27085                        // subject. "John saw him." ⇒ him ≠ John. If resolution picked the
27086                        // subject, use a fresh distinct individual instead.
27087                        let term = if resolved_sym == subject.noun {
27088                            let deictic = match (gender, number) {
27089                                (Gender::Male, Number::Singular) => "Him",
27090                                (Gender::Female, Number::Singular) => "Her",
27091                                (Gender::Neuter, Number::Singular) => "It",
27092                                _ => "Them",
27093                            };
27094                            let sym = self.interner.intern(deictic);
27095                            self.drs.introduce_referent_with_source(
27096                                sym,
27097                                sym,
27098                                gender,
27099                                number,
27100                                crate::drs::ReferentSource::ProperName,
27101                            );
27102                            Term::Constant(sym)
27103                        } else {
27104                            match resolved {
27105                                ResolvedPronoun::Variable(s) => Term::Variable(s),
27106                                ResolvedPronoun::Constant(s) => Term::Constant(s),
27107                            }
27108                        };
27109                        object_term = Some(term.clone());
27110                        args.push(term);
27111
27112                        // Check for distanced phrasal verb particle: "gave it up"
27113                        if let TokenType::Particle(particle_sym) = self.peek().kind {
27114                            let verb_str = self.interner.resolve(verb).to_lowercase();
27115                            let particle_str = self.interner.resolve(particle_sym).to_lowercase();
27116                            if let Some((phrasal_lemma, _class)) =
27117                                crate::lexicon::lookup_phrasal_verb(&verb_str, &particle_str)
27118                            {
27119                                self.advance();
27120                                verb = self.interner.intern(phrasal_lemma);
27121                            }
27122                        }
27123                    } // end non-deictic pronoun branch
27124                }
27125            } else if self.check(&TokenType::Either) {
27126                // "Tara danced either the hustle or the lindy." — disjunctive theme
27127                self.advance(); // consume "either"
27128                let np1 = self.parse_noun_phrase(true)?;
27129                if self.check(&TokenType::Or) {
27130                    self.advance(); // consume "or"
27131                    let np2 = self.parse_noun_phrase(true)?;
27132
27133                    let effective_time = self.pending_time.take().unwrap_or(verb_time);
27134                    let subject_term1 = self.coerce_agent(&subject);
27135                    let subject_term2 = self.coerce_agent(&subject);
27136                    let suppress_existential = self.drs.in_conditional_antecedent();
27137
27138                    let past_sym = self.interner.intern("Past");
27139                    let future_sym = self.interner.intern("Future");
27140                    let mut mods1 = vec![];
27141                    let mut mods2 = vec![];
27142                    match effective_time {
27143                        Time::Past => {
27144                            mods1.push(past_sym);
27145                            mods2.push(past_sym);
27146                        }
27147                        Time::Future => {
27148                            mods1.push(future_sym);
27149                            mods2.push(future_sym);
27150                        }
27151                        _ => {}
27152                    }
27153
27154                    let event_var1 = self.get_event_var();
27155                    let event_var2 = self.get_event_var();
27156
27157                    let neo1 = self
27158                        .ctx
27159                        .exprs
27160                        .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
27161                            event_var: event_var1,
27162                            verb,
27163                            roles: self.ctx.roles.alloc_slice(vec![
27164                                (ThematicRole::Agent, subject_term1),
27165                                (ThematicRole::Theme, Term::Constant(np1.noun)),
27166                            ]),
27167                            modifiers: self.ctx.syms.alloc_slice(mods1),
27168                            suppress_existential,
27169                            world: None,
27170                        })));
27171                    let neo2 = self
27172                        .ctx
27173                        .exprs
27174                        .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
27175                            event_var: event_var2,
27176                            verb,
27177                            roles: self.ctx.roles.alloc_slice(vec![
27178                                (ThematicRole::Agent, subject_term2),
27179                                (ThematicRole::Theme, Term::Constant(np2.noun)),
27180                            ]),
27181                            modifiers: self.ctx.syms.alloc_slice(mods2),
27182                            suppress_existential,
27183                            world: None,
27184                        })));
27185
27186                    // Keep each disjunct object NP's possessor and PPs — "either
27187                    // the hustle from Spain or …" must not drop "from Spain";
27188                    // "either Tara's routine or …" must not drop the possessor.
27189                    let placeholder = self.interner.intern("_PP_SELF_");
27190                    let possesses = self.interner.intern("Possesses");
27191                    let mut augment = |p: &mut Self,
27192                                       neo: &'a LogicExpr<'a>,
27193                                       np: &crate::ast::NounPhrase<'a>|
27194                     -> &'a LogicExpr<'a> {
27195                        let obj = Term::Constant(np.noun);
27196                        let mut e = neo;
27197                        if let Some(possessor) = np.possessor {
27198                            let poss = p.ctx.exprs.alloc(LogicExpr::Predicate {
27199                                name: possesses,
27200                                args: p
27201                                    .ctx
27202                                    .terms
27203                                    .alloc_slice([Term::Constant(possessor.noun), obj]),
27204                                world: None,
27205                            });
27206                            e = p.ctx.exprs.alloc(LogicExpr::BinaryOp {
27207                                left: e,
27208                                op: TokenType::And,
27209                                right: poss,
27210                            });
27211                        }
27212                        for pp in np.pps {
27213                            let pp_sub = match pp {
27214                                LogicExpr::Predicate { name, args, world } => {
27215                                    let new_args: Vec<Term<'a>> = args
27216                                        .iter()
27217                                        .map(|a| match a {
27218                                            Term::Variable(v) if *v == placeholder => obj,
27219                                            other => *other,
27220                                        })
27221                                        .collect();
27222                                    p.ctx.exprs.alloc(LogicExpr::Predicate {
27223                                        name: *name,
27224                                        args: p.ctx.terms.alloc_slice(new_args),
27225                                        world: *world,
27226                                    })
27227                                }
27228                                other => *other,
27229                            };
27230                            e = p.ctx.exprs.alloc(LogicExpr::BinaryOp {
27231                                left: e,
27232                                op: TokenType::And,
27233                                right: pp_sub,
27234                            });
27235                        }
27236                        e
27237                    };
27238                    let neo1 = augment(self, neo1, &np1);
27239                    let neo2 = augment(self, neo2, &np2);
27240                    let disj = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
27241                        left: neo1,
27242                        op: TokenType::Or,
27243                        right: neo2,
27244                    });
27245                    return self.wrap_with_definiteness_full(&subject, disj);
27246                }
27247                return Err(ParseError {
27248                    kind: ParseErrorKind::UnexpectedToken {
27249                        expected: TokenType::Or,
27250                        found: self.peek().kind.clone(),
27251                    },
27252                    span: self.current_span(),
27253                });
27254            } else if self.counting_np_lookahead().is_some()
27255                || self.check_quantifier()
27256                || self.check_article()
27257                || self.check_possessive_pronoun()
27258            {
27259                // Quantified object: "John loves every woman" or "John saw a dog"
27260                let (obj_quantifier, was_definite_article) =
27261                    if let Some(n) = self.counting_np_lookahead() {
27262                        // A digit-led counting NP object ("saw 6 brown manatees"):
27263                        // consume the integer and quantify the remaining
27264                        // "(adjective)+ noun" as ∃=n, reusing the canonical
27265                        // quantified-object construction below. Without this the
27266                        // adjective is mis-read as a measure unit (→ a bogus
27267                        // Recipient role and a dropped count).
27268                        self.advance();
27269                        (Some(TokenType::Cardinal(n)), false)
27270                    } else if self.check_possessive_pronoun() {
27271                        // Possessive NP object ("her dog"): parse_noun_phrase
27272                        // consumes the possessor; semantically definite.
27273                        (None, true)
27274                    } else if self.check_quantifier() {
27275                        (Some(self.advance().kind.clone()), false)
27276                    } else {
27277                        let art = self.advance().kind.clone();
27278                        if let TokenType::Article(def) = art {
27279                            if def == Definiteness::Indefinite {
27280                                (Some(TokenType::Some), false)
27281                            } else {
27282                                (None, true) // Was a definite article
27283                            }
27284                        } else {
27285                            (None, false)
27286                        }
27287                    };
27288
27289                // The quantifier/article/cardinal just established this as an NP,
27290                // so a verb-word head after an adjective is a deverbal noun.
27291                self.nominal_np_context = true;
27292                let object_np_result = self.parse_noun_phrase(false);
27293                self.nominal_np_context = false;
27294                let object_np = object_np_result?;
27295
27296                // Capture superlative info for constraint generation
27297                if let Some(adj) = object_np.superlative {
27298                    object_superlative = Some((adj, object_np.noun));
27299                }
27300
27301                // Capture a post-object secondary-predicate adjective (consuming it).
27302                if let TokenType::Adjective(a) = self.peek().kind {
27303                    self.advance();
27304                    secondary_adj = Some(a);
27305                }
27306
27307                // Check for distanced phrasal verb particle: "gave the book up"
27308                if let TokenType::Particle(particle_sym) = self.peek().kind {
27309                    let verb_str = self.interner.resolve(verb).to_lowercase();
27310                    let particle_str = self.interner.resolve(particle_sym).to_lowercase();
27311                    if let Some((phrasal_lemma, _class)) =
27312                        crate::lexicon::lookup_phrasal_verb(&verb_str, &particle_str)
27313                    {
27314                        self.advance(); // consume the particle
27315                        verb = self.interner.intern(phrasal_lemma);
27316                    }
27317                }
27318
27319                if let Some(obj_q) = obj_quantifier {
27320                    // Check for opaque verb with indefinite object (de dicto reading)
27321                    // For verbs like "seek", "want", "believe" with indefinite objects,
27322                    // use Term::Intension to represent the intensional (concept) reading
27323                    let verb_str = self.interner.resolve(verb).to_lowercase();
27324                    let is_opaque = lexicon::lookup_verb_db(&verb_str)
27325                        .map(|meta| meta.features.contains(&lexicon::Feature::Opaque))
27326                        .unwrap_or(false);
27327
27328                    // A finite verb after the NP makes it the subject of a
27329                    // CLAUSAL complement, not an object: "believes a spy
27330                    // exists" → Believe(s, ⟨∃y(Spy(y) ∧ Exist(y))⟩).
27331                    if is_opaque && self.check_verb() {
27332                        let emb_var = self.next_var_name();
27333                        let emb_restriction = self.ctx.exprs.alloc(LogicExpr::Predicate {
27334                            name: object_np.noun,
27335                            args: self.ctx.terms.alloc_slice([Term::Variable(emb_var)]),
27336                            world: None,
27337                        });
27338                        let emb_vp = self.parse_predicate_with_subject_as_var(emb_var)?;
27339                        let embedded =
27340                            self.quantify_argument(&obj_q, emb_var, emb_restriction, emb_vp);
27341                        let subject_term_for_event = self.coerce_agent(&subject);
27342                        let main_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
27343                            name: verb,
27344                            args: self
27345                                .ctx
27346                                .terms
27347                                .alloc_slice([subject_term_for_event, Term::Proposition(embedded)]),
27348                            world: None,
27349                        });
27350                        let effective_time = self.pending_time.take().unwrap_or(verb_time);
27351                        let with_time = match effective_time {
27352                            Time::Past => self.ctx.exprs.alloc(LogicExpr::Temporal {
27353                                operator: TemporalOperator::Past,
27354                                body: main_pred,
27355                            }),
27356                            _ => main_pred,
27357                        };
27358                        return self.wrap_with_definiteness_full(&subject, with_time);
27359                    }
27360
27361                    if is_opaque && matches!(obj_q, TokenType::Some) {
27362                        // De dicto reading: use Term::Intension for the theme
27363                        let intension_term = Term::Intension(object_np.noun);
27364
27365                        // Register intensional entity for anaphora resolution
27366                        let event_var = self.get_event_var();
27367                        let mut modifiers = self.collect_adverbs();
27368                        let effective_time = self.pending_time.take().unwrap_or(verb_time);
27369                        match effective_time {
27370                            Time::Past => modifiers.push(self.interner.intern("Past")),
27371                            Time::Future => modifiers.push(self.interner.intern("Future")),
27372                            _ => {}
27373                        }
27374
27375                        let subject_term_for_event = self.coerce_agent(&subject);
27376                        let roles = vec![
27377                            (ThematicRole::Agent, subject_term_for_event),
27378                            (ThematicRole::Theme, intension_term),
27379                        ];
27380
27381                        let suppress_existential = self.drs.in_conditional_antecedent();
27382                        if suppress_existential {
27383                            let event_class = self.interner.intern("Event");
27384                            self.drs.introduce_event_referent(event_var, event_class);
27385                        }
27386                        let neo_event =
27387                            self.ctx
27388                                .exprs
27389                                .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
27390                                    event_var,
27391                                    verb,
27392                                    roles: self.ctx.roles.alloc_slice(roles),
27393                                    modifiers: self.ctx.syms.alloc_slice(modifiers),
27394                                    suppress_existential,
27395                                    world: None,
27396                                })));
27397
27398                        return self.wrap_with_definiteness_full(&subject, neo_event);
27399                    }
27400
27401                    let obj_var = self.next_var_name();
27402
27403                    // Introduce object referent in DRS for cross-sentence anaphora
27404                    let obj_gender = Self::infer_noun_gender(self.interner.resolve(object_np.noun));
27405                    let obj_number = if Self::is_plural_noun(self.interner.resolve(object_np.noun))
27406                    {
27407                        Number::Plural
27408                    } else {
27409                        Number::Singular
27410                    };
27411                    // Definite descriptions presuppose existence, so they should be globally accessible
27412                    if object_np.definiteness == Some(Definiteness::Definite) {
27413                        self.drs.introduce_referent_with_source(
27414                            obj_var,
27415                            object_np.noun,
27416                            obj_gender,
27417                            obj_number,
27418                            ReferentSource::MainClause,
27419                        );
27420                    } else {
27421                        self.drs.introduce_referent(
27422                            obj_var,
27423                            object_np.noun,
27424                            obj_gender,
27425                            obj_number,
27426                        );
27427                    }
27428
27429                    let mut type_pred: &'a LogicExpr<'a> =
27430                        self.ctx.exprs.alloc(LogicExpr::Predicate {
27431                            name: object_np.noun,
27432                            args: self.ctx.terms.alloc_slice([Term::Variable(obj_var)]),
27433                            world: None,
27434                        });
27435
27436                    // Mass→count coercion (§6.2): a count determiner on a mass noun
27437                    // ("a water", "two waters") introduces a PORTION of the stuff:
27438                    // ∃x(Portion(x) ∧ Water(x) ∧ …). The mass noun itself stays
27439                    // non-atomic; the portion is the atom the determiner counts.
27440                    if crate::lexicon::is_mass_noun(
27441                        &self.interner.resolve(object_np.noun).to_lowercase(),
27442                    ) {
27443                        let portion_sym = self.interner.intern("Portion");
27444                        let portion_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
27445                            name: portion_sym,
27446                            args: self.ctx.terms.alloc_slice([Term::Variable(obj_var)]),
27447                            world: None,
27448                        });
27449                        type_pred = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
27450                            left: portion_pred,
27451                            op: TokenType::And,
27452                            right: type_pred,
27453                        });
27454                    }
27455
27456                    // Preserve the object's adjectives and PP restrictors ("has a
27457                    // RED book", "has a maximum range OF 475 ft") — the description
27458                    // is part of the object, and dropping it is a meaning-loss parse.
27459                    for &adj in object_np.adjectives {
27460                        let adj_pred = self.adjective_restriction(adj, obj_var, object_np.noun);
27461                        type_pred = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
27462                            left: type_pred,
27463                            op: TokenType::And,
27464                            right: adj_pred,
27465                        });
27466                    }
27467                    for pp in object_np.pps {
27468                        let pp_sub = self.substitute_pp_placeholder(pp, obj_var);
27469                        type_pred = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
27470                            left: type_pred,
27471                            op: TokenType::And,
27472                            right: pp_sub,
27473                        });
27474                    }
27475
27476                    let obj_restriction =
27477                        if self.check(&TokenType::That) || self.check(&TokenType::Who) {
27478                            self.advance();
27479                            let rel_clause = self.parse_relative_clause(obj_var)?;
27480                            self.ctx.exprs.alloc(LogicExpr::BinaryOp {
27481                                left: type_pred,
27482                                op: TokenType::And,
27483                                right: rel_clause,
27484                            })
27485                        } else {
27486                            type_pred
27487                        };
27488
27489                    // Continuations inside the object quantifier's scope: a
27490                    // recipient ("gave a book to Mary") or an object-control
27491                    // infinitive ("caused a fire to spread").
27492                    let mut recipient: Option<Term<'a>> = None;
27493                    let mut control_infinitive: Option<Symbol> = None;
27494                    if self.check_to_marker() {
27495                        let after_to = self.tokens.get(self.current + 1).map(|t| t.kind.clone());
27496                        match after_to {
27497                            Some(TokenType::Verb { lemma, .. }) => {
27498                                self.advance(); // to
27499                                self.advance(); // infinitive verb
27500                                control_infinitive = Some(lemma);
27501                            }
27502                            Some(TokenType::Noun(word))
27503                                if crate::lexicon::lookup_verb_db(
27504                                    &self.interner.resolve(word).to_lowercase(),
27505                                )
27506                                .is_some() =>
27507                            {
27508                                let lemma_str = crate::lexicon::lookup_verb_db(
27509                                    &self.interner.resolve(word).to_lowercase(),
27510                                )
27511                                .map(|m| m.lemma)
27512                                .unwrap();
27513                                self.advance(); // to
27514                                self.advance(); // infinitive verb (noun-classified)
27515                                control_infinitive = Some(self.interner.intern(lemma_str));
27516                            }
27517                            Some(kind)
27518                                if Lexer::is_ditransitive_verb(&self.interner.resolve(verb))
27519                                    && matches!(
27520                                        kind,
27521                                        TokenType::ProperName(_)
27522                                            | TokenType::Noun(_)
27523                                            | TokenType::Article(_)
27524                                    ) =>
27525                            {
27526                                self.advance(); // to
27527                                let r_np = self.parse_noun_phrase(false)?;
27528                                recipient = Some(Term::Constant(r_np.noun));
27529                            }
27530                            _ => {}
27531                        }
27532                    }
27533
27534                    let event_var = self.get_event_var();
27535                    let mut modifiers = self.collect_adverbs();
27536                    let effective_time = self.pending_time.take().unwrap_or(verb_time);
27537                    match effective_time {
27538                        Time::Past => modifiers.push(self.interner.intern("Past")),
27539                        Time::Future => modifiers.push(self.interner.intern("Future")),
27540                        _ => {}
27541                    }
27542
27543                    let subject_term_for_event = self.coerce_agent(&subject);
27544                    let mut roles = vec![
27545                        (ThematicRole::Agent, subject_term_for_event),
27546                        (ThematicRole::Theme, Term::Variable(obj_var)),
27547                    ];
27548                    if let Some(r) = recipient {
27549                        roles.push((ThematicRole::Recipient, r));
27550                    }
27551
27552                    // Secondary predication over the (quantified) object variable.
27553                    if let Some(adj) = secondary_adj {
27554                        let sec_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
27555                            name: adj,
27556                            args: self.ctx.terms.alloc_slice([Term::Variable(obj_var)]),
27557                            world: None,
27558                        });
27559                        let role = secondary_role(&self.interner.resolve(verb).to_lowercase());
27560                        roles.push((role, Term::Proposition(sec_pred)));
27561                    }
27562
27563                    // Capture template with object type for ellipsis reconstruction
27564                    // Use the object noun type instead of variable for reconstruction
27565                    let template_roles = vec![
27566                        (ThematicRole::Agent, subject_term_for_event),
27567                        (ThematicRole::Theme, Term::Constant(object_np.noun)),
27568                    ];
27569                    self.capture_event_template(verb, &template_roles, &modifiers);
27570
27571                    let suppress_existential = self.drs.in_conditional_antecedent();
27572                    if suppress_existential {
27573                        let event_class = self.interner.intern("Event");
27574                        self.drs.introduce_event_referent(event_var, event_class);
27575                    }
27576                    let neo_event =
27577                        if let Some(inf) = control_infinitive {
27578                            let inf_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
27579                                name: inf,
27580                                args: self.ctx.terms.alloc_slice([Term::Variable(obj_var)]),
27581                                world: None,
27582                            });
27583                            let control = self.ctx.exprs.alloc(LogicExpr::Control {
27584                                verb,
27585                                subject: self.ctx.terms.alloc(subject_term_for_event),
27586                                object: Some(&*self.ctx.terms.alloc(Term::Variable(obj_var))),
27587                                infinitive: inf_pred,
27588                            });
27589                            match effective_time {
27590                                Time::Past => &*self.ctx.exprs.alloc(LogicExpr::Temporal {
27591                                    operator: TemporalOperator::Past,
27592                                    body: control,
27593                                }),
27594                                Time::Future => &*self.ctx.exprs.alloc(LogicExpr::Temporal {
27595                                    operator: TemporalOperator::Future,
27596                                    body: control,
27597                                }),
27598                                _ => control,
27599                            }
27600                        } else {
27601                            let plain = &*self.ctx.exprs.alloc(LogicExpr::NeoEvent(Box::new(
27602                                NeoEventData {
27603                                    event_var,
27604                                    verb,
27605                                    roles: self.ctx.roles.alloc_slice(roles),
27606                                    modifiers: self.ctx.syms.alloc_slice(modifiers),
27607                                    suppress_existential,
27608                                    world: None,
27609                                },
27610                            )));
27611                            // Trailing event-PP adjuncts ("takes a holiday WITH a friend
27612                            // TO some location") attach to the event INSIDE the object's
27613                            // existential scope — the definite-object path consumes these
27614                            // inline; without this they strand after an indefinite object.
27615                            self.attach_trailing_event_pps(plain, event_var)?
27616                        };
27617
27618                    let obj_quantified =
27619                        self.quantify_argument(&obj_q, obj_var, obj_restriction, neo_event);
27620
27621                    // A trailing temporal offset after an INDEFINITE/quantified
27622                    // object ("has a birthday 4 days before Rosemarie") — the
27623                    // constraint relates the SUBJECT's position to the standard's,
27624                    // so it conjoins OUTSIDE the object quantifier. Mirrors the
27625                    // constant-object temporal check; self-guards (no-op without
27626                    // an offset).
27627                    let obj_quantified = {
27628                        let subj = self.coerce_agent(&subject);
27629                        // A trailing temporal OFFSET ("4 days before X") or a BARE
27630                        // temporal ordering ("after Inez", "sometime before Guy") after
27631                        // the quantified object relates the SUBJECT to the standard, so
27632                        // it conjoins OUTSIDE the object quantifier. Without this, an
27633                        // indefinite object ("has an appointment after Inez") stranded
27634                        // the ordering. Both self-guard (no-op when absent).
27635                        let temporal = match self.parse_temporal_offset_constraint(subj)? {
27636                            Some(off) => Some(off),
27637                            None => self.parse_bare_temporal_constraint(subj)?,
27638                        };
27639                        match temporal {
27640                            Some(t) => self.ctx.exprs.alloc(LogicExpr::BinaryOp {
27641                                left: obj_quantified,
27642                                op: TokenType::And,
27643                                right: t,
27644                            }),
27645                            None => obj_quantified,
27646                        }
27647                    };
27648                    // Now wrap the SUBJECT (don't skip it with early return!)
27649                    return self.wrap_with_definiteness_full(&subject, obj_quantified);
27650                } else {
27651                    // Definite object NP (e.g., "the house")
27652                    // Introduce to DRS for cross-sentence bridging anaphora
27653                    // E.g., "John entered the house. The door was open." - door bridges to house
27654                    // Note: was_definite_article is true because the article was consumed before parse_noun_phrase
27655                    if was_definite_article {
27656                        let obj_gender =
27657                            Self::infer_noun_gender(self.interner.resolve(object_np.noun));
27658                        let obj_number =
27659                            if Self::is_plural_noun(self.interner.resolve(object_np.noun)) {
27660                                Number::Plural
27661                            } else {
27662                                Number::Singular
27663                            };
27664                        // Definite descriptions presuppose existence, so they should be globally accessible
27665                        self.drs.introduce_referent_with_source(
27666                            object_np.noun,
27667                            object_np.noun,
27668                            obj_gender,
27669                            obj_number,
27670                            ReferentSource::MainClause,
27671                        );
27672                    }
27673
27674                    let term = self.noun_phrase_to_term(&object_np);
27675                    object_term = Some(term.clone());
27676                    object_adjectives = object_np.adjectives;
27677                    object_desc_pps = object_np.pps;
27678                    args.push(term);
27679
27680                    // Ditransitive with a DEFINITE indirect object: "gave the
27681                    // winner the prize" — the definite IO took this non-quantified
27682                    // branch (a definite article carries no obj_quantifier), so the
27683                    // direct object must still be picked up here. The double-object
27684                    // builder below then assigns Recipient(IO) ∧ Theme(DO); without
27685                    // this the DO strands as a trailing token.
27686                    let verb_str = self.interner.resolve(verb);
27687                    if Lexer::is_ditransitive_verb(verb_str)
27688                        && (self.check_content_word() || self.check_article())
27689                    {
27690                        let second_np = self.parse_noun_phrase(false)?;
27691                        let second_term = Term::Constant(second_np.noun);
27692                        second_object_term = Some(second_term);
27693                        args.push(second_term);
27694                    }
27695                }
27696            } else if self.check_focus() {
27697                let focus_kind = if let TokenType::Focus(k) = self.advance().kind {
27698                    k
27699                } else {
27700                    FocusKind::Only
27701                };
27702
27703                let event_var = self.get_event_var();
27704                let mut modifiers = self.collect_adverbs();
27705                let effective_time = self.pending_time.take().unwrap_or(verb_time);
27706                match effective_time {
27707                    Time::Past => modifiers.push(self.interner.intern("Past")),
27708                    Time::Future => modifiers.push(self.interner.intern("Future")),
27709                    _ => {}
27710                }
27711
27712                let subject_term_for_event = self.coerce_agent(&subject);
27713
27714                if self.check_preposition() {
27715                    let prep_token = self.advance().clone();
27716                    let prep_name = if let TokenType::Preposition(sym) = prep_token.kind {
27717                        sym
27718                    } else {
27719                        self.interner.intern("to")
27720                    };
27721                    let pp_obj = self.parse_noun_phrase(false)?;
27722                    let pp_obj_term = Term::Constant(pp_obj.noun);
27723
27724                    let roles = vec![(ThematicRole::Agent, subject_term_for_event)];
27725                    let suppress_existential = self.drs.in_conditional_antecedent();
27726                    if suppress_existential {
27727                        let event_class = self.interner.intern("Event");
27728                        self.drs.introduce_event_referent(event_var, event_class);
27729                    }
27730                    let neo_event =
27731                        self.ctx
27732                            .exprs
27733                            .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
27734                                event_var,
27735                                verb,
27736                                roles: self.ctx.roles.alloc_slice(roles),
27737                                modifiers: self.ctx.syms.alloc_slice(modifiers),
27738                                suppress_existential,
27739                                world: None,
27740                            })));
27741
27742                    let pp_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
27743                        name: prep_name,
27744                        args: self
27745                            .ctx
27746                            .terms
27747                            .alloc_slice([Term::Variable(event_var), pp_obj_term]),
27748                        world: None,
27749                    });
27750
27751                    let with_pp = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
27752                        left: neo_event,
27753                        op: TokenType::And,
27754                        right: pp_pred,
27755                    });
27756
27757                    let focused_ref = self.ctx.terms.alloc(pp_obj_term);
27758                    return Ok(self.ctx.exprs.alloc(LogicExpr::Focus {
27759                        kind: focus_kind,
27760                        focused: focused_ref,
27761                        scope: with_pp,
27762                    }));
27763                }
27764
27765                let focused_np = self.parse_noun_phrase(false)?;
27766                let focused_term = self.noun_phrase_to_term(&focused_np);
27767                args.push(focused_term.clone());
27768
27769                let roles = vec![
27770                    (ThematicRole::Agent, subject_term_for_event),
27771                    (ThematicRole::Theme, focused_term.clone()),
27772                ];
27773
27774                let suppress_existential = self.drs.in_conditional_antecedent();
27775                if suppress_existential {
27776                    let event_class = self.interner.intern("Event");
27777                    self.drs.introduce_event_referent(event_var, event_class);
27778                }
27779                let neo_event = self
27780                    .ctx
27781                    .exprs
27782                    .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
27783                        event_var,
27784                        verb,
27785                        roles: self.ctx.roles.alloc_slice(roles),
27786                        modifiers: self.ctx.syms.alloc_slice(modifiers),
27787                        suppress_existential,
27788                        world: None,
27789                    })));
27790
27791                let focused_ref = self.ctx.terms.alloc(focused_term);
27792                return Ok(self.ctx.exprs.alloc(LogicExpr::Focus {
27793                    kind: focus_kind,
27794                    focused: focused_ref,
27795                    scope: neo_event,
27796                }));
27797            } else if self.check_number() {
27798                // Handle "has 3 children" or "has cardinality aleph_0"
27799                let measure = self.parse_measure_phrase()?;
27800
27801                // If there's a noun after the measure (for "3 children" where
27802                // children wasn't a unit). An ARTICLE is NOT that noun — it begins
27803                // a rate denominator ("$700 A month", handled below) — so leave it.
27804                if self.check_content_word() && !self.check_article() {
27805                    let noun_sym = self.consume_content_word()?;
27806                    // Build: Has(Subject, 3, Children) where 3 is the count
27807                    let count_term = *measure;
27808                    object_term = Some(count_term.clone());
27809                    args.push(count_term);
27810                    second_object_term = Some(Term::Constant(noun_sym));
27811                    args.push(Term::Constant(noun_sym));
27812                } else {
27813                    // Just the measure: "has cardinality 5"
27814                    object_term = Some(*measure);
27815                    args.push(*measure);
27816                }
27817            } else if self.check_content_word() || self.check_article() {
27818                let object = self.parse_noun_phrase(false)?;
27819                if let Some(adj) = object.superlative {
27820                    object_superlative = Some((adj, object.noun));
27821                }
27822
27823                // A single subject and a single object pair trivially ("… respectively"). A
27824                // list of objects is coordinated at the clause, where each gets the verb
27825                // inside the subject's scope (`try_coordinated_objects`).
27826                if self.check(&TokenType::Respectively) {
27827                    self.advance(); // consume "respectively"
27828                }
27829
27830                let term = self.noun_phrase_to_term(&object);
27831                object_term = Some(term.clone());
27832                args.push(term.clone());
27833
27834                // Check for distanced phrasal verb particle: "gave the book up"
27835                if let TokenType::Particle(particle_sym) = self.peek().kind {
27836                    let verb_str = self.interner.resolve(verb).to_lowercase();
27837                    let particle_str = self.interner.resolve(particle_sym).to_lowercase();
27838                    if let Some((phrasal_lemma, _class)) =
27839                        crate::lexicon::lookup_phrasal_verb(&verb_str, &particle_str)
27840                    {
27841                        self.advance(); // consume the particle
27842                        verb = self.interner.intern(phrasal_lemma);
27843                    }
27844                }
27845
27846                // Check for "has cardinality aleph_0" pattern: noun followed by number
27847                if self.check_number() {
27848                    let measure = self.parse_measure_phrase()?;
27849                    second_object_term = Some(*measure);
27850                    args.push(*measure);
27851                }
27852                // Check for ditransitive: "John gave Mary a book"
27853                else {
27854                    let verb_str = self.interner.resolve(verb);
27855                    if Lexer::is_ditransitive_verb(verb_str)
27856                        && (self.check_content_word() || self.check_article())
27857                    {
27858                        let second_np = self.parse_noun_phrase(false)?;
27859                        let second_term = self.noun_phrase_to_term(&second_np);
27860                        second_object_term = Some(second_term.clone());
27861                        args.push(second_term);
27862                    }
27863                }
27864            }
27865
27866            let mut pp_predicates: Vec<&'a LogicExpr<'a>> = Vec::new();
27867
27868            // Rate denominator after a MEASURE object ("$700 A month", "5 miles
27869            // PER hour", "$2.50 PER pound") → Per(event, unit), a solver-ready
27870            // rate. "a"/"an" or "per" + a CalendarUnit/unit-noun. Guarded to a
27871            // measure object so a bare "a <noun>" elsewhere is not a false rate.
27872            if matches!(object_term, Some(Term::Value { .. })) {
27873                let rate = self.check_preposition_is("per")
27874                    || (matches!(
27875                        self.peek().kind,
27876                        TokenType::Article(Definiteness::Indefinite)
27877                    ) && matches!(
27878                        self.tokens.get(self.current + 1).map(|t| &t.kind),
27879                        Some(TokenType::CalendarUnit(_)) | Some(TokenType::Noun(_))
27880                    ));
27881                if rate {
27882                    self.advance(); // "per" / "a"
27883                    let unit_lexeme = self.peek().lexeme;
27884                    self.advance(); // the unit
27885                    let unit_cap = {
27886                        let s = self.interner.resolve(unit_lexeme).to_string();
27887                        let mut c = s.chars();
27888                        match c.next() {
27889                            Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
27890                            None => s,
27891                        }
27892                    };
27893                    let event_sym = self.get_event_var();
27894                    pp_predicates.push(self.ctx.exprs.alloc(LogicExpr::Predicate {
27895                        name: self.interner.intern("Per"),
27896                        args: self.ctx.terms.alloc_slice([
27897                            Term::Variable(event_sym),
27898                            Term::Constant(self.interner.intern(&unit_cap)),
27899                        ]),
27900                        world: None,
27901                    }));
27902                }
27903            }
27904
27905            while self.check_preposition() || self.check_to() {
27906                // "within N cycles" is a temporal bound, not a PP
27907                if self.check_preposition_is("within")
27908                    && self.current + 1 < self.tokens.len()
27909                    && matches!(
27910                        self.tokens[self.current + 1].kind,
27911                        TokenType::Cardinal(_) | TokenType::Number(_)
27912                    )
27913                {
27914                    break;
27915                }
27916                let prep_token = self.advance().clone();
27917                let prep_name = if let TokenType::Preposition(sym) = prep_token.kind {
27918                    sym
27919                } else if matches!(prep_token.kind, TokenType::To) {
27920                    self.interner.intern("To")
27921                } else {
27922                    continue;
27923                };
27924
27925                let pp_obj_term = if self.check(&TokenType::Reflexive) {
27926                    self.advance();
27927                    self.noun_phrase_to_term(&subject)
27928                } else if self.check_pronoun() {
27929                    let token = self.advance().clone();
27930                    if let TokenType::Pronoun { gender, number, .. } = token.kind {
27931                        let resolved = self.resolve_pronoun(gender, number)?;
27932                        match resolved {
27933                            ResolvedPronoun::Variable(s) => Term::Variable(s),
27934                            ResolvedPronoun::Constant(s) => Term::Constant(s),
27935                        }
27936                    } else {
27937                        continue;
27938                    }
27939                } else if self.check_content_word() || self.check_article() {
27940                    let prep_obj = self.parse_noun_phrase(false)?;
27941                    self.noun_phrase_to_term(&prep_obj)
27942                } else if self.check_number() {
27943                    // Numeric PP object ("sold for $105", "sell for $2.60").
27944                    *self.parse_measure_phrase()?
27945                } else if self.at_clause_boundary()
27946                    && crate::lexicon::is_particle(&self.interner.resolve(prep_name).to_lowercase())
27947                {
27948                    // A clause-final object-less PARTICLE preposition is an
27949                    // intransitive directional ("walked in", "sat down") — a
27950                    // lexically listed class; "of"/"to" cannot end a clause.
27951                    let event_sym = self.get_event_var();
27952                    pp_predicates.push(self.ctx.exprs.alloc(LogicExpr::Predicate {
27953                        name: prep_name,
27954                        args: self.ctx.terms.alloc_slice([Term::Variable(event_sym)]),
27955                        world: None,
27956                    }));
27957                    continue;
27958                } else {
27959                    // A mid-clause preposition with no object is not a PP —
27960                    // hand it back so the sentence-level parse reports it
27961                    // instead of silently dropping it.
27962                    self.current -= 1;
27963                    break;
27964                };
27965
27966                if self.pp_attach_to_noun {
27967                    if let Some(ref obj) = object_term {
27968                        // NP-attachment: PP modifies the object noun
27969                        let pp_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
27970                            name: prep_name,
27971                            args: self.ctx.terms.alloc_slice([obj.clone(), pp_obj_term]),
27972                            world: None,
27973                        });
27974                        pp_predicates.push(pp_pred);
27975                    } else {
27976                        args.push(pp_obj_term);
27977                    }
27978                } else {
27979                    // VP-attachment: PP modifies the event (instrument/manner)
27980                    let event_sym = self.get_event_var();
27981                    let pp_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
27982                        name: prep_name,
27983                        args: self
27984                            .ctx
27985                            .terms
27986                            .alloc_slice([Term::Variable(event_sym), pp_obj_term]),
27987                        world: None,
27988                    });
27989                    pp_predicates.push(pp_pred);
27990                }
27991
27992                // Manner adverbs interleaved with PPs ("with a key quietly in
27993                // the house") modify the same event; absorbing them here lets
27994                // the loop continue to the next PP.
27995                for adv in self.collect_adverbs() {
27996                    let event_sym = self.get_event_var();
27997                    pp_predicates.push(self.ctx.exprs.alloc(LogicExpr::Predicate {
27998                        name: adv,
27999                        args: self.ctx.terms.alloc_slice([Term::Variable(event_sym)]),
28000                        world: None,
28001                    }));
28002                }
28003            }
28004
28005            // Check for trailing relative clause on object NP: "the girl with the telescope that laughed"
28006            if self.check(&TokenType::That) || self.check(&TokenType::Who) {
28007                self.advance();
28008                let rel_var = self.next_var_name();
28009                let rel_pred = self.parse_relative_clause(rel_var)?;
28010                pp_predicates.push(rel_pred);
28011            }
28012
28013            // A trailing temporal offset on the VP ("has a birthday 4 days before
28014            // Rosemarie", "set sail 2 years after Glenda") — N <unit> after/before
28015            // STANDARD relates the subject's position to the standard's
28016            // (solver-ready add/sub). It trails the object, so it is checked here
28017            // after the object/PP loop. A BARE ordering with no count ("won the prize
28018            // before the winner who won in chemistry", "graduated before Orlando")
28019            // takes the same slot — the indefinite-object path already chains both, so
28020            // the definite/possessive-object path (which falls through here) must too,
28021            // or the ordering strands (TrailingTokens at Before/After). Both
28022            // self-guard (return None without consuming unless the pattern matches).
28023            {
28024                let subj = self.coerce_agent(&subject);
28025                let off = match self.parse_temporal_offset_constraint(subj)? {
28026                    Some(o) => Some(o),
28027                    None => self.parse_bare_temporal_constraint(subj)?,
28028                };
28029                if let Some(off) = off {
28030                    pp_predicates.push(off);
28031                }
28032            }
28033
28034            // Collect any trailing adverbs FIRST (before building NeoEvent)
28035            let mut modifiers = self.collect_adverbs();
28036
28037            // Add temporal modifier as part of event semantics
28038            let effective_time = self.pending_time.take().unwrap_or(verb_time);
28039            match effective_time {
28040                Time::Past => modifiers.push(self.interner.intern("Past")),
28041                Time::Future => modifiers.push(self.interner.intern("Future")),
28042                _ => {}
28043            }
28044
28045            // Add aspect modifier if applicable
28046            if verb_aspect == Aspect::Progressive {
28047                modifiers.push(self.interner.intern("Progressive"));
28048            } else if verb_aspect == Aspect::Perfect {
28049                modifiers.push(self.interner.intern("Perfect"));
28050            }
28051
28052            // Build thematic roles for Neo-Davidsonian event semantics
28053            let mut roles: Vec<(ThematicRole, Term<'a>)> = Vec::new();
28054
28055            // Check if verb is unaccusative (intransitive subject is Theme, not Agent)
28056            let verb_str_for_check = self.interner.resolve(verb).to_lowercase();
28057            let is_unaccusative = crate::lexicon::lookup_verb_db(&verb_str_for_check)
28058                .map(|meta| {
28059                    meta.features
28060                        .contains(&crate::lexicon::Feature::Unaccusative)
28061                })
28062                .unwrap_or(false);
28063
28064            // Unaccusative verbs used intransitively: subject is Theme
28065            let has_object = object_term.is_some() || second_object_term.is_some();
28066            let subject_role = if is_unaccusative && !has_object {
28067                ThematicRole::Theme
28068            } else {
28069                ThematicRole::Agent
28070            };
28071
28072            roles.push((subject_role, subject_term));
28073            if let Some(second_obj) = second_object_term {
28074                // Ditransitive: first object is Recipient, second is Theme
28075                if let Some(first_obj) = object_term {
28076                    roles.push((ThematicRole::Recipient, first_obj));
28077                }
28078                roles.push((ThematicRole::Theme, second_obj));
28079            } else if let Some(obj) = object_term {
28080                // Normal transitive: object is Theme
28081                roles.push((ThematicRole::Theme, obj));
28082            }
28083
28084            // Secondary predication: attach the captured post-object adjective as a
28085            // Result/Depictive role over the object ("painted the door red").
28086            if let (Some(adj), Some(obj)) = (secondary_adj, object_term) {
28087                let sec_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
28088                    name: adj,
28089                    args: self.ctx.terms.alloc_slice([obj]),
28090                    world: None,
28091                });
28092                let role = secondary_role(&self.interner.resolve(verb).to_lowercase());
28093                roles.push((role, Term::Proposition(sec_pred)));
28094            }
28095
28096            // Create event variable
28097            let event_var = self.get_event_var();
28098
28099            // Capture template for ellipsis reconstruction before consuming roles
28100            self.capture_event_template(verb, &roles, &modifiers);
28101
28102            // Create NeoEvent structure with all modifiers including time/aspect
28103            let suppress_existential = self.drs.in_conditional_antecedent();
28104            if suppress_existential {
28105                let event_class = self.interner.intern("Event");
28106                self.drs.introduce_event_referent(event_var, event_class);
28107            }
28108            let with_pps = self.event_with_conditions(
28109                NeoEventData {
28110                    event_var,
28111                    verb,
28112                    roles: self.ctx.roles.alloc_slice(roles),
28113                    modifiers: self.ctx.syms.alloc_slice(modifiers),
28114                    suppress_existential,
28115                    world: None,
28116                },
28117                pp_predicates,
28118            );
28119
28120            // Predicate a definite/constant object's adjectives + PP restrictors
28121            // of it ("ate the RED apple" → Red(Apple); "is the frat ON Holly
28122            // Street" → On(·, Holly Street)). Dropping them is meaning loss.
28123            let with_pps = if let Some(obj_term) = object_term {
28124                let placeholder = self.interner.intern("_PP_SELF_");
28125                let mut combined = with_pps;
28126                for &adj in object_adjectives {
28127                    let adj_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
28128                        name: adj,
28129                        args: self.ctx.terms.alloc_slice([obj_term]),
28130                        world: None,
28131                    });
28132                    combined = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
28133                        left: combined,
28134                        op: TokenType::And,
28135                        right: adj_pred,
28136                    });
28137                }
28138                for pp in object_desc_pps {
28139                    if let LogicExpr::Predicate { name, args, world } = pp {
28140                        let new_args: Vec<Term<'a>> = args
28141                            .iter()
28142                            .map(|a| match a {
28143                                Term::Variable(v) if *v == placeholder => obj_term,
28144                                other => *other,
28145                            })
28146                            .collect();
28147                        let sub = self.ctx.exprs.alloc(LogicExpr::Predicate {
28148                            name: *name,
28149                            args: self.ctx.terms.alloc_slice(new_args),
28150                            world: *world,
28151                        });
28152                        combined = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
28153                            left: combined,
28154                            op: TokenType::And,
28155                            right: sub,
28156                        });
28157                    }
28158                }
28159                combined
28160            } else {
28161                with_pps
28162            };
28163
28164            // Apply aspectual operators based on verb class
28165            let with_aspect = if verb_aspect == Aspect::Progressive {
28166                // Semelfactive + Progressive → Iterative
28167                if verb_class == crate::lexicon::VerbClass::Semelfactive {
28168                    self.ctx.exprs.alloc(LogicExpr::Aspectual {
28169                        operator: AspectOperator::Iterative,
28170                        body: with_pps,
28171                    })
28172                } else {
28173                    // Other verbs + Progressive → Progressive
28174                    self.ctx.exprs.alloc(LogicExpr::Aspectual {
28175                        operator: AspectOperator::Progressive,
28176                        body: with_pps,
28177                    })
28178                }
28179            } else if verb_aspect == Aspect::Perfect {
28180                self.ctx.exprs.alloc(LogicExpr::Aspectual {
28181                    operator: AspectOperator::Perfect,
28182                    body: with_pps,
28183                })
28184            } else if effective_time == Time::Present && verb_aspect == Aspect::Simple {
28185                // Non-state verbs in simple present get Habitual reading
28186                if !verb_class.is_stative() {
28187                    self.ctx.exprs.alloc(LogicExpr::Aspectual {
28188                        operator: AspectOperator::Habitual,
28189                        body: with_pps,
28190                    })
28191                } else {
28192                    // State verbs in present: direct predication
28193                    with_pps
28194                }
28195            } else {
28196                with_pps
28197            };
28198
28199            let with_adverbs = with_aspect;
28200
28201            // Check for temporal anchor adverb at end of sentence
28202            let with_temporal = if self.check_temporal_adverb() {
28203                let anchor = if let TokenType::TemporalAdverb(adv) = self.advance().kind.clone() {
28204                    adv
28205                } else {
28206                    panic!("Expected temporal adverb");
28207                };
28208                self.ctx.exprs.alloc(LogicExpr::TemporalAnchor {
28209                    anchor,
28210                    body: with_adverbs,
28211                })
28212            } else {
28213                with_adverbs
28214            };
28215
28216            let wrapped = self.wrap_with_definiteness_full(&subject, with_temporal)?;
28217
28218            // Add superlative constraint for object NP if applicable
28219            if let Some((adj, noun)) = object_superlative {
28220                let superlative_expr = self.ctx.exprs.alloc(LogicExpr::Superlative {
28221                    adjective: adj,
28222                    subject: self.ctx.terms.alloc(Term::Constant(noun)),
28223                    domain: noun,
28224                });
28225                return Ok(self.ctx.exprs.alloc(LogicExpr::BinaryOp {
28226                    left: wrapped,
28227                    op: TokenType::And,
28228                    right: superlative_expr,
28229                }));
28230            }
28231
28232            return Ok(wrapped);
28233        }
28234
28235        Ok(self.ctx.exprs.alloc(LogicExpr::Atom(subject.noun)))
28236    }
28237
28238    fn check_preposition(&self) -> bool {
28239        matches!(self.peek().kind, TokenType::Preposition(_))
28240    }
28241
28242    fn check_by_preposition(&self) -> bool {
28243        if let TokenType::Preposition(p) = self.peek().kind {
28244            p.is(self.interner, "by")
28245        } else {
28246            false
28247        }
28248    }
28249
28250    fn check_preposition_is(&self, word: &str) -> bool {
28251        if let TokenType::Preposition(p) = self.peek().kind {
28252            p.is(self.interner, word)
28253        } else {
28254            false
28255        }
28256    }
28257
28258    /// The optional inclusive-comparison tail `or equal to` after `greater than` / `less than`
28259    /// (`x is greater than or equal to y` → `≥`, `x is less than or equal to y` → `≤`). Consumes the
28260    /// three words ATOMICALLY and returns `true` iff the full `or equal to` follows (matched by lexeme,
28261    /// so the `Or` token, the `equal` word, and the `to` preposition all read); otherwise consumes
28262    /// nothing and returns `false`, leaving a bare `greater than` / `less than` as the strict operator.
28263    fn consume_or_equal_to(&mut self) -> bool {
28264        if self.peek_word_at(0, "or") && self.peek_word_at(1, "equal") && self.peek_word_at(2, "to")
28265        {
28266            self.advance(); // "or"
28267            self.advance(); // "equal"
28268            self.advance(); // "to"
28269            true
28270        } else {
28271            false
28272        }
28273    }
28274
28275    /// Check if current token is a word (noun/adj/verb lexeme) matching the given string
28276    /// Whether the token after the cursor ends a clause — used to tell the `Type …` verb from
28277    /// an ordinary word that happens to be spelled "type".
28278    fn next_is_colon_like(&self) -> bool {
28279        self.tokens.get(self.current + 1).is_some_and(|t| {
28280            matches!(
28281                t.kind,
28282                TokenType::Colon | TokenType::Period | TokenType::Newline
28283            )
28284        })
28285    }
28286
28287    /// Whether the `item` at the cursor opens the `item <index> of <collection>` form. It does
28288    /// when an index-shaped token follows; a closing bracket, separator, operator or end of
28289    /// clause means the word is being used as a variable.
28290    fn item_opens_an_index(&self) -> bool {
28291        // The form always reaches an `of` before the clause ends. Looking for it — rather than
28292        // guessing at the index's shape — keeps every index expression working (`item 2 of xs`,
28293        // `item i of xs`, `item (i + 1) of xs`) while a bare `item` stays a variable.
28294        let mut i = self.current + 1;
28295        let end = (self.current + 24).min(self.tokens.len());
28296        let mut depth = 0i32;
28297        while i < end {
28298            let tok = &self.tokens[i];
28299            match tok.kind {
28300                TokenType::LParen | TokenType::LBracket => depth += 1,
28301                TokenType::RParen | TokenType::RBracket => {
28302                    // A closing bracket that is not ours ends the surrounding expression, so the
28303                    // `item` was an argument, not an index.
28304                    if depth == 0 {
28305                        return false;
28306                    }
28307                    depth -= 1;
28308                }
28309                // Only a clause end at the TOP level rules the index form out — a comma or colon
28310                // inside the index expression (`item f(a, b) of xs`) is part of it.
28311                TokenType::Period | TokenType::Newline if depth == 0 => return false,
28312                TokenType::Comma | TokenType::Colon if depth == 0 => return false,
28313                _ if depth == 0 && self.interner.resolve(tok.lexeme).eq_ignore_ascii_case("of") => {
28314                    return true
28315                }
28316                _ => {}
28317            }
28318            i += 1;
28319        }
28320        false
28321    }
28322
28323    /// Whether the cursor sits on a `State:` / `Actions:` / `View:` / `Style:` section header.
28324    fn next_is_section_header(&self) -> bool {
28325        component::SECTION_WORDS
28326            .iter()
28327            .any(|w| self.lexeme_is(0, w))
28328            && self
28329                .tokens
28330                .get(self.current + 1)
28331                .is_some_and(|t| matches!(t.kind, TokenType::Colon))
28332    }
28333
28334    fn check_word(&self, word: &str) -> bool {
28335        let token = self.peek();
28336        // A string/char LITERAL is a value, never a keyword — a `"not"` literal
28337        // must not be mistaken for the `not` operator (which the lexeme match
28338        // would otherwise do, breaking `Show "not".`).
28339        if matches!(
28340            token.kind,
28341            TokenType::StringLiteral(_) | TokenType::CharLiteral(_)
28342        ) {
28343            return false;
28344        }
28345        let lexeme = self.interner.resolve(token.lexeme);
28346        lexeme.eq_ignore_ascii_case(word)
28347    }
28348
28349    /// Like [`check_word`], but declines when the current token is a bound user
28350    /// variable — an English arithmetic operator word (`plus`, `times`, …) must
28351    /// yield to a variable of the same name (declarer-wins), so `Let times be 5`
28352    /// keeps `times` an identifier rather than the multiply operator.
28353    fn check_op_word(&self, word: &str) -> bool {
28354        self.check_word(word) && !self.user_bound.contains(&self.peek().lexeme)
28355    }
28356
28357    fn peek_word_at(&self, offset: usize, word: &str) -> bool {
28358        if self.current + offset >= self.tokens.len() {
28359            return false;
28360        }
28361        let token = &self.tokens[self.current + offset];
28362        if matches!(
28363            token.kind,
28364            TokenType::StringLiteral(_) | TokenType::CharLiteral(_)
28365        ) {
28366            return false;
28367        }
28368        let lexeme = self.interner.resolve(token.lexeme);
28369        lexeme.eq_ignore_ascii_case(word)
28370    }
28371
28372    fn check_to_preposition(&self) -> bool {
28373        match self.peek().kind {
28374            TokenType::To => true,
28375            TokenType::Preposition(p) => p.is(self.interner, "to"),
28376            _ => false,
28377        }
28378    }
28379
28380    fn check_content_word(&self) -> bool {
28381        match &self.peek().kind {
28382            TokenType::Noun(_)
28383            | TokenType::Adjective(_)
28384            | TokenType::NonIntersectiveAdjective(_)
28385            | TokenType::Verb { .. }
28386            | TokenType::ProperName(_)
28387            | TokenType::Article(_)
28388            // A quoted string in NL prose is a name ("the \"Yellownose\"").
28389            | TokenType::StringLiteral(_)
28390            // "May" the MONTH collides with the modal token; in a content-word
28391            // position ("in May", "on May 3") it is the month noun.
28392            | TokenType::May
28393            // A numeric literal heads/fills a noun position ("equal to 42",
28394            // "2001, 2002, … are years") — consume_content_word already returns it.
28395            | TokenType::Number(_)
28396            | TokenType::Performative(_) => true,
28397            // "item"/"items" are LOGOS collection-index keywords, but in
28398            // declarative NL prose they are ordinary nouns ("three different
28399            // items", "the item is red"). The lexer keeps the keyword tokens (so
28400            // imperative index syntax still lexes); the declarative parser reads
28401            // them as nouns here.
28402            TokenType::Item | TokenType::Items if self.mode == ParserMode::Declarative => true,
28403            // Calendar-unit words ("year", "day", "week") are common nouns in a
28404            // noun position ("four different years", "the year was 2001"); the
28405            // lexer tokenizes them as CalendarUnit for the temporal grammar.
28406            TokenType::CalendarUnit(_) if self.mode == ParserMode::Declarative => true,
28407            TokenType::Ambiguous { primary, alternatives } => {
28408                Self::is_content_word_type(primary)
28409                    || alternatives.iter().any(Self::is_content_word_type)
28410            }
28411            _ => false,
28412        }
28413    }
28414
28415    fn is_content_word_type(t: &TokenType) -> bool {
28416        matches!(
28417            t,
28418            TokenType::Noun(_)
28419                | TokenType::Adjective(_)
28420                | TokenType::NonIntersectiveAdjective(_)
28421                | TokenType::Verb { .. }
28422                | TokenType::ProperName(_)
28423                | TokenType::Article(_)
28424                | TokenType::StringLiteral(_)
28425                | TokenType::Performative(_)
28426        )
28427    }
28428
28429    /// Check for trailing "within N cycles" and wrap in BoundedEventually.
28430    pub(super) fn try_wrap_bounded_delay(&mut self, expr: &'a LogicExpr<'a>) -> &'a LogicExpr<'a> {
28431        if !self.check_preposition_is("within") {
28432            return expr;
28433        }
28434        let has_number = if self.current + 1 < self.tokens.len() {
28435            matches!(
28436                self.tokens[self.current + 1].kind,
28437                TokenType::Cardinal(_) | TokenType::Number(_)
28438            )
28439        } else {
28440            false
28441        };
28442        if !has_number {
28443            return expr;
28444        }
28445        self.advance(); // consume "within"
28446        let bound = match self.advance().kind {
28447            TokenType::Cardinal(n) => n,
28448            TokenType::Number(sym) => {
28449                let n_str = self.interner.resolve(sym);
28450                n_str.parse::<u32>().unwrap_or(1)
28451            }
28452            _ => 1,
28453        };
28454        // Consume optional "cycle" / "cycles"
28455        if self.check_content_word() {
28456            let word = self.interner.resolve(self.peek().lexeme).to_lowercase();
28457            if word == "cycle" || word == "cycles" {
28458                self.advance();
28459            }
28460        }
28461        self.ctx.exprs.alloc(LogicExpr::Temporal {
28462            operator: TemporalOperator::BoundedEventually(bound),
28463            body: expr,
28464        })
28465    }
28466
28467    /// Whether the preposition at the cursor introduces a CYCLE-temporal delay
28468    /// ("in the next cycle", "within 3 cycles", "after 2 cycles") rather than a
28469    /// locative/temporal PP adjunct. Such constructs are left for the SVA temporal
28470    /// wrappers (`try_wrap_next_cycle` / `try_wrap_within_cycles`), so the embedded
28471    /// VP parser's PP-adjunct loop must NOT swallow them.
28472    pub(super) fn pp_is_cycle_temporal(&self) -> bool {
28473        let prep = match &self.peek().kind {
28474            TokenType::Preposition(s) => self.interner.resolve(*s).to_lowercase(),
28475            _ => return false,
28476        };
28477        if !matches!(prep.as_str(), "in" | "within" | "after") {
28478            return false;
28479        }
28480        let mut i = self.current + 1;
28481        let end = (self.current + 5).min(self.tokens.len());
28482        while i < end {
28483            match &self.tokens[i].kind {
28484                TokenType::Period | TokenType::Comma | TokenType::EOF => return false,
28485                _ => {
28486                    let w = self.interner.resolve(self.tokens[i].lexeme).to_lowercase();
28487                    if matches!(w.as_str(), "cycle" | "cycles" | "tick" | "ticks") {
28488                        return true;
28489                    }
28490                }
28491            }
28492            i += 1;
28493        }
28494        false
28495    }
28496
28497    /// Check for a trailing "next cycle" — both "in the next cycle" and the bare adverbial form
28498    /// "…grant is high next cycle." — and wrap in Temporal::Next.
28499    pub(super) fn try_wrap_next_cycle(&mut self, expr: &'a LogicExpr<'a>) -> &'a LogicExpr<'a> {
28500        // Bare "next cycle" (no preposition): "next" immediately followed by "cycle".
28501        let bare_next_cycle = self
28502            .interner
28503            .resolve(self.peek().lexeme)
28504            .eq_ignore_ascii_case("next")
28505            && self
28506                .tokens
28507                .get(self.current + 1)
28508                .map(|t| {
28509                    self.interner
28510                        .resolve(t.lexeme)
28511                        .eq_ignore_ascii_case("cycle")
28512                })
28513                .unwrap_or(false);
28514        if bare_next_cycle {
28515            self.advance(); // next
28516            self.advance(); // cycle
28517            return self.ctx.exprs.alloc(LogicExpr::Temporal {
28518                operator: TemporalOperator::Next,
28519                body: expr,
28520            });
28521        }
28522        if !self.check_preposition_is("in") {
28523            return expr;
28524        }
28525        // Lookahead: "in" (the)? next cycle — bail untouched otherwise.
28526        let mut i = self.current + 1;
28527        if matches!(
28528            self.tokens.get(i).map(|t| &t.kind),
28529            Some(TokenType::Article(_))
28530        ) {
28531            i += 1;
28532        }
28533        let next_matches = self
28534            .tokens
28535            .get(i)
28536            .map(|t| self.interner.resolve(t.lexeme).eq_ignore_ascii_case("next"))
28537            .unwrap_or(false);
28538        let cycle_matches = self
28539            .tokens
28540            .get(i + 1)
28541            .map(|t| {
28542                self.interner
28543                    .resolve(t.lexeme)
28544                    .eq_ignore_ascii_case("cycle")
28545            })
28546            .unwrap_or(false);
28547        if !next_matches || !cycle_matches {
28548            return expr;
28549        }
28550        while self.current <= i + 1 {
28551            self.advance();
28552        }
28553        self.ctx.exprs.alloc(LogicExpr::Temporal {
28554            operator: TemporalOperator::Next,
28555            body: expr,
28556        })
28557    }
28558
28559    fn check_verb(&self) -> bool {
28560        self.kind_is_verb(&self.peek().kind)
28561    }
28562
28563    /// Verb-ness of an arbitrary token kind, mirroring `check_verb` so a look-ahead
28564    /// token (e.g. the participle after a leading adverb) is judged the same way the
28565    /// current token is — an `Ambiguous` token counts when a verb reading is live and
28566    /// noun-priority is off.
28567    fn kind_is_verb(&self, kind: &TokenType) -> bool {
28568        match kind {
28569            TokenType::Verb { .. } => true,
28570            TokenType::Ambiguous {
28571                primary,
28572                alternatives,
28573            } => {
28574                if self.noun_priority_mode {
28575                    return false;
28576                }
28577                matches!(**primary, TokenType::Verb { .. })
28578                    || alternatives
28579                        .iter()
28580                        .any(|t| matches!(t, TokenType::Verb { .. }))
28581            }
28582            _ => false,
28583        }
28584    }
28585
28586    fn check_adverb(&self) -> bool {
28587        matches!(self.peek().kind, TokenType::Adverb(_))
28588    }
28589
28590    fn check_performative(&self) -> bool {
28591        matches!(self.peek().kind, TokenType::Performative(_))
28592    }
28593
28594    /// Resolve an optional direct object following a performative or its
28595    /// infinitival complement into a term. Deictic objects anchor to the
28596    /// discourse roles ("you" → Addressee, "me"/"myself" → Speaker); a proper
28597    /// name yields its constant. Returns `None` when no object NP follows.
28598    fn try_performative_object(&mut self) -> Option<Term<'a>> {
28599        match self.peek().kind {
28600            TokenType::Pronoun { .. } => {
28601                let text = self.interner.resolve(self.peek().lexeme).to_lowercase();
28602                let sym = match text.as_str() {
28603                    "you" => self.interner.intern("Addressee"),
28604                    "me" | "myself" => self.interner.intern("Speaker"),
28605                    other => {
28606                        let cap = other
28607                            .chars()
28608                            .next()
28609                            .map(|c| c.to_uppercase().collect::<String>() + &other[1..])
28610                            .unwrap_or_default();
28611                        self.interner.intern(&cap)
28612                    }
28613                };
28614                self.advance();
28615                Some(Term::Constant(sym))
28616            }
28617            TokenType::ProperName(s) => {
28618                self.advance();
28619                Some(Term::Constant(s))
28620            }
28621            _ => None,
28622        }
28623    }
28624
28625    /// Build the complement predicate `verb(subject [, object])` for a
28626    /// performative, capturing an optional direct object. Used both for the
28627    /// infinitival complement ("promise to call you" → `Call(speaker, hearer)`)
28628    /// and for the bare performative act ("thank you" → `Thank(speaker, hearer)`).
28629    fn performative_predicate(&mut self, verb: Symbol, subject: Symbol) -> &'a LogicExpr<'a> {
28630        if let Some(obj) = self.try_performative_object() {
28631            self.ctx.exprs.alloc(LogicExpr::Predicate {
28632                name: verb,
28633                args: self.ctx.terms.alloc_slice([Term::Constant(subject), obj]),
28634                world: None,
28635            })
28636        } else {
28637            self.ctx.exprs.alloc(LogicExpr::Predicate {
28638                name: verb,
28639                args: self.ctx.terms.alloc_slice([Term::Constant(subject)]),
28640                world: None,
28641            })
28642        }
28643    }
28644
28645    fn collect_adverbs(&mut self) -> Vec<Symbol> {
28646        let mut adverbs = Vec::new();
28647        loop {
28648            if self.check_adverb() {
28649                if let TokenType::Adverb(adv) = self.advance().kind.clone() {
28650                    adverbs.push(adv);
28651                }
28652                // Skip "and" between adverbs
28653                if self.check(&TokenType::And) {
28654                    self.advance();
28655                }
28656                continue;
28657            }
28658            // A trailing bare particle is a DIRECTIONAL event modifier
28659            // ("He sat down." → Sit(e) ∧ Down(e)). Phrasal-verb particles
28660            // ("gave the book up") are consumed by the phrasal lookup before
28661            // this point, so a particle still present here is unclaimed.
28662            if let TokenType::Particle(p) = self.peek().kind {
28663                if matches!(
28664                    self.tokens.get(self.current + 1).map(|t| &t.kind),
28665                    Some(TokenType::Period) | Some(TokenType::EOF) | None
28666                ) {
28667                    self.advance();
28668                    let raw = self.interner.resolve(p).to_string();
28669                    let mut capitalized = raw;
28670                    if let Some(first) = capitalized.get_mut(0..1) {
28671                        first.make_ascii_uppercase();
28672                    }
28673                    adverbs.push(self.interner.intern(&capitalized));
28674                    continue;
28675                }
28676            }
28677            break;
28678        }
28679        adverbs
28680    }
28681
28682    fn check_auxiliary(&self) -> bool {
28683        matches!(self.peek().kind, TokenType::Auxiliary(_))
28684    }
28685
28686    /// Check if the current auxiliary is being used as a true auxiliary (followed by "not" or verb)
28687    /// vs being used as a main verb (like "did" in "did it").
28688    ///
28689    /// "did not bark" → auxiliary usage (emphatic past + negation)
28690    /// "did run" → auxiliary usage (emphatic past)
28691    /// "did it" → main verb usage (past of "do" + object)
28692    fn is_true_auxiliary_usage(&self) -> bool {
28693        if self.current + 1 >= self.tokens.len() {
28694            return false;
28695        }
28696
28697        let next_token = &self.tokens[self.current + 1].kind;
28698
28699        // If followed by "not", it's auxiliary usage
28700        if matches!(next_token, TokenType::Not) {
28701            return true;
28702        }
28703
28704        // If followed by a verb, it's auxiliary usage
28705        if matches!(next_token, TokenType::Verb { .. }) {
28706            return true;
28707        }
28708
28709        // If followed by pronoun (it, him, her, etc.), article, or noun, it's main verb usage
28710        if matches!(
28711            next_token,
28712            TokenType::Pronoun { .. }
28713                | TokenType::Article(_)
28714                | TokenType::Noun(_)
28715                | TokenType::ProperName(_)
28716        ) {
28717            return false;
28718        }
28719
28720        // Default to auxiliary usage for backward compatibility
28721        true
28722    }
28723
28724    /// Check if we have an Auxiliary token that should be treated as a main verb.
28725    /// This is true for "did" when followed by an object (e.g., "the butler did it").
28726    fn check_auxiliary_as_main_verb(&self) -> bool {
28727        if let TokenType::Auxiliary(Time::Past) = self.peek().kind {
28728            // Check if followed by pronoun, article, or noun (object)
28729            if self.current + 1 < self.tokens.len() {
28730                let next = &self.tokens[self.current + 1].kind;
28731                matches!(
28732                    next,
28733                    TokenType::Pronoun { .. }
28734                        | TokenType::Article(_)
28735                        | TokenType::Noun(_)
28736                        | TokenType::ProperName(_)
28737                        // A numeric object ("did 49 previous jumps", "did 3
28738                        // routines") — "did" is the main verb, not do-support.
28739                        | TokenType::Number(_)
28740                )
28741            } else {
28742                false
28743            }
28744        } else {
28745            false
28746        }
28747    }
28748
28749    /// Parse "did" as the main verb "do" (past tense) with a transitive object.
28750    /// This handles constructions like "the butler did it" → Do(e) ∧ Agent(e, butler) ∧ Theme(e, it)
28751    fn parse_do_as_main_verb(&mut self, subject_term: Term<'a>) -> ParseResult<&'a LogicExpr<'a>> {
28752        // Consume the auxiliary token (we're treating it as past tense "do")
28753        let aux_token = self.advance();
28754        let verb_time = if let TokenType::Auxiliary(time) = aux_token.kind {
28755            time
28756        } else {
28757            Time::Past
28758        };
28759
28760        // Intern "Do" as the verb lemma
28761        let verb = self.interner.intern("Do");
28762
28763        let mut object_cardinal: Option<(u32, crate::intern::Symbol, &'a LogicExpr<'a>)> = None;
28764
28765        // Parse the object - handle pronouns specially
28766        let object_term = if let TokenType::Pronoun { .. } = self.peek().kind {
28767            // Pronoun object (like "it" in "did it")
28768            self.advance();
28769            // For "it", we use a generic placeholder or resolved referent
28770            // In the context of "did it", "it" often refers to "the crime" or "the act"
28771            let it_sym = self.interner.intern("it");
28772            Term::Constant(it_sym)
28773        } else if let Some(n) = self.counting_np_lookahead() {
28774            // "did 49 previous jumps" — a counting-NP object: bind ∃=n over the
28775            // deverbal noun + adjectives, wrapping the event below.
28776            self.advance(); // the number
28777            let obj_var = self.next_var_name();
28778            self.nominal_np_context = true;
28779            let obj_np_result = self.parse_noun_phrase(false);
28780            self.nominal_np_context = false;
28781            let obj_np = obj_np_result?;
28782            let mut restr: &'a LogicExpr<'a> = self.ctx.exprs.alloc(LogicExpr::Predicate {
28783                name: obj_np.noun,
28784                args: self.ctx.terms.alloc_slice([Term::Variable(obj_var)]),
28785                world: None,
28786            });
28787            for &adj in obj_np.adjectives {
28788                let adj_pred = self.adjective_restriction(adj, obj_var, obj_np.noun);
28789                restr = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
28790                    left: restr,
28791                    op: TokenType::And,
28792                    right: adj_pred,
28793                });
28794            }
28795            for pp in obj_np.pps {
28796                let pp_sub = self.substitute_pp_placeholder(pp, obj_var);
28797                restr = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
28798                    left: restr,
28799                    op: TokenType::And,
28800                    right: pp_sub,
28801                });
28802            }
28803            object_cardinal = Some((n, obj_var, restr));
28804            Term::Variable(obj_var)
28805        } else if self.check_number() {
28806            // A bare numeric object with no adjective ("did 49 jumps", "did 3
28807            // routines") is a measure-style Theme, like "made 49 jumps".
28808            *self.parse_measure_phrase()?
28809        } else {
28810            let object = self.parse_noun_phrase(false)?;
28811            self.noun_phrase_to_term(&object)
28812        };
28813
28814        // Build Neo-Davidsonian event structure
28815        let event_var = self.get_event_var();
28816        let suppress_existential = self.drs.in_conditional_antecedent();
28817
28818        let mut modifiers = Vec::new();
28819        if verb_time == Time::Past {
28820            modifiers.push(self.interner.intern("Past"));
28821        } else if verb_time == Time::Future {
28822            modifiers.push(self.interner.intern("Future"));
28823        }
28824
28825        let neo_event = self
28826            .ctx
28827            .exprs
28828            .alloc(LogicExpr::NeoEvent(Box::new(NeoEventData {
28829                event_var,
28830                verb,
28831                roles: self.ctx.roles.alloc_slice(vec![
28832                    (ThematicRole::Agent, subject_term),
28833                    (ThematicRole::Theme, object_term),
28834                ]),
28835                modifiers: self.ctx.syms.alloc_slice(modifiers),
28836                suppress_existential,
28837                world: None,
28838            })));
28839
28840        // A counting-NP object binds the event as ∃=n ("did 49 previous jumps"
28841        // → ∃=49 j(Jump(j) ∧ Previous(j) ∧ Do-event(…, j))).
28842        if let Some((n, obj_var, restr)) = object_cardinal {
28843            let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
28844                left: restr,
28845                op: TokenType::And,
28846                right: neo_event,
28847            });
28848            return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
28849                kind: QuantifierKind::Cardinal(n),
28850                variable: obj_var,
28851                body,
28852                island_id: self.current_island,
28853            }));
28854        }
28855
28856        Ok(neo_event)
28857    }
28858
28859    fn check_to(&self) -> bool {
28860        matches!(self.peek().kind, TokenType::To)
28861    }
28862
28863    /// The marker "to" regardless of how the lexer classified it. Context
28864    /// decides between `TokenType::To` and `Preposition("to")`, but the
28865    /// grammar concept — an infinitival or dative marker — is one thing.
28866    pub(super) fn check_to_marker(&self) -> bool {
28867        self.check_to() || self.check_preposition_is("to")
28868    }
28869
28870    /// Look ahead in the token stream for modal subordinating verbs (would, could, should, might).
28871    /// These verbs allow modal subordination: continuing a hypothetical context from a prior sentence.
28872    /// E.g., "A wolf might enter. It would eat you." - "would" subordinates to "might".
28873    fn has_modal_subordination_ahead(&self) -> bool {
28874        // Modal subordination verbs: would, could, should, might
28875        // These allow access to hypothetical entities from prior modal sentences
28876        for i in self.current..self.tokens.len() {
28877            match &self.tokens[i].kind {
28878                TokenType::Would | TokenType::Could | TokenType::Should | TokenType::Might => {
28879                    return true;
28880                }
28881                // Stop looking at sentence boundary
28882                TokenType::Period | TokenType::EOF => break,
28883                _ => {}
28884            }
28885        }
28886        false
28887    }
28888
28889    fn consume_verb(&mut self) -> Symbol {
28890        let t = self.advance().clone();
28891        match t.kind {
28892            TokenType::Verb { lemma, .. } => lemma,
28893            TokenType::Ambiguous { primary, .. } => match *primary {
28894                TokenType::Verb { lemma, .. } => lemma,
28895                _ => panic!("Expected verb in Ambiguous primary, got {:?}", primary),
28896            },
28897            _ => panic!("Expected verb, got {:?}", t.kind),
28898        }
28899    }
28900
28901    fn consume_verb_with_metadata(&mut self) -> (Symbol, Time, Aspect, VerbClass) {
28902        let t = self.advance().clone();
28903        match t.kind {
28904            TokenType::Verb {
28905                lemma,
28906                time,
28907                aspect,
28908                class,
28909            } => (lemma, time, aspect, class),
28910            TokenType::Ambiguous { primary, .. } => match *primary {
28911                TokenType::Verb {
28912                    lemma,
28913                    time,
28914                    aspect,
28915                    class,
28916                } => (lemma, time, aspect, class),
28917                _ => panic!("Expected verb in Ambiguous primary, got {:?}", primary),
28918            },
28919            _ => panic!("Expected verb, got {:?}", t.kind),
28920        }
28921    }
28922
28923    fn match_token(&mut self, types: &[TokenType]) -> bool {
28924        for t in types {
28925            if self.check(t) {
28926                self.advance();
28927                return true;
28928            }
28929        }
28930        false
28931    }
28932
28933    fn check_quantifier(&self) -> bool {
28934        matches!(self.peek().kind, TokenType::Any)
28935            || QuantifierKind::of_determiner(&self.peek().kind).is_some()
28936    }
28937
28938    fn check_npi_quantifier(&self) -> bool {
28939        matches!(
28940            self.peek().kind,
28941            TokenType::Nobody | TokenType::Nothing | TokenType::NoOne
28942        )
28943    }
28944
28945    fn check_npi_object(&self) -> bool {
28946        matches!(self.peek().kind, TokenType::Anything | TokenType::Anyone)
28947    }
28948
28949    fn check_temporal_npi(&self) -> bool {
28950        matches!(self.peek().kind, TokenType::Ever | TokenType::Never)
28951    }
28952
28953    fn parse_npi_quantified(&mut self) -> ParseResult<&'a LogicExpr<'a>> {
28954        let npi_token = self.advance().kind.clone();
28955        let var_name = self.next_var_name();
28956
28957        let (restriction_name, is_person) = match npi_token {
28958            TokenType::Nobody | TokenType::NoOne => ("Person", true),
28959            TokenType::Nothing => ("Thing", false),
28960            _ => ("Thing", false),
28961        };
28962
28963        let restriction_sym = self.interner.intern(restriction_name);
28964        let subject_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
28965            name: restriction_sym,
28966            args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
28967            world: None,
28968        });
28969
28970        self.negative_depth += 1;
28971
28972        let verb = self.consume_verb();
28973
28974        if self.check_npi_object() {
28975            let obj_npi_token = self.advance().kind.clone();
28976            let obj_var = self.next_var_name();
28977
28978            let obj_restriction_name = match obj_npi_token {
28979                TokenType::Anything => "Thing",
28980                TokenType::Anyone => "Person",
28981                _ => "Thing",
28982            };
28983
28984            let obj_restriction_sym = self.interner.intern(obj_restriction_name);
28985            let obj_restriction = self.ctx.exprs.alloc(LogicExpr::Predicate {
28986                name: obj_restriction_sym,
28987                args: self.ctx.terms.alloc_slice([Term::Variable(obj_var)]),
28988                world: None,
28989            });
28990
28991            let verb_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
28992                name: verb,
28993                args: self
28994                    .ctx
28995                    .terms
28996                    .alloc_slice([Term::Variable(var_name), Term::Variable(obj_var)]),
28997                world: None,
28998            });
28999
29000            let verb_and_obj = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
29001                left: obj_restriction,
29002                op: TokenType::And,
29003                right: verb_pred,
29004            });
29005
29006            let inner_existential = self.ctx.exprs.alloc(LogicExpr::Quantifier {
29007                kind: crate::ast::QuantifierKind::Existential,
29008                variable: obj_var,
29009                body: verb_and_obj,
29010                island_id: self.current_island,
29011            });
29012
29013            self.negative_depth -= 1;
29014
29015            let negated = self.ctx.exprs.alloc(LogicExpr::UnaryOp {
29016                op: TokenType::Not,
29017                operand: inner_existential,
29018            });
29019
29020            let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
29021                left: subject_pred,
29022                op: TokenType::Implies,
29023                right: negated,
29024            });
29025
29026            return Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
29027                kind: crate::ast::QuantifierKind::Universal,
29028                variable: var_name,
29029                body,
29030                island_id: self.current_island,
29031            }));
29032        }
29033
29034        let verb_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
29035            name: verb,
29036            args: self.ctx.terms.alloc_slice([Term::Variable(var_name)]),
29037            world: None,
29038        });
29039
29040        self.negative_depth -= 1;
29041
29042        let negated_verb = self.ctx.exprs.alloc(LogicExpr::UnaryOp {
29043            op: TokenType::Not,
29044            operand: verb_pred,
29045        });
29046
29047        let body = self.ctx.exprs.alloc(LogicExpr::BinaryOp {
29048            left: subject_pred,
29049            op: TokenType::Implies,
29050            right: negated_verb,
29051        });
29052
29053        Ok(self.ctx.exprs.alloc(LogicExpr::Quantifier {
29054            kind: crate::ast::QuantifierKind::Universal,
29055            variable: var_name,
29056            body,
29057            island_id: self.current_island,
29058        }))
29059    }
29060
29061    fn parse_temporal_npi(&mut self) -> ParseResult<&'a LogicExpr<'a>> {
29062        let npi_token = self.advance().kind.clone();
29063        let is_never = matches!(npi_token, TokenType::Never);
29064
29065        let subject = self.parse_noun_phrase(true)?;
29066
29067        if is_never {
29068            self.negative_depth += 1;
29069        }
29070
29071        let verb = self.consume_verb();
29072        let verb_pred = self.ctx.exprs.alloc(LogicExpr::Predicate {
29073            name: verb,
29074            args: self.ctx.terms.alloc_slice([Term::Constant(subject.noun)]),
29075            world: None,
29076        });
29077
29078        if is_never {
29079            self.negative_depth -= 1;
29080            Ok(self.ctx.exprs.alloc(LogicExpr::UnaryOp {
29081                op: TokenType::Not,
29082                operand: verb_pred,
29083            }))
29084        } else {
29085            Ok(verb_pred)
29086        }
29087    }
29088
29089    fn check(&self, kind: &TokenType) -> bool {
29090        if self.is_at_end() {
29091            return false;
29092        }
29093        std::mem::discriminant(&self.peek().kind) == std::mem::discriminant(kind)
29094    }
29095
29096    fn check_any(&self, kinds: &[TokenType]) -> bool {
29097        if self.is_at_end() {
29098            return false;
29099        }
29100        let current = std::mem::discriminant(&self.peek().kind);
29101        kinds.iter().any(|k| std::mem::discriminant(k) == current)
29102    }
29103
29104    fn check_article(&self) -> bool {
29105        matches!(self.peek().kind, TokenType::Article(_))
29106    }
29107
29108    fn advance(&mut self) -> &Token {
29109        if !self.is_at_end() {
29110            self.current += 1;
29111        }
29112        self.previous()
29113    }
29114
29115    fn is_at_end(&self) -> bool {
29116        self.peek().kind == TokenType::EOF
29117    }
29118
29119    fn peek(&self) -> &Token {
29120        // Clamp to the EOF terminator (guaranteed by `Parser::new`): a walk
29121        // that overshoots reads EOF forever instead of panicking.
29122        self.tokens
29123            .get(self.current)
29124            .unwrap_or_else(|| self.tokens.last().expect("stream ends with EOF"))
29125    }
29126
29127    /// Phase 35: Check if the next token (after current) is a string literal.
29128    /// Used to distinguish causal `because` from Trust's `because "reason"`.
29129    fn peek_next_is_string_literal(&self) -> bool {
29130        self.tokens
29131            .get(self.current + 1)
29132            .map(|t| matches!(t.kind, TokenType::StringLiteral(_)))
29133            .unwrap_or(false)
29134    }
29135
29136    fn previous(&self) -> &Token {
29137        // Clamp at the start of the stream: asking for the token before position
29138        // 0 (only reachable on malformed input, e.g. a lookahead that never
29139        // consumed) reads the first token instead of underflowing `current - 1`
29140        // into a panic. Mirrors `peek`'s EOF clamp — the parser never crashes on
29141        // adversarial input, it produces a structured error.
29142        let idx = self.current.saturating_sub(1);
29143        self.tokens
29144            .get(idx)
29145            .unwrap_or_else(|| self.tokens.last().expect("stream ends with EOF"))
29146    }
29147
29148    fn current_span(&self) -> crate::token::Span {
29149        self.peek().span
29150    }
29151
29152    fn consume(&mut self, kind: TokenType) -> ParseResult<&Token> {
29153        if self.check(&kind) {
29154            Ok(self.advance())
29155        } else {
29156            Err(ParseError {
29157                kind: ParseErrorKind::UnexpectedToken {
29158                    expected: kind,
29159                    found: self.peek().kind.clone(),
29160                },
29161                span: self.current_span(),
29162            })
29163        }
29164    }
29165
29166    /// After the first word of a proper name has been consumed, absorb any
29167    /// subsequent capitalized content words into one multi-word proper-name
29168    /// symbol ("Porcher" + "Place" → "Porcher_Place", "Highland" + "Drive" →
29169    /// "Highland_Drive"). The trailing words frequently lex as common
29170    /// nouns/verbs/adjectives, so the test is surface capitalization, not token
29171    /// class. Stops at the first lowercase word or non-content token.
29172    pub(super) fn absorb_multiword_proper_name(&mut self, first: Symbol) -> Symbol {
29173        let mut full = self.interner.resolve(first).to_string();
29174        loop {
29175            let next = self.peek();
29176            let is_content = matches!(
29177                next.kind,
29178                TokenType::ProperName(_)
29179                    | TokenType::Noun(_)
29180                    | TokenType::Adjective(_)
29181                    | TokenType::Verb { .. }
29182                    | TokenType::Ambiguous { .. }
29183            );
29184            let capitalized = self
29185                .interner
29186                .resolve(next.lexeme)
29187                .chars()
29188                .next()
29189                .map_or(false, |c| c.is_ascii_uppercase());
29190            if is_content && capitalized {
29191                full.push('_');
29192                full.push_str(&self.interner.resolve(self.peek().lexeme));
29193                self.advance();
29194            } else {
29195                break;
29196            }
29197        }
29198        self.interner.intern(&full)
29199    }
29200
29201    fn consume_content_word(&mut self) -> ParseResult<Symbol> {
29202        let t = self.advance().clone();
29203        match t.kind {
29204            TokenType::Noun(s)
29205            | TokenType::Adjective(s)
29206            | TokenType::NonIntersectiveAdjective(s) => Ok(s),
29207            // "item"/"items" are LOGOS index keywords but ordinary nouns in
29208            // declarative NL — read them as the noun via their (capitalized) lexeme.
29209            TokenType::Item | TokenType::Items if self.mode == ParserMode::Declarative => {
29210                let lex = self.interner.resolve(t.lexeme);
29211                let mut chars = lex.chars();
29212                let cap = match chars.next() {
29213                    Some(f) => f.to_uppercase().collect::<String>() + chars.as_str(),
29214                    None => String::new(),
29215                };
29216                Ok(self.interner.intern(&cap))
29217            }
29218            // A quoted string in NL prose names an entity ("the \"Yellownose\"").
29219            TokenType::StringLiteral(s) => {
29220                let s_str = self.interner.resolve(s);
29221                let gender = Self::infer_gender(s_str);
29222                self.drs.introduce_proper_name(s, s, gender);
29223                Ok(s)
29224            }
29225            // Phase 35: Allow single-letter articles (a, an) to be used as variable names
29226            TokenType::Article(_) => Ok(t.lexeme),
29227            // "May" the month (collides with the modal token) — use its lexeme.
29228            TokenType::May => Ok(self.interner.intern("May")),
29229            // Phase 35: Allow numeric literals as content words (e.g., "equal to 42")
29230            TokenType::Number(s) => Ok(s),
29231            // Calendar-unit words ("year", "day", "week") are common nouns (the
29232            // lexicon carries Year/Day/Week/… entries) that the lexer tokenizes as
29233            // CalendarUnit for the temporal grammar. In a noun position they are
29234            // their singular lemma, so "four different years" → Year(...), "the
29235            // year was 2001" → Year. The unit variant IS the lemma.
29236            TokenType::CalendarUnit(u) => {
29237                let lemma = match u {
29238                    crate::token::CalendarUnit::Second => "Second",
29239                    crate::token::CalendarUnit::Minute => "Minute",
29240                    crate::token::CalendarUnit::Hour => "Hour",
29241                    crate::token::CalendarUnit::Day => "Day",
29242                    crate::token::CalendarUnit::Week => "Week",
29243                    crate::token::CalendarUnit::Month => "Month",
29244                    crate::token::CalendarUnit::Year => "Year",
29245                    crate::token::CalendarUnit::Millisecond => "Millisecond",
29246                    crate::token::CalendarUnit::Microsecond => "Microsecond",
29247                    crate::token::CalendarUnit::Nanosecond => "Nanosecond",
29248                };
29249                Ok(self.interner.intern(lemma))
29250            }
29251            TokenType::ProperName(s) => {
29252                // In imperative mode, proper names are variable references that must be defined
29253                if self.mode == ParserMode::Imperative {
29254                    if !self.drs.has_referent_by_variable(s) {
29255                        return Err(ParseError {
29256                            kind: ParseErrorKind::UndefinedVariable {
29257                                name: self.interner.resolve(s).to_string(),
29258                            },
29259                            span: t.span,
29260                        });
29261                    }
29262                    return Ok(s);
29263                }
29264
29265                // Declarative mode: auto-register proper names as entities
29266                let s_str = self.interner.resolve(s);
29267                let gender = Self::infer_gender(s_str);
29268
29269                // Register in DRS for cross-sentence anaphora resolution
29270                self.drs.introduce_proper_name(s, s, gender);
29271
29272                Ok(s)
29273            }
29274            TokenType::Verb { lemma, .. } => Ok(lemma),
29275            TokenType::Performative(s) => Ok(s),
29276            TokenType::Ambiguous { primary, .. } => {
29277                match *primary {
29278                    TokenType::Noun(s)
29279                    | TokenType::Adjective(s)
29280                    | TokenType::NonIntersectiveAdjective(s) => Ok(s),
29281                    TokenType::Verb { lemma, .. } => Ok(lemma),
29282                    TokenType::Performative(s) => Ok(s),
29283                    TokenType::ProperName(s) => {
29284                        // In imperative mode, proper names must be defined
29285                        if self.mode == ParserMode::Imperative {
29286                            if !self.drs.has_referent_by_variable(s) {
29287                                return Err(ParseError {
29288                                    kind: ParseErrorKind::UndefinedVariable {
29289                                        name: self.interner.resolve(s).to_string(),
29290                                    },
29291                                    span: t.span,
29292                                });
29293                            }
29294                            return Ok(s);
29295                        }
29296                        // Register proper name in DRS for ambiguous tokens too
29297                        let s_str = self.interner.resolve(s);
29298                        let gender = Self::infer_gender(s_str);
29299                        self.drs.introduce_proper_name(s, s, gender);
29300                        Ok(s)
29301                    }
29302                    _ => Err(ParseError {
29303                        kind: ParseErrorKind::ExpectedContentWord { found: *primary },
29304                        span: self.current_span(),
29305                    }),
29306                }
29307            }
29308            other => Err(ParseError {
29309                kind: ParseErrorKind::ExpectedContentWord { found: other },
29310                span: self.current_span(),
29311            }),
29312        }
29313    }
29314
29315    fn consume_copula(&mut self) -> ParseResult<()> {
29316        if self.match_token(&[
29317            TokenType::Is,
29318            TokenType::Are,
29319            TokenType::Was,
29320            TokenType::Were,
29321        ]) {
29322            Ok(())
29323        } else {
29324            Err(ParseError {
29325                kind: ParseErrorKind::ExpectedCopula,
29326                span: self.current_span(),
29327            })
29328        }
29329    }
29330
29331    fn check_comparative(&self) -> bool {
29332        matches!(self.peek().kind, TokenType::Comparative(_))
29333    }
29334
29335    fn is_contact_clause_pattern(&self) -> bool {
29336        // Detect "The cat [the dog chased] ran" pattern
29337        // Also handles nested: "The rat [the cat [the dog chased] ate] died"
29338        let mut pos = self.current;
29339
29340        // Skip the article we're at
29341        if pos < self.tokens.len() && matches!(self.tokens[pos].kind, TokenType::Article(_)) {
29342            pos += 1;
29343        } else {
29344            return false;
29345        }
29346
29347        // Skip adjectives
29348        while pos < self.tokens.len() && matches!(self.tokens[pos].kind, TokenType::Adjective(_)) {
29349            pos += 1;
29350        }
29351
29352        // Must have noun/proper name (embedded subject)
29353        if pos < self.tokens.len()
29354            && matches!(
29355                self.tokens[pos].kind,
29356                TokenType::Noun(_) | TokenType::ProperName(_) | TokenType::Adjective(_)
29357            )
29358        {
29359            pos += 1;
29360        } else {
29361            return false;
29362        }
29363
29364        // Must have verb OR another article (nested contact clause) after
29365        pos < self.tokens.len()
29366            && matches!(
29367                self.tokens[pos].kind,
29368                TokenType::Verb { .. } | TokenType::Article(_)
29369            )
29370    }
29371
29372    fn check_superlative(&self) -> bool {
29373        matches!(self.peek().kind, TokenType::Superlative(_))
29374    }
29375
29376    fn check_scopal_adverb(&self) -> bool {
29377        matches!(self.peek().kind, TokenType::ScopalAdverb(_))
29378    }
29379
29380    fn check_temporal_adverb(&self) -> bool {
29381        matches!(self.peek().kind, TokenType::TemporalAdverb(_))
29382    }
29383
29384    fn check_non_intersective_adjective(&self) -> bool {
29385        matches!(self.peek().kind, TokenType::NonIntersectiveAdjective(_))
29386    }
29387
29388    fn check_focus(&self) -> bool {
29389        matches!(self.peek().kind, TokenType::Focus(_))
29390    }
29391
29392    fn check_measure(&self) -> bool {
29393        matches!(self.peek().kind, TokenType::Measure(_))
29394    }
29395
29396    fn check_presup_trigger(&self) -> bool {
29397        match &self.peek().kind {
29398            TokenType::PresupTrigger(_) => true,
29399            TokenType::Verb { lemma, .. } => {
29400                let s = self.interner.resolve(*lemma).to_lowercase();
29401                crate::lexicon::lookup_presup_trigger(&s).is_some()
29402            }
29403            TokenType::Ambiguous { primary, .. } => match primary.as_ref() {
29404                TokenType::Verb { lemma, .. } => {
29405                    let s = self.interner.resolve(*lemma).to_lowercase();
29406                    crate::lexicon::lookup_presup_trigger(&s).is_some()
29407                }
29408                _ => false,
29409            },
29410            _ => false,
29411        }
29412    }
29413
29414    fn consume_presup_trigger(&mut self) -> crate::token::PresupKind {
29415        let lemma = match self.advance().kind.clone() {
29416            TokenType::PresupTrigger(kind) => return kind,
29417            TokenType::Verb { lemma, .. } => lemma,
29418            TokenType::Ambiguous { primary, .. } => match *primary {
29419                TokenType::Verb { lemma, .. } => lemma,
29420                other => unreachable!("guarded by check_presup_trigger, got {other:?}"),
29421            },
29422            other => unreachable!("guarded by check_presup_trigger, got {other:?}"),
29423        };
29424        let s = self.interner.resolve(lemma).to_lowercase();
29425        crate::lexicon::lookup_presup_trigger(&s)
29426            .expect("Lexicon mismatch: Verb flagged as trigger but lookup failed")
29427    }
29428
29429    fn is_followed_by_np_object(&self) -> bool {
29430        if self.current + 1 >= self.tokens.len() {
29431            return false;
29432        }
29433        let next = &self.tokens[self.current + 1].kind;
29434        matches!(
29435            next,
29436            TokenType::ProperName(_)
29437                | TokenType::Article(_)
29438                | TokenType::Noun(_)
29439                | TokenType::Pronoun { .. }
29440                | TokenType::Reflexive
29441                | TokenType::Who
29442                | TokenType::What
29443                | TokenType::Where
29444                | TokenType::When
29445                | TokenType::Why
29446        )
29447    }
29448
29449    fn is_followed_by_gerund(&self) -> bool {
29450        if self.current + 1 >= self.tokens.len() {
29451            return false;
29452        }
29453        matches!(self.tokens[self.current + 1].kind, TokenType::Verb { .. })
29454    }
29455
29456    // =========================================================================
29457    // Phase 46: Agent System Parsing
29458    // =========================================================================
29459
29460    /// Parse spawn statement: "Spawn a Worker called 'w1'."
29461    fn parse_spawn_statement(&mut self) -> ParseResult<Stmt<'a>> {
29462        self.advance(); // consume "Spawn"
29463
29464        // Expect article (a/an)
29465        if !self.check_article() {
29466            return Err(ParseError {
29467                kind: ParseErrorKind::ExpectedKeyword {
29468                    keyword: "a/an".to_string(),
29469                },
29470                span: self.current_span(),
29471            });
29472        }
29473        self.advance(); // consume article
29474
29475        // Get agent type name (Noun or ProperName)
29476        let agent_type = match &self.tokens[self.current].kind {
29477            TokenType::Noun(sym) | TokenType::ProperName(sym) => {
29478                let s = *sym;
29479                self.advance();
29480                s
29481            }
29482            _ => {
29483                return Err(ParseError {
29484                    kind: ParseErrorKind::ExpectedKeyword {
29485                        keyword: "agent type".to_string(),
29486                    },
29487                    span: self.current_span(),
29488                });
29489            }
29490        };
29491
29492        // Expect "called"
29493        if !self.check(&TokenType::Called) {
29494            return Err(ParseError {
29495                kind: ParseErrorKind::ExpectedKeyword {
29496                    keyword: "called".to_string(),
29497                },
29498                span: self.current_span(),
29499            });
29500        }
29501        self.advance(); // consume "called"
29502
29503        // Get agent name (string literal)
29504        let name = if let TokenType::StringLiteral(sym) = &self.tokens[self.current].kind {
29505            let s = *sym;
29506            self.advance();
29507            s
29508        } else {
29509            return Err(ParseError {
29510                kind: ParseErrorKind::ExpectedKeyword {
29511                    keyword: "agent name".to_string(),
29512                },
29513                span: self.current_span(),
29514            });
29515        };
29516
29517        Ok(Stmt::Spawn { agent_type, name })
29518    }
29519
29520    /// Parse send statement: "Send Ping to 'agent'."
29521    fn parse_send_statement(&mut self) -> ParseResult<Stmt<'a>> {
29522        self.advance(); // consume "Send"
29523
29524        // Optional `cached`, `unchecked`, and `compressed [with <codec>]` modifiers,
29525        // in any order. Bare `compressed` = deflate; `with deflate|lz4|zstd` picks it.
29526        let mut cached = false;
29527        let mut unchecked = false;
29528        let mut compression = None;
29529        let mut layout = None;
29530        let mut shared = false;
29531        let mut computed = false;
29532        let mut indexed = false;
29533        let mut deduped = false;
29534        let mut unreliable = false;
29535        loop {
29536            if !cached && self.check_word("cached") {
29537                self.advance();
29538                cached = true;
29539            } else if !shared && (self.check_word("shared") || self.check_word("known")) {
29540                self.advance();
29541                shared = true;
29542            } else if !indexed && (self.check_word("indexed") || self.check_word("addressable")) {
29543                self.advance();
29544                indexed = true;
29545            } else if !deduped && (self.check_word("deduped") || self.check_word("shared_refs")) {
29546                self.advance();
29547                deduped = true;
29548            } else if !computed && self.check_word("computed") {
29549                self.advance();
29550                computed = true;
29551            } else if !unchecked && self.check_word("unchecked") {
29552                self.advance();
29553                unchecked = true;
29554            } else if !unreliable && self.check_word("unreliably") {
29555                self.advance();
29556                unreliable = true;
29557            } else if layout.is_none() && (self.check_word("fast") || self.check_word("quickly")) {
29558                self.advance();
29559                layout = Some(SendLayout::Fast);
29560            } else if layout.is_none() && (self.check_word("compact") || self.check_word("small")) {
29561                self.advance();
29562                layout = Some(SendLayout::Compact);
29563            } else if layout.is_none() && self.check_word("packed") {
29564                self.advance();
29565                layout = Some(SendLayout::Packed);
29566            } else if layout.is_none() && (self.check_word("smallest") || self.check_word("best")) {
29567                self.advance();
29568                layout = Some(SendLayout::Smallest);
29569            } else if layout.is_none() && (self.check_word("redundant") || self.check_word("tough"))
29570            {
29571                self.advance();
29572                layout = Some(SendLayout::Redundant);
29573            } else if compression.is_none() && self.check_word("compressed") {
29574                self.advance();
29575                let codec = if self.check_word("with") {
29576                    self.advance();
29577                    let name = self
29578                        .interner
29579                        .resolve(self.peek().lexeme)
29580                        .to_ascii_lowercase();
29581                    let c = match name.as_str() {
29582                        "deflate" => CompressionCodec::Deflate,
29583                        "lz4" => CompressionCodec::Lz4,
29584                        "zstd" => CompressionCodec::Zstd,
29585                        _ => {
29586                            return Err(ParseError {
29587                                kind: ParseErrorKind::ExpectedKeyword {
29588                                    keyword: "a codec (deflate, lz4, or zstd)".to_string(),
29589                                },
29590                                span: self.current_span(),
29591                            })
29592                        }
29593                    };
29594                    self.advance(); // consume the codec name
29595                    c
29596                } else {
29597                    CompressionCodec::Deflate
29598                };
29599                compression = Some(codec);
29600            } else {
29601                break;
29602            }
29603        }
29604
29605        // Parse message expression
29606        let message = self.parse_imperative_expr()?;
29607
29608        // Expect "to"
29609        if !self.check_preposition_is("to") {
29610            return Err(ParseError {
29611                kind: ParseErrorKind::ExpectedKeyword {
29612                    keyword: "to".to_string(),
29613                },
29614                span: self.current_span(),
29615            });
29616        }
29617        self.advance(); // consume "to"
29618
29619        // Parse destination expression
29620        let destination = self.parse_imperative_expr()?;
29621
29622        Ok(Stmt::SendMessage {
29623            message,
29624            destination,
29625            compression,
29626            cached,
29627            unchecked,
29628            layout,
29629            shared,
29630            computed,
29631            indexed,
29632            deduped,
29633            unreliable,
29634        })
29635    }
29636
29637    /// Parse await statement: "Await response from 'agent' into result."
29638    fn parse_await_statement(&mut self) -> ParseResult<Stmt<'a>> {
29639        self.advance(); // consume "Await"
29640
29641        // Optional `view` knob: receive a record-list LAZILY (zero-copy, decode-on-touch).
29642        // Optional `stream` knob: receive a batch STREAM and deframe it into a list.
29643        let mut view = false;
29644        let mut stream = false;
29645        if self.check_word("view") || self.check_word("viewed") {
29646            view = true;
29647            self.advance();
29648        }
29649        if self.check_word("stream") {
29650            stream = true;
29651            self.advance();
29652        }
29653
29654        // Skip optional "response" word
29655        if self.check_word("response") {
29656            self.advance();
29657        }
29658
29659        // Expect "from" (can be keyword or preposition)
29660        if !self.check(&TokenType::From) && !self.check_preposition_is("from") {
29661            return Err(ParseError {
29662                kind: ParseErrorKind::ExpectedKeyword {
29663                    keyword: "from".to_string(),
29664                },
29665                span: self.current_span(),
29666            });
29667        }
29668        self.advance(); // consume "from"
29669
29670        // Parse source expression
29671        let source = self.parse_imperative_expr()?;
29672
29673        // Expect "into"
29674        if !self.check_word("into") {
29675            return Err(ParseError {
29676                kind: ParseErrorKind::ExpectedKeyword {
29677                    keyword: "into".to_string(),
29678                },
29679                span: self.current_span(),
29680            });
29681        }
29682        self.advance(); // consume "into"
29683
29684        // The bound variable: a binding position accepts any content word, so a
29685        // name that elsewhere lexes as a verb ("reply", "run") or another keyword
29686        // is taken as the identifier here — the same rule as every other `let`-like
29687        // binding (`expect_identifier`).
29688        let into = self.expect_identifier()?;
29689
29690        Ok(Stmt::AwaitMessage {
29691            source,
29692            into,
29693            view,
29694            stream,
29695        })
29696    }
29697
29698    /// Parse a batch stream send. The transport is the one-word knob, exactly as `Send` chooses:
29699    ///   `Stream <values> into <pipe>` → an in-process channel send (`SendPipe`) — CROSS-TIER (runs
29700    ///                                   on the VM + AOT + tree-walker), the compute-speed pro.
29701    ///   `Stream <values> to <peer>`   → a relay batch stream (`StreamMessage`) — the NETWORK pro
29702    ///                                   (framed, zero-copy deframe), on the async tree-walker tier.
29703    fn parse_stream_statement(&mut self) -> ParseResult<Stmt<'a>> {
29704        self.advance(); // consume "Stream"
29705        let values = self.parse_imperative_expr()?;
29706        // `into` <pipe>: the cross-tier in-process transport (reuses the channel-send lowering).
29707        if self.check(&TokenType::Into) || self.check_word("into") {
29708            self.advance();
29709            let pipe = self.parse_imperative_expr()?;
29710            return Ok(Stmt::SendPipe {
29711                value: values,
29712                pipe,
29713            });
29714        }
29715        // `to` <peer>: the relay network transport.
29716        if self.check(&TokenType::To) || self.check_preposition_is("to") || self.check_word("to") {
29717            self.advance();
29718            let destination = self.parse_imperative_expr()?;
29719            return Ok(Stmt::StreamMessage {
29720                values,
29721                destination,
29722            });
29723        }
29724        Err(ParseError {
29725            kind: ParseErrorKind::ExpectedKeyword {
29726                keyword: "to (relay) or into (pipe)".to_string(),
29727            },
29728            span: self.current_span(),
29729        })
29730    }
29731
29732    // =========================================================================
29733    // Phase 49: CRDT Statement Parsing
29734    // =========================================================================
29735
29736    /// Parse merge statement: "Merge remote into local." or "Merge remote's field into local's field."
29737    fn parse_merge_statement(&mut self) -> ParseResult<Stmt<'a>> {
29738        self.parse_crdt_join_like(false)
29739    }
29740
29741    /// Parse converge statement: "Converge remote into local." — the IDEMPOTENT sibling of `Merge`
29742    /// (`Stmt::JoinCrdt`); a bare-`Int` field takes per-field MAX instead of ADD.
29743    fn parse_converge_statement(&mut self) -> ParseResult<Stmt<'a>> {
29744        self.parse_crdt_join_like(true)
29745    }
29746
29747    /// The shared `<verb> source into target` body for `Merge`/`Converge`. `is_join` selects the
29748    /// idempotent lattice join (`Stmt::JoinCrdt`) over the plain field-wise merge (`Stmt::MergeCrdt`).
29749    fn parse_crdt_join_like(&mut self, is_join: bool) -> ParseResult<Stmt<'a>> {
29750        self.advance(); // consume "Merge" / "Converge"
29751
29752        // Parse source expression
29753        let source = self.parse_imperative_expr()?;
29754
29755        // Expect "into"
29756        if !self.check_word("into") {
29757            return Err(ParseError {
29758                kind: ParseErrorKind::ExpectedKeyword {
29759                    keyword: "into".to_string(),
29760                },
29761                span: self.current_span(),
29762            });
29763        }
29764        self.advance(); // consume "into"
29765
29766        // Parse target expression
29767        let target = self.parse_imperative_expr()?;
29768
29769        Ok(if is_join {
29770            Stmt::JoinCrdt { source, target }
29771        } else {
29772            Stmt::MergeCrdt { source, target }
29773        })
29774    }
29775
29776    /// Parse a reversible swap: "Swap x and y." — exchanges the two variables' values with zero
29777    /// history-tape cost (`Op::RevSwap`, the canonical zero-garbage reversible primitive).
29778    fn parse_swap_statement(&mut self) -> ParseResult<Stmt<'a>> {
29779        self.advance(); // consume "Swap"
29780        let a_span = self.current_span();
29781        let a = self.expect_identifier()?;
29782        self.note_write_at(a, a_span, component::WriteVerb::Swap);
29783        if !self.check(&TokenType::And) {
29784            return Err(ParseError {
29785                kind: ParseErrorKind::ExpectedKeyword {
29786                    keyword: "and".to_string(),
29787                },
29788                span: self.current_span(),
29789            });
29790        }
29791        self.advance(); // consume "and"
29792        let b_span = self.current_span();
29793        let b = self.expect_identifier()?;
29794        self.note_write_at(b, b_span, component::WriteVerb::Swap);
29795        Ok(Stmt::Swap { a, b })
29796    }
29797
29798    /// `Increase x by n.` / `Decrease x by n.` — one walk for both directions. Two readings,
29799    /// decided by what follows the verb:
29800    ///
29801    ///   * a FIELD access → `Increase c's points by 10.`, the CRDT counter increment
29802    ///   * a PLAIN NAME   → ordinary assignment: `Increase n by 3.` IS `Set n to n + 3.`
29803    fn parse_step_statement(&mut self, increase: bool) -> ParseResult<Stmt<'a>> {
29804        self.advance(); // consume "Increase" / "Decrease"
29805
29806        let target_from = self.current;
29807        let expr = self.parse_imperative_expr()?;
29808        self.note_write(
29809            expr,
29810            target_from,
29811            self.current,
29812            if increase {
29813                component::WriteVerb::Increase
29814            } else {
29815                component::WriteVerb::Decrease
29816            },
29817        );
29818
29819        // Classified BEFORE `by` is consumed so the refusal below points at what was actually
29820        // written rather than at the middle of the sentence.
29821        let field_target = match expr {
29822            Expr::FieldAccess { object, field } => Some((*object, *field)),
29823            Expr::Identifier(_) => None,
29824            _ => {
29825                return Err(ParseError {
29826                    kind: ParseErrorKind::ExpectedKeyword {
29827                        keyword: "a name or a field access (e.g., 'count' or 'x's count')"
29828                            .to_string(),
29829                    },
29830                    span: self.current_span(),
29831                })
29832            }
29833        };
29834
29835        if !self.check_preposition_is("by") {
29836            return Err(ParseError {
29837                kind: ParseErrorKind::ExpectedKeyword {
29838                    keyword: "by".to_string(),
29839                },
29840                span: self.current_span(),
29841            });
29842        }
29843        self.advance(); // consume "by"
29844
29845        let amount = self.parse_imperative_expr()?;
29846
29847        if let Some((object, field)) = field_target {
29848            return Ok(if increase {
29849                Stmt::IncreaseCrdt {
29850                    object,
29851                    field,
29852                    amount,
29853                }
29854            } else {
29855                Stmt::DecreaseCrdt {
29856                    object,
29857                    field,
29858                    amount,
29859                }
29860            });
29861        }
29862
29863        let Expr::Identifier(target) = expr else {
29864            unreachable!("classified as a plain name above")
29865        };
29866        let left = self.ctx.alloc_imperative_expr(Expr::Identifier(*target));
29867        let op = if increase {
29868            BinaryOpKind::Add
29869        } else {
29870            BinaryOpKind::Subtract
29871        };
29872        let value = self.ctx.alloc_imperative_expr(Expr::BinaryOp {
29873            op,
29874            left,
29875            right: amount,
29876        });
29877        Ok(Stmt::Set {
29878            target: *target,
29879            value,
29880        })
29881    }
29882
29883    fn parse_increase_statement(&mut self) -> ParseResult<Stmt<'a>> {
29884        self.parse_step_statement(true)
29885    }
29886
29887    fn parse_decrease_statement(&mut self) -> ParseResult<Stmt<'a>> {
29888        self.parse_step_statement(false)
29889    }
29890
29891    /// Parse append statement: "Append value to sequence."
29892    fn parse_append_statement(&mut self) -> ParseResult<Stmt<'a>> {
29893        self.advance(); // consume "Append"
29894
29895        // Parse value to append
29896        let value = self.parse_imperative_expr()?;
29897
29898        // Expect "to" (can be TokenType::To or a preposition)
29899        if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
29900            return Err(ParseError {
29901                kind: ParseErrorKind::ExpectedKeyword {
29902                    keyword: "to".to_string(),
29903                },
29904                span: self.current_span(),
29905            });
29906        }
29907        self.advance(); // consume "to"
29908
29909        // Parse sequence expression
29910        let sequence = self.parse_imperative_expr()?;
29911
29912        Ok(Stmt::AppendToSequence { sequence, value })
29913    }
29914
29915    /// Parse resolve statement: "Resolve page's title to value."
29916    fn parse_resolve_statement(&mut self) -> ParseResult<Stmt<'a>> {
29917        self.advance(); // consume "Resolve"
29918
29919        // Parse object with field access (e.g., "page's title")
29920        let expr = self.parse_imperative_expr()?;
29921
29922        // Must be a field access
29923        let (object, field) = if let Expr::FieldAccess { object, field } = expr {
29924            (object, field)
29925        } else {
29926            return Err(ParseError {
29927                kind: ParseErrorKind::ExpectedKeyword {
29928                    keyword: "field access (e.g., 'x's title')".to_string(),
29929                },
29930                span: self.current_span(),
29931            });
29932        };
29933
29934        // Expect "to" (can be TokenType::To or a preposition)
29935        if !self.check(&TokenType::To) && !self.check_preposition_is("to") {
29936            return Err(ParseError {
29937                kind: ParseErrorKind::ExpectedKeyword {
29938                    keyword: "to".to_string(),
29939                },
29940                span: self.current_span(),
29941            });
29942        }
29943        self.advance(); // consume "to"
29944
29945        // Parse value
29946        let value = self.parse_imperative_expr()?;
29947
29948        Ok(Stmt::ResolveConflict {
29949            object,
29950            field: *field,
29951            value,
29952        })
29953    }
29954}
29955
29956/// A `Proof:` line that is ONE word — plus the sentence period — names a SINGLE proof step
29957/// rather than a script of them, and that is what
29958/// [`ProofStrategy::ByRule`](crate::ast::theorem::ProofStrategy::ByRule) classifies. Its own
29959/// documented example is `Proof: ModusPonens.`
29960///
29961/// The classification is purely SHAPE-based, so it never drifts against the inference-rule
29962/// enum or the tactic table: LOGOS has exactly one proof-step namespace (the tactics), and a
29963/// one-word line names one step in it. When the word is not a step — an inference-rule name
29964/// like `ModusPonens`, which belongs to the kernel's vocabulary and not to the vernacular —
29965/// the driver says so loudly and names the spelling that DOES apply a rule
29966/// ([`lower_rule_applications`]), rather than searching for a proof the author did not write.
29967///
29968/// Returns `None` for a multi-word line (`Induction on n .`, `Assume h . By assumption .`),
29969/// which stays a [`ProofStrategy::Script`](crate::ast::theorem::ProofStrategy::Script).
29970fn proof_line_names_one_rule(parts: &[String]) -> Option<String> {
29971    let mut words = parts.iter().filter(|p| !p.trim().is_empty());
29972    let head = words.next()?;
29973    match (words.next(), words.next()) {
29974        (None, _) => {}
29975        (Some(p), None) if p == "." => {}
29976        _ => return None,
29977    }
29978    let mut chars = head.chars();
29979    if !chars.next().is_some_and(|c| c.is_alphabetic() || c == '_') {
29980        return None;
29981    }
29982    if !chars.all(|c| c.is_alphanumeric() || c == '_') {
29983        return None;
29984    }
29985    Some(head.clone())
29986}
29987
29988/// The premise namespace a `Proof:` script actually cites, and the one rule that keeps a
29989/// citation from being a silent choice.
29990///
29991/// `ProofState::start_with_names` names premise `i` by its `Given (label):` when it has one and
29992/// by the positional `hp{i}` otherwise; a script's citation is then resolved against the FIRST
29993/// hypothesis carrying that name. So two premises under one name is not shadowing, it is a
29994/// coin flip the block gives its reader no way to see: `Given (h): A.` plus `Given (h): B.`
29995/// plus `exact h` proves `A` and says nothing whatever about `B`.
29996///
29997/// Lean cannot reach that state, twice over. `LocalContext.findFromUserName?` resolves a user
29998/// name with `findSomeRev?`, so the LAST binding wins rather than the first
29999/// (`Lean/LocalContext.lean:273`), and `getUnusedNameAux` guarantees a GENERATED name is never
30000/// one already in use (`Lean/LocalContext.lean:282`). Lean's generated names are also
30001/// inaccessible (`h✝`), so the second guarantee costs it nothing. OURS ARE CITABLE — `hp0` is
30002/// the documented way to cite an unnamed premise — so the same guarantee has to be stated
30003/// here, on the surface, instead of being a property of the elaborator.
30004///
30005/// Refusing rather than shadowing is a deliberate divergence from Lean, and the argument is
30006/// the surface: a `Given (h):` label is written in parentheses on purpose, and this surface
30007/// has no rendering for a shadowed premise, so a reader could never tell which `h` a script
30008/// meant. A block that cannot be read unambiguously is refused where it is written.
30009///
30010/// Returns the diagnostic, or `None` when every premise is citable by exactly one name.
30011fn premise_label_conflict(names: &[Option<String>]) -> Option<String> {
30012    // (1) `hp<digits>` is the system's namespace. A label may enter it only at its own index,
30013    //     where it renames a premise to the name it already had.
30014    for (i, label) in names.iter().enumerate() {
30015        let Some(label) = label else { continue };
30016        let positional = label.len() > 2
30017            && label.starts_with("hp")
30018            && label[2..].bytes().all(|b| b.is_ascii_digit());
30019        if positional && *label != format!("hp{i}") {
30020            return Some(format!(
30021                "premise {i} is labelled `{label}`, and `hp<n>` is the POSITIONAL name every \
30022                 `Proof:` script already has for premise n — so `{label}` would cite THIS \
30023                 premise while naming another. Choose a label outside the `hp<n>` namespace, \
30024                 or write `hp{i}`, which is what this premise is called"
30025            ));
30026        }
30027    }
30028    // (2) Whatever the labels, the names the tactic state will carry must be distinct.
30029    let effective: Vec<String> = names
30030        .iter()
30031        .enumerate()
30032        .map(|(i, n)| n.clone().unwrap_or_else(|| format!("hp{i}")))
30033        .collect();
30034    for (i, name) in effective.iter().enumerate() {
30035        if let Some(j) = effective[..i].iter().position(|earlier| earlier == name) {
30036            return Some(format!(
30037                "premises {j} and {i} are both named `{name}` — a `Proof:` script citing \
30038                 `{name}` would silently take premise {j} and never see premise {i}. Give \
30039                 them different `Given (<label>):` names"
30040            ));
30041        }
30042    }
30043    None
30044}
30045
30046/// Is `word` a step in the TACTIC namespace?
30047///
30048/// The tactic table is ASKED rather than copied: a script of one word either compiles, or
30049/// fails for a reason that still proves the word is a tactic (`cases` wants an argument), or
30050/// fails with `unknown tactic`, which is the one answer that means the namespace does not
30051/// carry it. A word list duplicated here would drift against `tactic_script::canonical` the
30052/// first time an alias was added there, and the drift would be silent.
30053fn names_a_tactic(word: &str) -> bool {
30054    match logicaffeine_proof::tactic_script::parse_script(word) {
30055        Ok(_) => true,
30056        Err(e) => !e.to_string().contains("unknown tactic"),
30057    }
30058}
30059
30060/// A mathematical NAME: a leading letter or `_`, then letters, digits, `_` or `'`.
30061fn is_proof_word(word: &str) -> bool {
30062    let mut chars = word.chars();
30063    chars.next().is_some_and(|c| c.is_alphabetic() || c == '_')
30064        && chars.all(|c| c.is_alphanumeric() || c == '_' || c == '\'')
30065}
30066
30067/// The English `By <Rule>` step, lowered to the tactic namespace's own `by_rule <Rule>` atom.
30068///
30069/// A kernel INFERENCE RULE is not a tactic, so a bare `ModusPonens` is (rightly) an unknown
30070/// tactic — exactly as Lean refuses `by modus_ponens` and wants `apply`. What was missing is
30071/// the spelling that DOES apply one, and `ProofState::by_rule` is now that door: it applies
30072/// the named rule to the facts in scope and CERTIFIES the node before reporting success, so
30073/// this lowering can never hand `qed` a tree the kernel would refuse.
30074///
30075/// # The tactic namespace always wins
30076///
30077/// The rewrite fires only for a word the tactic table does NOT carry, which is why
30078/// `By contradiction.` still means `by_contra`, `By reflexivity.` still means `rfl` and
30079/// `By cases on h.` is untouched — even though `Contradiction` and `Reflexivity` are also
30080/// rule names. One word, one meaning, and the vernacular's own vocabulary has priority over
30081/// the kernel's. [`names_a_tactic`] asks the tactic table itself, so that precedence cannot
30082/// rot.
30083///
30084/// The rewrite is anchored at a STEP boundary (the line's start, or just after a `.`/`;`), so
30085/// a `by` that is prose inside a step — `have h by …` — is left alone.
30086///
30087/// Returns `None` when nothing was rewritten, so the overwhelmingly common line keeps its
30088/// VERBATIM lexeme join and no allocation is spent on it.
30089fn lower_rule_applications(parts: &[String]) -> Option<String> {
30090    let mut out: Vec<String> = Vec::with_capacity(parts.len());
30091    let mut changed = false;
30092    let mut at_step_start = true;
30093    let mut i = 0;
30094    while i < parts.len() {
30095        let word = parts[i].trim();
30096        if word.is_empty() {
30097            out.push(parts[i].clone());
30098            i += 1;
30099            continue;
30100        }
30101        if at_step_start && word.eq_ignore_ascii_case("by") {
30102            if let Some(j) = (i + 1..parts.len()).find(|k| !parts[*k].trim().is_empty()) {
30103                let rule = parts[j].trim();
30104                if logicaffeine_proof::InferenceRule::from_name(rule).is_some()
30105                    && !names_a_tactic(rule)
30106                {
30107                    out.push("by_rule".to_string());
30108                    out.push(rule.to_string());
30109                    changed = true;
30110                    at_step_start = false;
30111                    i = j + 1;
30112                    continue;
30113                }
30114            }
30115        }
30116        at_step_start = word == "." || word == ";";
30117        out.push(parts[i].clone());
30118        i += 1;
30119    }
30120    changed.then(|| out.join(" "))
30121}
30122
30123/// `Proof: By induction on n.` — the whole line naming a METHOD and the variable it runs on,
30124/// and that is what
30125/// [`ProofStrategy::Induction`](crate::ast::theorem::ProofStrategy::Induction) classifies.
30126///
30127/// It is the same proof either way: both drivers lower the variant to the script `induction
30128/// <variable>`, which is what the bare `Proof: Induction on n.` spelling reaches through
30129/// [`ProofStrategy::Script`](crate::ast::theorem::ProofStrategy::Script). What the structured
30130/// variant adds is that the METHOD and its ARGUMENT are readable without re-parsing prose —
30131/// a UI can say "induction on n" instead of echoing a script — and that the public four-way
30132/// classification is honest: every variant has a construction site and a driver arm.
30133///
30134/// Anything longer stays a script, because a script is what it is: `By induction on n.
30135/// Automatically.` is two steps, and collapsing it would discard the second.
30136fn proof_line_is_induction_on(parts: &[String]) -> Option<String> {
30137    let mut words = parts.iter().map(|p| p.trim()).filter(|p| !p.is_empty());
30138    if !words.next()?.eq_ignore_ascii_case("by") {
30139        return None;
30140    }
30141    if !words.next()?.eq_ignore_ascii_case("induction") {
30142        return None;
30143    }
30144    if !words.next()?.eq_ignore_ascii_case("on") {
30145        return None;
30146    }
30147    let variable = words.next()?;
30148    if !is_proof_word(variable) {
30149        return None;
30150    }
30151    match (words.next(), words.next()) {
30152        (None, _) => {}
30153        (Some("."), None) => {}
30154        _ => return None,
30155    }
30156    Some(variable.to_string())
30157}