<s>
Constraint	B-Application
Handling	I-Application
Rules	I-Application
(	O
CHR	O
)	O
is	O
a	O
declarative	B-Language
,	O
rule-based	B-Language
programming	I-Language
language	O
,	O
introduced	O
in	O
1991	O
by	O
Thom	O
Frühwirth	O
at	O
the	O
time	O
with	O
European	O
Computer-Industry	O
Research	O
Centre	O
(	O
ECRC	O
)	O
in	O
Munich	O
,	O
Germany	O
.	O
</s>
<s>
Originally	O
intended	O
for	O
constraint	B-Application
programming	I-Application
,	O
CHR	O
finds	O
applications	O
in	O
grammar	B-Algorithm
induction	I-Algorithm
,	O
type	O
systems	O
,	O
abductive	O
reasoning	O
,	O
multi-agent	O
systems	O
,	O
natural	B-Language
language	I-Language
processing	I-Language
,	O
compilation	B-Language
,	O
scheduling	B-Application
,	O
spatial-temporal	O
reasoning	O
,	O
testing	O
,	O
and	O
verification	O
.	O
</s>
<s>
Although	O
CHR	O
is	O
Turing	B-Algorithm
complete	I-Algorithm
,	O
it	O
is	O
not	O
commonly	O
used	O
as	O
a	O
programming	O
language	O
in	O
its	O
own	O
right	O
.	O
</s>
<s>
Prolog	B-Language
is	O
by	O
far	O
the	O
most	O
popular	O
host	O
language	O
and	O
CHR	O
is	O
included	O
in	O
several	O
Prolog	B-Language
implementations	O
,	O
including	O
SICStus	B-Language
and	O
SWI-Prolog	B-Application
,	O
although	O
CHR	O
implementations	O
also	O
exist	O
for	O
Haskell	B-Language
,	O
Java	B-Language
,	O
C	B-Language
,	O
SQL	B-Language
,	O
and	O
JavaScript	O
.	O
</s>
<s>
In	O
contrast	O
to	O
Prolog	B-Language
,	O
CHR	O
rules	O
are	O
multi-headed	O
and	O
are	O
executed	O
in	O
a	O
committed-choice	O
manner	O
using	O
a	O
forward	O
chaining	O
algorithm	O
.	O
</s>
<s>
Traditionally	O
,	O
Prolog	B-Language
is	O
used	O
as	O
the	O
host	O
language	O
,	O
so	O
its	O
data	O
structures	O
and	O
variables	O
are	O
used	O
.	O
</s>
<s>
The	O
built-in	O
constraint	O
theory	O
must	O
include	O
at	O
least	O
true	O
(	O
the	O
constraint	O
that	O
always	O
holds	O
)	O
,	O
fail	O
(	O
the	O
constraint	O
that	O
never	O
holds	O
,	O
and	O
is	O
used	O
to	O
signal	O
failure	O
)	O
and	O
equality	O
of	O
terms	O
,	O
i.e.	O
,	O
unification	B-Algorithm
.	O
</s>
<s>
The	O
program	O
then	O
proceeds	O
by	O
matching	B-Language
rules	I-Language
against	O
the	O
store	O
and	O
applying	O
them	O
,	O
until	O
either	O
no	O
more	O
rules	O
match	O
(	O
success	O
)	O
or	O
the	O
fail	O
constraint	O
is	O
derived	O
.	O
</s>
<s>
Matching	O
is	O
defined	O
as	O
"	O
one-way	O
unification	B-Algorithm
"	O
:	O
it	O
binds	O
variables	O
only	O
on	O
one	O
side	O
of	O
the	O
equation	O
.	O
</s>
<s>
Pattern	B-Language
matching	I-Language
can	O
be	O
easily	O
implemented	O
when	O
as	O
unification	B-Algorithm
when	O
the	O
host	O
language	O
supports	O
it	O
.	O
</s>
<s>
The	O
following	O
CHR	O
program	O
,	O
in	O
Prolog	B-Language
syntax	O
,	O
contains	O
four	O
rules	O
that	O
implement	O
a	O
solver	O
for	O
a	O
less-or-equal	O
constraint	O
.	O
</s>
<s>
In	O
the	O
declarative	B-Language
reading	O
,	O
three	O
of	O
the	O
rules	O
specify	O
the	O
axioms	O
of	O
a	O
partial	O
ordering	O
:	O
</s>
<s>
All	O
three	O
rules	O
are	O
implicitly	O
universally	O
quantified	O
(	O
upper-cased	O
identifiers	O
are	O
variables	O
in	O
Prolog	B-Language
syntax	O
)	O
.	O
</s>
<s>
Such	O
an	O
equality	O
constraint	O
is	O
considered	O
built	O
in	O
,	O
and	O
implemented	O
as	O
a	O
unification	B-Algorithm
that	O
is	O
typically	O
handled	O
by	O
the	O
underlying	O
Prolog	B-Language
system	O
.	O
</s>
<s>
The	O
transitivity	O
rule	O
adds	O
A	O
leq	O
C	B-Language
.	O
Then	O
,	O
by	O
applying	O
the	O
antisymmetry	O
rule	O
,	O
A	O
leq	O
C	B-Language
and	O
C	B-Language
leq	O
A	O
are	O
removed	O
and	O
replaced	O
by	O
A	O
=	O
C	B-Language
.	O
Now	O
the	O
antisymmetry	O
rule	O
becomes	O
applicable	O
on	O
the	O
first	O
two	O
constraints	O
of	O
the	O
original	O
query	O
.	O
</s>
<s>
Now	O
all	O
CHR	O
constraints	O
are	O
eliminated	O
,	O
so	O
no	O
further	O
rules	O
can	O
be	O
applied	O
,	O
and	O
the	O
answer	O
A	O
=	O
B	O
,	O
A	O
=	O
C	B-Language
is	O
returned	O
:	O
CHR	O
has	O
correctly	O
inferred	O
that	O
all	O
three	O
variables	O
must	O
refer	O
to	O
the	O
same	O
object	O
.	O
</s>
<s>
To	O
decide	O
which	O
rule	O
should	O
"	O
fire	O
"	O
on	O
a	O
given	O
constraint	O
store	O
,	O
a	O
CHR	O
implementation	O
must	O
use	O
some	O
pattern	B-Language
matching	I-Language
algorithm	O
.	O
</s>
<s>
Candidate	O
algorithms	O
include	O
RETE	B-Algorithm
and	O
TREAT	O
,	O
but	O
most	O
implementation	O
use	O
a	O
lazy	O
algorithm	O
called	O
LEAPS	O
.	O
</s>
