Module 4 Notes
Module 4 Notes
PROgramming in LOGic
Emphasis on what rather than how
Problem in Declarative Form
Logic Machine
Basic Machine
Prolog’s strong and weak
points
Assists thinking in terms of objects and
entities
Not good for number crunching
Useful applications of Prolog in
Expert Systems (Knowledge Representation
and Inferencing)
Natural Language Processing
Relational Databases
What is Prolog?
Prolog is the most widely used language to
have been inspired by logic programming
research. Some features:
Prolog uses logical variables. These are not
the same as variables in other languages.
Programmers can use them as ‘holes’ in
data structures that are gradually filled in
as computation proceeds.
…More
cat(a,b)
In a functional language if b = nil then a
else cons(head(a), cat(tail(a),b))
cat([], Z, Z).
In a declarative language cat([H|T], L, [H|Z]) :- cat(T, L, Z).
Complete Syntax of Terms
Term
badger
means the same as
badger()
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
Facts
elephant(george).
Clauses elephant(mary).
elephant(X) :- grey(X), mammal(X), hasTrunk(X).
Rule
Example
?- elephant(george).
Queries
yes
?- elephant(jane).
Replies
no
Clauses: Facts ‘if’and Rules
‘provided that’
‘turnstile’
Goals
berkshire surrey
kent
likes(mary,X),likes(john,X)
likes(mary,food)
likes(mary,tea)
likes(john,tea)
likes(john,mary)
likes(mary,food)
likes(mary,tea)
likes(john,tea)
likes(john,mary)
likes(mary,X),likes(john,X)
likes(mary,food)
likes(mary,tea)
likes(john,tea)
likes(john,mary)
likes(mary,X),likes(john,X)
likes(mary,food)
likes(mary,tea)
likes(john,tea)
likes(john,mary)
X is a sister of Y is
X is a female and
X and Y have same parents
Question Answering in presence
of rules
Facts
male (ram).
male (shyam).
female (sita).
female (gita).
parents (shyam, gita, ram).
parents (sita, gita, ram).
Question Answering: Y/N type: is sita the
sister of shyam?
?- sister_of (sita, shyam)
parents(sita,M,F) parents(shyam,M,F)
female(sita)
parents(shyam,gita,ram)
parents(sita,gita,ram)
success
Question Answering: wh-type: whose
sister is sita?
?- ?- sister_of (sita, X)
parents(sita,M,F) parents(Y,M,F)
female(sita)
parents(Y,gita,ram)
parents(sita,gita,ram)
parents(shyam,gita,ram)
Success
Y=shyam
Exercise
1. From the above it is possible for
somebody to be her own sister. How
can this be prevented?
An example Prolog Program
Rules
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).
Definitions
An animal is a bird if it has feathers
bird(X) :- animal(X), has_feather(X).
Prolog Program Flow,
BackTracking and Cut
DO NOT BACKTRACK
Rules
A wrong prolog program!
1. member(a).
2. member(b).
3. member(c).
4. mc(X);sk(X) :- member(X) /* X is a mountain climber or skier or
both if X is a member; operators NOT allowed in the head of a
horn clause; hence wrong*/
5. like(X, snow) :- sk(X). /*all skiers like snow*/
6. \+like(X, rain) :- mc(X). /*no mountain climber likes rain; \+ is
the not operator; negation by failure; wrong clause*/
7. \+like(a, X) :- like(b,X). /* a dislikes whatever b likes*/
8. like(a, X) :- \+like(b,X). /* a dislikes whatever b likes*/
9. like(a,rain).
10. like(a,snow).
?- member(X),mc(X),\+sk(X).
Prolog’s way of making and
breaking a list
Problem: to remove duplicates from a list
rem_dup([],[]).
rem_dup([H|T],L) :- member(H,T), !, rem_dup(T,L).
rem_dup([H|T],[H|L1]) :- rem_dup(T,L1).