Inheritance 如何在Prolog中编写子类语句?

Inheritance 如何在Prolog中编写子类语句?,inheritance,prolog,subclass,Inheritance,Prolog,Subclass,大家好这是我第一次来这里: 我有下面的Prolog定义集,唯一不起作用的是子类。有谁能帮我弄清楚为什么这不是: % definitions of classes in our system class(object). class(animal). class(cat). class(dog). class(dachshund). class(toy). class(ball). class(post). % defin

大家好这是我第一次来这里:

我有下面的Prolog定义集,唯一不起作用的是子类。有谁能帮我弄清楚为什么这不是:

    % definitions of classes in our system 
    class(object). 
    class(animal). 
    class(cat). 
    class(dog). 
    class(dachshund). 
    class(toy). 
class(ball). 
class(post). 

% definitions of interfaces 
interface(iwoof). 
interface(imeow). 

% definitions of class inheritance 
inherits(animal,object). 
inherits(cat,animal). 
inherits(dog,animal). 
inherits(dachshund, dog). 
inherits(toy,object). 
inherits(ball,object). 
inherits(post,object). 
inherits(ball,toy).
inherits(post,toy).

% definitions of interface implementation 
implements(cat,imeow). 
implements(dog,iwoof). 

% definitions of objects (instances of classes) 
instance(fluffy,cat). 
instance(fido,dog). 
instance(rex,dog). 
instance(schnitzel,dachshund). 
instance(superscratch,post). 
instance(bouncyball,ball). 
instance(tennisball,ball). 

% definitions of behavior 
playswith(cat,post). 
playswith(dog,ball). 

% definitions of superclasses 
superclass(C,D) :- inherits(C,D). 
superclass(D,C) :- inherits(D,X), superclass(X,C). 

% definitions of subclasses 
subclass(C,D) :- superclass(X,D), inherits(C,X).
我需要的输出是

?- subclass(toy,X).
X = ball ;
X = post ;
false.
但我明白了

这是痕迹

?- trace, subclass(toy,X).
   Call: (7) subclass(toy, _G246) ? creep
   Call: (8) inherits(toy, _G366) ? creep
   Exit: (8) inherits(toy, object) ? creep
   Call: (8) superclass(object, _G246) ? creep
   Call: (9) inherits(object, _G246) ? creep
   Fail: (9) inherits(object, _G246) ? creep
   Redo: (8) superclass(object, _G246) ? creep
   Call: (9) inherits(object, _G366) ? creep
   Fail: (9) inherits(object, _G366) ? creep
   Fail: (8) superclass(object, _G246) ? creep
   Fail: (7) subclass(toy, _G246) ? creep
false.

如果您阅读跟踪,您将看到最终的问题:没有对象继承的东西,或者,没有形式继承对象的事实,任何东西。。但这可能不是你的意思

对我来说,似乎只是说:

subclass(A, B) :-
    superclass(B, A).

在您的情况下应该足够了。

Logtalk中的一个替代实现,您可以在大多数Prolog系统中作为后端编译器运行:

:- object(metaclass,
    imports(class_hierarchy),   % from the standard library
    instantiates(metaclass)).   % make metaclass its own metaclass

:- end_object.


:- object(object,
    instantiates(metaclass)).

:- end_object.


:- object(animal,
    instantiates(metaclass),
    specializes(object)).

    :- public(playswith/1).

:- end_object.


:- protocol(imeow).

    :- public(imeow/0).

:- end_protocol.


:- protocol(iwoof).

    :- public(iwoof/0).

:- end_protocol.


:- object(cat,
    implements(imeow),
    instantiates(metaclass),
    specializes(animal)).

    imeow.
    playswith(post). 

:- end_object.


:- object(dog,
    implements(iwoof),
    instantiates(metaclass),
    specializes(animal)).

    iwoof.
    playswith(ball).

:- end_object.


:- object(dachshund,
    instantiates(metaclass),
    specializes(dog)).

:- end_object.


:- object(toy,
    instantiates(metaclass),
    specializes(object)).

:- end_object.


:- object(ball,
    instantiates(metaclass),
    specializes(toy)).

:- end_object.


:- object(post,
    instantiates(metaclass),
    specializes(toy)).

:- end_object.


:- object(fluffy,
    instantiates(cat)).

:- end_object.


:- object(fido,
    instantiates(dog)).

:- end_object.


:- object(rex,
    instantiates(dog)).

:- end_object.


:- object(schnitzel,
    instantiates(dachshund)).

:- end_object.


:- object(superscratch,
    instantiates(post)).

:- end_object.


:- object(bouncyball,
    instantiates(ball)).

:- end_object.


:- object(tennisball,
    instantiates(ball)).

:- end_object.
您的帖子中的示例查询:

?- toy::subclass(Subclass).
Subclass = ball ;
Subclass = post.

当然,在这个解决方案中,您的任务主要简化为指定层次结构和接口。通过自己实现继承可以获得学习上的回报。

以什么方式不起作用?你切换超类/2中参数的顺序有什么原因吗?或者这是一个错误?如果我要键入?-子类玩具,X。我应该得到这个:X=ball;X=员额;错误的然而,我只是在撒谎,这是我们被教导这样做的方式。我以前从未在Prolog中做过任何事情,所以我不是100%确定。你试过吗?-跟踪,子类玩具,X。看看哪里出了问题?失败:9 inheritsobject,\u G246?爬行是成功的一步。。。我觉得很糟糕,事情就这么简单。。非常感谢。如果你知道去哪里看的话,它总是很简单的。这就是我指出跟踪结果的原因。使用Windows 7 64位上的SWI Prolog,我可以使用logtalk_loadFile加载您的代码。但我得到了这样的信息:`logtalk_load'C:/Users/usilisateur/Documents/logtalk/trav/first.lgt'.*在编译对象元类%[c:/users/Usilisateur/documents/logtalk/trav/first.lgt]时,在第1-3行*之间对未知类别的引用:文件c:/users/Usilisateur/documents/logtalk/trav/first.lgt*中的类\层次结构]%1编译警告“?-toy::Subclass子类。错误:对象“toy”不存在抱歉,我无法销毁上面的注释,复制您的代码时出错。您还需要首先通过调用logtalk\u LoadLibraryHierarchys\u loader加载使用过的库实体。