use super::build::unfold;
use crate::Error;
use crate::limits::{Limits, Progress};
use crate::linear::fragment::Mode;
use crate::linear::occurrences::instances::Framing;
use crate::linear::occurrences::{Forest, Member, OccId, Reading};
use crate::linear::proofs::check;
use crate::linear::proofs::{Named, NodeId, Proof};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize), serde(transparent))]
pub struct InfId(u32);
impl InfId {
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
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", serde(default, deny_unknown_fields))]
pub struct ViewOptions {
pub compact: Compact,
pub sides: Sides,
pub classical: ClassicalSides,
pub structural: Structural,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", serde(rename_all = "lowercase"))]
pub enum Structural {
Explicit,
#[default]
Admissible,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", serde(rename_all = "lowercase"))]
pub enum ClassicalSides {
#[default]
One,
Two,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", serde(rename_all = "lowercase"))]
pub enum Sides {
#[default]
Auto,
One,
Two,
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", serde(rename_all = "lowercase"))]
pub enum Compact {
#[default]
Auto,
Always,
Never,
}
impl ViewOptions {
pub const DEFAULT_CLASSICAL: ClassicalSides = ClassicalSides::One;
pub const DEFAULT_STRUCTURAL: Structural = Structural::Admissible;
#[must_use]
pub const fn with_structural(self, structural: Structural) -> Self {
Self { structural, ..self }
}
#[must_use]
pub const fn with_classical(self, classical: ClassicalSides) -> Self {
Self { classical, ..self }
}
#[must_use]
pub const fn with_compact(self, compact: Compact) -> Self {
Self { compact, ..self }
}
#[must_use]
pub const fn with_sides(self, sides: Sides) -> Self {
Self { sides, ..self }
}
pub(crate) fn two_sided(&self, proof: &Proof) -> bool {
match self.sides {
Sides::One => false,
Sides::Two => true,
Sides::Auto => proof.mode().is_some_and(Mode::is_intuitionistic),
}
}
}
#[non_exhaustive]
#[derive(Clone, Debug, Eq)]
pub struct Inference {
pub(crate) sequent: Vec<Member>,
pub(crate) rule: Named,
pub(crate) principal: Option<usize>,
pub(crate) premises: Vec<InfId>,
pub(crate) times: u32,
pub(crate) cut: Option<Member>,
pub(crate) node: Option<NodeId>,
}
impl PartialEq for Inference {
fn eq(&self, other: &Self) -> bool {
let Self {
sequent,
rule,
principal,
premises,
times,
cut,
..
} = self;
*sequent == other.sequent
&& *rule == other.rule
&& *principal == other.principal
&& *premises == other.premises
&& *times == other.times
&& *cut == other.cut
}
}
impl Inference {
pub fn sequent(&self) -> &[Member] {
&self.sequent
}
pub const fn rule(&self) -> Named {
self.rule
}
pub const fn principal(&self) -> Option<usize> {
self.principal
}
pub fn premises(&self) -> &[InfId] {
&self.premises
}
pub const fn times(&self) -> u32 {
self.times
}
pub const fn cut(&self) -> Option<Member> {
self.cut
}
pub const fn node(&self) -> Option<NodeId> {
self.node
}
}
#[derive(Clone, Debug)]
pub struct Derivation<'a> {
forest: &'a Forest,
reading: Option<Reading<'a>>,
inferences: Vec<Inference>,
of_goal: bool,
mode: Option<Mode>,
framing: Option<Box<Framing<'a>>>,
}
impl<'a> Derivation<'a> {
pub(crate) fn new(
proof: &'a Proof,
view: &ViewOptions,
limits: &Limits,
stop: impl FnMut(Progress) -> bool,
) -> Result<Self, Error> {
Self::build(proof, Self::ONE_SIDED, None, view, limits, stop)
}
pub const MOST: u64 = u32::MAX as u64;
pub(crate) const ONE_SIDED: Mode = Mode::CLASSICAL.with_affine().with_mix();
pub(crate) const TWO_SIDED: Mode = Mode::INTUITIONISTIC.with_affine();
pub(crate) fn two_sided_mode(proof: &Proof) -> Mode {
if proof.mode().is_some_and(Mode::is_ordered) {
Mode::LAMBEK.with_empty_antecedents()
} else {
Self::TWO_SIDED
}
}
pub(crate) fn two_sided(
proof: &'a Proof,
view: &ViewOptions,
limits: &Limits,
stop: impl FnMut(Progress) -> bool,
) -> Result<Self, Error> {
let mode = Self::two_sided_mode(proof);
let reading = check::reading(proof, mode)?;
Self::build(proof, mode, reading, view, limits, stop)
}
fn build(
proof: &'a Proof,
mode: Mode,
reading: Option<Reading<'a>>,
view: &ViewOptions,
limits: &Limits,
mut stop: impl FnMut(Progress) -> bool,
) -> Result<Self, Error> {
let conclusion = proof.conclusion();
let inferences = unfold(
proof,
&conclusion,
mode,
reading.as_ref(),
view,
limits,
&mut stop,
)?;
Ok(Self {
forest: proof.forest(),
reading,
inferences,
of_goal: proof.goal().is_some(),
mode: proof.mode(),
framing: proof
.instances()
.map(|table| Box::new(Framing::new(&proof.forest().sequent().fo, table))),
})
}
#[cfg(feature = "interactive")]
pub(crate) fn from_parts(
forest: &'a Forest,
reading: Option<Reading<'a>>,
inferences: Vec<Inference>,
mode: Mode,
) -> Self {
debug_assert_ne!(inferences, []);
Self {
forest,
reading,
inferences,
of_goal: false,
mode: Some(mode),
framing: None,
}
}
#[cfg(feature = "interactive")]
pub(crate) fn with_framing(self, table: &'a crate::linear::Instances) -> Self {
Self {
framing: Some(Box::new(Framing::new(&self.forest.sequent().fo, table))),
..self
}
}
pub(crate) const fn is_of_goal(&self) -> bool {
self.of_goal
}
pub(crate) const fn mode(&self) -> Option<Mode> {
self.mode
}
pub fn term(
&self,
limits: &Limits,
stop: impl FnMut(Progress) -> bool,
) -> Result<crate::lambda::Term, Error> {
crate::lambda::linear::read(self, limits, stop)
}
#[cfg(feature = "interactive")]
pub(crate) fn of_goal(
proof: &'a Proof,
goal: &[OccId],
mode: Mode,
view: &ViewOptions,
limits: &Limits,
stop: &mut dyn FnMut(Progress) -> bool,
) -> Result<Vec<Inference>, Error> {
let reading = check::reading(proof, mode)?;
let view = view.with_compact(Compact::Never);
let mut grafted = unfold(proof, goal, mode, reading.as_ref(), &view, limits, stop)?;
for inference in &mut grafted {
inference.node = None;
}
Ok(grafted)
}
pub fn forest(&self) -> &'a Forest {
self.forest
}
pub fn occurrence(&self, member: Member) -> OccId {
self.framing
.as_deref()
.map_or(member.occ(), |framing| framing.occurrence(member))
}
pub fn formula(&self, member: Member) -> impl std::fmt::Display + 'a {
if let Some(framing) = &self.framing
&& member.index() >= self.forest.len()
{
let mut text = String::new();
crate::linear::sequents::notation::TEXT
.instance(
&mut text,
self.forest,
self.reading.as_ref(),
framing,
member,
None,
)
.expect("writing into a string cannot fail");
return text;
}
match &self.reading {
Some(reading) => reading.formula(member.occ()).to_string(),
None => self.forest.formula(member.occ()).to_string(),
}
}
pub(crate) fn framing(&self) -> Option<&Framing<'a>> {
self.framing.as_deref()
}
pub fn reading(&self) -> Option<&Reading<'a>> {
self.reading.as_ref()
}
pub fn inferences(&self) -> &[Inference] {
&self.inferences
}
pub fn inference(&self, id: InfId) -> &Inference {
&self.inferences[id.index()]
}
pub fn root(&self) -> InfId {
InfId::new(self.inferences.len() as u32 - 1)
}
}