Chapter 6 Control Backtracking. 344-302 LP and Prolog Chapter 62 The Cut ! Cut is the built-in predicate that instructs the interpreter not to backtrack.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Slide 1Fig. 20.1, p.607. Slide 2Table 20.1, p.608.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic.
Formal Models of Computation Part II The Logic Model
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Computational Complexity
Macroeconomic Themes:61 Taxing and Spending. Macroeconomic Themes:62.
AI/ES (Artificial Intelligence / Expert System) Visual Prolog: Part 2
RAIK 283: Data Structures & Algorithms
First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
CE 498/698 and ERS 685 (Spring 2004) Lecture 61 Lecture 6: Feedback Systems of Reactors CE 498/698 and ERS 685 Principles of Water Quality Modeling.
More Prolog test vs. find built-in predicates list operations: member, append, nth0, reverse, … not, default vs. logical negation comparison operators,
Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?-
Biology 2 Plant Kingdom Identification Test Review.
Simply Logical – Chapter 3© Peter Flach, 2000 :-teaches(peter,ai_techniques) :-teaches(peter,expert_systems) :-teaches(peter,computer_science) ?-student_of(S,peter)
Chapter 61 Processing the Client Request JavaServer Pages By Xue Bai.
Sensitivity Analysis in GEM-SA. GEM-SA course - session 62 Example ForestETP vegetation model 7 input parameters 120 model runs Objective: conduct a variance-based.
SLD-resolution Introduction Most general unifiers SLD-resolution
Chapter 11 :: Logic Languages
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
Standard Logical Equivalences
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
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).
Lecture 10 Logic programming, PROLOG (PROgramming in LOGic)
Chapter 12 - Logic Programming
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Arithmetic Program Examples
Prolog Programming Lecture Module 13. Objective ● What is Prolog? ● Prolog program ● Syntax of Prolog ● Prolog Control Strategy ● Execution of Prolog.
Formal Models of Computation Part II The Logic Model
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Control Algorithms 1 Chapter 6 Control Algorithms 1 Chapter 6 Pattern Search.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
CSE S. Tanimoto Horn Clauses and Unification 1 Horn Clauses and Unification Propositional Logic Clauses Resolution Predicate Logic Horn Clauses.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
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.
1 Implementing Prolog with Coroutines Ras Bodik, Thibaud Hottelier, James Ide UC Berkeley CS164: Introduction to Programming Languages and Compilers Fall.
Lab Lecture#4 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami.
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Programming Languages Third Edition Chapter 4 Logic Programming.
Logic Programming Dr. Yasser Ahmed Nada Fall 2010/2011 Lecture 2 1 Logic Programming.
CHA2555 Week2 Practical: Lee McCluskey First term:
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.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
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.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Logic programming Candidates should be familiar with the concept of logic programming for declaring logical relationships.
Logic Programming Lecture 2: Unification and proof search.
The portion of a Prolog interpreter that executes queries (goals) is known as the inference engine. An inference engine is a kind of theorem prover, using.
More on Prolog syntax Already seen program statement types: Already seen program statement types: rules: p(X) :- q(X,Y), r(Y,z). rules: p(X) :- q(X,Y),
CSE 341, S. Tanimoto Logic Programming -
Horn Clauses and Unification
Tests, Backtracking, and Recursion
Horn Clauses and Unification
Horn Clauses and Unification
Horn Clauses and Unification
Horn Clauses and Unification
Presentation transcript:

Chapter 6 Control Backtracking

LP and Prolog Chapter 62 The Cut ! Cut is the built-in predicate that instructs the interpreter not to backtrack beyond the point at which it occurs. The cut is used to reduce the size of the search space of a query. Cut may effect : on a compound query or on the set of clauses.

LP and Prolog Chapter 63 Cut Compound query Get a value of X from “ a ” Get a value of Y from “ b ” Get a value of X,Y,Z from “ c ” Goal : a(X), b(Y), !, c(X,Y,Z). It will not go beyond ! command

LP and Prolog Chapter 64 predicates a(integer) b(integer) c(integer,integer,integer) run1 /* when success then stop */ run2 /* find all solution, backtrack all "b" value first before backtrack "a" value */ run3 /* not go further than ! */ run4 /* fail and cut work together */ run5 /* fail and cut work together, not go to "a" value */ clauses a(3). a(7). a(9). b(10). b(200). c(S,T,U) :- U = S + T. run1 :- a(X), b(Y), c(X,Y,Z), write("a = ", X," b = ", Y, " c = ",Z), nl. run2 :- a(X), b(Y), c(X,Y,Z), write("a = ", X," b = ", Y, " c = ",Z), nl, fail. run3 :- a(X), b(Y), !, c(X,Y,Z), write("a = ", X," b = ", Y, " c = ",Z), nl. run4 :- a(X), b(Y), !, c(X,Y,Z), write("a = ", X," b = ", Y, " c = ",Z), nl, fail. run5 :- a(X),!, b(Y), c(X,Y,Z), write("a = ", X," b = ", Y, " c = ", Z), nl, fail. LP24.proCut

LP and Prolog Chapter 65 Factorial Ex07EX03.pro /* Recursive program to compute factorials. */ predicates factorial(integer, real) clauses factorial(1, 1) :- !. factorial(X, FactX) :- Y = X-1, factorial(Y, FactY), FactX = X*FactY.

LP and Prolog Chapter 66 Factorial (2) /* Recursive program to compute factorials. */ predicates factorial(integer, integer) clauses factorial(1, 1) :- !. factorial(FactX, X ) :- Y = X-1, factorial( FactY, Y), FactX = X*FactY.

LP and Prolog Chapter 67 Evaluation of a query Recursive cycle of unification (pattern matching) Subgoal evaluation Evaluate the body of a clause If unification fail, go on to the next clause Ex: uncle(john,daniel) :- brother(john,bill), father(bill, daniel)

LP and Prolog Chapter 68 Unification 1. A variable unifies with a constant ? X = john 2. A variable unifies with a variable ? X = Y 3. _ unifies with anything ? likes(_,What)

LP and Prolog Chapter 69 Unification 4. A constant unifies with a constant, if they are identical ? likes(john, dog) yes 5. A structure unifies with a structure if the structure names are the same and if the arguments can be unified. ? father(john) = father(X) X = john

LP and Prolog Chapter 610 Failure and backtracking If the active query is part of compound query (a list of subgoal) and is not the first subgoal in the compound query then the interpreter backtracks to reconsider the previous subgoal in the compound query. If the active query is the first subgoal in the compound query, then when it is fail, the compound query fail as well.

LP and Prolog Chapter 611 predicates uncle(symbol,symbol) brother(symbol,symbol) father(symbol,symbol) clauses brother(daniel, kenneth). brother(daniel, jim). brother(john,jim). father(bill, daniel). father(kenneth, bill ). uncle(U,N) :- brother(U,B), father(B,N). LP Lec20.pro

LP and Prolog Chapter 612 predicates register(symbol) age(symbol,integer) male(symbol) clauses male(brain). male(mike). male(steve). age(brain, 18). age(mile, 17). age(steve, 18). register(X) :- male(X), age(X,Y), Y = 18. LP Lec20.pro

LP and Prolog Chapter 613 Recursive procedures 1. A nonrecursive clause defining the base case of the procedure, that is where the recursion stops. 2. A recursive rule. In the body of this rule, the first subgoals generate new argument values. Then follows a recursive subgoal utilizing the new argument values.

LP and Prolog Chapter 614 case 1 ancestor(A,C) :- parent(A,C). /*base case */ ancestor(A,C) :- parent(A,B),ancestor(B,C). Recursive procedures case 2 ancestor(A,C) :- parent(A,C). /*base case */ ancestor(A,C) :- ancestor(B,C), parent(A,B). OK

LP and Prolog Chapter 615 Case 1 : lp23c1.pro : Top down predicates ancestor1(symbol,symbol) parent(symbol,symbol) clauses parent(ellen, john). parent(ellen, lisa). parent(lisa, sherry). parent(john, tom). parent(tom, eric). parent(eric, mark). /* case */ ancestor1(A,C) :- parent(A,C). /* base case */ ancestor1(A,C) :- parent(A,B), ancestor1(B,C). /* recursive clause */ Q1 ?ancestor1(ellen,tom) Q2 ?ancestor1(ellen,mark) Q3 ?ancestor1(ellen,sherry) Yes

LP and Prolog Chapter 616 Family Tree ellen john Lisa tom sherry eric mark L1 L2 L3 L4 L5 TOP Bottom case 1 case 2

LP and Prolog Chapter 617 Case 2 : lp23c2.pro : Bottom up predicates ancestor2(symbol,symbol) parent(symbol,symbol) clauses parent(ellen, john). parent(ellen, lisa). parent(lisa, sherry). parent(john, tom). parent(tom, eric). parent(eric, mark). /* case */ ancestor2(A,C) :- parent(A,C). /* base case */ ancestor2(A,C) :- ancestor2(B,C), parent(A,B). /* recursive clause */ Q1 ?ancestor2(ellen,tom) Q2 ?ancestor2(ellen,mark) Q3 ?ancestor2(ellen,sherry) Yes

LP and Prolog Chapter 618 Both people like the same thing predicates both_like(symbol,symbol,symbol) like(symbol,symbol) same_thing clauses both_like(First,Second, Thing) :- like(First,Thing), like(Second,Thing), First <> Second. same_thing :- both_like(First,Second,Thing), write(First, " and ",Second," like ",Thing, "\n \n"). like(kosin,water). like(dum,wine). like(dum,salad). like(nipa,salad). like(nipa,ice_cream). like(nipa,wine).

LP and Prolog Chapter 619 Count down case 1 predicates countdown1. numb(integer). clauses numb(5). numb(4). numb(3). numb(2). numb(1). numb(0). countdown1 :- numb(X), write(X," "), fail.

LP and Prolog Chapter 620 Count down case 2 (p.44 thai bk) predicates countdown(integer). clauses countdown(0) :- write("0 "). countdown(N) :- write(N," "), M = N - 1, countdown(M).

LP and Prolog Chapter 621 Count up case 1 (p.48 thai bk) predicates countup1. numb(integer). clauses numb(0). numb(1). numb(2). numb(3). numb(4). numb(5). countup1 :- numb(X), write(X," "), fail.

LP and Prolog Chapter 622 Count up case 2 (p.49 thai bk) predicates countup2. numb(integer). clauses countup2 :- numb(X), write(X," "), fail. numb(0). numb(A) :- numb(B), A = B + 1. Use Fail Loop until ………… infinity

LP and Prolog Chapter 623 Count up case 3 (p.49 thai bk) domains X = integer predicates countup3(integer). numb(integer). clauses countup3(N) :- numb(X), write(X," "), X = N. numb(0). numb(A) :- numb(B), A = B + 1. Do until X = N Loop until ………… N

LAB : Recursive clause /* แบบที่ */ ako1(A,C) :- isa(A,C). /* base case */ ako1(A,C) :- isa(A,B), ako1(B,C). /* recursive clause */ /* แบบที่ */ ako2(A,C) :- isa(A,C). /* base case */ ako2(A,C) :- ako2(B,C), isa(A,B). /* recursive clause */ จงเขียนโปรแกรมภาษาโปรล็อกเพื่อพิสูจน์ว่าประโยค Recursive ในแบบที่ 1 และ แบบที่ 2 ที่กำหนดให้ว่า สามารถใช้พิสูจน์การทำงาน ako ได้เหมือนหรือแตกต่างกัน อย่างไร ให้นักศึกษายกตัวอย่างข้อเท็จจริงขึ้นเอง พร้อม อธิบายผลการทำงานที่ได้จากการรันโปรแกรมโดยละเอียด

LAB :count up / count down จงเขียนโปรแกรมภาษาโปรล็อก 1) เพื่อพิมพ์เลขคู่เพิ่มขึ้นจาก จาก 0 ถึง 30 2) พิมพ์เลขคี่ลดลงจาก 29 ถึง 1

The end