Prolog: Unification & Recursion © Patrick Blackburn, Johan Bos & Kristina Striegnitz.

Slides:



Advertisements
Similar presentations
© Johan Bos Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical work [lab] Teaching material –Learn Prolog.
Advertisements

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 1 Introduction to Prolog.
1 COMP313A Programming Languages Logic Programming (3)
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 11: Database Manipulation and Collecting Solutions Theory –Discuss database manipulation in.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 11: Database Manipulation and Collecting Solutions Exercises –Solutions to exercises LPN chapter.
Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms,
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3: Recursion Theory –Introduce recursive definitions in Prolog –Four examples –Show that there.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3: Recursion Theory –Introduce recursive definitions in Prolog –Four examples –Show that there.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog (II) Lecture 22 – Prolog (II), Spring CSE3302 Programming Languages,
Declarative Programming Autumn 2014 Basic syntax and sample programs.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 2 Theory –Unification –Unification in Prolog –Proof search Exercises –Correction exercises.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 5: Arithmetic Theory –Introduce Prolog`s built-in abilities for performing arithmetic –Apply.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
1 Example 1 Explain why the Intermediate Value Theorem does or does not apply to each of the following functions. (a) f(x) = 1/x with domain [1,2]. Solution.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Horn clauses A literal is an atomic formula or its negation A clause is a disjunction of literals.
CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.
LING 388: Language and Computers Sandiway Fong Lecture 5.
LING 388: Language and Computers Sandiway Fong Lecture 4.
Agile Software Development Lab 2008 Dr. Günter Kniesel, Daniel Speicher, Tobias Rho, Pascal Bihler Spring 2008 R O O T S Prolog - Part 1 Alexis Raptarchis.
ITEC 380 Organization of programming languages Lecture 9 – Prolog.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3 “Input & Output”, Negation, Search.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
CSC 270 – Survey of Programming Languages Prolog Lecture 2 – Unification and Proof Search.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
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).
Lab Lecture#4 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
4.2 – The Mean Value Theorem
Introduction to Prolog
Introduction to Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog Lecture 21 – Prolog, Spring CSE3302 Programming Languages, UT-Arlington.
CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.
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 Dr. Yasser Nada Fall 2010/2011 Lecture 3 1 Logic Programming.
Instructor :Bayan Ghozlan الاستاذه : بيان غزلان.  البرولوغ Prolog هي لغة برمجة منطقية. وتعني برمجه المنطق programing language  تم اختراع اللغة بواسطة.
Pengenalan Prolog Disampaikan Oleh : Yusuf Nurrachman, ST, MMSI.
علمتني الثلوج أن أكون كبياض الثلج لا يحمل قلبي غير الحب والعطف والرحمة لا أسيئ الظن بالأخرين ولا أحكم عليهم من مظهرهم فإذا لم تعاشرهم فأنت تجهلهم !
Matching Matching and unification. Rules for matching terms. Examples of matching. –The most general instantiation. Computation using matching.
Warm up f(x) = 3x + 5, g(x) = x – 15, h(x) = 5x, k(x) = -9
Unification, Recursion and Lists
Prolog Recursion Pepper Major portions credited to :
Chapter 11 :: Logic Languages
Prolog syntax + Unification
Prolog: cut & fail © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Prolog Search Strategy
Part (a) 1 1 ax ax ax 2 g(x) = e + f(x) g’(x) = e (ln e) (a) + f’(x)
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Chapter 12 :: Logic Languages
4.6 The Mean Value Theorem.
Introduction to Prolog
Chapter 12 :: Logic Languages
Prolog Based on: Chapter 12 of PLP “Seven languages in seven weeks”
Presentation transcript:

Prolog: Unification & Recursion © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Unification Recall previous example, where we said that Prolog unifies woman(X) with woman(mia) thereby instantiating the variable X with the atom mia.

Unification Working definition: Two terms unify if they are the same term or if they contain variables that can be uniformly instantiated with terms in such a way that the resulting terms are equal

Unification This means that: mia and mia unify 42 and 42 unify woman(mia) and woman(mia) unify This also means that: vincent and mia do not unify woman(mia) and woman(jody) do not unify

Unification What about the terms: mia and X

Unification What about the terms: mia and X woman(Z) and woman(mia)

Unification What about the terms: mia and X woman(Z) and woman(mia) loves(mia,X) and loves(X,vincent)

Instantiations When Prolog unifies two terms it performs all the necessary instantiations, so that the terms are equal afterwards This makes unification a powerful programming mechanism

Revised Definition 1/3 1.If T 1 and T 2 are constants, then T 1 and T 2 unify if they are the same atom, or the same number.

Revised Definition 2/3 1.If T 1 and T 2 are constants, then T 1 and T 2 unify if they are the same atom, or the same number. 2.If T 1 is a variable and T 2 is any type of term, then T 1 and T 2 unify, and T 1 is instantiated to T 2. (and vice versa)

Revised Definition 3/3 1.If T 1 and T 2 are constants, then T 1 and T 2 unify if they are the same atom, or the same number. 2.If T 1 is a variable and T 2 is any type of term, then T 1 and T 2 unify, and T 1 is instantiated to T 2. (and vice versa) 3.If T 1 and T 2 are complex terms then they unify if: a)They have the same functor and arity, and b)all their corresponding arguments unify, and c)the variable instantiations are compatible.

Prolog unification: =/2 ?- mia = mia. yes ?-

Prolog unification: =/2 ?- mia = mia. yes ?- mia = vincent. no ?-

Prolog unification: =/2 ?- mia = X. X=mia yes ?-

How will Prolog respond? ?- X=mia, X=vincent.

How will Prolog respond? ?- X=mia, X=vincent. no ?- Why? After working through the first goal, Prolog has instantiated X with mia, so that it cannot unify it with vincent anymore. Hence the second goal fails.

Example with complex terms ?- k(s(g),Y) = k(X,t(k)).

Example with complex terms ?- k(s(g),Y) = k(X,t(k)). X=s(g) Y=t(k) yes ?-

Example with complex terms ?- k(s(g),t(k)) = k(X,t(Y)).

Example with complex terms ?- k(s(g),t(k)) = k(X,t(Y)). X=s(g) Y=k yes ?-

One last example ?- loves(X,X) = loves(marsellus,mia).

Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))).

Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). ?-

Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). ?- vertical(line(point(1,1),point(1,3))). yes ?-

Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). ?- vertical(line(point(1,1),point(1,3))). yes ?- vertical(line(point(1,1),point(3,2))). no ?-

Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). ?- horizontal(line(point(1,1),point(1,Y))). Y = 1; no ?-

Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). ?- horizontal(line(point(2,3),Point)). Point = point(_554,3); no ?-

Proof Search Now that we know about unification, we are in a position to learn how Prolog searches a knowledge base to see if a query is satisfied. In other words: we are ready to learn about proof search

Example f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y).

Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y).

Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- f(X), g(X), h(X). Y=X

Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- f(X), g(X), h(X). ?- g(a), h(a). X=a Y=X

Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- f(X), g(X), h(X). ?- g(a), h(a). ?- h(a). X=a Y=X

Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- f(X), g(X), h(X). ?- g(a), h(a). ?- h(a). X=a † Y=X

Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- f(X), g(X), h(X). ?- g(a), h(a). ?- h(a). X=a ?- g(b), h(b). X=b † Y=X

Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). ?- f(X), g(X), h(X). ?- g(a), h(a). ?- h(a). X=a ?- g(b), h(b). X=b ?- h(b). † Y=X

Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=b ?- k(Y). ?- f(X), g(X), h(X). ?- g(a), h(a). ?- h(a). X=a ?- g(b), h(b). X=b ?- h(b). † Y=X

Example: search tree f(a). f(b). g(a). g(b). h(b). k(X):- f(X), g(X), h(X). ?- k(Y). Y=b; no ?- ?- k(Y). ?- f(X), g(X), h(X). ?- g(a), h(a). ?- h(a). X=a ?- g(b), h(b). X=b ?- h(b). † Y=X

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y).

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y).

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). ?- loves(A,C), loves(B,C). X=AY=B

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). ?- loves(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia X=AY=B

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). X=vincent Y=vincent ?- jealous(X,Y). ?- loves(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia B=vincent X=AY=B

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). X=vincent Y=vincent; X=vincent Y=marsellus ?- jealous(X,Y). ?- loves(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia B=vincent B=marsellus X=AY=B

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). ?- jealous(X,Y). X=vincent Y=vincent; X=vincent Y=marsellus; ?- jealous(X,Y). ?- loves(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia ?- loves(B,mia). A=marsellus C=mia B=vincent B=marsellus X=AY=B

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). …. X=vincent Y=marsellus; X=marsellus Y=vincent ?- jealous(X,Y). ?- loves(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia ?- loves(B,mia). A=marsellus C=mia B=vincent B=marsellus X=AY=B

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). …. X=marsellus Y=vincent; X=marsellus Y=marsellus ?- jealous(X,Y). ?- loves(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia ?- loves(B,mia). A=marsellus C=mia B=vincent B=marsellus X=AY=B

Another example loves(vincent,mia). loves(marsellus,mia). jealous(A,B):- loves(A,C), loves(B,C). …. X=marsellus Y=vincent; X=marsellus Y=marsellus; no ?- jealous(X,Y). ?- loves(A,C), loves(B,C). ?- loves(B,mia). A=vincent C=mia ?- loves(B,mia). A=marsellus C=mia B=vincent B=marsellus X=AY=B

Recursive Definitions Prolog predicates can be defined recursively A predicate is recursively defined if one or more rules in its definition refers to itself

Example 1: Ancestor parent(john,paul). parent(paul,tom). parent(tom,mary). ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y). ?-

Example 1: Ancestor parent(john,paul). parent(paul,tom). parent(tom,mary). ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y). ?- ancestor(john,mary).

Example 1: Ancestor parent(john,paul). parent(paul,tom). parent(tom,mary). ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y). ?- ancestor(john,mary). yes

Example 1: Ancestor parent(john,paul). parent(paul,tom). parent(tom,mary). ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y). ?- ancestor(john,mary). yes ?- ancestor(john,X).

Example 1: Ancestor parent(john,paul). parent(paul,tom). parent(tom,mary). ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y). ?- ancestor(john,mary). yes ?- ancestor(john,X). X = paul ; X = tom ; X = mary ; no

Another recursive definition p:- p. ?-

Another recursive definition p:- p. ?- p.

Another recursive definition p:- p. ?- p. ERROR: out of memory

descend1.pl child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), descend(Z,Y). ?- descend(A,B). A=anna B=bridget

descend2.pl child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Z), descend(Z,Y). descend(X,Y):- child(X,Y). ?- descend(A,B). A=anna B=emily

descend3.pl child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- descend(Z,Y), child(X,Z). descend(X,Y):- child(X,Y). ?- descend(A,B). ERROR: OUT OF LOCAL STACK

Summary of this lecture In this lecture we have – unification – introduced search trees – Recursion