Title: Constraint Handling Rules
1Constraint Handling Rules
- May, 2005
- Jairson Vitorino and Marcos Aurélio
- ORCAS
Orcas
2Contents
- CHR
- Introduction
- Syntax
- Operational Semantics
- Example
- CHR-?
- Introduction
- Operational Semantics
- Example
- References
3CHR
- Constraint Handling Rules
4Introduction
- Constraint Logic Programming
- Logic Programming Constraint Solving
- CHR Language for writing Constraint Solvers
- rule-based
- Flexible to define new domains
- Constraint resolution via rules (declarative)
5Introduction
- CHR
- Rewrite rules (pattern matching simplify rules)
- Plus propagation rules
- Similiar to production rules (forward chaining),
but with a well defined semantics - CHR disjunction is turing-complete, first-order
knowledge language and monotonic
Current CSP software packages have fixed domains
and constraints, answer CHR
6Syntax
- Constraints
- User-defined
- Ex noattack(Q1,Q2), connected(X,Y,3)
- Built-In
- Ex AgtB, AB, ground(A)
7Syntax typical rule
H1,...,Hn ltgtG1,...Gn B1,...,Bn
Multiple Heads
Guards
Body
Both constraint categories
(Only built-in)
(Only user-defined)
Commited choice When the engine picks a
rule there will not be any backtracking, so in
CHR order matters!
8Syntax
- Rules
- Simplification
- H1,...,Hn ltgt G1,...Gn B1,...,Bn
- Propagation
- H1,...,Hn gtG1,...Gn B1,...,Bn
- Simpagation
- H1,...,Hn / Hn1,...HnnltgtG1,...Gn B1,...,Bn
9Operational Semantics
State
User defined Constraints Store (UDCS)
Logical meaning ?y Goal ? UDCS ? BICS
Goal or query
Built-in Constraint Store (BICS)
CHR Program Or Base Rule
CHR ENGINE
Final State true or false
10Operational Semantics
- Transitions
- Solve and Introduce
- Propagate
- Simplify
- Commited-choice and Confluence
- The confluence property of a program guarantees
that any computation starting from an arbitrary
given initial state, i.e. any possible order of
rule applications, results in the same final
state.
11Operational Semantics
Built-in
12Operational Semantics
User-defined
13Operational Semantics
- Transitions (Simplify)
- U1,U2 ?B1 U3,U4
Rule in CHR program
14Operational Semantics
- Transitions (Propagate)
- U1,U2 ?B1 U3,U4
Rule in CHR program
15Operational Semantics
- Transitions (Simpagate)
- U1,U2 / U5?B1 U3,U4
Rule in CHR program
16Example
- CHR Program
- XltY ltgt XY true.
- XltY,YltX ltgt XY.
- XltY,YltZ gt XltZ.
17Example
18Example
19Example
20Example
(Logo CltA equivale a BltA)
true
21Example
Final State
22CHR-?
- Constraint Handling Rules with Disjunctions
23Introduction
- Allows disjunctions in the bodies of rules and in
goals - It can be used as a general-purpose logic
programming language
24Operational Semantics
- State
- Minimal set of subgoals
- Ex a ? (b ? (c ? d)) ? e
- Logical meaning ?y A
- Initial State
- Final State
- Successful
- Failed
25Operational Semantics
26Operational Semantics
- Solve
- To normalize the predefined means to produce a
new state G that is (according to the logical
theory T0) logically equivalent to the state G
27Operational Semantics
- Transitions (Simplify)
- b ? c ?f ? g
28Operational Semantics
29Example
- bird ? albatross ? penguin.
- penguin ? flies ? false.
30Example
31Example
Final State (no derivation step can be applied)
32Example K-coloring map
main() ltgt true color(X1),       Â
 color(X2),         color(X3),    Â
    color(X4),       Â
 connected(X1,X2),       Â
 connected(X1,X3),       Â
 connected(X3,X4),       Â
 connected(X2,X4).connected(X1,X2) ltgt
ground(X1), ground(X2), X1X2 false.color(X) Â
   gt true (X1 X2 X3).
33Example 4-Queens
main() ltgt true c(X1), c(X2), c(X3), c(X4), //
rainhas         na(X1,X2,1),
 na(X1,X3,2), na(X1,X4,3),       Â
 na(X2,X3,1),  na(X2,X4,2),       Â
 na(X3,X4,1), sol(X1,X2,X3,X4).na(X,Y,D) ltgt
ground(X), ground(Y) PYD, MYD, ne(X,Y),
ne(X,P),ne(X,M).c(X) ltgt true (X1 X2
X3 X4).sol(X1,X2,X3,X4) ltgt ground(X1),
ground(X2), ground(X3), ground(X4) Â Â Â Â Â Â
      write(X1), write(X2), write(X3),
writeLn(X4).
34Reference
- Theory and practice of Constraint Handling Rules
(http//www.pst.informatik.uni-muenchen.de/persone
n/fruehwir/jlp-chr1/jlp-chr1.html) - A languagem for experimenting with Declarative
Paradigms (http//citeseer.ist.psu.edu/611754.html
)
35Reference
- XSB Prolog
- http//xsb.sf.net
- The ECLiPSe Constraint Logic Programming System
- http//www.icparc.ic.ac.uk/eclipse/
- WebCHR
- http//bach.informatik.uni-ulm.de/webchr/
- JACK Java Constraint Kit
- http//www.pms.ifi.lmu.de/software/jack/