1.61k likes | 1.78k Vues
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
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 Outline
Constraint Handling Rules (CHR):Key Ideas • 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
CHR by Example:Rule Base Defining in Terms of = reflexivity@ X Y <=> X = Y | true. asymmetry@ X Y, Y X <=> X=Y. % Constraint simplification (or rewriting) rules % Syntax: <ruleName>@ <simplifiedHead> <=> <guard> | <body> % Logically:Xvars(head guard) % <guard> (<head> Yvars(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:Xvars(head guard) % <guard> (<head> Yvars(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
CHR by Example: Rule Base Defining in Terms of = idempotence@ X Y \ X Y <=> true. % Constraint simpagation rule (in this case, unguarded) % Syntax: <ruleName>@ <propagatedHead> \ <simplifiedHead> <=> guard | <body> % Logically:Xvars((head guard) <guard> (<propagatedhead> <simplifiedHead>% Yvars(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. CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = Active Constraint
CHR by Example: Rule Base Defining in Terms of = 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. CHR by Example: Rule Base Defining in Terms of = 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; CHR by Example: Rule Base Defining in Terms of = 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. CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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. CHR by Example: Rule Base Defining in Terms of = 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. CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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. CHR by Example: Rule Base Defining in Terms of = 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. CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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. CHR by Example: Rule Base Defining in Terms of =
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 CHR by Example: Rule Base Defining in Terms of = 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. CHR by Example: Rule Base Defining in Terms of = 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. CHR by Example: Rule Base Defining in Terms of =
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 CHR by Example: Rule Base Defining in Terms of = 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. CHR by Example: Rule Base Defining in Terms of = 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. CHR by Example: Rule Base Defining in Terms of = 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. CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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. CHR by Example: Rule Base Defining in Terms of = 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. CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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. CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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. CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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. CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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. CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = 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 CHR by Example: Rule Base Defining in Terms of = Active Constraint Partner Constraint