0% found this document useful (0 votes)
3 views79 pages

Lecture 4 - Introduction To Prolog

Chapter 4 of TEB 2023 introduces Prolog, a logic programming language that emphasizes declarative programming and recursion. It covers the history of Prolog, its syntax, and how to create knowledge bases using facts and rules. The chapter also discusses the use of Prolog in various applications, including AI and natural language processing.

Uploaded by

watif16207
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)
3 views79 pages

Lecture 4 - Introduction To Prolog

Chapter 4 of TEB 2023 introduces Prolog, a logic programming language that emphasizes declarative programming and recursion. It covers the history of Prolog, its syntax, and how to create knowledge bases using facts and rules. The chapter also discusses the use of Prolog in various applications, including AI and natural language processing.

Uploaded by

watif16207
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/ 79

TEB 2023: Artificial Intelligence

Chapter 4
Logic Programming with PROLOG
Today: Course Objectives

 Theory:
 Introduction to Prolog
 Facts, Rules and Queries
 Prolog Syntax

 Begin study of Prolog by defining:


 Terms, atoms, and variables
SWI Prolog
 Freely available Prolog interpreter

 Works with
 Linux,
 Windows, or
 Mac OS

 There are many more Prolog interpreters

 Not all are ISO compliant/free


Prolog
 "Programming with Logic“
 Very different from other programming languages:
 Declarative (not procedural)
 Recursion (no “for” or “while” loops)
 Relations (no functions)
 Unification
History of
Prolog

first Prolog interpreter by


Alain Colmerauer and
Philippe Roussel

1972 1977 1980 1980s/1990s 2005


History of
Prolog

implementation of DEC10
compiler by David H.D. Warren

1972 1977 1980 1980s/1990s 2005


History of
Prolog

Definite Clause Grammars


implementation by
Pereira and Warren

1972 1977 1980 1980s/1990s 2005


History of
Prolog

Prolog grows in popularity


especially in Japan and Europe

1972 1977 1980 1980s/1990s 2005


History of
Prolog
Prolog used to
program natural
language interface in
International Space
Station by NASA

1972 1977 1980 1980s/1990s 2005


History of
Prolog
Parts of IBM’s
Watson QA
supercomputer
were coded in
Prolog

1972 1977 1980 1980s/1990s 2011


Prolog and Web Applications
• prolog programs are often smaller
• smallness encourages well written code
• hence, easier to maintain

Source:

http://www.pathwayslms.com/swipltuts/
Basic idea of
Prolog
• Describe the situation of interest

• Ask a question

• Prolog:
– logically deduces new facts about the situation
we described
– gives us its deductions back as answers
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 (knowledge
representation, inference)
“Hello World”

• The most striking feature of Prolog for the newcomer is


how much simpler the programs look than in other
languages.

• All the user has to do in Prolog is to enter

write('Hello World!').

?- write('Hello World'),nl,write('Welcome to
Prolog'),nl.
Example : Knowledge Base

• There are only two components to any program: facts


and rules.
• The Prolog system reads in the program and simply
stores it. The user then types in a series of questions
(strictly known as queries), which the system answers
using the facts and rules available to it.
• This is a simple example, a series of queries and
answers about animals.
Example : Knowledge Base

The first three lines are facts, with the obvious


dog(fido). interpretation that fido, rover and henry are all dogs.
dog(rover). The next three facts say that felix, michael and jane are all
dog(henry). cats.The final line is a rule saying that anything (let us call
it X) is an animal if it is a dog.
cat(felix).
Having loaded the program, the user is then faced with the
cat(michael). two character symbol ?- which is called the system
prompt.
cat(jane).
animal(X):-dog(X). To check whether fido is a dog all that is necessary is to
type the query dog(fido) followed by a full stop and press
the 'return' key, which indicates to the system that a
response is needed

Last line is a rule. The :- character (colon and hyphen) can


be read as 'if'. X is called a variable.
Example : Knowledge Base

?- dog(fido).
Yes

?-dog(jane).
No

?- dog(X).
X = fido ;
X = rover ;
X = henry
Example : Knowledge Base

?-halt. 'Exit from the Prolog system.'

/* Animals Program 1 */
Example : Knowledge Base

/* Animals Program 1 */
dog(fido).
cat(mary). dog(rover).
dog(tom). cat(harry).
dog(henry).
cat(bill). cat(steve).

?-cat(X),dog(Y).
Knowledge Base 1

woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?-
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- woman(mia).
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- woman(mia).
yes
?-
Knowledge Base 1

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

?- woman(mia).
yes
?-
playsAirGuitar(j
ody).
Knowledge Base 1

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

?- woman(mia).
yes
?-
playsAirGuitar(j
ody).
yes
?-
Knowledge Base 1

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

?- woman(mia).
yes
?-
playsAirGuitar(j
ody).
yes
?-
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- woman(mia).
yes
?-
playsAirGuitar(j
ody).
yes
?- playsAirGuitar(mia).
Knowledge Base 1

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

?- tattoed(jody).
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- tattoed(jody).
no
?-
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

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

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

?- party.
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- party.
yes
?-
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- rockConcert.
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
party.

?- rockConcert.
no
?-
Knowledge Base 2

happy(yolanda).
listens2music(mia).
listens2music(yola
nda):-
happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
Knowledge Base 2
happy(yolanda). fact
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
Knowledge Base 2

happy(yolanda). fact
listens2music(mia). fact
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
Knowledge Base 2

happy(yolanda). fact
listens2music(mia). fact
listens2music(yolanda):- happy(yolanda). rule
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
Knowledge Base 2

happy(yolanda). fact
listens2music(mia). fact
listens2music(yolanda):- happy(yolanda). rule
playsAirGuitar(mia):- listens2music(mia). rule
playsAirGuitar(yolanda):- listens2music(yolanda).
Knowledge Base 2
happy(yolanda). fact
listens2music(mia). fact
listens2music(yolanda):- happy(yolanda). rule
playsAirGuitar(mia):- listens2music(mia). rule
playsAirGuitar(yolanda):- listens2music(yolanda). rule
Knowledge Base 2
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

hea bod
d y
Knowledge Base 2

happy(yolanda).
listens2music(mia).
listens2music(yola
nda):-
happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
?-
Knowledge Base 2
happy(yolanda).
listens2music(mia).
listens2music(yola
nda):-
happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
?- playsAirGuitar(mia).
yes
?-
Knowledge Base 2

happy(yolanda).
listens2music(mia).
listens2music(yola
nda):-
happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
?- playsAirGuitar(mia).
yes
?-
playsAirGuitar(yoland
a).
yes
Clauses

happy(yolanda).
listens2music(mia).
listens2music(yola
nda):-
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.


Predicates
happy(yolanda).
listens2music(mia).
listens2music(yola
nda):-
happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).

There are three predicates in this knowledgebase:


happy, listens2music, and playsAirGuitar
Knowledge Base 3

happy(vincent).
listens2music(butch).
playsAirGuitar(vince
nt):-
listens2music(vincen
t), happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).
Expressing Conjunction

happy(vincent).
listens2music(butch).
playsAirGuitar(vince
nt):-
listens2music(vincen
t), happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).
The comma “," expresses conjunction in Prolog
Knowledge Base 3
happy(vincent).
listens2music(butch).
playsAirGuitar(vince
nt):-
listens2music(vincen
t), happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).
?- playsAirGuitar(vincent).
Knowledge Base 3

happy(vincent).
listens2music(butch).
playsAirGuitar(vince
nt):-
listens2music(vincen
t), happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).
?- playsAirGuitar(vincent).
no
?-
Knowledge Base 3

happy(vincent).
listens2music(butch).
playsAirGuitar(vince
nt):-
listens2music(vincen
t), happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).
?- playsAirGuitar(butch).
Knowledge Base 3

happy(vincent).
listens2music(butch).
playsAirGuitar(vince
nt):-
listens2music(vincen
t), happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).
?- playsAirGuitar(butch).
yes
?-
Expressing Disjunction

happy(vincent).
listens2music(butch).
playsAirGuitar(vince
nt):-
listens2music(vincen
t), happy(vincent).
playsAirGuitar(butch):- happy(butch).
playsAirGuitar(butch):- listens2music(butch).
happy(vincent).
listens2music(butch).
playsAirGuitar(vince
nt):-
listens2music(vincen
t), happy(vincent).
playsAirGuitar(butc
Prolog and Logic

• Clearly, Prolog has something to do with logic...


Prolog Logic
Implication A :- B BA
Conjunction A,B A∧ B
Disjunction A;B A∨ B

• Use of inference (modus ponens)


• Negation (?)
Knowledge Base 4
woman(mia).
woman(jody).
woman(yolanda).

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

loves(vincent, mia).
loves(marsellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny,
pumpkin).
?- woman(X).
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
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;
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
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
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
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).
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
?-
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).
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
?-
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).


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).
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
?-
Prolog terms

Terms

Simple Terms Comple Terms


x

Constants Variables

Atom Number
s s
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


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', '@$%'
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: : , ; . :-
Numbers

• Integers:

12, - 22342
34,
• Floats:

34573.3234,
0.3435
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


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
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))))
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
Arity is important

• 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
Example of Arity

happy(yolanda).
listens2music(mia).
listens2music(yola
nda):-
happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
• This knowledge base defines
– happy/1
– listens2music/1
– playsAirGuitar/1

You might also like