使用 dynamic/1 和 var/1 创建允许以下内容的 Prolog 子句

Use dynamic/1 and var/1 Create Prolog clauses that allow the following

?- say([the, capital, of, switzerland, is, bern]). 
Thank you. 
?- say([the, capital, of, switzerland, is, bern]). 
I already know that. 
?- say([the, capital, of, switzerland, is, zurich]). 
No, you said the capital of switzerland is bern. 
?- say([the, capital, of, france, is, bern]). 
No, you said bern is the capital of switzerland. 
?- say([the, capital, of, What, is, bern]). 
What = switzerland. 
?- say([the, capital, of, switzerland, is, What]). 
What = Bern. 

最后两个很简单,我用say([the, capital, of , switzerland, is, bern])。 但是如何让Prolog输出Thank you而不是true呢?那其他句子呢?非常感谢。

我试着写了一些代码,但效果不是很好。

:- dynamic say/1.
say([the, capital, of, switzerland, is, bern]) :- write('Thank you').
say([the, capital, of, switzerland, is, bern]) :- write('I already know that.').
say([the, capital, of, switzerland, is, X]):-
    X\==bern, write('No, you said the capital of switzerland is bern.').
say([the, capital, of, X, is, bern]):-
    X\==switzerland, write('No, you said bern is the capital of switzerland.').

我认为练习的重点不是让程序在输入句子 [the, capital, of, switzerland, is, bern] 时说 "Thank you",而是使用变量,assert ,以及让它学习和回忆事物,并做一些模糊智能的事情的查询。

你的规则的头部应该是类似 say([the, capital, of, Subj, is, Obj]) 的东西,其中 SubjObj 是那个关系的主题和对象的变量,这样你的规则就可以匹配多个句子。

动态谓词不应该是say/1。那就是做知识的手工制作的谓词store/recall。你应该有一个不同的谓词来存储你的知识,我们称之为 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]).

最后,最后一个案例是查询回答,所以SubjObj(或两者)必须是非地面变量,我们打印知识库中的内容:

say([the, capital, of, Subj, is, Obj]) :-
    (var(Subj) ; var(Obj)),
    capital(Subj, Obj), !,
    write([the, capital, of, Subj, is, Obj]).

请注意,因为程序使用了剪切 (!) 和 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.