Professional Documents
Culture Documents
Chuong 7 - Prolog
Chuong 7 - Prolog
loves(chuck X)
loves(chuck,
call exit
female(X) rich(X)
fail redo
female(jane).
f l (j )
female(mary). female(X)
female(susan)
female(susan).
female(jane)
?- female(X).
X = jane
j
female(mary)
X = mary
X = susan
female(susan)
Yes
1 ?- consult('C:\\prolog\\dragon.pl').
% C:\prolog\dragon.pl compiled 0.00 sec, 12,468 bytes
true.
true
Trí tuệ nhân tạo
28
Các lệnh của trò chơi
?- 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.
true.
true
true.
true.
?- e.
The door appears
pp to be locked.
You can't go that way.
true.
true.
?- look.
Y are in
You i a big,
bi d dark
k cave. Th
The air
i is
i fetid.
f tid
true.
Đầ vào
Đầu à ((output):
t t) sử ửddụng write(...)
it ( ) để đ đưa ra các
á hiể
hiển
thị
ụ g n
Sử dụng nl để kết thúc dòng
g hiển thịị ((để bắt đầu viết
dòng hiển thị mới)
Ví dụ: describe(closet) :-
write('You
it ('Y are ini an old
ld storage
t closet.'),
l t ') nl.
l
meadow
b ildi
building closet
l t
p
path(meadow,
( s, building).
g)
path(building, n, meadow).
há listing(predicate)
Cú pháp: li ti ( di t )
?- listing(at).
at(key,
(k cave_entrance).)
at(flashlight, building).
at(sword, closet).
true.
n :- go(n).
g ( )
s :- go(s).
e :- go(e).
w :- go(w).
( )
go(_) :-
write('You can''t go that way.').
take(X) :-
holding(X),
write('You\'re
it ('Y \' already
l d hholding
ldi it!')
it!'), nl.
l
take(X) :-
write('I
it ('I don\'t
d \'t see it here.'),
h ') nl.
l
Trí tuệ nhân tạo
41
Sử dụngg từ khóa fail
Một vị từ thất bại (fail) nếu nó không thành công
(không thể chứng minh được)
Có thể phát biểu rõ ràng một mệnh đề là thất bại,
bằng cách sử dụng fail
call
fail
fail
call exit
!
drop(A) :-
write('You
write( You aren\
aren\'tt holding it!
it!')), nl
nl.