不完全确定这是否是您所需要的,但您可以通过创建事实的图形结构轻松表达一个类具有一组属性(请参阅下面的属性事实列表和在集合中查找属性的规则)。
然后,要表达该组属性的组合,您需要另一组组合事实和规则来发现类的任何子属性,从而发现它可以组成的事物。
我在下面也给出了一个代码示例来帮助解释。
property(bird, red_robotic_bird).
property(red, red_robotic_bird).
property(robot, red_robotic_bird).
property(tasty, cake).
property(red_robotic_bird, macaw).
property(Property, Thing) :-
property(PropertySet, Thing),
property(Property, PropertySet).
composition(buttons, red_robotic_bird).
composition(cheese, red_robotic_bird).
composition(wire, red_robotic_bird).
composition(flour, cake).
composition(Material, Thing) :-
property(Property, Thing),
composition(Material, Property).
示例查询
?- composition(Material, macaw).
Material = buttons ;
Material = cheese ;
Material = wire ;
no
?- composition(buttons, Thing).
Thing = red_robotic_bird ;
Thing = macaw ;
no
?- composition(flour, macaw).
no
?- property(bird, macaw).
yes
?- property(bird, cake).
no
property(Property, macaw).
Property = red_robotic_bird ;
Property = bird ;
Property = red ;
Property = robot ;
no
Prolog 规则简述。
规则本质上只是animal(cat).
以其他规则或事实为真为条件的事实(例如)。规则由头部和主体 ( head :- body.
) 组成。主体是最常以合取范式 (A /\ B /\ C) 表示的逻辑证明。prolog 中的and,
运算符 is ,or运算符 is ;
(但在规则中不鼓励使用它),句点 ( .
) 表示规则或事实的结束。
请注意,如果正文中的后续规则或事实失败,则 prolog 将回溯并要求先前规则或事实的替代答案,然后重试。考虑下面这个有点做作的例子。
share_same_colour(FruitA, FruitB) :- 颜色(Color, FruitA), colour(Color, FruitB)。
如果我们执行查询share_same_colour(apple, strawberry).
,则colour(Colour, apple).
可能将 Color 返回为绿色。但是,没有绿色草莓,所以 prolog 会回溯并询问苹果还有哪些其他颜色。下一个答案可能是红色,在此基础上第二个颜色语句将成功并且整个规则为真。