0% found this document useful (0 votes)
104 views51 pages

Logic and PROLOG

Prolog is a logic programming language where programs consist of facts and rules expressed in terms of predicates and variables. Prolog programs describe situations and answer questions by logically deducing facts. Queries are posed to Prolog by using predicates and variables, and Prolog will attempt to match the query against the facts and rules to determine if the query can be logically proven or not. Common elements of Prolog programs include predicates, variables, facts represented as clauses, and rules represented as clauses with heads and bodies connected by ":-".

Uploaded by

PATSON CHIDARI
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)
104 views51 pages

Logic and PROLOG

Prolog is a logic programming language where programs consist of facts and rules expressed in terms of predicates and variables. Prolog programs describe situations and answer questions by logically deducing facts. Queries are posed to Prolog by using predicates and variables, and Prolog will attempt to match the query against the facts and rules to determine if the query can be logically proven or not. Common elements of Prolog programs include predicates, variables, facts represented as clauses, and rules represented as clauses with heads and bodies connected by ":-".

Uploaded by

PATSON CHIDARI
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/ 51

5.

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:

1. Describe the situation of interest


2. Ask a question
3. Prolog logically deduces new facts about the
situation we described
4. Prolog gives us its deductions back as
answers
What a PROLOG program looks like
/* At the Zoo */

elephant(george).
elephant(mary).

panda(chi_chi).
panda(ming_ming).

dangerous(X) :- big_teeth(X).
dangerous(X) :- venomous(X).

guess(X, tiger) :- stripey(X), big_teeth(X), isaCat(X).


guess(X, koala) :- arboreal(X), sleepy(X).
guess(X, zebra) :- stripey(X), isaHorse(X).
Structure of Programs
• Programs consist of procedures.
• Procedures consist of clauses.
• Each clause is a fact or a rule.
• Programs are executed by posing queries.

An example…
Example
Predicate

Procedure for elephant

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

valuable(gold) Gold is valuable.

owns(john,gold) John owns gold.

father(john,mary) John is the father of


Mary
gives (john,book,mary) John gives the book to
Mary
2. Questions in PROLOG
• Questions based on facts are answered by matching.
Unification is the name given to the way Prolog
does its matching.

Two facts match if their predicates are same (spelt


the same way) and the arguments each are same.

• If matched, prolog answers yes, else no.


• When no match is found, answer is no. This means
not provable from the given facts.
• More on this in later slide
3. Variables in PROLOG
• ALWAYS begin with a CAPITAL LETTER
– ?- likes (john, X).
– ?- likes (john, Something).
• But not
– ?- likes (john,something)
Variables in PROLOG (2)
Facts: Answer:
likes(john, flowers). X=flowers and wait
likes(john, mary). ;
likes(paul, mary). mary
;
Question:
?- likes(john, X)
Variables in PROLOG (3)

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:

friend(tsitsi, kuda):-friend(kuda, tino) ;


friend(kuda,rumbie).
6. Rules in PROLOG (:-)
• Statements about objects and their relationships
• Expess
– If-then conditions
• I use an umbrella if there is a rain
• use(i, umbrella) :- occur(rain).
– Generalizations
• All men are mortal
• mortal(X) :- man(X).
• X is mortal if X is a man
– Definitions
• An animal is a bird if it has feathers
• bird(X) :- animal(X), has_feather(X).
• Meaning: X is a bird IF X is an animal AND X has feathers
Rules in PROLOG (2): Syntax

• <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

1. First, write a prolog program in notepad and


save it as a .pl file.
2. Then open your prolog, and under file, click
on consult, to load the file, using the prolog
interpreter.
3. Then you can ask question.
How to ask Questions in PROLOG
Example 1
• The treeadefined
Given whole by the Prolog
family tree program:

parent( pam, bob). % Pam is a parent of Bob


parent( tom,pam
bob). tom
parent( tom, liz).
parent( bob, ann).
bob liz
parent( bob, pat).
parent( pat, jim).
ann pat

jim
How to ask Questions in PROLOG
Example 1 …(2)
• Questions:
– Is Bob a parent of Pat? pam tom

• ?- parent( bob, pat).


• ?- parent( liz, pat).
bob liz
• ?- parent( tom, ben).

– Who is Liz’s parent? ann pat

• ?- parent( X, liz).
jim

– Who are Bob’s children?


• ?- parent( bob, X).
How to ask Questions in PROLOG
Example 1 …(3)
How to ask Questions in PROLOG
Example 1 …(4)
• Questions: pam tom

– Who is a parent of whom?


bob liz
• Find X and Y such that X is a
parent of Y.
ann pat
• ?- parent( X, Y).
jim

– Who is a grandparent of Jim? X

• ?- parent( Y, jim), parent


Y grandparent
parent( X, Y).
parent
jim
26
How to ask Questions in PROLOG
Example 1 …(5)
How to ask Questions in PROLOG
Example 1 …(6)
• Questions:
– Who are Tom’s grandchildren? pam tom

• ?- parent( tom, X),


parent( X, Y).
bob liz

– Do Ann and Pat have a common


ann pat
parent?
• ?- parent( X, ann),
parent( X, pat). 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.

The query succeeds if it gets a “yes” and fails if


it gets a “no”.
Knowledge Base 1
woman(mia).
woman(jody).
woman(yolanda).
playsAirGuitar(jody).

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

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

There are three predicates


in this knowledge base:
happy, listens2music, and playsAirGuitar
PROLOG mini project
• In groups of 3, i.e. groups you used for
presenting searches, think of a mini project,
where you can use PROLOG and work on it in
your spare time. Give it your all,.. I will
evaluate it and award marks for it as part of
your CA.

You might also like