polydat 0.8.0

Polydat — a variates construction engine
Documentation
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
---
type: specification
title: The Graph Compiler
timestamp: 2026-09-27
description: "The compiler pipeline and its ordering: wire resolution, adapter insertion, node fusion, hoisting, context fusion, and the H, CF, and NF axioms."
tags: [compiler]
---

# The Graph Compiler

This document specifies the scope-aware compiler that turns an
authored polydat graph into a ready kernel: the order of its
passes, what each pass produces, and the axioms each pass
preserves over the [composition substrate](composition_substrate.md)'s
slot contract. It covers hoisting (the H-axioms), Context Fusion
(the CF-axioms), and Node Fusion (the NF-axioms). A workload
declares clauses, bindings, and operations, but never which
values are computed where, which subgraphs are merged, or which
inputs are constant for a scope's lifetime; the compiler derives
all of these.

**Related specifications:** the
[Composition Substrate](composition_substrate.md) (the slot
contract and the S, T, and L axioms); the
[Runtime Model](runtime_model.md), which specifies what the
compiled program does at runtime and defines the terms used
here; the [Evaluation Model](evaluation_model.md), which
specifies the two-lifecycle classification that hoisting
applies and the const-binding contract it enforces; and
[engines.md](engines.md).

---

## 1. Scope

This document uses the terms of
[runtime_model.md](runtime_model.md) §Terms, the terms *slot*,
*chain*, *scope-init*, *cycle time*, and *engine* as defined in
[composition_substrate.md](composition_substrate.md) §1, and
the following:

- **Hoisting.** The compile-time lifecycle analysis that
  classifies every node as compile-constant, scope-init, or
  dynamic, and so decides whether it is evaluated once at
  build, once at scope-init, or per cycle (§3).
- **Graph Fusion.** The two passes that adapt a graph to its
  context and its engine: Node Fusion and Context Fusion.
- **Node Fusion** (Graph Fusion Phase 2). Compile-time graph
  rewriting: type-conversion adapters inserted at wire
  resolution, and recognised subgraphs replaced by fused nodes
  (§5).
- **Context Fusion** (Graph Fusion Phase 1). Slot filling at
  scope-init: the kernel's extern slots are bound from the
  outer scope, which fills the synthesis surface that
  auto-extern declared (§4).
- **Synthesis surface.** The set of extern slots, by name and
  type, that the chain must fill from outer-scope state at
  scope-init (composition_substrate.md S1).

The compiler is scope-aware: it treats a kernel not as a closed
compilation unit but as one scope in a chain of nested scopes,
each with its own lifecycle classification and synthesis
surface. It builds a kernel with three coordinated mechanisms:

```text
                  AUTHORED GRAPH
                        │
                        ▼
                ┌──────────────────┐
                │   Node Fusion    │   compile-time graph
                │   (Phase 2 GF)   │   rewriting
                └────────┬─────────┘
                         │
                         ▼
                ┌──────────────────┐
                │     Hoisting     │   compile-time lifecycle
                │     (analysis)   │   classification
                └────────┬─────────┘
                         │
                         ▼
                ┌──────────────────┐
                │   kernel on the  │   the program is built
                │   chosen Engine  │   and ready to instantiate
                └────────┬─────────┘
                         │
                         │   (per scope-init)
                         ▼
                ┌──────────────────┐
                │  Context Fusion  │   scope-init-time
                │  (Phase 1 GF)    │   slot filling
                └────────┬─────────┘
                         │
                         ▼
                  READY KERNEL
                  (slot contract held)
```

Together these form the construction pipeline, and every pass
in it preserves the substrate's typed slot contract.

---

## 2. Pipeline overview

The table lists the passes in the order the code runs them.
Every pass up to and including the `ResolvedDag` is
engine-neutral: it runs once, in the same way, whichever engine
the host names. The engine then decides how the resolved graph
is run.

| Pass | Where | What it does |
|---|---|---|
| Parse | `polydat_grammar::lexer` / `parser` (re-exported as `dsl::lexer`, `dsl::parser`) | Source to AST. |
| Prologue | `dsl::compile` (`Prepared`) | One prologue for every entry point: the compile options, the required outputs, the pragmas, the data-file base directory. |
| Bind | `dsl::compile::assemble_parent` | One lowering for every entry point: bindings become assembler nodes; every referenced name not defined locally becomes an input slot (the conditional-shadow rule of [none_semantics.md](none_semantics.md)); tiles are typed. `for` statements are lifted out first and their bodies compiled as traversals. |
| Const capture | `compile::assembly::resolve_with_log` (`capture_consts`), first | Every output marked `const`, whether written in source or marked through `PolydatAssembler::mark_const_output`, is classified by its cone. A const whose cone reads no input and no nondeterministic node stays in the graph and folds at build. Any other const is rewritten into a capture: its expression becomes the output `__init_<name>`, its value the `InputKind::Const` slot `__const_<name>`, and a `ConstInit` record orders it for `Kernel::init` ([evaluation_model.md](evaluation_model.md), "Const Binding Contract"). A `shared` register's computed starting value gets a `ConstInit` record here too. A capture that reads a coordinate, or consts that read each other in a cycle, fail the build. The pass runs once, before every other resolve step, so the result is the same on all four engines. |
| Wire resolution + adapter insertion | `compile::assembly::resolve_with_log` | Arity is checked. Each wire's producer type is compared with the consumer's declared input type; a mismatch is either healed through `auto_adapter` (the adapter node is inserted and `TypeAdapterInserted` logged) or fails as `AssemblyError::TypeMismatch`. Strict mode refuses the implicit conversion instead. |
| Strict-wire checks | same pass | Under `strict_values`, every constrained wire input is checked (§2.3): a compile-time constant source at build, where a violation fails the build, and any other source through an inserted `AssertValue` node (`AssertionInserted` / `AssertionSkipped`). |
| Node Fusion | same pass, `compile::fusion::apply_fusions` | `fusion::default_rules` (every rule the linked node crates register, in priority order) applied to a fixpoint; `FusionApplied` logged. |
| Dead-code elimination | same pass | Nodes not reachable from a declared output are dropped; the side-channel `log_*` nodes are always kept. |
| Topological sort | same pass | Kahn's algorithm over the live nodes; a cycle is `CycleDetected`. |
| Round-trip lint | same pass, `compile::roundtrip_lint` | A value converted `T → Y → … → T` through pure conversion nodes is a warning, and an error under `strict_values`. |
| → `ResolvedDag` | | The engine-neutral product: nodes in topological order, wiring, input definitions, output map. |
| Hoisting | `PolydatProgram::classify_lifecycle` | Classifies every node as compile-constant, scope-init, or dynamic (§3); run by the engine build. |
| Engine build | `PolydatAssembler::compile_with(Engine)` | Interpreter: native cone extraction, then the fold (`fold_init_constants_impl`). Closure tier: the closure plan (`build_p2_layout`). Native: the hybrid kernel's segments. Pure native (with the `jit` feature): one native kernel over the whole program (`JitKernelRaw` or `JitKernelPushPull`). The strict refusals (`refuse_strict`) and the compile-constant fold run on all four engines, and `ConstantFolded` is logged on all four. |
| Context Fusion | `kernel::bind_under` | Slot synthesis at scope-init, on the child's engine under a parent of any engine (§4). |

### 2.1 What the pipeline produces

The product is a `PolydatProgram`, which is immutable once built
and shared across kernels through an `Arc`. It holds the DAG as
parallel vectors, together with typed input metadata and
ordered output metadata:

```rust
pub struct PolydatProgram {
    nodes: Vec<Box<dyn PolydatNode>>,   // node instances, topological order
    wiring: Vec<Vec<WireSource>>,       // per node, the source of each input port
    input_defs: Vec<InputDef>,          // typed inputs and their lifecycle classes
    coord_count: usize,                 // the leading coordinate-input prefix
    output_map: HashMap<String, (usize, usize)>,  // name → (node, port)
    output_list: Vec<(String, usize, usize)>,     // declaration order
    …                                   // provenance, dependents, and cone metadata
}

pub enum WireSource {
    Input(usize),               // a named input, by index
    NodeOutput(usize, usize),   // the output of (node_index, port_index)
}
```

Two properties of this layout are required by the rest of the
system:

- **A wire source is either an input or another node's
  output.** Data therefore flows forward only along the wire
  chain, and nothing reads backwards
  ([runtime_model.md R3](runtime_model.md)). Acyclicity itself
  is established by the topological sort, not by the
  representation.
- **Lifecycle is recorded on the input, not on the wire.**
  There is no third `WireSource` variant for a lifecycle class.
  Each input's lifecycle is `InputDef::kind` (`Coordinate`,
  `IterationExtern`, or `ExternalWrite`). A consumer's wiring
  records where a value comes from, and the input definition
  records how often it changes, so hoisting (§3) classifies
  lifecycle without rewriting any wiring.

Inputs are ordered with the coordinates first, so `coord_count`
splits the array and a host can write the coordinates by
position without naming them.

Outputs are stored twice on purpose. `output_map` finds the
value of a named output in one lookup, and `output_list`
preserves declaration order so that positional access is stable
across compiles of the same source.

### 2.2 The compile log

Every pass writes to one event log instead of to stderr. The log
is part of the compiled artifact, not a debugging side channel:
the binary's `explain` command reads it back, and so can any
embedding host. Each event has a level:

| Level | Tag | What it marks |
|---|---|---|
| Info | `polydat[info]` | A normal step: parsed, bound, folded, sorted. |
| Advisory | `polydat[advisory]` | An implicit choice the compiler made for the author — an inserted adapter, a widened operand. Worth reviewing for module quality; not a defect. |
| Warning | `polydat[warning]` | A likely performance or correctness problem, such as a value round-tripped through conversions back to its own type. |

The advisory level records implicit conversions. Every implicit
conversion in polydat is silent at runtime by design, because
the adapter is an ordinary node; without the log entry, the
author could not see that a `u64` operand became an `f64`, or
that a value was converted to `Str` on its way into an
interpolation. Recording each one as an event keeps implicit
conversions inspectable, and lets strict mode refuse whole
classes of them without a second mechanism.

<a id="sec-strict-wires"></a>
### 2.3 Strict-wire checks

A node trusts its inputs: its evaluation does not check them, and a
value outside its domain panics at cycle time. An input port may
declare a **value constraint** (`Port::constraint`, in the
`ConstConstraint` vocabulary the constant-parameter validator uses) to
name that domain. `mod_wire`'s `divisor` port declares `NonZeroU64`,
for example. The **strict-wire checks** enforce these constraints. They
are on for a node when `pragma strict_values` (or its alias `strict`)
is in force in the scope the node is written in
([polydat_grammar.md §14](polydat_grammar.md#sec-pragmas)).

The pass runs per wire, after adapter insertion, and decides by the
wire's source:

| Source | Result |
|---|---|
| A **compile-time constant**: a node with no wire inputs that is not nondeterministic | The node is evaluated at build and its value checked. A value that satisfies the constraint needs no guard. A `None` value needs none either, because the sink is never evaluated on `None` ([none_semantics.md](none_semantics.md) Rule 1). A value that fails the constraint fails the build. |
| The guard this pass inserted for an equal constraint | No second guard is inserted. |
| Anything else: an input slot, a node with wire inputs, or a nondeterministic node | An `AssertValue` node is inserted in front of the sink. It passes the value through, and it panics with the constraint's message when the value fails, with the same message on every engine. |

A node is **nondeterministic** when it declares
`Purity::Nondeterministic` or a `volatile` binding names it. Its value at
build says nothing about its value at cycle time: `counter()` yields 0
on its first evaluation and 1 on its second. Such a node therefore gets
a runtime guard, even though it has no wire inputs. A guard counts as
proof only when this pass inserted it for the same constraint. An
assertion node is recognised by that mark and never by its name, and
assertion nodes are not callable from Polydat source.

Only the source node itself is evaluated. A computed source is guarded
even when its inputs are constants, as in `mod_wire(cycle, 3 - 3)`. The
compile-constant fold then evaluates that guard at build, so such a
program fails the build as well, reported as a fold failure of
`assert_u64_nonzero`. A `const` binding captured at kernel
initialization reaches its readers through an input slot, so its
readers are guarded too.

A constant that fails is reported with the port, the node, the
constraint, and the value:

```text
pragma strict_values
input cycle: u64
b := mod_wire(cycle, 0)
```

```text
strict_values: port 'divisor' of 'b' (mod_wire) must be non-zero, but its source is the constant 0
```

Each wire that gets no guard is logged as `AssertionSkipped` with the
reason, and each inserted guard as `AssertionInserted`. A round trip
the round-trip lint (§2) finds is an error rather than a warning when
the scope its restoring binding is written in has `strict_values` on:
the program, a `for` body, or a module body, each under its own
pragmas as [polydat_grammar.md
§14.1](polydat_grammar.md#sec-pragma-scoping) scopes them. An adapter
the resolver inserts belongs to the scope of the port it feeds.

`pragma strict_types` is accepted and acknowledged
(`PragmaAcknowledged`), and it has no effect on the graph. Wires are
statically typed. Wire resolution makes every wire's type the sink
port's declared type, through an adapter or a `TypeMismatch` refusal,
so a runtime type assertion would have nothing to catch. The `strict`
alias turns on both pragmas, so its effect is that of `strict_values`.

---

## 3. Hoisting

### 3.0 What "hoisting" means in polydat

In general compiler usage, *hoisting* often means cross-scope
code motion, such as moving a loop-invariant computation out of
a loop. In polydat the term is narrower:

> **Hoisting is the per-node lifecycle classification within
> a single kernel's program, used to partition the program's
> execution into code paths built into the same program: a
> compile-constant fold that runs once at build, a scope-init
> evaluation that runs once when the kernel materialises, and
> a per-cycle dispatch that runs each `set_inputs` advance.**

Hoisting partitions work within one kernel; it does not move
code between kernels. Kernels are optimization boundaries: no
computation is moved between a parent kernel and a child
kernel, and values that pass between them use the scope
materialization protocol.

Hoisting classifies each node from the *final* wire chain,
meaning which inputs the node depends on and through which
computed nodes. Node Fusion changes the wire structure, so Node
Fusion runs before hoisting, and the classification is computed
on the post-fusion graph.

### 3.1 The hoisting algebra

Under the [Evaluation Model](evaluation_model.md), every node
in a kernel has one of two lifecycles:

- **Effectively-const.** Resolved once, at build
  (compile-constant) or at scope-init, and fixed for the
  scope's lifetime.
- **Dynamic.** Resolved on pull at cycle time.

Hoisting walks the kernel's wire chain and classifies every
node by lifecycle. The classification is structural:

```text
classify(node) =
    seed from the inputs the node reads directly:
      a Coordinate input slot:        Dynamic
      an ExternalWrite input slot:    Dynamic
      an IterationExtern input slot:  ScopeInit
      a Const input slot:             ScopeInit
      no input at all:                CompileConst
    a node declared Purity::Nondeterministic, or feeding a
    `volatile` output:                Dynamic (and nondeterministic)
    then the join with every upstream node:
      the maximum of its own seed and its producers'
      (Dynamic > ScopeInit > CompileConst); nondeterminism
      is contagious downstream.
```

A `Const` input slot (`__const_<name>`) holds the value a `const`
binding took at initialization ([Evaluation Model](evaluation_model.md),
"Const Binding Contract"). A node that reads a const reads that
slot, so it is scope-init even when the const's own expression
(the output `__init_<name>`) is dynamic or nondeterministic:
nondeterminism does not pass through a const.

A computed node's class is the *join* (the maximum) of its own
seed and its upstream nodes' classes. The join is monotonic and
computed bottom-up, as a fixpoint over the wiring.

The classifier is `PolydatProgram::classify_lifecycle`. It is
the single classification used by the interpreter's fold, the
strict refusal, the closure plan, the hybrid builder, and the
pure-native builder, so all four engines (interpreter, closure
tier, native, and pure native) fold, defer, and recompute the
same nodes. The interpreter's native-cone extraction derives
the same three classes again to keep its cones within per-cycle
work.

### 3.2 The hoisting boundary

A kernel's hoisting boundary is the set of wires with
effectively-const nodes on the upstream side and dynamic nodes
on the downstream side. The build produces one evaluation path
per class:

- **Compile-constant path.** Every compile-constant node is
  evaluated once at build and replaced with a constant.
- **Scope-init path.** The kernel's initialization evaluates
  every `const` binding once and writes each value to its const
  slot; every scope-init node is then evaluated once, from the
  bound iteration externs and the const slots, and its value is
  stored in a slot or buffer.
- **Per-cycle path.** Every dynamic node is evaluated per
  `set_inputs` advance, reading effectively-const upstream
  values from their folded constants or pre-evaluated buffers.

The boundary is structural, not declared. A node author does not
mark a node as hoistable; the analysis derives it from the
upstream wire chain.

### 3.3 Hoisting eligibility — what moves to scope-init

A computed node is hoisting-eligible if and only if every input
in its upstream cone is effectively-const. Any dynamic upstream
keeps the node on the per-cycle path.

Eligibility therefore depends on the wire chain, not on the node
itself. For example:

- A `hash(k)` node where `k` is a `for` traversal element is
  hoistable, because `k` is `IterationExtern` (effectively-const
  for the scope's lifetime).
- A `hash(cycle)` node is not hoistable, because `cycle` is a
  `Coordinate` (dynamic).
- A node that reads a `const X := <expr>` binding reads X's
  const slot, so it is hoistable whatever `<expr>` reads.
- A `const X := hash(cycle)` binding is a build error on all four
  engines, because a const is evaluated once at initialization
  and a coordinate advances every cycle.

### 3.4 The Axiom suite — H-axioms

The substrate's axioms (S1–S5, T1–T3, L1–L2) are general
guarantees about slots and layers. The H-axioms are specific
guarantees about the hoisting analysis.

#### Axiom H1 — Classification is total

**Every node in a well-formed graph has exactly one
lifecycle classification (compile-constant, scope-init, or
dynamic). The classification function is total: there is no
"unknown" or "deferred" state.**

Enforcement: the structural walk in §3.1 is exhaustive; an
unresolved wire fails at construction (`UnknownWire`); strict
mode refuses what lax compilation only warns about.

#### Axiom H2 — Classification is monotonic and stable under composition

**Node `n`'s classification is a monotonic function of its
upstream cone's classifications: if any upstream is dynamic,
`n` is dynamic; otherwise `n` is effectively-const. The
classification is preserved under fan-in composition (joining
upstream cones) and under fan-out (multiple consumers of
`n`).**

Enforcement: the join rule in §3.1. Adding an upstream wire can
change `n` from effectively-const to dynamic but never the
reverse, so the analysis stays stable under graph rewrites
(Node Fusion, §5).

#### Axiom H3 — Hoisting boundary is preservation-of-determinism

**A node `n` moved from the per-cycle path to the scope-init or
compile-constant path produces the same value as it would have
produced per cycle, for every cycle. Hoisting does not change
the value of any wire; it changes only when the value is
computed.**

Enforcement: H1's totality, S3 (the typed writes change
coordinates only), and L2 (lifecycle bridging). An
effectively-const value does not change after scope-init, by
definition of the lifecycle; hoisting relies on exactly that
guarantee.

A `const` binding is not a hoisting and H3 does not apply to it.
Its value is defined as its expression's value at the kernel's
initialization, not per cycle, so a const that reads an extern
written after initialization, or a volatile source, differs from
the same expression evaluated per cycle, as its definition
states. H3 applies to the nodes that read the const: they read
its slot, whose value is fixed, so moving them to the scope-init
path does not change their values.

---

## 4. Graph Fusion Phase 1 — Context Fusion

Context Fusion implements the substrate's Synthesis pillar (S1,
S2) at scope-init. When a scope is materialised, its declared
extern slots are filled from the outer scope's bindings, its
effectively-const buffer is evaluated, and its dispatch state is
initialised.

### 4.1 The synthesis surface

Under S1, the compiler discovers extern slots through
auto-extern, and the discovered externs form the **synthesis
surface**: the slot names and types that the chain must fill
from outer state at scope-init.

The synthesis surface is recorded in the kernel's `input_defs`.
The `IterationExtern` entries and the relevant `ExternalWrite`
entries are the non-coordinate slots, each filled by
construction or at the external-write boundary, whichever
applies.

### 4.2 The synthesis act

Under S2, parent-gated construction is the binder
`kernel::bind_under(parent, program, iter_bindings)`. It creates
the child kernel uninitialized from the shared program, on that
program's engine, writes the iteration bindings into their
slots, and runs the crate-private wiring pass against `outer`
(the parent). Both sides of the pass are `dyn Kernel`, so a
child of any of the four engines binds under a parent of any of
them. Every public child-construction path reaches it:
`PolydatMatter::build_under`, `Construction::subscope`,
`ScopeKernel::spawn`, and `ScopeModule::instantiate_under`. The
pass performs these steps in order:

1. **Cell cascade.** Every cell visible at `outer` (the cells
   on its own input slots and its transit cells) is attached to
   the inner input slot of the same name. A cell whose name the
   inner scope declares `const` is dropped (transit
   suppression, [wire_materialization.md](wire_materialization.md)).
   A cell with no matching inner slot is recorded on the inner
   kernel's transit list, so that a deeper descendant can
   attach it.
2. **Outer outputs into inner slots.** For each output of
   `outer` that names an inner input slot not already bound
   to a cell, the first applicable form is used:
   - *value copy through `outer.lookup`* when the name is an
     input slot on `outer` (a passthrough extern) or a `const`
     output of `outer`. This is the two-tier read, so a `None`
     const falls through to the grandparent's value;
   - *cell attach* when `outer` publishes the output through a
     broadcast cell (a computed, per-cycle output);
   - *plain value copy* of `outer.lookup(name)` otherwise;
   - *the registered extern resolver*
     (`dsl::factories::register_extern_resolver`) when the
     outer chain has no binding at all.
   A coordinate is never copied. Every copied value is written
   under the host-write rule, and a value the child's input
   refuses fails binding (§4.4 and
   [input_variance.md](input_variance.md) §7).
3. **Initialization.** The child kernel is initialized
   (`Kernel::init`): every `const` is evaluated once, in
   dependency order, against the filled slots, and a const whose
   expression fails makes construction fail with
   `KernelError::ConstInit` naming it (the Evaluation Model's
   const-binding contract).
4. **Scope coordinates.** The inner scope's path becomes its
   own coordinates followed by `outer`'s.

The following diagram shows how step 2 chooses the form for
one outer output; the registered extern resolver, used when the
outer chain has no binding at all, is not shown.

![Decision flow for step 2: a slot already bound to a cell keeps the cell; otherwise a passthrough extern or const output is copied through outer.lookup, a broadcast-cell output has its cell attached, and any other output is copied by value](../diagrams/graph_compiler-outer-output-forms.png)

After these steps, every declared extern slot in the child holds
a value or a `SharedCell` handle, and transit cells are queued
for later materialisations further down the chain.

The output **manifest**, the typed contract a program exposes
to descendant synthesisers, is a separate read-only summary
produced by `kernel::extract_manifest`. Synthesisers read it
before compiling an inner program, to decide which auto-externs
the inner program may declare. It is not used during synthesis;
it determines the shape of the synthesis surface in advance.

The same forms are available through the `Kernel` trait on all
four engines (interpreter, closure tier, native, and pure
native): a value copy is `set_input`, a shared-binding input
cell is attached with `attach_shared_cell`, and compile
constants are folded at build. Broadcast output cells exist on
all four engines: a kernel makes an output's cell on the first
ask and publishes through it on every later pull of that
output, so a computed output is cell-attached under a parent of
any engine. A traversal's activation copies the parent's values
into the body's kernel on whichever of the four engines runs it
([for_traversal.md](for_traversal.md)).

### 4.3 The Axiom suite — CF-axioms

#### Axiom CF1 — Surface completeness

**Every extern slot in the kernel's `input_defs` is in the
synthesis surface, and Context Fusion fills every slot before
scope-init evaluation begins. No slot is filled lazily or on
first read; every declared slot is filled at scope-init.**

Enforcement: the binder's wiring pass (§4.2) matches every cell
and every outer output against `input_defs`. Parent-gated
construction ([subcontext_construction.md](subcontext_construction.md))
is the only path that materialises a child, so no other
synthesis path exists.

#### Axiom CF2 — Deterministic synthesis

**For a fixed outer scope state and a fixed program, Context
Fusion produces the same slot values every time. It has no
nondeterministic ordering, no random choice, and no implicit
context that cannot be derived from the outer chain.**

Enforcement: the synthesis walk follows the outer program's
outputs and `input_defs` in a defined order, and the outer
chain lookup respects shadowing (transit suppression).
Identical inputs produce identical slot vectors.

#### Axiom CF3 — Gradient honouring

**The classification of each slot during synthesis (value
copy, cell, or transit) is exactly the materialization
gradient of [wire_materialization.md](wire_materialization.md),
derived from the outer binding's modifier and ownership.
Context Fusion never promotes or demotes a classification; it
applies it as given.**

Enforcement: the classification is read from the outer
program's binding modifiers (`const`, `shared`) and input
kinds, fixed when the outer program is built, and applied by
the materializer at scope-init. Nothing is reclassified during
synthesis.

#### Axiom CF4 — Synthesis happens once per scope-init

**Context Fusion runs once, at scope-init, when the kernel is
instantiated as a new scope. It does not run per coordinate.
Changes between coordinates are made by the typed writes (S3),
each of which is narrow and named.**

Enforcement: the private materializer runs once per child
construction, and `set_inputs`, the per-cycle write, changes
only coordinate slots. No operation re-runs Context Fusion in
the middle of a scope's lifetime.

### 4.4 Interaction with the substrate

Context Fusion is the runtime mechanism that implements the
Synthesis pillar (S1, S2): where the substrate states that the
chain synthesises scope state into slots, Context Fusion
performs that synthesis.

The binder, the part of Context Fusion that wires a child
under its parent, fills each input the child declares from the
parent binding of the same name, and the kind of that binding
decides how the child sees it. Take a parent with these bindings:

```
extern region: str = "us"
const batch := 1000
load := hash(cycle)
shared errors := 0
```

A child that mentions all four gets them as follows:

- `region` and `batch` are copied into the child's inputs once,
  when the child is created. A later change to either on the
  parent is not seen by the child until the child is created or
  bound again.
- `load` is linked: the child's input reads the parent's output,
  so each recompute of `load` on the parent is seen by the child.
- `errors` is one register that parent and child share. Either
  side reads the current value, and a write from either side is
  seen by both.

This is the value-copy, cell, and transit classification of
CF3, applied to the parent's binding kinds. T1 and T2
(type-checked slots) are enforced as each input is filled. A
value copy into an input whose type was inferred is converted
through the boundary adapter catalog (T2); a copy into a
declared input is taken as it is. Either is then written under
the same rule as a host write
([evaluation_model.md](evaluation_model.md)). A value the input
refuses fails binding with
`KernelError::Write(WriteError::FromParent { slot, expected, got })`,
which names the input, the type the child declares, and the
type of the parent's value
([input_variance.md](input_variance.md) §7). Coordinates are
never copied.

---

## 5. Graph Fusion Phase 2 — Node Fusion

Node Fusion is compile-time rewriting of the graph. It runs
during wire resolution, before hoisting, so the lifecycle
classification is computed on the rewritten structure. It has
two mechanisms.

### 5.1 Adapter insertion at wire resolution

When a wire's producer `PortType` differs from its consumer's
declared input type, wire resolution heals it with a node from
the adapter catalog (`compile::assembly::auto_adapter`, the
class-A conversions of [type_system.md](type_system.md) §3).
The adapter is inserted between producer and consumer, and
`TypeAdapterInserted` is logged. A pair of types with no catalog
entry is `AssemblyError::TypeMismatch`. Under strict mode the
implicit conversion is refused, with a message naming the
explicit conversion to write instead. A few variadic,
type-polymorphic nodes (`printf`, `pick`, the `log_*` family,
`exactly_one_value`) declare placeholder port types and skip
the check; they validate their inputs at eval.

### 5.2 Subgraph fusion

After every wire is resolved, `compile::fusion::apply_fusions`
rewrites recognised subgraphs into single fused nodes.

- **The rule catalog** is whatever the linked node crates
  register through `FusionRuleRegistration` (inventory),
  collected by `fusion::default_rules` in ascending priority,
  ties broken by name. polydat-nodes contributes
  `hash_mod_to_hash_range` (10),
  `hash_unit_lerp_to_hash_interval` (20), and
  `unit_lerp_to_scale_range` (30).
- **A `FusionRule`** is a pattern, a replacement factory, and
  the names of the captured wires that become the fused node's
  inputs.
- **Equivalence.** Every fused node implements
  `FusedNode::decomposed`, which rebuilds the unfused subgraph
  it replaces. That is the equivalence contract NF2 relies on,
  and the equivalence tests exercise it.
- **Named outputs.** A node directly referenced by a declared
  output is never consumed as an interior node of a fusion.
- **Termination.** The pass runs to a fixpoint (§8.3).

Both mechanisms are graph rewrites that add, remove, or
reconnect nodes. The graph after Node Fusion is structurally
different from the one the parser produced, and the
classification that follows is computed on the rewritten graph,
not the original.

### 5.3 The Axiom suite — NF-axioms

#### Axiom NF1 — Slot contract preservation

**A Node Fusion rewrite must preserve the slot contract:
the input slots, output slots, and their declared
`PortType`s of the rewritten subgraph match the input
slots, output slots, and types of the original subgraph
modulo type-compatible edge adapters.**

Enforcement: the assembler ([`compile::assembly`]) resolves
every wire of the rewritten graph against T1 and T2. A fusion
that violates the slot contract fails assembly.

#### Axiom NF2 — Determinism preservation

**A Node Fusion rewrite must preserve evaluation determinism:
for every input vector `v`, the rewritten subgraph produces
the same output vector as the original subgraph.**

Enforcement: `FusedNode::decomposed`. Every fused node can
rebuild the subgraph it replaced, and the equivalence tests
evaluate both over a representative input space.

#### Axiom NF3 — Lifecycle preservation or weakening

**A Node Fusion rewrite may preserve or weaken the lifecycle
classification of any wire, but may not strengthen it.
Effectively-const → effectively-const (preserve) and
effectively-const → dynamic (weaken) are allowed; dynamic →
effectively-const requires explicit proof and lifecycle
pinning.**

Enforcement: hoisting (§3) classifies the fused graph from
scratch. A fused node's lifecycle is the join of the inputs the
fused subgraph reads, which are the inputs its members read, so
the rewrite cannot produce a stronger class than its members
had.

#### Axiom NF4 — Compositional closure

**The result of a Node Fusion rewrite is itself a subgraph
eligible for further Node Fusion. The fusion pass repeats
until no pattern applies.**

Enforcement: `apply_fusions` is a fixpoint iteration over the
graph with the registered rules, and each rewritten graph is
matched again.

### 5.4 Interaction with hoisting and Context Fusion

**Node Fusion precedes hoisting.** The lifecycle classification
is computed on the fused graph, not the original, and NF3
ensures that any lifecycle change caused by fusion is visible
and monotonic.

**Node Fusion is invisible to Context Fusion.** Context Fusion
operates on the synthesis surface (the `input_defs` of the
built program), which is determined after fusion. The synthesis
act does not know which nodes were fused; it fills the declared
slots.

**Adapter insertion preserves the slot contract at
construction.** A wire from a `U64` source to a `Str` consumer
gets a `U64 → Str` adapter. In the resolved wire chain both the
source and the consumer are correctly typed, so the
substrate's T1 and T2 hold across the inserted adapter.

---

## 6. The pipeline as ordered composition

```text
                  ┌──────────────────────────────┐
                  │   Parse + Bind               │   one prologue (Prepared),
                  │   (assemble_parent)          │   one lowering; auto-externs;
                  │                              │   tiles typed; for bodies lifted
                  └─────────────┬────────────────┘
                                │ assembler
                                ▼
                  ┌──────────────────────────────┐
                  │   resolve_with_log           │
                  │   - wire resolution +        │   auto_adapter; strict refuses
                  │     adapter insertion        │   the implicit coercion
                  │   - strict-wire assertions   │   AssertValue under strict_values
                  │   - Node Fusion (§5)         │   default_rules to a fixpoint
                  │   - dead-code elimination    │   reachable from outputs
                  │   - topological sort         │   Kahn; CycleDetected
                  │   - round-trip lint          │   warning; error under strict_values
                  └─────────────┬────────────────┘
                                │ ResolvedDag (engine-neutral)
                                ▼
                  ┌──────────────────────────────┐
                  │   compile_with(Engine)       │   classify_lifecycle (§3) on
                  │   interpreter: cones + fold  │   every engine; strict refusals;
                  │   closures: closure plan     │   compile-constant fold;
                  │   native: hybrid segments    │   ConstantFolded logged
                  └─────────────┬────────────────┘
                                │ kernel on the chosen engine
                                ▼
                       --- compile end ---

                       --- scope-init begin ---

                                │
                                ▼
                  ┌──────────────────────────────┐
                  │   Context Fusion (§4)        │   kernel::bind_under:
                  │   - cell cascade             │   cells, value copies,
                  │   - value copy / cell attach │   resolver, const pull,
                  │   - const pull               │   scope coordinates
                  └─────────────┬────────────────┘
                                │ ready kernel
                                ▼
                       --- READY KERNEL ---
                       (slot contract held)
```

In this diagram, "every engine" means all four engines
(interpreter, closure tier, native, and pure native); pure
native, which the diagram does not list, is built by the same
`compile_with(Engine)` step (§2).

The pipeline is correct when every pass preserves the slot
contract. The substrate guarantees that the slot contract holds
at every boundary between passes, and this document's axioms
(H, CF, NF) guarantee that each pass preserves it. The pipeline
is engine-neutral up to `ResolvedDag`. The `Engine` the host
names ([engines.md](engines.md)) selects how the resolved graph
is run and does not change what it computes.

---

## 7. Rationale

The graph compiler enforces the substrate's slot contract
automatically, so workload authors never maintain it by hand.
Three design constraints follow:

- **No synthesis declarations (S1, S2).** The compiler
  discovers which slots need filling, and Context Fusion fills
  them. An author writes `query[id={k}]`; the compiler finds
  that `{k}` refers to the outer iteration variable; Context
  Fusion fills the slot. Polydat has no syntax for declaring
  that a kernel reads `k` from the outer scope, and needs none.
- **Performance from compile-time analysis (H1, H2, H3).**
  Hoisting moves work from the per-cycle path to build time or
  scope-init. The analysis runs once per program, and the
  runtime executes the partitioned code paths without a
  per-cycle check of whether a value has changed; the lifecycle
  classification guarantees that it has not.
- **Fusion rules are proved locally (NF1–NF4).** Because Node
  Fusion rewrites preserve the slot contract, a fusion rule
  does not need its own proof of soundness across every
  upstream and downstream combination. Its soundness follows
  from NF1–NF4 together with its own equivalence contract
  (`FusedNode::decomposed`).

---

## 8. Compiler boundaries and deterministic ordering

### 8.1 Adapter catalog

The conversions the compiler inserts are exactly the entries of
`auto_adapter`. There is no other implicit or host-discovered
conversion namespace; a type pair with no entry is a type
error, which the author resolves with an explicit conversion
node.

### 8.2 Kernel boundary

Hoisting does not move work across scope kernels. Each child is
compiled and classified independently, and parent values enter
through explicit scope inputs. This keeps each kernel's state,
invalidation, and per-fiber boundary separate.

### 8.3 Node Fusion order

Fusion is deterministic for a fixed graph and rule catalog.
Each fixpoint round examines rules in ascending `priority`
(ties broken by name) and nodes in ascending graph index,
applies the first valid match, then recomputes consumer counts
and starts the round again. An interior node with an external
consumer or a named-output reference is not consumed. A rule's
`priority` is therefore part of the compiler contract, and a
new rule must be equivalence-correct under every lower-priority
rule.

---

[`compile::assembly`]: ../../../polydat-core/src/compile/assembly.rs