72
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Artificial Intelligence Course and exercises web site http://www2.kbs.uni-hannover.de/ ki.html About the AI exercises (Prolog) – Taught in English – Thursdays morning 11:00-11:45

Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Artificial Intelligence

•  Course and exercises web site http://www2.kbs.uni-hannover.de/ki.html

•  About the AI exercises (Prolog) – Taught in English – Thursdays morning 11:00-11:45

Page 2: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Artificial Intelligence Introduction to Prolog

•  Exercises Teaching Material – Learn Prolog Now!

http://www.learnprolognow.org – SWI Prolog interpreter

http://www.swi-prolog.org/

Page 3: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Learn Prolog Now!

•  Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises (corrected in class)

•  Assigned exercises are corrected in class after the presentation of Prolog’s theory

•  Corrections available on the course web site – Practical session (not covered)

•  Not covered in the lecture, but it is strongly encouraged to do them!

•  Knowledge bases code available at http://l3s.de/~gaugaz/ki/prolog/

Page 4: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz SWI Prolog

•  Freely available Prolog interpreter •  Works with

– Linux, – Windows, or – Mac OS

•  There are many more Prolog interpreters

•  Not all are ISO compliant

Page 5: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Lecture 1

•  Theory –  Introduction to Prolog – Facts, Rules and Queries – Prolog Syntax

•  Exercises – Exercises of LPN chapter 1

Page 6: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Aim of this lecture

•  Give some simple examples of Prolog programs

•  Discuss the three basic constructs in Prolog: –  Facts –  Rules –  Queries

•  Introduce other concepts, such as –  the role of logic –  unification with the help of variables

•  Begin the systematic study of Prolog by defining terms, atoms, and variables

Page 7: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Prolog

•  "Programming with Logic" •  Declarative •  Very different from other (procedural)

programming languages •  Good for knowledge-rich tasks

Page 8: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

History of Prolog

1972 1977 1980 1980s/1990s 2005

first Prolog interpreter by Colmerauer and Roussel

Page 9: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

History of Prolog

1972 1977 1980 1980s/1990s 2005

implementation of DEC10 compiler by Warren

Page 10: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

History of Prolog

1972 1977 1980 1980s/1990s 2005

Definite Clause Grammars implementation by Pereira

and Warren

Page 11: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

History of Prolog

1972 1977 1980 1980s/1990s 2005

Prolog grows in popularity especially in Europe and Japan

Page 12: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

History of Prolog

1972 1977 1980 1980s/1990s 2005

Prolog used to program natural language

interface in International Space

Station by NASA

Page 13: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Basic idea of Prolog

•  Describe the situation of interest •  Ask a question •  Prolog logically deduces new facts

about the situation we described •  Prolog gives us its deductions back as

answers

Page 14: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Consequences

•  Think declaratively, not procedurally – Challenging – Requires a different mindset

•  High-level language – Not as efficient as, say, C – Good for rapid prototyping – Useful in many AI applications

Page 15: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.

Page 16: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.

?-

Page 17: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.

?- woman(mia).

Page 18: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.

?- woman(mia). yes ?-

Page 19: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.

?- woman(mia). yes ?- playsAirGuitar(jody).

Page 20: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.

?- woman(mia). yes ?- playsAirGuitar(jody). yes ?-

Page 21: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.

?- woman(mia). yes ?- playsAirGuitar(jody). yes ?- playsAirGuitar(mia). no

Page 22: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.

?- tattoed(jody).

Page 23: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.

?- tattoed(jody). no ?-

Page 24: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.

?- tattoed(jody). ERROR: predicate tattoed/1 not defined. ?-

Page 25: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.

?- party.

Page 26: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.

?- party. yes ?-

Page 27: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.

?- rockConcert.

Page 28: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 1

woman(mia). woman(jody). woman(yolanda). playsAirGuitar(jody). party.

?- rockConcert. no ?-

Page 29: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 2

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

Page 30: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 2

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

fact

Page 31: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 2

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

fact fact

Page 32: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 2

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

fact fact

rule

Page 33: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 2

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

fact fact

rule rule

Page 34: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 2

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

fact fact

rule rule

rule

Page 35: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 2

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

head body

Page 36: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 2

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

?-

Page 37: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 2

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

?- playsAirGuitar(mia). yes ?-

Page 38: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 2

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

?- playsAirGuitar(mia). yes ?- playsAirGuitar(yolanda). yes

Page 39: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Clauses

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

There are five clauses in this knowledge base: two facts, and three rules.

The end of a clause is marked with a full stop.

Page 40: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz Predicates

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

There are three predicates in this knowledge base:

happy, listens2music, and playsAirGuitar

Page 41: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 3

happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch).

Page 42: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Expressing Conjunction

happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch).

The comma “," expresses conjunction in Prolog

Page 43: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 3

happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch).

?- playsAirGuitar(vincent). no ?-

Page 44: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 3

happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch).

?- playsAirGuitar(butch). yes ?-

Page 45: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Expressing Disjunction

happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch). playsAirGuitar(butch):- listens2music(butch).

happy(vincent). listens2music(butch). playsAirGuitar(vincent):- listens2music(vincent), happy(vincent). playsAirGuitar(butch):- happy(butch); listens2music(butch).

Page 46: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Prolog and Logic

•  Clearly Prolog has something to do with logic

•  Operators –  Implication :- – Conjunction , – Disjunction ;

•  Use of modus ponens •  Negation

Page 47: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 4

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).

Page 48: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Prolog Variables

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).

?- woman(X).

Page 49: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Variable Instantiation

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).

?- woman(X). X=mia

Page 50: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Asking Alternatives

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).

?- woman(X). X=mia;

Page 51: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Asking Alternatives

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).

?- woman(X). X=mia; X=jody

Page 52: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Asking Alternatives

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).

?- woman(X). X=mia; X=jody; X=yolanda

Page 53: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Asking Alternatives

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).

?- woman(X). X=mia; X=jody; X=yolanda; no

Page 54: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 4

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).

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

Page 55: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 4

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).

?- loves(marsellus,X), woman(X). X=mia yes ?-

Page 56: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 4

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).

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

Page 57: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 4

woman(mia). woman(jody). woman(yolanda). loves(vincent, mia). loves(marsellus, mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin).

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

Page 58: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 5

loves(vincent,mia). loves(marsellus,mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z).

Page 59: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 5

loves(vincent,mia). loves(marsellus,mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z).

?- jealous(marsellus,W).

Page 60: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Knowledge Base 5

loves(vincent,mia). loves(marsellus,mia). loves(pumpkin, honey_bunny). loves(honey_bunny, pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z).

?- jealous(marsellus,W). W=vincent ?-

Page 61: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

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 62: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

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', '@$%'

•  A sequence of special characters

•  Examples: : , ; . :-

Page 63: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Numbers

•  Integers: 12, -34, 22342 •  Floats: 34573.3234

Page 64: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

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 65: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

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 66: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Examples of complex terms

•  Examples we have seen before: – playsAirGuitar(jody) –  loves(vincent, mia) –  jealous(marsellus, W)

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

Page 67: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Arity

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

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

Page 68: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

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 69: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Example of Arity

•  This knowledge base defines – happy/1 –  listens2music/1 – playsAirGuitar/1

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

Page 70: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz Exercises

•  In next lecture we will correct the following exercises –  1.1, 1.2, 1.3

Page 71: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz

Summary of this lecture

•  Simple examples of Prolog programs •  Introduced three basic constructs in Prolog:

–  Facts –  Rules –  Queries

•  Discussed other concepts, such as –  the role of logic –  unification with the help of variables

•  Definition of Prolog constructs: terms, atoms, and variables

Page 72: Artificial Intelligence - Leibniz Universität Hannover€¦ · Learn Prolog Now! • Each chapter is composed of – Prolog theory (presented in KI Übungen) – Prolog exercises

© P

atri

ck B

lack

bu

rn,

Joh

an B

os &

Kri

stin

a S

trie

gn

itz Next lecture

•  Discuss unification in Prolog •  Prolog's search strategy