举个例子。假设所有红色的东西都有值5。我写了以下内容:
:- use_module(library(chr)).
:- use_module(library(clpfd)).
:- chr_type color ---> red ; blue.
:- chr_constraint hasColor(?any, ?color).
:- chr_constraint hasValue(?any, ?int).
hasColor(Thing, red) <=> hasValue(Thing, X), X #= 5.
hasColor(Thing, blue) <=> hasValue(Thing, X), X #\= 5.
hasColor(moose, red)
正确地生成推断输出
hasColor(moose, red), hasValue(moose, 5)
.
然而,我希望这个推论也能起到相反的作用;指定如果某个值为5,则它必须为红色。目前,
hasValue(moose, 5)
被接受为约束,但不产生推论。但添加规则:
hasValue(Thing, 5) ==> hasColor(Thing, red).
导致任何调用
hasValue(驼鹿,5岁)
完全锁定和堆栈溢出,显然会导致无限循环,即使
==>
声明该规则“只调用其主体一次”。
我很感激我没有说过一件东西只能有一种颜色或一个值(我不确定我会怎么做),而是反复添加了
moose
是
red
而且有价值
5
当然不应该更改约束集。
如何使系统能够在两个方向上正确执行此推断?