-
Notifications
You must be signed in to change notification settings - Fork 175
/
forest.rs
166 lines (145 loc) · 5.48 KB
/
forest.rs
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use ir::*;
use solve::Solution;
use solve::slg::aggregate;
use solve::slg::{DepthFirstNumber, SimplifiedAnswer, TableIndex, UCanonicalGoal};
use solve::slg::on_demand::logic::RootSearchFail;
use solve::slg::on_demand::stack::{Stack, StackIndex};
use solve::slg::on_demand::tables::Tables;
use solve::slg::on_demand::table::{Answer, AnswerIndex};
use std::sync::Arc;
pub struct Forest {
crate program: Arc<ProgramEnvironment>,
crate tables: Tables,
crate stack: Stack,
crate max_size: usize,
dfn: DepthFirstNumber,
}
impl Forest {
crate fn new(program: &Arc<ProgramEnvironment>, max_size: usize) -> Self {
Forest {
program: program.clone(),
tables: Tables::default(),
stack: Stack::default(),
max_size,
dfn: DepthFirstNumber::MIN,
}
}
// Gets the next depth-first number. This number never decreases.
pub(super) fn next_dfn(&mut self) -> DepthFirstNumber {
self.dfn.next()
}
/// Finds the first N answers, looping as much as needed to get
/// them.
///
/// Thanks to subgoal abstraction and so forth, this should always
/// terminate.
pub(super) fn force_answers(
&mut self,
goal: UCanonicalGoal,
num_answers: usize,
) -> Vec<Answer> {
let table = self.get_or_create_table_for_ucanonical_goal(goal);
let mut answers = Vec::with_capacity(num_answers);
for i in 0..num_answers {
let i = AnswerIndex::from(i);
loop {
match self.ensure_root_answer(table, i) {
Ok(()) => break,
Err(RootSearchFail::QuantumExceeded) => continue,
Err(RootSearchFail::NoMoreSolutions) => return answers,
}
}
answers.push(self.answer(table, i).clone());
}
answers
}
/// Returns a "solver" for a given goal in the form of an
/// iterator. Each time you invoke `next`, it will do the work to
/// extract one more answer. These answers are cached in between
/// invocations. Invoking `next` fewer times is preferable =)
pub fn iter_answers<'f>(
&'f mut self,
goal: &UCanonicalGoal,
) -> impl Iterator<Item = SimplifiedAnswer> + 'f {
let table = self.get_or_create_table_for_ucanonical_goal(goal.clone());
let answer = AnswerIndex::ZERO;
ForestSolver { forest: self, table, answer }
}
/// Solves a given goal, producing the solution. This will do only
/// as much work towards `goal` as it has to (and that works is
/// cached for future attempts).
pub fn solve(
&mut self,
goal: &UCanonicalGoal,
) -> Option<Solution> {
aggregate::make_solution(&goal.canonical, self.iter_answers(goal))
}
/// True if all the tables on the stack starting from `depth` and
/// continuing until the top of the stack are coinductive.
///
/// Example: Given a program like:
///
/// ```
/// struct Foo { a: Option<Box<Bar>> }
/// struct Bar { a: Option<Box<Foo>> }
/// trait XXX { }
/// impl<T: Send> XXX for T { }
/// ```
///
/// and then a goal of `Foo: XXX`, we would eventually wind up
/// with a stack like this:
///
/// | StackIndex | Table Goal |
/// | ---------- | ----------- |
/// | 0 | `Foo: XXX` |
/// | 1 | `Foo: Send` |
/// | 2 | `Bar: Send` |
///
/// Here, the top of the stack is `Bar: Send`. And now we are
/// asking `top_of_stack_is_coinductive_from(1)` -- the answer
/// would be true, since `Send` is an auto trait, which yields a
/// coinductive goal. But `top_of_stack_is_coinductive_from(0)` is
/// false, since `XXX` is not an auto trait.
pub(super) fn top_of_stack_is_coinductive_from(&self, depth: StackIndex) -> bool {
self.stack.top_of_stack_from(depth).all(|d| {
let table = self.stack[d].table;
self.tables[table].coinductive_goal
})
}
}
struct ForestSolver<'forest> {
forest: &'forest mut Forest,
table: TableIndex,
answer: AnswerIndex,
}
impl<'forest> Iterator for ForestSolver<'forest> {
type Item = SimplifiedAnswer;
fn next(&mut self) -> Option<SimplifiedAnswer> {
loop {
match self.forest.ensure_root_answer(self.table, self.answer) {
Ok(()) => {
let answer = self.forest.answer(self.table, self.answer);
// FIXME -- if answer has delayed literals, we
// *should* try to simplify here (which might
// involve forcing `table` and its dependencies to
// completion. But instead we'll err on the side
// of ambiguity for now. This will sometimes lose
// us completeness around negative reasoning
// (we'll give ambig when we could have given a
// concrete yes/no answer).
let simplified_answer = SimplifiedAnswer {
subst: answer.subst.clone(),
ambiguous: !answer.delayed_literals.is_empty(),
};
self.answer.increment();
return Some(simplified_answer);
}
Err(RootSearchFail::NoMoreSolutions) => {
return None;
}
Err(RootSearchFail::QuantumExceeded) => {
}
}
}
}
}