use std::{
fmt::{Display, Error, Formatter},
ops::Add,
};
use voile_util::meta::MI;
use crate::syntax::core::Elim;
#[derive(Debug, Clone)]
pub enum Stuck {
OnMeta(MI),
OnElim(Elim),
UnderApplied,
AbsurdMatch,
MissingClauses,
NotBlocked,
}
impl Default for Stuck {
fn default() -> Self {
Stuck::NotBlocked
}
}
impl Add for Stuck {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(Stuck::NotBlocked, b) => b,
(Stuck::OnMeta(m), _) | (_, Stuck::OnMeta(m)) => Stuck::OnMeta(m),
(Stuck::MissingClauses, _) | (_, Stuck::MissingClauses) => Stuck::MissingClauses,
(Stuck::OnElim(e), _) | (_, Stuck::OnElim(e)) => Stuck::OnElim(e),
(b, _) => b,
}
}
}
impl Display for Stuck {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
match self {
Stuck::OnMeta(MI(m)) => write!(f, "blocked by meta {:?}", m),
Stuck::OnElim(e) => write!(f, "blocked on argument `{}`", e),
Stuck::UnderApplied => write!(f, "missing necessary arguments"),
Stuck::AbsurdMatch => write!(f, "trying to instantiate an absurd match"),
Stuck::MissingClauses => write!(f, "cannot find a suitable clause"),
Stuck::NotBlocked => write!(f, "not blocked by anything, you may have found a bug"),
}
}
}
impl Stuck {
pub fn is_meta(&self) -> Option<MI> {
match self {
Stuck::OnMeta(m) => Some(*m),
_ => None,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct Blocked<T> {
pub stuck: Stuck,
pub anyway: T,
}
impl Add for Blocked<()> {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Blocked::new(self.stuck + rhs.stuck, ())
}
}
impl<T> Blocked<T> {
pub fn is_meta(&self) -> Option<MI> {
self.stuck.is_meta()
}
pub fn new(stuck: Stuck, anyway: T) -> Self {
Self { stuck, anyway }
}
pub fn map_anyway<R>(self, f: impl FnOnce(T) -> R) -> Blocked<R> {
Blocked::new(self.stuck, f(self.anyway))
}
}
impl<T: Display> Display for Blocked<T> {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(
f,
"I'm not sure if I should give `{}` because I'm {}.",
self.anyway, self.stuck
)
}
}