Unification, Recursion and Lists

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Prolog programming....Dr.Yasser Nada. Chapter 8 Parsing in Prolog Taif University Fall 2010 Dr. Yasser Ahmed nada prolog programming....Dr.Yasser Nada.
Chapter 11 :: Logic Languages
Prolog.
Declarative Programming Lists in PROLOG Autumn 2014.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
© 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.
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).
© 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.
Prolog Recursion Pepper. A rule with that calls itself is_digesting(X,Y) :- just_ate(X,Y). is_digesting(X,Y) :- just_ate(X,Z), is_digesting(Z,Y). 2 rules.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
Prolog: Unification & Recursion © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
© 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 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 4: Lists Theory –Introduce lists, an important recursive data structure often used in Prolog.
Prolog Programming Lecture Module 13. Objective ● What is Prolog? ● Prolog program ● Syntax of Prolog ● Prolog Control Strategy ● Execution of Prolog.
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.
ITEC 380 Organization of programming languages Lecture 9 – Prolog.
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)
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 defining.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
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).
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Lab Lecture#4 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Introduction to Prolog
Introduction to Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.
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:... :-....
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),
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.
Section 16.5, 16.6 plus other references
Prolog Programming.
Chapter 12 :: Logic Languages
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Tests, Backtracking, and Recursion
Prolog Recursion Pepper Major portions credited to :
Chapter 11 :: Logic Languages
PROGRAMMING IN HASKELL
Prolog Search Strategy
© Patrick Blackburn, Johan Bos & Kristina Striegnitz
Chapter 12 :: Logic Languages
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Chapter 12 :: Logic Languages
Programming Paradigms and Languages
Introduction to Prolog
Programming Techniques
Chapter 2 Syntax and meaning of prolog programs
Programming Languages 2nd edition Tucker and Noonan
Chapter 12 :: Logic Languages
Prolog Based on: Chapter 12 of PLP “Seven languages in seven weeks”
Presentation transcript:

Unification, Recursion and Lists Chapter 4 Unification, Recursion and Lists Taif University Fall 2010 Dr. Yasser Ahmed nada

Unification

Unification is the name given to the way Prolog does its matching. A naive view of the matching process might be represented by the question “can the target object be made to fit one of the source objects”. Unification implies mutual coercion. There is an attempt to alter both the target and the current source object to make them look the same. Unification is a two way matching process

Consider how we might match the term book(waverley,X) against some clause for which book(Y, scott) book(waverley,X) book(Y, scott) Unification provides the substitutions X/scott and Y/waverley these substitutions both terms look like book(waverley,scott)

The substitution X/scott and Y/waverley is known as a unifier , the most general unifier If we unify X with Y then one unifier might be the substitution X/1 and Y/1 but this is not the most general unifier.

Success with X=fred, Y=fred possible unifications X=fred fails because you can’t match two distinct atoms jane=fred Y=fred, X=Y Success with X=fred, Y=fred

Success X=happy(jim) Success X=Y

likes(jim,X) = likes(X,fred) Consider a query likes(jim,X). Suppose there is one clause: likes(X,fred) and mentions a variable with the textual name of X So now consider whether the head of the clause likes(X,fred) unifies with the current goal likes(jim,X). Match the first arguments, we get X=jim. Then we try to match the second arguments. Now can X=fred? If X=jim then the answer is no. failed likes(jim,X) = likes(X,fred)

Another possible unifications Success X=M,Y=M Likes(X,Y)=likes(M,M) Success X=M,X=a Likes(X,X)=likes(M,a) Failed 5+1=6 Failed 6=5+1

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

Recall Prolog Terms Terms Simple Terms Complex Terms Constants Variables Atoms Numbers © Patrick Blackburn, Johan Bos & Kristina Striegnitz

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: This also 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 If T1 and T2 are constants, then T1 and T2 unify if they are the same atom, or the same number.

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

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

Prolog unification ?- mia = mia. yes ?-

Prolog unification ?- mia = mia. yes ?- mia = vincent. no ?-

Prolog unification ?- 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.

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

Prolog and unification Prolog does not use a standard unification algorithm Consider the following query: ?- father(X) = X. Do these terms unify or not?

Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Programming with Unification vertical( line(point(X,Y), point(X,Z))). horizontal( line(point(X,Y), point(Z,Y))). ?- © Patrick Blackburn, Johan Bos & Kristina Striegnitz

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 ?- © Patrick Blackburn, Johan Bos & Kristina Striegnitz

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 ?- © Patrick Blackburn, Johan Bos & Kristina Striegnitz

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 ?- © Patrick Blackburn, Johan Bos & Kristina Striegnitz

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 ?- © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Exercise: unification

Recursion

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

One of my ancestors is one of my parents or one of their ancestors. A string of characters is a single character or a single character followed by a string of characters. A paragraph is a sentence or a sentence appended to a paragraph. To decouple a train, uncouple the first carriage and then decouple the rest of the train.

An example recursive program: talks about(A,B):- knows(A,B). talks about(P,R):- knows(P,Q), talks about(Q,R). Rule 1 Rule 2 recursion in Rule2

If you look at the AND/OR tree of the search space you can see that There is a subtree which is the same shape as the whole tree reflecting the single recursive call to talks about/2. The solution of a given problem depends on being able to stop recursing at some point. Because the leftmost path down the tree is not infinite in length it is reasonable to hope for a solution.

Rule 2 Rule 1 Rule 1 Rule 2 talks about(X,Y) talks about(Z,Y) knows(X,Z) knows(X,Y) Rule 1 knows(Z,Y) talks about(Z1,Y) knows(Z,Z1) Rule 1 Rule 2

Goal is talks about(X,Y) Data base talks about(A,B):- knows(A,B). talks about(P,R):- knows(P,Q), talks about(Q,R). knows(bill,jane). knows(jane,pat). knows(jane,fred). knows(fred,bill). Goal is talks about(X,Y)

we get, in the order shown: X= bill Y= jane X= jane Y= pat X= jane Y= fred X= fred Y= bill X= bill Y= pat and so on The search strategy implies that Prolog keep on trying to satisfy the subgoal knows(X,Y) until there are no more solutions to this.

Example 1: Eating isDigesting(X,Y):- justAte(X,Y). isDigesting(X,Y):- justAte(X,Z), isDigesting(Z,Y). justAte(mosquito,blood(john)). justAte(frog,mosquito). justAte(stork,frog). ?-

Example 1: Eating isDigesting(X,Y):- justAte(X,Y). isDigesting(X,Y):- justAte(X,Z), isDigesting(Z,Y). justAte(mosquito,blood(john)). justAte(frog,mosquito). justAte(stork,frog). ?- isDigesting(stork,mosquito).

Another recursive definition p:- p. ?-

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

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

Example 2: Decendant child(bridget,caroline). child(caroline,donna). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), child(Z,Y).

Example 2: Decendant child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), child(Z,Y).

Example 2: Decendant child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), child(Z,Y). ?- descend(anna,donna). no ?-

Example 2: Decendant child(anna,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y):- child(X,Y). descend(X,Y):- child(X,Z), child(Z,Y). descend(X,Y):- child(X,Z), child(Z,U), child(U,Y). ?-

Example 2: Decendant 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). ?-

Example 2: Decendant 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(anna,donna).

Search tree Draw search tree for ?- descend(anna,donna).

Example 3: Successor Suppose we use the following way to write numerals: 0 is a numeral. If X is a numeral, then so is succ(X).

Example 3: Successor numeral(0). numeral(succ(X)):- numeral(X).

Example 3: Successor numeral(0). numeral(succ(X)):- numeral(X). ?- numeral(succ(succ(succ(0)))). yes ?-

Example 3: Successor numeral(0). numeral(succ(X)):- numeral(X).

Example 3: Successor numeral(0). numeral(succ(X)):- numeral(X). X=succ(0); X=succ(succ(0)); X=succ(succ(succ(0))); X=succ(succ(succ(succ(0))))

Example 4: Addition add(0,X,X). %%% base clause

Example 4: Addition add(0,X,X). %%% base clause add(succ(X),Y,succ(Z)):- %%% recursive clause add(X,Y,Z). ?- add(succ(succ(0)),succ(succ(succ(0))), Result). Result=succ(succ(succ(succ(succ(0))))) yes

Exercises

Lists

Lists, for now, can be regarded as special Prolog structures that can be used to represent an ordered sequence of Prolog terms. For example [ice cream, coffee, chocolate] a list with three elements (all atoms) [a, b, c, c, d, e] a list with six elements (all atoms) [ ] a list with no elements in it (it is an atom) [dog(fido), cat(rufus), goldfish(jimmy)] a list with three elements (all Prolog terms) [happy(fred),[ice cream,chocolate],[1,[2],3]] a list with three elements!

Note that the “,” used in the construction of a list is just an argument separator as in the term foo(a,b). Also note that, because order is preserved, the list [a,b,c] is not the same as [a,c,b].

How to construct/deconstruct a list

[X|Y] = [f,r,e,d] result is X=f Y=[r,e,d] Head Tail The basic approach provides a simple way of splitting a list into two bits: the first element (if there is one!) and the rest of the list. The corresponding way of joining two bits to form a list requires taking an element and a list and inserting the element at the front of the list. List Destruction: first, we show how to remove the first element from a list. [X|Y] = [f,r,e,d] result is X=f Y=[r,e,d] Head Tail

List Construction: the construction of a list is the reverse: take a variable bound to any old list say, X=[r, e, d] and add the element, say, b at the front with: Result Wanted = [b|X] Bigger Chunks: it is possible to add (or take away) bigger chunks onto (from) the front of a list than one element at a time. Suppose you want to stick the elements a, b and c onto the front of the list X to make a new list Y. then this can be done with Y=[a,b,c|X].

suppose you want to take three elements off the front of a list X in such a way that the remaining list, Y, is available for use. This can be done with X=[A,B,C|Y] A limitation of this approach is that there is no direct way of evading specifying how many elements to attach/rip off.

The Empty List Simply written [ ] This list ([ ]) has no elements in it: it cannot therefore be destructed. The empty list ([ ]) is an atom.

Some Possible Matches [b,a,d]=[d,a,b] [X]=[b,a,d] [X|Y]=[he,is,a,cat] fails —as the order matters [b,a,d]=[d,a,b] fails —the two lists are of different lengths [X]=[b,a,d] succeeds with X=he, Y=[is,a,cat] [X|Y]=[he,is,a,cat] succeeds with X=a, Y=b, Z=[c,d] [X,Y|Z]=[a,b,c,d]

succeeds with X=[a], Y=[] fails —the empty list can’t be deconstructed [X|Y]=[] succeeds with X=[a,[b,c]], Y=[d] [X|Y]=[[a,[b,c]],d] succeeds with X=[a], Y=[] [X|Y]=[a]

A Recursive Program Using Lists We make use of a built-in predicate called write/1 to write out all the elements of a list in order write/1 is a side-effecting predicate. It captures the logical relation of always being true but it also produces output which has no part to play in the logical interpretation. It is therefore hard to produce a declarative reading for this predicate despite its utility from the procedural point of view. There are a fair number of other predicates which suffer from this problem including consult/1 and reconsult/1.

To write out a list of terms, write out the first element and then write out the remainder (the tail). print a list([]). print a list([H|T]):- write(H), print a list(T). Note that this can be improved by printing a space between elements of the list. This requires you to add the subgoal write(’ ’) into the body of the second clause and before the recursive call to print a list/1. This will write the elements out on a single line. If you wanted to write each element on a different line then you would need the built-in predicate nl/0.

What You Should Be Able To Do You should be able to determine whether or not two Prolog terms unify. You should be able to identify programs that are recursive. You should be able to build and take apart list structures. You should be able to write simple list processing programs using recursion.

End Of The Chapter