使用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:
- 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/3comes after the CHR rules, so Tau Prolog can't resolve it during rule compilation—this breaks the registration of thecolor/1constraint. - 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/3definitions: Moving these helper predicates to the top ensures Tau Prolog can find and evaluatemix(X,Y,Z)when compiling the CHR rules, which allows thecolor/1constraint 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
相关产品推荐
相关产品推荐

