Skip to content

Commit c35c5ee

Browse files
committed
Revert "move Priority out from search graph and into the "value""
This reverts commit d05412f.
1 parent d05412f commit c35c5ee

File tree

4 files changed

+38
-66
lines changed

4 files changed

+38
-66
lines changed

chalk-recursive/src/lib.rs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use crate::search_graph::DepthFirstNumber;
2-
use chalk_ir::{
3-
interner::Interner, ClausePriority, Fallible, Goal, InEnvironment, NoSolution, UCanonical,
4-
};
2+
use chalk_ir::{Goal, InEnvironment, UCanonical};
53

64
pub type UCanonicalGoal<I> = UCanonical<InEnvironment<Goal<I>>>;
75

@@ -14,35 +12,8 @@ pub mod solve;
1412
mod stack;
1513

1614
pub use cache::Cache;
17-
use chalk_solve::{Guidance, Solution};
1815
pub use recursive::RecursiveSolver;
19-
#[derive(Clone, PartialEq, Eq, Debug)]
20-
pub struct PrioritizedSolution<I: Interner> {
21-
priority: ClausePriority,
22-
solution: Fallible<Solution<I>>,
23-
}
24-
25-
impl<I: Interner> PrioritizedSolution<I> {
26-
/// Create a new prioritized solution.
27-
pub(crate) fn new(solution: Fallible<Solution<I>>, priority: ClausePriority) -> Self {
28-
Self { priority, solution }
29-
}
30-
31-
/// Create a high priority solution.
32-
pub(crate) fn high(solution: Fallible<Solution<I>>) -> Self {
33-
Self::new(solution, ClausePriority::High)
34-
}
3516

36-
/// Returns a high-priority solution that represents an error (no solution)
37-
pub(crate) fn error() -> Self {
38-
Self::high(Err(NoSolution))
39-
}
40-
41-
/// Returns a high-priority solution that represents ambiguity with no guidance.
42-
pub(crate) fn ambiguity() -> Self {
43-
Self::high(Ok(Solution::Ambig(Guidance::Unknown)))
44-
}
45-
}
4617
/// The `minimums` struct is used while solving to track whether we encountered
4718
/// any cycles in the process.
4819
#[derive(Copy, Clone, Debug)]

chalk-recursive/src/recursive.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use crate::cache::Cache;
12
use crate::search_graph::DepthFirstNumber;
23
use crate::search_graph::SearchGraph;
34
use crate::solve::{SolveDatabase, SolveIteration};
45
use crate::stack::{Stack, StackDepth};
5-
use crate::{cache::Cache, PrioritizedSolution};
66
use crate::{Minimums, UCanonicalGoal};
77
use chalk_ir::{interner::Interner, NoSolution};
88
use chalk_ir::{Canonical, ConstrainedSubst, Goal, InEnvironment, UCanonical};
@@ -17,12 +17,12 @@ struct RecursiveContext<I: Interner> {
1717

1818
/// The "search graph" stores "in-progress results" that are still being
1919
/// solved.
20-
search_graph: SearchGraph<UCanonicalGoal<I>, PrioritizedSolution<I>>,
20+
search_graph: SearchGraph<UCanonicalGoal<I>, Fallible<Solution<I>>>,
2121

2222
/// The "cache" stores results for goals that we have completely solved.
2323
/// Things are added to the cache when we have completely processed their
2424
/// result.
25-
cache: Option<Cache<UCanonicalGoal<I>, PrioritizedSolution<I>>>,
25+
cache: Option<Cache<UCanonicalGoal<I>, Fallible<Solution<I>>>>,
2626

2727
/// The maximum size for goals.
2828
max_size: usize,
@@ -46,7 +46,7 @@ impl<I: Interner> RecursiveSolver<I> {
4646
pub fn new(
4747
overflow_depth: usize,
4848
max_size: usize,
49-
cache: Option<Cache<UCanonicalGoal<I>, PrioritizedSolution<I>>>,
49+
cache: Option<Cache<UCanonicalGoal<I>, Fallible<Solution<I>>>>,
5050
) -> Self {
5151
Self {
5252
ctx: Box::new(RecursiveContext::new(overflow_depth, max_size, cache)),
@@ -84,7 +84,7 @@ impl<I: Interner> RecursiveContext<I> {
8484
pub fn new(
8585
overflow_depth: usize,
8686
max_size: usize,
87-
cache: Option<Cache<UCanonicalGoal<I>, PrioritizedSolution<I>>>,
87+
cache: Option<Cache<UCanonicalGoal<I>, Fallible<Solution<I>>>>,
8888
) -> Self {
8989
RecursiveContext {
9090
stack: Stack::new(overflow_depth),
@@ -149,7 +149,7 @@ impl<'me, I: Interner> Solver<'me, I> {
149149
// so this function will eventually be constant and the loop terminates.
150150
loop {
151151
let minimums = &mut Minimums::new();
152-
let current_answer = self.solve_iteration(&canonical_goal, minimums);
152+
let (current_answer, current_prio) = self.solve_iteration(&canonical_goal, minimums);
153153

154154
debug!(
155155
"solve_new_subgoal: loop iteration result = {:?} with minimums {:?}",
@@ -160,22 +160,24 @@ impl<'me, I: Interner> Solver<'me, I> {
160160
// None of our subgoals depended on us directly.
161161
// We can return.
162162
self.context.search_graph[dfn].solution = current_answer;
163+
self.context.search_graph[dfn].solution_priority = current_prio;
163164
return *minimums;
164165
}
165166

166167
// Some of our subgoals depended on us. We need to re-run
167168
// with the current answer.
168-
if self.context.search_graph[dfn].solution.solution == current_answer.solution {
169+
if self.context.search_graph[dfn].solution == current_answer {
169170
// Reached a fixed point.
170171
return *minimums;
171172
}
172173

173-
let current_answer_is_ambig = match &current_answer.solution {
174+
let current_answer_is_ambig = match &current_answer {
174175
Ok(s) => s.is_ambig(),
175176
Err(_) => false,
176177
};
177178

178179
self.context.search_graph[dfn].solution = current_answer;
180+
self.context.search_graph[dfn].solution_priority = current_prio;
179181

180182
// Subtle: if our current answer is ambiguous, we can just stop, and
181183
// in fact we *must* -- otherwise, we sometimes fail to reach a
@@ -204,7 +206,7 @@ impl<'me, I: Interner> SolveDatabase<I> for Solver<'me, I> {
204206
if let Some(cache) = &self.context.cache {
205207
if let Some(value) = cache.get(&goal) {
206208
debug!("solve_reduced_goal: cache hit, value={:?}", value);
207-
return value.solution.clone();
209+
return value.clone();
208210
}
209211
}
210212

@@ -229,11 +231,12 @@ impl<'me, I: Interner> SolveDatabase<I> for Solver<'me, I> {
229231

230232
// Return the solution from the table.
231233
let previous_solution = self.context.search_graph[dfn].solution.clone();
234+
let previous_solution_priority = self.context.search_graph[dfn].solution_priority;
232235
info!(
233-
"solve_goal: cycle detected, previous solution {:?}",
234-
previous_solution,
236+
"solve_goal: cycle detected, previous solution {:?} with prio {:?}",
237+
previous_solution, previous_solution_priority
235238
);
236-
previous_solution.solution
239+
previous_solution
237240
} else {
238241
// Otherwise, push the goal onto the stack and create a table.
239242
// The initial result for this table depends on whether the goal is coinductive.
@@ -250,11 +253,10 @@ impl<'me, I: Interner> SolveDatabase<I> for Solver<'me, I> {
250253
} else {
251254
Err(NoSolution)
252255
};
253-
let dfn = self.context.search_graph.insert(
254-
&goal,
255-
depth,
256-
PrioritizedSolution::high(initial_solution),
257-
);
256+
let dfn = self
257+
.context
258+
.search_graph
259+
.insert(&goal, depth, initial_solution);
258260
let subgoal_minimums = self.solve_new_subgoal(goal, depth, dfn);
259261
self.context.search_graph[dfn].links = subgoal_minimums;
260262
self.context.search_graph[dfn].stack_depth = None;
@@ -263,6 +265,7 @@ impl<'me, I: Interner> SolveDatabase<I> for Solver<'me, I> {
263265

264266
// Read final result from table.
265267
let result = self.context.search_graph[dfn].solution.clone();
268+
let priority = self.context.search_graph[dfn].solution_priority;
266269

267270
// If processing this subgoal did not involve anything
268271
// outside of its subtree, then we can promote it to the
@@ -280,8 +283,8 @@ impl<'me, I: Interner> SolveDatabase<I> for Solver<'me, I> {
280283
}
281284
}
282285

283-
info!("solve_goal: solution = {:#?}", result);
284-
result.solution
286+
info!("solve_goal: solution = {:?} prio {:?}", result, priority);
287+
result
285288
}
286289
}
287290

chalk-recursive/src/search_graph.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::stack::StackDepth;
22
use crate::{Cache, Minimums};
3+
use chalk_ir::ClausePriority;
34
use rustc_hash::FxHashMap;
45
use std::fmt::Debug;
56
use std::hash::Hash;
@@ -29,6 +30,7 @@ pub(super) struct Node<K, V> {
2930
pub(crate) goal: K,
3031

3132
pub(crate) solution: V,
33+
pub(crate) solution_priority: ClausePriority,
3234

3335
/// This is `Some(X)` if we are actively exploring this node, or
3436
/// `None` otherwise.
@@ -76,6 +78,7 @@ where
7678
let node = Node {
7779
goal: goal.clone(),
7880
solution,
81+
solution_priority: ClausePriority::High,
7982
stack_depth: Some(stack_depth),
8083
links: Minimums { positive: dfn },
8184
};

chalk-recursive/src/solve.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::combine;
22
use super::fulfill::Fulfill;
3-
use crate::{Minimums, PrioritizedSolution, UCanonicalGoal};
3+
use crate::{Minimums, UCanonicalGoal};
44
use chalk_ir::could_match::CouldMatch;
55
use chalk_ir::fold::Fold;
66
use chalk_ir::interner::{HasInterner, Interner};
@@ -39,7 +39,7 @@ pub(super) trait SolveIteration<I: Interner>: SolveDatabase<I> {
3939
&mut self,
4040
canonical_goal: &UCanonicalGoal<I>,
4141
minimums: &mut Minimums,
42-
) -> PrioritizedSolution<I> {
42+
) -> (Fallible<Solution<I>>, ClausePriority) {
4343
let UCanonical {
4444
universes,
4545
canonical:
@@ -68,14 +68,14 @@ pub(super) trait SolveIteration<I: Interner>: SolveDatabase<I> {
6868
// or from the lowered program, which includes fallback
6969
// clauses. We try each approach in turn:
7070

71-
let solution = {
71+
let (prog_solution, prog_prio) = {
7272
debug_span!("prog_clauses");
7373

7474
self.solve_from_clauses(&canonical_goal, minimums)
7575
};
76-
debug!(?solution);
76+
debug!(?prog_solution);
7777

78-
solution
78+
(prog_solution, prog_prio)
7979
}
8080

8181
_ => {
@@ -107,11 +107,11 @@ trait SolveIterationHelpers<I: Interner>: SolveDatabase<I> {
107107
&mut self,
108108
canonical_goal: &UCanonicalGoal<I>,
109109
minimums: &mut Minimums,
110-
) -> PrioritizedSolution<I> {
110+
) -> (Fallible<Solution<I>>, ClausePriority) {
111111
let (infer, subst, goal) = self.new_inference_table(canonical_goal);
112112
match Fulfill::new_with_simplification(self, infer, subst, goal) {
113-
Ok(fulfill) => PrioritizedSolution::high(fulfill.solve(minimums)),
114-
Err(NoSolution) => PrioritizedSolution::error(),
113+
Ok(fulfill) => (fulfill.solve(minimums), ClausePriority::High),
114+
Err(e) => (Err(e), ClausePriority::High),
115115
}
116116
}
117117

@@ -122,7 +122,7 @@ trait SolveIterationHelpers<I: Interner>: SolveDatabase<I> {
122122
&mut self,
123123
canonical_goal: &UCanonical<InEnvironment<DomainGoal<I>>>,
124124
minimums: &mut Minimums,
125-
) -> PrioritizedSolution<I> {
125+
) -> (Fallible<Solution<I>>, ClausePriority) {
126126
let mut clauses = vec![];
127127

128128
let db = self.db();
@@ -137,7 +137,7 @@ trait SolveIterationHelpers<I: Interner>: SolveDatabase<I> {
137137
match program_clauses_that_could_match(db, canonical_goal) {
138138
Ok(goal_clauses) => clauses.extend(goal_clauses.into_iter().filter(could_match)),
139139
Err(Floundered) => {
140-
return PrioritizedSolution::ambiguity();
140+
return (Ok(Solution::Ambig(Guidance::Unknown)), ClausePriority::High);
141141
}
142142
}
143143

@@ -155,7 +155,7 @@ trait SolveIterationHelpers<I: Interner>: SolveDatabase<I> {
155155

156156
// If we have a completely ambiguous answer, it's not going to get better, so stop
157157
if cur_solution == Some((Solution::Ambig(Guidance::Unknown), ClausePriority::High)) {
158-
return PrioritizedSolution::ambiguity();
158+
return (Ok(Solution::Ambig(Guidance::Unknown)), ClausePriority::High);
159159
}
160160

161161
let ProgramClauseData(implication) = program_clause.data(self.interner());
@@ -184,12 +184,7 @@ trait SolveIterationHelpers<I: Interner>: SolveDatabase<I> {
184184
debug!("Error");
185185
}
186186
}
187-
188-
if let Some((s, p)) = cur_solution {
189-
PrioritizedSolution::new(Ok(s), p)
190-
} else {
191-
PrioritizedSolution::error()
192-
}
187+
cur_solution.map_or((Err(NoSolution), ClausePriority::High), |(s, p)| (Ok(s), p))
193188
}
194189

195190
fn new_inference_table<T: Fold<I, Result = T> + HasInterner<Interner = I> + Clone>(

0 commit comments

Comments
 (0)