polydat 0.11.2

Polydat — a variates construction engine
Documentation
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0

//! The questions a host asks about values crossing a scope boundary,
//! each answered by running two programs: a parent scope and a child
//! bound under it.
//!
//! `docs/guides/host_questions.md` quotes this output, and the suite
//! holds the guide to it, so every answer there is what polydat does.
//!
//! Run with: `cargo run -p polydat --example host_questions`

use polydat::Kernel;
use polydat::ast::Value;
use polydat::dsl::compile::compile_polydat_to_assembler;
use polydat::kernel::bind_under;

/// A kernel of `source` on the default engine.
fn kernel(source: &str) -> Box<dyn Kernel> {
    compile_polydat_to_assembler(source)
        .and_then(|asm| asm.compile_kernel())
        .unwrap_or_else(|e| panic!("{e}\n{source}"))
}

/// A kernel of `source` bound under `parent`.
fn child_of(parent: &dyn Kernel, source: &str) -> Box<dyn Kernel> {
    bind_under(parent, kernel(source).into_program(), &[])
        .unwrap_or_else(|e| panic!("{e}\n{source}"))
}

/// What an input holds, as a host reads it without evaluating anything.
fn held(kernel: &dyn Kernel, input: &str) -> String {
    match kernel.input_value(input) {
        Some(Value::None) | None => "nothing yet".to_string(),
        Some(value) => value.to_display_string(),
    }
}

fn main() {
    // The parent scope: two constants and one value computed per cycle.
    let mut parent = kernel(
        "input cycle: u64\n\
         const buckets := 16\n\
         const table := \"users\"\n\
         slot := hash(cycle) % buckets\n",
    );

    println!("== A constant reaches a child when the child is bound ==");
    let mut reader = child_of(
        parent.as_ref(),
        "input cycle: u64\n\
         extern buckets: u64\n\
         extern table: str\n\
         label := \"{table}/{buckets}\"\n",
    );
    println!(
        "before any cycle, buckets holds: {}",
        held(reader.as_ref(), "buckets")
    );
    println!(
        "before any cycle, table holds: {}",
        held(reader.as_ref(), "table")
    );
    reader.set_inputs(&[0]);
    println!("label = {}", reader.pull("label").to_display_string());

    println!();
    println!("== A value computed per cycle reaches a child when the parent computes it ==");
    let mut follower = child_of(
        parent.as_ref(),
        "input cycle: u64\n\
         extern slot: u64\n\
         next := slot + 1\n",
    );
    println!(
        "before any cycle, slot holds: {}",
        held(follower.as_ref(), "slot")
    );
    for cycle in [7u64, 8] {
        parent.set_inputs(&[cycle]);
        let slot = parent.pull("slot").to_display_string();
        follower.set_inputs(&[cycle]);
        println!(
            "cycle {cycle}: parent slot = {slot}, child next = {}",
            follower.pull("next").to_display_string()
        );
    }

    println!();
    println!("== What a child knows when it is constructed ==");
    let knows = child_of(
        parent.as_ref(),
        "input cycle: u64\n\
         extern buckets: u64\n\
         extern slot: u64\n\
         out := slot + buckets\n",
    );
    for (name, port_type) in knows.externs() {
        println!("{name}: {port_type} holds: {}", held(knows.as_ref(), &name));
    }

    println!();
    println!("== Needing at construction a fact about a per-cycle value ==");
    // The fact is the constant the value is made from. The child takes
    // the constant and makes the value itself.
    let mut maker = child_of(
        parent.as_ref(),
        "input cycle: u64\n\
         extern buckets: u64\n\
         slot := hash(cycle) % buckets\n",
    );
    println!(
        "at construction, buckets holds: {}",
        held(maker.as_ref(), "buckets")
    );
    for cycle in [7u64, 8] {
        parent.set_inputs(&[cycle]);
        maker.set_inputs(&[cycle]);
        println!(
            "cycle {cycle}: parent slot = {}, child slot = {}",
            parent.pull("slot").to_display_string(),
            maker.pull("slot").to_display_string()
        );
    }

    println!();
    println!("== A child that declares a name itself owns it ==");
    let mut owner = child_of(
        parent.as_ref(),
        "input cycle: u64\n\
         const buckets := 4\n\
         slot := hash(cycle) % buckets\n",
    );
    owner.set_inputs(&[7]);
    println!(
        "child buckets = {}",
        owner.pull("buckets").to_display_string()
    );
    println!(
        "parent buckets = {}",
        parent.pull("buckets").to_display_string()
    );
}