Title: Updating Agents
1Constraint Handling Rules
- Pierangelo DellAcqua
- Dept. of Science and Technology
- Linköping University
- pier_at_itn.liu.se
Constraint programming 2001 November 13th
2001 http//www.ida.liu.se/labs/logpro/ulfni/cp200
1/
2Overview
- Motivation
- Language CHR
- Declarative and operational semantics
- Properties
- Examples of CHR constraint solvers
3Based on
- Theory and Practice of Constraint Handling Rules
- Thom Frühwirth, J. Logic Programming 199419,
201-679 - Examples CHR constraint solvers available at
- www.informatik.uni-muenchen.de/fruehwir/chr/
4Black-box vs Glass-box solvers
- In most systems constraint solving is hard-wired
in a built-in constraint solver in a low-level
language black-box approach - efficiency
- non-extensible, unpredictable, hard to debug
- Some systems facilitate defining new constraints
and solvers glass-box approach - improved control of propagation and search
- examples CHR, HAL, ...
5Constraint Handling Rules (CHR)
- Declarative programming language for the
specification and implementation of constraint
solvers and programs
Application
- CHR-constraint solvers are open and flexible, can
be maintained, debugged and analysed
CHR-constraints
CHR-solver
built-in constraints
Black-box
Host language (Prolog, Lisp, )
6CHR by example
- The partial order relation X ? Y as a
user-defined constraint
XltY ltgt XY true. reflexivity XltY ? YltX
ltgt XY. antisymmetry XltY ? YltZ gt
XltZ. transitivity
Computation AltB ? CltA ? BltC CltA ? AltB
propagates CltB by transitivity CltB ? BltC
simplifies to BC by antisymmetry AltB ? CltA
simplifies to AB by antisymmetry since BC AB ?
BC
7CHR syntax
Head H conjunction of CHR-constraints Guard G conj
unction of built-in constraints Body B conjunction
of built-in and CHR-constraints
A CHR-program is a finite set of CHR-rules. There
are three kinds of CHR-rules
Simplification H ltgt G B Propagation H gt G
B Simpagation H1 \ H2 ltgt G B
8Declarative semantics
Simplification rule H ltgt G B ?h (?g (G) ? ( H
? ?b ( B ) ) ) Propagation rule H gt G B ?h
(?g (G) ? ( H ? ?b ( B ) ) ) Simpagation rule H1
\ H2 ltgt G B ?h1?h2 (?g (G) ? (H1?H2 ? ?b
(H1?B)))
9Declarative semantics (2)
Declarative semantics of a CHR-program P
Sem(P) LP, CT where LP is
the logical reading of the CHR-rules in P and CT
is a theory for built-in constraints
10Operational semantics
A state is a tuple ?F,E,D? where F is a
conjunction of CHR- and built-in constraints
(goal store) E is a conjunction of
CHR-constraints (CHR-store) D is a conjunction of
built-in constraints (built-in constraints store)
11CHR transitions
Solve ?C?F, E, D? ? ?F, E, D2? if C is a
built-in constraint and CT (C?D) ? D2
Introduce ?H?F, E, D? ? ?F, H?E, D? if H is a
CHR-constraint
Simplify ?F, H2?E, D? ? ?B?F, E, HH2?D? if (H
ltgt G B) in P and CT D ? ?h (HH2 ? G)
12CHR transitions (2)
Propagate ?F, H2?E, D? ? ?B?F, H2?E,
HH2?D? if (H gt G B) in P and CT D ? ?h
(HH2 ? G)
13Initial and final states
An initial state consists of a goal G and empty
constraint stores
?G,true,true?
A final state is either of the form
(i) ?F,E,false? failed final state or of the
form (ii) ?true,E,D? successful final state
where no transition is applicable and D
? false
14CHR computations
A computation of a goal G is a sequence S0, S1,
of states with Si ? Si1 beginning with the
initial state S0 ?G,true,true? and ending with
a final state or diverging
An answer of a goal G is the final state of a
computation for G
The logical meaning of a state ?F,E,D?, which
occurs in a computation for G, is ?x (F?E?D),
where x are the variables in ?F,E,D? but not in G
15Example CHR calculus
- XltY ltgt XY true. reflexivity
- XltY ? YltX ltgt XY. antisymmetry
- XltY ? YltZ gt XltZ. transitivity
?AltB ? CltA ? BltC, true, true? Introduce
?3 ?true, AltB ? CltA ? BltC, true? Propagate
? ?CltB, AltB ? CltA ? BltC, true? Introduce
? ?true, AltB ? CltA ? BltC ? CltB,
true? Simplify ? ?BC, AltB ? CltA, true? Solve
? ?true, AltB ? CltA, BC ? Simplify ? ?AB,
true, BC ? Solve ? ?true, true, AB ? BC ?
16Logical equivalence of states
CHR transitions preserve the logical meaning of
states
Lemma Let P be a CHR program and G a goal. If C
is the logical meaning of a state in a
computation of G, then LP , CT ? ( G ? C)
There is no distinction between successful and
failed computations
17Correspondence between semantics
Theorem (Soundness) Let P be a CHR program and G
a goal. If G has a computation with answer C,
then LP, CT ? ( C ? G)
Theorem (Completeness) Let P be a CHR program
and G a goal with at least one finite
computation. Let C be a conjunction of
constraints. If LP, CT ?(G?C), then G has a
computation with answer C2 such that LP, CT
? ( C ? C2)
18Example completeness
The completeness theorem does not hold if G has
no finite computations
Let P be p ltgt p and G the goal p. Since LP
is p?p, it holds that LP,CT p?p, but G has
only an infinite computation
19Example failed computations
The completeness theorem is weak for failed
computations
p ltgt q, p ltgt false
Let P be
We have that LP, CT ?q, but q has no failed
computation. It has a successful derivation with
answer q.
20Confluence
Confluence The answer of a goal G is always the
same, no matter which of the applicable rules are
applied
p ltgt q, p ltgt false, q ltgt false is
confluent
p ltgt q, p ltgt false is not confluent
21Soundness and Completeness revisited
- Theorem (Strong Soundness and Completeness)
- Let P be a terminating and confluent CHR program,
G a goal and C a conjunction of constraints. - Then the following are equivalent
- LP, CT ?(C?G)
- G has a computation with answer C2 such that
- LP, CT ?(C?C2)
- Every computation of G has an answer C2 such
that - LP, CT ?(C?C2)
22CLP CHR
Any CLP language can be extended with CHR
Idea
- Allow clauses for CHR constraints introduce
choices - Regard a predicate as a constraint and
add CHR rules for it
Dont know and dont care nondeterminism combined
in a declarative way
23CLPCHR language
A CLPCHR program is a finite set of (i) CLP
clauses for predicates and CHR constraints,
and (ii) CHR rules for CHR constraints.
A CLP clause is of the form
H - B1,,Bk (k ? 0)
conjunction of atoms, CHR constraints and
built-in constraints
an atom or a CHR constraint not a built-in
constraint
24CLPCHR language (2)
The logical meaning of a CLP clause is given by
Clarks completion
Backward compatibility
Labelling declarations (see def. 6.1 of JLP
paper) are dropped, easily simulated
H - B label-with H if G
lw, H ltgt G H2, lw H2 - B
new CHR constraint
CHR constraint
new predicate
25CLPCHR transitions
Unfold (revisited) ?H2?F, E, D? ? ?B?F, E,
HH2?D? if (H-B) in P, H2 is a predicate and CT
D??h (HH2)
Label (revisited) ?F, H2?E, D? ? ?B?F, E,
HH2?D? if (H-B) in P, H2 is a CHR constraint
and CT D??h (HH2)
26Examples of CHR solvers
Several constraint solvers have been written in
CHR, including new constraint domains such as
terminological and temporal reasoning
bool.pl boolean constraints arc.pl arc-consistency
over finite domains interval.pl interval domains
over integers and reals list.pl equality
constraints over concatenation of lists
27Sicstus Prolog CHR
A CHR rule in SicstusPrologCHR is of the form
H ltgt G B H gt G B H1 \ H2
ltgt G B
where
H is a conjunction of CHR-constraints G is a
conjunction of atoms and built-in constraints B
is a conjunction of atoms, built-in and
CHR-constraints
A CHR rule can be fired if its guard G is
true Note that during the proof of the guard G
no new binding can be generated for variables
that occur also in H