lec19 Accumulators

lec19 Accumulators -...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
http://www.cs.ucsd.edu/classes/fa06/cse130/lectures/prolog2.txt =============================== Lecture 2 =============================== ----------------------------------- Cuts ---------------------------------- - Ordering clauses and goals is a way to somewhat control the search and backtracking process, but it is very limited. - There is something called a "cut" that prevents Prolog from backtracking. - Example: Let's say we're writing a program to compute the following step function: X < 3 phi(X) = 0 3 <= X < 6 phi(X) = 2 6 <= X phi(X) = 4 In Prolog we can implement this with a binary predicate, f(X,Y), which is true if Y is the function value at point X. For instance, f(0,0) is true, f(4,2) is true, but f(2,4) is false. Here is the program: f(X,0) :- X < 3. [rule 1] f(X,2) :- 3 =< X, X < 6. [rule 2] note '=<' f(X,4) :- 6 =< X. [rule 3] There are two sources of inefficiency in this program, that we'll see on one example: ?- f(1,Y), 2 < Y. [find a Y such that Y = f(1) and 2 < Y] [ we can see this is going to fail] what does Prolog do? f(1,Y) 2 < Y ---------- rule 1 / \ \ Y = 0 / \ rule 2 \ rule 3 / | Y = 2 | Y = 4 | | | 1 < 3 3 <= 1 6 <= 1 2 < 0 1 < 6 2 < 4 | 2 < 2 NO | NO 2 < 0 NO There is really no point in trying rule 2 and rule 3 because since X < 3, we know that rule 2 and rule 3 will fail. Basically, the three rules are mututally exclusive. We know that. Prolog doesn't. So, we can "cut" the backtracking by using the '!' operator: f(X,0) :- X < 3, !. f(X,2) :- 3 <= X, X < 6, !. f(X,4) :- 6 <= X. The new execution looks like: http://www.cs.ucsd.edu/classes/fa06/cse130/lectures/prolog2.txt (1 of 7) [2/13/2008 5:17:37 PM]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
http://www.cs.ucsd.edu/classes/fa06/cse130/lectures/prolog2.txt f(1,Y) 2 < Y rule 1 / Y = 0 / / | 1 < 3 2 < 0 | CUT | 2 < 0 NO Lessons: cuts can be used to prevent Prolog from going into branches of the search tree that we know, due to our understanding and knowledge of the problem, will not succedd anyway. - There are many more things possible with cuts and using them well is an art. A program with no cuts at all will run orders of magnitude slower than an equivalent program with a few '!' thrown in. -------------------------- Accumulators --------------------------- - There are cases in which you want to add an argument to a predicate just to keep track of useful information Example: List Reverse: ---------------------- We will now write a predicate: rev(X,Y) that is true if the list Y is the reverse of the list X. To do so, we will use an accumulator that tracks the elements seen so far in X. rev(X,Y) :- acc_rev(X,Y,[]).
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

lec19 Accumulators -...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online