如何在prolog中执行此操作而不会导致无限循环?

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.

问题显然是第一行创建了一个无限循环.我怎么能绕过这个?

Ale*_*nik 5

可能有多种解决方案:

  • 将谓词拆分为两个:

    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.