CS 541 Lecture -*- Outline -*- * Procedural model of Prolog ** practical questions in using resolution (and Prolog's answers) our abstract interpreter has a number of choices to make... ------------------------------------------ CHOICES IN THE ABSTRACT INTERPRETER what substitution to use? what to resolve? what disjunct in goal to resolve? what clause to resolve against? ------------------------------------------ ... most general no limitation here ... goal or descendent of goal called backwards chaining ... leftmost called linear resolution ... from program, top-down gives depth-first search strategy breadth-first - sure to find solution (complete) depth first - less storage for backtracking (Prolog) *** Top-down vs. bottom up control **** top-down control (backward chaining) as in Prolog goal directed, start with goal as in resolution algorithm only works well when there is a small set of possible answers **** bottom up control (forward chaining) as in OPS5 hard to know what goals to generate (this is the point of the RETE algorithm) *** Depth-first search vs breadth-first search. *draw trees* breadth-first search strategy guaranteed to find refutation if possible may require exponential amount of space (hence time) to track *mimics parallel search! depth-first may not find refutation on another branch requires less space if succeeds. makes order of clauses tried very important example: ------------------------------------------ sig needs_breadth_first. type p o. type q o. module needs_breadth_first. p :- p, q. p. q. ------------------------------------------ Prolog goes into infinite loop on goal [needs_breadth_first] ?- p. Simulator: Stack overflow. breadth first search finds answer. ** search trees that show backtracking e.g., to answer the query ?- (logician Y), (american Y) (**draw following tree as you go, top to bottom, depth first, left to right **) (logician Y), (american Y) | X = U0 | %5 Y = U0 | | (scientist U0), (american U0) | | U0 = sue | %1 U0 = ron | %2 | | (american sue) (american ron) | | % 4 fails! succeeds (american sue) does not unify with any lhs, so that branch fails backtrack to last selection, starting after use of rule 1, ** Another way to see examples, time vs. goals stack graphs from Sam Kamin's book: Programming Languages, an Interpreter-based approach (McGraw-Hill) ------------------------------------------ sig pop_psych1. type imokay o. type youreokay o. type hesokay o. type theyreokay o. module pop_psych1. imokay :- youreokay, hesokay. % C1 youreokay :- theyreokay. % C2 hesokay. % C3 theyreokay. % C4 [pop_psych1] ?- imokay. yes ------------------------------------------ draw time (x-axis) vs. goals (stacked on Y axis) pictures the following graphs plot time (x-axis) vs. goals (stacked on Y axis) current goal is on the bottom, as we go left to right, we are taking steps in evaluation process, indicated by the arrows labeled with the clause name (see the comments in the program). graph for the query ?- imokay. ============================================================== ^ hesokay hesokay goals imokay -C1-> youreokay -C2-> theyreokay -C4-> hesokay -C3-> [] time --> ============================================================== ------------------------------------------ sig pop_psych2. accum_sig pop_psych1. type shesokay o. type imnotokay o. type hesnotokay o. module pop_psych2. accumulate pop_psych1. hesnotokay :- imnotokay. % C5 shesokay :- hesnotokay. % C6 shesokay :- theyreokay. % C7 [pop_psych2] ?- shesokay. yes ------------------------------------------ draw pictures, show backtracking graph for the query ?- shesokay =========================================================== ^ goals shesokay -C6-> hesnotokay -C5-> imnotokay -> theyreokay -C4-> [] \-----------------------------C7--/ time --> ============================================================== The imnotokay goal fails, so we backtrack to see if there's any other way to satisfy the goal hesnotokay, but there's no other way to do that, so we backtrack again to see if there is some other way to satisfy the goal shesokay, and this is where the rule C7 gets used. We pick up from where we left off in trying to satisfy a goal; you can think of that putting your finger where you were at each stage of the process in trying to satisfy each goal. ------------------------------------------ sig pop_psych3. accum_sig pop_psych2. module pop_psych3. accumulate pop_psych2. hesnotokay :- shesokay. % C8 hesnotokay :- imokay. % C9 [pop_psych3] ?- shesokay. Simulator: Stack overflow. ------------------------------------------ Q: can you trace this? it gets into an infinite loop this picture is accurate for variable-free prolog, but need to do more to show how it works with variables. draw time vs. goals picture ** practical implications declarative, or logical meaning of a Prolog program does not match the way it really executes. if it was just logic, adding more information would not cause any difference in the answer. def: a computation of a goal Q by a program P terminates if Prolog comes up with a substitution, or fails in a finite time. *** order of rules matters determines order in which solutions are found, and termination *** order of clauses in body matters determines order of new subgoals, and hence the search tree recall searched depth first *** termination domain (skip) Q: how would you define the termination domain of a program? def: the domain of a program P is a set of goals, D, such that if A is in D and A' is an instance of A, then A' is in D. (closed under instantiation) def: a termination domain of a program P is a domain D such that every computation of P on D terminates