@@ -3,11 +3,10 @@ use crate::logic::RootSearchFail;
33use crate :: table:: AnswerIndex ;
44use crate :: tables:: Tables ;
55use crate :: { TableIndex , TimeStamp } ;
6- use std:: fmt:: Display ;
76
87use chalk_ir:: debug;
98use chalk_ir:: interner:: Interner ;
10- use chalk_ir:: { Canonical , ConstrainedSubst , Goal , InEnvironment , Substitution , UCanonical } ;
9+ use chalk_ir:: { Goal , InEnvironment , Substitution , UCanonical } ;
1110
1211pub struct Forest < I : Interner , C : Context < I > > {
1312 pub ( crate ) tables : Tables < I > ,
@@ -39,7 +38,7 @@ impl<I: Interner, C: Context<I>> Forest<I, C> {
3938 /// iterator. Each time you invoke `next`, it will do the work to
4039 /// extract one more answer. These answers are cached in between
4140 /// invocations. Invoking `next` fewer times is preferable =)
42- fn iter_answers < ' f > (
41+ pub fn iter_answers < ' f > (
4342 & ' f mut self ,
4443 context : & ' f impl ContextOps < I , C > ,
4544 goal : & UCanonical < InEnvironment < Goal < I > > > ,
@@ -54,89 +53,6 @@ impl<I: Interner, C: Context<I>> Forest<I, C> {
5453 _context : std:: marker:: PhantomData :: < C > ,
5554 }
5655 }
57-
58- /// Solves a given goal, producing the solution. This will do only
59- /// as much work towards `goal` as it has to (and that works is
60- /// cached for future attempts).
61- pub fn solve (
62- & mut self ,
63- context : & impl ContextOps < I , C > ,
64- goal : & UCanonical < InEnvironment < Goal < I > > > ,
65- should_continue : impl Fn ( ) -> bool ,
66- ) -> Option < C :: Solution > {
67- context. make_solution ( & goal, self . iter_answers ( context, goal) , should_continue)
68- }
69-
70- /// Solves a given goal, producing the solution. This will do only
71- /// as much work towards `goal` as it has to (and that works is
72- /// cached for future attempts). Calls provided function `f` to
73- /// iterate over multiple solutions until the function return `false`.
74- pub fn solve_multiple (
75- & mut self ,
76- context : & impl ContextOps < I , C > ,
77- goal : & UCanonical < InEnvironment < Goal < I > > > ,
78- mut f : impl FnMut ( SubstitutionResult < Canonical < ConstrainedSubst < I > > > , bool ) -> bool ,
79- ) -> bool {
80- let mut answers = self . iter_answers ( context, goal) ;
81- loop {
82- let subst = match answers. next_answer ( || true ) {
83- AnswerResult :: Answer ( answer) => {
84- if !answer. ambiguous {
85- SubstitutionResult :: Definite ( answer. subst )
86- } else {
87- if context. is_trivial_constrained_substitution ( & answer. subst ) {
88- SubstitutionResult :: Floundered
89- } else {
90- SubstitutionResult :: Ambiguous ( answer. subst )
91- }
92- }
93- }
94- AnswerResult :: Floundered => SubstitutionResult :: Floundered ,
95- AnswerResult :: NoMoreSolutions => {
96- return true ;
97- }
98- AnswerResult :: QuantumExceeded => continue ,
99- } ;
100-
101- if !f ( subst, !answers. peek_answer ( || true ) . is_no_more_solutions ( ) ) {
102- return false ;
103- }
104- }
105- }
106- }
107-
108- #[ derive( Debug ) ]
109- pub enum SubstitutionResult < S > {
110- Definite ( S ) ,
111- Ambiguous ( S ) ,
112- Floundered ,
113- }
114-
115- impl < S > SubstitutionResult < S > {
116- pub fn as_ref ( & self ) -> SubstitutionResult < & S > {
117- match self {
118- SubstitutionResult :: Definite ( subst) => SubstitutionResult :: Definite ( subst) ,
119- SubstitutionResult :: Ambiguous ( subst) => SubstitutionResult :: Ambiguous ( subst) ,
120- SubstitutionResult :: Floundered => SubstitutionResult :: Floundered ,
121- }
122- }
123- pub fn map < U , F : FnOnce ( S ) -> U > ( self , f : F ) -> SubstitutionResult < U > {
124- match self {
125- SubstitutionResult :: Definite ( subst) => SubstitutionResult :: Definite ( f ( subst) ) ,
126- SubstitutionResult :: Ambiguous ( subst) => SubstitutionResult :: Ambiguous ( f ( subst) ) ,
127- SubstitutionResult :: Floundered => SubstitutionResult :: Floundered ,
128- }
129- }
130- }
131-
132- impl < S : Display > Display for SubstitutionResult < S > {
133- fn fmt ( & self , fmt : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
134- match self {
135- SubstitutionResult :: Definite ( subst) => write ! ( fmt, "{}" , subst) ,
136- SubstitutionResult :: Ambiguous ( subst) => write ! ( fmt, "Ambiguous({})" , subst) ,
137- SubstitutionResult :: Floundered => write ! ( fmt, "Floundered" ) ,
138- }
139- }
14056}
14157
14258struct ForestSolver < ' me , I : Interner , C : Context < I > , CO : ContextOps < I , C > > {
0 commit comments