General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf ·...
Transcript of General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf ·...
![Page 1: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/1.jpg)
Sebastian Wandelt
WBI, Humboldt-Universität zu Berlin
General Game Playing (GGP) Winter term 2013/2014 2. Prolog / Datalog
![Page 2: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/2.jpg)
General Game Playing: Winter term 2013/2014
Organization: Group finding
• Do you need help finding a group?
![Page 3: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/3.jpg)
General Game Playing: Winter term 2013/2014
Organization: Examination
• Three options:
– Oral
– Written
– Special examination such that
1. You pass the course if you give a short presentation about your
approach (each group around 5-10 minutes) and hand in a small
abstract describing your solution(around 1 A4 page)
2. Your actual grade is determined by a multiple-choice test with 30
questions (per student, not per group!)
Which one do you prefer?
![Page 4: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/4.jpg)
General Game Playing: Winter term 2013/2014
Outline
Date What will we do? 22.10.2013 Introduction, Repetition propositional logic and FOL 29.10.2013 Repetition FOL / Datalog and Prolog 05.11.2013 Game Description Language 12.11.2013 Design of GDL games 19.11.2013 Search Algorithms 1 26.11.2013 Search Algorithms 2 03.12.2013 Incomplete information 10.12.2013 Fluent Calculus and Fluxplayer 17.12.2013 Midterm competition 14.01.2014 Meta-Gaming 21.01.2014 Game Theory 28.01.2014 ? 04.02.2014 Final Competition 11.02.2014 Exam
![Page 5: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/5.jpg)
General Game Playing: Winter term 2013/2014
Prolog:
By example
![Page 6: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/6.jpg)
General Game Playing: Winter term 2013/2014
Basic workflow when using Prolog
1. Describe the situation of interest as a set of facts and
rules
2. Ask a question
3. Prolog logically deduces new facts about the situation we
described
4. Prolog gives us its deductions back as answers
![Page 7: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/7.jpg)
General Game Playing: Winter term 2013/2014
A simple example of a prolog database
We have four facts:
female(mutti).
male(peerlusconi).
male(obami).
male(westerwave).
![Page 8: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/8.jpg)
General Game Playing: Winter term 2013/2014
A simple example of a prolog database
Let us add four more facts:
hasTelephone(mutti).
hasTelephone(peerlusconi).
usesTelephone(mutti)
controls(obami,nsa) %Facts can have arbitrary arity
Keep in mind, we still have:
female(mutti).
male(peerlusconi).
male(obami).
male(westerwave).
![Page 9: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/9.jpg)
General Game Playing: Winter term 2013/2014
A simple example of a prolog database
Let us add a simple rule:
eavesdrops(X,Y):-
controls(X,nsa),hasTelephone(Y),usesTelephone(Y).
What could that mean?
![Page 10: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/10.jpg)
General Game Playing: Winter term 2013/2014
A simple example of a prolog database
Let us add a simple rule:
eavesdrops(X,Y):-
controls(X,nsa),hasTelephone(Y),usesTelephone(Y).
What could rule that mean?
Anybody who controls nsa, eavesdrops everybody who has a
telephone and uses the telephone.
![Page 11: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/11.jpg)
General Game Playing: Winter term 2013/2014
A simple example of a prolog database
eavesdrops(X,Y):-
controls(X,nsa),hasTelephone(Y),usesTelephone(Y).
hasTelephone(mutti).
hasTelephone(peerlusconi).
usesTelephone(mutti)
controls(obami,nsa)
female(mutti).
male(peerlusconi).
male(obami).
male(westerwave).
Task:
Name, by intuition, all pairs of the eavesdrops relationship!
![Page 12: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/12.jpg)
General Game Playing: Winter term 2013/2014
A simple example of a prolog database
eavesdrops(X,Y):-
controls(X,nsa),hasTelephone(Y),usesTelephone(Y).
hasTelephone(mutti).
hasTelephone(peerlusconi).
usesTelephone(mutti)
controls(obami,nsa)
female(mutti).
male(peerlusconi).
male(obami).
male(westerwave).
Task:
Name, by intuition, all pairs of the eavesdrops relationship!
Result:
eavesdrops(obami,mutti)
![Page 13: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/13.jpg)
General Game Playing: Winter term 2013/2014
A simple example of a prolog database
eavesdrops(X,Y):-
controls(X,nsa),hasTelephone(Y),usesTelephone(Y).
hasTelephone(mutti).
hasTelephone(peerlusconi).
usesTelephone(mutti)
controls(obami,nsa)
female(mutti).
male(peerlusconi).
male(obami).
male(westerwave).
Task:
What needs to be changed/added such that we have eavesdrops(obami,peerusconi)?
At least three alternatives …
![Page 14: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/14.jpg)
General Game Playing: Winter term 2013/2014
Prolog implementation
• We will use SWI-Prolog from http://www.swi-prolog.org
• Cheat sheet:
– Add a fact/rule at top-level: assert(…).
– Remove a fact/rule at top-level: retract(…)
– Load a file with facts/rules: [‘D:\pl\test.pl’].
– Remove the definition of a fact/rule: retract/retracall
– Print the definition of a binary function f: listing(f/2).
– Find next solution: hit ; key
– Auto-completion: hit tabulator key
– DON’T forget “.” at the end!
![Page 15: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/15.jpg)
General Game Playing: Winter term 2013/2014
Prolog:
Formal introduction
![Page 16: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/16.jpg)
General Game Playing: Winter term 2013/2014
Prolog Syntax
• What exactly are facts, rules and queries built from?
Terms
Simple Terms Complex Terms
Constants Variables
Atoms Numbers
Terms
Simple Terms Complex Terms
Constants Variables
Atoms Numbers
![Page 17: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/17.jpg)
General Game Playing: Winter term 2013/2014
Atoms
• A sequence of characters of upper-case letters, lower-case letters, digits, or underscore, starting with a lowercase letter • Examples: butch, big_kahuna_burger, playGuitar
• An arbitrary sequence of characters enclosed in single quotes • Examples: 'Vincent', 'Five dollar shake', '@$%'
![Page 18: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/18.jpg)
General Game Playing: Winter term 2013/2014
Numbers
• Integers: 12, -34, 22342
• Floats: 34573.3234
![Page 19: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/19.jpg)
General Game Playing: Winter term 2013/2014
Prolog Syntax
• What exactly are facts, rules and queries built out of?
Terms
Simple Terms Complex Terms
Constants Variables
Atoms Numbers
Terms
Simple Terms Complex Terms
Constants Variables
Atoms Numbers
![Page 20: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/20.jpg)
General Game Playing: Winter term 2013/2014
Variables
• A sequence of characters of upper-case letters, lower-case
letters, digits, or underscore, starting with either an
uppercase letter or an underscore
• Examples:
X, Y, Variable, Vincent, _tag
![Page 21: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/21.jpg)
General Game Playing: Winter term 2013/2014
Prolog Syntax
• What exactly are facts, rules and queries built out of?
Terms
Simple Terms Complex Terms
Constants Variables
Atoms Numbers
Terms
Simple Terms Complex Terms
Constants Variables
Atoms Numbers
![Page 22: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/22.jpg)
General Game Playing: Winter term 2013/2014
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 arguments
• Arguments are put in round brackets, separated by
commas
• The functor must be an atom
![Page 23: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/23.jpg)
General Game Playing: Winter term 2013/2014
Examples of complex terms
• Examples:
– playsAirGuitar(jody)
– loves(vincent, mia)
– jealous(marsellus, W)
• Complex terms inside complex terms:
– hide(X,hasfather(X,Y))
![Page 24: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/24.jpg)
General Game Playing: Winter term 2013/2014
Arity
• The number of arguments a complex term has is called its
arity
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
![Page 25: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/25.jpg)
General Game Playing: Winter term 2013/2014
Arity is important
• In Prolog you can define two predicates with the same
functor but with different arity
• Prolog would treat this as two different predicates
• In Prolog documentation arity of a predicate is usually
indicated with the suffix "/" followed by a number to indicate
the arity
![Page 26: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/26.jpg)
General Game Playing: Winter term 2013/2014
Facts/rules/queries
• Fact:
– Complex term
• Rule:
– A:- B,C,D,…
– Where A and B and C and D and … are complex terms
– Intuition: If B and C and D and … are true, then A is true
– (There is more, but we only discuss this later)
• Query:
– Complex term
![Page 27: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/27.jpg)
General Game Playing: Winter term 2013/2014
Exercise
Task:
Define a Prolog knowledge base, consisting of facts and rules, describing the Simpsons family.
Use your imagination to define interesting rules!
![Page 28: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/28.jpg)
General Game Playing: Winter term 2013/2014
Prolog:
Unification
![Page 29: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/29.jpg)
General Game Playing: Winter term 2013/2014
Unification
• Unification (just as in FOL) is one of the most important
operations of any Prolog implementation
• For instance, Prolog unifies
woman(X)
with
woman(mia)
thereby instantiating the variable X with the atom mia.
![Page 30: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/30.jpg)
General Game Playing: Winter term 2013/2014
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
![Page 31: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/31.jpg)
General Game Playing: Winter term 2013/2014
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
![Page 32: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/32.jpg)
General Game Playing: Winter term 2013/2014
Unification
• What about the terms:
• luis and X
![Page 33: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/33.jpg)
General Game Playing: Winter term 2013/2014
Unification
• What about the terms:
• luis and X
• woman(Z) and woman(mia)
![Page 34: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/34.jpg)
General Game Playing: Winter term 2013/2014
Unification
• What about the terms:
• luis and X
• woman(Z) and woman(mia)
• loves(mia,X) and loves(X,vincent)
![Page 35: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/35.jpg)
General Game Playing: Winter term 2013/2014
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
![Page 36: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/36.jpg)
General Game Playing: Winter term 2013/2014
Revised Definition 1/3
1. If T1 and T2 are constants, then
T1 and T2 unify if they are the same atom, or the
same number.
![Page 37: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/37.jpg)
General Game Playing: Winter term 2013/2014
Revised Definition 2/3
1. If T1 and T2 are constants, then
T1 and T2 unify if they are the same atom, or the
same number.
2. 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)
![Page 38: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/38.jpg)
General Game Playing: Winter term 2013/2014
Revised Definition 3/3
1. If T1 and T2 are constants, then T1 and T2 unify if they are the same atom, or the same number.
2. 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)
3. If T1 and T2 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.
![Page 39: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/39.jpg)
General Game Playing: Winter term 2013/2014
Prolog unification: =/2
?- mia = mia.
yes
?-
![Page 40: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/40.jpg)
General Game Playing: Winter term 2013/2014
Prolog unification: =/2
?- mia = mia.
yes
?- mia = vincent.
no
?-
![Page 41: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/41.jpg)
General Game Playing: Winter term 2013/2014
Prolog unification: =/2
?- mia = X.
X=mia
yes
?-
![Page 42: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/42.jpg)
General Game Playing: Winter term 2013/2014
How will Prolog respond?
?- X=mia, X=vincent.
![Page 43: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/43.jpg)
General Game Playing: Winter term 2013/2014
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.
![Page 44: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/44.jpg)
General Game Playing: Winter term 2013/2014
Example with complex terms
?- k(s(g),Y) = k(X,t(k)).
Task:
Unifiable? If yes, what are possible variable bindings after unification?
![Page 45: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/45.jpg)
General Game Playing: Winter term 2013/2014
Example with complex terms
?- k(s(g),Y) = k(X,t(k)).
X=s(g)
Y=t(k)
yes
?-
![Page 46: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/46.jpg)
General Game Playing: Winter term 2013/2014
Prolog and unification
• Prolog does not use a standard unification algorithm
• Consider the following query:
?- father(X) = X.
• Do these terms unify or not?
![Page 47: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/47.jpg)
General Game Playing: Winter term 2013/2014
Infinite terms
?- father(X) = X.
X=father(father(father(father(father(father(father(father(father(
father(father(father(father(father(father(father(father(father(
father(father(father(father(father(father(father(father(father(
father(father(father(father(father(father(father(father(father(
father(father(father(father(father(father(father(father(father(
father(father(father(
![Page 48: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/48.jpg)
General Game Playing: Winter term 2013/2014
Infinite terms
?- father(X) = X.
X=father(father(father(…))))
yes
?-
![Page 49: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/49.jpg)
General Game Playing: Winter term 2013/2014
Occurs Check
• Prolog does, by default, not perform an occurs check
• Why does it make sense to leave out the occurs check?
![Page 50: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/50.jpg)
General Game Playing: Winter term 2013/2014
Occurs Check
• Prolog does, by default, not perform an occurs check
• Why does it make sense to leave out the occurs check?
• Answer
– For reasons of efficiency
– When unifying a term t1 with a term t2, the time complexity is
reduced from O(|t1|)+O(|t2|) to min(O(|t1|), O(|t2|)).
![Page 51: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/51.jpg)
General Game Playing: Winter term 2013/2014
Occurs Check
• In Prolog, you can enforce the occurs check seperatly:
?- unify_with_occurs_check(father(X), X).
no
![Page 52: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/52.jpg)
General Game Playing: Winter term 2013/2014
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)).
What will be the answer of Prolog?
![Page 53: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/53.jpg)
General Game Playing: Winter term 2013/2014
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
?-
![Page 54: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/54.jpg)
General Game Playing: Winter term 2013/2014
Prolog:
Reasoning
![Page 55: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/55.jpg)
General Game Playing: Winter term 2013/2014
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
![Page 56: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/56.jpg)
General Game Playing: Winter term 2013/2014
Example
f(a).
f(b).
g(a).
g(b).
h(b).
k(X):- f(X), g(X), h(X).
?- k(Y).
![Page 57: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/57.jpg)
General Game Playing: Winter term 2013/2014
Example: search tree
f(a).
f(b).
g(a).
g(b).
h(b).
k(X):- f(X), g(X), h(X).
?- k(Y).
?- k(Y).
![Page 58: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/58.jpg)
General Game Playing: Winter term 2013/2014
Example: search tree
f(a).
f(b).
g(a).
g(b).
h(b).
k(X):- f(X), g(X), h(X).
?- k(Y).
?- k(Y).
?- f(X), g(X), h(X).
Y=X
![Page 59: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/59.jpg)
General Game Playing: Winter term 2013/2014
Example: search tree
f(a).
f(b).
g(a).
g(b).
h(b).
k(X):- f(X), g(X), h(X).
?- k(Y).
?- k(Y).
?- f(X), g(X), h(X).
?- g(a), h(a).
X=a
Y=X
![Page 60: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/60.jpg)
General Game Playing: Winter term 2013/2014
Example: search tree
f(a).
f(b).
g(a).
g(b).
h(b).
k(X):- f(X), g(X), h(X).
?- k(Y).
?- k(Y).
?- f(X), g(X), h(X).
?- g(a), h(a).
?- h(a).
X=a
Y=X
![Page 61: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/61.jpg)
General Game Playing: Winter term 2013/2014
Example: search tree
f(a).
f(b).
g(a).
g(b).
h(b).
k(X):- f(X), g(X), h(X).
?- k(Y).
?- k(Y).
?- f(X), g(X), h(X).
?- g(a), h(a).
?- h(a).
X=a
†
Y=X
![Page 62: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/62.jpg)
General Game Playing: Winter term 2013/2014
Example: search tree
f(a).
f(b).
g(a).
g(b).
h(b).
k(X):- f(X), g(X), h(X).
?- k(Y).
?- k(Y).
?- f(X), g(X), h(X).
?- g(a), h(a).
?- h(a).
X=a
?- g(b), h(b).
X=b
†
Y=X
![Page 63: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/63.jpg)
General Game Playing: Winter term 2013/2014
Example: search tree
f(a).
f(b).
g(a).
g(b).
h(b).
k(X):- f(X), g(X), h(X).
?- k(Y).
?- 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
![Page 64: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/64.jpg)
General Game Playing: Winter term 2013/2014
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
![Page 65: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/65.jpg)
General Game Playing: Winter term 2013/2014
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
![Page 66: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/66.jpg)
General Game Playing: Winter term 2013/2014
Another example
loves(vincent,mia).
loves(marsellus,mia).
jealous(A,B):-
loves(A,C),
loves(B,C).
?- jealous(X,Y).
![Page 67: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/67.jpg)
General Game Playing: Winter term 2013/2014
Another example
loves(vincent,mia).
loves(marsellus,mia).
jealous(A,B):-
loves(A,C),
loves(B,C).
?- jealous(X,Y).
?- jealous(X,Y).
![Page 68: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/68.jpg)
General Game Playing: Winter term 2013/2014
Another example
loves(vincent,mia).
loves(marsellus,mia).
jealous(A,B):-
loves(A,C),
loves(B,C).
?- jealous(X,Y).
?- jealous(X,Y).
?- loves(A,C), loves(B,C).
X=A
Y=B
![Page 69: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/69.jpg)
General Game Playing: Winter term 2013/2014
Another example
loves(vincent,mia).
loves(marsellus,mia).
jealous(A,B):-
loves(A,C),
loves(B,C).
?- jealous(X,Y).
?- jealous(X,Y).
?- loves(A,C), loves(B,C).
?- loves(B,mia).
A=vincent
C=mia
X=A
Y=B
![Page 70: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/70.jpg)
General Game Playing: Winter term 2013/2014
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=A
Y=B
![Page 71: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/71.jpg)
General Game Playing: Winter term 2013/2014
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=A
Y=B
![Page 72: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/72.jpg)
General Game Playing: Winter term 2013/2014
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=A
Y=B
![Page 73: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/73.jpg)
General Game Playing: Winter term 2013/2014
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=vincent
B=marsellus
X=A
Y=B
![Page 74: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/74.jpg)
General Game Playing: Winter term 2013/2014
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=vincent
B=marsellus B=marsellus
X=A
Y=B
![Page 75: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/75.jpg)
General Game Playing: Winter term 2013/2014
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=vincent
B=marsellus B=marsellus
X=A
Y=B
![Page 76: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/76.jpg)
General Game Playing: Winter term 2013/2014
Exercise
Task: Solve the puzzle “Tower of Hanoi” using Prolog.
For three disks, the output of your program should similar to:
?- hanoi(3,left,middle,right).
Move top disk from left to middle
Move top disk from left to right
Move top disk from middle to right
Move top disk from left to middle
Move top disk from right to left
Move top disk from right to middle
Move top disk from left to middle
![Page 77: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/77.jpg)
General Game Playing: Winter term 2013/2014
Prolog:
Collecting solutions
![Page 78: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/78.jpg)
General Game Playing: Winter term 2013/2014
Consider this database
child(martha,charlotte).
child(charlotte,caroline).
child(caroline,laura).
child(laura,rose).
descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z),
descend(Z,Y).
?- descend(martha,X).
X=charlotte;
X=caroline;
X=laura;
X=rose;
no
![Page 79: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/79.jpg)
General Game Playing: Winter term 2013/2014
Collecting solutions
• There may be many solutions to a Prolog query
• However, Prolog generates solutions one by one
• Sometimes we would like to have all the solutions to a
query in one go
=>
• Prolog has three built-in predicates that do this: findall/3,
bagof/3 and setof/3
• In essence, all these predicates collect all the solutions to a
query and put them into a single list
• But there are important differences between them
![Page 80: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/80.jpg)
General Game Playing: Winter term 2013/2014
findall/3
• The query
produces a list L of all the objects O that satisfy the goal G
– Always succeeds
– Unifies L with empty list if G cannot be satisfied
?- findall(O,G,L).
![Page 81: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/81.jpg)
General Game Playing: Winter term 2013/2014
A findall/3 example
child(martha,charlotte).
child(charlotte,caroline).
child(caroline,laura).
child(laura,rose).
descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z),
descend(Z,Y).
?- findall(X,descend(martha,X),L).
L=[charlotte,caroline,laura,rose]
yes
![Page 82: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/82.jpg)
General Game Playing: Winter term 2013/2014
Other findall/3 examples
child(martha,charlotte).
child(charlotte,caroline).
child(caroline,laura).
child(laura,rose).
descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z),
descend(Z,Y).
?- findall(X,descend(rose,X),L).
What is the output of Prolog?
![Page 83: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/83.jpg)
General Game Playing: Winter term 2013/2014
Other findall/3 examples
child(martha,charlotte).
child(charlotte,caroline).
child(caroline,laura).
child(laura,rose).
descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z),
descend(Z,Y).
?- findall(X,descend(rose,X),L).
L=[ ]
yes
![Page 84: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/84.jpg)
General Game Playing: Winter term 2013/2014
findall/3 is sometimes rather crude
child(martha,charlotte).
child(charlotte,caroline).
child(caroline,laura).
child(laura,rose).
descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z),
descend(Z,Y).
?- findall(Chi,descend(Mot,Chi),L).
What is the output of Prolog?
![Page 85: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/85.jpg)
General Game Playing: Winter term 2013/2014
findall/3 is sometimes rather crude
child(martha,charlotte).
child(charlotte,caroline).
child(caroline,laura).
child(laura,rose).
descend(X,Y):- child(X,Y).
descend(X,Y):- child(X,Z),
descend(Z,Y).
?- findall(Chi,descend(Mot,Chi),L).
L=[charlotte,caroline,laura, rose,
caroline,laura,rose,laura,rose,rose]
yes
![Page 86: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/86.jpg)
General Game Playing: Winter term 2013/2014
bagof/3
• The query
produces a list L of all the objects O that satisfy the goal G
– Only succeeds if the goal G succeeds
– Binds free variables in G
?- bagof(O,G,L).
![Page 87: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/87.jpg)
General Game Playing: Winter term 2013/2014
Using bagof/3
child(martha,charlotte).
child(charlotte,caroline).
child(caroline,laura).
child(laura,rose).
descend(X,Y):-
child(X,Y).
descend(X,Y):-
child(X,Z), descend(Z,Y).
?- bagof(Chi,descend(Mot,Chi),L).
Mot=caroline
L=[laura, rose];
Mot=charlotte
L=[caroline,laura,rose];
Mot=laura
L=[rose];
Mot=martha
L=[charlotte,caroline,laura,rose];
no
![Page 88: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/88.jpg)
General Game Playing: Winter term 2013/2014
Prolog:
What’s left?
![Page 89: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/89.jpg)
General Game Playing: Winter term 2013/2014
Prolog in programming languages
• Using Prolog from Java
– http://www.gnu.org/software/gnuprologjava/
• C++-Interface for SWI-Prolog
– http://www.swi-prolog.org/pldoc/package/pl2cpp.html
• Python interface for SWI-Prolog
– http://code.google.com/p/pyswip/
• Prolog-interpreter written in Scala
– http://code.google.com/p/styla/
![Page 90: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/90.jpg)
General Game Playing: Winter term 2013/2014
Prolog
• Prolog is much, much more, than what we have seen
– Lists
– Negation as failure
– I/O
– Cut operator
• A nice manual can be found here: – http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/contents.html
• During the lecture, we will look at these things, only
whenever it becomes necessary
![Page 91: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/91.jpg)
General Game Playing: Winter term 2013/2014
Datalog?
- Datalog=Prolog minus non-terminating queries
- Datalog does not allow function symbols
- Datalog does not allow negation (not or … \+)
![Page 92: General Game Playing (GGP) Winter term 2013/2014 2. Prolog / …wandelt/ggp/Prolog.pdf · 2013-10-29 · General Game Playing: Winter term 2013/2014 Organization: Examination •](https://reader030.fdocuments.net/reader030/viewer/2022040306/5ec7774faee82911a755b9ae/html5/thumbnails/92.jpg)
General Game Playing: Winter term 2013/2014
Acknowledgements
• Prolog material based on introduction at
– http://www.learnprolognow.org/lpnpage.php?pageid=teaching
– © Patrick Blackburn, Johan Bos & Kristina Striegnitz