在CHR和CLP / FD中创建双向规则

问题描述 投票:0回答:1

例如。假设所有红色的值为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)的任何调用完全锁定和堆栈溢出,显然会导致无限循环,即使==>声明规则“仅精确地调用其主体一次”。]]

[我很高兴我没有说过事物只能具有一种颜色或值(我不确定该怎么做),而是反复添加moosered且值为5的约束。当然不应更改约束集。

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

例如。假设所有红色的值为5。我编写以下代码::-use_module(library(chr))。 :-use_module(library(clpfd))。 :-chr_type color --->红色;蓝色。 :-...

prolog clpfd constraint-handling-rules
1个回答
0
投票

也许像:

© www.soinside.com 2019 - 2024. All rights reserved.