Professional Documents
Culture Documents
Chuong 7 - Prolog
Chuong 7 - Prolog
Ni dung mn hc:
Tc t
Hc my
Lp k
hoch
Tr tu nhn to
Lp
p trnh logic
g
Tr tu nhn to
Ngn
g ng
g lp
p trnh logic
g Prologg
Mt
t ngn
g ng
g lp
p ttrnh logic
og c c s d
dng
g rt
t p
ph
b
bin
a
ra bi ngi
i dng,
d
l nh
h l cn
chng
h minh
i h
Phn mm SWI-Prologg
Tr tu nhn to
T l mt
Tun
t sinh
i h vin
i ca
HUT
studentHUT(tuan)
studentHUT(tuan).
studDiscretMath(X) :- studentHUT(X).
?- studDiscretMath(tuan).
(
)
Tr tu nhn to
studDiscretMath(X) :: studentHUT(X).
studentHUT(X)
studentHUT(tuan).
t d tHUT(t
)
?- studDiscretMath(tuan).
Tr tu nhn to
Tr tu nhn to
(Vi h iu
i hnh
h h Windows),
Wi d
) kch
k h p
ln
l tp
t tin
ti chng
h
t h
trnh,
hoc
Chy phn mm SWI-Prolog, v tham vn (consult) ti tp tin
chng
h
t h
trnh
?- consult('C:\\PrologPrograms\\myPrologProg.pl').
Sau ,
a ra cu hi mong mun
?- studDiscretMath(tuan).
Prolog
o og a ra
a cu ttr
li
Yes
Tr tu nhn to
Prolog
P
l tr
t v
gi
i tr
t No
N c
ngha
h l khng
kh th chng
h
minh
? stud_DiscretMath(hai).
?stud DiscretMath(hai)
No
10
C p
php:
p Cc cu trc
Cc v d ca cu trc
sunshine
man(socrates)
Tr tu nhn to
11
C p
php:
p Cc mnh c s
Mt mnh
h c s
biu
bi din
di mt
t s kin
ki (fact)
(f t)
Cc v d ca mnh c s
j hn
john.
mary
bill
loves(john, mary).
loves(mary, bill).
Tr tu nhn to
12
C p
php:
p Biu din lut
Mt cu
trc
t (biu
(bi din
di mnh
h kt lun
l ca
lut
l t mnh
h
THEN)
K hiu :Mt danh sch cc cu trc (biu din mnh gi thit ca lut
mnh IF), ngn cch nhau bi du ,
Cc v d ca lut
mortal(X) :- man(X).
happy(X)
ppy( ) :- healthy(X),
y( ) wealthy(X),
y( ) wise(X).
( )
Tr tu nhn to
13
C p
php:
p Cc v t logic
g
Mt v t (predicate) l mt tp hp cc mnh vi
cng tn v mt s (>
(>=1)
1) cc tham s
V d: Cc mnh biu din v t loves
loves(john mary).
loves(john,
mary)
loves(mary, bill).
loves(chuck, X) :- female(X), rich(X).
Tr tu nhn to
14
C p
php:
p Chngg trnh
Cc mnh ca mt v t c s dng (c xt n)
theo ng trt t ca chng trong chng trnh
Tr tu nhn to
15
C p
php:
p Cc bin v hngg
Vd: x, socrates
hin th k t
t nhy
nh n,
n th phi s dng:
d ng '' hoc \
\'
Tr tu nhn to
16
C php: Ch thch
H c
Hoc
t sau du
d %
V d
parent(x,y).
t(x ) % s
s kin
ki m t x l cha
h m
m ca
y
Tr tu nhn to
17
C p
php:
p Cc li hay ggp
p
Khng c
Kh
c
khong
kh trng
t (space
(
characters)
h
t ) gia
i
tn v danh sch tham s ca mt cu trc
Biu din hp
p l:
man(socrates).
(
)
Tr tu nhn to
18
Gi s ngi dng t cu hi
loves(chuck, X)
female(X) = female(jane)
female(jane), X = jane.
jane
rich(jane).
: tht bi (khng th chng minh c!)
female(X) = female(mary), X = mary.
rich(mary)
rich(mary).
: thnh cng (chng minh c!)
Tr tu nhn to
19
Cc li gi
g
exit
redo
fail
Cng
g fail kt ni vi cng
g redo
Tr tu nhn to
20
loves(chuck,
(
X)) :- female(X),
( ) rich(X).
( )
loves(chuck X)
loves(chuck,
call
fail
female(X)
Tr tu nhn to
rich(X)
exit
redo
21
Cc cu tr li b sungg
female(jane).
f
l (j
)
female(mary).
female(susan)
female(susan).
female(X)
?- female(X).
X = jane
j
X = mary
X = susan
Yes
Tr tu nhn to
female(jane)
female(mary)
female(susan)
22
Cc cch c
C h c
Cch
theo
th kiu
ki khai
kh i bo
b thng
th c
a thch
th h hn
h
Tr tu nhn to
23
Logic
g n iu
th i
iu s
mi
i
mi ng
Logic n iu khng ph hp vi cc bi ton (ng
dng) thc t
Tr tu nhn to
24
Logic
g khngg n iu
Trong Prolog,
Prolog cc s kin v cc lut c th c thay i
mt thi im no
25
Cc v d v assert v retract
assert(man(plato)).
assert((loves(chuck,X)
t((l
( h k X) :- female(X),
f
l (X) rich(X))).
i h(X)))
retract(man(plato)).
retract((loves(chuck X) :- female(X),
retract((loves(chuck,X)
female(X) rich(X)))
rich(X))).
trnh xy ra li c php
Gi s
nu
vit:
it assert(foo
ss t(f ::- bar,
b
b )
baz).
S c bao nhiu tham s i vi assert?
Tr tu nhn to
26
Cc ng
g dng
g tr chi l mt v d in hnh v cc
bi ton m trong cc s vic c th
thay i
Tr tu nhn to
27
28
Cc lnh ca tr chi
?- start.
E t commands
Enter
d using
i standard
t d dP
Prolog
l syntax.
t
Available commands are:
start.
-- to start the game.
n. s. e. w.
-- to
t go in
i th
thatt direction.
di
ti
take(Object).
-- to pick up an object.
drop(Object).
-- to put down an object.
use(Object).
(Obj t)
-- to
t use an object.
bj t
attack.
-- to attack an enemy.
look.
-- to look around you again.
i
instructions.
i
-- to see this
hi message again.
i
halt.
-- to end the game and quit.
Tr tu nhn to
29
Bt u chngg trnh
Tr tu nhn to
30
i v hngg nam
?- s.
You are in a small building. The exit is to the
north. The room is devoid of furniture, and the
only
y feature seems to be a small door to the east.
There is a flashlight here.
true.
Tr tu nhn to
31
Ly vt, Ca b kha
?- take(flashlight).
OK.
OK
true.
?- e.
The door appears
pp
to be locked.
You can't go that way.
true.
Tr tu nhn to
32
?- use(key).
The closet is no longer locked.
true.
?- look.
Y are in
You
i a big,
bi d
dark
k cave. Th
The air
i is
i fetid.
f tid
There is a chest here.
true.
Tr tu nhn to
33
V tr ca cc vt:
at(flashlight,
at(flashlight building)
building).
Ti ang gi (nm) vt g:
holding(key).
holding(key)
Tr tu nhn to
34
u vo v u ra
Tr tu nhn to
35
Bn ca tr chi
N
W
E
S
cave_entrance
cave
meadow
b ildi
building
Tr tu nhn to
closet
l
t
36
Biu din bn
path(cave, w, cave_entrance).
path(cave_entrance,
th(
t
e, cave).
)
p
path(meadow,
(
s, building).
g)
path(building, n, meadow).
Hoc c th biu din nh sau:
path(cave, w, cave_entrance).
path(X, e, Y) :- path(Y, w, X).
Tr tu nhn to
37
Lit k
Tr tu nhn to
38
Tr tu nhn to
39
V t go
g
go(Direction) :i_am_at(Here),
path(Here, Direction, There),
retract(i am at(Here)),
retract(i_am_at(Here)),
assert(i_am_at(There)),
look.
go(_) :write('You can''t go that way.').
Tr tu nhn to
40
V t take
take(X) :i am at(Place)
i_am_at(Place),
at(X, Place),
retract(at(X, Place)),
assert(holding(X)),
assert(holding(X))
write('OK.'),
nl.
take(X) :holding(X),
write('You\'re
it ('Y \' already
l
d h
holding
ldi it!')
it!'), nl.
l
take(X) :write('I
it ('I don\'t
d \'t see it here.'),
h
') nl.
l
Tr tu nhn to
41
fail
Tr tu nhn to
42
Ct
Tr tu nhn to
exit
43
S dngg kt hp
p ct-tht bi
Tr tu nhn to
44
V d v !, fail
45
Th cc vt
drop(A) :h ldi (A)
holding(A),
i_am_at(B),
retract(holding(A)),
retract(holding(A))
assert(at(A, B)),
write('OK.'),
( K ), nl.
drop(A) :write('You
write(
You aren\
aren\'tt holding it!
it!')), nl
nl.
Tr tu nhn to
46