0

举个例子。假设红色的所有东西的值都是 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(moose, 5)完全锁定并堆栈溢出,显然会导致无限循环,即使==>声明规则“仅调用其主体一次”。

我很感激我没有说事物只能有一种颜色或值(我不确定我将如何做到这一点),但重复添加moose具有red价值5的约束肯定不会改变约束集。

如何使系统能够在两个方向上正确执行此推理?

4

1 回答 1

0

也许是这样的:

:- use_module(library(chr)).
:- chr_type color ---> red ; blue.

:- chr_constraint thing_color_value(?any, ?color, ?int).

thing_color_value(T,red,V) <=> var(V)|thing_color_value(T,red,5).
thing_color_value(T,C,5) <=> var(C)|thing_color_value(T,red,5).
于 2019-11-29T22:40:41.747 回答