Constraint Handling Rules

35
Orcas Constraint Handling Rules May, 2005 Jairson Vitorino and Marcos Aurélio ORCAS Orcas

description

Constraint Handling Rules. May, 2005 Jairson Vitorino and Marcos Aurélio ORCAS. Orcas. Contents. CHR Introduction Syntax Operational Semantics Example CHR-  Introduction Operational Semantics Example References. CHR. Constraint Handling Rules. Introduction. - PowerPoint PPT Presentation

Transcript of Constraint Handling Rules

Page 1: Constraint Handling Rules

Orcas

Constraint Handling Rules

May, 2005Jairson Vitorino and Marcos Aurélio

ORCAS

Orcas

Page 2: Constraint Handling Rules

Orcas

Contents

• CHR– Introduction – Syntax– Operational Semantics– Example

• CHR-– Introduction – Operational Semantics– Example

• References

Page 3: Constraint Handling Rules

Orcas

CHR

Constraint Handling Rules

Page 4: Constraint Handling Rules

Orcas

Introduction

• 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)

Page 5: Constraint Handling Rules

Orcas

Introduction

• 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

Page 6: Constraint Handling Rules

Orcas

Syntax

• Constraints– User-defined

• Ex: noattack(Q1,Q2), connected(X,Y,3)

– Built-In• Ex: A>B, A=B, ground(A)

Page 7: Constraint Handling Rules

Orcas

Syntax: typical rule

H1,...,Hn <=>G1,...Gn | B1,...,Bn

MultipleHeads

Guards Body

Commited choice: When the engine picks a rulethere will not be any backtracking, so in CHR order matters!

(Only user-defined)(Only built-in) Both constraint

categories

Page 8: Constraint Handling Rules

Orcas

Syntax

• Rules– Simplification

• H1,...,Hn <=> G1,...Gn | B1,...,Bn

– Propagation• H1,...,Hn =>G1,...Gn | B1,...,Bn

– Simpagation• H1,...,Hn / Hn1,...Hnn<=>G1,...Gn |

B1,...,Bn

Page 9: Constraint Handling Rules

Orcas

Operational Semantics

State

Goal or query

User defined Constraints Store (UDCS)

Built-in Constraint Store (BICS)

CHR ENGINE

Logical meaning: y Goal UDCS BICS

CHR ProgramOr Base Rule

Final State: true or false

Page 10: Constraint Handling Rules

Orcas

Operational 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.”

Page 11: Constraint Handling Rules

Orcas

Operational Semantics

• Transitions (Solve)

X=Y, G1, G2Goal

U1, ..., UnUDCS

B1, ..., BnBICS

Solve:

G1, G2Goal

U1, ..., UnUDCS

B1, ..., Bn, X=YBICS

Built-in

Page 12: Constraint Handling Rules

Orcas

Operational Semantics

• Transitions (Introduce)

C(a,b), G1, ..., GnGoal

U1, ..., UnUDCS

B1, ..., BnBICS

Solve:

G1, G2Goal

C(a,b), U1, ..., UnUDCS

B1, ..., BnBICS

User-defined

Page 13: Constraint Handling Rules

Orcas

Operational Semantics

• Transitions (Simplify)U1,U2 B1| U3,U4

U5..., UnGoal

U1, U2 ,V1, ..., VnUDCS

B1, ..., BnBICS

Simplify:

U5,..., UnGoal

U3, U4, V1, ..., VnUDCS

B1, ..., BnBICS

Rule in CHR program

Page 14: Constraint Handling Rules

Orcas

Operational Semantics

• Transitions (Propagate)U1,U2 B1| U3,U4

UnGoal

U1, U2, ..., UnUDCS

B1, ..., BnBICS

Propagate:

U1, U2,..., UnGoal

U3, U4, U1, U2, ..., UnUDCS

B1, ..., BnBICS

Rule in CHR program

Page 15: Constraint Handling Rules

Orcas

Operational Semantics

• Transitions (Simpagate)U1,U2 / U5B1| U3,U4

U4,..., Un

GoalU1, U2,U5, ..., Un

UDCSB1, ..., Bn

BICS

Simpagate:

U6, ..., Un

GoalU3,U4, U1,U2 ..., Un

UDCSB1, ..., Bn

BICS

Rule in CHR program

Page 16: Constraint Handling Rules

Orcas

Example

CHR ProgramX=<Y <=> X=Y | true. X=<Y,Y=<X <=> X=Y. X=<Y,Y=<Z ==> X=<Z.

Initial state:

A<=B, C<=A, B<=CGoal

trueUDCS

trueBICS

Page 17: Constraint Handling Rules

Orcas

ExampleIntroduce:

C<=A, B<=CGoal

A<=BUDCS

trueBICS

Introduce:

B<=CGoal

C<=A, A<=BUDCS

trueBICS

Page 18: Constraint Handling Rules

Orcas

ExampleIntroduce:

trueGoal

C<=A, A<=B, B<=C

UDCStrue

BICS

Propagate: X<=Y,Y<=Z ==> X<=Z.

trueGoal

C<=B, C<=A, A<=B, B<=C

UDCStrue

BICS

Page 19: Constraint Handling Rules

Orcas

ExampleIntroduce:

trueGoal

C<=A, A<=BB<=C, C<=B

UDCStrue

BICS

Simplify: X<=Y,Y<=X <=> X=Y.

trueGoal

C<=A, A<=BUDCS

B=CBICS

Page 20: Constraint Handling Rules

Orcas

ExampleSolve:

trueGoal

C<=A, A<=BUDCS

B=CBICS

Simplify: X<=Y,Y<=X <=> X=Y.

Goaltrue

UDCSB=C, A=B

BICS

(Logo: C<=A equivale a B<=A)

true

Page 21: Constraint Handling Rules

Orcas

ExampleSolve:

trueGoal

trueUDCS

B=C, A=BBICS

Final State

Page 22: Constraint Handling Rules

Orcas

CHR-

Constraint Handling Rules with Disjunctions

Page 23: Constraint Handling Rules

Orcas

Introduction

• Allows disjunctions in the bodies of rules and in goals

• It can be used as a general-purpose logic programming language

Page 24: Constraint Handling Rules

Orcas

Operational Semantics

• State– Minimal set of subgoals

• Ex: a (b (c d)) e

– Logical meaning: y A– Initial State– Final State

• Successful• Failed

Page 25: Constraint Handling Rules

Orcas

Operational Semantics

• Transitions (Solve)

Solve:

a (b c) (b d) eGoal

a (b (c d)) eGoal

Page 26: Constraint Handling Rules

Orcas

Operational 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”

Page 27: Constraint Handling Rules

Orcas

Operational Semantics

• Transitions (Simplify)– b c f g

Simplify:

a (f g) (b d) eGoal

a (b c) (b d) eGoal

Page 28: Constraint Handling Rules

Orcas

Operational Semantics

• Transitions (Split)

Simplify:

(b (a d e f)) (c (a d e f))Goal

(b c) (a d e f)Goal

Page 29: Constraint Handling Rules

Orcas

Example

bird albatross penguin.penguin flies false.

Initial state:

bird flies

Goal

Page 30: Constraint Handling Rules

Orcas

ExampleSimplify: bird albatross penguin.

(albatross penguin) flies

Goal

Split:

(albatross flies) (penguin flies)Goal

Page 31: Constraint Handling Rules

Orcas

Example

Propagate: penguin flies false.

(albatross flies) falseGoal

Final State (no derivation step can be applied)

Page 32: Constraint Handling Rules

Orcas

Example: K-coloring map

main() <=> true | color(X1),                 color(X2),                 color(X3),                 color(X4),                 connected(X1,X2),                 connected(X1,X3),                 connected(X3,X4),                 connected(X2,X4).

connected(X1,X2) <=> ground(X1), ground(X2), X1=X2 |false.color(X)         ==> true | (X=1 ; X=2 ; X=3).

Page 33: Constraint Handling Rules

Orcas

Example: 4-Queens

main() <=> 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) <=> ground(X), ground(Y) | P=Y+D, M=Y~D, ne(X,Y), ne(X,P),ne(X,M).c(X) <=> true | (X=1 ; X=2 ; X=3 ; X=4).sol(X1,X2,X3,X4) <=> ground(X1), ground(X2), ground(X3), ground(X4) |                       write(X1), write(X2), write(X3), writeLn(X4).

Page 34: Constraint Handling Rules

Orcas

Reference

• Theory and practice of Constraint Handling Rules (http://www.pst.informatik.uni-muenchen.de/personen/fruehwir/jlp-chr1/jlp-chr1.html)

• A languagem for experimenting with Declarative Paradigms (http://citeseer.ist.psu.edu/611754.html)

Page 35: Constraint Handling Rules

Orcas

Reference

• 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/