neural/inf/rs/value/src/lib.rs

//! inf value model โ€” the runtime values inf computes over.
//!
//! values are nox atoms (field / word / hash) plus the structural shapes built
//! from them (bool, bytes, list). there is no floating point โ€” numbers are exact
//! field elements or integers. set semantics come for free from the derived
//! total order: a relation is a `BTreeSet<Tuple>`, deduplicated and sorted.

mod field;
pub use field::{F, P};

/// A runtime value. The numeric atoms are exact: `Int` for ordinary integers and
/// counts, `Field` for Goldilocks field elements (scores, weights as committed),
/// `Word` for 32-bit-style unsigned values. No floating point exists.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Value {
    Null,
    Bool(bool),
    Int(i64),
    Field(F),
    Word(u64),
    /// 32-byte digest โ€” particle / neuron identity.
    Hash([u8; 32]),
    /// Raw bytes โ€” particle content and text.
    Bytes(Vec<u8>),
    /// Heterogeneous list โ€” nox cons-structure.
    List(Vec<Value>),
}

/// A row: an ordered list of values.
pub type Tuple = Vec<Value>;

/// A relation: a set of tuples. `BTreeSet` gives datalog set semantics
/// (deduplicated) and sorted iteration (needed by semi-naive evaluation).
pub type Relation = std::collections::BTreeSet<Tuple>;

impl Value {
    pub fn int(i: i64) -> Value {
        Value::Int(i)
    }
    pub fn field(x: u64) -> Value {
        Value::Field(F::from_u64(x))
    }
    pub fn hash(h: [u8; 32]) -> Value {
        Value::Hash(h)
    }
    pub fn str(s: &str) -> Value {
        Value::Bytes(s.as_bytes().to_vec())
    }

    pub fn as_int(&self) -> Option<i64> {
        match self {
            Value::Int(i) => Some(*i),
            _ => None,
        }
    }
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            Value::Bool(b) => Some(*b),
            _ => None,
        }
    }
    pub fn is_null(&self) -> bool {
        matches!(self, Value::Null)
    }

    /// Truthiness for conditions: a non-null, non-false value passes.
    pub fn truthy(&self) -> bool {
        !matches!(self, Value::Null | Value::Bool(false))
    }
}

/// A type-correct, deterministic interface to a particle identity built from a
/// short tag โ€” used by tests and fixtures so `#seed` reads as a stable hash.
pub fn tag_hash(tag: &str) -> [u8; 32] {
    // A small, deterministic, non-cryptographic mapping: enough for fixtures and
    // for the engine, which only ever compares identities for equality/order.
    let mut h = [0u8; 32];
    let b = tag.as_bytes();
    for (i, &c) in b.iter().enumerate() {
        h[i % 32] ^= c.wrapping_add(i as u8);
    }
    h[31] = b.len() as u8;
    h
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn set_semantics_dedup_and_order() {
        let mut r: Relation = Relation::new();
        r.insert(vec![Value::int(2)]);
        r.insert(vec![Value::int(1)]);
        r.insert(vec![Value::int(2)]); // duplicate
        assert_eq!(r.len(), 2);
        let order: Vec<i64> = r.iter().map(|t| t[0].as_int().unwrap()).collect();
        assert_eq!(order, vec![1, 2]); // sorted
    }

    #[test]
    fn cross_type_total_order_is_stable() {
        // variant order: Null < Bool < Int < Field < Word < Hash < Bytes < List
        let mut v = vec![
            Value::List(vec![Value::int(1)]),
            Value::Int(5),
            Value::Null,
            Value::Bool(true),
            Value::field(7),
        ];
        v.sort();
        assert_eq!(v[0], Value::Null);
        assert_eq!(v[1], Value::Bool(true));
        assert_eq!(v[2], Value::Int(5));
        assert_eq!(v[3], Value::field(7));
        assert!(matches!(v[4], Value::List(_)));
    }

    #[test]
    fn truthiness() {
        assert!(Value::int(0).truthy());
        assert!(Value::Bool(true).truthy());
        assert!(!Value::Bool(false).truthy());
        assert!(!Value::Null.truthy());
    }

    #[test]
    fn tag_hash_is_deterministic_and_distinct() {
        assert_eq!(tag_hash("seed"), tag_hash("seed"));
        assert_ne!(tag_hash("seed"), tag_hash("goal"));
    }
}

Homonyms

cyb/optica/src/lib.rs
soft3/strata/src/lib.rs
cyb/honeycrisp/src/lib.rs
warriors/trisha/honeycrisp/lib.rs
warriors/trisha/wgpu/lib.rs
soft3/glia/import/lib.rs
soft3/foculus/src/lib.rs
soft3/nox/rs/lib.rs
soft3/cybergraph/src/lib.rs
soft3/tru/rs/lib.rs
soft3/mudra/src/lib.rs
soft3/glia/run/lib.rs
cyb/prysm/rs/lib.rs
warriors/trisha/rs/lib.rs
cyb/src-tauri/src/lib.rs
soft3/mir/src/lib.rs
soft3/lens/src/lib.rs
neural/trident/src/lib.rs
neural/rune/rs/subject/lib.rs
cyb/cyb/cyb-services/src/lib.rs
soft3/strata/nebu/rs/lib.rs
soft3/lens/core/src/lib.rs
neural/rs/mir-format/src/lib.rs
soft3/zheng/rs/src/lib.rs
neural/rune/rs/interp/lib.rs
soft3/radio/iroh-willow/src/lib.rs
neural/rune/rs/parse/lib.rs
neural/eidos/rs/src/lib.rs
neural/rs/darwin-sys/src/lib.rs
soft3/radio/iroh-gossip/src/lib.rs
soft3/radio/iroh-ffi/src/lib.rs
soft3/radio/iroh-car/src/lib.rs
soft3/radio/iroh-relay/src/lib.rs
soft3/bbg/rs/src/lib.rs
soft3/radio/iroh-docs/src/lib.rs
soft3/lens/ikat/src/lib.rs
neural/rune/rs/lex/lib.rs
cyb/honeycrisp/aruminium/src/lib.rs
soft3/hemera/rs/src/lib.rs
neural/rune/rs/ast/lib.rs
soft3/radio/iroh-blobs/src/lib.rs
cyb/honeycrisp/acpu/src/lib.rs
soft3/lens/porphyry/src/lib.rs
cyb/honeycrisp/rane/src/lib.rs
neural/rune/rs/compile/lib.rs
neural/rune/rs/parse-pure/lib.rs
neural/rs/codegen/src/lib.rs
soft3/lens/binius/src/lib.rs
neural/rune/rs/prysm/lib.rs
neural/rs/link/src/lib.rs
neural/rune/rs/mold/lib.rs
soft3/strata/proof/src/lib.rs
soft3/lens/brakedown/src/lib.rs
soft3/strata/kuro/rs/lib.rs
soft3/lens/assayer/src/lib.rs
neural/rs/core/src/lib.rs
neural/rs/macros/src/lib.rs
soft3/radio/cyber-bao/src/lib.rs
soft3/strata/compute/src/lib.rs
soft3/radio/iroh-base/src/lib.rs
soft3/radio/iroh-dns-server/src/lib.rs
neural/rune/rs/lower/lib.rs
soft3/strata/ext/src/lib.rs
soft3/strata/core/src/lib.rs
soft3/hemera/wgsl/src/lib.rs
soft3/radio/iroh/src/lib.rs
cyb/honeycrisp/unimem/src/lib.rs
cyb/evy/crates/evy_engine_tasks/src/lib.rs
cyb/evy/crates/evy_dialect/src/lib.rs
cyb/wysm/crates/wasi/src/lib.rs
cyb/wysm/crates/fuzz/src/lib.rs
soft3/strata/genies/rs/src/lib.rs
cyb/evy/crates/evy_platform_caps/src/lib.rs
neural/inf/rs/oracle/src/lib.rs
soft3/strata/jali/wgsl/src/lib.rs
cyb/evy/forks/bevy_transform/src/lib.rs
soft3/tape/impl/rust/src/lib.rs
cyb/wysm/crates/wasmi/src/lib.rs
cyb/evy/forks/bevy_render/src/lib.rs
cyb/evy/crates/evy_ecs_storage/src/lib.rs
cyb/evy/forks/naga/src/lib.rs
soft3/strata/trop/wgsl/src/lib.rs
cyb/wysm/crates/c_api/artifact/lib.rs
cyb/evy/forks/bevy_ecs/src/lib.rs
cyb/wysm/crates/ir/src/lib.rs
cyb/evy/forks/bevy_animation/src/lib.rs
cyb/evy/forks/bevy_sprite_render/src/lib.rs
cyb/wysm/crates/c_api/src/lib.rs
neural/inf/rs/parse/src/lib.rs
soft3/strata/trop/rs/src/lib.rs
soft3/strata/kuro/wgsl/src/lib.rs
neural/trident/editor/zed/src/lib.rs
cyb/evy/forks/bevy_mesh/src/lib.rs
cyb/evy/crates/evy_radio/src/lib.rs
cyb/evy/forks/bevy_anti_alias/src/lib.rs
soft3/strata/jali/rs/src/lib.rs
cyb/wysm/crates/wast/src/lib.rs
neural/inf/rs/plan/src/lib.rs
neural/rs/tests/macro-integration/src/lib.rs
soft3/radio/iroh-ffi/iroh-js/src/lib.rs
cyb/evy/forks/bevy_image/src/lib.rs
cyb/evy/forks/bevy_post_process/src/lib.rs
neural/inf/rs/source/src/lib.rs
cyb/wysm/crates/core/src/lib.rs
cyb/evy/crates/evy_diagnostic/src/lib.rs
cyb/evy/crates/evy_engine_dispatch/src/lib.rs
cyb/evy/forks/bevy_pbr/src/lib.rs
cyb/evy/forks/bevy_gizmos/src/lib.rs
cyb/evy/forks/bevy_gizmos_render/src/lib.rs
soft3/radio/iroh/bench/src/lib.rs
neural/inf/rs/lex/src/lib.rs
neural/inf/rs/ast/src/lib.rs
soft3/strata/genies/wgsl/src/lib.rs
soft3/strata/nebu/wgsl/src/lib.rs
cyb/wysm/crates/collections/src/lib.rs
neural/inf/rs/lower/src/lib.rs
cyb/evy/forks/bevy_sprite/src/lib.rs
cyb/evy/forks/bevy_diagnostic/src/lib.rs
neural/inf/rs/eval/src/lib.rs
cyb/wysm/crates/c_api/macro/lib.rs
cyb/evy/forks/bevy_tasks/src/lib.rs
cyb/evy/forks/bevy_core_pipeline/src/lib.rs
cyb/evy/crates/evy_prysm_core/src/lib.rs
cyb/evy/crates/evy_engine_core/src/lib.rs
soft3/radio/tests/integration/src/lib.rs
bootloader/go-cyber/cw/packages/cyber-std-test/src/lib.rs
bootloader/go-cyber/cw/contracts/std-test/src/lib.rs
bootloader/go-cyber/cw/contracts/graph-filter/src/lib.rs
bootloader/go-cyber/cw/packages/cyber-std/src/lib.rs

Graph