0% found this document useful (0 votes)
205 views71 pages

Lec07 AI Introduction To Prolog

Prolog (programming in logic) is a logic-based programming language: programs correspond to sets of logical formulas and the Prolog interpreter uses logical methods to resolve queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
205 views71 pages

Lec07 AI Introduction To Prolog

Prolog (programming in logic) is a logic-based programming language: programs correspond to sets of logical formulas and the Prolog interpreter uses logical methods to resolve queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 71

Prolog:

Programming in Logic

with some mention of Datalog and


Constraint Logic Programming

600.325/425 Declarative Methods


- J. Eisner 1
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

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Artificial Intelligence
Introduction to Prolog
■Exercises Teaching Material
❑ Learn Prolog Now! http://www.learnprolognow.org
❑ SWI Prolog interpreter http://www.swi-prolog.org/

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
The original declarative
programming

language
Courses in programming languages …
❑ Prolog is always the declarative language they teach.
❑ (imperative, functional, object-oriented, declarative)

■ Alain Colmeraeur & Philippe Roussel, 1971-1973


❑ With help from theorem proving folks such as Robert Kowalski
❑ Original project: Type in French statements & questions
■ Computer needed NLP and deductive reasoning
❑ Efficiency by David Warren, 1977 (compiler, virtual machine)
❑ Colmerauer & Roussel wrote 20 years later:
“Prolog is so simple that one has the sense that sooner or
later someone had to discover it … that period of our lives
remains one of the happiest in our memories.
❑ “We have had the pleasure of recalling it for this paper over
almonds accompanied by a dry martini.”
600.325/425 Declarative Methods
- J. Eisner 4
SWI Prolog

■Freely available Prolog interpreter


■Works with
❑ Linux,
❑ Windows, or
❑ Mac OS
■There are many more Prolog interpreters
■Not all are ISO compliant

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Distributions of Prolog

■ Most common free Prolog implementation is SWI Prolog.


❑ Very nice, though faster ones are for sale (e.g., SICSTUS Prolog).
■ To run Prolog, you can just run ECLiPSe!
❑ ECLiPSe is a perfectly good Prolog implementation,
although so far we’ve concentrated only on its “extra” features.

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

600.325/425 Declarative Methods


- J. Eisner 6
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
© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Prolog

■"Programming with Logic"


■Declarative
■Very different from other (procedural)
programming languages
■Good for knowledge-rich tasks

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
History of Prolog

first Prolog interpreter by


Colmerauer and Roussel

1972 1977 1980 1980s/1990s 2005

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
History of Prolog

implementation of DEC10
compiler by Warren

1972 1977 1980 1980s/1990s 2005

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
History of Prolog

Definite Clause Grammars


implementation by Pereira and
Warren

1972 1977 1980 1980s/1990s 2005

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
History of Prolog
Prolog grows in popularity especially in
Europe and Japan

1972 1977 1980 1980s/1990s 2005

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
History of Prolog
Prolog used to program
natural language interface
in International Space
Station by NASA

1972 1977 1980 1980s/1990s 2005

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

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

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 1

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

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 1

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

?-

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 1

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

?- woman(mia).

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 1

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

?- woman(mia).
yes
?-

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 1

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

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

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 1

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

?- woman(mia).
yes
?- playsAirGuitar(jody).
yes
?-
© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 1

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

?- woman(mia).
yes
?- playsAirGuitar(jody).
yes
?- playsAirGuitar(mia).
no
© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 1

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

?- tattoed(jody).

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 1

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

?- tattoed(jody).
no
?-

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 1

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

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

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 1

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

?- party.

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 1

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

?- party.
yes
?-

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 1

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

?- rockConcert.

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 1

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

?- rockConcert.
no
?-

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 2

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

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 2
fact
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 2
fact
happy(yolanda).
fact
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 2
fact
happy(yolanda).
fact
listens2music(mia).
rule
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 2
fact
happy(yolanda).
fact
listens2music(mia).
rule
listens2music(yolanda):- happy(yolanda).
rule
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 2
fact
happy(yolanda).
fact
listens2music(mia).
rule
listens2music(yolanda):- happy(yolanda).
rule
playsAirGuitar(mia):- listens2music(mia).
rule
playsAirGuitar(yolanda):- listens2music(yolanda).

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 2

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

head body

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 2

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

?-

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 2

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

?- playsAirGuitar(mia).
yes
?-

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

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

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

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 3

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

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

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 3

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

?- playsAirGuitar(vincent).
no
?-

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 3

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

?- playsAirGuitar(butch).
yes
?-

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

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Prolog and Logic

■Clearly Prolog has something to do with logic


■Operators
❑ Implication :-
❑ Conjunction ,
❑ Disjunction ;
■Use of modus ponens
■Negation

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Knowledge Base 4

woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Prolog Variables

woman(mia).
woman(jody).
woman(yolanda).

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

?- woman(X).

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

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

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

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
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
© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
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;
© Patrick Blackburn, no
Johan Bos & Kristina
Striegnitz
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).

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

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

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

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

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

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Prolog Syntax

■What exactly are facts, rules and queries


built out of?
Terms
Terms

Simple
SimpleTerms
Terms Complex
ComplexTerms
Terms

Constants
Constants Variables
Variables

Atoms
Atoms
© Patrick Blackburn,
Johan Bos & Kristina
Numbers
Numbers
Striegnitz
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: : , ; . :-
© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Numbers

■Integers: 12, -34, 22342


■Floats: 34573.3234

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

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

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

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

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

© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
Example of Arity

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

■This knowledge base defines


❑ happy/1
❑ listens2music/1
❑ playsAirGuitar/1
© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz
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
© Patrick Blackburn,
Johan Bos & Kristina
Striegnitz

You might also like