350 likes | 534 Views
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.
E N D
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 • 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)
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
Syntax • Constraints • User-defined • Ex: noattack(Q1,Q2), connected(X,Y,3) • Built-In • Ex: A>B, A=B, ground(A)
Syntax: typical rule H1,...,Hn <=>G1,...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!
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
Operational 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
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.”
Solve: X=Y, G1, G2 G1, G2 U1, ..., Un U1, ..., Un B1, ..., Bn B1, ..., Bn, X=Y Goal Goal UDCS UDCS BICS BICS Operational Semantics • Transitions (Solve) Built-in
Solve: C(a,b), G1, ..., Gn G1, G2 U1, ..., Un C(a,b), U1, ..., Un B1, ..., Bn B1, ..., Bn Goal Goal UDCS UDCS BICS BICS Operational Semantics • Transitions (Introduce) User-defined
Simplify: U5..., Un U5,..., Un U1, U2 ,V1, ..., Vn U3, U4, V1, ..., Vn B1, ..., Bn B1, ..., Bn Goal Goal UDCS UDCS BICS BICS Operational Semantics Rule in CHR program • Transitions (Simplify) U1,U2 B1| U3,U4
Propagate: Un U1, U2,..., Un U1, U2, ..., Un U3, U4,U1, U2, ..., Un B1, ..., Bn B1, ..., Bn Goal Goal UDCS UDCS BICS BICS Operational Semantics • Transitions (Propagate) U1,U2 B1| U3,U4 Rule in CHR program
Simpagate: U4,..., Un U6, ..., Un U1, U2,U5, ..., Un U3,U4,U1,U2 ..., Un B1, ..., Bn B1, ..., Bn Goal Goal UDCS UDCS BICS BICS Operational Semantics • Transitions (Simpagate) U1,U2 / U5B1| U3,U4 Rule in CHR program
Initial state: A<=B, C<=A, B<=C true true Goal UDCS BICS Example CHR Program X=<Y <=> X=Y | true. X=<Y,Y=<X <=> X=Y. X=<Y,Y=<Z ==> X=<Z.
Introduce: C<=A, B<=C A<=B true Goal UDCS BICS Introduce: B<=C C<=A, A<=B true Goal UDCS BICS Example
Introduce: true C<=A, A<=B, B<=C true Goal UDCS BICS Propagate: X<=Y,Y<=Z ==> X<=Z. true C<=B, C<=A, A<=B, B<=C true Goal UDCS BICS Example
Introduce: true C<=A, A<=B B<=C, C<=B true Goal UDCS BICS Simplify: X<=Y,Y<=X <=> X=Y. true C<=A, A<=B B=C Goal UDCS BICS Example
Solve: true C<=A, A<=B B=C Goal UDCS BICS Simplify: X<=Y,Y<=X <=> X=Y. true B=C, A=B Goal UDCS BICS Example (Logo: C<=A equivale a B<=A) true
Solve: true true B=C, A=B Goal UDCS BICS Example Final State
CHR- Constraint Handling Rules with Disjunctions
Introduction • Allows disjunctions in the bodies of rules and in goals • It can be used as a general-purpose logic programming language
Operational Semantics • State • Minimal set of subgoals • Ex: a (b (c d)) e • Logical meaning: y A • Initial State • Final State • Successful • Failed
Solve: • a (b c) (b d) e • a (b (c d)) e Goal Goal Operational Semantics • Transitions (Solve)
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”
Simplify: • a (f g) (b d) e • a (b c) (b d) e Goal Goal Operational Semantics • Transitions (Simplify) • b c f g
Simplify: • (b (a d e f)) (c (a d e f)) • (b c) (a d e f) Goal Goal Operational Semantics • Transitions (Split)
Initial state: bird flies Goal Example bird albatross penguin. penguin flies false.
Simplify: bird albatross penguin. Split: (albatross penguin) flies (albatross flies) (penguin flies) Goal Goal Example
Propagate: penguin flies false. (albatross flies) false Goal Example Final State (no derivation step can be applied)
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).
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).
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)
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/