1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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(']')
}
}