Mat*_*att 1 prolog infinite-loop
我想在Prolog中做这样的事情:
some_commutative_property(X,Y) :- some_commutative_property(Y,X).
some_commutative_property(1,2).
some_commutative_property(3,4).
Run Code Online (Sandbox Code Playgroud)
这样查询两者some_commutative_property(1,2).并some_commutative_property(2,1).返回true.
问题显然是第一行创建了一个无限循环.我怎么能绕过这个?
可能有多种解决方案:
将谓词拆分为两个:
some_commutative_property(X,Y) :- some_commutative_property_aux(X,Y).
some_commutative_property(X,Y) :- some_commutative_property_aux(Y,X).
用some_commutative_property_aux(1,2).和some_commutative_property_aux(3,4).
执行订单:
some_commutative_property(X,Y) :- X>Y, some_commutative_property(Y,X).
with some_commutative_property_aux(1,2).和some_commutative_property_aux(3,4).只有在假设基本事实只是形式的情况下才能some_commutative_property(A,B)使用A =< B.
:- table some_commutative_property/2到原始代码也可以解决问题.