大家好,这是我第一次来这里:)!
我有以下Prolog定义集,唯一不起作用的是Subclass。任何人都可以帮助我弄清楚为什么这不是:(
% 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.
答案 0 :(得分:0)
如果您阅读了跟踪信息,则会看到最终问题:object
没有继承,或者没有inherits(object, Anything).
形式的事实。但这可能不是你的意思!
对我而言,似乎只是说:
subclass(A, B) :-
superclass(B, A).
在你的情况下,应该足够了。
答案 1 :(得分:0)
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.
当然,在此解决方案中,您的任务主要是简化为指定层次结构和接口。通过自己实施继承来获得学习奖励。