Previous Up Next

9.5  More on the CHR Language

The following subsections describe declarations, clauses, options and built-in predicates of the CHR language.

9.5.1  Declarations

Declarations name the constraint handler, its constraints, specify their syntax and use in built-in labeling.

Declaration ::= handler Name.
  ::= constraints SpecList.
  ::= operator(Precedence,Associativity,Name).
  ::= label_with Constraint if Guard.

The optional handler declaration documents the name of the constraint handler. Currently it can be omitted, but will be useful in future releases for combining handlers.

The mandatory constraints declaration lists the constraints defined in the handler. A “SpecList” is a list of Name/Arity pairs for the constraints. The declaration of a constraint must appear before the constraint handling rules and ECLiPSe clauses which define it, otherwise a syntax error is raised. There can be several constraints declarations.

The optional operator declaration declares an operator, with the same arguments as op/3 in ECLiPSe. However, while the usual operator declarations are ignored during compilation from chr to pl files, the CHR operator declarations are taken into account (see also the subsection on clauses).

The optional label_with declaration specifies when the ECLiPSe clauses of a constraint can be used for built-in labeling (see subsection on labeling).

Example, contd.: The first lines of the minmax handler are declarations:

handler minmax.

constraints leq/2, neq/2, minimum/3, maximum/3.

operator(700, xfx, leq).
operator(700, xfx, neq).

9.5.2  ECLiPSe Clauses

A constraint handler program may also include arbitrary ECLiPSe code (written with the four operators :- /[1,2] and ?- /[1,2]).

Clause ::= Head :- Body.
  ::= Head ?- Body.
  ::= :- Body.
  ::= ?- Body.

Note that :-/1 and ?-/1 behave different from each other in CHR programs. Clauses starting with :- are copied into the pl file by the CHR compiler, clauses with ?- are executed by the compiler. As the op declaration needs both copying and execution, we have introduced the special operator declaration (see previous subsection on declarations). A "Head" can be a "Constraint", such clauses are used for built-in labeling only (see section on labeling).

9.5.3  Options

The option command allows the user to set options in the CHR compiler.

Option ::= option(Option, On_or_off).

Options can be switched on or off. Default is on. Advanced users may switch an option off to improve the efficiency of the handler at the cost of safety. Options are:

9.5.4  CHR Built-In Predicates

There are some built-in predicates to compile chr files, for debugging, built-in labeling and to inspect the constraint store and remove its constraints:


Previous Up Next