Home Contents Index Summary Previous Next

7.3 CHR in SWI-Prolog Programs

7.3.1 Embedding in Prolog Programs

The CHR constraints defined in a particulary .chr file are associated with a module. The default module is user. One should never load different .chr files with the same CHR module name.

7.3.2 Constraint declaration

Every constraint used in CHR rules has to be declared. There are two ways to do this. The old style is as follows:


option(type_definition,type(list(T),[ [] , [T|list(T)] ]).
option(mode,foo(+,?)).
option(type_declaration,foo(list(int),float)).
:- constraints foo/2, bar/0.

The new style is as follows:


:- chr_type list(T) ---> [] ; [T|list(T)].
:- constraints foo(+list(int),?float), bar.

7.3.3 Compilation

The SWI-Prolog CHR compiler exploits term_expansion/2 rules to translate the constraint handling rules to plain Prolog. These rules are loaded from the library library(chr). They are activated if the compiled file has the .chr extension or after finding a declaration of the format below.


:- constraints ...

It is adviced to define CHR rules in a module file, where the module declaration is immediately followed by including the library(chr) library as examplified below:


:- module(zebra, [ zebra/0 ]).
:- use_module(library(chr)).

:- constraints ...

Using this style CHR rules can be defined in ordinary Prolog .pl files and the operator definitions required by CHR do not leak into modules where they might cause conflicts.