datalog_structural_model/ir/head/
rule_head.rs1use std::collections::BTreeSet;
4
5use super::collections::{ExistentialVariables, HeadConjunction, HeadDisjunction};
6use super::form::HeadForm;
7use crate::ir::atom::Atom;
8use crate::ir::symbols::VariableSymbol;
9use crate::position::{HeadAtomIndex, HeadAtomPath, HeadDisjunctIndex};
10
11#[derive(Clone, Debug, Eq, Hash, PartialEq, serde::Serialize, serde::Deserialize)]
17#[serde(deny_unknown_fields)]
18pub enum RuleHead {
19 Atom(Atom),
21 Conjunction(HeadConjunction),
23 Existential {
26 variables: ExistentialVariables,
28 conjunction: HeadConjunction,
30 },
31 Disjunction(HeadDisjunction),
33 Falsum,
35}
36
37impl RuleHead {
38 #[must_use]
40 pub fn form(&self) -> HeadForm {
41 match self {
42 Self::Falsum => HeadForm::Inconsistency,
43 Self::Existential { .. } => HeadForm::Existential,
44 Self::Disjunction(disjunction) if disjunction.disjuncts().len() > 1 => {
45 HeadForm::Disjunctive
46 }
47 Self::Atom(_) | Self::Conjunction(_) | Self::Disjunction(_) => {
48 if self
49 .disjunct(HeadDisjunctIndex::new(0))
50 .map_or(0, <[Atom]>::len)
51 > 1
52 {
53 HeadForm::Conjunctive
54 } else {
55 HeadForm::Atomic
56 }
57 }
58 }
59 }
60
61 #[must_use]
63 pub fn as_atom(&self) -> Option<&Atom> {
64 match self {
65 Self::Atom(atom) => Some(atom),
66 Self::Conjunction(_)
67 | Self::Existential { .. }
68 | Self::Disjunction(_)
69 | Self::Falsum => None,
70 }
71 }
72
73 pub fn as_atom_mut(&mut self) -> Option<&mut Atom> {
75 match self {
76 Self::Atom(atom) => Some(atom),
77 Self::Conjunction(_)
78 | Self::Existential { .. }
79 | Self::Disjunction(_)
80 | Self::Falsum => None,
81 }
82 }
83
84 #[must_use]
86 pub fn disjunct_count(&self) -> usize {
87 match self {
88 Self::Falsum => 0,
89 Self::Disjunction(disjunction) => disjunction.disjuncts().len(),
90 Self::Atom(_) | Self::Conjunction(_) | Self::Existential { .. } => 1,
91 }
92 }
93
94 #[must_use]
96 pub fn disjunct(&self, index: HeadDisjunctIndex) -> Option<&[Atom]> {
97 match (self, index.get()) {
98 (Self::Atom(atom), 0) => Some(std::slice::from_ref(atom)),
99 (Self::Conjunction(conjunction) | Self::Existential { conjunction, .. }, 0) => {
100 Some(conjunction.atoms())
101 }
102 (Self::Disjunction(disjunction), index) => disjunction
103 .disjuncts()
104 .get(index)
105 .map(HeadConjunction::atoms),
106 _ => None,
107 }
108 }
109
110 pub fn atoms(&self) -> impl Iterator<Item = (HeadAtomPath, &Atom)> {
112 (0..self.disjunct_count()).flat_map(move |disjunct| {
113 let disjunct = HeadDisjunctIndex::new(disjunct);
114 self.disjunct(disjunct)
115 .into_iter()
116 .flatten()
117 .enumerate()
118 .map(move |(atom, value)| {
119 (HeadAtomPath::new(disjunct, HeadAtomIndex::new(atom)), value)
120 })
121 })
122 }
123
124 pub fn atoms_mut(&mut self) -> Box<dyn Iterator<Item = &mut Atom> + '_> {
126 match self {
127 Self::Atom(atom) => Box::new(std::iter::once(atom)),
128 Self::Conjunction(conjunction) | Self::Existential { conjunction, .. } => {
129 Box::new(conjunction.atoms_mut().iter_mut())
130 }
131 Self::Disjunction(disjunction) => Box::new(
132 disjunction
133 .disjuncts_mut()
134 .iter_mut()
135 .flat_map(|disjunct| disjunct.atoms_mut().iter_mut()),
136 ),
137 Self::Falsum => Box::new(std::iter::empty()),
138 }
139 }
140
141 #[must_use]
143 pub fn existential_variables(&self) -> &[VariableSymbol] {
144 match self {
145 Self::Existential { variables, .. } => variables.variables(),
146 Self::Atom(_) | Self::Conjunction(_) | Self::Disjunction(_) | Self::Falsum => &[],
147 }
148 }
149
150 #[must_use]
152 pub fn is_existential(&self, variable: &VariableSymbol) -> bool {
153 match self {
154 Self::Existential { variables, .. } => variables.contains(variable),
155 Self::Atom(_) | Self::Conjunction(_) | Self::Disjunction(_) | Self::Falsum => false,
156 }
157 }
158
159 #[must_use]
161 pub fn variables(&self) -> BTreeSet<VariableSymbol> {
162 self.atoms()
163 .flat_map(|(_, atom)| atom.variables())
164 .collect()
165 }
166}
167
168impl From<Atom> for RuleHead {
169 fn from(atom: Atom) -> Self {
170 Self::Atom(atom)
171 }
172}