14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
CS344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 16- Prolog.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
Prolog OR (disjunction) “;” is same as a logical OR “;” is same as a logical OR It is also equivalent to using separate clauses... It is also equivalent.
4. PROLOG Data Objects And PROLOG Arithmetic
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Part 1 The Prolog Language Chapter 5 Controlling Backtracking
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Introduction to Logic for Artificial Intelligence Lecture 2 Erik Sandewall 2010.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Formal Models of Computation Part II The Logic Model
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
CS 321 Programming Languages and Compilers Prolog part 2.
CS 2104 – Prog. Lang. Concepts Logic Programming - II Dr. Abhik Roychoudhury School of Computing.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3 “Input & Output”, Negation, Search.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
CSC 270 – Survey of Programming Languages Prolog Lecture 2 – Unification and Proof Search.
CSE S. Tanimoto Horn Clauses and Unification 1 Horn Clauses and Unification Propositional Logic Clauses Resolution Predicate Logic Horn Clauses.
1 Relational Algebra and Calculas Chapter 4, Part A.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Lab Lecture#4 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Cs774 (Prasad)L6Backtracking1 Controlling Backtracking : Cuts
Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate.
CSE (c) S. Tanimoto, 2008 Predicate Calculus II 1 Predicate Calculus 2 Outline: Unification: definitions, algorithm Formal interpretations and satisfiability.
Prolog Unification and clause order. The notion of Unification Unification is when two things “become one” Unification is kind of like assignment Unification.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Answer Extraction To use resolution to answer questions, for example a query of the form  X C(X), we must keep track of the substitutions made during.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
COSC 2P93 Prolog: Debugging
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Horn Clauses and Unification
Tests, Backtracking, and Recursion
Chapter 11 :: Logic Languages
Prolog: cut & fail © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Horn Clauses and Unification
Horn Clauses and Unification
Programming Paradigms and Languages
Horn Clauses and Unification
Horn Clauses and Unification
Controlling Backtracking : Cuts
Presentation transcript:

14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04

AIPP Lecture 7: The Cut2 Clearing up equality There are various ways to test equality in Prolog. X = Y succeeds if the terms X and Y unify. X is Y succeeds if the arithmetic value of expression Y matches the value of term X. X =:= Y succeeds if the arithmetic value of two expressions X and Y match. X =\= Y succeeds if the arithmetic value of two expressions X and Y DO NOT match. X == Y succeeds if the two terms have literal equality = are structurally identical and all their components have the same name. X \== Y succeeds if the two terms are NOT literally identical. \+ Goal succeeds if Goal does not true

14/10/04AIPP Lecture 7: The Cut3 Clearing up equality (2) | ?- 3+4 = 4+3. no % treats them as terms | ?- 3+4 = 3+4. yes | ?- X = 4+3. X = 4+3 ? yes | ?- X is 4+3. X = 7 ? yes | ?- 3+4 is 4+3. no % left arg. has to be a term | ?- 3+4 =:= 4+3. yes % calculates both values | ?- 3+4 =\= 4+3. no | ?- 3+4 == 4+3. no | ?- 3+4 \== 4+3. yes | ?- 3+X = 3+4. X = 4 ? yes | ?- 3+X == 3+4. no | ?- \+ 3+4 == 4+3. yes

14/10/04AIPP Lecture 7: The Cut4 Processing in Prolog To call the goal G: 1. Find first clause head that matches G: 1. bind all variables accordingly, 2. call goals in body in order; 3. if all succeed, G succeeds (and exits ). 2. else try next clause down; 3. if no next clause, fail the goal G. When a goal fails: redo the most recent successful goal To redo a goal: 1.discard bindings from previous success; 2.try clauses for this goal not so far tried; 3.if none, fail the goal.

14/10/04AIPP Lecture 7: The Cut5 Byrd Box model This is the model of execution used by the tracer. Originally suggested by Lawrence Byrd. GOAL Exception (error) CALL FAIL EXIT REDO

14/10/04AIPP Lecture 7: The Cut6 Redo-ing a Goal fact(b,1). fact(b,2). a :- fact(b,N), fact(c,N). |?- a. fact(b,N) CALL EXIT N=1

14/10/04AIPP Lecture 7: The Cut7 Redo-ing a Goal (2) fact(b,1). fact(b,2). a :- fact(b,N), fact(c,N). |?- a. fact(b,N) CALL EXIT N=1 REDO fact(c,1) CALL FAIL

14/10/04AIPP Lecture 7: The Cut8 Redo-ing a Goal (3) fact(b,1). fact(b,2). a :- fact(b,N), fact(c,N). |?- a. fact(b,N) CALL EXIT N=1 REDO EXIT N=2

14/10/04AIPP Lecture 7: The Cut9 Redo-ing a Goal (4) fact(b,1). fact(b,2). a :- fact(b,N), fact(c,N). |?- a. no. fact(b,N) CALL EXIT N=1 REDO EXIT N=2 fact(c,2) CALL FAIL

14/10/04AIPP Lecture 7: The Cut10 Prolog’s Persistence When a sub-goal fails, Prolog will backtrack to the most recent successful goal and try to find another match. Once there are no more matches for this sub-goal it will backtrack again; retrying every sub-goal before failing the parent goal. A call can match any clause head. A redo ignores old matches. a:- b, c, d, e, f, g, h, I, j. Succeed Fail Redo Backtrack a:- b, c, d, e, f, g, h, I, j. A new instantiation a:- b, c, d, e, f, g, h, I, j.

14/10/04AIPP Lecture 7: The Cut11 Cut ! If we want to restrict backtracking we can control which sub-goals can be redone using the cut = !. We use it as a goal within the body of clause. It succeeds when call ed, but fails the parent goal (the goal that matched the head of the clause containing the cut) when an attempt is made to redo it on backtracking. It commits to the choices made so far in the predicate. –unlimited backtracking can occur before and after the cut but no backtracking can go through it. a:- b, c, d, e, !, f, g, h, I, j. immediate fail

14/10/04AIPP Lecture 7: The Cut12 Failing the parent goal The cut succeeds when it is call ed and commits the system to all choices made between the time the parent goal was invoked and the cut. This includes committing to the clause containing the cut. = the goal can only succeed if this clause succeeds. When an attempt is made to backtrack through the cut –the clause is immediately failed, and –no alternative clauses are tried. a:- b, c, d, e, !, f, g, h, I, j. a:- k. a:- m. a:- b, c, d, e, !, f, g, h, I, j. a:- k. a:- m. This clause and these choices committed to Treated as if don’t exist

14/10/04AIPP Lecture 7: The Cut13 Mutually Exclusive Clauses We should only use a cut if the clauses are mutually exclusive (if one succeeds the others won’t). If the clauses are mutually exclusive then we don’t want Prolog to try the other clauses when the first fails = redundant processing. By including a cut in the body of a clause we are committing to that clause. –Placing a cut at the start of the body commits to the clause as soon as head unification succeeds. a(1,X):- !, b(X), c(X). –Placing a cut somewhere within the body (even at the end) states that we cannot commit to the clause until certain sub-goals have been satisfied. a(_,X):- b(X), c(X), !.

14/10/04AIPP Lecture 7: The Cut14 Mutually Exclusive Clauses (2) f(X,0):- X < 3. f(X,1):- 3 =< X, X < 6. f(X,2):- 6 =< X. |?- trace, f(2,N). 1 1 Call: f(2,_487) ? 2 2 Call: 2<3 ? 2 2 Exit: 2<3 ? ? 1 1 Exit: f(2,0) ? N = 0 ? ; 1 1 Redo: f(2,0) ? 3 2 Call: 3=<2 ? 3 2 Fail: 3=<2 ? 4 2 Call: 6=<2 ? 4 2 Fail: 6=<2 ? 1 1 Fail: f(2,_487) ? no

14/10/04AIPP Lecture 7: The Cut15 Green Cuts ! f(X,0):- X < 3, !. f(X,1):- 3 =< X, X < 6, !. f(X,2):- 6 =< X. |?- trace, f(2,N). 1 1 Call: f(2,_487) ? 2 2 Call: 2<3 ? 2 2 Exit: 2<3 ? ? 1 1 Exit: f(2,0) ? N = 0 ? ; no If you reach this point don’t bother trying any other clause. Notice that the answer is still the same, with or without the cut. –This is because the cut does not alter the logical behaviour of the program. –It only alters the procedural behaviour: specifying which goals get checked when. This is called a green cut. It is the correct usage of a cut. Be careful to ensure that your clauses are actually mutually exclusive when using green cuts!

14/10/04AIPP Lecture 7: The Cut16 Because the clauses are mutually exclusive and ordered we know that once the clause above fails certain conditions must hold. We might want to make our code more efficient by removing superfluous tests. Red Cuts ! | ?- f(7,N). 1 1 Call: f(7,_475) ? 2 2 Call: 7<3 ? 2 2 Fail: 7<3 ? 3 2 Call: 3=<7 ? 3 2 Exit: 3=<7 ? 4 2 Call: 7<6 ? 4 2 Fail: 7<6 ? 5 2 Call: 6=<7 ? 5 2 Exit: 6=<7 ? 1 1 Exit: f(7,2) ? N = 2 ? yes f(X,0):- X < 3, !. f(X,1):- 3 =< X, X < 6, !. f(X,2):- 6 =< X. Redundant?

14/10/04AIPP Lecture 7: The Cut17 Red Cuts ! f(X,0):- X < 3, !. f(X,1):- X < 6, !. f(X,2). | ?- f(7,N). 1 1 Call: f(7,_475) ? 2 2 Call: 7<3 ? 2 2 Fail: 7<3 ? 3 2 Call: 7<6 ? 3 2 Fail: 7<6 ? 1 1 Exit: f(7,2) ? N = 2 ? yes f(X,0):- X < 3. f(X,1):- X < 6. f(X,2). | ?- f(1,Y). 1 1 Call: f(1,_475) ? 2 2 Call: 1<3 ? 2 2 Exit: 1<3 ? ? 1 1 Exit: f(1,0) ? Y = 0 ? ; 1 1 Redo: f(1,0) ? 3 2 Call: 1<6 ? 3 2 Exit: 1<6 ? ? 1 1 Exit: f(1,1) ? Y = 1 ? ; 1 1 Redo: f(1,1) ? 1 1 Exit: f(1,2) ? Y = 2 ? yes

14/10/04AIPP Lecture 7: The Cut18 Using the cut Red cuts change the logical behaviour of a predicate. TRY NOT TO USE RED CUTS! Red cuts make your code hard to read and are dependent on the specific ordering of clauses (which may change once you start writing to the database). If you want to improve the efficiency of a program use green cuts to control backtracking. Do not use cuts in place of tests. To ensure a logic friendly cut either: p(X):- test1(X), !, call1(X). p(X):- test2(X), !, call2(X). p(X):- testN(X), !, callN(X). testI predicates are mutually exclusive. p(1,X):- !, call1(X). p(2,X):- !, call2(X). p(3,X):- !, callN(X). The mutually exclusive tests are in the head of the clause.

14/10/04AIPP Lecture 7: The Cut19 As well as specifying conditions under which a goal can succeed sometimes we also want to specify when it should fail. We can use the built-in predicate fail in combination with a cut to achieve this: “ !, fail. “ = if you reach this point, fail regardless of other clauses. e.g. If we want to represent the fact that ‘Mary likes all animals except snakes’. likes(mary,X):- snake(X), !, fail. likes(mary,X):- \+ snake(X), animal(X). Cut - fail We need to combine a cut with the fail to stop the redundant call to the second clause on backtracking.

14/10/04AIPP Lecture 7: The Cut20 Cut – fail: why? However, using a cut-fail can make your code hard to follow. It is generally clearer and easier to define the conditions under which a fact is true rather than when it is false. likes(mary,X):- \+ snake(X), This is sufficient to represent the fact. animal(X). However, sometimes it can be much simpler to specify when something is false rather than true so cut-fail can make your code more efficient. As with all cuts; be careful how you use it.

14/10/04AIPP Lecture 7: The Cut21 Summary Clearing up equality: =, is, =:=, =\=, ==, \==, \+ REDO vs. CALL Controlling backtracking: the cut ! –Efficiency: avoids needless REDO-ing which cannot succeed. –Simpler programs: conditions for choosing clauses can be simpler. –Robust predicates: definitions behave properly when forced to REDO. Green cut = cut doesn’t change the predicate logic = good Red cut = without the cut the logic is different = bad Cut – fail: when it is easier to prove something is false than true.