我创建了一个鸟类识别系统,该系统基于一些知识库来识别鸟类家族。只有当用户对提示的问题回答"是"或"否"时,识别过程才会发生。这是一个基本的示例代码。我使用了一个断言值的history/3谓词,但我现在已经删除了它,以便在这里发布。加载知识库的主文件如下所示:
go :-
greeting,
repeat,
write('> '),
read(X),
do(X),
X == quit.
greeting :-
write('This is the Native Prolog shell.'), nl,
write('Enter load, consult, or quit at the prompt.'), nl.
do(load) :- load_kb, !.
do(consult) :- solve, !.
do(quit).
do(X) :-
write(X),
write('is not a legal command.'), nl,
fail.
load_kb :-
write('Enter file name: '),
read(F),
reconsult(F).
solve :-
retractall(known(_,_,_)),
top_goal(X),
write('The answer is '), write(X), nl.
solve :-
write('No answer found.'), nl.
ask(A,V):-
known(yes,A,V),!. %%% succeed if true.
ask(A,V):-
known(_,A,V), %%% fail If false.
!,fail.
ask(A,V):-
write(A: V), %%% ask user
write('?:'),
read(Y), %%% get the answer
asserta(known(Y,A,V)), %%% remember it
Y == yes. %%% succeed or fail.
menuask(A,V,MenuList):-
write('What is the value for'),write(A), %%% asking user
write('?'),nl,
write(MenuList),
read(X), %%% get the value
check_val(X,A,V,_), %%% checks the value entered is in the menu.
asserta(known(yes,A,X)), %%% if yes remembers it.
X==V.
check_val(X,_,_,MenuList):-member(X,MenuList).
check_val(X, A, V, MenuList) :
write(X),
write(' is not a legal value, try again.'), nl,
menuask(A, V, MenuList).
我使用了known/3谓词来存储用户输入的值。我得到了输出,但当被问及如何(进球(时,我也想得到结论的证明。
示例:我得到了如下所示的完美输出。
?- go.
This is the Native Prolog shell.
Enter load, consult, or quit at the prompt.
> load.
Enter file name: |: load.
> |: consult.
nostrils:external_tubular?:|: yes.
live:at_sea?:|: yes.
bill:hooked?:|: yes.
What is the value forsize?
[large,plump,medium,small]|: large.
wings:long_narrow?:|: yes.
color:white?:|: yes.
The answer is laysan_albatross
但我也想得到这个:
?- how(laysan_albatross).
bird(laysan_albatross): family(albatross)color(white)
family(albatross): order(tubenose)size(large)wings(long_narrow)
order(tubenose): nostrils(external_tubular)live(at_sea)bill(hooked)
显然,这是在追溯过程中使用的规则,但我不知道如何破解它。我陷入了困境。非常感谢您的帮助。
谢谢。。
有跟踪谓词(SWI-Prolog有gtrace,更容易理解(。
如果您想要一个解释而不是跟踪,请搜索"元循环解释器"。它也出现在大多数Prolog教科书中;如果你不能理解其中一个,那就试试另一个(我碰巧喜欢奥基夫的Prolog Craft,但它可能有点让人不知所措(。
这种口译员的一个例子是http://www.amzi.com/distribution/files/xsip_book.pdf。。。查找"为什么要问"部分(第41页(。不过,代码可能比您的示例所需的更复杂。