Tests, Backtracking, and Recursion

Slides:



Advertisements
Similar presentations
Chapter 11 :: Logic Languages
Advertisements

Notes for CS3310 Artificial Intelligence Part 5: Prolog arithmetic and lists Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009.
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.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
F28PL1 Programming Languages Lecture 17: Prolog 2.
Introduction to Prolog What is Prolog? Application Areas of Prolog How does Prolog work? (Syntax of Prolog) Program Structure.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
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.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Prolog Programming Lecture Module 13. Objective ● What is Prolog? ● Prolog program ● Syntax of Prolog ● Prolog Control Strategy ● Execution of Prolog.
Sequence Control Chapter 6. 2 l Control structures: the basic framework within which operations and data are combined into programs. Sequence control.
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.
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.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
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.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
F28PL1 Programming Languages Lecture 16: Prolog 1.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Chương 3 Tri thức và lập luận. Nội dung chính chương 3 I.Logic – ngôn ngữ của tư duy II.Logic mệnh đề (cú pháp, ngữ nghĩa, sức mạnh biểu diễn, các thuật.
Rules Statements about objects and their relationships Expess ◦ If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain).
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.
Programming Languages Third Edition Chapter 4 Logic Programming.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
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:... :-....
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
Logic Programming Lecture 2: Unification and proof search.
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),
Pengenalan Prolog Disampaikan Oleh : Yusuf Nurrachman, ST, MMSI.
Section 16.5, 16.6 plus other references
COSC 2P93 Prolog: Debugging
Prolog a declarative language
Selection (also known as Branching) Jumail Bin Taliba by
CS 3304 Comparative Languages
Python: Control Structures
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
3.5 Programming paradigms
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Chapter 11 :: Logic Languages
Algorithm design and Analysis
Prolog a declarative language
Prolog a declarative language
This Lecture Substitution model
Prolog a declarative language
Chapter 12 :: Logic Languages
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Chapter 12 :: Logic Languages
Programming Techniques
Programming Languages 2nd edition Tucker and Noonan
This Lecture Substitution model
This Lecture Substitution model
Chapter 2: Prolog (Introduction and Basic Concepts)
Representations & Reasoning Systems (RRS) (2.2)
Chapter 12 :: Logic Languages
Prolog Based on: Chapter 12 of PLP “Seven languages in seven weeks”
PROLOG.
Presentation transcript:

Tests, Backtracking, and Recursion Artificial Intelligence Programming in Prolog Lecture 3 30/09/04 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

AIPP Lecture 3: Rules, Results, and Backtracking Re-cap A Prolog program consists of predicate definitions. A predicate denotes a property or relationship between objects. Definitions consist of clauses. A clause has a head and a body (Rule) or just a head (Fact). A head consists of a predicate name and arguments. A clause body consists of a conjunction of terms. Terms can be constants, variables, or compound terms. We can set our program goals by typing a command that unifies with a clause head. A goal unifies with clause heads in order (top down). Unification leads to the instantiation of variables to values. If any variables in the initial goal become instantiated this is reported back to the user. 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

Correction: Re-trying Goals When a question is asked with a variable as an argument (e.g. greet(Anybody).) we can ask the Prolog interpreter for multiple answers using: ; ; fails the last clause used and searches down the program for another that matches. It then performs all the tasks contained within the body of the new clause and returns the new value of the variable. greet(hamish):- write(‘How are you doin, pal?’). greet(amelia):- write(‘Awfully nice to see you!’). | ?- greet(Anybody). How are you doin, pal? Anybody = hamish? ; Awfully nice to see you! Anybody = amelia? ; no 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

AIPP Lecture 3: Rules, Results, and Backtracking Tests When we ask Prolog a question we are asking for the interpreter to prove that the statement is true. ?- 5 < 7, integer(bob). yes = the statement can be proven. no = the proof failed because either the statement does not hold, or the program is broken. Error = there is a problem with the question or program. *nothing* = the program is in an infinite loop. We can ask about: Properties of the database: mother(jane,alan). Built-in properties of individual objects: integer(bob). Absolute relationships between objects: Unification: =/2 Arithmetic relationships: <, >, =<, >=, =:=, +, -, *, / Atom is either a constant or a string NOT a variable, integer, list, or compound term. 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

AIPP Lecture 3: Rules, Results, and Backtracking Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t need to be written Comparisons: <, >, =<, >=, =:= (equals), =\= (not equals) = Infix operators: go between two terms. =</2 is used 5 =< 7. (infix) =<(5,7). (prefix)  all infix operators can also be prefixed Equality is different from unification =/2 checks if two terms unify =:=/2 compares the arithmetic value of two expressions ?- X=Y. ?- X=:=Y. ?-X=4,Y=3, X+2 =:= Y+3. yes Instantiation error X=4, Y=3? yes 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

Arithmetic Operators (2) = Infix operators but can also be used as prefix. Need to use is/2 to access result of the arithmetic expression otherwise it is treated as a term: |?- X = 5+4. |?- X is 5+4. X = 5+4 ? X = 9 ? yes yes (Can X unify with 5+4?) (What is the result of 5+4?) Mathematical precedence is preserved: /, *, before +,- Can make compound sums using round brackets Impose new precedence Inner-most brackets first | ?- X is (5+4)*2. X = 18 ? yes | ?- X is 5+4*2. X = 13 ? yes 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

AIPP Lecture 3: Rules, Results, and Backtracking Tests within clauses These operators can be used within the body of a clause: To manipulate values, sum(X,Y,Sum):- Sum is X+Y. To distinguish between clauses of a predicate definition bigger(N,M):- N < M, write(‘The bigger number is ‘), write(M). N > M, write(‘The bigger number is ‘), write(N). N =:= M, write(‘Numbers are the same‘). 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

AIPP Lecture 3: Rules, Results, and Backtracking |?- bigger(5,4). bigger(N,M):- N < M, write(‘The bigger number is ‘), write(M). N > M, write(‘The bigger number is ‘), write(N). N =:= M, write(‘Numbers are the same‘). 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

AIPP Lecture 3: Rules, Results, and Backtracking |?- bigger(5,4). bigger(5,4):- 5 < 4,  fails write(‘The bigger number is ‘), write(M). bigger(N,M):- N > M, write(‘The bigger number is ‘), write(N). N =:= M, write(‘Numbers are the same‘). Backtrack 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

AIPP Lecture 3: Rules, Results, and Backtracking |?- bigger(5,4). bigger(N,M):- N < M, write(‘The bigger number is ‘), write(M). bigger(5,4):- 5 > 4, write(‘The bigger number is ‘), write(N). N =:= M, write(‘Numbers are the same‘). 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

AIPP Lecture 3: Rules, Results, and Backtracking |?- bigger(5,4). bigger(N,M):- N < M, write(‘The bigger number is ‘), write(M). bigger(5,4):- 5 > 4,  succeeds, go on with body. write(‘The bigger number is ‘), write(5). The bigger number is 5 yes |?- Reaches full-stop = clause succeeds 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

AIPP Lecture 3: Rules, Results, and Backtracking |?- bigger(5,5).  If our query only matches the final clause bigger(N,M):- N < M, write(‘The bigger number is ‘), write(M). N > M, write(‘The bigger number is ‘), write(N). bigger(5,5):- 5 =:= 5, write(‘Numbers are the same‘).  Is already known as the first two clauses failed. 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

Backtracking |?- bigger(5,5).  If our query only matches the final clause bigger(N,M):- N < M, write(‘The bigger number is ‘), write(M). N > M, write(‘The bigger number is ‘), write(N). bigger(5,5):- write(‘Numbers are the same‘). Numbers are the same yes  Satisfies the same conditions. Clauses should be ordered according to specificity Most specific at top Universally applicable at bottom 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

AIPP Lecture 3: Rules, Results, and Backtracking Reporting Answers |?- bigger(5,4).  Question is asked The bigger number is 5  Answer is written to terminal yes  Succeeds but answer is lost This is fine for checking what the code is doing but not for using the proof. |?- bigger(6,4), bigger(Answer,5). Instantiation error! To report back answers we need to put an uninstantiated variable in the query, instantiate the answer to that variable when the query succeeds, pass the variable all the way back to the query. 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

AIPP Lecture 3: Rules, Results, and Backtracking Passing Back Answers To report back answers we need to put an uninstantiated variable in the query, | ?- bigger(6,4,Answer),bigger(Answer,5,New_answer). 2. instantiate the answer to that variable when the query succeeds, pass the variable all the way back to the query. 1 3 2 bigger(X,Y,Answer):- X>Y. bigger(X,Y,Answer):- X=<Y. , Answer = X. , Answer = Y. 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

AIPP Lecture 3: Rules, Results, and Backtracking Head Unification To report back answers we need to put an uninstantiated variable in the query, | ?- bigger(6,4,Answer),bigger(Answer,5,New_answer). Or, do steps 2 and 3 in one step by naming the variable in the head of the clause the same as the correct answer. = head unification 1 2 3 bigger(X,Y,X):- X>Y. bigger(X,Y,Y):- X=<Y. 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

AIPP Lecture 3: Rules, Results, and Backtracking Satisfying Subgoals Most rules contain calls to other predicates in their body. These are known as Subgoals. These subgoals can match: facts, other rules, or the same rule = a recursive call 1) drinks(alan,beer). 2) likes(alan,coffee). 3) likes(heather,coffee). 4) likes(Person,Drink):- drinks(Person,Drink).  a different subgoal 5) likes(Person,Somebody):- likes(Person,Drink),  recursive subgoals likes(Somebody,Drink).  30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

Representing Proof using Trees To help us understand Prolog’s proof strategy we can represent its behaviour using AND/OR trees. Query is the top-most point (node) of the tree. Tree grows downwards (looks more like roots!). Each branch denotes a subgoal. The branch is labelled with the number of the matching clause and any variables instantiated when matching the clause head. Each branch ends with either: A successful match , A failed match , or Another subgoal. |?- likes(alan,X). 2 X/coffee 1st solution = “Alan likes coffee.” X = coffee 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

Representing Proof using Trees (2) Using the tree we can see what happens when we ask for another match ( ; ) |?- likes(alan,X). Backtracking 2 X/coffee 4 X = coffee drinks(alan,X). 1st match is failed and forgotten 1 X/beer Tries to match likes(alan,What) again but can’t match a uniary clause so it matches the first rule X = beer 2nd solution = “Alan likes beer because Alan drinks beer.” 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

AIPP Lecture 3: Rules, Results, and Backtracking Recursion using Trees When a predicate calls itself within its body we say the clause is recursing |?- likes(alan,X). Conjoined subgoals 2 5 X/coffee 4 X = coffee drinks(alan,X). likes(alan,Drink) likes(Somebody,Drink) 1 X/beer X/coffee 2 X = beer X = coffee 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

Recursion using Trees (2) When a predicate calls itself within its body we say the clause is recursing |?- likes(alan,X). 2 5 X/coffee 4 X = coffee drinks(alan,X). likes(alan,coffee) likes(Somebody,coffee) 1 X/beer X/coffee 2 Somebody /alan 2 X = beer X = coffee Somebody = alan 3rd solution = “Alan likes Alan because Alan likes coffee.” 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

Recursion using Trees (3) When a predicate calls itself within its body we say the clause is recursing |?- likes(alan,X). 2 5 X/coffee 4 X = coffee drinks(alan,X). likes(alan,coffee) likes(Somebody,coffee) 1 X/beer Somebody / heather X/coffee 2 Somebody /alan 3 X = beer 2 X = coffee Somebody = heather 4th solution = “Alan likes Heather because Heather likes coffee.” Somebody = alan 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

Infitite Recursive Loop If a recursive clause is called with an incorrect goal it will loop as it can neither prove it nor disprove it. likes(Someb,coffee) Somebody = alan 2 3 5 likes(Someb,coffee) Somebody = heather 2 likes(coffee,coffee) Someb = alan likes(coffee,X) likes(coffee,X) likes(coffee,X2) likes(X,X2) likes(coffee,X3) likes(X2,X3) 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking

The central ideas of Prolog SUCCESS/FAILURE any computation can “succeed'' or “fail'', and this is used as a ‘test‘ mechanism. MATCHING any two data items can be compared for similarity, and values can be bound to variables in order to allow a match to succeed. SEARCHING the whole activity of the Prolog system is to search through various options to find a combination that succeeds. Main search tools are backtracking and recursion BACKTRACKING when the system fails during its search, it returns to previous choices to see if making a different choice would allow success. 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking