0

我想在 Prolog 中创建与以下语句等效的知识库。

  1. 约翰喜欢各种食物。

  2. 苹果和蔬菜是食物

  3. 任何人吃的东西,没有被杀死的东西都是食物。

  4. 阿尼尔吃花生还活着

  5. 哈利吃掉了阿尼尔吃的所有东西。

所以这是从这里借来的版本之一

在此处输入图像描述

创建此文件后,我想找到查询“约翰喜欢花生”的答案。说likes(john, peanut).

从我这边尝试在 Prolog 中编写规则如下

alive(anil).
eats(anil,peanut).
food(apple).
food(vegitables).

food(X):-likes(john,X).

eats(X,Y),not(killed(X)):-food(Y).

eats(anil,X):-eats(harry,X).

killed(X):-not(alive(X)).

alive(X):-not(killed(X)).

但我收到如下错误和警告;

ERROR:    No permission to modify static procedure `(',')/2'

Warning:    Clauses of eats/2 are not together in the source-file

Warning:    Current predicate: food/1

Warning:    Use ':- discontiguous eats/2.' to suppress this message

Warning:    Clauses of alive/1 are not together in the source-file

Warning:    Current predicate: killed/1

Warning:    Use ':- discontiguous alive/1.' to suppress this message
4

1 回答 1

1

“约翰喜欢各种食物。”

likes(john,X) :- food(X).

“苹果和蔬菜是食物。”

food(apple).
food(X) :- vegetable(X).

“任何人吃掉但没有被杀死的东西都是食物。”

food(X) :- eats(P,X), alive(P).

“阿尼尔吃花生还活着。”

eats(anil,peanuts).
alive(anil).

“哈利吃阿尼尔吃的所有东西。”

eats(harry,X) :- eats(anil,X).

?- likes(john,X).

产生(在 SWI Prolog 中)

70 ?- likes(john,X).
X = apple ;
ERROR: food/1: Undefined procedure: vegetable/1
   Exception: (9) vegetable(_G19387424) ? fail
   Exception: (8) food(_G19387424) ? fail
71 ?- 

修复它

:- dynamic(vegetable/1).

我们得到

75 ?- likes(john,X).
X = apple ;
X = peanuts ;
false.

所以,当然,

101 ?- likes(john,peanuts).
true .
于 2021-03-03T15:06:33.793 回答