结果与给定事实不符

时间:2015-12-23 04:11:30

标签: prolog transitive-closure

使用prolog制作代码以获得一些比较输出,但某些输出无法正常工作。似乎那些与事实不相符。这里是代码

fact(Fact) :- Fact,!.
fact(Fact):-Fact = ..[Rel, Arg1, Arg2],isa(Arg1, SuperArg).
SuperFact =..[Rel,SuperArg,Arg2].
covers(animal,skin).
isa(fish,animal).
isa(bird,animal).
isa(mammal,animal).
isa(shark,fish).
isa(salmon,fish).
isa(parrot,bird).
isa(penguin,bird).
speacial_organ(fish,gills).
travel(fish,swim).
birth(bird,lay_eggs).
special_organ(bird,wings).
travel(bird,fly).
birth(fish,lay_eggs).
birth(mammal,not_lay_eggs).
birth(shark,not_lay_eggs).
nature(shark,dangerous).
food(salmon,delicacy).
colour(parrot,green).
travel(penguin,walk).

这是我想从这个程序中找到答案的问题

•鹦鹉可以飞吗? •鹦鹉的颜色是什么? •鹦鹉有皮肤吗?
•鲨鱼危险吗?

1 个答案:

答案 0 :(得分:3)

第一步:定义isa/2的反身(以下名称为is_a/2)。

isa(fish,animal).
isa(bird,animal).
isa(mammal,animal).
isa(shark,fish).
isa(salmon,fish).
isa(parrot,bird).
isa(penguin,bird).

is_a(X, Y) :-
   closure0(isa, X, Y).

第二步:指定剩余事实(按谓词指标分组)。

covers(animal,skin).

special_organ(fish,gills).
special_organ(bird,wings).

travel(fish,swim).
travel(bird,fly).
travel(penguin,walk).

birth(bird,lay_eggs).
birth(fish,lay_eggs).
birth(mammal,not_lay_eggs).
birth(shark,not_lay_eggs).

nature(shark,dangerous).

food(salmon,delicacy).

colour(parrot,green).

第三步:让我们提出一些问题!

  •   

    鹦鹉可以飞吗?

    ?- is_a(parrot, X), travel(X, fly).
       X = bird
    ;  false.
    
  •   

    鹦鹉的颜色是什么?

    ?- is_a(parrot, X), colour(X, Colour).
       Colour = green, X = parrot         
    ;  false.
    
  •   

    鹦鹉有皮肤吗?

    ?- is_a(parrot, X), covers(X, skin).
       X = animal
    ;  false.
    
  •   

    鲨鱼是危险的吗?

    ?- is_a(shark, X), nature(X, dangerous).
       X = shark
    ;  false.