FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.

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.
CS344: Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 12–Prolog examples: Himalayan club, member, rem_duplicate,
Chapter 11 :: Logic Languages
Prolog.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
CS344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 16- 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).
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.
F28PL1 Programming Languages Lecture 17: Prolog 2.
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
© 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.
1 Non-determinism and Cut  Non-determinism  Backtracking  Controlling Backtracking.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
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.
LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
1 LING 438/538 Computational Linguistics Sandiway Fong Lecture 4: 8/31.
(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 Programming Style and Technique Notes for Ch.8 of Bratko For CENG421&553 Fall03.
CS 321 Programming Languages and Compilers Prolog part 2.
CS 2104 – Prog. Lang. Concepts Logic Programming - II Dr. Abhik Roychoudhury School of Computing.
1 Lecture 15: Introduction to Logic Programming with Prolog (Section 11.3) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill.
© 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.
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 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.
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.
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures (predicates),
Section 16.5, 16.6 plus other references
Recursive stack-based version of Back-chaining using Propositional Logic
Tests, Backtracking, and Recursion
Prolog: cut & fail © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Chapter 12 :: Logic Languages
Programming Paradigms and Languages
Programming Techniques
Controlling Backtracking : Cuts
Presentation transcript:

FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03

FATIH UNIVERSITY Department of Computer 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

FATIH UNIVERSITY Department of Computer 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

FATIH UNIVERSITY Department of Computer 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

FATIH UNIVERSITY Department of Computer 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

FATIH UNIVERSITY Department of Computer 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)

FATIH UNIVERSITY Department of Computer 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

FATIH UNIVERSITY Department of Computer 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)

FATIH UNIVERSITY Department of Computer 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

FATIH UNIVERSITY Department of Computer 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).

FATIH UNIVERSITY Department of Computer 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)

FATIH UNIVERSITY Department of Computer 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

FATIH UNIVERSITY Department of Computer Engineering Logical Negation Logical negation cannot be handled using Prolog’s goal tree mechanism See HoleInGoalTrees.ppt