You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Tau Prolog运行CHR代码报错,SWI-Prolog可正常运行

Troubleshooting Tau Prolog CHR Error: existence_error(procedure, '/'(color, 1))

I see your CHR code runs without issues in SWI-Prolog but throws an existence error for color/1 when using Tau Prolog. Let's break down the root cause and fix this problem:

Root Cause

The error stems from two key differences between Tau Prolog and SWI-Prolog's CHR implementations:

  1. Strict Predicate Order: Tau Prolog requires helper predicates referenced in CHR guard clauses (like mix(X,Y,Z) in your first rule) to be defined before the CHR rules themselves. In your original code, mix/3 comes after the CHR rules, so Tau Prolog can't resolve it during rule compilation—this breaks the registration of the color/1 constraint.
  2. Early Binding Behavior: SWI-Prolog uses late binding for predicates, allowing you to define them after rules. Tau Prolog relies on upfront definition for guard predicates to properly set up CHR constraints.

Fixed Code

Here's the adjusted code that works correctly in Tau Prolog:

:- use_module(library(chr)).

% Define mix/3 BEFORE CHR rules so the guard clause can resolve it
mix(red,blue,purple).
mix(blue,yellow,green).
mix(yellow,red,orange).

% Declare CHR constraint
:- chr_constraint(color/1).

% CHR rules (unchanged logic, now properly supported)
color(X), color(Y) <=> mix(X,Y,Z) | color(Z).
color(brown) \ color(_) <=> true.

Explanation of Changes

  • Reordered mix/3 definitions: Moving these helper predicates to the top ensures Tau Prolog can find and evaluate mix(X,Y,Z) when compiling the CHR rules, which allows the color/1 constraint to be registered correctly.
  • Core CHR logic preserved: The constraint declaration and rule behavior stay identical to your original code—only the predicate order was adjusted to fit Tau Prolog's stricter compilation requirements.

When you run your query ?- color(yellow), color(red). with this fixed code, Tau Prolog will correctly derive color(orange) as expected.

内容的提问来源于stack exchange,提问作者MahmoudLamei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 21:08:13