Logic and PROLOG
Logic and PROLOG
Introduction to PROLOG
Lecture Overview
1. Introduction to Prolog
2. Predicates, Facts, Rules, Variables, Queries
3. Prolog Syntax
PROLOG Introduction
• Prolog is a logical and a declarative programming
language. The name itself, Prolog, is short for
PROgramming in LOGic.
• Prolog is the most widely used language to have been
inspired by logic programming research
• Search is by backtracking with unification.
• Basic data structure used is a tree.
• SWI Prolog interpreter: Freely available Prolog
interpreter works with Linux, Windows, or Mac OS
http://www.swi-prolog.org/
PROLOG Introduction (2)
• The Basic Idea Behind PROLOG:
elephant(george).
elephant(mary).
panda(chi_chi).
panda(ming_ming).
dangerous(X) :- big_teeth(X).
dangerous(X) :- venomous(X).
An example…
Example
Predicate
Facts
elephant(george).
Clauses elephant(mary).
elephant(X) :- grey(X), mammal(X), hasTrunk(X).
Rule
Example
?- elephant(george).
Queries
yes
?- elephant(jane).
Replies
no
1. Clauses in PROLOG
• Clauses are statements about what is true
about a problem, instead of instructions how
to accomplish the solution.
• The Prolog system uses the clauses to work
out how to accomplish the solution by
searching through the space of possible
solutions.
Clauses in PROLOG (2)
• John likes Mary
– likes(john, mary).
• Names of relationships and objects MUST begin
with a lower-case letter.
• Relationship is written first (typically the predicate
of the sentence).
• Objects are written separated by commas and are
enclosed by a pair of round brackets.
• The full stop character ‘.’ must come at the end of a
fact.
Clauses in PROLOG (3): Examples
Predicate Interpretation
The semi-colon above allows us to see all the things that John likes
in this context, had we not included it, it was gonna tell us that
john likes flowers only leaving out mary
4. Conjunctions (,)
• Use ‘,’ and pronounce • Question:
it as AND. ?-likes(mary, X),likes(john, X).
• Example
Facts: • Meaning is anything liked by
likes(mary, food). Mary also liked by John?
likes(mary, tea).
likes(john, tea).
likes(john, mary)
5. Disjunction (;)
• Disjunction of predicates is represented as a
sequence of structures, separated by
semicolon”;”.
• It is referred as “OR” e.g:
• <head> :- <body>
• Read ‘:-’ as ‘if’.
• E.g.:
– likes(john, X) :- likes(X,cricket).
– “John likes X if X likes cricket”.
– i.e., “John likes anyone who likes cricket”.
• Rules always end with ‘.’.
Rules in PROLOG (3)
sister_of (X,Y):- female (X),
parents (X, M, F),
parents (Y, M, F).
Meaning: X is a sister of Y IF
X is a female AND
X AND Y have same parents
7. Negation in PROLOG
• Consider the following code:
enjoys(vincent,X) :- big_kahuna_burger(X),!,fail.
enjoys(vincent,X) :- burger(X).
burger(X) :- big_mac(X).
burger(X) :- big_kahuna_burger(X).
burger(X) :- whopper(X).
big_mac(a).
big_kahuna_burger(b).
big_mac(c).
whopper(d).
• Using Negation
enjoys(vincent,X) :- burger(X), neg(big_kahuna_burger(X)).
How to ask Questions in PROLOG
jim
How to ask Questions in PROLOG
Example 1 …(2)
• Questions:
– Is Bob a parent of Pat? pam tom
• ?- parent( X, liz).
jim
28
How to ask Questions in PROLOG
Example 2
/* Clause 1 */ located_in(atlanta,georgia).
/* Clause 2 */ located_in(houston,texas).
/* Clause 3 */ located_in(austin,texas).
/* Clause 4 */ located_in(toronto,ontario).
/* Clause 5 */ located_in(X,usa) :- located_in(X,georgia).
/* Clause 6 */ located_in(X,usa) :- located_in(X,texas).
/* Clause 7 */ located_in(X,canada) :- located_in(X,ontario).
/* Clause 8 */ located_in(X,north_america) :- located_in(X,usa).
/* Clause 9 */ located_in(X,north_america) :- located_in(X,canada).
How to ask Questions in PROLOG
Example 2…(2)
• To ask whether atlanta is in georgia:
?- located_in(atlanta,georgia).
– This query matches clause 1. So prolog replies
“yes”.
?- located_in(atlanta,usa).
This query can be solve by calling clause 5, and
then clause 1. So prolog replies “yes”.
How to ask Questions in PROLOG
Example 2…(3)
?-located_in(atlanta,texas).
this query gets “no” as its answer because this
fact cannot be deduced from the knowledge
base.
?-
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
?- woman(mia).
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
?- woman(mia).
yes
?-
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
?- woman(mia).
yes
?- playsAirGuitar(jody).
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
?- woman(mia).
yes
?- playsAirGuitar(jody).
yes
?-
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
?- woman(mia).
yes
?- playsAirGuitar(jody).
yes
?- playsAirGuitar(mia).
no
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
?- tattoed(jody).
no
?-
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).
?- tattoed(jody).
ERROR: predicate tattoed/1 not defined.
?-
Knowledge Base 2
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
Knowledge Base 2
fact
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
Knowledge Base 2
fact
happy(yolanda).
fact
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
Knowledge Base 2
fact
happy(yolanda).
fact
listens2music(mia).
rule
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
Knowledge Base 2
fact
happy(yolanda).
fact
listens2music(mia). rule
listens2music(yolanda):- happy(yolanda).
rule
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
Knowledge Base 2
fact
happy(yolanda).
listens2music(mia). fact
rule
listens2music(yolanda):- happy(yolanda).
rule
playsAirGuitar(mia):- listens2music(mia).
rule
playsAirGuitar(yolanda):- listens2music(yolanda).
Knowledge Base 2
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
head body
Knowledge Base 2
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).
?- playsAirGuitar(mia).
yes
?-
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
Clauses
happy(yolanda).
listens2music(mia).
listens2music(yolanda):- happy(yolanda).
playsAirGuitar(mia):- listens2music(mia).
playsAirGuitar(yolanda):- listens2music(yolanda).