0% found this document useful (0 votes)
4 views27 pages

Cs344 Lect16 Prolog 12feb08

The document provides an introduction to Prolog programming, illustrating how to define facts, rules, and queries to model relationships and perform theorem proving. It explains key concepts such as backtracking, variables, conjunctions, and control flow in Prolog. Additionally, it discusses the use of cut and fail predicates for controlling program execution and implementing negation.

Uploaded by

bishalsomare09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views27 pages

Cs344 Lect16 Prolog 12feb08

The document provides an introduction to Prolog programming, illustrating how to define facts, rules, and queries to model relationships and perform theorem proving. It explains key concepts such as backtracking, variables, conjunctions, and control flow in Prolog. Additionally, it discusses the use of cut and fail predicates for controlling program execution and implementing negation.

Uploaded by

bishalsomare09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

CS344 : Introduction to Artificial

Intelligence

Pushpak Bhattacharyya
CSE Dept.,
IIT Bombay

Lecture 16- Prolog


An example Prolog
Program
Shows path with mode of conveyeance
from city C1 to city C2
 :-use_module(library(lists)).  go(C1,C2) :- travel(C1,C2,L),
show_path(L).
 byCar(auckland,hamilton).
 byCar(hamilton,raglan).
 travel(C1,C2,L) :-
direct_path(C1,C2,L).
 byCar(valmont,saarbruecke
n).
 byCar(valmont,metz).
 travel(C1,C2,L) :-
direct_path(C1,C3,L1),travel
(C3,C2,L2),append(L1,L2,L).
 byTrain(metz,frankfurt).
 byTrain(saarbruecken,frank  direct_path(C1,C2,[C1,C2,'
furt). by car']):- byCar(C1,C2).
 byTrain(metz,paris).  direct_path(C1,C2,[C1,C2,'
 byTrain(saarbruecken,paris) by train']):- byTrain(C1,C2).
.  direct_path(C1,C2,[C1,C2,'
by plane']):-
 byPlane(frankfurt,bangkok). byPlane(C1,C2).
 byPlane(frankfurt,singapore
).  show_path([C1,C2,M|T]) :-
 byPlane(paris,losAngeles). write(C1),write(' to
 byPlane(bangkok,auckland). '),write(C2),write(M),nl,sho
w_path(T).
 byPlane(losAngeles,aucklan
d).
Questions
 Questions based on facts
 Answered by 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.


 No does not mean falsity.
Prolog does theorem
proving
 When a question is asked, prolog
tries to match transitively.
 When no match is found, answer is
no.
 This means not provable from the
given facts.
Variables
 Always begin with a capital letter
 ?- likes (john,X).
 ?- likes (john, Something).
 But not
 ?- likes (john,something)
Example of usage of
variable
Facts:
likes(john,flowers).
likes(john,mary).
likes(paul,mary).
Question:
?- likes(john,X)
Answer:
X=flowers and wait
;
mary
;
no
Conjunctions
 Use ‘,’ and pronounce it as and.
 Example

Facts:

likes(mary,food).

likes(mary,tea).

likes(john,tea).

likes(john,mary)
 ?-

likes(mary,X),likes(john,X).

Meaning is anything liked by Mary also liked by John?
Backtracking (an inherent
property of prolog programming)

likes(mary,X),likes(john,X)

likes(mary,food)
likes(mary,tea)
likes(john,tea)
likes(john,mary)

1. First goal succeeds. X=food


2. Satisfy likes(john,food)
Backtracking (continued)
Returning to a marked place and trying to resatisfy
is called Backtracking
likes(mary,X),likes(john,X)

likes(mary,food)
likes(mary,tea)
likes(john,tea)
likes(john,mary)

1. Second goal fails


2. Return to marked place
and try to resatisfy the first goal
Backtracking (continued)

likes(mary,X),likes(john,X)

likes(mary,food)
likes(mary,tea)
likes(john,tea)
likes(john,mary)

1. First goal succeeds again, X=tea


2. Attempt to satisfy the likes(john,tea)
Backtracking (continued)

likes(mary,X),likes(john,X)

likes(mary,food)
likes(mary,tea)
likes(john,tea)
likes(john,mary)

1. Second goal also suceeds


2. Prolog notifies success and waits for a reply
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).
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 ‘.’.
Another Example
sister_of (X,Y):- female (X),
parents (X, M, F),
parents (Y, M, F).

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?
Prolog Program Flow,
BackTracking and Cut

Controlling the program flow


Prolog’s computation
 Depth First Search
 Pursues a goal till the end
 Conditional AND; falsity of any
goal prevents satisfaction of
further clauses.
 Conditional OR; satisfaction of
any goal prevents further
clauses being evaluated.
Control flow (top level)
Given
g:- a, b, c. (1)
g:- d, e, f; g. (2)

If prolog cannot satisfy (1), control


will automatically fall through to
(2).
Control Flow within a rule
Taking (1),
g:- a, b, c.
If a succeeds, prolog will try to satisfy b,
succeding which c will be tried.
For ANDed clauses, control flows forward
till the ‘.’, iff the current clause is true.
For ORed clauses, control flows forward
till the ‘.’, iff the current clause
evaluates to false.
What happens on failure

 REDO the immediately


preceding goal.
Fundamental Principle of
prolog programming

 Always place the more general


rule AFTER a specific rule.
CUT
 Cut tells the system that

IF YOU HAVE COME THIS FAR

DO NOT BACKTRACK

EVEN IF YOU FAIL SUBSEQUENTLY.

‘CUT’ WRITTEN AS ‘!’ ALWAYS


SUCCEEDS.
Fail
 This predicate always fails.
 Cut and Fail combination is used to
produce negation.
 Since the LHS of the neck cannot
contain any operator, A  ~B is
implemented as
B :- A, !, Fail.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy