Skip to main content

datalog_structural_model/digest/
ir_preimage.rs

1//! Canonical preimages of programs, rules, literals, atoms and terms.
2
3use super::{
4    domain::{CanonicalPreimage, DomainDigest},
5    output::ProgramDigest,
6    sink::PreimageSink,
7};
8use crate::{
9    AggregateBody, Aggregator, Atom, ConstantSymbol, HeadConjunction, Literal, PredicateSymbol,
10    Program, Rule, RuleHead, Term, VariableSymbol,
11};
12
13/// Leading count of every non-atomic head. An atomic head starts with its
14/// predicate's text length, which can never be `u64::MAX`, so this prefix
15/// keeps atomic-head preimages byte-identical to the original encoding while
16/// staying unambiguous.
17const NON_ATOMIC_HEAD: u64 = u64::MAX;
18
19impl DomainDigest for Program {
20    const DOMAIN: &'static str = "datalog-structural-model/program-digest/v1";
21    type Output = ProgramDigest;
22}
23
24impl CanonicalPreimage for Program {
25    fn write_preimage(&self, sink: &mut PreimageSink) {
26        Rule::write_sequence(&self.rules, sink);
27    }
28}
29
30impl CanonicalPreimage for Rule {
31    fn write_preimage(&self, sink: &mut PreimageSink) {
32        self.head.write_preimage(sink);
33        Literal::write_sequence(&self.body, sink);
34    }
35}
36
37impl CanonicalPreimage for RuleHead {
38    fn write_preimage(&self, sink: &mut PreimageSink) {
39        if let Self::Atom(atom) = self {
40            atom.write_preimage(sink);
41            return;
42        }
43        sink.word64(NON_ATOMIC_HEAD);
44        match self {
45            Self::Atom(_) => unreachable!("atomic heads were written above"),
46            Self::Conjunction(conjunction) => {
47                sink.tag(0);
48                conjunction.write_preimage(sink);
49            }
50            Self::Existential {
51                variables,
52                conjunction,
53            } => {
54                sink.tag(1);
55                VariableSymbol::write_sequence(variables.variables(), sink);
56                conjunction.write_preimage(sink);
57            }
58            Self::Disjunction(disjunction) => {
59                sink.tag(2);
60                HeadConjunction::write_sequence(disjunction.disjuncts(), sink);
61            }
62            Self::Falsum => sink.tag(3),
63        }
64    }
65}
66
67impl CanonicalPreimage for HeadConjunction {
68    fn write_preimage(&self, sink: &mut PreimageSink) {
69        Atom::write_sequence(self.atoms(), sink);
70    }
71}
72
73impl CanonicalPreimage for Atom {
74    fn write_preimage(&self, sink: &mut PreimageSink) {
75        self.predicate.write_preimage(sink);
76        Term::write_sequence(&self.terms, sink);
77    }
78}
79
80impl CanonicalPreimage for PredicateSymbol {
81    fn write_preimage(&self, sink: &mut PreimageSink) {
82        sink.text(self.as_str());
83    }
84}
85
86impl CanonicalPreimage for VariableSymbol {
87    fn write_preimage(&self, sink: &mut PreimageSink) {
88        sink.text(self.as_str());
89    }
90}
91
92impl CanonicalPreimage for ConstantSymbol {
93    fn write_preimage(&self, sink: &mut PreimageSink) {
94        sink.text(self.as_str());
95    }
96}
97
98impl CanonicalPreimage for Term {
99    fn write_preimage(&self, sink: &mut PreimageSink) {
100        match self {
101            Self::Variable(var) => {
102                sink.tag(0);
103                var.write_preimage(sink);
104            }
105            Self::Constant(constant) => {
106                sink.tag(1);
107                constant.write_preimage(sink);
108            }
109            Self::TripleTerm(atom) => {
110                sink.tag(2);
111                atom.write_preimage(sink);
112            }
113            Self::Skolem { name, args } => {
114                sink.tag(3);
115                name.write_preimage(sink);
116                Self::write_sequence(args, sink);
117            }
118        }
119    }
120}
121
122impl CanonicalPreimage for Literal {
123    fn write_preimage(&self, sink: &mut PreimageSink) {
124        match self {
125            Self::Positive(atom) => {
126                sink.tag(0);
127                atom.write_preimage(sink);
128            }
129            Self::Negative(atom) => {
130                sink.tag(1);
131                atom.write_preimage(sink);
132            }
133            Self::Filter(filter) => {
134                sink.tag(2);
135                filter.write_preimage(sink);
136            }
137            Self::Bind { var, expr } => {
138                sink.tag(3);
139                var.write_preimage(sink);
140                expr.write_preimage(sink);
141            }
142            Self::Aggregate(body) => {
143                sink.tag(4);
144                body.write_preimage(sink);
145            }
146        }
147    }
148}
149
150impl CanonicalPreimage for Aggregator {
151    fn write_preimage(&self, sink: &mut PreimageSink) {
152        sink.tag(match self {
153            Self::Count => 0,
154            Self::Sum => 1,
155            Self::Min => 2,
156            Self::Max => 3,
157            Self::Avg => 4,
158        });
159    }
160}
161
162impl CanonicalPreimage for AggregateBody {
163    fn write_preimage(&self, sink: &mut PreimageSink) {
164        self.result_var.write_preimage(sink);
165        self.aggregator.write_preimage(sink);
166        self.input_var.write_preimage(sink);
167        VariableSymbol::write_sequence(&self.group_by, sink);
168        Literal::write_sequence(&self.inner_body, sink);
169    }
170}
171
172impl Program {
173    /// Canonical content identity of this program as authored.
174    ///
175    /// Rule order, body literal order and argument order are significant.
176    /// The digest is computed from a versioned, length-framed preimage and
177    /// never from a Serde encoding.
178    #[must_use]
179    pub fn digest(&self) -> ProgramDigest {
180        DomainDigest::digest(self)
181    }
182}