polydat_core/kernel/bind.rs
1// Copyright 2024-2026 Jonathan Shook
2// SPDX-License-Identifier: Apache-2.0
3
4//! Binding a child kernel under a parent, on any engine
5//! (native_scope_trees.md §5).
6//!
7//! A scope tree is built by binding each child under its parent: the
8//! parent's cells attached, its values copied in, the child's scope-init
9//! constants materialized. The wiring is over `dyn Kernel` on both sides,
10//! so a child of any engine binds under a parent of any engine, and a
11//! parent shared across threads may have children bound under it from
12//! several at once (`Kernel: Sync`).
13
14use std::sync::Arc;
15
16use crate::ast::Value;
17use crate::kernel::{Kernel, KernelProgram, PolydatKernel, WriteError};
18
19/// A kernel of `program` bound under `parent`: `iter_bindings` written
20/// first, so the child's own scope coordinates see them, then the
21/// parent's cells attached and its values copied in.
22///
23/// What the child holds when this returns is what its scope knows at
24/// construction. A parent `const`, and anything else fixed when the
25/// parent was initialized, is in the child's input of the same name,
26/// by value, and [`Kernel::input_value`] reads it. A value the parent
27/// computes per cycle is attached as a cell and holds nothing until
28/// the parent computes it. So a child that needs a fact at
29/// construction declares an `extern` for the constant, not for a
30/// per-cycle value made from it ([host questions guide], and
31/// scope_model.md §4 for the rule form by form).
32///
33/// [host questions guide]: https://github.com/nosqlbench/polydat/blob/main/crates/polydat/docs/guides/host_questions.md
34///
35/// The kernel's [`Kernel::program_id`] is `program`'s, whichever engine
36/// `program` is on, so a host that sealed a plan of indices against the
37/// program can use it on every kernel bound from it.
38pub fn bind_under(
39 parent: &dyn Kernel,
40 program: Arc<dyn KernelProgram>,
41 iter_bindings: &[(String, Value)],
42) -> Result<Box<dyn Kernel>, crate::KernelError> {
43 let mut child = program.create_uninitialized();
44 for (var, value) in iter_bindings {
45 child
46 .set_input(var, value.clone())
47 .map_err(crate::KernelError::Write)?;
48 }
49 // Binding initializes the child once the parent's values are in.
50 PolydatKernel::wire_child_under(child.as_mut(), parent)?;
51 Ok(child)
52}
53
54/// Copy `parent`'s input values into `child`'s inputs of the same name:
55/// what carries a cascade of externs down a scope tree past what the
56/// child imports as outputs.
57///
58/// Coordinates are not copied; a host positions a child with
59/// `set_inputs`. An input with no value on the parent is skipped. A
60/// value the child's declared input refuses is an error naming both,
61/// not a skip (input_variance.md §7).
62pub fn propagate_inputs(parent: &dyn Kernel, child: &mut dyn Kernel) -> Result<(), WriteError> {
63 let child_coords = child.coord_count();
64 for (index, name) in parent.input_names().iter().enumerate() {
65 let Some(value) = parent.input_value_at(index) else {
66 continue;
67 };
68 if matches!(value, Value::None) {
69 continue;
70 }
71 let Some(child_index) = child.input_index(name) else {
72 continue;
73 };
74 // A coordinate is positioned with `set_inputs`; a cell-bound slot
75 // reads its cell, and writing it would publish into a register
76 // the whole scope shares.
77 if child_index < child_coords || child.input_is_cell_bound(child_index) {
78 continue;
79 }
80 child.set_input_at(child_index, value)?;
81 }
82 Ok(())
83}