use std::collections::BTreeSet;
use std::{error::Error, fmt};
use crate::error_code::ErrorCode;
use crate::ir::atom::Atom;
use crate::ir::symbols::VariableSymbol;
#[derive(
Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
)]
pub enum HeadCollectionKind {
Conjunction,
Disjunction,
ExistentialVariables,
}
impl fmt::Display for HeadCollectionKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Conjunction => "head conjunction",
Self::Disjunction => "head disjunction",
Self::ExistentialVariables => "existential variable set",
})
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EmptyHeadCollection {
kind: HeadCollectionKind,
}
impl EmptyHeadCollection {
pub const CODE_EMPTY_HEAD_COLLECTION: ErrorCode = ErrorCode::new("empty-head-collection");
const fn new(kind: HeadCollectionKind) -> Self {
Self { kind }
}
#[must_use]
pub const fn kind(&self) -> HeadCollectionKind {
self.kind
}
#[must_use]
pub const fn code(&self) -> ErrorCode {
Self::CODE_EMPTY_HEAD_COLLECTION
}
}
impl fmt::Display for EmptyHeadCollection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"[{}] a {} needs at least one element",
self.code(),
self.kind
)
}
}
impl Error for EmptyHeadCollection {}
#[derive(Clone, Debug, Eq, Hash, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(try_from = "Vec<Atom>", into = "Vec<Atom>")]
pub struct HeadConjunction(
Vec<Atom>,
);
impl HeadConjunction {
pub fn try_new(atoms: Vec<Atom>) -> Result<Self, EmptyHeadCollection> {
if atoms.is_empty() {
return Err(EmptyHeadCollection::new(HeadCollectionKind::Conjunction));
}
Ok(Self(atoms))
}
#[must_use]
pub fn atoms(&self) -> &[Atom] {
&self.0
}
pub fn atoms_mut(&mut self) -> &mut [Atom] {
&mut self.0
}
}
impl TryFrom<Vec<Atom>> for HeadConjunction {
type Error = EmptyHeadCollection;
fn try_from(atoms: Vec<Atom>) -> Result<Self, Self::Error> {
Self::try_new(atoms)
}
}
impl From<HeadConjunction> for Vec<Atom> {
fn from(conjunction: HeadConjunction) -> Self {
conjunction.0
}
}
impl From<Atom> for HeadConjunction {
fn from(atom: Atom) -> Self {
Self(vec![atom])
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(try_from = "Vec<HeadConjunction>", into = "Vec<HeadConjunction>")]
pub struct HeadDisjunction(
Vec<HeadConjunction>,
);
impl HeadDisjunction {
pub fn try_new(disjuncts: Vec<HeadConjunction>) -> Result<Self, EmptyHeadCollection> {
if disjuncts.is_empty() {
return Err(EmptyHeadCollection::new(HeadCollectionKind::Disjunction));
}
Ok(Self(disjuncts))
}
#[must_use]
pub fn disjuncts(&self) -> &[HeadConjunction] {
&self.0
}
pub fn disjuncts_mut(&mut self) -> &mut [HeadConjunction] {
&mut self.0
}
}
impl TryFrom<Vec<HeadConjunction>> for HeadDisjunction {
type Error = EmptyHeadCollection;
fn try_from(disjuncts: Vec<HeadConjunction>) -> Result<Self, Self::Error> {
Self::try_new(disjuncts)
}
}
impl From<HeadDisjunction> for Vec<HeadConjunction> {
fn from(disjunction: HeadDisjunction) -> Self {
disjunction.0
}
}
#[derive(Clone, Debug, Eq, Hash, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(try_from = "Vec<VariableSymbol>", into = "Vec<VariableSymbol>")]
pub struct ExistentialVariables(
Vec<VariableSymbol>,
);
impl ExistentialVariables {
pub fn try_new(
variables: impl IntoIterator<Item = VariableSymbol>,
) -> Result<Self, EmptyHeadCollection> {
let set: BTreeSet<VariableSymbol> = variables.into_iter().collect();
if set.is_empty() {
return Err(EmptyHeadCollection::new(
HeadCollectionKind::ExistentialVariables,
));
}
Ok(Self(set.into_iter().collect()))
}
#[must_use]
pub fn variables(&self) -> &[VariableSymbol] {
&self.0
}
#[must_use]
pub fn contains(&self, variable: &VariableSymbol) -> bool {
self.0.binary_search(variable).is_ok()
}
}
impl TryFrom<Vec<VariableSymbol>> for ExistentialVariables {
type Error = EmptyHeadCollection;
fn try_from(variables: Vec<VariableSymbol>) -> Result<Self, Self::Error> {
Self::try_new(variables)
}
}
impl From<ExistentialVariables> for Vec<VariableSymbol> {
fn from(variables: ExistentialVariables) -> Self {
variables.0
}
}