Skip to content

Commit 835fa68

Browse files
committed
Make the prioritization code slightly nicer
1 parent 60b3892 commit 835fa68

File tree

1 file changed

+52
-66
lines changed
  • chalk-solve/src/recursive

1 file changed

+52
-66
lines changed

chalk-solve/src/recursive/mod.rs

Lines changed: 52 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -364,34 +364,7 @@ impl<'me, I: Interner> Solver<'me, I> {
364364
where
365365
C: IntoIterator<Item = ProgramClause<I>>,
366366
{
367-
// TODO we don't actually need to keep track of the inputs
368367
let mut cur_solution = None;
369-
fn combine_with_priorities<I: Interner>(
370-
a: Solution<I>,
371-
prio_a: ClausePriority,
372-
inputs_a: Vec<Parameter<I>>,
373-
b: Solution<I>,
374-
prio_b: ClausePriority,
375-
inputs_b: Vec<Parameter<I>>,
376-
) -> (Solution<I>, ClausePriority, Vec<Parameter<I>>) {
377-
match (prio_a, prio_b) {
378-
(ClausePriority::High, ClausePriority::Low) if inputs_a == inputs_b => {
379-
debug!(
380-
"preferring solution: {:?} over {:?} because of higher prio",
381-
a, b
382-
);
383-
(a, ClausePriority::High, inputs_a)
384-
}
385-
(ClausePriority::Low, ClausePriority::High) if inputs_a == inputs_b => {
386-
debug!(
387-
"preferring solution: {:?} over {:?} because of higher prio",
388-
b, a
389-
);
390-
(b, ClausePriority::High, inputs_b)
391-
}
392-
_ => (a.combine(b), prio_a, inputs_a),
393-
}
394-
}
395368
for program_clause in clauses {
396369
debug_heading!("clause={:?}", program_clause);
397370

@@ -407,29 +380,15 @@ impl<'me, I: Interner> Solver<'me, I> {
407380
);
408381
if let (Ok(solution), priority) = res {
409382
debug!("ok: solution={:?} prio={:?}", solution, priority);
410-
let inputs = if let Some(subst) = solution.constrained_subst() {
411-
let subst_goal = subst.value.subst.apply(
412-
&canonical_goal.canonical.value.goal,
413-
self.program.interner(),
414-
);
415-
debug!("subst_goal = {:?}", subst_goal);
416-
subst_goal.inputs(self.program.interner())
417-
} else {
418-
canonical_goal
419-
.canonical
420-
.value
421-
.goal
422-
.inputs(self.program.interner())
423-
};
424383
cur_solution = Some(match cur_solution {
425-
None => (solution, priority, inputs),
426-
Some((cur, cur_priority, cur_inputs)) => combine_with_priorities(
384+
None => (solution, priority),
385+
Some((cur, cur_priority)) => combine_with_priorities(
386+
self.program.interner(),
387+
canonical_goal,
427388
cur,
428389
cur_priority,
429-
cur_inputs,
430390
solution,
431391
priority,
432-
inputs,
433392
),
434393
});
435394
} else {
@@ -440,29 +399,15 @@ impl<'me, I: Interner> Solver<'me, I> {
440399
let res = self.solve_via_implication(canonical_goal, implication, minimums);
441400
if let (Ok(solution), priority) = res {
442401
debug!("ok: solution={:?} prio={:?}", solution, priority);
443-
let inputs = if let Some(subst) = solution.constrained_subst() {
444-
let subst_goal = subst.value.subst.apply(
445-
&canonical_goal.canonical.value.goal,
446-
self.program.interner(),
447-
);
448-
debug!("subst_goal = {:?}", subst_goal);
449-
subst_goal.inputs(self.program.interner())
450-
} else {
451-
canonical_goal
452-
.canonical
453-
.value
454-
.goal
455-
.inputs(self.program.interner())
456-
};
457402
cur_solution = Some(match cur_solution {
458-
None => (solution, priority, inputs),
459-
Some((cur, cur_priority, cur_inputs)) => combine_with_priorities(
403+
None => (solution, priority),
404+
Some((cur, cur_priority)) => combine_with_priorities(
405+
self.program.interner(),
406+
canonical_goal,
460407
cur,
461408
cur_priority,
462-
cur_inputs,
463409
solution,
464410
priority,
465-
inputs,
466411
),
467412
});
468413
} else {
@@ -471,9 +416,7 @@ impl<'me, I: Interner> Solver<'me, I> {
471416
}
472417
}
473418
}
474-
cur_solution.map_or((Err(NoSolution), ClausePriority::High), |(s, p, _)| {
475-
(Ok(s), p)
476-
})
419+
cur_solution.map_or((Err(NoSolution), ClausePriority::High), |(s, p)| (Ok(s), p))
477420
}
478421

479422
/// Modus ponens! That is: try to apply an implication by proving its premises.
@@ -524,6 +467,49 @@ impl<'me, I: Interner> Solver<'me, I> {
524467
}
525468
}
526469

470+
fn calculate_inputs<I: Interner>(
471+
interner: &I,
472+
canonical_goal: &UCanonical<InEnvironment<DomainGoal<I>>>,
473+
solution: &Solution<I>,
474+
) -> Vec<Parameter<I>> {
475+
if let Some(subst) = solution.constrained_subst() {
476+
let subst_goal = subst
477+
.value
478+
.subst
479+
.apply(&canonical_goal.canonical.value.goal, interner);
480+
subst_goal.inputs(interner)
481+
} else {
482+
canonical_goal.canonical.value.goal.inputs(interner)
483+
}
484+
}
485+
486+
fn combine_with_priorities<I: Interner>(
487+
interner: &I,
488+
canonical_goal: &UCanonical<InEnvironment<DomainGoal<I>>>,
489+
a: Solution<I>,
490+
prio_a: ClausePriority,
491+
b: Solution<I>,
492+
prio_b: ClausePriority,
493+
) -> (Solution<I>, ClausePriority) {
494+
match (prio_a, prio_b, a, b) {
495+
(ClausePriority::High, ClausePriority::Low, higher, lower)
496+
| (ClausePriority::Low, ClausePriority::High, lower, higher) => {
497+
let inputs_higher = calculate_inputs(interner, canonical_goal, &higher);
498+
let inputs_lower = calculate_inputs(interner, canonical_goal, &lower);
499+
if inputs_higher == inputs_lower {
500+
debug!(
501+
"preferring solution: {:?} over {:?} because of higher prio",
502+
higher, lower
503+
);
504+
(higher, ClausePriority::High)
505+
} else {
506+
(higher.combine(lower), ClausePriority::High)
507+
}
508+
}
509+
(_, _, a, b) => (a.combine(b), prio_a),
510+
}
511+
}
512+
527513
impl Minimums {
528514
fn new() -> Self {
529515
Minimums {

0 commit comments

Comments
 (0)