AI Lab Manual Dessalew
AI Lab Manual Dessalew
Lab manual
By: Dessalew G.
AI Lab Manual By Dessalew 04/15/2024
Lab class one
1. Introduction
2. Understanding prolog programming
3. Installing developing tools
4. Starting basic prolog programing
5. Executing the first code
6. Exercise for lab one
For efficiently building AI systems one should know at least one programming
language
In our lab session we are going to learn prolog programming, which is one of the
For example we can represent the fact that it is sunny by writing the program :
sunny.
More complicated facts consist of a relation and the items that they refers to.
Consider the following sentence : 'All men are mortal' We can express this thing in Prolog
by :
mortal(X) :- human(X).
The clause can be read as 'X is mortal if X is human'.
To continue with this example, let us define the fact that Socrates is a human. Our program will be :
mortal(X) :- human(X).
human(socrates).
2. Compiler (SWI-PROLOG)
Both of these software are open source, they are available freely.
When you open notepad++ and SWI-PROLOG the following interfaces are displayed
respectively.
AI Lab Manual By Dessalew 04/15/2024 8
Cont…
1. The program:
In prolog, We declare some facts and rules
query
Variables are declared with Capital letters (start with capital letter)
If :-
Or ;
Not not
If the answer for the query exists on the facts and the rules the out put will be true,
otherwise the answer is false.
AI Lab Manual By Dessalew 04/15/2024 18
Cont…
Queries with
eats(sam,What). one variable
eats(Who,curry).
compatible(sam, Who). queries
Compatible(Who,rajiv)
Queries wit
h more
than one va
eats(Who,What). riable
AI Lab Manual By Dessalew 04/15/2024 23
Cont…
vertical(line(point(X,Y), point(X,Y2))).
horizontal(line(point(X,Y), point(X2,Y))).
member( b, [a,b,c]) is
true
member( b, [a,[b,c]] )
is not true,
but member([b,c], [a,[b,c]] )
true
AI Lab Manual By Dessalew 04/15/2024 28
Cont…
The program for the membership relation can be based on the following observation: X is a member of L
if either if
1. X is the head of L, or
This can be written in two clauses, the first is a simple fact and the second is a rule:
member( X, [X I Tail] ).
member( X, Tail).
AI Lab Manual By Dessalew 04/15/2024 29
List unification
The following table gives examples of list unification:
append([1,2,3],[5,6],L).
Then we gate the concatenated list:
L= [1,2,3,5,6].
X is 3+4 = 7 addition
X is 5-3 =2 subtraction
X is 3*4= 12 multiplication
X is 5/2 =2.5 division
X is 5//2 = 2 integer division
X is 75 mod 12 =3 modulus
X is 2**3 = 8 2 the power of three
avg_temp(dire, 100).
avg_temp(harar, 68).
avg_temp_cel(Town, C_Temp):-
avg_temp(Town, F_Temp),
C_Temp is (F_Temp - 32) * 5//9.
just_ate(deer, grass),
it means
is_digesting(tiger, grass),
this will be true if
So if we want to express the predecessor logic, that can be expressed using the
following diagrams
Z
Predecessor
Z
X
Y2
Predecessor
Y1
Z
Y2
Y2
Predecessor
Predecessor
parent
Y
X
Z
…
Predecessor
04/15/2024
40
Cont…
So we can understand the predecessor relationship is recursive.
%f(0) =0
%f(1)=2
%f(2)=3
function(X,Y) :- X1 is X-1,
X2 is X-2,
function(X1,Y1),
function(X2,Y2),
E.g.
?- S = “Vicky“. Query
?- atom_codes(vicky,S). query
S = [118,105,99,107,121] yes response
say_hi :-
write(‘ what is your name’),
read(X), String wise
write(‘Hi’),
write(X) .
put(X), nl.
no response
Obviously:
The functor
The arity
The argument
e.g.
?- functor(friends(lou,andy),F,A). query
F = friends
responses
A=2
?- functor(friends(lou,andy,eld),F,A). query
F = friends
responses
A=3
?- functor(mia,F,A). query
F = mia
response
A=0
?- functor(14,F,A). query
F = 14
response
A=0
e.g.
?- functor(Term,friends,2). query
Term = friends(_,_) response
1. A number N
2. A complex term T
?- arg(2,likes(lou,andy),A). query