Professional Documents
Culture Documents
THC HNH
X L NGN NG T NHIN
Bui 1: Gii thiu ngn ng lp trnh Prolog
Nguyn Tr Phc
phucnt@uit.edu.vn
Ni dung
1.
2.
3.
4.
5.
6.
7.
8.
9.
2. Ci t SWI-Prolog
a ch ci t:
http://www.swi-prolog.org/download/stable
Phin bn thc hnh:
SWI-Prolog 7.2.3
Sau khi download, ci t v m chng trnh; giao din chng trnh
nh sau:
3. Mt s hm hu ch (1)
Hm duyt file ngun:
Cch 1: ?- consult(tn_file).
Lu : tn_file phi nm trong th mc ang duyt
Cch 2: ?- [ng_dn_file].
V d: ?- [C:\Users\user\Desktop\workspace\kb1.pl].
3. Mt s hm hu ch (2)
Hm in th mc lm vic hin ti:
?- pwd.
Hm in danh sch files th mc hin ti:
?- ls.
Hm load tt c files sau khi edit source code:
?- make.
4. Cc thut ng c bn (1)
Atom:
Bin (Variable):
Chui cc k t bao gm ch ci
v s v du gch di _; k t
u tin l ch thng.
Tt c cc t nm trong du
nhy .
Cc k t t bit.
V d:
nguyen_a, giao_vien.
Nguyen A
+, , ,
V d:
X, Y
Result, Danh_sach_giao_vien
_x1, _y2
4. Cc thut ng c bn (2)
S kin (Fact):
Lut (Rule):
Cu truy vn (query):
V d:
?- sinh_vien(nguyen_a).
Ca trnh
dch Prolog
Cu truy vn
5. Knowlegde base 1:
Cc s kin (facts) v truy vn (1)
File: kb1.pl
Queries
sinh_vien(nguyen_a).
?- sinh_vien(nguyen_a).
true.
?- cham_bai(nguyen_f, pham_e).
true.
?- sinh_vien(nguyen_d).
false.
?- giang_vien(nguyen_g).
false.
?- giao_vu(nguyen_a).
ERROR: toplevel: Undefined
procedure: giao_vu/1 (DWIM could
not correct goal)
sinh_vien(pham_b).
sinh_vien(nguyen_c).
giang_vien(nguyen_d).
giang_vien(nguyen_e).
cham_bai(nguyen_f, pham_e).
5. Knowlegde base 1:
Cc s kin (facts) v truy vn (2)
File: kb1.pl
Queries
sinh_vien(nguyen_a).
?- sinh_vien(X).
X = nguyen_a ;
X = pham_b ;
X = nguyen_c.
sinh_vien(pham_b).
sinh_vien(nguyen_c).
giang_vien(nguyen_d).
giang_vien(nguyen_e).
cham_bai(nguyen_f, pham_e).
?- cham_bai(X,Y).
X = nguyen_f,
Y = pham_e.
?- cham_bai(X,X).
false.
10
6. Knowlegde base 2:
Cc s kin (facts) v lut (rules)
File: kb2.pl
bat(cho, chuot).
Nu .. th: Nu con mo vui th
an_thit(meo).
ng ch vui
buon(chuot).
vui(ong_chu) :- vui(meo).
vui(ba_chu) :- vui(meo), buon(chuot).
11
6. Knowlegde base 2:
Truy vn (Queries)
File: kb2.pl
Queries
bat(cho, chuot).
an_thit(meo).
buon(chuot).
vui(ong_chu) :- vui(meo).
vui(ba_chu) :- vui(meo), buon(chuot).
?- vui(meo).
true.
?- vui(ong_chu).
true.
?- vui(ba_chu).
true.
?- vui(chuot).
false.
?- vui(X).
X = ong_chu ;
X = ba_chu ;
X = meo.
12
7. Knowlegde base 3:
facts, rules, variables
File: kb3.pl
Vn A
Th D
cha(van_a, van_b).
cha(van_b, van_c).
Vn B
me(thi_d, van_b).
me(thi_f, van_c).
bac_si(thi_f).
Vn C
bac_si(thi_d).
bac_si(van_a).
bac_si(X) :- cha(Y,X), bac_si(Y), me(Z,X), bac_si(Z).
Th F
13
7. Knowlegde base 3:
Queries
Vn A
Th D
Vn B
Th F
Vn C
File: kb3.pl
Queries
cha(van_a, van_b).
?- bac_si(van_b).
true.
?- bac_si(X).
X = thi_f ;
X = thi_d ;
X = van_a ;
X = van_b ;
X = van_c .
?- bac_si(X), me(Y,X), bac_si(Y).
X = van_b,
Y = thi_d ;
X = van_c,
Y = thi_f ;
14
false.
cha(van_b, van_c).
me(thi_d, van_b).
me(thi_f, van_c).
bac_si(thi_f).
bac_si(thi_d).
bac_si(van_a).
bac_si(X) :- cha(Y,X),
bac_si(Y), me(Z,X), bac_si(Z).
7. Bi tp:
Vit nhng facts v rules sau:
Harry l thut s.
Hagrid s Dudley.
Tt c thut s u c php thut.
7. Bi gii
Knowledge base:
thuat_si(harry).
so(hagrid,dudley).
co_phep_thuat(X) :- thuat_si(X).
ghet(vernon,X) :- co_phep_thuat(X).
ghet(petunia,X) :- co_phep_thuat(X).
ghet(petunia,X) :- so(X,dudley).
Queries:
?- ghet(petunia,hagrid). true.
8. qui
parent_of(paul,petunia).
parent_of(helen,petunia).
parent_of(paul,lili).
parent_of(helen,lili).
parent_of(albert,james).
parent_of(ruth,james).
parent_of(petunia,dudley).
parent_of(vernon,dudley).
parent_of(lili,harry).
parent_of(james,harry).
nh ngha mt predicate
ancestor_of(X,Y) sao cho
X l t tin ca Y
ancestor_of(X,Y) :- parent_of(X,Z).
ancestor_of(X,Y) :- parent_of(X,Z),
ancestor_of(Z,Y).
17
8. Bi tp
Cho mt bn sau:
connected(1,2).
connected(3,4).
connected(5,6).
connected(7,8).
connected(9,10).
connected(12,13).
connected(13,14).
connected(15,16).
connected(17,18).
connected(19,20).
connected(4,1).
connected(6,3).
connected(4,7).
connected(6,11).
connected(14,9).
connected(11,15).
connected(16,12).
connected(14,17).
connected(16,19).
1. Tm xem c ng
i gia 5 v 10 hay
khng?
2. T im s 1 c th
i ti cc im no?
3. Cc im no i ti
c im 13?
path(X,Y).
path(X,Y) :- connected(X,Z), path(Z,Y).
18
List
Gii thch
[]
19
?- [X,Y | Z] = [1,2,3,4,5].
X = 1,
Y = 2,
Z = [3, 4, 5].
?- [H|T] = [1].
H = 1,
T = [].
?- [_,X,Y,_|_] = [1,2,3,4,5].
X = 2,
Y = 3.
Anonymous
variable
?- [H|T] = [].
false.
List rng ko c phn
t u v cui
20
9. Tm kim
File: kblist.pl
is_member(X,[X|T]).
is_member(X,[X|_]).
is_member(X,[H|T]):-member(X,T).
is_member(X,[_|T]):-member(X,T).
Queries:
?- is_member(1,[1,2,3,4,5]).
true ;
?- is_member(0,[1,2,3,4,5]).
false.
?- is_member(X,[1,2,3,4,5]).
X=1;
X=2;
X=3;
X=4;
X = 5.
21
sum([],0).
sum([H|T],X):-sum(T,X1), X is X1+H.
Queries:
?- get_length([1,2,3,4,5],_length).
_length = 5.
?- sum([1,2,3,4,5],_sum).
_sum = 15.
22
9. S ln nht
File: kblist.pl
max([X],X).
max([H|T],X):-max(T,X1), H>X1 , X=H.
max([H|T],X):-max(T,X1), H=<X1, X=X1.
Queries:
?- max([1,2,3,4,5],_max).
_max = 5 .
23
9. Bi tp
1. Tm phn t cui
?- get_last(X, [a, b, c, d]).
X=d
2. Tm phn t v tr k
?- get_at(X, [a, b, c, d, e], 3).
X=c
9. Bi tp
6. Danh sch thng k cc phn t trng
?-get_count_duplicate([a, b, a, c, b],X).
X = [[2, a], [2, b], [1, c]]
Tng kt
1.
2.
3.
4.
5.
6.
7.
8.
9.