AI Lecture 4 Practical
AI Lecture 4 Practical
AI Lecture 4 Practical
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
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
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.
faster faster
Ardelan Allan Mihtab
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