You are on page 1of 6

Introduction for the prolog programming laboratory for fourth year student

 To run prolog program with write on notepad or on the software follow the following steps
1. Write the fact or rules on the notepad editor or use emacs editor by writing emacs in the
prolog query
2. Save your file with file name.pl(prolog)
3. Open your saved file by double clinking on saved file
4. Write any question on the queries to retrieved from the knowledge base
5. Use consult command to retrieve other file that used before
The predicate and terms must be written with small letter whereas the variable must be
written with uppercase. Otherwise quote the terms.
Example to display HELLO WORLD
 You can type on the queries by saying write(‘HELLO WORLD’). Or
write(‘HELLO ’),write (‘WORLD’).
 Or you can type display:- write(‘HELLO WORLD’). and save it display.pl then
open it and write display. on the queries you will get the same thing
 Try to display Hello
Welcome to
Prolog
First
Programming!
 The consult command is used retrieve the facts and rules which saved before
or after you execute the current program

Example you can take one program parent (daniel,dawite). and save it parent.pl
and open the display.pl program after that type consult(‘parent.pl’). it returns true
and when you type parent(X,Y)., parent(X,dawite).,parent(daniel,Y). returns
different output from the knowledge base

6. In the prolog program to print you use write operation and to insert from the keyboard
you use read operation
Example greeting:- write(‘What is your name ?’),nl,read(X),nl,write (‘Hello ’),write(X).
Try to do the area of triangle by accepting the height and base of the triangle from the
keyboard on the queries
Write the following expression on and see the result which is generated by the query
In prolog 'is' has a special functionality in evaluating arithmetic expressions. But with
condition that the expression should be on the right side of 'is' otherwise it will give an
error. On Prolog Query Prompt :
?- X is 3+2. // expression on right side of 'is' X = 5.
?- 3+2 is X. // expression on left side of 'is' ERROR: is/2: Arguments are not sufficiently
instantiated
?- X = 3+2. // just instantiate variable X to value 3+2 X = 3+2.
?- 3+2 = X. X = 3+2. ?- X is +(3,2). X = 5.
?- 5 is 3+2. true.
?- 3+2 is 5. false.
?- X is 3*2. X = 6.
?- X is 3-2. X = 1.
?- X is -(2,3). X = -1.
?- X is 5-3-1. X = 1.
?- X is -(5,3,1). ERROR: is/2: Arithmetic: `(-)/3' is not a function
?- X is -(-(5,3),1). X = 1.
?- X is 5-3-1. X = 1
?- X is 3/5. X = 0.6.
?- X is 3 mod 5. X = 3.
?- X is 5 mod 3. X = 2.
?- X is 5^3. X = 125.
?- X is (5^3)^2. X = 15625.
?- X = (5^3)^2. X = (5^3)^2.
?- 25 is 5^2. true.
?- Y is 3+2*4-1. Y = 10.
?- Y is (3+2)*(4)-(1). Y = 19.
?- Y is -(*(+(3,2),4),1). Y = 19.
?- X is 3*2, Y is X*2. X = 6, Y = 12.
Exercise :- How does Prolog answer to below Queries
(1) ?- 3<5.
(2) ?- 4<2.
(3) ?- 6>5.
(4) ?- 12<=12.
(5) ?- 12 =< 12.
(6) ?- 3+4 =< 7.
(7) ?- 5=\=5. // 5 is not equal to 5.
(8) ?- 5=\=4.
(9) ?- 5=\=(3+2).
(10) ?- 8=8.
(11) ?- 8=:=8. // 8 equal to 8.
(12) ?- 8=:=9.
(13) ?- (2+1)*10 = 30.
(14) ?- (2+1)*10 =:= 30.
(15) ?- X=2, X<3.
(16)? - X=4, X<3.
(17)? - *(2,3) = 2*3.
(18) ?- X = Z.
(19) ?-(X>3)=(4>3).
(20) ?- X = 3, C is X*X*X.
(21) ?- X = 3, X*X*X is C.
(22) ?- is(Y,^(2,2)). // don't type space between 'is' and bracket
1. Example for rule
A) Write a prolog program which find the area of rectangle, triangle, and square using option
and use the name of predicate display.

display:- write('1. for area of

triangle'),nl, write('2. for the

area of rectangle'),nl, write('3.

for the area of square'),nl,

write('select from your choice

from the above alternative'),nl,

write('Enter your choice from

the given alternative(1-3):'),nl,

read(X),(X==1,area_triangle;X

==2,area_rectangle;X==3,area_

square;

not(x==1;X==2;X==3),msg).

area_triangle:- write('enter base of the triangle'),nl,read(B),

write('enter the height of the triangle'),nl,read(H),nl, write('the area of

triangle is '),Area is (1/2)*B*H,write(Area),nl,again.

area_rectangle:- write('enter length of the rectangle'),nl,read(L),

write('enter the width'),nl,read(W),nl, write('the area of rectangle

is '),Area is L*W,write(Area),nl,again.

area_square:- write('enter side the square'),nl,read(S), write('the area of

square is '),Area is S*S,write(Area),nl,again. msg:-write('sorry! you are not

correct you must enter only (1-3):'),nl,display.


again:-write('are you sure you want to exit? Y/N:'),read(A),

(A== 'y',termi; A=='n',display).

termi:-halt.

2. Example of fact
B) Write a prolog program which display the celebration date of different holiday in 2014
E.C haliday('Ethiopian ChristMass',date(january,7,2013)).
haliday('Eid_al_adha',date(january,10,2013)).
haliday('Epiphany',date(january,19,2013)). haliday('Adwa Victory
day',date(march,2,2013)). haliday('The phrophet Birth Day',date(april,11,2013)).
haliday('Ethiopian Good Friday',date(april,21,2013)). haliday('Ethiopian Easter
Sunday',date(april,23,2013)). haliday('International Labor Day',date(may,1,2013)).
haliday('Freedom Day',date(may,5,2013)). haliday('Derg
Downfall Day',date(may,28,2013)). haliday('Ethiopian New
Year',date(september,11,2013)). haliday('Finding of the True
Cross',date(september,27,2013)). haliday('Eid-al-
Fitr',date(october,24,2013)).
haliday('Eid al-Adha',date(december,31,2013)).

squreroot(16,4).

squre(5,25).

cuberoot(27,3). cube(4,64).

lessthan(3,7).

3. Example Rule
C) Write a prolog program which shows greeting use the name of predicate display.

display:-

write('
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@ '),nl,

write(' @@@@@@@@@@'),write(' Hello how are you doing? '),write('


@@@@@@@@@@@@@'),nl,

write(' @@@@@@@@@@'),write(' I am so fine what about you?


'),write('@@@@@@@@@@@@@'),nl,

write('
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@ ').

4. Example of fact and rule


D) write the prolog program which predict the position of some one using fact and rule and
use the name of predicate find_position.
position('Abebe',director). position('Bekele',superviser).

find_position:- write('for whose position you

want to know?'),nl,

read(Input),position(Input,Output), write('the

position of '),write(Input),

write('is '),write(Output),write('.').

E) write a prolog program which display the grade of course using fact and rule use the
name of predicate show.

course('java',90). course('meltimidia',85).

course('technical_Report',80).

show:-

write('Enter the name of student'),nl,read(Name),write('Enter the name of


course'),nl,read(X),write('Enter the total
mark'),nl,(read(Y),write(Name),tab(5),write(X),tab(5),write(Y),tab(5),((Y>=90,Y=<100),write('
A+');(Y>=85,Y<90),write('A');(Y>=80,Y<85),write('A-');(Y>=75,Y<80),write('B+')),nl;msg).

msg:-write('Enter correct mark'),nl,show.

5. Example of rule

greeting:-

write('What is your name ?'),nl,read(X),nl,write('Hello '),write(X).

F) Prolog program which perform addation of two numbers

add:- write("wel come to addation of two

numbers!"),nl, write('Enter the first number:'),nl,

read(Number_one),nl, write('Enter

the second number:'),nl,


read(Number_two),nl,write('the

sum of the number is:'),Sum is

Number_one +

Number_two,write(Sum).

G) Prolog program which display personal information

displayinformation:- write('displaying personal information')

,nl,write('enter name'),nl,read(Name),nl, write('enter

age'),nl,read(Age),nl, write('address'),nl,read(Address),nl,

write('Your detail is='),nl,write(Name),

tab(3),write(Age),tab(3),write(Address).

You might also like