datalog-structural-model 3.2.1

Typed Datalog rule IR, validation, and stratification
Documentation
//! Canonical identities of grounded structural values.

use super::{
    domain::{CanonicalPreimage, DomainDigest},
    output::GroundTermDigest,
    sink::PreimageSink,
};
use crate::{GroundAtom, GroundTerm};

impl GroundTerm {
    /// Computes this value's canonical BLAKE3 content identity.
    ///
    /// Preserves exact constant text, term kinds, predicates, argument counts,
    /// argument order and nesting. The encoding is independent of Serde and
    /// uses the `datalog-structural-model/ground-term-digest/v1` domain.
    /// Structural validation remains separate; this method does not select
    /// or enforce a nesting limit.
    #[must_use]
    pub fn digest(&self) -> GroundTermDigest {
        DomainDigest::digest(self)
    }
}

impl DomainDigest for GroundTerm {
    const DOMAIN: &'static str = "datalog-structural-model/ground-term-digest/v1";
    type Output = GroundTermDigest;
}

impl CanonicalPreimage for GroundTerm {
    fn write_preimage(&self, sink: &mut PreimageSink) {
        match self {
            Self::Constant(constant) => {
                sink.tag(0);
                constant.write_preimage(sink);
            }
            Self::TripleTerm(atom) => {
                sink.tag(1);
                atom.write_preimage(sink);
            }
        }
    }
}

impl CanonicalPreimage for GroundAtom {
    fn write_preimage(&self, sink: &mut PreimageSink) {
        self.predicate.write_preimage(sink);
        GroundTerm::write_sequence(&self.args, sink);
    }
}