Cs774 (Prasad)L6Backtracking1 Controlling Backtracking : Cuts

Slides:



Advertisements
Similar presentations
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Advertisements

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
SLD-resolution Introduction Most general unifiers SLD-resolution
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Logic Programming (cont’d): Lists Lists in Prolog are represented by a functor (similar to cons in Scheme): 1.The empty list is represented by the constant.
1 Logic Programming School of Informatics, University of Edinburgh Transformations Specification-Program An introduction to moving between Prolog and First.
Answer Set Programming Overview Dr. Rogelio Dávila Pérez Profesor-Investigador División de Posgrado Universidad Autónoma de Guadalajara
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
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.
Bahar Pamuk. Outline Introduction Syntaxa Clauses, Programs and Queries List Manipulation Operators Backtracking, Cuts References.
CS 2104 – Prog. Lang. Concepts Final wrap-up Dr. Abhik Roychoudhury School of Computing.
Side effects in Prolog Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Cs774 (Prasad)L7Negation1 Negation by Failure
CHORD Semantics January, F-Atoms User Defined Constraint – A::B – A[B->C], A[B=>C] – A[B(V)->C], A[B(P:T0)=>T1] Built-in Constraint – 1 : Integer,
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
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.
LING 388 Language and Computers Lecture 2 9/04/03 Sandiway FONG.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Discrete Mathematics Lecture 2 Alexander Bukharovich New York University.
Inference and Resolution for Problem Solving
Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.
Artificial Intelligence Chapter 17 Knowledge-Based Systems Biointelligence Lab School of Computer Sci. & Eng. Seoul National University.
Introduction to Logic for Artificial Intelligence Lecture 2 Erik Sandewall 2010.
0 1 Todays Topics Resolution – top down and bottom up j-DREW BU procedure Subsumption – change to procedure Infinite Loops RuleML input – Prolog output.
1 Predicates and Quantifiers CS 202, Spring 2007 Epp, Sections 2.1 and 2.2 Aaron Bloomfield.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
For Wednesday Read chapter 10 Prolog Handout 4. Exam 1 Monday Take home due at the exam.
CS 2104 – Prog. Lang. Concepts Logic Programming - II Dr. Abhik Roychoudhury School of Computing.
Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog
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.
Prolog Programming. 2 DATA STRUCTURES IN PROLOG PROGRAMMING TECHNIQUES CONTROL IN PROLOG CUTS.
Logical Inference 2 rule based reasoning
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
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.
Resolution Strategies One common strategy for applying resolution is called level saturation. Here you try to resolve every pair of clauses from the original.
The AI War LISP and Prolog Basic Concepts of Logic Programming
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.
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.
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.
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.
Announcements Rainbow grades up HW1, Quiz 1-2 in HW Server Deadline for HW3 (Prolog) is extended to Monday, March 7 th. Submit in HW Server A few notes.
Logic Programming Lecture 2: Unification and proof search.
Cs7120 (Prasad)L26-ProgTech1 Programming Techniques
3. The Logic of Quantified Statements Summary
Logical Inference 2 Rule-based reasoning
Prolog a declarative language
Knowledge-Based Systems Chapter 17.
Logical Inference 2 Rule-based reasoning
Chapter 1 The Foundations: Logic and Proofs
CS 1502 Formal Methods in Computer Science
Prolog a declarative language
Prolog syntax + Unification
Prolog: cut & fail © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Prolog a declarative language
Prolog a declarative language
Programming Paradigms and Languages
Logic Programming Part 3: Control Flow
Controlling Backtracking : Cuts
Logical Inference 4 wrap up
Presentation transcript:

cs774 (Prasad)L6Backtracking1 Controlling Backtracking : Cuts

Motivation Efficiency by preventing needless backtracking –Cf. CFL Parsing Lookaheads vs Backtracking vs Dynamic programming Avoiding duplicates or resatisfaction Prolog as programming language –Tailoring control strategy cs774 (Prasad)L6Backtracking2

Efficiency Cut Preventing backtracking when faced with mutually disjoint cases sign(X, positive) :- X > 0. sign(X, negative) :- X < 0. sign(X, zero). cs774 (Prasad)L6Backtracking3

(cont’d) Good use of cut if declarative reading preserved In general, reordering of clauses may not preserve the declarative meaning. sign(X, positive) :- X > 0, !. sign(X, negative) :- X < 0, !. sign(X, zero). cs774 (Prasad)L6Backtracking4

Green Cut vs Red Cut A use of a cut which improves only efficiency of a program, without altering its declarative meaning, is referred to as a green cut. –In practice, prunes away irrelevant proofs, or proofs bound to fail. –In practice, use them when mutually exclusive cases occur, or if non-determinism can be minimized. cs774 (Prasad)L6Backtracking5

Green Cut vs Red Cut A use of a cut that alters the declarative meaning of a program is referred to as a red cut. –In practice, eliminates unwanted logical solutions Note that mature Prolog implementations index clauses for a predicate on the principal functor of the first argument in the head of a clause thereby reducing non-determinism. pf p(f(_),_,_) :- q1(…), …, qn(…). cs774 (Prasad)L6Backtracking6

Implementing Conditional “p(x) :- if c(x) then r(x) else q(x)” p(X) :- c(X), !, r(X). p(X) :- q(X). In this case, reordering of clauses changes the meaning. cs774 (Prasad)L6Backtracking7

Avoiding Duplicates member(X,[X|L]) :-!. member(X,[_|L]) :- member(X,L). Without cut, the goal ?- member(a, [a, a, a]) will succeed thrice. Note that with cut member -predicate cannot be used to generate/enumerate elements in a list. That is, invertibility is destroyed. cs774 (Prasad)L6Backtracking8

Bugs with Cuts min(X, Y, X) :- X =< Y,!. min(X, Y, Y). Flaw: the goal ?- min(3,6,6) does not fail. Reason: min(3,6,6) does not unify with min(X,Y,X). Fix : change first rule to: min(X, Y, Z) :- X =< Y,!,Z = X. cs774 (Prasad)L6Backtracking9

Effect of Cut Matching Rule: H :- B1, B2, …, Bm,!, …, Bn. Goal: ?-G. When “!” is encountered, the bindings for variables in goals B1, …,Bm, and G are frozen. That is, alternate solutions to B1, …, Bm, and remaining alternatives for G are discarded. Note that the ordering of body literals and clauses becomes significant in the presence of cut. cs774 (Prasad)L6Backtracking10

Effect of Cut p(X) :- q(X), r(X), !, s(X). p(d). Declarative Reading (ignoring cut): { p(d)} Procedural reading (using cut): { p(d)} ?- p(X). -> one solution ?- p(a). -> no ?- p(d). -> yes cs774 (Prasad)L6Backtracking11

Effect of Cut p(X) :- q(X), r(X), !, s(X). p(d). Additional Facts: {q(a),r(a),s(a)} Declarative Reading : { p(a), p(d)} Procedural reading : { p(a)} ?- p(X). -> X = a ?- p(a). -> yes ?- p(d). -> yes cs774 (Prasad)L6Backtracking12

(cont’d) p(X) :- q(X), r(X), !, s(X). p(d). Additional Facts: {q(b),s(b)} Declarative Reading (ignoring cut): { p(d)} Procedural reading (using cut): { p(d)} ?- p(X). -> X = d ?- p(a). -> no ?- p(d). -> yes cs774 (Prasad)L6Backtracking13

(cont’d) p(X) :- q(X), r(X), !, s(X). p(d). Additional Facts: {q(c),r(c)} Declarative Reading : { p(d)} Procedural reading : { } ?- p(X). -> no ?- p(c). -> no ?- p(d). -> yes cs774 (Prasad)L6Backtracking14

(cont’d) p(X) :- q(X), r(X), !, s(X). p(d). Additional Facts: {q(a),r(a),s(a), q(b),r(b),s(b)} ?- p(X). Declarative Reading (ignoring cut): { p(a), p(b), p(d)} Procedural reading (using cut): { p(a)} cs774 (Prasad)L6Backtracking15

Disadvantages of Cut Destroys the declarative reading : Need to know the behavior of the interpreter to understand the meaning (“side-effects”) p :- a, b. p :- c. p :- a, !, b. p :- c. p :- a, !, b. cs774 (Prasad)L6Backtracking16

Conditional vs Cut tp(X,Y) :- q(X) -> r(Y) ; s(Y). tp(m,m). tp(n,n). is not equivalent to sp(X,Y) :- q(X),!,r(Y). sp(X,Y) :- s(Y). sp(m,m). sp(n,n). q(a). q(c). r(e). r(f). s(g). s(h). cs774 (Prasad)L6Backtracking17

(cont’d) ?- tp(X,Y). X = a Y = e X = a Y = f X = m Y = m X = n Y = n –Local cut ?- sp(X,Y). X = a Y = e X = a Y = f –sp-goal is not resatisfied for sp-facts because of the cut. cs774 (Prasad)L6Backtracking18

Other uses of Cut Implementing default or otherwise clause. a(X,Y) :- p1(X), !, q1(X,Y). … a(X,Y) :- pn(X), !, qn(X,Y). a(X,Y) :- r(X,Y). Cut-Fail combination to formalize conditions under which failure is guaranteed. a(X) :- p(X), !, fail. cs774 (Prasad)L6Backtracking19