The CHR constraints defined in a .pl
file are associated
with a module. The default module is user
. One should never
load different .pl
files with the same CHR module name.
Every constraint used in CHR rules has to be declared with a . declaration by the constraint specifier. For convenience multiple consttraints may be declared at once with the same eclaration followed by a comma-separated list of constraint specifiers.
A constraint specifier is, in its compact form, F/A where F and A are respectively the functor name and arity of the constraint, e.g.:
:- chr_constraint foo/1. :- chr_constraint bar/2, baz/3. |
In its extended form, a constraint specifier is c(A_1, ... ,A_n) where c is the constraint's functor, n its arity and the A_i are argument specifiers. An argument specifier is a mode, optionally followed by a type. E.g.
:- chr_constraint get_value(+,?). :- chr_constraint domain(?int,+list(int)), alldifferent(?list(int)). |
A mode is one of:
-
+
?
A type can be a user-defined type or one of the built-in types. A type comprises a (possibly inifinite) set of values. The type declaration for a constraint argument means that for every instance of that constraint the corresponding argument is only ever bound to values in that set. It does not state that the argument necessarily has to be bound to a value.
The built-in types are:
A user-defined type is defined with a declaration. For example, types for lists, trees and colors are defined as:
:- chr_type list(T) ---> [] ; [T|list(T)]. :- chr_type tree(T) ---> nil ; tree(T,tree(T),tree(T)). :- chr_type color ---> red ; blue ; green. |
library(chr)
.
They are activated if the compiled file has the .chr
extension or after finding a declaration of the format below.
:- chr_constraint ... |
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 exemplified below:
:- module(zebra, [ zebra/0 ]). :- use_module(library(chr)). :- chr_constraint ... |
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.