Anda di halaman 1dari 16

# Artificial Intelligent & Expert System Lab

INDEX

## Exp. No. Experiment Name Page No. Date Signature

1 Introduction to Prolog. 1

## Write simple fact for following

1. Ram likes mango.
2. Seema is a girl.
2 4
3. Bill likes Cindy.
4. Rose is red.
5. John owns gold.
Write predicates for following facts
1. Dashrath is Ram’s father.
3 2. Ram is father of Luv. 5
3. Kush is son of Ram.
4. Koushaliya is wife of Dashrath.

## Write a program to find union and intersection of

5 7
two given sets represented as lists.
Write a program to delete the first occurrence and
6 also all Occurrences of a particular element in a 8
given list.

8 10
problem.

## Dr. Mukesh Nandanwar

Artificial Intelligent & Expert System Lab

EXPERIMENT NO. 1

## AIM: Introduction to Prolog.

Prolog, which stands for PROgramming in LOGic, is the most widely available language in the logic
programming paradigm. Logic and therefore Prolog is based the mathematical notions of relations and
logical inference. Prolog is a declarative language meaning that rather than describing how to compute
a solution, a program consists of a data base of facts and logical relationships (rules) which describe the
relationships which hold for the given application. Rather than running a program to obtain a solution,
the user asks a question. When asked a question, the run time system searches through the data base of
facts and rules to determine (by logical deduction) the answer.
Among the features of Prolog are `logical variables' meaning that they behave like mathematical
variables, a powerful pattern-matching facility (unification), a backtracking strategy to search for
proofs, uniform data structures, and input and output are interchangeable.
Often there will be more than one way to deduce the answer or there will be more than one solution, in
such cases the run time system may be asked find other solutions, backtracking to generate alternative
solutions. Prolog is a weakly typed language with dynamic type checking and static scope rules.
Prolog is used in artificial intelligence applications such as natural language interfaces, automated
reasoning systems and expert systems. Expert systems usually consist of a data base of facts and rules
and an inference engine, the run time system of Prolog provides much of the services of an inference
engine.

Prolog is based on First Order Predicate Logic -sometimes abbreviated to FOPL. First order predicate
logic implies the existence of a set of predicate symbols along with a set of connectives.
First order predicate logic implies that there are no means provided for "talking about'' the predicates
themselves.
Prolog is based on FOPL but uses a restricted version of statements in FOPL, called Horn clause form.

Horn Clauses
Consider .
The statement Q will be true if all statements P1, P2, … Pn are true.
In disjunctive normal form the implication will be represented as:

Expressions of this type are called Horn clauses. All Prolog statements are instances of Horn clauses.
The inference in Prolog is based on the modus ponens syllogism
(1) If P(x) then Q(x)
(2) P(a)
(3) Therefore Q(a)

Example:
If human(X) then mortal(X).
human(socrates)
Therefore: mortal(socrates)
A Prolog program consists of logical expressions of type (1) and (2), represented as Prolog clauses
(discussed below).

Building blocks
A Prolog program consists of clauses.
A clause is a term.
 Term: constant | variable | structure

## Dr Mukesh Nandanwar Page 1

Artificial Intelligent & Expert System Lab

##  Constants: atoms | integers

 Atoms: strings of characters starting with a lower case letter, representing names of objects or
relations.
Examples: member, part_of, iowa, john, t12
Note: if we need the first letter to be a capital letter, we need to enclose the string in single
quotes: 'John'
 Variables: strings of characters, starting with a capital letter
 Structure: a predicate with a name (functor) and fixed number of arguments. Each argument
is a term, i.e. a constant, a variable, or a structure.
Example of a structure: mother(mary,X).

Types of clauses:
Rules - if-then logical expressions, Horn clauses.
Facts - predicates with 0 or more arguments.
Rules: structure with functor ' :- ' and two arguments: head and body.
<rule_body>::= sequence of structures, separated by commas or ;
Facts: rules with an empty body.
Example:
divisible_by_two(X):- even(X).
This is equivalent to the predicate calculus statement

Semantics:
Here is an informal version of the procedural semantics for the example above:
If we can find a value of X that satisfies the goal even(X) then we have also found a number
that satisfies the goal divisible_by_two(X).
The declarative semantics is:
If an integer X is even then X is divisible by two.
Note that there is an implicit universal quantification here. That is, for all objects those that are
even are also divisible by two.
Another example: divisible_by_six(X):- divisible_by_two(X), divisible_by_three(X).
Thus each rule
Q:- P1, P2, …., Pn
corresponds to an implication in predicate logic of the type:

The head of the rule (Q) is the predicate in the right part of the implication.
The body of the rule (P1, P2, …., Pn) contains the predicates in the left part of the implication.
The left part is a conjunction.
 Conjunction of predicates is represented as a sequence of structures, separated by commas.
 Disjunction in Prolog:
a. using ; to separate structures
b. using separate clauses
Example:
a. siblings(X,Y):- brother(X,Y) ; sister(X,Y).
b. siblings(X,Y):-brother(X,Y).
siblings(X,Y):- sister(X,Y).

## Dr Mukesh Nandanwar Page 2

Artificial Intelligent & Expert System Lab

For example, in the definition of the predicate "brother(X,Y)" we would not like to have same values
for X and Y, and we write:
brother(X,Y):- male(X), parent(Z,X), parent(Z,Y), not X = Y.
We may negate any predicate, as in:
odd(X):- not even(X).

## Defining predicates by means of clauses

Each predicate can be defined by one or more clauses of any of the two types - rules and facts.
Example:
mother(X,Y):- parent(X,Y), female(X).
mother(mary, john).
grandmother(X,Y):- mother(X,Z), mother(Z,Y).
grandmother(X,Y):- mother(X,Z), father(Z,Y).
or
grandmother(X,Y):- mother(X,Z), parent(Z,Y).

Scope of Variables
The scope rule for Prolog is that two uses of an identical name for a logical variable only refer to the
same object if the uses are within a single clause. Therefore in
happy(X):-healthy(X).
wise(X):- old(X).
the two references to X in the first clause do not refer to the same object as the references to X in the
second clause.

## 3. How Prolog works

Queries and Goals
A Prolog program is initiated by a query - a predicate or a sequence of predicates to be proved.
The predicate to be proved is called a goal.
Prolog tries to match the goal to the head of some fact or some rule.
If a match is found with a rule, Prolog continues with proving each predicate in the body of the rule.

## Dr Mukesh Nandanwar Page 3

Artificial Intelligent & Expert System Lab

EXPERIMENT NO. 2

## Aim: Write simple fact for following

1. Ram likes mango.
2. Seema is a girl.
3. Bill likes Cindy.
4. Rose is red.
5. John owns gold.

Clauses
likes(ram, mango).
girl(seema).
red(rose).
likes(bill, cindy).
owns(john, gold).

Goal
1?- likes(ram, mango).
Yes

2 ?- likes(bill, cindy).
Yes

3 ?- likes(ram,What).
What = mango ;
No

## Dr Mukesh Nandanwar Page 4

Artificial Intelligent & Expert System Lab

EXPERIMENT NO. 3

## AIM: Write predicates for following facts

1. Dashrath is Ram’s father.
2. Ram is father of Luv.
3. Kush is son of Ram.
4. Koushaliya is wife of Dashrath.

Rules
We said earlier a predicate is defined by clauses, which may be facts or rules. A rule is no more than a
stored query. Its syntax is
where
head a predicate definition (just like a fact) :- the neck symbol, sometimes read as "if" body
one or more goals (a query)

Production rules:

Parse tree:

Clauses:
father(dashrath, ram).
father(ram, luv).
son(kush, ram).
wife(koushaliya, dashrath).
mother(X,Y):-wife(X,Z),father(Z,Y).
grandfather(X,Y):-father(X,Z),father(Z,Y).
grandfather(X,Y):-father(X,Z),son(Y,Z).

Queries:
?- father(Who,ram).
Who= Dashrath.
?- mother(Who,ram).
Who= Koushaliya.
?- grandfather(dashrath,Who).
Who= luv.
Who= Kush.

## Dr Mukesh Nandanwar Page 5

Artificial Intelligent & Expert System Lab

EXPERIMENT NO. 4

## AIM: Write a prolog program to find length of given list.

Lists
Lists are powerful data structures for holding and manipulating groups of things.
In Prolog, a list is simply a collection of terms. The terms can be any Prolog data types, including
structures and other lists. Syntactically, a list is denoted by square brackets with the terms separated by
commas. For example, a list of things in the kitchen is represented as
[apple, broccoli, refrigerator]
Rather than having separate location predicates for each thing, we can have one location predicate per
container, with a list of things in the container.
loc_list([apple, broccoli, crackers], kitchen).
loc_list([desk, computer], office).
loc_list([flashlight, envelope], desk).
loc_list([stamp, key], envelope).
loc_list(['washing machine'], cellar).
loc_list([nani], 'washing machine').
The special notation for list structures. [X | Y]
There is a special list, called the empty list, which is represented by a set of empty brackets ([]). It is
also referred to as nil. It can describe the lack of contents of a place or thing.
loc_list([], hall)
Unification works on lists just as it works on other data structures. With what we now know about lists
?- loc_list(X, kitchen).
X = [apple, broccoli, crackers]

X = broccoli

Production rule:

## length increment, length

Clauses:
length([ ] , 0).
length([_ | T], N):- length(T,M), N=M+1.

Queries:
?- length([1,2,3,4,5,6,7], A).
A= 7

?- length([a,b,c,d,e,f,g,h,i], B).
B= 9

C= 3

## Dr Mukesh Nandanwar Page 6

Artificial Intelligent & Expert System Lab

EXPERIMENT NO. 5

AIM: Write a program to find union and intersection of two given sets represented as lists.

Clauses:
member(X,[_|R) :- member(X,R).
member(X,[X|_]).
union([X|Y],Z,W) :- member(X,Z), union(Y,Z,W).
union([X|Y],Z,[X|W]) :- \+ member(X,Z), union(Y,Z,W).
union([],Z,Z).
intersection([X|Y],M,[X|Z]) :- member(X,M), intersection(Y,M,Z).
intersection([X|Y],M,Z) :- \+ member(X,M), intersection(Y,M,Z).
intersection([],M,[]).

Queries:
?- union([1,2,3,4],[1,a,b,4],A).
A = [2,3,1,a,b,4]

?- intersection([1,2,3,4],[1,a,b,4],B).
B = [1,4]

## Dr Mukesh Nandanwar Page 7

Artificial Intelligent & Expert System Lab

EXPERIMENT NO. 6

AIM: Write a program to delete the first occurrence and also all occurrences of a particular
element in a given list.

Clauses:
delete(X,[],[]).
delete(X,[X|L],M):-delete(X,L,M).
delete(X,[Y|L],[Y|M]):-not(X=Y),delete(X,L,M).

Queries:
?delete(a,[a,b,a,c],M).
M=[b,c];
M=[b,a,c];
M=[a,b,a,c];
No

## Dr Mukesh Nandanwar Page 8

Artificial Intelligent & Expert System Lab

EXPERIMENT NO. 7

## Aim: Write compound query for Cat and Fish problem.

Facts are:
Cat likes fish.
Tuna is fish.
Tom is cat.

Production Rules:

Parse tree:

Clauses:
likesToEat(X,Y) :- cat(X), fish(Y).
cat(tom).
fish(tuna).

Queries:
?- likesToEat(X, Y), X = tom.
Y = tuna

?- likesToEat(tom, What)
What = tuna

?- likesToEat(Who, tuna)
Who = tom

## Dr Mukesh Nandanwar Page 9

Artificial Intelligent & Expert System Lab

EXPERIMENT NO. 8

## AIM: Write a program to solve the Monkey Banana problem.

Imagine a room containing a monkey, chair and some bananas. That have been hanged from the center
of ceiling. If the monkey is clever enough he can reach the bananas by placing the chair directly below
the bananas and climb on the chair. The problem is to prove the monkey can reach the bananas.

Clauses:
% initial state: Monkey is at door,
% Monkey is on floor,
% Box is at window,
% Monkey doesn't have banana.
%
% prolog structure: structName(val1, val2, ... )
% state(Monkey location in the room, Monkey onbox/onfloor, box location, has/hasnot banana)
% legal actions

## do( state(middle, onbox, middle, hasnot), % grab banana

grab,
state(middle, onbox, middle, has) ).

## do( state(L, onfloor, L, Banana), % climb box

climb,
state(L, onbox, L, Banana) ).

## do( state(L1, onfloor, L1, Banana), % push box from L1 to L2

push(L1, L2),
state(L2, onfloor, L2, Banana) ).

## do( state(L1, onfloor, Box, Banana), % walk from L1 to L2

walk(L1, L2),
state(L2, onfloor, Box, Banana) ).

## canget(State1) :- % not goal state, do some work to get it

do(State1, Action, State2), % do something (grab, climb, push, walk)
canget(State2). % canget from State2

## canget(State1, Plan) :- % not goal state, do some work to get it

do(State1, Action, State2), % do something (grab, climb, push, walk)
canget(State2, PartialPlan), % canget from State2

## Dr Mukesh Nandanwar Page 10

Artificial Intelligent & Expert System Lab

Queries:
% ?- canget(state(atdoor, onfloor, atwindow, hasnot), Plan).
% Plan = [walk(atdoor, atwindow), push(atwindow, middle), climb, grasp]
% Yes

% No

## % ?- canget(state(Monkey, onfloor, atwindow, hasnot), Plan).

% Monkey = atwindow
% Plan = [push(atwindow, middle), climb, grasp]
% Yes

## Dr Mukesh Nandanwar Page 11

Artificial Intelligent & Expert System Lab

EXPERIMENT NO. 9

## AIM: Write a prolog program to find the family tree.

Clauses:
male(james1).
male(charles1).
male(charles2).
male(james2).
male(george1).

female(catherine).
female(elizabeth).
female(sophia).

parent(charles1, james1).
parent(elizabeth, james1).
parent(charles2, charles1).
parent(catherine, charles1).
parent(james2, charles1).
parent(sophia, elizabeth).
parent(george1, sophia).

Queries:
1 ?- parent(charles1, george1).
No

2 ?- parent(charles1,X).
X = james1 ;
No

3 ?- parent(X,charles1).
X = charles2 ;
X = catherine ;
X = james2 ;
No

## Dr Mukesh Nandanwar Page 12

Artificial Intelligent & Expert System Lab

EXPERIMENT NO. 10

## Let consider the following block problem to understand the problem

A
C
B

Clauses
on(X,Y).
on(a,b).
on_table(b).
on_table(c).
on_table(X).
clear(X).
clear(a).
clear(c).
holding(X).
arm(empty).
unstuck(X,Y):-arm(empty),clear(X),on(X,Y).
stack(X,Y):- holding(X),clear(Y).
pickup(X):-arm(empty),clear(X),on(X).
putdown(X):-holding(X).

Queries:
?- unstuck(a,b).
yes
?- unstuck(b,a).
No

## Dr Mukesh Nandanwar Page 13

Artificial Intelligent & Expert System Lab

EXPERIMENT NO. 11

## AIM: Write a program to solve Tower of Hanoi.

This object of this famous puzzle is to move N disks from the left peg to the right peg using the center
peg as an auxiliary holding peg. At no time can a larger disk be placed upon a smaller disk. The
following diagram depicts the starting setup for N=3 disks.

Production Rules
hanoi(N)move(N,left,middle,right).
move(1,A,_,C)inform(A,C),fail.
move(N,A,B,C)N1=N-1,move(N1,A,C,B),inform(A,C),move(N1,B,A,C).

Diagram

Clauses
% move(N,X,Y,Z) - move N disks from peg X to peg Y, with peg Z being the
% auxilliary peg
%
% Strategy:
% Base Case: One disc - To transfer a stack consisting of 1 disc from
% peg X to peg Y, simply move that disc from X to Y
% Recursive Case: To transfer n discs from X to Y, do the following:
Transfer the first n-1 discs to some other peg X
Move the last disc on X to Y
Transfer the n-1 discs from X to peg Y

## Dr Mukesh Nandanwar Page 14

Artificial Intelligent & Expert System Lab

move(1,X,Y,_) :-
write('Move top disk from '),
write(X),
write(' to '),
write(Y),
nl.
move(N,X,Y,Z) :-
N>1,
M is N-1,
move(M,X,Z,Y),
move(1,X,Y,_),
move(M,Z,Y,X).

Queries:
?- move(3,left,right,center).
Move top disk from left to right
Move top disk from left to center
Move top disk from right to center
Move top disk from left to right
Move top disk from center to left
Move top disk from center to right
Move top disk from left to right
yes