AI Lab Manual Prolog Programs
AI Lab Manual Prolog Programs
List of Programs
1. Study of SWI Prolog IDE and steps in the execution of prolog program.
c. To use operators.
d. To use loops.
6. Program to solve the Water Jug Problem using DFS, BFS blind search algorithms.
9. Program to find the optimal path between two cities using best first search and A* heuristic
algorithms.
3. Create a prolog source file and save it with .pl extension in any disk file.
4. The prolog editor opens. Type the program and save.
8. At the prompt, type your query and press enter to view the results.
Exercise 2: Simple Programs in Prolog
2. a) Programs to represent Facts and Rules.
Prolog programs describe relations, defined by means of clauses. There are two types of
clauses: facts and rules.
Facts
Clauses with empty bodies are called facts.
To represent the fact Tom is a cat, we specify cat (tom). This is equivalent to the rule cat(tom):-
true.
The built-in predicate true is always true. Given the above fact, one can ask: Is tom a cat?
?- cat(tom).
Yes
what things are cats?
?- cat(X).
X = tom
Rules
Clauses with bodies are called rules. A rule is of the form Head:- Body. It is read as "Head is
true if Body is true". A rule's body consists of calls to predicates, which are called the rule's
goals. The built-in predicate ‘,’ denotes conjunction of goals and ‘; ‘denotes disjunction.
Conjunctions and disjunctions can only appear in the body, not in the head of a rule.
To represent the relation that all cats are animals, we specify the rule as animal(X) :- cat(X).
If we add this rule and ask what things are animals?
?- animal(X).
X = tom
Program1:
weather(vellore,hot,summer).
weather(coimbatore,warm,summer).
weather(pondy,warm,winter).
weather(ooty,cool,winter).
warmer_than(C1,C2):-weather(C1,hot,summer), weather(C2,warm,summer).
Output:
Is Vellore warm in summer?
?- weather(vellore,warm,summer).
false.
Is Vellore hot in summer?
?- weather(vellore,hot,summer).
true.
Which City is hot in summer?
?- weather(X,hot,summer).
X=vellore.
?- warmer_than(City1,City2).
City1 = vellore,
City2 = coimbatore
Program2:
person(john,smith,40,london,engineer).
person(mary,jones,30,amsterdam,doctor).
person(mike,wilson,50,london,lawyer).
person(mark,robert,23,london,non-graduate).
graduate(X):-person(X, _,_,_,engineer); person(X, _,_,_,doctor); person(X, _,_,_,lawyer).
Output:
Who lives in London?
?- person(X,_,_,london,_).
X = john ;
X = mike ;
X = mark.
Program 3:
player(anand).
plays(X,chess):-player(X).
plays(anand,chess).
playedMatch(X,Y):-plays(X,chess), plays(Y,chess).
worldChampion(X,chess):-player(X).
worldChampion(anand,chess).
win(X,Y):-playedMatch(X,Y).
beat(X,Y):-playedMatch(X,Y), win(X,Y).
beat(anand,gary).
Output:
Who plays chess?
?- plays(X,chess).
X = anand ;
Do Anand play Chess?
- plays(anand,chess).
true .
Was Anand a world Champion?
?-worldChampion(anand,_).
true.
Was Prabhu a world Champion?
?- worldChampion(prabhu,_).
false.
Who was the world champion in Chess?
?- worldChampion(X,chess).
X = anand
Did Anand beat Gary?
?-beat(anand,gary).
true.
Exercise 2b: Simple Program to read and write input using read, write and writeq built-
in predicates.
The built-in predicate read is provided to input terms. It takes a single argument, which must
be a variable.
?- read(X).
: jim.
X = jim.
?- read(X).
: 26.
X = 26.
The main built-in predicate provided to output terms is write/1. The write/1 predicate takes a
single argument, which must be a valid Prolog term.
?- write(26),nl.
26
yes
?- write('a string of characters'),nl.
a string of characters
Yes
?- writeq('a string of characters'),nl.
'a string of characters'
Yes
The built-in predicates get and put can also be used to read and write respectively.
Program1: To read and print using get0 and put built in predicates.
a
r
e
y
o
u
This will not terminate and is an endless loop asking for user to enter a term.
Program2: To read a number and print its cube.
cube :- write('Write a number: '), read(Number), process(Number).
process(stop) :- !.
process(Number) :- C is Number * Number * Number,
write('Cube of '),write(Number),write(': '),write(C),nl, cube.
Output:
?- cube.
Write a number: 6.
Cube of 6: 216
Write a number: |: 4.
Cube of 4: 64
Write a number: |:
Exercise 2c: Program to use operators.
Output:
?- largest(65,5).
65
true.
?- largest(5,-3).
5
true.
?- largest(30,500).
500
true.
average(X,Y):-Z is (X+Y)/2,
write('Average is'),
write( Z).
Output:
?- average(35,7).
Average is 21
true.
Exercise 2d: Simple Program to use loops.
Program1: To print a given number by subtracting a value of 1 until it becomes 0.
loop(0).
loop(N):-N>0, write(N),nl,
M is N-1, loop(M).
Output:
?- loop(6).
6
5
4
3
2
1
true.
Output:
?- fibonacci(6).
0
1
1
2
3
5
true.
Program 3: To print the factorial of a number.
fac(0,1).
fac(N,F):-( N>0->
(
N1 is N-1,
fac(N1,F1),F is N*F1
);write('Enter a non-zero value')
).
Output:
?- fac(6,X).
X = 720
?- fac(0,X).
X=1
Program 4: To find prime or not
prime(X):-X>1,loop(X,2);
write('Neither prime nor composite').
loop(X,I):-I=<X/2,
(mod(X,I)=:=0,
fin(1);
J is I+1,
loop(X,J));
fin(0).
fin(A):-A=:=0,
write('Prime');
write('Composite').
Output:
?- prime(7).
Prime
true .
?- prime(10).
Composite
true
Exercise 2e: To perform list processing.
A list is either empty or it is composed of a first element (head) and a tail, which is a list
itself. In Prolog we represent the empty list by the atom [] and a non-empty list by a term
[H|T] where H denotes the head and T denotes the tail.
Some example lists are shown below:
[] - empty list
[a] - singleton list
[hello, world] - 2 element list
[[1,2,3,4], p, this] - 3 element list
[[[1, 2], [3, 4]], [[a, b], [x, y]]] - nested list (3 level nesting)
Output :
?- likes(john,X).
X = jane ;
X = tom.
Here, jane and tom are substituted for X.
Exercise 4: Backward Chaining:
Backward chaining involves trying to prove a given goal by using rules to generate sub-goals
and recursively trying to satisfy those. The backward and forward chaining techniques are
used by the inference engine as strategies for proposing solutions or deducing information.
Prolog (PROgramming in LOGic) represents programs as logical Horn clauses and treats
execution as answering queries with backward chaining.
Program:
male(albert).
male(edward).
female(alice).
female(victoria).
parents(edward, victoria, albert).
parents(alice, victoria, albert).
sister(X,Y) :- female(X), parents(X,M,F), parents(Y,M,F), not(X=Y).
Output:
?- sister(X,Y).
X = alice,
Y = edward ;
?- sister(alice, X).
X = edward ;
/* classification rules */
mammal :- verify(has_hair), !.
mammal :- verify(gives_milk).
bird :- verify(has_feathers), !.
bird :- verify(flys), verify(lays_eggs).
carnivore :- verify(eats_meat), !.
carnivore :- verify(has_pointed_teeth), verify(has_claws), verify(has_forward_eyes).
ungulate :- mammal, verify(has_hooves), !.
ungulate :- mammal, verify(chews_cud).
Output:
?- go.
Does the animal have the following attribute: has_hair? yes.
Does the animal have the following attribute: eats_meat? |: yes.
Does the animal have the following attribute: has_tawny_color? |: yes.
Does the animal have the following attribute: has_dark_spots? |: yes.
I guess that the animal is: cheetah
true.
?- go.
Does the animal have the following attribute: has_feathers? yes.
Does the animal have the following attribute: does_not_fly? |: yes.
Does the animal have the following attribute: has_long_neck? |: yes.
I guess that the animal is: ostrich
true.