26
Agile Software Development Lab 2008 Dr. Günter Kniesel, Daniel Speicher, Tobias Rho, Pascal Bihler Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla [email protected] [email protected]

Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla [email protected] [email protected] . Vortragstitel (in „Ansicht -> Master

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Agile Software Development Lab 2008Dr. Günter Kniesel, Daniel Speicher, Tobias Rho, Pascal Bihler

Prolog - Part 1

Spring 2008 R O O T S

Alexis Raptarchis

Patrick Rypalla

[email protected]

[email protected]

Page 2: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 2

Table of Contents

1. Facts, Rules and Queriesa. Prolog examples

b. Prolog Syntax

c. Exercise 1

2. Matchinga. Proof Search

b. Exercise 2

Agile Software Development Lab Spring 2008 R O O T S

b. Exercise 2

3. Recursiona. Exercise 3

4. List

5. Arithmetic

6. Cuts and Negation

7. Meta-Predicates

Page 3: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 3

Facts , Rules, and Queries

� There are only three basic constructs in Prolog: facts, rules, and queries

� Collection of facts and rules is called a knowledge base (KB)

� Queries are questions about the information stored in the KB

� Facts are used to state things that are unconditionally true of the domain of interest

woman(mia).fact

Agile Software Development Lab Spring 2008 R O O T S

domain of interest

� We can ask Prolog whether Mia is a woman by posing a query

� If we ask whether Jody is a woman Prolog will answer no, because Jody is not known to the KB

?- woman(mia).query

Prolog will answer:

yes

Page 4: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 4

Facts, Rules , and Queries

� Rules state information that is conditionally true of the domain of interest

playsAirGuitar(mia) :- listens2Music(mia).

head body

rule

Agile Software Development Lab Spring 2008 R O O T S

� The rule says that Mia plays air guitar if she listens to music

� :- should be read as ``if'', or ``is implied by'‘

� In general rules say: if the body of the rule is true, then the head of the rule is true too

� The body can contain more then one fact, for example:

playsAirGuitar(mia):- listens2music(mia), happy(mia).

Page 5: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 5

Facts, Rules , and Queries

Lets add a fact to our KB, namely

listens2Music(mia).

playsAirGuitar(mia) :- listens2Music(mia).

� We will ask Prolog whether Mia plays air guitar

factrule

Agile Software Development Lab Spring 2008 R O O T S

?- playsAirGuitar(mia).

� Remember playsAirGuitar(mia) Is not a fact in our KB

� But Prolog will respond yes! Hence Prolog can use so called modus ponens to deduce facts from the KB

� This new fact is not explicitly recorded in the knowledge base. It is only implicitly present

Page 6: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 6

Facts, Rules, and Queries

man(vincent).

woman(mia).

woman(jody).

woman(yolanda).

• Prolog answers this query by working from top to bottom through the KB, trying to match the expression woman(X) with the information KB contains.

• Prolog instantiates X to mia, or that it binds X to mia

Agile Software Development Lab Spring 2008 R O O T S

?- woman(X).

X = mia

?- ;

X = jody;

X = yolanda;

no

mia

• ; means or, so this query means: are there any more women?

Page 7: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 7

Facts, Rules, and Queries

happy(yolanda).

listens2music(mia).

listens2music(yolanda):- happy(yolanda).

playsAirGuitar(mia):- listens2music(mia),happy(mia).

playsAirGuitar(yolanda):- listens2music(yolanda).

The facts and rules contained in a KB

are called clauses .

In this case the KB contains 5 clauses, namely 2 facts and

3 rules

Agile Software Development Lab Spring 2008 R O O T S

?- playsAirGuitar(mia).

no

?- playsAirGuitar(yolanda).

yes

Page 8: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 8

Facts, Rules, and Queries

woman(mia).

woman(jody).

loves(vincent, mia).

loves(marsellus, mia).

loves(pumpkin, honey_bunny).

loves(honey_bunny, pumpkin).

Agile Software Development Lab Spring 2008 R O O T S

jealous(X,Y):- loves(X,Z), loves(Y,Z).

?- loves(pumpkin,X), woman(X).

no

loves(marcellus,X),woman(X).

X = mia

?- jealous(marsellus,W).

W = vincent

Page 9: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 9

Prolog Syntax

� What exactly are facts, rules and queries built out of?

Terms

Simple Terms Complex Terms

Terms

Simple Terms Complex Terms

Agile Software Development Lab Spring 2008 R O O T S

Constants Variables

Atoms Numbers

Constants Variables

Atoms Numbers

Page 10: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 10

Prolog Syntax: Atoms and Variables

� Atoms are sequence of characters of upper-case letters, lower-case letters, digits, or underscore, starting with a lowercase letter• Examples: butch , mia , playGuitar

� An arbitrary sequence of characters enclosed in single quotes

• Examples: 'Vincent' , 'Five dollar shake' , '@$%'

Agile Software Development Lab Spring 2008 R O O T S

� A sequence of special characters• Examples: : , ; . :-

� Variables Same as Atoms, just starting with either an uppercase letter or an underscore

� Examples: X, Y, Variable, Vincent, _tag

Page 11: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 11

Prolog Syntax: Complex Terms

� Operators� Implication :-

� Conjunction ,

� Disjunction ;

� Complex Terms� Atoms, numbers and variables are building blocks for complex terms

� Complex terms are built out of a functor directly followed by a sequence of

Agile Software Development Lab Spring 2008 R O O T S

� Complex terms are built out of a functor directly followed by a sequence of arguments

� Arguments are put in round brackets, separated by commas

� The functor must be an atom

� Examples we have seen before:� playsAirGuitar(jody)

� loves(vincent, mia)

� jealous(marsellus, W)

argumentfunctor

Page 12: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 12

Prolog Syntax: Arity

� Complex terms inside complex terms:� hide(X,father(father(father(butch))))

� Functor is hide and it has two arguments: X and the complex term father(father(father(butch)))

� The number of arguments a complex term has is called its arity

Agile Software Development Lab Spring 2008 R O O T S

� Examples:woman(mia) /1 is a term with arity 1 loves(vincent,mia) /2 has arity 2father(father(butch)) /1 arity 1

� In Prolog documentation arity of a predicate is usually indicated with the suffix "/" followed by a number to indicate the arity

Page 13: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 13

Exercise 1

� Which of the following sequences of characters are atoms and which are variables?� vINCENT

� Footmassage

� variable23

� big_kahuna_burger

� 'big kahuna burger'

� Jules

Agile Software Development Lab Spring 2008 R O O T S

� Jules

� ‘@Jules‘

� How many facts, rules and clausesare there in the following knowledge base? What are the heads of the rules, and what are the goals they contain?woman(vincent).woman(mia).man(jules).person(X) :- man(X); woman(X).loves(X,Y) :- knows(Y,X).father(Y,Z) :- man(Y), son(Z,Y).father(Y,Z) :- man(Y), daughter(Z,Y).

Page 14: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 14

Exercise 1 - Solution

� Which of the following sequences of characters are atoms and which are variables?� vINCENT atom

� Footmassage variable

� Variable23 atom

� big_kahuna_burger atom

� 'big kahuna burger‘ atom

� Jules variable

Agile Software Development Lab Spring 2008 R O O T S

� Jules variable

� ‘@Jules‘ atom

� How many facts, rules and clauses are there in the following knowledge base? What are the heads of the rules, and what are the goals they contain?woman(vincent).woman(mia).man(jules).person(X) :- man(X); woman(X).loves(X,Y) :- knows(Y,X).father(Y,Z) :- man(Y), son(Z,Y).father(Y,Z) :- man(Y), daughter(Z,Y).

3 facts

4 rules

7 clauses

Page 15: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 15

Matching

� Two terms match, if they are equal or if they contain variables that can be instantiated in such a way that the resulting terms are equal

Exampels:

� =(mia,mia) match, because they are the same atom

� woman(mia) = woman(mia) match, because they are the same complex term

� mia = X match, because X can be instantiated to mia

Agile Software Development Lab Spring 2008 R O O T S

� How about loves(vincent, X) and loves(X, mia)?

no match, because its impossible to find an instantiation of X

� And does kill(shoot(gun), stab(knife)) = kill (X, stab(Y)) match?

X = shoot(gun)Y = knifeyes

Page 16: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 16

Matching

� Definition of Matching

1. If term1 and term2 are constants, then term1 and term2 match if and only if they are the same atom, or the same number.

2. If term1 is a variable and term2 is any type of term, then term1 and term2 match, and term1 is instantiated to term2.

3. If term1 and term2 are complex terms, then they match if and only if: � They have the same functor and arity.

Agile Software Development Lab Spring 2008 R O O T S

� They have the same functor and arity.

� All their corresponding arguments match

� and the variable instantiations are compatible. (I.e. it is not possible to instantiate variable X to mia, when matching one pair of arguments, and to then instantiate X to vincent, when matching another pair of arguments.)

4. Two terms match if and only if it follows from the previous three clauses that they match.

Page 17: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 17

Matching - occurs check

� Consider the following query: father(X) = X.

Let`s try and instantiate X to father(father(butch)):father(father(father(butch))) = father(father(butch))

� Prolog is desperately trying to match these terms, but it won't succeed.X = father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(father(….

Agile Software Development Lab Spring 2008 R O O T S

(father(father(father(father(father(father(father(father(….

� newer versions of Prolog can detect cycles in termsX = father(father(father(father(father(father(...))))))))))yes

� Now we know about matching� Next: we will learn how Prolog actually searches a KB to see if a query is satisfied

� Proof search

Page 18: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 18

Proof Search

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).

X=b

Y=X

?- g(a), h(a).

X=a

Agile Software Development Lab Spring 2008 R O O T S

?- k(Y).

Y=b;

no

?- g(a), h(a).

?- h(a).

?- g(b), h(b).

?- h(b).

?- g(a), h(a).

?- h(a).

Page 19: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 19

Proof Search

loves(vincent,mia).

loves(marsellus,mia).

jealous(A,B):-

loves(A,C),

loves(B,C).

?- jealous(X,Y).

?- loves(A,C), loves(B,C).

A=vincent

C=mia

A=marsellus

C=mia

X=A Y=B

Agile Software Development Lab Spring 2008 R O O T S

?- jealous(X,Y).

X=marsellus

Y=vincent;

X=vincent

Y=marsellus;

no

?- loves(B,mia).

C=mia

?- loves(B,mia).

C=mia

B=vincent B=vincent

B=marsellus B=marsellus

X = vincent

Y = vincent;

X = vincent

Y = marsellus;

Page 20: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 20

Exercise 2

� Which of the following pairs of terms match? Give the variable instantiations that lead to successful matching.

� food(bread,X,beer) = food(Y,sausage,X)

� food(bread,X,beer) = food(Y,kahuna_burger)

� meal(food(bread),drink(beer)) = meal(X,Y)

� ?- loves(X,X) = loves(marsellus,mia).

Agile Software Development Lab Spring 2008 R O O T S

� ?- loves(X,X) = loves(marsellus,mia).

� ?- k(s(g),Y) = k(X,t(k)).

� ?- k(s(g),t(k)) = k(X,t(Y)).

Page 21: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 21

Exercise 2 - Solution

� food(bread,X,beer) = food(Y,sausage,X)

no

X = beer and X = sausage

� food(bread,X,beer) = food(Y,kahuna_burger)

Agile Software Development Lab Spring 2008 R O O T S

no

Because we have 3 arguments on the left side but only 2 on the right

� meal(food(bread),drink(beer)) = meal(X,Y)

X = food(bread)

Y = drink(beer)Yes

Page 22: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 22

Exercise 2 - Solution

� ?- loves(X,X) = loves(marsellus,mia).

noX = marsellus and X = mia

� ?- k(s(g),Y) = k(X,t(k)).

X=s(g)

Agile Software Development Lab Spring 2008 R O O T S

X=s(g)

Y=t(k)

yes

� ?- k(s(g),t(k)) = k(X,t(Y)).

X=s(g)

Y=k

yes

Page 23: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 23

Recursion

� Prolog predicates can be defined recursively

� A predicate is recursively defined if one or more rules in its definition refers to itself

� Let’s take a look on two rules:

descend(X,Y):- child(X,Y).

Agile Software Development Lab Spring 2008 R O O T S

descend(X,Y):- child(X,Y).

descend(X,Y):- child(X,Z), descend(Z,Y).

� What does this say? 1. if Y is a child of X, then Y is a descendant of X

2. if Z is a child of X, and Y is a descendant of Z, then Y is a descendant of X

Page 24: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 24

Recursion - descend

child(martha, charlotte).

child(charlotte, caroline).

child(caroline, laura).

child(laura, rose).

descend(X,Y):- child(X,Y).

descend(X,Y):-

child(martha,laura)

descend(martha,laura)

child (martha,Y)

descend(Y,laura)

descend(charlotte,laura)

Agile Software Development Lab Spring 2008 R O O T S

child(X,Z),descend(Z,Y).

?- descend(martha, laura)

yes

child(charlotte,laura)

†child (charlotte,Y)

descend(Y,laura)

descend(charlotte,laura)

child(charlotte,laura)

Page 25: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 25

Recursion - successor

� Suppose we use the following way to write numerals:1. 0 is a numeral.

2. If X is a numeral, then so is succ(X) .

� That is, succ(X) represents the number obtained by adding one to the number represented by X

� It simply says that 0 is a numeral, and that all other numerals are built by stacking succ symbols in front.

Agile Software Development Lab Spring 2008 R O O T S

stacking succ symbols in front.

numeral(0).

numeral(succ(X)) :- numeral(X).

Exercise:

What will happen if you ask:

?- numeral(X)

X = 0 ;

X = succ(0) ;

X = succ(succ(0)) ;

X = succ(succ(succ(0))) ;

X = succ(succ(succ(succ(0)))) ;

X = succ(succ(succ(succ(succ(0))))) ;

X = succ(succ(succ(succ(succ(succ(0)))))) ;

Page 26: Prolog - Part 1€¦ · Prolog - Part 1 Spring 2008 R O O T S Alexis Raptarchis Patrick Rypalla raptarch@cs.uni-bonn.de rypalla@cs.uni-bonn.de . Vortragstitel (in „Ansicht -> Master

Vortragstitel (in „Ansicht -> Master änderbar) 26

Exercise 3

child(ron, hermione).

child(hermione, draco).

child(draco, harry).

descend(X,Y):- child(X,Y).

descend(X,Y):-child(X,Z),descend(Z,Y).

child(ron,harry)

descend(ron,harry)

child (ron,Y)

descend(Y,harry)

descend(hermione,harry)

Agile Software Development Lab Spring 2008 R O O T S

?- descend(ron, harry)

child(hermione,harry)

†child (hermione,Y)

descend(Y,harry)

descend(draco,harry)

child(draco,harry)