aufbau 0.3.1

Generalized prefix parsing for a class of context-dependent languages
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//! Typing evaluation — §2. One operation: ascription, discharged by term
//! unification. A type is a tree over the grammar (`Term`); a rule's flat
//! `TypeExpr` is parsed into a `TyExpr` pattern (by the runtime), resolved to a
//! `Term`, and unified. No subtyping: every relation is unifiability.
//!
//! Metavariables are global: a rule's holes are α-renamed per evaluation
//! (`?A` → `A#run`), and the bindings an evaluation discovers on *foreign*
//! variables (a child's, a sibling's) are exported as the residual equations of
//! its [`Evidence`]. Parents merge children's equations before running their own
//! rule, so a constraint discovered deep in one subtree reaches every other use
//! of the same metavariable at their least common ancestor (`def:solve`).

use std::cell::Cell;
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};

use crate::engine::error::{TransitionError, UnresolvedKind};
use crate::engine::parse::arena::{Lexeme, NodeStatus};
use crate::engine::Segment;
use crate::semantics::domain::Verdict;
use crate::semantics::evidence::EvidenceStore;
use crate::semantics::Obligations;
use crate::typing::ir::{Instr, Program};
use crate::typing::normalize::{failure_is_stable, unify_modulo, Normalizer};
use crate::typing::pattern::Pattern;
use crate::typing::rule::{PremiseStatus, RuleResult};
use crate::typing::term::{apply, Evidence, Term};
use crate::typing::{Context, ContextTransition, Subst, TyExpr, TypeExpr};

/// A rule's flat `TypeExpr`s mapped to their parsed trees. Precomputed by the
/// runtime, which holds the grammar.
pub type Trees = HashMap<TypeExpr, TyExpr>;

/// Rename a term's variables to globally-fresh ones — instantiation of a
/// polymorphic scheme at a use site. Reached only through `inst(x)`; ordinary
/// lookups share metavariable identity (the monomorphic default).
fn instantiate(t: &Term) -> Term {
    static NEXT: AtomicU64 = AtomicU64::new(0);
    let mut n = NEXT.fetch_add(1 << 20, Ordering::Relaxed);
    t.freshen(&mut n)
}

/// A fresh evaluation id: the α-renaming scope of one rule run.
fn rid() -> u64 {
    static NEXT: AtomicU64 = AtomicU64::new(1);
    NEXT.fetch_add(1, Ordering::Relaxed)
}

/// Per-instruction execution counts. Each `Cell` is incremented in the hot
/// path with a single atomic-free store; the bench snapshots the values via
/// `TypingDomain::stats`. Zero-cost when the domain is freshly default-
/// constructed (the field is dropped unused).
#[derive(Clone, Debug, Default)]
pub struct Stats {
    pub eval: Cell<usize>,
    pub ascribe: Cell<usize>,
    pub equate: Cell<usize>,
    pub member: Cell<usize>,
    pub push_scope: Cell<usize>,
    pub pop_scope: Cell<usize>,
    pub extend: Cell<usize>,
    pub emit: Cell<usize>,
    pub effect: Cell<usize>,
    pub unify: Cell<usize>,
    pub unify_fail: Cell<usize>,
}

/// Plain snapshot of [`Stats`] for benches and tests.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct StatsSnap {
    pub eval: usize,
    pub ascribe: usize,
    pub equate: usize,
    pub member: usize,
    pub push_scope: usize,
    pub pop_scope: usize,
    pub extend: usize,
    pub emit: usize,
    pub effect: usize,
    pub unify: usize,
    pub unify_fail: usize,
}

impl StatsSnap {
    fn snap(s: &Stats) -> Self {
        Self {
            eval: s.eval.get(),
            ascribe: s.ascribe.get(),
            equate: s.equate.get(),
            member: s.member.get(),
            push_scope: s.push_scope.get(),
            pop_scope: s.pop_scope.get(),
            extend: s.extend.get(),
            emit: s.emit.get(),
            effect: s.effect.get(),
            unify: s.unify.get(),
            unify_fail: s.unify_fail.get(),
        }
    }
}

/// Pure, value-level typing evaluation. The stateful id-interning shell lives
/// in `semantics::runtime::TypingRuntime`.
///
/// `TRACK` selects whether per-instruction execution counts are accumulated.
/// When `false` (the default), every `tick` is a constant-folded no-op — the
/// compiler eliminates the `Cell::set` from the hot path entirely. Set `true`
/// in benches and tests that need a `StatsSnap`; the production parser path
/// (and `TypingRuntime`) always uses the default `false`.
#[derive(Clone, Debug, Default)]
pub struct TypingDomain<const TRACK: bool = false> {
    stats: Stats,
}

impl<const TRACK: bool> TypingDomain<TRACK> {
    /// Snapshot the per-instruction execution counts. Cheap: one `Cell::get`
    /// per field.
    #[must_use]
    pub fn stats(&self) -> StatsSnap {
        StatsSnap::snap(&self.stats)
    }

    /// Zero the per-instruction counts. Used by the bench to isolate a single
    /// `parse` call's IR cost.
    pub fn reset_stats(&self) {
        self.stats.eval.set(0);
        self.stats.ascribe.set(0);
        self.stats.equate.set(0);
        self.stats.member.set(0);
        self.stats.push_scope.set(0);
        self.stats.pop_scope.set(0);
        self.stats.extend.set(0);
        self.stats.emit.set(0);
        self.stats.effect.set(0);
        self.stats.unify.set(0);
        self.stats.unify_fail.set(0);
    }

    /// Increment a per-instruction counter, or do nothing. The `if TRACK` is
    /// constant-folded; in the production (`false`) case the body vanishes.
    #[inline(always)]
    fn tick(cell: &Cell<usize>) {
        if TRACK {
            cell.set(cell.get() + 1);
        }
    }

    // ── Obligation helpers ──────────────────────────────────────────────────

    fn ob_resolve<'a>(obligations: &'a Obligations, name: &str) -> Option<&'a Lexeme> {
        obligations
            .iter()
            .find(|o| o.name == name)
            .and_then(|o| o.value.as_ref())
    }

    fn ob_type(obligations: &Obligations, name: &str) -> Option<usize> {
        obligations
            .iter()
            .find(|o| o.name == name)
            .and_then(|o| o.evidence)
    }

    // ── Expression evaluation ───────────────────────────────────────────────

    /// Resolve a `TyExpr` pattern to a concrete `Term`: holes α-renamed into
    /// this run's scope, refs from evidence, ctx from the context.
    fn eval(
        evidence: &EvidenceStore<Evidence>,
        ty: &TyExpr,
        obligations: &Obligations,
        ctx: &Context,
        segs: &[Segment],
        run: u64,
    ) -> Option<Term> {
        Some(match ty {
            TyExpr::Top => Term::top(),
            TyExpr::Bot => Term::bottom(),
            TyExpr::Lit(s) => Term::Leaf(Pattern::raw(s)),
            TyExpr::Var(n) => Term::Var(format!("{n}#{run}")),
            TyExpr::Ref(b) => Self::resolve_ref(evidence, obligations, b)?,
            TyExpr::Ctx(v) => Self::resolve_ctx(obligations, ctx, segs, v)?,
            // `inst(x)` is `Γ(x)` with its variables freshened — a polymorphic
            // scheme made concrete at this use. The one instantiation point.
            TyExpr::Inst(v) => instantiate(&Self::resolve_ctx(obligations, ctx, segs, v)?),
            TyExpr::Con(label, kids) => {
                let mut out = Vec::with_capacity(kids.len());
                for k in kids {
                    out.push(Self::eval(evidence, k, obligations, ctx, segs, run)?);
                }
                Term::Con(label.clone(), out)
            }
        })
    }

    /// The type of a binding: its evidence term, identity shared. `⊤` (no
    /// constraint yet) is not yet a type, so it reads as unresolved.
    fn resolve_ref(
        evidence: &EvidenceStore<Evidence>,
        obligations: &Obligations,
        b: &str,
    ) -> Option<Term> {
        let id = Self::ob_type(obligations, b)?;
        let ev = evidence.get(id)?;
        (!ev.is_top()).then_some(ev.term)
    }

    /// `Γ(v)`: the type bound to `v`'s value in the context, identity shared.
    fn resolve_ctx(
        obligations: &Obligations,
        ctx: &Context,
        segs: &[Segment],
        v: &str,
    ) -> Option<Term> {
        let lex = Self::ob_resolve(obligations, v)?;
        let text = lex.value(segs).unwrap_or_default();
        if let Some(t) = ctx.lookup(&text) {
            return Some(t.clone());
        }
        if lex.open {
            return ctx.lookup_starts_with(&text).cloned();
        }
        None
    }

    // ── Ascription ──────────────────────────────────────────────────────────

    /// Discharge `actual : expected` by unification modulo the rewrite theory.
    /// Success binds holes into `subst` and is `Satisfied`; a failure is
    /// `Contradiction` only when stable under instantiation
    /// ([`failure_is_stable`]) and the node can no longer grow.
    fn ascribe(
        &self,
        norm: &Normalizer,
        actual: &Term,
        expected: &Term,
        subst: &mut Subst,
        open: bool,
    ) -> PremiseStatus {
        let mut s = subst.clone();
        Self::tick(&self.stats.unify);
        if unify_modulo(norm, expected, actual, &mut s, true) {
            *subst = s;
            PremiseStatus::Satisfied
        } else {
            Self::tick(&self.stats.unify_fail);
            if open || !failure_is_stable(norm, expected, actual) {
                PremiseStatus::Unknown
            } else {
                PremiseStatus::Contradiction
            }
        }
    }

    /// Merge the residual equations exported by resolved children into `subst`:
    /// the incremental closure of the constraint graph (`def:solve`). A stable
    /// conflict between subtrees is a `Contradiction`; an unstable one is
    /// postponed.
    fn merge_eqs(
        &self,
        norm: &Normalizer,
        evidence: &EvidenceStore<Evidence>,
        obligations: &Obligations,
        subst: &mut Subst,
    ) -> PremiseStatus {
        let mut st = PremiseStatus::Satisfied;
        for ob in obligations {
            let Some(ev) = ob.evidence.and_then(|id| evidence.get(id)) else {
                continue;
            };
            for (x, t) in &ev.eqs {
                let v = Term::Var(x.clone());
                Self::tick(&self.stats.unify);
                if !unify_modulo(norm, &v, t, subst, true) {
                    Self::tick(&self.stats.unify_fail);
                    if failure_is_stable(norm, &v, t) {
                        return PremiseStatus::Contradiction;
                    }
                    st = PremiseStatus::Unknown;
                }
            }
        }
        st
    }

    /// Bindings on variables visible outside this evaluation — everything not
    /// α-renamed by this run — with values fully resolved.
    fn export(subst: &Subst, run: u64) -> Vec<(String, Term)> {
        let suffix = format!("#{run}");
        let mut eqs: Vec<(String, Term)> = subst
            .keys()
            .filter(|k| !k.ends_with(&suffix))
            .map(|k| (k.clone(), apply(&Term::Var(k.clone()), subst)))
            .collect();
        eqs.sort_by(|a, b| a.0.cmp(&b.0));
        eqs
    }

    // ── Context helpers ─────────────────────────────────────────────────────

    fn extend(ctx: &Context, value: &str, resolved: Term) -> Context {
        ctx.shadow(value.to_string(), resolved)
    }

    /// Top of the premise-local context stack (never empty).
    fn top(ctxs: &[Context]) -> &Context {
        ctxs.last().expect("context stack is never empty")
    }

    /// The term in register `i`, if any.
    fn reg(regs: &[Option<Term>], i: usize) -> Option<Term> {
        regs.get(i).cloned().flatten()
    }

    /// Write register `dst`, growing the file as needed.
    fn set(regs: &mut Vec<Option<Term>>, dst: usize, v: Option<Term>) {
        if dst >= regs.len() {
            regs.resize(dst + 1, None);
        }
        regs[dst] = v;
    }

    /// Run one `Eval` instruction: resolve `expr` against `ctx` and store it in
    /// register `dst`. The single implementation of evaluation-into-a-register,
    /// shared by the `descend` and `run` interpreters.
    #[allow(clippy::too_many_arguments)]
    fn eval_to_reg(
        &self,
        regs: &mut Vec<Option<Term>>,
        dst: usize,
        expr: &TyExpr,
        evidence: &EvidenceStore<Evidence>,
        obligations: &Obligations,
        ctx: &Context,
        segs: &[Segment],
        run: u64,
    ) {
        Self::tick(&self.stats.eval);
        let v = Self::eval(evidence, expr, obligations, ctx, segs, run);
        Self::set(regs, dst, v);
    }

    /// The `(context key, type term)` an `Extend` shadows, when both its binding
    /// value and its type register are available. `Err` names which half is
    /// missing; the two interpreters differ only in what they do with that
    /// (`descend` retries later, `run` stays `Unknown`), not in how it resolves.
    fn extend_inputs(
        obligations: &Obligations,
        regs: &[Option<Term>],
        binding: &str,
        ty: usize,
        segs: &[Segment],
    ) -> Result<(String, Term), UnresolvedKind> {
        let value = Self::ob_resolve(obligations, binding)
            .and_then(|lex| lex.value(segs))
            .ok_or(UnresolvedKind::Value)?;
        let r = Self::reg(regs, ty).ok_or(UnresolvedKind::Type)?;
        Ok((value, r))
    }

    // ── IR execution ─────────────────────────────────────────────────────────

    /// Discharge an `ascribe` instruction: the obligation bound to `binding` must
    /// have a type that unifies with `expected` (the evaluated register).
    #[allow(clippy::too_many_arguments)]
    fn run_ascribe(
        &self,
        norm: &Normalizer,
        evidence: &EvidenceStore<Evidence>,
        obligations: &Obligations,
        binding: &str,
        expected: Option<Term>,
        subst: &mut Subst,
        allow_missing: bool,
    ) -> PremiseStatus {
        Self::tick(&self.stats.ascribe);
        let Some(ob) = obligations.iter().find(|o| o.name.as_str() == binding) else {
            return if allow_missing {
                PremiseStatus::Unknown
            } else {
                PremiseStatus::Contradiction
            };
        };
        if ob.value.is_none() {
            return PremiseStatus::Unknown;
        }
        let open = ob.value.as_ref().is_some_and(|v| v.open);
        let Some(actual_id) = ob.evidence else {
            return if open {
                PremiseStatus::Unknown
            } else {
                PremiseStatus::Contradiction
            };
        };
        let Some(actual) = evidence.get(actual_id) else {
            return PremiseStatus::Contradiction;
        };
        let Some(expected) = expected else {
            return PremiseStatus::Unknown;
        };
        self.ascribe(norm, &actual.term, &expected, subst, open)
    }

    /// Discharge a `member` instruction: `binding`'s value is in the context (an
    /// open prefix may still match).
    fn run_member(
        &self,
        obligations: &Obligations,
        ctx: &Context,
        binding: &str,
        segs: &[Segment],
    ) -> PremiseStatus {
        Self::tick(&self.stats.member);
        let Some(lex) = Self::ob_resolve(obligations, binding) else {
            return PremiseStatus::Unknown;
        };
        let text = lex.value(segs).unwrap_or_default();
        let exact = !text.is_empty() && ctx.lookup(&text).is_some();
        let prefix = lex.open && !text.is_empty() && ctx.lookup_starts_with(&text).is_some();
        match (exact || prefix, text.is_empty()) {
            (true, _) => PremiseStatus::Satisfied,
            (false, true) => PremiseStatus::Unknown,
            (false, false) => PremiseStatus::Contradiction,
        }
    }

    /// Execute a compiled rule program: a flat fold over the instruction stream
    /// threading a substitution and a stack of premise-local contexts. The control
    /// flow the tree-walk did implicitly (premise scoping) is the `Push`/`Pop`
    /// instructions, so this is the single rule evaluator.
    #[allow(clippy::too_many_arguments)]
    fn run(
        &self,
        program: &Program,
        norm: &Normalizer,
        evidence: &EvidenceStore<Evidence>,
        obligations: &Obligations,
        ctx: Context,
        status: NodeStatus,
        segs: &[Segment],
    ) -> RuleResult {
        let allow_missing = status.open();
        let run = rid();
        let mut subst = Subst::new();
        let mut regs: Vec<Option<Term>> = Vec::new();
        let mut ctxs = vec![ctx];
        let mut satisfied = true;
        let mut output: Option<Term> = None;
        let mut effects: Vec<(String, Term)> = Vec::new();

        // A premise status folds into the running verdict; a contradiction ends it.
        macro_rules! combine {
            ($st:expr) => {
                match $st {
                    PremiseStatus::Contradiction => return RuleResult::Contradiction,
                    PremiseStatus::Unknown => satisfied = false,
                    PremiseStatus::Satisfied => {}
                }
            };
        }

        combine!(self.merge_eqs(norm, evidence, obligations, &mut subst));

        for instr in &program.instrs {
            match instr {
                Instr::Eval { dst, expr } => {
                    let top = Self::top(&ctxs).clone();
                    self.eval_to_reg(&mut regs, *dst, expr, evidence, obligations, &top, segs, run);
                }
                Instr::Ascribe { binding, expected } => {
                    let exp = Self::reg(&regs, *expected);
                    combine!(self.run_ascribe(
                        norm,
                        evidence,
                        obligations,
                        binding,
                        exp,
                        &mut subst,
                        allow_missing
                    ));
                }
                Instr::Equate { left, right } => {
                    Self::tick(&self.stats.equate);
                    let st = match (Self::reg(&regs, *left), Self::reg(&regs, *right)) {
                        // No subtyping: equality reduces to unifiability.
                        (Some(l), Some(r)) => {
                            let mut s = subst.clone();
                            Self::tick(&self.stats.unify);
                            if unify_modulo(norm, &l, &r, &mut s, true) {
                                subst = s;
                                PremiseStatus::Satisfied
                            } else {
                                Self::tick(&self.stats.unify_fail);
                                if failure_is_stable(norm, &l, &r) {
                                    PremiseStatus::Contradiction
                                } else {
                                    PremiseStatus::Unknown
                                }
                            }
                        }
                        _ => PremiseStatus::Unknown,
                    };
                    combine!(st);
                }
                Instr::Member { binding } => {
                    combine!(self.run_member(obligations, Self::top(&ctxs), binding, segs));
                }
                Instr::PushScope => {
                    Self::tick(&self.stats.push_scope);
                    let t = Self::top(&ctxs).clone();
                    ctxs.push(t);
                }
                Instr::PopScope => {
                    Self::tick(&self.stats.pop_scope);
                    if ctxs.len() > 1 {
                        ctxs.pop();
                    }
                }
                Instr::Extend { binding, ty } => {
                    Self::tick(&self.stats.extend);
                    match Self::extend_inputs(obligations, &regs, binding, *ty, segs) {
                        Ok((v, r)) => {
                            let top = ctxs.last_mut().expect("context stack is never empty");
                            *top = top.shadow(v, r);
                        }
                        // A setting that cannot resolve leaves the rule unsatisfied.
                        Err(_) => satisfied = false,
                    }
                }
                Instr::Emit { ty } => {
                    Self::tick(&self.stats.emit);
                    output = Self::reg(&regs, *ty);
                }
                Instr::Effect { binding, ty } => {
                    Self::tick(&self.stats.effect);
                    let name = Self::ob_resolve(obligations, binding)
                        .and_then(|lex| lex.value(segs))
                        .unwrap_or_else(|| binding.clone());
                    if let Some(r) = Self::reg(&regs, *ty) {
                        effects.push((name, r));
                    }
                }
            }
        }

        let eqs = Self::export(&subst, run);
        let Some(ty) = output else {
            return RuleResult::Partial(Evidence {
                term: Term::top(),
                eqs,
            });
        };
        let ev = Evidence {
            term: apply(&ty, &subst),
            eqs,
        };
        if !satisfied {
            return RuleResult::Partial(ev);
        }
        let transforms = effects
            .into_iter()
            .map(|(n, t)| (n, apply(&t, &subst)))
            .collect();
        RuleResult::Success((ev, Some(ContextTransition { transforms })))
    }
}

// ── Value-level evaluation ──────────────────────────────────────────────────

impl<const TRACK: bool> TypingDomain<TRACK> {
    /// Context to use when entering the child bound by `binding`.
    ///
    /// Typing runs per node during the parse, so a binding's setting must be
    /// resolved against the premises *already* discharged before the parser
    /// reaches it — not later, in `finalize`. So this first replays the program
    /// up to `b`'s setting, threading the substitution those premises determine
    /// (e.g. `scrutinee : ?A list` binds `?A` to the element type), then applies
    /// `b`'s setting with that substitution. Without the replay a setting like a
    /// `match` arm's `[head:?A]` would bind `head` to a free variable, and the
    /// arm could be typed against *any* type — unsound.
    ///
    /// `Err(Unresolved)` when a setting extension can't be resolved yet: the
    /// parser treats that as a soft no-op and retries when more input arrives.
    #[allow(clippy::too_many_arguments)]
    pub fn descend(
        &self,
        program: &Program,
        norm: &Normalizer,
        binding: Option<&str>,
        ctx: &Context,
        obligations: &Obligations,
        segs: &[Segment],
        evidence: &EvidenceStore<Evidence>,
    ) -> Result<Context, TransitionError> {
        let Some(b) = binding else {
            return Ok(ctx.clone());
        };
        let Some(range) = program.splices.get(b).cloned() else {
            return Ok(ctx.clone());
        };
        let run = rid();
        let mut regs: Vec<Option<Term>> = Vec::new();
        let mut subst = Subst::new();
        let _ = self.merge_eqs(norm, evidence, obligations, &mut subst);
        // Replay the earlier premises for the substitution they fix. Their own
        // settings are premise-local, so only the judgments that bind metavariables
        // (ascriptions against a resolved sibling, equalities) matter here.
        for instr in &program.instrs[..range.start] {
            match instr {
                Instr::Eval { dst, expr } => {
                    self.eval_to_reg(&mut regs, *dst, expr, evidence, obligations, ctx, segs, run);
                }
                Instr::Ascribe { binding, expected } => {
                    if let Some(actual) = Self::resolve_ref(evidence, obligations, binding)
                        && let Some(exp) = Self::reg(&regs, *expected)
                    {
                        let _ = unify_modulo(norm, &exp, &actual, &mut subst, true);
                    }
                }
                Instr::Equate { left, right } => {
                    if let (Some(l), Some(r)) = (Self::reg(&regs, *left), Self::reg(&regs, *right)) {
                        let _ = unify_modulo(norm, &l, &r, &mut subst, true);
                    }
                }
                _ => {}
            }
        }
        // Apply `b`'s own setting, now that the substitution is known.
        let mut out = ctx.clone();
        for instr in &program.instrs[range] {
            match instr {
                Instr::Eval { dst, expr } => {
                    self.eval_to_reg(&mut regs, *dst, expr, evidence, obligations, &out, segs, run);
                }
                Instr::Extend { binding, ty } => {
                    Self::tick(&self.stats.extend);
                    let (value, r) = Self::extend_inputs(obligations, &regs, binding, *ty, segs)
                        .map_err(|kind| TransitionError::Unresolved {
                            rule: program.name.clone(),
                            binding: b.to_string(),
                            setting: binding.clone(),
                            kind,
                        })?;
                    out = out.shadow(value, apply(&r, &subst));
                }
                _ => {}
            }
        }
        Ok(out)
    }

    /// Per-node verdict, evidence, and exported effect.
    #[allow(clippy::too_many_arguments)]
    pub fn finalize(
        &self,
        program: &Program,
        norm: &Normalizer,
        ctx: &Context,
        obligations: &Obligations,
        segs: &[Segment],
        status: NodeStatus,
        evidence: &EvidenceStore<Evidence>,
    ) -> (Verdict, Evidence, Option<ContextTransition>) {
        match self.run(
            program,
            norm,
            evidence,
            obligations,
            ctx.clone(),
            status,
            segs,
        ) {
            RuleResult::Contradiction => (Verdict::Lost, Evidence::top(), None),
            RuleResult::Partial(ev) => {
                if status.open() {
                    (Verdict::Live, ev, None)
                } else {
                    (Verdict::Lost, Evidence::top(), None)
                }
            }
            RuleResult::Success((ev, transition)) => {
                let effect = if status == NodeStatus::Exact {
                    transition.filter(|t| !t.transforms.is_empty())
                } else {
                    None
                };
                (Verdict::Satisfied, ev, effect)
            }
        }
    }

    /// Apply a right-bound effect to a sibling context.
    pub fn apply_effect(&self, ctx: Context, effect: &ContextTransition) -> Context {
        effect
            .transforms
            .iter()
            .fold(ctx, |acc, (var, ty)| Self::extend(&acc, var, ty.clone()))
    }

    /// Left-to-right composition of effects for transparent productions.
    pub fn compose_effects(&self, effects: &[&ContextTransition]) -> Option<ContextTransition> {
        let mut composed = ContextTransition::identity();
        for &effect in effects {
            composed = composed.compose(effect);
        }
        (!composed.transforms.is_empty()).then_some(composed)
    }
}