use std::{
fmt::{Debug, Display, Error, Formatter, Write},
rc::Rc,
};
use voile_util::{meta::MI, uid::DBI};
use crate::syntax::core::subst::{RedEx, Subst};
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum MetaSol<Val> {
Solved(DBI, Box<Val>),
Unsolved,
}
impl<Val> Default for MetaSol<Val> {
fn default() -> Self {
MetaSol::Unsolved
}
}
impl<R, T: RedEx<R>> RedEx<MetaSol<R>> for MetaSol<T> {
fn reduce_dbi(self, subst: Rc<Subst>) -> MetaSol<R> {
use MetaSol::*;
match self {
Solved(i, t) => MetaSol::solved(i, t.reduce_dbi(subst)),
Unsolved => Unsolved,
}
}
}
#[derive(Clone, Debug)]
pub struct MetaContext<Val>(Vec<MetaSol<Val>>);
impl<Val> Default for MetaContext<Val> {
fn default() -> Self {
MetaContext(Vec::new())
}
}
impl<Val> MetaSol<Val> {
pub fn solved(at: DBI, val: Val) -> Self {
MetaSol::Solved(at, Box::new(val))
}
}
impl<Val> MetaContext<Val> {
pub fn solutions(&self) -> &Vec<MetaSol<Val>> {
&self.0
}
pub fn solution(&self, index: MI) -> &MetaSol<Val> {
&self.solutions()[index.0]
}
pub fn mut_solutions(&mut self) -> &mut Vec<MetaSol<Val>> {
&mut self.0
}
pub fn expand_with_fresh_meta(&mut self, meta_count: MI) {
debug_assert!(self.solutions().len() <= meta_count.0);
self.mut_solutions()
.resize_with(meta_count.0, Default::default);
}
pub fn fresh_meta(&mut self, new_meta: impl FnOnce(MI) -> Val) -> Val {
let meta = new_meta(MI(self.solutions().len()));
self.mut_solutions().push(MetaSol::Unsolved);
meta
}
}
impl<Val: Debug + Eq> MetaContext<Val> {
pub fn solve_meta(&mut self, meta_index: MI, at: DBI, solution: Val) {
let meta_solution = &mut self.mut_solutions()[meta_index.0];
debug_assert_eq!(meta_solution, &mut MetaSol::Unsolved);
*meta_solution = MetaSol::solved(at, solution);
}
}
impl<Val: Display> Display for MetaContext<Val> {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
use MetaSol::*;
f.write_char('[')?;
let solutions = self.solutions();
let mut iter = solutions.iter().enumerate();
if let Some((ix, sol)) = iter.next() {
write!(f, "?{:?}", ix)?;
if let Solved(DBI(i), sol) = sol {
write!(f, "={}({:?})", sol, i)?;
}
}
for (ix, sol) in iter {
write!(f, ", ?{:?}", ix)?;
match sol {
Solved(DBI(i), sol) => write!(f, "={}({:?})", sol, i)?,
Unsolved => f.write_char(',')?,
}
}
f.write_char(']')
}
}