- 46 Views
- Uploaded on
- Presentation posted in: General

Constraint Handling Rules (CHR): Rule-Based Constraint Solving and Deduction

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Constraint Handling Rules (CHR):Rule-Based Constraint Solving and Deduction

Jacques Robin

Constraint Handling Rules (CHR)

Key ideas

Introductory example

CHR constraint solver over real variables

CHR with disjunction (CHRïƒš)

CHR constraint solver over finite domain variables

General purpose rule-based reasoning with CHRïƒš

A taxonomy of rule-based languages

Production rules and ECA rules in CHRïƒš

Conditional term rewrite rules in CHRïƒš

Prolog and CLP rules in CHRïƒš

Deduction with CHRïƒš

Propositional deduction as Boolean constraint solving in CHRïƒš

First-order Horn Logic forward chaining with CHRïƒš

First-order Horn Logic backward chaining with CHRïƒš

First-order logic refutation and resolution based entailment with CHRïƒš

Description logic reasoning with CHRïƒš

- Originally a logical rule-based language to declaratively program specialized constraint solvers on top of a host programming language (Prolog, Haskell, Java)
- Since evolved in a general purpose first-order knowledge representation language and Turing-complete programming language
- Fact base contains both extensional and intentional knowledge in the form of a conjunction of constraints
- Rule base integrates and generalizes:
- Event-Condition-Action rules (themselves generalizing production rules) for constraint propagation
- Conditional rewrite rules for constraint simplification

- Relies on forward chaining and rule Left-Hand-Side (LHS) matching
- Extended variant CHRV adds backtracking search and thus generalizes Prolog rules as well

reflexivity@ X ï‚£ Y <=> X = Y | true.

asymmetry@ X ï‚£ Y, Y ï‚£ X <=> X=Y.

% Constraint simplification (or rewriting) rules

% Syntax: <ruleName>@ <simplifiedHead> <=> <guard> | <body>

% Logically:ï€¢XïƒŽvars(head ïƒˆ guard)

% <guard> ïƒž (<head> ïƒ› ï€¤YïƒŽvars(body - (head ïƒˆ guard)) <body>)

% Operationally: substitute in constraint store (knowledge base) constraints that match

% the rule simplified head by those in rule body with their variables instantiated from

% the match

transitivity@ X ï‚£ Y , Y ï‚£ Z ==> X ï‚£ Z.

% Constraint propagation (or production) rule (in this case, unguarded)

% Syntax: <ruleName>@ <propagatedHead> ==> guard | <body>

% Logically:ï€¢XïƒŽvars(head ïƒˆ guard)

% <guard> ïƒž (<head> ïƒž ï€¤YïƒŽvars(body - (head ïƒˆ guard)) <body>)

% Operationally: if constraint store (knowledge base) contains constraints that match

% the rule propagated head then add those in rule body to the store with their variables

% instantiated from the match

idempotence@ X ï‚£ Y \ X ï‚£Y <=> true.

% Constraint simpagation rule (in this case, unguarded)

% Syntax: <ruleName>@ <propagatedHead> \ <simplifiedHead> <=> guard | <body>

% Logically:ï€¢XïƒŽvars((head ïƒˆ guard) <guard> ïƒž (<propagatedhead> ïƒ™ <simplifiedHead>% ïƒ› ï€¤YïƒŽvars(body - (head ïƒˆ guard)) <body> ïƒ™ <propagatedhead>)

% Operationally: if constraint store (knowledge base) contains constraints that match

% the rule simplified head and the rule propagated head, then substitute in the store% those matching the simplified head by the rule body with their variables instantiated% from the match

query1: A ï‚£ B, C ï‚£ A, B ï‚£ C, A = 2% Initial constraint store: a constraint conjunction

answer1:A = 2, B = 2, C = 2, % Final constraint store = initial constraint store% simplified through repeated rule application until no rule neither simplifies nor% propagates any new constraint

query2:A ï‚£ B, B ï‚£ C, C ï‚£ A

answer2:A = B, B = C

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Rule-Defined Constraint Store

Built-In Constraint Store

Matching Equations ïƒˆ Guard

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Condition for firing a rule:

Rule head matches active constraint in RDCS

Generates set of equations between variables and constants from the head and the constraint (inserted to MEG)

Every other head from the rule matches against some other (partner) constraint in the RDCS

Generates another set of equations (inserted to MEG)

Rule r fires iff:ï€¢X1,...,Xi ïƒŽ vars(MEG ïƒˆ BICS - r) BICS ïƒž ï€¤Y1,...,Yj ïƒŽ vars(r) MEG

Active Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Normalizing Simplification

Active Constraint

r@ X ï‚£ Y <=> X = Y | true.ïƒ˜(ï€¢A,B A = 2 ïƒž ï€¤X',Y' X' = A = Y' = B), eg, B = 3 ï‚¹ 2 = A

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Active Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Rule firing order depends on 3 heuristics, with the following priority:

Rule-defined constraint ordering to become active

Rule ordering to try matching and entailment check with active constraint

Rule-defined constraint ordering to become partner constraints

Engine first tries matching and entailment check:

All rules with current active constraint, before trying any rule with the next constraint in the RDCS;

For all elements of the RDCS as partner for the first multi-headed rule that matches the active constraint, before trying the next rule that matches the active constraint;

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y' X' = A ïƒ™Y' = B = C), eg, B = 3 ï‚¹ 4 = C

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y' X' = B = C ïƒ™Y' = A),eg, B = 3 ï‚¹ 4 = C

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y' X' = A = C ïƒ™ Y' = B),eg, C = 3 ï‚¹ 2 = A

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y' X' = B ïƒ™ Y' = A = C),eg, C = 3 ï‚¹ 2 = A

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y', Z' X' = Z' = A ïƒ™ Y' = B = C), eg, B = 3 ï‚¹ 4 = C

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.ï€¢A,B,C A = 2 ïƒž ï€¤X',Y',Z' X' = C ïƒ™ Y' = A ïƒ™ Z' = B, e.g.,X'=C,Y'=2,Z'=B

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

For a given active constraint:

a matching multi-headed propagation rule is reapplied with all matching partner constraints, before any other rule is tried;

in contrast, a matching multi-headed simplification or simpagation rule is applied only once with the first matching partner constraint, and then engine moves on to the next rule

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.ï€¢A,B,C A = 2 ïƒž ï€¤X',Y',Z' X' = A ïƒ™ Y' = B ïƒ™ Z' = B, e.g.,X'=A,Y'=B, Z'=C

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Attempt to reapply same propagation rule matching same pair of active and partner constraints with same head pair but swapped assignments:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y', Z' X' = Z' = B ïƒ™ Y' = A = C), eg, A = 2 ï‚¹ 4 = C

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z. ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y', Z' X' = A ïƒ™ Y' = Z' =B = C), eg, B = 3 ï‚¹ 4 = C

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y', Z' X' = C ïƒ™ Y' = Z' =A = B), eg, A = 2 ï‚¹ 3 = B

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y', Z' X' = Y' =A = B ïƒ™ Z' = C ), eg, A = 2 ï‚¹ 3 = B

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y', Z' X' = Y' =A = C ïƒ™ Z' = B ), eg, A = 2 ï‚¹ 4 = C

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true. ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y', Z' X' = Y' =Z' = A = B = C ), eg, A = 2 ï‚¹ 4 = C

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true. ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y', Z' X' = Y' =Z' = A = B = C ), eg, A = 2 ï‚¹ 4 = C

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \X ï‚£ Y <=> true.

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \X ï‚£ Y <=> true. ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Yâ€˜ X' = Y' =A = B = C ), eg, A = 2 ï‚¹ 4 = C

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true. ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y' X' = Y' = A = B = C ), eg, A = 2 ï‚¹ 4 = C

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \X ï‚£ Y <=> true.

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \X ï‚£ Y <=> true. ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Yâ€˜ X' =A = C, Yâ€™ = B), eg, A = 2 ï‚¹ 4 =C

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true. ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y' X' = A = C,Yâ€™ = B ), eg, A = 2 ï‚¹ 4 = C

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \X ï‚£ Y <=> true.

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \X ï‚£ Y <=> true. ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Yâ€˜ X' =A, Yâ€™ = B = C), eg, B = 3 ï‚¹ 4 =C

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true. ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y' X' = A, Yâ€™ = B = C), eg, B = 3 ï‚¹ 4 = C

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Active

Constraint

Partner

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Heuristic to choose next active constraint after processing of active constraint A added to the store constraints N1, ... Nn

N1, ... , Nn in order

Constraints O1, ... , Om present in the store before processing A

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y' X' = Y' = B = C ), eg, B = 3 ï‚¹ 4 = C

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.

[email protected] ï‚£ Y,Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Partner

Constraint

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.

[email protected] ï‚£ Y,Y ï‚£ X <=> X=Y ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y' X' = Y' =A = B = C), eg, B = 3 ï‚¹ 4 = C

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Partner

Constraint

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.

[email protected] ï‚£ Y,Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Partner

Constraint

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.

[email protected] ï‚£ Y,Y ï‚£ X <=> X=Y ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y' X' = A = C ïƒ™ Y' = B), eg, A = 2 ï‚¹ 4 = C

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Partner

Constraint

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.

[email protected] ï‚£ Y,Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Partner

Constraint

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.

[email protected] ï‚£ Y,Y ï‚£ X <=> X=Y ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Y' X' = Y' = A = B = C), eg, A = 2 ï‚¹ 4 = C

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Partner

Constraint

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.

[email protected] ï‚£ Y,Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Partner

Constraint

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.

[email protected] ï‚£ Y,Y ï‚£ X <=> X=Y ïƒ˜(ï€¢A,B,C A = 2 ïƒž ï€¤X',Yâ€™ X' = Y' = A = B = C), eg, A = 2 ï‚¹ 4 = C

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Partner

Constraint

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.

[email protected] ï‚£ Y,Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Partner

Constraint

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.

[email protected] ï‚£ Y,Y ï‚£ X <=> X=Y ï€¢A,B,C A = 2 ïƒž ï€¤X',Y' X' = Câ€™ ïƒ™ Yâ€™ = B), eg, A = 2, Xâ€™ = C, Yâ€™ = B

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Partner

Constraint

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.

[email protected] ï‚£ Y,Y ï‚£ X <=> X=Y ï€¢A,B,C A = 2 ïƒž ï€¤X',Y' X' = Câ€™ ïƒ™ Yâ€™ = B), eg, A = 2, Xâ€™ = C, Yâ€™ = B

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true. ïƒ˜(ï€¢A,B,C A = 2, B = C ïƒž ï€¤X',Yâ€™ X' = Y' = A = B = C), eg, A = 2 ï‚¹ 4 = C

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Partner

Constraint

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y ïƒ˜(ï€¢A,B,C A = 2, B = C ïƒž ï€¤X',Yâ€™ X' = Y' = A = B = C), eg, A = 2 ï‚¹ 4 = C

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Partner

Constraint

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Alternate matching combination:

Active constraint matched against rightmost head

Partner constraint matched against leftmost head

Partner

Constraint

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y ïƒ˜(ï€¢A,B,C A = 2, B = C ïƒž ï€¤X',Yâ€™ X' = Y' = A = B = C), eg, A = 2 ï‚¹ 4 = C

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Partner

Constraint

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Partner

Constraint

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y ï€¢A,B,C A = 2, B = C ïƒž ï€¤X',Yâ€™ X' = A ïƒ™ Yâ€™ = C), eg, Xâ€™ = 2, Yâ€™ = C

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Partner

Constraint

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y ï€¢A,B,C A = 2, B = C ïƒž ï€¤X',Yâ€™ X' = A ïƒ™ Yâ€™ = C), eg, Xâ€™ = 2, Yâ€™ = C

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

r@ X ï‚£ Y <=> X = Y | true.

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true. ï€¢A,B,C A = B = C = 2 ïƒž ï€¤X',Yâ€™ X' = Yâ€™ = A = B), eg, Xâ€™ = 2, Yâ€™ = 2

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Active

Constraint

r@ X ï‚£ Y <=> X = Y | true. ï€¢A,B,C A = B = C = 2 ïƒž ï€¤X',Yâ€™ X' = Yâ€™ = A = B), eg, Xâ€™ = 2, Yâ€™ = 2

a@ X ï‚£ Y, Y ï‚£ X <=> X=Y

t@ X ï‚£ Y, Y ï‚£ Z ==> X ï‚£ Z.

i@ X ï‚£ Y \ X ï‚£ Y <=> true.

Constraints Simplified

Final Normalized Solved Form

body

guard

0..1

Logical

Formula

CHR

Rule

2..*

simplified head

*

0..1

And Formula

propagated head

0..1

{non-overlapping, complete}

Atomic Formula

Simpagation

Rule

Simplification

Rule

Propagation

Rule

Built-In

Constraint

Rule Defined

Constraint

CHR

Base

- Simplification rule: sh1(X,a), sh2(b,Y)<=>g1(X,Y), g2(a,b,c) | b1(X,c), b2(Y,c).
- Propagation rule: ph1(X,Y), ph2(d) ==>g3(X), g4(d,Y)|b3(X,d), b4(X,Y).
- Simpagation rule: ph3(X), ph4(Y,Z) \ sh3(X,U), sh4(Y,V)<=> g5(X,Z), g6(Z,Y) | b5(X), b6(Y,Z).
- Simplification rules are conditional rewrite rules (condition is the guard)
- Propagation rules are event-condition-action rules (event is the guard)
- Simpagation rules heads are hybrid syntactic sugar, each can be replaced by a semantically equivalent simplification rule, ex, p, r \ s, t <=> g, h | b, c. is equivalent to p, r, s, t <=> g, h | p, r, b, c.

Head: Rule-Defined Constraints

Guard:

Built-In Constraints

(from host language)

Body:

Rule-Defined and

Built-In Constraints

arg

*

Constraint

Symbol

*

Term

*

{non-overlapping,

complete}

{non-overlapping,

complete}

Rule Defined

Constraint

Symbol

Rule Defined

Constraint

arg

Built-In

Constraint

Symbol

Non-Ground

Term

Ground

Term

Non-Functional

Term

Functional

Term

Built-In

Constraint

true

false

Constant

Symbol

Function

Symbol

Constraint

Domain

Variable

*

*

body

guard

0..1

Logical

Formula

CHR

Rule

2..*

simplified head

*

CHR

Base

0..1

And Formula

propagated head

0..1

{non-overlapping, complete}

Atomic Formula

Simpagation

Rule

Simplification

Rule

Propagation

Rule

Used

Rule

*

Built-In

Constraint Store

Rule Defined

Constraint Store

*

*

Derivation

State

*

{ordered}

CHR

Derivation

body

guard

0..1

CHR

Logical

Formula

CHR

Rule

2..*

simplified head

*

CHR

Base

0..1

And Formula

propagated head

0..1

arg

Atomic Formula

*

Simpagation

Rule

Simplification

Rule

Propagation

Rule

Term

Built-In

Constraint

Rule Defined

Constraint

- Simplificationrule: sh1, ... , shi<=>g1, ..., gj|b1, ..., bk.
where: {X1, ..., Xn} = vars(sh1ïƒˆ ... ïƒˆ shiïƒˆg1ïƒˆ ... ïƒˆ gj) and {Y1, ... , Ym} = vars(b1ïƒˆ ... ïƒˆ bk) \ {X1, ..., Xn}ï€¢X1, ..., Xng1 ïƒ™ ... ïƒ™ gjïƒž (sh1ïƒ™ ... ïƒ™ shiïƒ› ï€¤Y1, ... , Ymb1ïƒ™ ... ïƒ™bk)

- Propagation rule: ph1, ... , phi==>g1, ..., gj|b1, ..., bk.
where: {X1, ..., Xn} = vars(ph1ïƒˆ ... ïƒˆ phiïƒˆg1ïƒˆ ... ïƒˆ gj)and {Y1, ... , Ym} = vars(b1ïƒˆ ... ïƒˆ bk) \ {X1, ..., Xn}ï€¢X1, ..., Xng1ïƒ™ ... ïƒ™ gjïƒž (ph1ïƒ™ ... ïƒ™ phiïƒž ï€¤Y1, ... , Ymb1ïƒ™ ... ïƒ™bk)

- No standard, implementation dependent
- Active constraint priority heuristics:
- Preferring constraints most recently inserted in store
- Left-to-right writing order in query

- Rule priority heuristics:
- Preferring simplification rules over simpagation rules and simpagation over propagation rules
- Preferring simplification and simpagation rules with highest number of heads
- Preferring propagation rules with lowest number of heads
- Preferring rules whose head constraint have never be matched yet
- Top to bottom writing order

- Partner constraint priority heuristics:
- Preferring constraints most recently inserted in store
- Left-to-right writing order in query

r1@ min(X,Y,Z) <=> X ï‚£ Y | Z = X

r2@ min(X,Y,Z) <=> Y ï‚£ X | Z = Y.

r3@ min(X,Y,Z) <=> Z < X | Y = Z.

r4@ min(X,Y,Z) <=> Z < Y | X = Z.

r5@ min(X,Y,Z) ==> Z ï‚£ X, Z ï‚£ Y.

r1@ min(X,Y,Z) <=> X ï‚£ Y | Z = Xï€¢M true |= ï€¤X'=1,Y'=2,Z'=M X' = 1, Y' = 2, Z' = M, 1 ï‚£ 2

r2@ min(X,Y,Z) <=> Y ï‚£ X | Z = Y.

r3@ min(X,Y,Z) <=> Z < X | Y = Z.

r4@ min(X,Y,Z) <=> Z < Y | Y = Z.

r5@ min(X,Y,Z) ==> Z ï‚£ X, Z ï‚£ Y.

r1@ min(X,Y,Z) <=> X ï‚£ Y | Z = Xï€¢M true |= ï€¤X'=1,Y'=2,Z'=M X' = 1, Y' = 2, Z' = M, X' = 1 ï‚£ 2 = Y'

r2@ min(X,Y,Z) <=> Y ï‚£ X | Z = Y.

r3@ min(X,Y,Z) <=> Z < X | Y = Z.

r4@ min(X,Y,Z) <=> Z < Y | Y = Z.

r5@ min(X,Y,Z) ==> Z ï‚£ X, Z ï‚£ Y.

r1@ min(X,Y,Z) <=> X ï‚£ Y | Z = Xï€¢M true |= ï€¤X'=1,Y'=2,Z'=M X' = 1, Y' = 2, Z' = M, X' = 1 ï‚£ 2 = Y'

r2@ min(X,Y,Z) <=> Y ï‚£ X | Z = Y.

r3@ min(X,Y,Z) <=> Z < X | Y = Z.

r4@ min(X,Y,Z) <=> Z < Y | Y = Z.

r5@ min(X,Y,Z) ==> Z ï‚£ X, Z ï‚£ Y.

Projection(CS,vars(Query))

r1@ min(X,Y,Z) <=> X ï‚£ Y | Z = X

r2@ min(X,Y,Z) <=> Y ï‚£ X | Z = Y.

r3@ min(X,Y,Z) <=> Z < X | Y = Z.

r4@ min(X,Y,Z) <=> Z < Y | Y = Z.

r5@ min(X,Y,Z) ==> Z ï‚£ X, Z ï‚£ Y.

r1@ min(X,Y,Z) <=> X ï‚£ Y | Z = X ï€¢A,B,M A ï‚£ B |= ï€¤X'=A,Y'=B,Z'=M X' = A, Y' = B, Z' = M, X' = A ï‚£ B = Y'

r2@ min(X,Y,Z) <=> Y ï‚£ X | Z = Y.

r3@ min(X,Y,Z) <=> Z < X | Y = Z.

r4@ min(X,Y,Z) <=> Z < Y | Y = Z.

r5@ min(X,Y,Z) ==> Z ï‚£ X, Z ï‚£ Y.

r1@ min(X,Y,Z) <=> X ï‚£ Y | Z = X ï€¢A,B,M A ï‚£ B |= ï€¤X'=A,Y'=B,Z'=M X' = A, Y' = B, Z' = M, X' = A ï‚£ B = Y'

r2@ min(X,Y,Z) <=> Y ï‚£ X | Z = Y.

r3@ min(X,Y,Z) <=> Z < X | Y = Z.

r4@ min(X,Y,Z) <=> Z < Y | Y = Z.

r5@ min(X,Y,Z) ==> Z ï‚£ X, Z ï‚£ Y.

r1@ min(X,Y,Z) <=> X ï‚£ Y | Z = X ï€¢A,B,M A ï‚£ B |= ï€¤X'=A,Y'=B,Z'=M X' = A, Y' = B, Z' = M, X' = A ï‚£ B = Y'

r2@ min(X,Y,Z) <=> Y ï‚£ X | Z = Y.

r3@ min(X,Y,Z) <=> Z < X | Y = Z.

r4@ min(X,Y,Z) <=> Z < Y | Y = Z.

r5@ min(X,Y,Z) ==> Z ï‚£ X, Z ï‚£ Y.

Projection(CS,vars(Query))

- Several solvers, each one implemented by a pair(CHR base, CHR engine)
- can be assembled in a component-based architecture,
- with server solvers' CHR bases defining in their rule heads
- the constraints used as built-ins by client solvers' CHR bases

<<Component>>

CHRDEngine

<<Component>>

MinCHRDBase

<<Interface>>

Min

min(X,Y,Z) ïƒ› X ï‚£ Y | Z = X

min(X,Y,Z) ïƒ› Z ï€¼ Y | Z = X

min(X,Y,Z) ïƒ› Y ï‚£ Z | Z = Y

min(X,Y,Z) ïƒ› Z ï€¼ X | Z = Y

min(X,Y,Z) ïƒž Z ï‚£ X ïƒ™ Z ï‚£ Y

min(X:Real, Y:Real, Z:Real)

<<Interface>>

CHRDEngine

Â«usesÂ»

Â«usesÂ»

derive()

<<Component>>

LoeStlCHRDBase

<<Component>>

HostPlatform

X ï‚£ Y ïƒ› X = Y | true

X ï‚£ Y ïƒ™ Y ï‚£ X ïƒž X = Y

X ï‚£ Y ïƒ™ Y ï‚£ Z ïƒž X ï‚£ ZX ï‚£ Y \ X ï‚£ Y ïƒ› true

X ï€¼ X ïƒ› false

X ï€¼ Y ïƒ™ Y ï€¼ Z ïƒž X ï‚¹ Y ïƒ™ Y ï‚¹ Z | X ï€¼ ZY ï€¼ Z ïƒ™ X ï‚£ Y ïƒž X ï‚¹ Y ïƒ™ Y ï‚¹ Z | X ï€¼ Z

X ï€¼ Y ïƒ™ Y ï‚£ Z ïƒž X ï‚¹ Y ïƒ™ Y ï‚¹ Z | X ï€¼ Z

<<Interface>>

LoeStl

<<Interface>>

EqNeq

Â«usesÂ»

ï‚£(X:Real, Y:Real): Boolean

- (X:Real, Y:Real): Boolean

= (X:Real, Y:Real): Boolean

ï‚¹ (X:Real, Y:Real): Boolean

r1@ ?P == C <=> P = C.

r2@ ?P + ?Q == C <=> ?Q.number, R := C - ?Q | ?P = R.

r3@ ?P + ?Q == C <=> ?P.number, R := C - ?P | ?Q = R.

r4@ ?P + ?Q == C \ ?P - ?Q == D <=> R := (C + D) / 2 | ?P = R.

Notation:

?P Constraint Domain Variable and CHR Variable

C Constraint Domain Constant and CHR Variable

== Constraint Domain Equality Predicate

= CHR Equality Predicate

0,1,2, ... CHR and Host Programming Language Constants

:= Host Programming Language Variable Assignment Predicate, always returns true, performs arithmetic computation as side-effect

+, -, / Host Programming Language Arithmetic Function

number Host Programming Language Type Checking Function

r1@ ?P == C <=> P = C ï€¢?Y, true|= ï€¤?P=?Y,C=2 ?P = ?Y, C = 2

r2@ ?P + ?Q == C <=> ?Q.number, R := C - ?Q | ?P = R.

r3@ ?P + ?Q == C <=> ?P.number, R := C - ?P | ?Q = R.

r4@ ?P + ?Q == C \ ?P - ?Q == D <=> R := (C + D) / 2 | ?P = R.

r1@ ?P == C <=> P = C

r2@ ?P + ?Q == C <=> ?Q.number, R := C - ?Q | ?P = R.

r3@ ?P + ?Q == C <=> ?P.number, R := C - ?P | ?Q = R.

r4@ ?P + ?Q == C \ ?P - ?Q == D <=> R := (C + D) / 2 | ?P = R.

r1@ ?P == C <=> P = C

r2@ ?P + ?Q == C <=> ?Q.number, R := C - ?Q | ?P = R.

r3@ ?P + ?Q == C <=> ?P.number, R := C - ?P | ?Q = R.

r4@ ?P + ?Q == C \ ?P - ?Q == D <=> R := (C + D) / 2 | ?P = R.

Why r1 does not apply?

r1@?P == C<=> P = C

r2@ ?P + ?Q == C <=> ?Q.number, R := C - ?Q | ?P = R.

ï€¢?X,?Y,?U,?V ?Y = 2 |= ï€¤<?P,?Q,C,R> = <?X,2,3,1> ?P = ?X, ?Q = ?Y, C = 3, ?Q.number, R = 1

r3@ ?P + ?Q == C <=> ?P.number, R := C - ?P | ?Q = R.

r4@ ?P + ?Q == C \ ?P - ?Q == D <=> R := (C + D) / 2 | ?P = R.

r1@?P == C<=> P = C

r2@ ?P + ?Q == C <=> ?Q.number, R := C - ?Q | ?P = R.

ï€¢?X,?Y ?Y = 2 |= ï€¤<?P,?Q,C,R> = <?X,2,3,1> ?P = ?X, ?Q = ?Y, C = 3, ?Q.number, R = 1

r3@ ?P + ?Q == C <=> ?P.number, R := C - ?P | ?Q = R.

r4@ ?P + ?Q == C \ ?P - ?Q == D <=> R := (C + D) / 2 | ?P = R.

r1@ ?P == C <=> P = C

r2@ ?P + ?Q == C <=> ?Q.number, R := C - ?Q | ?P = R.

r3@ ?P + ?Q == C <=> ?P.number, R := C - ?P | ?Q = R.

r4@ ?P + ?Q == C \ ?P - ?Q == D <=> R := (C + D) / 2 | ?P = R.

r1@ ?P == C <=> P = C

r2@ ?P + ?Q == C <=> ?Q.number, R := C - ?Q | ?P = R

r3@ ?P + ?Q == C <=> ?P.number, R := C - ?P | ?Q = R.

r4@ ?P + ?Q == C \ ?P - ?Q == D <=> R := (C + D) / 2 | ?P = R.

r1@ ?P == C <=> P = C

r2@ ?P + ?Q == C <=>?Q.number, R := C - ?Q | ?P = R

r3@ ?P + ?Q == C <=> ?P.number, R := C - ?P | ?Q = R.

r4@ ?P + ?Q == C \ ?P - ?Q == D <=> R := (C + D) / 2 | ?P = R.

r1@ ?P == C <=> P = C

r2@ ?P + ?Q == C <=>?Q.number, R := C - ?Q | ?P = R

r3@ ?P + ?Q == C <=> ?P.number, R := C - ?P | ?Q = R.

r4@ ?P + ?Q == C \ ?P - ?Q == D <=> R := (C + D) / 2 | ?P = R.

r1@ ?P == C <=> P = C

r2@ ?P + ?Q == C <=>?Q.number, R := C - ?Q | ?P = R

r3@ ?P + ?Q == C <=> ?P.number, R := C - ?P | ?Q = R.

r4@ ?P + ?Q == C \ ?P - ?Q == D <=> R := (C + D) / 2 | ?P = R

ï€¢?X,?Y,?U,?V ?X = 1, ?Y = 2 |= ï€¤<?P,?Q,C,D,R> = <?U,?V,0,2,1> ?P = ?U, ?Q = ?V, C = 0, D = 2, R = 1

r1@ ?P == C <=> P = C

r2@ ?P + ?Q == C <=>?Q.number, R := C - ?Q | ?P = R

r3@ ?P + ?Q == C <=> ?P.number, R := C - ?P | ?Q = R.

r4@ ?P + ?Q == C \ ?P - ?Q == D <=> R := (C + D) / 2 | ?P = R.

ï€¢?X,?Y,?U,?V ?X = 1, ?Y = 2 |= ï€¤<?P,?Q,C,D,R> = <?U,?V,0,2,1> ?P = ?U, ?Q = ?V, C = 0, D = 2, R = 1

r1@ ?P == C <=> P = C

r2@ ?P + ?Q == C <=> ?Q.number, R := C - ?Q | ?P = R

r3@ ?P + ?Q == C <=> ?P.number, R := C - ?P | ?Q = R.

r4@ ?P + ?Q == C \ ?P - ?Q == D <=> R := (C + D) / 2 | ?P = R.

r1@ ?P == C <=> P = C

r2@ ?P + ?Q == C <=> ?Q.number, R := C - ?Q | ?P = R

r3@ ?P + ?Q == C <=> ?P.number, R := C - ?P | ?Q = R.

r4@ ?P + ?Q == C \ ?P - ?Q == D <=> R := (C + D) / 2 | ?P = R.

r1@ ?P == C <=> P = C

r2@ ?P + ?Q == C <=> ?Q.number, R := C - ?Q | ?P = R.

r3@ ?P + ?Q == C <=> ?P.number, R := C - ?P | ?Q = R.

ï€¢?X,?Y,?U,?V ?X = 1, ?Y = 2, ?U = 1 |= ï€¤<?P,?Q,C,R> = <1,?V,0,-1> ?P = ?U, ?Q = ?V, C = 0, ?P.number, R = -1

r4@ ?P + ?Q == C \ ?P - ?Q == D <=> R := (C + D) / 2 | ?P = R.

r1@ ?P == C <=> P = C

r2@ ?P + ?Q == C <=> ?Q.number, R := C - ?Q | ?P = R.

r3@ ?P + ?Q == C <=> ?P.number, R := C - ?P | ?Q = R.

ï€¢?X,?Y,?U,?V ?X = 1, ?Y = 2, ?U = 1 |= ï€¤<?P,?Q,C,R> = <1,?V,0,-1> ?P = ?U, ?Q = ?V, C = 0, ?P.number, R = -1

r4@ ?P + ?Q == C \ ?P - ?Q == D <=> R := (C + D) / 2 | ?P = R.

OrAnd Formula

connective: enum{or,and}

2..*

body

guard

0..1

CHR

Rule

And

Formula

Tried

Alternative

Body

simplified head

*

CHR

Base

0..1

*

propagated head

0..1

Atomic Formula

Fired

Rule

Simpagation

Rule

Simplification

Rule

Propagation

Rule

Constraint

*

{ordered}

*

*

*

Derivation

State

CHR

Derivation

Rule Defined

Constraint Store

Rule Defined

Constraint

Built-In

Constraint

Built-In

Constraint Store

*

*

true

false

- Simplificationrule: sh1, ... , shi<=>g1, ..., gj|b11, ..., bkp ; ... ; b11, ..., blq.
where: {X1, ..., Xn} = vars(sh1ïƒˆ ... ïƒˆ shiïƒˆg1ïƒˆ ... ïƒˆ gj) and {Y1, ... , Ym} = vars(b1ïƒˆ ... ïƒˆ bk) \ {X1, ..., Xn}ï€¢X1, ..., Xng1 ïƒ™ ... ïƒ™ gjïƒž (sh1ïƒ™ ... ïƒ™shiïƒ› ï€¤Y1, ... , Ym((b11ïƒ™ ... ïƒ™bkp) ïƒš ... ïƒš (b11 ïƒ™ ... ïƒ™bkq))

- Propagation rule: ph1, ... , phi==>g1, ..., gj|b11, ..., bkp ; ... ; b11, ..., blq.
where: {X1, ..., Xn} = vars(ph1ïƒˆ ... ïƒˆ phiïƒˆg1ïƒˆ ... ïƒˆ gj)and {Y1, ... , Ym} = vars(b1ïƒˆ ... ïƒˆ bk) \ {X1, ..., Xn}ï€¢X1, ..., Xng1ïƒ™ ... ïƒ™ gjïƒž (ph1 ïƒ™ ... ïƒ™phiïƒž ï€¤Y1, ... , Ym ((b11ïƒ™ ... ïƒ™bkp) ïƒš ... ïƒš (b11 ïƒ™ ... ïƒ™bkq))

- When rule R with disjunctive body B1 ; ... ; Bk is fired
- Update both constraint stores using B1
- Start next matching-updating cycle

- When BICS = false or when no rule matches the RDCS
- Backtrack to last alternative body Bi
- Restore both constraint stores to their states prior to their update with Bi
- Update both constraint stores using Bi+1
- Start next matching-updating cycle

- Exhaustively try all alternative bodies of all fired rules through backtracking

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

d7@ d(r7,C) ==> (C = r ; C = b).

d4@ d(r4,C) ==> (C = r ; C = b).

d3@ d(r3,C) ==> (C = r ; C = b).

d2@ d(r2,C) ==> (C = b ; C = g).

d5@ d(r5,C) ==> (C = r ; C = g).

d6@ d(r6,C) ==> (C = r ; C = g; C = t).

m@ m <=> n(r1,r2), n(r1,r3), n(r1,r4), n(r1,r7), n(r2,r6), n(r3,r7), n(r4,r5), n(r4,r7), n(r5,r6), n(r5,r7).

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

...

d6@ d(r6,C) ==> (C = r ; C = g; C = t).

m@ m <=> n(r1,r2), n(r1,r3), n(r1,r4), n(r1,r7), n(r2,r6), n(r3,r7), n(r4,r5), n(r4,r7), n(r5,r6), n(r5,r7).

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

...

d6@ d(r6,C) ==> (C = r ; C = g; C = t).

m@ m <=> n(r1,r2), n(r1,r3), n(r1,r4), n(r1,r7), n(r2,r6), n(r3,r7), n(r4,r5), n(r4,r7), n(r5,r6), n(r5,r7).

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

...

d6@ d(r6,C) ==> (C = r ; C = g; C = t).

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

...

d6@ d(r6,C) ==> (C = r ; C = g; C = t).

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

...

d6@ d(r6,C) ==> (C = r ; C = g; C = t).

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

...

d6@ d(r6,C) ==> (C = r ; C = g; C = t).

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

...

d6@ d(r6,C) ==> (C = r ; C = g; C = t).

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

...

d6@ d(r6,C) ==> (C = r ; C = g; C = t).

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

...

d6@ d(r6,C) ==> (C = r ; C = g; C = t).

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

Already fired w/ same constraint. Not repeated to avoid trivial non-termination

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

...

d6@ d(r6,C) ==> (C = r ; C = g; C = t).

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

...

d6@ d(r6,C) ==> (C = r ; C = g; C = t).

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

d7@ d(r7,C) ==> (C = r ; C = b).

...

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false. ï€¢C1,C7ï€¤Ri',Rj',Ci',Cj' C1=r |ï‚¹ Ri=r1, Rj=r7, Ci=C1, Cj=C7, Ci=Cj

l1@ l([ ],[ ]) <=> true. eg., C1 = b ï‚¹ r = C7

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

d7@ d(r7,C) ==> (C = r ; C = b).

...

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

d7@ d(r7,C) ==> (C = r ; C = b).

...

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

d7@ d(r7,C) ==> (C = r ; C = b).

...

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

d7@ d(r7,C) ==> (C = r ; C = b).

...

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

d7@ d(r7,C) ==> (C = r ; C = b).

...

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

d7@ d(r7,C) ==> (C = r ; C = b).

...

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

d7@ d(r7,C) ==> (C = r ; C = b).

...

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

d7@ d(r7,C) ==> (C = r ; C = b).

...

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false. ï€¢C1,C7ï€¤Ri',Rj',Ci',Cj' C1=r |ï‚¹ Ri=r1, Rj=r7, Ci=C1, Cj=C7, Ci=Cj

l1@ l([ ],[ ]) <=> true. eg., Cj = b ï‚¹ r = Ci

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

d7@ d(r7,C) ==> (C = r ; C = b).

...

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

d7@ d(r7,C) ==> (C = r ; C = b).

...

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

d7@ d(r7,C) ==> (C = r ; C = b).

...

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

d7@ d(r7,C) ==> (C = r ; C = b).

d4@ d(r4,C) ==> (C = r ; C = b).

d3@ d(r3,C) ==> (C = r ; C = b).

d2@ d(r2,C) ==> (C = b ; C = g).

d5@ d(r5,C) ==> (C = r ; C = g).

d6@ d(r6,C) ==> (C = r ; C = g; C = t).

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

r1

r7

r2

r6

r

b

g

r3

r4

r5

r

g

t

b

b

g

g

r

r

r

b

b

b

% More efficient version with forward checking

d1@ c(r1,r), c(r1,b), c(r1,g) ==> false.

d1@ d(r1,C), c(r1,r), c(r1,b) ==> C = g.

d1@ d(r1,C), c(r1,r), c(r1,g) ==> C = b.

d1@ d(r1,C), c(r1,b), c(r1,g) ==> C = r.

d1@ d(r1,C), c(r1,b) ==> (C = r ; C = g).

d1@ d(r1,C), c(r1,g) ==> (C = r ; C = b).

d1@ d(r1,C), c(r1,r) ==> (C = b ; C = g).

d1@ d(r1,C) ==> (C = r ; C = b ; C = g).

...

d6@ d(r6,C) ==> (C = r ; C = g; C = t).

fcr@ n(Ri,Rj), d(Rj,Cj) ==> c(Ri,Cj).

n@ n(Ri,Rj), d(Ri,Ci), d(Rj,Cj) ==> Ci = Cj | false.

l1@ l([ ],[ ]) <=> true.

l2@ l([R|Rs],[C|Cs]) <=> d(R,C), l(Rs,Cs).

- Map each conditional rewrite system rule of the formCondition | LHS ï‚® RHSonto a CHRïƒš simplification rule of the formLHS ïƒ› Condition | RHSi.e., map the rewrite rule condition onto the CHRïƒšguard the rewrite rule LHS onto the CHRïƒšhead the rewrite rule RHS onto the CHRïƒšbody
- Replace each functional terms ti appearing in a Condition, LHS or RHS of the rewrite rule by:
- a new variable Vi, and
- a new equational atom Vi = ti in the guard, head or body (respectively) of the CHRïƒš

- For example:
- fib(suc(suc(N)) ï‚® plus(fib(suc(N)),fib(N)), becomes
- fib(U,V) <=> U = suc(W), W = suc(N) |fib(N,Y), fib(W,X), plus(X,Y,V).

plus(X,0) ï‚® X

plus(X,suc(Y)) ï‚® suc(plus(X,Y))

fib(0) ï‚® suc(0)

fib(suc(0)) ï‚® suc(0)

fib(suc(suc(N)) ï‚® plus(fib(suc(N)),fib(N))

a@ plus(X,U,V) <=> U = 0 | V = X.

b@ plus(X,U,V) <=> U = suc(Y) |

V = suc(W), plus(X,Y,W).

c@ fib(U,V) <=> U = 0 | V = suc(0).

d@ fib(U,V) <=> U = suc(0) | V = suc(0).

e@ fib(U,V) <=> U = suc(W), W = suc(N) |

fib(N,Y), fib(W,X), plus(X,Y,V).

a@ p(X,U,V) <=> U = 0 | V = X.

b@ p(X,U,V) <=> U = s(Y) | V = s(W), p(X,Y,W).

c@ f(U,V) <=> U = 0 | V = s(0).

d@ f(U,V) <=> U = s(0) | V = s(0).

e@ f(U,V) <=> U = s(W), W = s(N) |

f(N,Y), f(W,X), p(X,Y,V).

a@ p(X,U,V) <=> U = 0 | V = X.

b@ p(X,U,V) <=> U = s(Y) | V = s(W), p(X,Y,W).

c@ f(U,V) <=> U = 0 | V = s(0).

d@ f(U,V) <=> U = s(0) | V = s(0).

e@ f(U,V) <=> U = s(W), W = s(N) |

f(N,Y), f(W,X), p(X,Y,V).

Guard Entailment Condition:ï€¢R true ïƒž ï€¤N1,U1,V1,W1 U1=s(s(0)) ïƒ™ V1=R ïƒ™ U1=s(W1) ïƒ™ W1=s(N1),

e.g., N1=0, U1=s(s(0)), V1=R, W1=s(0)

a@ p(X,U,V) <=> U = 0 | V = X.

b@ p(X,U,V) <=> U = s(Y) | V = s(W), p(X,Y,W).

c@ f(U,V) <=> U = 0 | V = s(0).

d@ f(U,V) <=> U = s(0) | V = s(0).

e@ f(U,V) <=> U = s(W), W = s(N) |

f(N,Y), f(W,X), p(X,Y,V).

Built-in First-Order Atom Syntactic Equality Solver (Unification):U1=s(s(0)) ïƒ™ U1=s(W1) ïƒžW1=s(0)

W1=s(0)ïƒ™ W1=s(N1) ïƒžN1=0

a@ p(X,U,V) <=> U = 0 | V = X.

b@ p(X,U,V) <=> U = s(Y) | V = s(W), p(X,Y,W).

c@ f(U,V) <=> U = 0 | V = s(0).

d@ f(U,V) <=> U = s(0) | V = s(0).

e@ f(U,V) <=> U = s(W), W = s(N) |

f(N,Y), f(W,X), p(X,Y,V).

Built-in First-Order Atom Syntactic Equality Solver (Unification):U1=s(s(0)) ïƒ™ U1=s(W1) ïƒžW1=s(0)

W1=s(0)ïƒ™ W1=s(N1) ïƒžN1=0

a@ p(X,U,V) <=> U = 0 | V = X.

b@ p(X,U,V) <=> U = s(Y) | V = s(W), p(X,Y,W).

c@ f(U,V) <=> U = 0 | V = s(0).

d@ f(U,V) <=> U = s(0) | V = s(0).

e@ f(U,V) <=> U = s(W), W = s(N) |

f(N,Y), f(W,X), p(X,Y,V).

Guard Entailment Condition:

ï€¢R,N1,U1,V1,Y1,W1 R=V1 ïƒ™ N1=0 ïƒ™ U1=s(s(0)) ïƒ™ W1=s(0)

ïƒž ï€¤U2,V2 U2=N1 ïƒ™ V2=Y1 ïƒ™ U2=0,e.g., U2=0, V2=Y1

a@ p(X,U,V) <=> U = 0 | V = X.

b@ p(X,U,V) <=> U = s(Y) | V = s(W), p(X,Y,W).

c@ f(U,V) <=> U = 0 | V = s(0).

d@ f(U,V) <=> U = s(0) | V = s(0).

e@ f(U,V) <=> U = s(W), W = s(N) |

f(N,Y), f(W,X), p(X,Y,V).

Built-in First-Order Atom Syntactic Equality Solver (Unification):

V2=Y1 ïƒ™ V2=s(0) ïƒžY1=s(0)

a@ p(X,U,V) <=> U = 0 | V = X.

b@ p(X,U,V) <=> U = s(Y) | V = s(W), p(X,Y,W).

c@ f(U,V) <=> U = 0 | V = s(0).

d@ f(U,V) <=> U = s(0) | V = s(0).

e@ f(U,V) <=> U = s(W), W = s(N) |

f(N,Y), f(W,X), p(X,Y,V).

Guard Entailment Condition: ï€¢R,N1,U1,V1,Y1,W1,U2,V2

R=V1 ïƒ™ N1=U2=0 ïƒ™ U1=s(s(0)) ïƒ™ W1=Y1=V2=s(0)

ïƒž ï€¤U3,V3 U3=W1 ïƒ™ V3=X1 ïƒ™ U3=s(0) e.g., U3=s(0), V3=X1

a@ p(X,U,V) <=> U = 0 | V = X.

b@ p(X,U,V) <=> U = s(Y) | V = s(W), p(X,Y,W).

c@ f(U,V) <=> U = 0 | V = s(0).

d@ f(U,V) <=> U = s(0) | V = s(0).

e@ f(U,V) <=> U = s(W), W = s(N) |

f(N,Y), f(W,X), p(X,Y,V).

Built-in First-Order Atom Syntactic Equality Solver (Unification):

V3=X1 ïƒ™ V3=s(0) ïƒžX1=s(0)

a@ p(X,U,V) <=> U = 0 | V = X.

b@ p(X,U,V) <=> U = s(Y) | V = s(W), p(X,Y,W).

c@ f(U,V) <=> U = 0 | V = s(0).

d@ f(U,V) <=> U = s(0) | V = s(0).

e@ f(U,V) <=> U = s(W), W = s(N) |

f(N,Y), f(W,X), p(X,Y,V).

Guard Entailment Condition: ï€¢R,N1,U1,V1,X1,Y1,W1,U2,V2,U3,V3

R=V1 ïƒ™ N1=U2=0 ïƒ™ U1=s(s(0)) ïƒ™ W1=X1=Y1=V2=U3=V3=s(0)

ïƒž ï€¤U4,V4,X4,Y4,W4 X4=X1 ïƒ™ U4=Y1 ïƒ™ V4=V1 ïƒ™ U4=s(Y4)

e.g., U4=s(0), V4=R, X4=s(0), Y4=0

a@ p(X,U,V) <=> U = 0 | V = X.

b@ p(X,U,V) <=> U = s(Y) | V = s(W), p(X,Y,W).

c@ f(U,V) <=> U = 0 | V = s(0).

d@ f(U,V) <=> U = s(0) | V = s(0).

e@ f(U,V) <=> U = s(W), W = s(N) |

f(N,Y), f(W,X), p(X,Y,V).

Built-in First-Order Atom Syntactic Equality Solver (Unification):

U4=Y1 ïƒ™ Y1=s(0) ïƒžU4=s(0)

U4=s(0) ïƒ™ U4=s(Y4) ïƒž Y4=0

X4=X1 ïƒ™ X1=s(0) ïƒž X4=s(0)

a@ p(X,U,V) <=> U = 0 | V = X.

b@ p(X,U,V) <=> U = s(Y) | V = s(W), p(X,Y,W).

c@ f(U,V) <=> U = 0 | V = s(0).

d@ f(U,V) <=> U = s(0) | V = s(0).

e@ f(U,V) <=> U = s(W), W = s(N) |

f(N,Y), f(W,X), p(X,Y,V).

Guard Entailment Condition: ï€¢R,N1,U1,V1,X1,Y1,W1,U2,V2,U3,V3,U4,V4,W4,X4,Y4,

R=V1=V4=s(W4) ïƒ™ N1=U2=Y4=0 ) ïƒ™ U1=s(s(0)) ) ïƒ™ W1=X1=Y1=V2=U3=V3=U4=X4=s(0)

ïƒž ï€¤U5,V5,X5 X5=X4 ïƒ™ U5=Y4 ïƒ™ V5=W4 ïƒ™ U5 = 0

e.g., U5=0, V5=W4, X5=s(0)

a@ p(X,U,V) <=> U = 0 | V = X.

b@ p(X,U,V) <=> U = s(Y) | V = s(W), p(X,Y,W).

c@ f(U,V) <=> U = 0 | V = s(0).

d@ f(U,V) <=> U = s(0) | V = s(0).

e@ f(U,V) <=> U = s(W), W = s(N) |

f(N,Y), f(W,X), p(X,Y,V).

Built-in First-Order Atom Syntactic Equality Solver (Unification):

X5=X4 ïƒ™ X4=s(0) ïƒžX5=s(0)

X5=s(0) ïƒ™ V5=X5 ïƒž V5=s(0)

V5=s(0) ïƒ™ V5=W4 ïƒž W4=s(0)

W4=s(0) ïƒ™ R=s(W4) ïƒž R=s(s(0))

a@ p(X,U,V) <=> U = 0 | V = X.

b@ p(X,U,V) <=> U = s(Y) | V = s(W), p(X,Y,W).

c@ f(U,V) <=> U = 0 | V = s(0).

d@ f(U,V) <=> U = s(0) | V = s(0).

e@ f(U,V) <=> U = s(W), W = s(N) |

f(N,Y), f(W,X), p(X,Y,V).

Built-in First-Order Atom Syntactic Equality Solver (Unification):

X5=X4 ïƒ™ X4=s(0) ïƒžX5=s(0)

X5=s(0) ïƒ™ V5=X5 ïƒž V5=s(0)

V5=s(0) ïƒ™ V5=W4 ïƒž W4=s(0)

W4=s(0) ïƒ™ R=s(W4) ïƒž R=s(s(0))

a@ p(X,U,V) <=> U = 0 | V = X.

b@ p(X,U,V) <=> U = s(Y) | V = s(W), p(X,Y,W).

c@ f(U,V) <=> U = 0 | V = s(0).

d@ f(U,V) <=> U = s(0) | V = s(0).

e@ f(U,V) <=> U = s(W), W = s(N) |

f(N,Y), f(W,X), p(X,Y,V).

Built-in First-Order Atom Syntactic Equality Solver (Unification):

X5=X4 ïƒ™ X4=s(0) ïƒžX5=s(0)

X5=s(0) ïƒ™ V5=X5 ïƒž V5=s(0)

V5=s(0) ïƒ™ V5=W4 ïƒž W4=s(0)

W4=s(0) ïƒ™ R=s(W4) ïƒž R=s(s(0))

Projection(BICS, vars(Query))

CHRV:

Matching applied to atomic formula conjunctions

Rule head is matched with constraint store sub-set, which requires the head to be more general than the sub-set

Propagation rules provide further simplification opportunities

Common characteristics:

- Forward chains rules
- Requires conflict resolution strategy to choose:
- Which of several matching rules to fire

- Non-monotonic reasoning due to:
- Constraint retraction in Rule-Defined Constraint Store
- Retraction of substituted sub-term

- Tricky confluence and termination issues

Rewriting Systems:

- Unification of LHS is applied recursively down to sub-terms
- Rule LHS is unified with sub-term which allows the sub-term to be more general than the LHS
- All reasoning done through rewriting (no propagation rules)

- Map each production rule of the form:IF m1 AND ... AND ml THEN a1 AND ... AND anwhere: {a1 ,..., an} = {add(n1) ,..., add(ni)}ïƒˆ {delete(o1) ,..., delete(oj)}ïƒˆ {hplOp1(p11,..., p1n) ,..., hplOpk(pk1,..., pkm)}
- onto a CHRïƒš simpagation rule of the form:p1,..., pr \ o1 ,..., oj ïƒ›hplOp1(p11, ..., p1n) ,..., hplOpk(pk1,..., pkm) | n1 ,..., ni.where {p1,..., pr} = {m1,..., ml} \ {o1 ,..., oj}
- Valid only when:
- {o1 , ... , oj} \ {m1, ... , ml} = ïƒ†, and
- ï€¢CïƒŽ{hplOp1(p11,..., p1n),...,hplOpk(pk1,..., pkm)}, ï€¢OïƒŽ{o1,...,oj}, ï€¢NïƒŽ{n1,...,ni} C occurs before O and N in a1 and ... and an

- i.e., there no direct way in CHRïƒš to:
- delete facts (ground constraints) not matched in the rule head
- call host programming language operations after some matched facts have been deleted or add to the fact base (constraint store)
- two possibilities allowed in production systems that make the resulting rule base operational behavior hard to comprehend, verify and maintain

CHRV:

Constraint store contains arbitrary atoms including functional, non-ground atoms

Simplification rules allow straightforward modeling for goal-driven reasoning, with rewriting simulating Prolog-like backward chaining

Disjunctive bodies

Built-in backtracking search

Common characteristics:

- Forward chains rules
- Requires conflict resolution strategy to choose:
- Which of several matching rules to fire

- Non-monotonic reasoning due to:
- Constraint retraction in Rule-Defined Constraint Store
- Fact retraction in the RHS

- Tricky confluence and termination issues

Production Systems:

- Fact base only contains ground Datalog atoms
- Cumbersome modeling to implement goal-driven reasoning
- No disjunctions in RHS
- No built-in search

- Map Prolog fact base of the form {f1. ... fn.}
- onto a fact introduction CHRïƒšpropagation rule: facts ïƒžf1 ,..., fn.

- Map each set of Prolog deductive rules of the form{p(t11,...,tn1) :- b1. ... p(t1k,...,tnk) :- bk.}that provide the intentional part of the definition for predicate p
- onto a CHRïƒšsimplification rule of the formp(X1,...,Xn) ïƒ› (X1=t11,..., Xn=tn1, b1) ;...; (X1=t1k ,..., Xn=tnk, bk).where {X1,...,Xn} is a set of fresh variablesnot occurring in {t11,...,tn1,b1, ... p(t1k,...,tnk), bk}

- Map each set of Prolog facts of the form{p(t'11,...,t'n1). ,..., p(t'1k,...,t'nk).}that provide the extensional part of the definition for predicate p
- onto a CHRïƒšworld closure propagation rule of the formp(X1,...,Xn) ïƒž (X1=t'11,..., Xn=t'n1) ;...; (X1=t'1k ,..., Xn=t'nk).

- Valid only for pure Prolog programs

Prolog Program

father(john,mary). father(john,peter).mother(jane,mary).

person(john,male). person(peter,male). person(jane,female). person(mary,female). person(paul, male).

parent(P,C) :- father(P,C).parent(P,C) :- mother(P,C).

sibling(C1,C2) :- not C1 = C2, parent(P,C1),

parent(P,C2).

CHRïƒš Translation

facts ïƒž father(john,mary), father(john,peter), mother(jane,mary), person(john,male), person(peter,male), person(jane,female),

person(mary,female), person(paul, male).

parent(P,C) ïƒ› father(P,C) ; mother(P,C).

sibling(C1,C2) ïƒ› C1 ï‚¹ C2 | parent(P,C1), parent(P,C2).

father(F,C) ïƒž (F=john,C=mary) ; (F=john,C=peter).

mother(M,C) ïƒž (M=jane,C=mary) .

person(P,G) ïƒž (P=john, G=male) ; (P=peter, G=male) ;

(P=jane, G=male) ; (P=mary, G=male) ;

(P=paul, G=male).

- CFOL semantics of CHRV guardless, single head simplification rule, equivalent to CFOL semantics of pure Prolog clause set sharing same conclusion (Clark's completion)
- Simplificationrule: sh<=>true|b11, ..., bkp ; ... ; b11, ..., blq.
where: {X1, ..., Xn} = vars(shi), and {Y1, ... , Ym} = vars(b1ïƒˆ ... ïƒˆ bk) \ {X1, ..., Xn}ï€¢X1, ..., Xntrue ïƒž (shïƒ› ï€¤Y1, ... , Ym((b11ïƒ™ ... ïƒ™bkp) ïƒš ... ïƒš (b11 ïƒ™ ... ïƒ™bkq))

- Simplificationrule: sh<=>true|b11, ..., bkp ; ... ; b11, ..., blq.
- Equivalent Prolog clauses:{sh :- b11, ..., bkp. , ... , sh :- b11, ..., blq.}
- Thus, using Clark's completion, any Prolog program can be reformulated into a semantically equivalent CHRV program
- CHRV extends Prolog with:
- Conjunctions in the heads
- Guards
- Non-ground numerical constraints heads, guards and bodies
- Propagation rules

CLP Application Rule Base

CLP Engine

CHR Base for Domain D1 Solver

Prolog Engine

CHR Engine

...

CHR

Host

Programming

Language L

Prolog/L Bridge

CHR Base for Domain Dk Solver

CLP Application Rule Base

CHR Base for Domain D1 Solver

CHRïƒš Engine

...

CHRïƒš

Host

Programming

Language L

CHR Base for Domain Dk Solver

- Declarative, easy to extend and compose constraint solvers and all their applications
- Scheduling, allocation, planning, optimization, recommendation, configuration

- Deductive theorem proving (propositional and first-order) and all its applications:
- CASE tools, declarative programs analysis, formal methods in hardware and software design,

- Hypothetical abductive reasoning and all its applications:
- Diagnosis and repair, observation explanation, sensor data integration

- Multi-agent reasoning
- Spatio-temporal reasoning and robotics
- Hybrid reasoning integrating:
- Deduction, belief revision, abduction, constraint solving and optimization
- with open and closed world assumption

- Heterogeneous knowledge integration
- Semantic web services
- Natural language processing