datalog_structural_model/ir/
atom.rs1use std::collections::BTreeSet;
4use std::fmt;
5
6use super::symbols::{PredicateSymbol, VariableSymbol};
7use super::term::Term;
8
9#[derive(Clone, Debug, Eq, Hash, PartialEq, serde::Serialize, serde::Deserialize)]
11#[serde(deny_unknown_fields)]
12pub struct Atom {
13 pub predicate: PredicateSymbol,
15 pub terms: Vec<Term>,
17}
18
19impl Atom {
20 #[must_use]
22 pub fn arity(&self) -> usize {
23 self.terms.len()
24 }
25
26 #[must_use]
30 pub fn variables(&self) -> BTreeSet<VariableSymbol> {
31 let mut acc = BTreeSet::new();
32 self.collect_variables(&mut acc);
33 acc
34 }
35
36 pub fn visit_variables<'a>(&'a self, visitor: &mut impl FnMut(&'a VariableSymbol)) {
39 for term in &self.terms {
40 term.visit_variables(visitor);
41 }
42 }
43
44 fn collect_variables(&self, acc: &mut BTreeSet<VariableSymbol>) {
45 self.visit_variables(&mut |variable| {
46 acc.insert(variable.clone());
47 });
48 }
49
50 #[must_use]
55 pub(super) fn nesting_depth(&self) -> usize {
56 self.terms
57 .iter()
58 .map(Term::nesting_depth)
59 .max()
60 .unwrap_or(0)
61 }
62}
63
64impl fmt::Display for Atom {
68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 write!(f, "{}(", self.predicate.as_str())?;
70 for (idx, term) in self.terms.iter().enumerate() {
71 if idx > 0 {
72 write!(f, ", ")?;
73 }
74 write!(f, "{term}")?;
75 }
76 write!(f, ")")
77 }
78}