Constraint Handling Rules - Example Program

Example Program

The following SWI-Prolog program contains four CHR rules that implement a handler for the less-or-equal constraint:

:- use_module(library(chr)). :- op(500, xfx, leq). :- chr_constraint leq/2. % X leq Y means variable X is less-or-equal to variable Y reflexivity @ X leq X <=> true. antisymmetry @ X leq Y, Y leq X <=> X=Y. idempotence @ X leq Y \ X leq Y <=> true. transitivity @ X leq Y, Y leq Z ==> X leq Z.

The first rule, which is called reflexivity (rule names are optional), is a single-headed simplification rule. It removes constraints of the form A leq A from the constraint store. The second rule, antisymmetry, is a simplification rule with two heads. It replaces two symmetric leq constraints by an equality constraint (handled by Prolog unification). Simplification rules correspond to logical equivalence, as the syntax suggests. The third rule is a simpagation rule which removes redundant copies of the same constraint. Such rules are often needed because of the multi-set semantics of CHR. Finally, the last rule (transitivity) is a propagation rule that adds redundant constraints. Propagation rules correspond to logical implication.

Execution proceeds by exhaustively applying the rules to a given input query. For example, given the query A leq B, B leq C, C leq A the transitivity rule adds A leq C. Then, by applying the antisymmetry rule, A leq C and C leq A are removed and replaced by A=C. Now the antisymmetry rule becomes applicable on the first two constraints of the original query. Now all CHR constraints are eliminated so no further rules can be applied, and the answer A=C, A=B is returned.

Read more about this topic:  Constraint Handling Rules

Famous quotes containing the word program:

    Adjoining a refreshment stand ... is a small frame ice house ... with a whitewashed advertisement on its brown front stating, simply, “Ice. Glory to Jesus.” The proprietor of the establishment is a religious man who has seized the opportunity to broadcast his business and his faith at the same time.
    —For the State of New Jersey, U.S. public relief program (1935-1943)

    They had their fortunes to make, everything to gain and nothing to lose. They were schooled in and anxious for debates; forcible in argument; reckless and brilliant. For them it was but a short and natural step from swaying juries in courtroom battles over the ownership of land to swaying constituents in contests for office. For the lawyer, oratory was the escalator that could lift a political candidate to higher ground.
    —Federal Writers’ Project Of The Wor, U.S. public relief program (1935-1943)