polydat 0.11.2

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
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0

//! One compile path behind every entry point. A source string, a parsed
//! file, or an assembler a host built by hand reaches the same graph and
//! the same kernel on every engine; the options mean the same thing
//! whichever entry point carries them; and what the interpreter refuses
//! under strict, every engine refuses.

use polydat::ast::Value;
use polydat::compile::assembly::{PolydatAssembler, WireRef};
use polydat::dsl::compile::{
    CompileOptions, compile_polydat, compile_polydat_interpreter,
    compile_polydat_interpreter_with_log, compile_polydat_interpreter_with_options,
    compile_polydat_kernel, compile_polydat_kernel_with_options, compile_polydat_to_assembler,
    compile_polydat_to_assembler_with, compile_polydat_with, compile_polydat_with_engine,
};
use polydat::dsl::events::CompileEventLog;
use polydat::{Engine, JitMode, Kernel, KernelError, Provenance};

const SRC: &str = "input cycle: u64\nh := hash(cycle)\nk := mod_wire(h, 7)\ns := \"k={k}\"\n";

/// Every engine a build can name; a refusal is skipped, as elsewhere.
fn engines() -> Vec<Engine> {
    let mut all = vec![
        Engine::Interpreter(JitMode::Auto),
        Engine::Closures(Provenance::Auto),
        Engine::Closures(Provenance::Raw),
    ];
    if cfg!(feature = "jit") {
        all.push(Engine::Native(Provenance::Auto));
        all.push(Engine::Native(Provenance::Raw));
        all.push(Engine::PureNative(Provenance::Auto));
        all.push(Engine::PureNative(Provenance::Raw));
    }
    all
}

fn values(k: &mut dyn Kernel, cycle: u64) -> Vec<Value> {
    k.set_inputs(&[cycle]);
    k.output_names().iter().map(|o| k.pull(o)).collect()
}

#[test]
fn every_entry_point_reaches_the_same_kernel() {
    let mut oracle = compile_polydat_interpreter(SRC).unwrap();
    let want: Vec<Value> = values(&mut oracle, 5);
    let options = CompileOptions::default();
    let mut log = CompileEventLog::new();
    let mut kernels: Vec<(&str, Box<dyn Kernel>)> = vec![
        (
            "compile_polydat_kernel",
            compile_polydat_kernel(SRC).unwrap(),
        ),
        (
            "compile_polydat_kernel_with_options",
            compile_polydat_kernel_with_options(SRC, &options, Some(&mut log)).unwrap(),
        ),
        (
            "compile_polydat_interpreter_with_options",
            Box::new(compile_polydat_interpreter_with_options(SRC, &options, None).unwrap()),
        ),
        (
            "compile_polydat_interpreter_with_log",
            Box::new(
                compile_polydat_interpreter_with_log(SRC, &mut CompileEventLog::new()).unwrap(),
            ),
        ),
        (
            "compile_polydat_to_assembler",
            compile_polydat_to_assembler(SRC)
                .unwrap()
                .compile_kernel()
                .unwrap(),
        ),
        (
            "compile_polydat_to_assembler_with",
            Box::new(
                compile_polydat_to_assembler_with(SRC, &options)
                    .unwrap()
                    .compile()
                    .unwrap(),
            ),
        ),
    ];
    for engine in engines() {
        if let Ok(k) = compile_polydat_with(SRC, engine) {
            kernels.push(("compile_polydat_with", k));
        }
        if let Ok(k) = compile_polydat_with_engine(SRC, engine, &options, None) {
            kernels.push(("compile_polydat_with_engine", k));
        }
        if let Ok(k) = compile_polydat_to_assembler(SRC)
            .unwrap()
            .compile_with(engine)
        {
            kernels.push(("PolydatAssembler::compile_with", k));
        }
    }
    for (name, k) in kernels.iter_mut() {
        assert_eq!(
            k.output_names(),
            oracle.output_names(),
            "{name} on {}",
            k.engine()
        );
        assert_eq!(values(k.as_mut(), 5), want, "{name} on {}", k.engine());
    }
}

/// The assembler a host builds by hand is the same first-class road to a
/// kernel: nodes and wires added one by one compile on every engine to
/// what the same graph from source computes.
#[test]
fn a_hand_built_assembler_compiles_on_every_engine() {
    fn build() -> PolydatAssembler {
        let mut asm = PolydatAssembler::new(vec!["cycle".into()]);
        use polydat::ast::PortType;
        let ctx = polydat::dsl::factory::BuildContext::default();
        let hash = polydat::dsl::factory::build_node(
            &ctx,
            "hash",
            &[WireRef::input("cycle")],
            &[PortType::U64],
            &[],
        )
        .unwrap();
        asm.add_node("h", hash, vec![WireRef::input("cycle")]);
        let m = polydat::dsl::factory::build_node(
            &ctx,
            "mod",
            &[WireRef::node("h")],
            &[PortType::U64],
            &[polydat::dsl::ConstArg::Int(7)],
        )
        .unwrap();
        asm.add_node("k", m, vec![WireRef::node("h")]);
        asm.add_output("h", WireRef::node("h"));
        asm.add_output("k", WireRef::node("k"));
        asm
    }
    fn hk(k: &mut dyn Kernel) -> (Value, Value) {
        k.set_inputs(&[9]);
        (k.pull("h"), k.pull("k"))
    }
    let mut from_source =
        compile_polydat_interpreter("input cycle: u64\nh := hash(cycle)\nk := mod(h, 7)\n")
            .unwrap();
    let want = hk(&mut from_source);
    let mut p1 = build().compile().unwrap();
    assert_eq!(hk(&mut p1), want);
    for engine in engines() {
        let mut k = match build().compile_with(engine) {
            Ok(k) => k,
            Err(KernelError::Refused { .. }) => continue,
            Err(e) => panic!("{engine}: {e}"),
        };
        assert_eq!(hk(k.as_mut()), want, "{engine}");
    }
    let mut default = build().compile_kernel().unwrap();
    // The default engine, in the provenance mode the selector chose.
    assert!(matches!(
        (default.engine(), Engine::default()),
        (Engine::Native(_), Engine::Native(_)) | (Engine::Closures(_), Engine::Closures(_))
    ));
    assert_eq!(hk(default.as_mut()), want);
}

/// What strict refuses, it refuses on every engine: an implicit adapter
/// between a float wire and an integer port is an error under
/// `strict: true` whichever engine the options reach.
#[test]
fn strict_refuses_implicit_coercions_on_every_engine() {
    let src = "input cycle: u64\nh := hash(cycle)\nf := sqrt(h)\n";
    let strict = CompileOptions {
        strict: true,
        ..CompileOptions::default()
    };
    assert!(compile_polydat_interpreter_with_options(src, &strict, None).is_err());
    for engine in engines() {
        let outcome = compile_polydat_with_engine(src, engine, &strict, None);
        assert!(
            matches!(
                outcome,
                Err(KernelError::Assembly(_)) | Err(KernelError::Source(_))
            ),
            "{engine}: strict must refuse the implicit coercion, got {:?}",
            outcome.map(|k| k.engine())
        );
        let lax = compile_polydat_with_engine(src, engine, &CompileOptions::default(), None);
        assert!(
            lax.is_ok() || matches!(lax, Err(KernelError::Refused { .. })),
            "{engine}: {:?}",
            lax.err()
        );
    }
}

/// The extended API is a subtrait, not a hidden corner of the core one.
///
/// `compile_slots` hands back a `Box<dyn SlotKernel>`, so a caller
/// reaches the slot surface — a buffer slot, a raw `u64`, an evaluation
/// that returns one — without naming a kernel type, and the same box
/// upcasts to `Box<dyn Kernel>` wherever the ordinary surface will do.
/// The interpreter is refused rather than half-answered: it holds typed
/// `Value` buffers and has no slot to name.
#[test]
fn the_slot_surface_is_a_subtrait_reachable_without_a_kernel_type() {
    use polydat::SlotKernel;

    let mut oracle = compile_polydat_interpreter(SRC).unwrap();
    let want = values(&mut oracle, 5);

    for engine in engines() {
        if matches!(engine, Engine::Interpreter(_)) {
            // Honestly refused, with a reason that says why and what to
            // ask for instead.
            match compile_polydat_to_assembler(SRC)
                .unwrap()
                .compile_slots(engine)
            {
                Err(KernelError::Refused { reason, .. }) => {
                    assert!(reason.contains("slot"), "{reason}")
                }
                Err(e) => panic!("expected a refusal naming the slot buffer, got {e}"),
                Ok(_) => panic!("the interpreter has no slot buffer to hand back"),
            }
            continue;
        }
        let mut k = match compile_polydat_to_assembler(SRC)
            .unwrap()
            .compile_slots(engine)
        {
            Ok(k) => k,
            Err(KernelError::Refused { .. }) => continue,
            Err(e) => panic!("{engine}: {e}"),
        };
        // The extended surface: a slot, then an evaluation that returns
        // the raw word rather than a `Value`.
        let slot = SlotKernel::resolve_output(k.as_ref(), "k").expect("a named output");
        let raw = k.eval_for_slot(&[5], slot);
        assert_eq!(raw, SlotKernel::get(k.as_ref(), "k"));

        // And the core surface, on the same box, because the extended
        // trait extends it. No second build, no downcast.
        assert_eq!(k.engine(), engine_of(engine, k.engine()));
        assert_eq!(values(k.as_mut(), 5), want, "{engine}");
        assert_eq!(k.pull("k").as_u64(), raw, "{engine}");
    }
}

/// `Auto` delegates the provenance choice, so the reported engine is
/// the tier that was asked for with whatever mode the selector took.
fn engine_of(asked: Engine, reported: Engine) -> Engine {
    match (asked, reported) {
        (Engine::Closures(Provenance::Auto), r @ Engine::Closures(_)) => r,
        (Engine::Native(Provenance::Auto), r @ Engine::Native(_)) => r,
        (Engine::PureNative(Provenance::Auto), r @ Engine::PureNative(_)) => r,
        _ => asked,
    }
}

/// The pure native tier is an engine a host names like any other: it
/// arrives through the same options, it reports itself rather than the
/// hybrid it is the differential for, and a provenance mode it has no
/// kernel for is refused instead of answered with another.
#[test]
#[cfg(feature = "jit")]
fn the_pure_tier_names_itself_and_refuses_the_modes_it_lacks() {
    let mut oracle = compile_polydat_interpreter(SRC).unwrap();
    let want = values(&mut oracle, 5);

    for prov in [Provenance::Auto, Provenance::Raw, Provenance::PushPull] {
        let engine = Engine::PureNative(prov);
        let mut k = compile_polydat_with(SRC, engine).unwrap_or_else(|e| panic!("{engine}: {e}"));
        // The tier it ran, not the `Native` tier it stands behind.
        let ran = k.engine();
        assert!(
            matches!(ran, Engine::PureNative(_)),
            "{engine}: reported {ran}",
        );
        // A named mode is the mode; `Auto` delegated the choice.
        if prov != Provenance::Auto {
            assert_eq!(ran, engine, "a named mode is honoured or refused");
        }
        assert_eq!(values(k.as_mut(), 5), want, "{engine}");
    }

    // Push and pull have no pure kernel, so they are refusals naming
    // the engine, not silent substitutions of push+pull.
    for prov in [Provenance::Push, Provenance::Pull] {
        let engine = Engine::PureNative(prov);
        match compile_polydat_with(SRC, engine) {
            Err(KernelError::Refused {
                engine: named,
                reason,
            }) => {
                assert!(matches!(named, Engine::PureNative(_)), "{named}");
                assert!(reason.contains("pure native"), "{reason}");
            }
            other => panic!(
                "{engine}: expected a refusal, got {:?}",
                other.map(|k| k.engine())
            ),
        }
    }
}

/// What is knowable at build is known at build, and fails at build. A
/// step no input reaches runs once while the kernel is being built, so
/// a constant that cannot be computed reports it then rather than on
/// the first pull.
///
/// Every engine does this and reports it the same way: the same
/// `KernelError` variant, and a message carrying the node's own
/// failure. It is `ConstantFold` and not `Refused` because no engine is
/// declining a program the others accept — the value cannot be
/// computed anywhere. The pure tier keeps no step list, so its
/// constants are compiled a second time into an entry of their own and
/// run over the same buffer to reach the same rule.
#[test]
fn every_engine_folds_its_constants_at_build() {
    let src = "input cycle: u64\nn := mod_wire(10, 0)\nv := n + cycle\n";
    for engine in engines() {
        let built = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            compile_polydat_with(src, engine).map(|k| k.engine())
        }))
        .unwrap_or_else(|_| panic!("{engine}: a build failure is an error, never a panic"));
        match built {
            Err(KernelError::ConstantFold { reason }) => assert!(
                reason.contains("zero") && reason.contains("mod_wire"),
                "{engine}: the message carries the node's own failure: {reason}",
            ),
            other => panic!(
                "{engine}: a constant that cannot be computed is a ConstantFold error at \
                 build, got {other:?}",
            ),
        }
    }
    // The same node with the divisor on a wire is not a constant, so
    // nothing runs it at build and every engine accepts the program.
    // The rule is about what is knowable at build, not about the node.
    let dynamic = "input cycle: u64\nn := mod_wire(10, cycle)\n";
    for engine in engines() {
        assert!(
            compile_polydat_with(dynamic, engine).is_ok(),
            "{engine}: a dynamic divisor is not a compile-time constant",
        );
    }
}

/// `Native` and `PureNative` differ in one thing: what becomes of a node
/// with no native form. `Native` runs that node's closure and always
/// succeeds, so it can never tell a host whether its program went fully
/// native; the pure tier refuses and names the node, which is the whole
/// reason to ask for it. Every node in this library lowers today, so the
/// two agree here — what this pins is that they agree in value and that
/// each reports its own tier.
#[test]
#[cfg(feature = "jit")]
fn the_two_native_tiers_agree_in_value_and_differ_in_name() {
    let mut hybrid = compile_polydat_with(SRC, Engine::Native(Provenance::Auto)).unwrap();
    let mut pure = compile_polydat_with(SRC, Engine::PureNative(Provenance::Auto)).unwrap();
    assert!(matches!(hybrid.engine(), Engine::Native(_)));
    assert!(matches!(pure.engine(), Engine::PureNative(_)));
    assert_eq!(values(hybrid.as_mut(), 9), values(pure.as_mut(), 9));
}

/// The diagnostic entry point diagnoses the program it would compile.
///
/// `compile_polydat_checked` builds the same program the kernel entry
/// points build, pragmas and source text included, so a host is never
/// told a program is fine and then has it refused, or the reverse.
/// It goes through `compile_ast_with_engine` on the default
/// engine, so it accepts exactly what `compile_polydat_kernel` accepts,
/// across the shapes that depend on the pragmas and the source text.
///
/// `compile_polydat_to_assembler` is the one entry point that may
/// disagree, and only about `for`: an assembler holds no traversal, so
/// it refuses a program with one and says so. That is a property of
/// what an assembler is, not a second opinion about the program.
#[test]
fn the_diagnostic_entry_point_agrees_with_the_compile_it_diagnoses() {
    use polydat::dsl::compile::compile_polydat_checked;

    for (label, src) in [
        ("plain", "input cycle: u64\nh := hash(cycle)\n"),
        (
            "traversal",
            "input cycle: u64\nfor k in 1..3 {\n  y := k * 10\n}\n",
        ),
        (
            "pragma",
            "pragma strict_types\ninput cycle: u64\nh := hash(cycle)\n",
        ),
        ("tile", "input cycle: u64\ntile t := \"c=${cycle}\"\n"),
        (
            "module",
            "input cycle: u64\nd(a: u64) -> (o: u64) := {\n  o := a + 1\n}\nx := d(cycle)\n",
        ),
        ("broken", "input cycle: u64\nh := no_such_function(cycle)\n"),
    ] {
        let compiles = compile_polydat_kernel(src).is_ok();
        let (checked, report) = compile_polydat_checked(src);
        assert_eq!(
            checked.is_ok(),
            compiles,
            "{label}: the diagnostic entry disagrees with the compile it diagnoses"
        );
        assert_eq!(
            report.errors().is_empty(),
            compiles,
            "{label}: the report should carry an error exactly when the compile fails"
        );
    }

    // The assembler's one documented disagreement, and only that one.
    assert!(compile_polydat_to_assembler("input cycle: u64\nh := hash(cycle)\n").is_ok());
    assert!(
        compile_polydat_to_assembler("input cycle: u64\nfor k in 1..3 {\n  y := k * 10\n}\n")
            .is_err(),
        "an assembler holds no traversal, so it refuses a program with one"
    );
}

/// The logged interpreter compile is the plain one with a log: a program
/// with a traversal compiles through it, as it does everywhere else.
#[test]
fn the_logged_compile_accepts_a_traversal() {
    let src = "input cycle: u64\nfor k in 1..3 {\n  y := k * 10\n}\n";
    let mut log = CompileEventLog::new();
    let k = compile_polydat_interpreter_with_log(src, &mut log)
        .expect("a for statement compiles with a log");
    assert_eq!(k.traversals().len(), 1);
}

/// A cursor extent computed from a constant expression is resolved on
/// every engine, not only on the interpreter: `cursor_schemas` reports
/// the same extent whichever engine built the kernel.
#[test]
fn deferred_cursor_extents_resolve_on_every_engine() {
    let src = "input cycle: u64\nn := 10 + 5\ncursor q = range(0, n)\nv := hash(q.ordinal)\n";
    let want = compile_polydat_interpreter(src).unwrap().cursor_schemas()[0].extent;
    assert_eq!(want, Some(15));
    for engine in engines() {
        let k = match compile_polydat_with(src, engine) {
            Ok(k) => k,
            Err(KernelError::Refused { .. }) => continue,
            Err(e) => panic!("{engine}: {e}"),
        };
        assert_eq!(k.cursor_schemas()[0].extent, want, "{engine}");
    }
}

/// A module the program defines is visible inside its `for` bodies
/// whichever way the program was compiled: from a string with no source
/// directory, the body resolves the module from the program, not from a
/// file on disk.
#[test]
fn a_program_module_is_visible_inside_a_traversal_body() {
    let src = "\
input cycle: u64
twice(x: u64) -> (y: u64) := {
  y := x * 2
}
for k in 1..4 {
  d := twice(k)
}
";
    for engine in engines() {
        let mut k = match compile_polydat_with(src, engine) {
            Ok(k) => k,
            Err(KernelError::Refused { .. }) => continue,
            Err(e) => panic!("{engine}: {e}"),
        };
        k.set_inputs(&[0]);
        let streams = k.traverse_all().unwrap();
        let mut seen = Vec::new();
        for s in &streams {
            for i in 0..s.len() {
                let mut act = s.activation(i).unwrap();
                act.kernel.set_inputs(&[0]);
                seen.push(act.kernel.pull("d").as_u64());
            }
        }
        assert_eq!(seen, vec![2, 4, 6], "{engine}");
    }
}

/// Asking for no engine gets the build's default, which is compiled
/// code — not the interpreter.
///
/// This is the rule the plainest entry point is most able to break
/// quietly: it hands back `dyn Kernel` either way, so a body that built
/// the interpreter would look right at every call site and cost every
/// caller the ladder. The engine is a value the options carry, and the default
/// value is the most native form the build has.
#[test]
fn the_engine_less_entry_points_build_the_default_engine() {
    let src = "out := hash(cycle)\n";
    let expected = Engine::default();

    for (name, kernel) in [
        (
            "compile_polydat",
            compile_polydat(src).expect("it compiles"),
        ),
        (
            "compile_polydat_kernel",
            compile_polydat_kernel(src).expect("it compiles"),
        ),
        (
            "compile_polydat_kernel_with_options",
            compile_polydat_kernel_with_options(src, &CompileOptions::default(), None)
                .expect("it compiles"),
        ),
    ] {
        let got = kernel.engine();
        assert_ne!(
            got,
            Engine::Interpreter(JitMode::Off),
            "{name} built the interpreter; an engine-less entry point builds {expected}"
        );
        assert!(
            matches!(got, Engine::Native(_) | Engine::Closures(_)),
            "{name} built {got}, which is neither tier of {expected}"
        );
    }
}

/// The interpreter is reachable, and reached by name. It is the
/// oracle a differential test compares against, so it has to be, and
/// a caller that wants it says so.
#[test]
fn the_interpreter_is_reached_by_naming_it() {
    let src = "out := hash(cycle)\n";

    let named = compile_polydat_interpreter(src).expect("it compiles");
    assert!(
        matches!(Kernel::engine(&named), Engine::Interpreter(_)),
        "compile_polydat_interpreter must build the interpreter"
    );

    let mut configured = {
        let options = CompileOptions {
            engine: Engine::Interpreter(JitMode::Off),
            ..CompileOptions::default()
        };
        compile_polydat_kernel_with_options(src, &options, None).expect("it compiles")
    };
    assert!(
        matches!(configured.engine(), Engine::Interpreter(_)),
        "an engine asked for in the options is the engine that is built"
    );

    // And it computes what the default engine computes: the oracle is
    // an oracle, not a different program.
    let mut default_engine = compile_polydat(src).expect("it compiles");
    for cycle in [0u64, 1, 7, 99] {
        assert_eq!(
            values(configured.as_mut(), cycle),
            values(default_engine.as_mut(), cycle),
            "cycle {cycle}"
        );
    }
}