UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.

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.
Prolog.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
PROLOG Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
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)
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for CSCE 330 Based on Bratko and Van Emden Marco.
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
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
Part 1 The Prolog Language Chapter 5 Controlling Backtracking
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 580 Artificial Intelligence Ch.5 [P]: Propositions and Inference Sections.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Built-in Predicates Notes for Ch.7 of Bratko For CSCE 580 Sp03 Marco Valtorta.
1 Non-determinism and Cut  Non-determinism  Backtracking  Controlling Backtracking.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 580 Artificial Intelligence Ch.12 [P]: Individuals and Relations Proofs.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for Ch.1 of Bratko For CSCE 580 Sp03 Marco Valtorta.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Input and Output Notes for Ch.6 of Bratko For CSCE 580 Sp03 Marco Valtorta.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Haskell Fall 2005 Marco Valtorta
LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering A Hole in Goal Trees Notes for: D.W. Loveland and M. Stickel. “A Hole in Goal.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for CSCE 330 Based on Bratko and Van Emden Marco.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Definite Knowledge Notes for Ch.3 of Poole et al. CSCE 580 Marco Valtorta.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Syntax and Meaning of Prolog Programs Notes for Ch.2 of Bratko For CSCE 580.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Functional Programming Notes for CSCE 190 Based on Sebesta,
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Heuristic Search Notes for Ch.12 of Bratko and Ch.2 of Shoham For CSCE 580.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
CS1502 Formal Methods in Computer Science Lecture Notes 10 Resolution and Horn Sentences.
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.
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.
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.
Flow of Control Chapter 5. Flow of Control n What order computer uses to get answers –sub-goal ordering –clause ordering n Prolog flow-of-control –sequence.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 781 AILog for Ch.5 Spring 2011 Marco Valtorta
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
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 and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Lab Lecture#4 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami.
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 programming....Dr.Yasser Nada. Chapter 7 Control and Negation Taif University Fall 2010 Dr. Yasser Ahmed Nada prolog programming....Dr.Yasser Nada.
CS 152: Programming Language Paradigms March 5 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 152: Programming Language Paradigms March 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Chapter 2 Syntax and meaning of prolog programs Part 1.
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.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Logic Programming: Prolog Notes for CSCE 190 Based on Bratko,
Recursive stack-based version of Back-chaining using Propositional Logic
Tests, Backtracking, and Recursion
Prolog: cut & fail © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Programming Paradigms and Languages
Programming Techniques
Controlling Backtracking : Cuts
Presentation transcript:

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Preventing Backtracking Prolog automatically backtracks in its attempts to satisfy a goal (equivalently, in its traversal of a goal tree) Sometimes, backtracking is a source of inefficiency For example, Prolog, when requested for additional answers to a goal, may try clauses that are known to lead, eventually, to failure

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering A Double-step Function if X < 3 then Y = 0 if 3 =< X and X < 6 then Y = 2 if 6 =< X then Y = 4 In Prolog, f( X,0) :- X < 3. % Rule 1 f( X,2) :- 3 =< X, X < 6. % Rule 2 f( X,4) :- 6 =< X. % Rule 3 Assume that X is instantiated to a number when the program is used The program does not exploit the fact that the three rules are mutually exclusive

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Experiment 1: ch5_1.pl [trace] 6 ?- f( 1,Y), Y > 2. Call: (7) f(1, _G423) ? creep ^ Call: (8) 1<3 ? creep ^ Exit: (8) 1<3 ? creep Exit: (7) f(1, 0) ? creep No need to go beyond here! Redo: (7) f(1, _G423) ? creep But rules 2 and 3 ^ Call: (8) 3=<1 ? creep are tried anyway ^ Fail: (8) 3=<1 ? creep Redo: (7) f(1, _G423) ? creep ^ Call: (8) 6=<1 ? creep ^ Fail: (8) 6=<1 ? creep Fail: (7) f(1, _G423) ? creep No

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using the cut: ch5_2.pl f( X,0) :- X < 3, !. % commit! f( X,2) :- 3 =< X, X < 6, !. % commit! f( X,4) :- 6 =< X. [trace] 9 ?- f( 1,Y), Y > 2. Call: (7) f(1, _G423) ? creep ^ Call: (8) 1<3 ? creep ^ Exit: (8) 1<3 ? creep Exit: (7) f(1, 0) ? creep Fail: (7) f(1, 0) ? creep since rule1 was “used”, No rules 2 and 3 are not tried “used” means: the cut in rule 1 was reached

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Green and Red Cuts f( X,0) :- X < 3, !. % if X < 3 then Y = 0 f( X,2) :- X < 6, !. % otherwise, if X < 6 then Y = 2 f( _,4). % otherwise Y = 4 (ch5_3.pl) But the following is incorrect f( X,0) :- X < 3. f( X,2) :- X < 6. f( _,4). Sometimes a cut changes the meaning of a program Red cuts change the meaning of a program (as in the program above) Green cuts do not change the meaning of a program (as in ch5_2.pl)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Cut Mechanism Let the parent goal be the one that matches the head of the clause containing the cut When the cut is encountered as a goal, it succeeds immediately, but it commits the system to all choices made between the time the parent goal was invoked and the time the cut was encountered: all the remaining possibilities between the parent goal and the cut are discarded

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Examples Using Cut: ch5_4.pl max/3 (with cut); max1/3 (without cut) strange behavior when one of the arguments is a list of numbers: the first number in the list is used in the comparison. Cf. SWI-Prolog manual, section problem with max( 3,1,1): it succeeds! max2/3 fixes this problem max2( X,Y,Max) :- X >= Y, !, Max = X. max2( _,Y,Max) :- Max = Y. deterministic membership (member1/2)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Examples Using Cut: ch5_4.pl, Ctd. adding an element to a list without duplication add/3 classification into categories class/2 in both cases, the last argument should not be instantiated

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Negation as Failure different( X,Y) :- X = Y, !, fail. different( X,Y). Or: different( X,Y) :- X = Y, !, fail; true. not( P) :- P, !, fail ; true. op( 900, fy, [not, ‘\+’]). not P succeeds if P fails. different( X,Y) :- not( X = Y).

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Problem with Negation As Failure Cf. even/odd mutually recursive program (ch5_5.pl) A positive query ?-p(X) is interpreted as a proof for “there exists X s.t. P(X) is true” A negative query ?- not(p(X)) is interpreted as a proof for “for all X, P(X) is false)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Problems with cut and negation The main problem with cut is that we may lose the valuable correspondence between declarative and procedural meanings of a program Prolog negation is based on the closed world assumption: “if something cannot be proven, it is false.” This leads to especially bad results when the argument of not is a term containing unistantiated variables Some Prolog dialects (e.g., NU-Prolog) try to delay not subgoals until all variables in them are instantiated, when possible

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Logical Negation Logical negation cannot be handled using Prolog’s goal tree mechanism See HoleInGoalTrees.ppt