use crate::Error;
use crate::linear::occurrences::Side;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::str::FromStr;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Rule {
Ax,
Tensor,
Par,
One,
Bot,
With,
PlusLeft,
PlusRight,
Top,
Promotion,
Dereliction,
Contraction,
Weakening,
Mix,
AffineWeakening,
Cut,
Forall,
Exists,
Open,
}
impl Rule {
pub const ALL: [Self; 19] = {
use Rule::*;
[
Ax,
Tensor,
Par,
One,
Bot,
With,
PlusLeft,
PlusRight,
Top,
Promotion,
Dereliction,
Contraction,
Weakening,
Mix,
AffineWeakening,
Cut,
Forall,
Exists,
Open,
]
};
pub const fn name(self) -> &'static str {
use Rule::*;
match self {
Ax => "ax",
Tensor => "⊗",
Par => "⅋",
One => "1",
Bot => "⊥",
With => "&",
PlusLeft => "⊕₁",
PlusRight => "⊕₂",
Top => "⊤",
Promotion => "!",
Dereliction => "?d",
Contraction => "?c",
Weakening => "?w",
Mix => "mix",
AffineWeakening => "wk",
Cut => "cut",
Forall => "∀",
Exists => "∃",
Open => "open",
}
}
pub const fn is_structural(self) -> bool {
matches!(
self,
Self::Contraction | Self::Weakening | Self::AffineWeakening
)
}
pub const fn premises(self) -> u8 {
use Rule::*;
match self {
Ax | One | Top | Open => 0,
Tensor | With | Mix | Cut => 2,
Par | Bot | PlusLeft | PlusRight | Promotion | Dereliction | Contraction
| Weakening | AffineWeakening | Forall | Exists => 1,
}
}
pub const fn has_principal(self) -> bool {
!matches!(self, Self::Ax | Self::Mix | Self::Cut | Self::Open)
}
pub const fn on(self, side: Side) -> Named {
Named::new(self, Some(side))
}
}
impl Display for Rule {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.write_str(self.name())
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Named {
pub rule: Rule,
pub side: Option<Side>,
pub division: Option<Division>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Division {
Under,
Over,
}
impl Named {
pub const ALL: [Self; 41] = {
use Rule::*;
use Side::{Input, Output};
[
one_sided(Ax),
one_sided(Tensor),
one_sided(Par),
one_sided(One),
one_sided(Bot),
one_sided(With),
one_sided(PlusLeft),
one_sided(PlusRight),
one_sided(Top),
one_sided(Promotion),
one_sided(Dereliction),
one_sided(Contraction),
one_sided(Weakening),
one_sided(Mix),
one_sided(AffineWeakening),
one_sided(Cut),
one_sided(Forall),
one_sided(Exists),
two_sided(Tensor, Input),
two_sided(Par, Output),
two_sided(Par, Input),
two_sided(Tensor, Output),
two_sided(PlusLeft, Input),
two_sided(PlusRight, Input),
two_sided(With, Output),
two_sided(With, Input),
two_sided(PlusLeft, Output),
two_sided(PlusRight, Output),
two_sided(Bot, Input),
two_sided(One, Output),
two_sided(Top, Input),
two_sided(Top, Output),
two_sided(Dereliction, Input),
two_sided(Promotion, Output),
two_sided(Contraction, Input),
two_sided(Weakening, Input),
two_sided(Forall, Output),
two_sided(Forall, Input),
two_sided(Exists, Output),
two_sided(Exists, Input),
one_sided(Open),
]
};
pub const fn new(rule: Rule, side: Option<Side>) -> Self {
use Rule::*;
use Side::{Input, Output};
let side = match (rule, side) {
(Ax | Mix | AffineWeakening | Cut | Open, _) | (_, None) => None,
(Bot | Dereliction | Contraction | Weakening, Some(_)) => Some(Input),
(One | Promotion, Some(_)) => Some(Output),
(_, side) => side,
};
Self {
rule,
side,
division: None,
}
}
pub(crate) const DIVISIONS: [Self; 4] = [
divided(Rule::Tensor, Side::Input, Division::Under),
divided(Rule::Par, Side::Output, Division::Under),
divided(Rule::Tensor, Side::Input, Division::Over),
divided(Rule::Par, Side::Output, Division::Over),
];
pub(crate) fn applied(
rule: Rule,
reading: Option<&crate::linear::Reading<'_>>,
o: crate::linear::OccId,
) -> Self {
use crate::linear::occurrences::IllConnective as C;
let named = Self::new(rule, reading.map(|reading| reading.position(o)));
let division = match reading.map(|reading| reading.connective(o)) {
Some(C::Under) => Division::Under,
Some(C::Over) => Division::Over,
_ => return named,
};
match (named.rule, named.side) {
(Rule::Tensor, Some(Side::Input)) | (Rule::Par, Some(Side::Output)) => Self {
division: Some(division),
..named
},
_ => named,
}
}
pub(crate) const fn at(index: usize) -> Self {
if index < Self::ALL.len() {
Self::ALL[index]
} else {
Self::DIVISIONS[index - Self::ALL.len()]
}
}
pub const fn name(self) -> &'static str {
use Rule::*;
use Side::{Input, Output};
match (self.rule, self.side, self.division) {
(Tensor, Some(Input), Some(Division::Under)) => return "\\L",
(Par, Some(Output), Some(Division::Under)) => return "\\R",
(Tensor, Some(Input), Some(Division::Over)) => return "/L",
(Par, Some(Output), Some(Division::Over)) => return "/R",
_ => {}
}
match (self.rule, self.side) {
(Tensor, Some(Input)) => "⊸L",
(Par, Some(Output)) => "⊸R",
(Par, Some(Input)) => "⊗L",
(Tensor, Some(Output)) => "⊗R",
(PlusLeft, Some(Input)) => "&L₁",
(PlusRight, Some(Input)) => "&L₂",
(With, Some(Output)) => "&R",
(With, Some(Input)) => "⊕L",
(PlusLeft, Some(Output)) => "⊕R₁",
(PlusRight, Some(Output)) => "⊕R₂",
(Bot, Some(_)) => "1L",
(One, Some(_)) => "1R",
(Top, Some(Input)) => "0L",
(Top, Some(Output)) => "⊤R",
(Dereliction, Some(_)) => "!L",
(Promotion, Some(_)) => "!R",
(Contraction, Some(_)) => "!c",
(Weakening, Some(_)) => "!w",
(Forall, Some(Output)) => "∀R",
(Forall, Some(Input)) => "∃L",
(Exists, Some(Output)) => "∃R",
(Exists, Some(Input)) => "∀L",
(rule, _) => rule.name(),
}
}
pub const fn is_structural(self) -> bool {
self.rule.is_structural()
}
pub(crate) const fn index(self) -> usize {
if let Some(division) = self.division {
let left = matches!(self.rule, Rule::Tensor);
return Self::ALL.len() + 2 * (division as usize) + if left { 0 } else { 1 };
}
if self.side.is_none() && !matches!(self.rule, Rule::Open) {
return self.rule as usize;
}
let mut i = 0;
while i < Self::ALL.len() {
let named = Self::ALL[i];
if named.rule as u8 == self.rule as u8 && same_side(named.side, self.side) {
return i;
}
i += 1;
}
self.rule as usize
}
}
const fn one_sided(rule: Rule) -> Named {
Named {
rule,
side: None,
division: None,
}
}
const fn two_sided(rule: Rule, side: Side) -> Named {
Named {
rule,
side: Some(side),
division: None,
}
}
const fn divided(rule: Rule, side: Side, division: Division) -> Named {
Named {
rule,
side: Some(side),
division: Some(division),
}
}
const fn same_side(a: Option<Side>, b: Option<Side>) -> bool {
match (a, b) {
(None, None) => true,
(Some(a), Some(b)) => a as u8 == b as u8,
_ => false,
}
}
impl From<Rule> for Named {
fn from(rule: Rule) -> Self {
one_sided(rule)
}
}
impl Display for Named {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.write_str(self.name())
}
}
impl FromStr for Named {
type Err = Error;
fn from_str(text: &str) -> Result<Self, Error> {
use Rule::*;
use Side::{Input, Output};
if let Some(&named) = Self::DIVISIONS.iter().find(|named| named.name() == text) {
return Ok(named);
}
let (rule, side) = match text {
"ax" => (Ax, None),
"⊗" | "*" => (Tensor, None),
"⅋" | "par" | "|" => (Par, None),
"1" => (One, None),
"⊥" | "bot" => (Bot, None),
"&" => (With, None),
"⊕₁" | "+1" => (PlusLeft, None),
"⊕₂" | "+2" => (PlusRight, None),
"⊤" | "top" => (Top, None),
"!" => (Promotion, None),
"?d" => (Dereliction, None),
"?c" => (Contraction, None),
"?w" => (Weakening, None),
"mix" => (Mix, None),
"wk" => (AffineWeakening, None),
"cut" => (Cut, None),
"∀" | "forall" => (Forall, None),
"∃" | "exists" => (Exists, None),
"⊸L" | "-oL" => (Tensor, Some(Input)),
"⊸R" | "-oR" => (Par, Some(Output)),
"⊗L" | "*L" => (Par, Some(Input)),
"⊗R" | "*R" => (Tensor, Some(Output)),
"&L₁" | "&L1" => (PlusLeft, Some(Input)),
"&L₂" | "&L2" => (PlusRight, Some(Input)),
"&R" => (With, Some(Output)),
"⊕L" | "+L" => (With, Some(Input)),
"⊕R₁" | "+R1" => (PlusLeft, Some(Output)),
"⊕R₂" | "+R2" => (PlusRight, Some(Output)),
"1L" => (Bot, Some(Input)),
"1R" => (One, Some(Output)),
"0L" => (Top, Some(Input)),
"⊤R" | "topR" => (Top, Some(Output)),
"!L" => (Dereliction, Some(Input)),
"!R" => (Promotion, Some(Output)),
"!c" => (Contraction, Some(Input)),
"!w" => (Weakening, Some(Input)),
"∀R" | "forallR" => (Forall, Some(Output)),
"∃L" | "existsL" => (Forall, Some(Input)),
"∃R" | "existsR" => (Exists, Some(Output)),
"∀L" | "forallL" => (Exists, Some(Input)),
"open" => (Open, None),
_ => {
return Err(Error::UnknownName {
what: "rule",
name: text.into(),
known: KNOWN,
});
}
};
Ok(Self::new(rule, side))
}
}
#[cfg(test)]
const NAMES: &[&str] = KNOWN.split_at(Named::ALL.len()).0;
const KNOWN: &[&str] = &[
"ax", "⊗", "⅋", "1", "⊥", "&", "⊕₁", "⊕₂", "⊤", "!", "?d", "?c", "?w", "mix", "wk", "cut", "∀",
"∃", "⊸L", "⊸R", "⊗L", "⊗R", "&L₁", "&L₂", "&R", "⊕L", "⊕R₁", "⊕R₂", "1L", "1R", "0L", "⊤R",
"!L", "!R", "!c", "!w", "∀R", "∃L", "∃R", "∀L", "open", "\\L", "\\R", "/L", "/R",
];
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn names_round_trip() {
for (i, named) in Named::ALL.into_iter().enumerate() {
assert_eq!(named.name(), NAMES[i]);
assert_eq!(named.name().parse::<Named>().unwrap(), named, "{named:?}");
assert_eq!(named.index(), i);
}
for rule in Rule::ALL {
for side in [None, Some(Side::Input), Some(Side::Output)] {
let named = Named::new(rule, side);
assert_eq!(Named::ALL[named.index()], named, "{named:?}");
}
}
for (i, named) in Named::DIVISIONS.into_iter().enumerate() {
let index = Named::ALL.len() + i;
assert_eq!(named.name(), KNOWN[index]);
assert_eq!(named.name().parse::<Named>().unwrap(), named, "{named:?}");
assert_eq!((named.index(), Named::at(index)), (index, named));
}
assert!("?x".parse::<Named>().is_err());
}
}