#[cfg(any(test, feature = "parse", feature = "serialize"))]
use crate::Error;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct TermId(u32);
impl TermId {
pub const fn new(index: u32) -> Self {
Self(index)
}
pub const fn get(self) -> u32 {
self.0
}
pub const fn index(self) -> usize {
self.0 as usize
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Atom(u32);
impl Atom {
pub const fn new(index: u32) -> Self {
Self(index)
}
pub const fn get(self) -> u32 {
self.0
}
pub const fn index(self) -> usize {
self.0 as usize
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Term {
Atom(Atom),
DualAtom(Atom),
One,
Bot,
Top,
Zero,
Tensor(TermId, TermId),
Par(TermId, TermId),
With(TermId, TermId),
Plus(TermId, TermId),
Bang(TermId),
Quest(TermId),
Forall(TermId),
Exists(TermId),
}
const _: () = assert!(size_of::<Term>() == 12);
impl std::hash::Hash for Term {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
use Term::*;
let (a, b) = match *self {
Atom(a) | DualAtom(a) => (a.0, 0),
One | Bot | Top | Zero => (0, 0),
Tensor(k, l) | Par(k, l) | With(k, l) | Plus(k, l) => (k.0, l.0),
Bang(k) | Quest(k) | Forall(k) | Exists(k) => (k.0, 0),
};
let kind = u64::from(self.kind() as u8) << 56;
state.write_u64(u64::from(a) | (u64::from(b) << 32) ^ kind);
}
}
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Kind {
Atom,
DualAtom,
One,
Bot,
Top,
Zero,
Tensor,
Par,
With,
Plus,
Bang,
Quest,
Forall,
Exists,
}
impl Kind {
#[must_use]
pub const fn dual(self) -> Self {
use Kind::*;
match self {
Atom => DualAtom,
DualAtom => Atom,
One => Bot,
Bot => One,
Top => Zero,
Zero => Top,
Tensor => Par,
Par => Tensor,
With => Plus,
Plus => With,
Bang => Quest,
Quest => Bang,
Forall => Exists,
Exists => Forall,
}
}
pub const fn arity(self) -> u8 {
use Kind::*;
match self {
Atom | DualAtom | One | Bot | Top | Zero => 0,
Bang | Quest | Forall | Exists => 1,
Tensor | Par | With | Plus => 2,
}
}
pub const fn is_literal(self) -> bool {
matches!(self, Kind::Atom | Kind::DualAtom)
}
pub const fn is_binder(self) -> bool {
matches!(self, Kind::Forall | Kind::Exists)
}
}
impl Term {
pub const fn kind(self) -> Kind {
use Term::*;
match self {
Atom(_) => Kind::Atom,
DualAtom(_) => Kind::DualAtom,
One => Kind::One,
Bot => Kind::Bot,
Top => Kind::Top,
Zero => Kind::Zero,
Tensor(..) => Kind::Tensor,
Par(..) => Kind::Par,
With(..) => Kind::With,
Plus(..) => Kind::Plus,
Bang(_) => Kind::Bang,
Quest(_) => Kind::Quest,
Forall(_) => Kind::Forall,
Exists(_) => Kind::Exists,
}
}
pub const fn atom(self) -> Option<Atom> {
match self {
Term::Atom(a) | Term::DualAtom(a) => Some(a),
Term::One
| Term::Bot
| Term::Top
| Term::Zero
| Term::Tensor(..)
| Term::Par(..)
| Term::With(..)
| Term::Plus(..)
| Term::Bang(_)
| Term::Quest(_)
| Term::Forall(_)
| Term::Exists(_) => None,
}
}
#[must_use]
pub const fn dual(self) -> Self {
use Term::*;
match self {
Atom(a) => DualAtom(a),
DualAtom(a) => Atom(a),
One => Bot,
Bot => One,
Top => Zero,
Zero => Top,
Tensor(k, l) => Par(k, l),
Par(k, l) => Tensor(k, l),
With(k, l) => Plus(k, l),
Plus(k, l) => With(k, l),
Bang(k) => Quest(k),
Quest(k) => Bang(k),
Forall(k) => Exists(k),
Exists(k) => Forall(k),
}
}
pub fn subterms(self) -> impl Iterator<Item = TermId> {
let (first, second) = self.operands();
first.into_iter().chain(second)
}
pub(crate) const fn operands(self) -> (Option<TermId>, Option<TermId>) {
use Term::*;
match self {
Tensor(k, l) | Par(k, l) | With(k, l) | Plus(k, l) => (Some(k), Some(l)),
Bang(k) | Quest(k) | Forall(k) | Exists(k) => (Some(k), None),
Atom(_) | DualAtom(_) | One | Bot | Top | Zero => (None, None),
}
}
pub(crate) fn map_subterms(self, mut f: impl FnMut(TermId) -> TermId) -> Self {
use Term::*;
match self {
Tensor(k, l) => Tensor(f(k), f(l)),
Par(k, l) => Par(f(k), f(l)),
With(k, l) => With(f(k), f(l)),
Plus(k, l) => Plus(f(k), f(l)),
Bang(k) => Bang(f(k)),
Quest(k) => Quest(f(k)),
Forall(k) => Forall(f(k)),
Exists(k) => Exists(f(k)),
leaf @ Atom(_)
| leaf @ DualAtom(_)
| leaf @ One
| leaf @ Bot
| leaf @ Top
| leaf @ Zero => leaf,
}
}
pub(crate) fn offset(self, atoms: u32, terms: u32) -> Self {
use Term::*;
match self {
Atom(a) => Atom(self::Atom(a.0 + atoms)),
DualAtom(a) => DualAtom(self::Atom(a.0 + atoms)),
other @ One
| other @ Bot
| other @ Top
| other @ Zero
| other @ Tensor(..)
| other @ Par(..)
| other @ With(..)
| other @ Plus(..)
| other @ Bang(_)
| other @ Quest(_)
| other @ Forall(_)
| other @ Exists(_) => other.map_subterms(|k| TermId(k.0 + terms)),
}
}
#[cfg(any(test, feature = "parse", feature = "serialize"))]
pub(crate) fn check_bounds(self, atom_bound: u32, term_bound: u32) -> Result<(), Error> {
if let Some(a) = self.atom()
&& a.0 >= atom_bound
{
return Err(Error::IndexOutOfBounds {
space: crate::limits::Space::Atom,
index: a.index(),
len: atom_bound as usize,
});
}
for k in self.subterms() {
if k.0 >= term_bound {
return Err(Error::NotTopological {
space: crate::limits::Space::Term,
index: k.index(),
parent: term_bound as usize,
});
}
}
Ok(())
}
}