我认为练习的重点不是让程序在输入句子 [the, capital, of, switzerland, is, bern] 时说“谢谢”,而是使用变量、断言和查询来实现学习和回忆事情,做一些隐约聪明的事情。
你的规则的头部应该是这样的say([the, capital, of, Subj, is, Obj])
,whereSubj
和Obj
是该关系的主语和宾语的变量,这样你的规则就可以匹配多个句子。
动态谓词不应该是say/1
。那是进行知识存储/召回的手工制作的谓词。你应该有一个不同的谓词来存储你的知识,我们称之为capital/2
。
:- dynamic capital/2.
您将使用assertz(capital(a, b)).
在知识库中插入一个事实,并capital(X, Y)
对其进行查询。
在第一个用例中,检查变量是否为基础(这意味着我们被告知某事),并检查我们是否已经不知道相同的事实,然后将事实插入知识库:
say([the, capital, of, Subj, is, Obj]) :-
ground((Subj, Obj)),
\+ capital(Subj, Obj), !,
assertz(capital(Subj, Obj)),
write([thank, you]).
对于第二种情况,我们再次检查变量是否接地,但随后我们进行相反的检查,并说我们已经知道:
say([the, capital, of, Subj, is, Obj]) :-
ground((Subj, Obj)),
capital(Subj, Obj), !,
write([i, already, know, that]).
第三种和第四种情况以类似的方式工作,使用基础变量,但要查找指令与知识库中的内容之间的不匹配:
say([the, capital, of, Subj, is, Obj]) :-
ground((Subj, Obj)),
capital(Subj, Obj1),
Obj1 \= Obj, !,
write([no, you, said, the, capital, of, Subj, is, Obj1]).
say([the, capital, of, Subj, is, Obj]) :-
ground((Subj, Obj)),
capital(Subj1, Obj),
Subj1 \= Subj, !,
write([no, you, said, Obj, is, the, capital, of, Subj1]).
最后,最后一种情况是查询回答,所以Subj
或Obj
(或两者)必须是非地面变量,我们打印知识库中的内容:
say([the, capital, of, Subj, is, Obj]) :-
(var(Subj) ; var(Obj)),
capital(Subj, Obj), !,
write([the, capital, of, Subj, is, Obj]).
请注意,由于程序使用了 cut ( !
) 和assertz/1
,因此规则的顺序很重要。更改顺序会产生不正确的结果。
这是完整的程序:
:- dynamic capital/2.
say([the, capital, of, Subj, is, Obj]) :-
ground((Subj, Obj)),
capital(Subj, Obj1),
Obj1 \= Obj, !,
write([no, you, said, the, capital, of, Subj, is, Obj1]).
say([the, capital, of, Subj, is, Obj]) :-
ground((Subj, Obj)),
capital(Subj1, Obj),
Subj1 \= Subj, !,
write([no, you, said, Obj, is, the, capital, of, Subj1]).
say([the, capital, of, Subj, is, Obj]) :-
ground((Subj, Obj)),
\+ capital(Subj, Obj), !,
assertz(capital(Subj, Obj)),
write([thank, you]).
say([the, capital, of, Subj, is, Obj]) :-
ground((Subj, Obj)),
capital(Subj, Obj), !,
write([i, already, know, that]).
say([the, capital, of, Subj, is, Obj]) :-
(var(Subj) ; var(Obj)),
capital(Subj, Obj), !,
write([the, capital, of, Subj, is, Obj]).
和试运行:
?- say([the, capital, of, switzerland, is, bern]).
[thank,you]
true.
?- say([the, capital, of, switzerland, is, bern]).
[i,already,know,that]
true.
?- say([the, capital, of, switzerland, is, zurich]).
[no,you,said,the,capital,of,switzerland,is,bern]
true.
?- say([the, capital, of, france, is, bern]).
[no,you,said,bern,is,the,capital,of,switzerland]
true.
?- say([the, capital, of, What, is, bern]).
[the,capital,of,switzerland,is,bern]
What = switzerland.
?- say([the, capital, of, switzerland, is, What]).
[the,capital,of,switzerland,is,bern]
What = bern.