use std::collections::BTreeSet;
use super::collections::{ExistentialVariables, HeadConjunction, HeadDisjunction};
use super::form::HeadForm;
use crate::ir::atom::Atom;
use crate::ir::symbols::VariableSymbol;
use crate::position::{HeadAtomIndex, HeadAtomPath, HeadDisjunctIndex};
#[derive(Clone, Debug, Eq, Hash, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub enum RuleHead {
Atom(Atom),
Conjunction(HeadConjunction),
Existential {
variables: ExistentialVariables,
conjunction: HeadConjunction,
},
Disjunction(HeadDisjunction),
Falsum,
}
impl RuleHead {
#[must_use]
pub fn form(&self) -> HeadForm {
match self {
Self::Falsum => HeadForm::Inconsistency,
Self::Existential { .. } => HeadForm::Existential,
Self::Disjunction(disjunction) if disjunction.disjuncts().len() > 1 => {
HeadForm::Disjunctive
}
Self::Atom(_) | Self::Conjunction(_) | Self::Disjunction(_) => {
if self
.disjunct(HeadDisjunctIndex::new(0))
.map_or(0, <[Atom]>::len)
> 1
{
HeadForm::Conjunctive
} else {
HeadForm::Atomic
}
}
}
}
#[must_use]
pub fn as_atom(&self) -> Option<&Atom> {
match self {
Self::Atom(atom) => Some(atom),
Self::Conjunction(_)
| Self::Existential { .. }
| Self::Disjunction(_)
| Self::Falsum => None,
}
}
pub fn as_atom_mut(&mut self) -> Option<&mut Atom> {
match self {
Self::Atom(atom) => Some(atom),
Self::Conjunction(_)
| Self::Existential { .. }
| Self::Disjunction(_)
| Self::Falsum => None,
}
}
#[must_use]
pub fn disjunct_count(&self) -> usize {
match self {
Self::Falsum => 0,
Self::Disjunction(disjunction) => disjunction.disjuncts().len(),
Self::Atom(_) | Self::Conjunction(_) | Self::Existential { .. } => 1,
}
}
#[must_use]
pub fn disjunct(&self, index: HeadDisjunctIndex) -> Option<&[Atom]> {
match (self, index.get()) {
(Self::Atom(atom), 0) => Some(std::slice::from_ref(atom)),
(Self::Conjunction(conjunction) | Self::Existential { conjunction, .. }, 0) => {
Some(conjunction.atoms())
}
(Self::Disjunction(disjunction), index) => disjunction
.disjuncts()
.get(index)
.map(HeadConjunction::atoms),
_ => None,
}
}
pub fn atoms(&self) -> impl Iterator<Item = (HeadAtomPath, &Atom)> {
(0..self.disjunct_count()).flat_map(move |disjunct| {
let disjunct = HeadDisjunctIndex::new(disjunct);
self.disjunct(disjunct)
.into_iter()
.flatten()
.enumerate()
.map(move |(atom, value)| {
(HeadAtomPath::new(disjunct, HeadAtomIndex::new(atom)), value)
})
})
}
pub fn atoms_mut(&mut self) -> Box<dyn Iterator<Item = &mut Atom> + '_> {
match self {
Self::Atom(atom) => Box::new(std::iter::once(atom)),
Self::Conjunction(conjunction) | Self::Existential { conjunction, .. } => {
Box::new(conjunction.atoms_mut().iter_mut())
}
Self::Disjunction(disjunction) => Box::new(
disjunction
.disjuncts_mut()
.iter_mut()
.flat_map(|disjunct| disjunct.atoms_mut().iter_mut()),
),
Self::Falsum => Box::new(std::iter::empty()),
}
}
#[must_use]
pub fn existential_variables(&self) -> &[VariableSymbol] {
match self {
Self::Existential { variables, .. } => variables.variables(),
Self::Atom(_) | Self::Conjunction(_) | Self::Disjunction(_) | Self::Falsum => &[],
}
}
#[must_use]
pub fn is_existential(&self, variable: &VariableSymbol) -> bool {
match self {
Self::Existential { variables, .. } => variables.contains(variable),
Self::Atom(_) | Self::Conjunction(_) | Self::Disjunction(_) | Self::Falsum => false,
}
}
#[must_use]
pub fn variables(&self) -> BTreeSet<VariableSymbol> {
self.atoms()
.flat_map(|(_, atom)| atom.variables())
.collect()
}
}
impl From<Atom> for RuleHead {
fn from(atom: Atom) -> Self {
Self::Atom(atom)
}
}