Professional Documents
Culture Documents
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.
read(X),(X==1,area_triangle;X
==2,area_rectangle;X==3,area_
square;
not(x==1;X==2;X==3),msg).
is '),Area is L*W,write(Area),nl,again.
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('
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@ ').
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:-
5. Example of rule
greeting:-
read(Number_one),nl, write('Enter
Number_one +
Number_two,write(Sum).
age'),nl,read(Age),nl, write('address'),nl,read(Address),nl,
tab(3),write(Age),tab(3),write(Address).