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
use crate::fixed_point::{Cache, Minimums, RecursiveContext, SolverStuff};
use crate::solve::{SolveDatabase, SolveIteration};
use crate::UCanonicalGoal;
use chalk_ir::{interner::Interner, NoSolution};
use chalk_ir::{Canonical, ConstrainedSubst, Goal, InEnvironment, UCanonical};
use chalk_ir::{Constraints, Fallible};
use chalk_solve::{coinductive_goal::IsCoinductive, RustIrDatabase, Solution};
use std::fmt;
struct Solver<'me, I: Interner> {
program: &'me dyn RustIrDatabase<I>,
context: &'me mut RecursiveContext<UCanonicalGoal<I>, Fallible<Solution<I>>>,
}
pub struct RecursiveSolver<I: Interner> {
ctx: Box<RecursiveContext<UCanonicalGoal<I>, Fallible<Solution<I>>>>,
}
impl<I: Interner> RecursiveSolver<I> {
pub fn new(
overflow_depth: usize,
max_size: usize,
cache: Option<Cache<UCanonicalGoal<I>, Fallible<Solution<I>>>>,
) -> Self {
Self {
ctx: Box::new(RecursiveContext::new(overflow_depth, max_size, cache)),
}
}
}
impl<I: Interner> fmt::Debug for RecursiveSolver<I> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "RecursiveSolver")
}
}
impl<'me, I: Interner> Solver<'me, I> {
pub(crate) fn new(
context: &'me mut RecursiveContext<UCanonicalGoal<I>, Fallible<Solution<I>>>,
program: &'me dyn RustIrDatabase<I>,
) -> Self {
Self { program, context }
}
}
impl<I: Interner> SolverStuff<UCanonicalGoal<I>, Fallible<Solution<I>>> for &dyn RustIrDatabase<I> {
fn is_coinductive_goal(self, goal: &UCanonicalGoal<I>) -> bool {
goal.is_coinductive(self)
}
fn initial_value(
self,
goal: &UCanonicalGoal<I>,
coinductive_goal: bool,
) -> Fallible<Solution<I>> {
if coinductive_goal {
Ok(Solution::Unique(Canonical {
value: ConstrainedSubst {
subst: goal.trivial_substitution(self.interner()),
constraints: Constraints::empty(self.interner()),
},
binders: goal.canonical.binders.clone(),
}))
} else {
Err(NoSolution)
}
}
fn solve_iteration(
self,
context: &mut RecursiveContext<UCanonicalGoal<I>, Fallible<Solution<I>>>,
goal: &UCanonicalGoal<I>,
minimums: &mut Minimums,
) -> Fallible<Solution<I>> {
Solver::new(context, self).solve_iteration(goal, minimums)
}
fn reached_fixed_point(
self,
old_answer: &Fallible<Solution<I>>,
current_answer: &Fallible<Solution<I>>,
) -> bool {
old_answer == current_answer || {
match ¤t_answer {
Ok(s) => s.is_ambig(),
Err(_) => false,
}
}
}
fn error_value(self) -> Fallible<Solution<I>> {
Err(NoSolution)
}
}
impl<'me, I: Interner> SolveDatabase<I> for Solver<'me, I> {
fn solve_goal(
&mut self,
goal: UCanonicalGoal<I>,
minimums: &mut Minimums,
) -> Fallible<Solution<I>> {
self.context.solve_goal(&goal, minimums, self.program)
}
fn interner(&self) -> I {
self.program.interner()
}
fn db(&self) -> &dyn RustIrDatabase<I> {
self.program
}
fn max_size(&self) -> usize {
self.context.max_size()
}
}
impl<I: Interner> chalk_solve::Solver<I> for RecursiveSolver<I> {
fn solve(
&mut self,
program: &dyn RustIrDatabase<I>,
goal: &UCanonical<InEnvironment<Goal<I>>>,
) -> Option<chalk_solve::Solution<I>> {
self.ctx.solve_root_goal(goal, program).ok()
}
fn solve_limited(
&mut self,
program: &dyn RustIrDatabase<I>,
goal: &UCanonical<InEnvironment<Goal<I>>>,
_should_continue: &dyn std::ops::Fn() -> bool,
) -> Option<chalk_solve::Solution<I>> {
self.ctx.solve_root_goal(goal, program).ok()
}
fn solve_multiple(
&mut self,
_program: &dyn RustIrDatabase<I>,
_goal: &UCanonical<InEnvironment<Goal<I>>>,
_f: &mut dyn FnMut(
chalk_solve::SubstitutionResult<Canonical<ConstrainedSubst<I>>>,
bool,
) -> bool,
) -> bool {
unimplemented!("Recursive solver doesn't support multiple answers")
}
}