AI Lecture 4 Practical

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Components of Prolog

Lecture 4
Program Structure
• Prolog program structure consists of five segments, not all of them
must appear in each program.
• The following segment must be included in each program
• Predicates
• Clauses
• Goal.
Program Structure 2
1. Domains: define global parameter used in the program.
Domains
I,i = integer 2. Database: define internal data base generated by the program
C = char
Database
S = string
Greater(integer)
R = real

3. Predicates: define rule and fact used in the program.


Predicates
mark(symbol,integer).

4. Clauses: define the body of the program.. For the above predicates the clauses portion may contain
Clauses
mark(a, 20).

5. Goal: can be internal or external, internal goal written after clauses portion , external goal supported by the prolog
compiler if the program syntax is correct This portion contains the rule that drive the program execution.
Goal
mark(ari, Mark).
Predefined in Prolog
Mathematical operations Symbols
Addition +
subtraction -
multiplication *
Division /
Remainder of division mod
greater > 
Less than < 
Equal =
Not equal <> 
Greater or equal >=
Less than or equal <=
Predefined in Prolog 2
Mathematical Functions symbol

cos(X) Return the cosine of its argument


sine(X) Return the sine of its argument
tan(X) Return the tranget of its argument
exp(X) Return e raised to the value to which X is bound
ln(X) Return the natural logarithm of X (base e)

log(X) Return the base 10 logarithm of log 10x

sqrt(X) Return the positive square of X

Return the rounded value of X. Rounds X up or down to


round(X) the nearest integer

trunc(X) Truncates X to the right of the decimal point

abs(X) Return the absolute value of X


Predefined in Prolog 3
I/O Functions Symbols

readint(Var) : read integer variable.

readchar(Var) : read character variable.

readreal(Var) : read read (decimal) variable.

readln(Var) : read string.

write(Var) : write variable of any type.


Example 1
Write a prolog program to read a number and show it on the screen.
Domains
I = integer
Predicates
print.

Clauses
print:- write ("Enter a number :"),
readint(I),
write("The number is :",I).

Goal
print.
Example 2
Write a prolog program that reads two numbers and prints the greater number.
Domains
i = integer

Predicates
nondeterm compare(i,i)

Clauses
compare(X,Y):- X > Y , write(X," is greater than ",Y).
compare(X,Y):- write (X," is smaller ",Y).

Goal
compare(8,9).
Example 3
What would happen if the two numbers are equal and prints the equal numbers.
Domains
i = integer

Predicates
nondeterm compare(i,i)
Clauses
compare(X,Y):- X > Y , write(X," is greater than ",Y).
compare(X,Y):- X < Y , write (X," is smaller ",Y).
compare(X,Y):- write(X," equals ",Y).
Goal
compare(8,9).
Example 3
Write a Prolog program to sum two number then display the result.

Two Solutions
1. sum(X,Y)  the result needs to be printed.

2. sum(X,Y,R)  it will return the result.

Which one is better any, Why?


Activity 1
Write a program to represent the following diagram, adapt your program to meet the following
goals:
Write the facts then write rule for :
1. Sister: sister(X,Y)
2. Brother: brother(X,Y)
3. Parent: parent(X,Y)
4. Aunt: aunt(X,Y).
5. Uncle: uncle(X,Y).
6. Grandfather: gfather(X,Y).
7. Grandmother: gmother(X,Y).
Activity 2
Write a prolog program to implement the following scenario:-
Aram is faster than Mariwan
Ardelan is faster than Allan
Allan is faster than Mihtab
faster
Aram Mariwan

faster faster
Ardelan Allan Mihtab

Find all the people who are faster than others.


Activity 1
Write a program to represent the following diagram, adapt your program to meet the following
goals:
Write the facts then write rule for : Nasrin

1. Sister: sister(X,Y)
2. Brother: brother(X,Y)
3. Parent: parent(X,Y)
4. Aunt: aunt(X,Y).
5. Uncle: uncle(X,Y).
6. Grandfather: gfather(X,Y).
7. Grandmother: gmother(X,Y).
Ali Nasrin

Fata Xula Hama


Xaja
Ali Nasrin

Fata Xula Hama


Xaja

You might also like