swi-prolog / packages-chr Goto Github PK
View Code? Open in Web Editor NEWCHR: Leuven Constraint Handling Rules
CHR: Leuven Constraint Handling Rules
CHR for SWI-Prolog ================== Authors and license ==================== This package contains code from the following authors. All code is distributed under the SWI-Prolog conditions with permission from the authors. * Tom Schrijvers, K.U.Leuven [email protected] * Christian Holzbaur [email protected] * Jan Wielemaker [email protected] Files and their roles: ====================== # library(chr) chr_swi.pl Make user-predicates and hooks for loading CHR files available to the user. # library(chr/chr_op) Include file containing the operator declaractions # library(chr/chr_translate) Core translation module. Defines chr_translate/2. # library(chr/chr_debug) Debugging routines, made available to the user through library(chr). Very incomplete. # library(chr/hprolog) Compatibility to hProlog. Should be abstracted. # library(chr/pairlist) Deal with lists of Name-Value. Used by chr_translate.pl Status ====== Work in progress. The compiler source (chr_translate.pl) contains various `todo' issues. The debugger is almost non existent. Future work should improve on the compatibility with the reference CHR documentation. Details on loading CHR files are subject to change.
I am using the 32-bit-Version of SWI-Prolog 8.2.0 on Windows 10 64-bit.
In the attached file, testchr.txt (rename to .pl before running), running testit
should lead to none of the CHR rules to be executed.
testit
adds the constraint c2(_Addr)
to the store. For c2_rule
to fire, c1(Dest)
must fail. The expected behaviour is for c1(Dest)
not to fail, as Dest
is unified with _Addr
from the testit
-call, which is a variable and not an integer. Thus, the expected behaviour is for c2_rule
not to fire.
However, when running testit
, we get the following output:
>swipl
Welcome to SWI-Prolog (threaded, 32 bits, version 8.2.0)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
1 ?- use_module(testchr).
true.
2 ?- testit.
c2_rule(_97180)
true.
3 ?-
The output c2_rule
indicates that the rule c2_rule
was indeed executed.
The example comes from a much larger CHR code-base, but has been distilled down to the minimal example. As soon as the rule c1_rule
is removed, the system behaves as expected.
I have also attached an execution trace in testchr_trace.txt.
I don't know whether these are expected or not.
In SWI Prolog 8.3.5, running the test suite and grepping for WARN in the file build/Testing/Temporary/LastTest.log
:
CHR test suite. To run all tests run ?- test.
Running scripts from Tests swipl-devel_original/packages/chr/Tests/dense_int.chr
. swipl-devel_original/packages/chr/Tests/fibonacci.chr
. swipl-devel_original/packages/chr/Tests/leq.chr
. swipl-devel_original/packages/chr/Tests/passive_check2.chr
. swipl-devel_original/packages/chr/Tests/passive_check.chr
. swipl-devel_original/packages/chr/Tests/primes.chr
================================================================================
CHR compiler WARNING:
`--> Heads will never match or guard will always fail in rule number 4 at swipl-devel_original/packages/chr/Tests/primes.chr:24.
This rule will never fire!
================================================================================
================================================================================
CHR compiler WARNING:
`--> All heads passive in rule number 4 at swipl-devel_original/packages/chr/Tests/primes.chr:24.
This rule never fires. Please check your program.
================================================================================
. swipl-devel_original/packages/chr/Tests/trigger_no_active_occurrence.chr
. swipl-devel_original/packages/chr/Tests/zebra.chr
. done
All tests passed
Hi,
For the following program
%% test.pl
:- use_module(library(chr)).
:- chr_constraint c/2.
c(K,_I), c(K,_J) <=> writeln('rule 1 fired').
c(_I,K), c(_J,K) <=> writeln('rule 2 fired').
?- consult(["test.pl"]).
true.
?- c(X,Y).
rule 2 fired
c($VAR(X),$VAR(Y)).
Although the 2nd rule has two heads and I only added one constraint (c(X,Y))
, the rule still fired.
This issue can be reproduced in Stable release - SWI-Prolog 8.4.1-1 for Microsoft Windows (64 bit), and Daily builds - swipl-w64-2022-01-31.exe (version 8.5.6-26-gee0b3fea1).
N.B. swish has no the same problem, but the constraint are not preserved, see https://swish.swi-prolog.org/p/unexpected_rule_fired.pl
Thanks.
The reproduce steps as follows.
main.pl
:- debug.
:- use_module(library(chr)).
:- chr_constraint c/1.
:- include('test.pl').
test.pl
test0 :- true.
test :- do_test.
c(X,Y,Z) <=> true. %% line number 10
?- consult(["main.pl"]).
================================================================================
CHR compiler ERROR: invalid syntax "c(_78356,_78358,_78360)".
`--> Undeclared constraint _78370/_78372 in head of rule number 1 at e:/work-pl/prolog/code/include/main.pl:10.
Constraint should be one of [c/1].
================================================================================
true.
Notice that the compiling error shows the wrong location main.pl:10
. It is should be in test.pl:10
.
As already discussed in pull request #2 it is a common request to have a persistent constraint store in SWI's top-level. My suggestion was to have a new chr_option(persistent_store, OffOn)
. Unfortunately it is not enough to simply prevent the top-level's backtracking by changing the used b_setval
into nb_setval
for saving constraints.
@JanWielemaker already proposed some thoughts on this in #2:
If we want an incremental toplevel for CHR it might be better to see whether the normal Prolog toplevel could be changed from backtracking to a recursive loop. That is surely possible. Also there I'm not sure it is the right direction. I tend to prefer the SWISH toplevel which gives you a completely fresh environment for each query.
I am willing in implementing an alternative approach again, but I am not sure if the requested behaviour is ok for Jan and which would be the best starting point for implementation. Let's use this issue to track this down :)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.