Download presentation
Presentation is loading. Please wait.
1
Ontologies Reasoning Components Agents Simulations Rule-Based Reasoning with Constraint Handling Rules Jacques Robin
2
Outline Rules as a knowledge representation formalism Common characteristics of rule-based systems Roadmap of rule-based languages Common advantages and limitations Example practical application of rules: declarative business rules History of rule-based systems Constraint Handling Rules (CHR) Abstract syntax Declarative logical semantics High-level operational semantics Example rule bases Extension: CHR V Practical applications
3
Rules as a Knowledge Representation Formalism What is a rule? A statement that specifies that: If a determined logical combination of conditions is satisfied, over the set of an agent’s percepts and/or facts in its Knowledge Base (KB) that represent the current, past and/or hypothetical future of its environment model, its goals and/or its preferences, then a logico-temporal combination of actions can or must be executed by the agent, directly on its environment (through actuators) or on the facts in its KB. A KB agent such that the persistent part of its KB consists entirely of such rules is called a rule-base agent; In such case, the inference engine used by the KB agent is an interpreter or a compiler for a specific rule language.
4
Rule-Based Agent Environment Sensors Effectors Rule Base: Persistant intentional knowledge Dependent on problem class, not instance Declarative code Ask Fact Base: Volatile knowledge Dependent on problem instance Data Rule Engine: Problem class independent Only dependent on rule language Declarative code interpreter or compiler TellRetract Ask
5
Rule Languages: Common Characteristics Syntax generally: Extends a host programming language and/or Restricts a formal logic and/or Uses a semi-natural language with closed keyword set expressing logical connectives and actions classes, and an open keyword set to refer to the entities and relations appearing in the agent’s fact base; Some systems provide 3 distinct syntax layers for different users with automated tools to translate a rule across the various layers; Declarative semantics: generally based on some formal logic; Operational semantics: Generally based on transition systems, automata or similar procedural formalisms; Formalizes the essence of the rule interpreter algorithm.
6
Rule Languages: General Advantages Human experts in many domains (medicine, law, finance, marketing, administration, design, engineering, equipment maintenance, games, sports, etc.) find it intuitive and easy to formalize their knowledge as a rule base Facilitates knowledge acquisition Rules can be easily paraphrased in semi-natural language syntax, friendlier to experts averse to computational languages Facilitates knowledge acquisition Rule bases easy to formalize as logical formulas (conjunctions of equivalences and/or implications) Facilitates building rule engine that perform sound, logic-based inference Each rule largely independent of others in the base (but to precisely what degree depends highly of the rule engine algorithm) Can thus be viewed as an encapsulated, declarative piece of knowledge; Facilitates life cycle evolution and composition of knowledge bases Very sophisticated, mature rule base compilation techniques available Allows scalable inference in practice Some engines for simple rule languages can efficiently handle millions of rules
7
Rule Languages: General Limitations Subtle interactions between rules hard to debug without: sophisticated rule explanation GUI detailed knowledge of the rule engine’s algorithm Especially serious with: Object-oriented rule languages for combining rule-based deduction or abduction with class-based inheritance; Probabilistic rule languages for combining logical and Bayesian inference; But purely logical relational rule language do not naturally: Embed within mainstream object-oriented modeling and programming languages Represent inherently procedural, taxonomic and uncertain knowledge Current research challenge: User-friendly reasoning engine for probabilistic object-oriented rules
8
Business Rules Example of modern commercial application of rule-based knowledge representation GUI Layer Data Layer Business Logic Layer Classic 3-Tier Information System Architecture Imperative OO Program Imperative OO Language SQL API Imperative OO Language GUI API Classic Imperative OO Implementation Rule-Based Implementation Imperative OO Host Language Embedded Production Rule Engine Imperative OO Language SQL API Imperative OO Language GUI API Production Rule Base Generic Component Reusable in Any Application Domain Easier to reflect frequent policy changes than imperative code
9
Semi-Natural Language Syntax for Business Rules Associate key word or key phrase to: Each domain model entity or relation name Each rule language syntactic construct Each host programming language construct used in rules Substitute in place of these constructs and symbols the associated words or phrase Example: “Is West Criminal?” in semi-natural language syntax: IF P is American AND P sells a W to N AND W is a weapon AND N is a nation AND N is hostile THEN P is a criminal IF nono owns a W AND W is a missile THEN west sells nono to W IF W is a missile THEN W is a weapon IF N is an enemy of America THEN N is hostile
10
OO Rule Languages NeOPS JEOPS CLIPS JESS XML Web Markup Languages CLP(X) Rule-Based Constraint Languages Roadmap of Rule-Based Languages XSLT OPS5 Production Rules ISO Prolog Logic Programming Transaction Logic HiLog Concurrent Prolog Courteous Rules CCLP(X) Frame Logic Flora OCLMOF UML CHORD RuleML ELAN Maude Otter EProver Rewrite Rules SWSL CHR V CHR QVT Java Smalltalk C++ Pure OO Languages
11
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 CHR V adds backtracking search and thus generalizes Prolog rules as well
12
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: @ | % Logically: X vars(head guard) % ( Y vars(body - (head guard)) ) % 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: @ ==> guard | % Logically: X vars(head guard) % ( Y vars(body - (head guard)) ) % 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
13
CHR by Example: Rule Base Defining in Terms of = idempotence@ X Y \ X Y true. % Constraint simpagation rule (in this case, unguarded) % Syntax: @ \ guard | % Logically: X vars((head guard) % Y vars(body - (head guard)) % 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
14
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. RuleRDCSBICSMEG A B, C A, B C A = 2 Matching Equations Guard Built-In Constraint StoreRule-Defined Constraint Store
15
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. Condition for firing a rule: 1.Rule head matches active constraint in RDCS Generates set of equations between variables and constants from the head and the constraint (inserted to MEG) 2.Every other head from the rule matches against some other (partner) constraint in the RDCS Generates another set of equations (inserted to MEG) 3.Rule r fires iff: X1,...,Xi vars(MEGS BICS - r) BICS Y1,...,Yj vars(r) MEG RuleRDCSBICSMEG r? A B, C A, B C A = 2X' = A, Y' = B, X' = Y' Active Constraint
16
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. RuleRDCSBICSMEG r? A B, C A, B C A = 2X' = A = Y' = B Normalizing Simplification Active Constraint
17
CHR by Example: Rule Base Defining in Terms of = 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. RuleRDCSBICSMEG r? A B, C A, B C A = 2X' = A = Y' = B Active Constraint
18
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. Rule firing order depends on 3 heuristics, with the following priority: 1.Rule-defined constraint ordering to become active 2.Rule ordering to try matching and entailment check with active constraint 3.Rule-defined constraint ordering to become partner constraints Engine first tries matching and entailment check for all rules with current active constraint, before trying any rule with the next constraint in the RDCS RuleRDCSBICSMEG a? A B, C A, B C A = 2X' = A, Y' = B, Y' = C, X' = A Active Constraint Partner Constraint
19
CHR by Example: Rule Base Defining in Terms of = 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. RuleRDCSBICSMEG a? A B, C A, B C A = 2X' = A, Y' = B = C Active Constraint Partner Constraint
20
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. Alternate matching combination: Active constraint matched against rightmost head Partner constraint matched against leftmost head RuleRDCSBICSMEG a? A B, C A, B C A = 2X' = C, Y' = A, Y' = A, X' = B Active Constraint Partner Constraint
21
CHR by Example: Rule Base Defining in Terms of = 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 RuleRDCSBICSMEG a? A B, C A, B C A = 2X' = B = C, Y' = A Active Constraint Partner Constraint
22
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. RuleRDCSBICSMEG t? A B, C A, B C A = 2X' = A, Y' = B, Y' = C, Z' = A Active Constraint Partner Constraint
23
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. 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. RuleRDCSBICSMEG t? A B, C A, B C A = 2X' = Z' = A, Y' = B = C Active Constraint Partner Constraint
24
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. Alternate matching combination: Active constraint matched against rightmost head Partner constraint matched against leftmost head RuleRDCSBICSMEG t? A B, C A, B C A = 2X' = C, Y' = A, Y' = A, Z' = B Active Constraint Partner Constraint
25
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. A,B,C A = 2 | X'=C,Y'=2,Z'=B, X' = C, Y' = A, Z' = B i@ X Y \ X Y true. Alternate matching combination: Active constraint matched against rightmost head Partner constraint matched against leftmost head RuleRDCSBICSMEG t? A B, C A, B C A = 2X' = C, Y' = A, Z' = B Active Constraint Partner Constraint
26
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. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B A B, C A, B C, C B A = 2
27
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. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B r? A B, C A, B C, C B A = 2X' = C, Y' = B, X' = Y' Active Constraint
28
CHR by Example: Rule Base Defining in Terms of = r@ X Y X = Y | true. B,C A = 2 | X',Y' X' = Y' = B = C, eg, B = 3 2 = C a@ X Y, Y X X=Y t@ X Y, Y Z ==> X Z. i@ X Y \ X Y true. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B r? A B, C A, B C, C B A = 2X' = Y' = B = C Active Constraint
29
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. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B a? A B, C A, B C, C B A = 2X' = C, Y' = B, Y' = A, X' = B Active Constraint Partner Constraint
30
CHR by Example: Rule Base Defining in Terms of = r@ X Y X = Y | true. a@ X Y, Y X X=Y A,B,C A = 2 | X',Y' X' = Y' = A = B = C, eg, B = 3 2 = C t@ X Y, Y Z ==> X Z. i@ X Y \ X Y true. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B a? A B, C A, B C, C B A = 2X' = Y' = A = B = C Active Constraint Partner Constraint
31
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. Alternate matching combination: Active constraint matched against rightmost head Partner constraint matched against leftmost head RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B a? A B, C A, B C, C B A = 2X' = A, Y' = B, Y' = C, X' = B Active Constraint Partner Constraint
32
CHR by Example: Rule Base Defining in Terms of = r@ X Y X = Y | true. a@ X Y, Y X X=Y A,B,C A = 2 | X',Y' X' = Y' = A = B = C, eg, B = 3 2 = 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 RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B a? A B, C A, B C, C B A = 2X' = Y' = A = B = C Active Constraint Partner Constraint
33
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. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B t? A B, C A, B C, C B A = 2X' = C, Y' = B, Y' = A, Z' = B Active Constraint Partner Constraint
34
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. A,B,C A = 2 | X',Y',Z' X' = C, Y' = A = B = C eg, B = 3 2 = C i@ X Y \ X Y true. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B t? A B, C A, B C, C B A = 2X' = C, Y' = A = B = C Active Constraint Partner Constraint
35
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. Alternate matching combination: Active constraint matched against rightmost head Partner constraint matched against leftmost head RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B t? A B, C A, B C, C B A = 2X' = A, Y' = B, Y' = C, Z' = B Active Constraint Partner Constraint
36
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. A,B,C A = 2 | X',Y',Z' X' = A, Y' = B = C, Z' = B eg, B = 3 2 = C i@ X Y \ X Y true. Alternate matching combination: Active constraint matched against rightmost head Partner constraint matched against leftmost head RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B t? A B, C A, B C, C B A = 2X' = A, Y' = B = C, Z' = B Active Constraint Partner Constraint
37
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. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i? A B, C A, B C, C B A = 2X' = C, Y' = B, X' = A, Y' = B Active Constraint Partner Constraint
38
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. A,B,C A = 2 | X',Y' X' = A = C, Y' = B, eg, C = 3 2 = A RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i? A B, C A, B C, C B A = 2X' = A = C, Y' = B Active Constraint Partner Constraint
39
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. Alternate matching combination: Active constraint matched against rightmost head Partner constraint matched against leftmost head RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i? A B, C A, B C, C B A = 2X' = A, Y' = B, X' = C, Y' = B Active Constraint Partner Constraint
40
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. A,B,C A = 2 | X',Y' X' = A = C, Y' = B, eg, C = 3 2 = A Alternate matching combination: Active constraint matched against rightmost head Partner constraint matched against leftmost head RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i? A B, C A, B C, C B A = 2X' = A = C, Y' = B Active Constraint Partner Constraint
41
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. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i? A B, C A, B C, C B A = 2X' = C, Y' = B, Y' = C, X' = A Active Constraint Partner Constraint
42
CHR by Example: Rule Base Defining in Terms of = r@ X Y X = Y | true. a@ X Y, Y X X=Y A,B,C A = 2 | X',Y' X' = Y' = A = B = C eg, C = 3 2 = A t@ X Y, Y Z ==> X Z. i@ X Y \ X Y true. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i? A B, C A, B C, C B A = 2X' = Y' = A = B = C Active Constraint Partner Constraint
43
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. Alternate matching combination: Active constraint matched against rightmost head Partner constraint matched against leftmost head RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i? A B, C A, B C, C B A = 2X' = C, Y' = A, X' = C, Y' = B Active Constraint Partner Constraint
44
CHR by Example: Rule Base Defining in Terms of = r@ X Y X = Y | true. a@ X Y, Y X X=Y A,B,C A = 2 | X',Y' X' = C, Y' = A = B eg, B = 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 RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i? A B, C A, B C, C B A = 2X' = C, Y' = A = B Active Constraint Partner Constraint
45
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. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i? A B, C A, B C, C B A = 2X' = C, Y' = B, Y' = C, Z' = A Active Constraint Partner Constraint
46
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. A,B,C A = 2 | X',Y',Z' X' = Y' = B = C, Z' = A eg, B = 3 2 = C i@ X Y \ X Y true. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i? A B, C A, B C, C B A = 2X' = Y' = B = C, Z' = A Active Constraint Partner Constraint
47
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. Alternate matching combination: Active constraint matched against rightmost head Partner constraint matched against leftmost RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i? A B, C A, B C, C B A = 2X' = C, Y' = A, Y' = C, Z' = B Active Constraint Partner Constraint
48
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. A,B,C A = 2 | X',Y',Z' X' = Y' = A = C, Z' = B eg, B = 3 2 = A i@ X Y \ X Y true. Alternate matching combination: Active constraint matched against rightmost head Partner constraint matched against leftmost RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i? A B, C A, B C, C B A = 2X' = Y' = A = C, Z' = B Active Constraint Partner Constraint
49
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. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i? A B, C A, B C, C B A = 2X' = C, Y' = B, X' = C, Y' = A Active Constraint Partner Constraint
50
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. A,B,C A = 2 | X',Y' X' = C, Y' = A = B eg, B = 3 2 = A RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i? A B, C A, B C, C B A = 2X' = C, Y' = A = B Active Constraint Partner Constraint
51
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. Alternate matching combination: Active constraint matched against rightmost head Partner constraint matched against leftmost RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i? A B, C A, B C, C B A = 2X' = C, Y' = A, X ' = C, Y' = B Active Constraint Partner Constraint
52
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. A,B,C A = 2 | X',Y' X' = C, Y' = A = B eg, B = 3 2 = A Alternate matching combination: Active constraint matched against rightmost head Partner constraint matched against leftmost RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i? A B, C A, B C, C B A = 2X' = C, Y' = A = B Active Constraint Partner Constraint
53
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. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i? A B, C A, B C, C B A = 2X' = C, Y' = B, Y' = B, Z' = C Active Constraint Partner Constraint
54
CHR by Example: Rule Base Defining in Terms of = r@ X Y X = Y | true. a@ X Y, Y X X=Y A,B,C A = 2 |= X'=C,Y'=2,Z'=C X' = Z' = C, Y' = B t@ X Y, Y Z ==> X Z. i@ X Y \ X Y true. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i? A B, C A, B C, C B A = 2X' = Z' = C, Y' = B Active Constraint Partner Constraint
55
CHR by Example: Rule Base Defining in Terms of = r@ X Y X = Y | true. a@ X Y, Y X X=Y A,B,C A = 2 |= X'=C,Y'=2,Z'=C X' = Z' = C, Y' = B t@ X Y, Y Z ==> X Z. i@ X Y \ X Y true. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i! A B, C A, B C, C B A = 2X' = Z' = C, Y' = B A B, C A A = 2, B = C
56
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. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i! A B, C A, B C, C B A = 2X' = Z' = C, Y' = B r? A B, C A A = 2, B = CX' = A, Y' = B, X' = Y' Active Constraint
57
CHR by Example: Rule Base Defining in Terms of = r@ X Y X = Y | true. A,B,C A = 2 B = C | X',Y' X' = Y' = A = 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. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i! A B, C A, B C, C B A = 2X' = Z' = C, Y' = B r? A B, C A A = 2, B = CX' = Y' = A = B Active Constraint
58
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. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i! A B, C A, B C, C B A = 2X' = Z' = C, Y' = B a? A B, C A A = 2, B = CX' = A, Y' = B, Y' = C, X' = A Active Constraint Partner Constraint
59
CHR by Example: Rule Base Defining in Terms of = r@ X Y X = Y | true. a@ X Y, Y X X=Y A,B,C A = 2 B = C |= X'=2,Y'=B X' = A, Y' = B = C t@ X Y, Y Z ==> X Z. i@ X Y \ X Y true. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i! A B, C A, B C, C B A = 2X' = Z' = C, Y' = B a! A B, C A A = 2, B = CX' = A, Y' = B = C Active Constraint Partner Constraint
60
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. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i! A B, C A, B C, C B A = 2X' = Z' = C, Y' = B a! A B, C A A = 2, B = CX' = A, Y' = B = C A = 2, B = C, A = B Constraints Simplified
61
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. RuleRDCSBICSMEG t! A B, C A, B C A = 2X' = C, Y' = A, Z' = B i! A B, C A, B C, C B A = 2X' = Z' = C, Y' = B a! A B, C A A = 2, B = CX' = A, Y' = B = C A = B = C = 2 Final Normalized Solved Form
62
Body: Rule-Defined and Built-In Constraints Guard: Built-In Constraints (from host language) Head: Rule-Defined Constraints 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. 2..* And Formula CHR: Syntax Overview CHR Base * CHR Rule guard simplified head propagated head body Logical Formula 0..1 Atomic Formula Simpagation Rule Simplification Rule Propagation Rule {non-overlapping, complete} Built-In Constraint Rule Defined Constraint
63
CHR: Complete Abstract Syntax Simpagation Rule Simplification Rule Propagation Rule CHR Base * CHR Rule guard simplified head propagated head body Logical Formula 0..1 2..* {non-overlapping, complete} Non-Ground Term Ground Term {non-overlapping, complete} And Formula Atomic Formula arg * Term Constraint Symbol Functional Term Non-Functional Term {non-overlapping, complete} arg * Function Symbol Constraint Domain * * * Built-In Constraint Rule Defined Constraint truefalse Variable Constant Symbol Rule Defined Constraint Symbol Built-In Constraint Symbol
64
CHR: Derivation Data Structures Simpagation Rule Simplification Rule Propagation Rule CHR Base * CHR Rule guard simplified head propagated head body CHR Logical Formula Atomic Formula Built-In Constraint Rule Defined Constraint 0..1 2..* Term And Formula arg * * * * {ordered} Rule Defined Constraint Store Built-In Constraint Store Used Rule Derivation State * CHR Derivation
65
CHR: Declarative Semantics in Classical First-Order Logic (CFOL) Simplification rule: sh 1,..., sh i g 1,..., g j | b 1,..., b k. where: {X 1,..., X n } = vars(sh 1 ... sh i g 1 ... g j ) and {Y 1,..., Y m } = vars(b 1 ... b k ) \ {X 1,..., X n } X 1,..., X n g1 ... gj (sh 1 ... sh i Y 1,..., Y m b 1 ... b k ) Propagation rule: ph 1,..., ph i ==> g 1,..., g j | b 1,..., b k. where: {X 1,..., X n } = vars(ph 1 ... ph i g 1 ... g j ) and {Y 1,..., Y m } = vars(b 1 ... b k ) \ {X 1,..., X n } X 1,..., X n g 1 ... g j (ph 1 ... ph i Y 1,..., Y m b 1 ... b k )
66
CHR: Constraint and Rule Priority Heuristics 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
67
CHR Base Example: Defining min in Terms of , and = 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. RuleRDCSBICSMEG min(1,2,M)
68
CHR Base Example: Defining min in Terms of , and = 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. RuleRDCSBICSMEG r?min(1,2,M)true X' = 1, Y' = 2, Z' = M, X' Y'
69
CHR Base Example: Defining min in Terms of , and = 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. RuleRDCSBICSMEG r!min(1,2,M)true X' = 1, Y' = 2, Z' = M, X' Y' trueM = Z' = X' = 1
70
CHR Base Example: Defining min in Terms of , and = 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. RuleRDCSBICSMEG r!min(1,2,M)true X' = 1, Y' = 2, Z' = M, X' Y' trueM = 1 Projection(CS,vars(Query))
71
CHR Base Example: Defining min in Terms of , and = 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. RuleRDCSBICSMEG min(A,B,M) A B
72
CHR Base Example: Defining min in Terms of , and = 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. RuleRDCSBICSMEG r1?min(A,B,M) A BX' = A, Y' = B, Z' = M, X' Y'
73
CHR Base Example: Defining min in Terms of , and = 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. RuleRDCSBICSMEG r1!min(A,B,M) A BX' = A, Y' = B, Z' = M, X' Y' true M = Z' = X' = A, A B
74
CHR Base Example: Defining min in Terms of , and = 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. RuleRDCSBICSMEG r1!min(A,B,M) A BX' = A, Y' = B, Z' = M, X' Y' true M = A, A B Projection(CS,vars(Query))
75
CHR Component Bases 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
76
X Y X = Y | true X Y Y X X = Y X Y Y Z X = Z X Y X Y X Y X X false X Y Y Z X Y Y Z | X Z X Y Y Z X Y Y Z | X Z X Y Y Z X Y Y Z | X Z > strictlyLessCHRDBase derive > lessOrEqualCHRDBase = derive > HostPlatform > CHRDEngine derive = > SyntacticEquality = (X:Real, Y:Real): Boolean (X:Real, Y:Real): Boolean > strictlyLess (X:Real, Y:Real): Boolean 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 > MinCHRDBase min derive > Min min(X:Real, Y:Real, out Z:Real) > CHRDEngine derive() > lessOrEqual (X:Real, Y:Real): Boolean
77
CHR Base Example: Restricted Form of Real Linear Equations Solver 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 RuleRDCSBICSMEG ?Y == 2, ?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 true
78
CHR Base Example: Restricted Form of Real Linear Equations Solver 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. RuleRDCSBICSMEG r1??Y == 2, ?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 true?P = ?Y, C = 2
79
CHR Base Example: Restricted Form of Real Linear Equations Solver 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. RuleRDCSBICSMEG r1!?Y == 2, ?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 true?P = ?Y, C = 2 ?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 ?Y = 2
80
CHR Base Example: Restricted Form of Real Linear Equations Solver 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. RuleRDCSBICSMEG r1!?Y == 2, ?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 true?P = ?Y, C = 2 r1??X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 ?Y = 2
81
CHR Base Example: Restricted Form of Real Linear Equations Solver r1@ ?P == C P = C r2@ ?P + ?Q == C ?Q.number, R := C - ?Q | ?P = R. ?X,?Y,?U,?V ?Y = 2 |= = ?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. RuleRDCSBICSMEG r1!?Y == 2, ?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 true?P = ?Y, C = 2 r2??X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 ?Y = 2?P = ?X, ?Q = ?Y, C = 3, ?Q.number, R = 1
82
CHR Base Example: Restricted Form of Real Linear Equations Solver r1@ ?P == C P = C r2@ ?P + ?Q == C ?Q.number, R := C - ?Q | ?P = R. ?X,?Y,?U,?V ?Y = 2 |= = ?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. RuleRDCSBICSMEG r1!?Y == 2, ?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 true?P = ?Y, C = 2 r2!?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 ?Y = 2?P = ?X, ?Q = ?Y, C = 3, ?Q.number, R = 1 ?Y = 2, ?X = 1
83
CHR Base Example: Restricted Form of Real Linear Equations Solver 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. RuleRDCSBICSMEG r1!?Y == 2, ?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 true?P = ?Y, C = 2 r2!?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 ?Y = 2?P = ?X, ?Q = ?Y, C = 3, ?Q.number, R = 1 r1??U - ?V == 2, ?U + ?V == 0 ?Y = 2, ?X = 1
84
CHR Base Example: Restricted Form of Real Linear Equations Solver 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. RuleRDCSBICSMEG r1!?Y == 2, ?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 true?P = ?Y, C = 2 r2!?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 ?Y = 2?P = ?X, ?Q = ?Y, C = 3, ?Q.number, R = 1 r2??U - ?V == 2, ?U + ?V == 0 ?Y = 2, ?X = 1?P = ?U, ?Q = ?V, C = 2, ?Q.number
85
CHR Base Example: Restricted Form of Real Linear Equations Solver 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. RuleRDCSBICSMEG r1!?Y == 2, ?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 true?P = ?Y, C = 2 r2!?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 ?Y = 2?P = ?X, ?Q = ?Y, C = 3, ?Q.number, R = 1 r3??U - ?V == 2, ?U + ?V == 0 ?Y = 2, ?X = 1?P = ?U, ?Q = ?V, C = 2, ?P.number
86
CHR Base Example: Restricted Form of Real Linear Equations Solver 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. RuleRDCSBICSMEG r1!?Y == 2, ?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 true?P = ?Y, C = 2 r2!?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 ?Y = 2?P = ?X, ?Q = ?Y, C = 3, ?Q.number, R = 1 r4??U - ?V == 2, ?U + ?V == 0 ?Y = 2, ?X = 1?P = ?U, ?Q = ?V, C = 0, ?P = ?U, ?Q = ?V, D = 2, R = 1
87
CHR Base Example: Restricted Form of Real Linear Equations Solver 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 = ?U, ?Q = ?V, C = 0, D = 2, R = 1 RuleRDCSBICSMEG r1!?Y == 2, ?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 true?P = ?Y, C = 2 r2!?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 ?Y = 2?P = ?X, ?Q = ?Y, C = 3, ?Q.number, R = 1 r4??U - ?V == 2, ?U + ?V == 0 ?Y = 2, ?X = 1?P = ?U, ?Q = ?V, C = 0, D = 2, R = 1
88
CHR Base Example: Restricted Form of Real Linear Equations Solver 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 = ?U, ?Q = ?V, C = 0, D = 2, R = 1 RuleRDCSBICSMEG r1!?Y == 2, ?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 true?P = ?Y, C = 2 r2!?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 ?Y = 2?P = ?X, ?Q = ?Y, C = 3, ?Q.number, R = 1 r4!?U - ?V == 2, ?U + ?V == 0 ?Y = 2, ?X = 1?P = ?U, ?Q = ?V, C = 0, D = 2, R = 1 ?U + ?V == 0?Y = 2, ?X = 1, ?U = 1
89
CHR Base Example: Restricted Form of Real Linear Equations Solver 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. RuleRDCSBICSMEG r1!?Y == 2, ?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 true?P = ?Y, C = 2 r2!?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 ?Y = 2?P = ?X, ?Q = ?Y, C = 3, ?Q.number, R = 1 r4!?U - ?V == 2, ?U + ?V == 0 ?Y = 2, ?X = 1?P = ?U, ?Q = ?V, C = 0, D = 2, R = 1 r1??U + ?V == 0?Y = 2, ?X = 1, ?U = 1
90
CHR Base Example: Restricted Form of Real Linear Equations Solver 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. RuleRDCSBICSMEG r1!?Y == 2, ?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 true?P = ?Y, C = 2 r2!?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 ?Y = 2?P = ?X, ?Q = ?Y, C = 3, ?Q.number, R = 1 r4!?U - ?V == 2, ?U + ?V == 0 ?Y = 2, ?X = 1?P = ?U, ?Q = ?V, C = 0, D = 2, R = 1 r2??U + ?V == 0?Y = 2, ?X = 1, ?U = 1?P = ?U, ?Q = ?V, C = 0, ?Q.number
91
CHR Base Example: Restricted Form of Real Linear Equations Solver 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 = ?U, ?Q = ?V, C = 0, ?P.number, R = -1 r4@ ?P + ?Q == C \ ?P - ?Q == D R := (C + D) / 2 | ?P = R. RuleRDCSBICSMEG r1!?Y == 2, ?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 true?P = ?Y, C = 2 r2!?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 ?Y = 2?P = ?X, ?Q = ?Y, C = 3, ?Q.number, R = 1 r4!?U - ?V == 2, ?U + ?V == 0 ?Y = 2, ?X = 1?P = ?U, ?Q = ?V, C = 0, D = 2, R = 1 r3??U + ?V == 0?Y = 2, ?X = 1, ?U = 1?P = ?U, ?Q = ?V, C = 0, ?P.number, R = -1
92
CHR Base Example: Restricted Form of Real Linear Equations Solver 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 = ?U, ?Q = ?V, C = 0, ?P.number, R = -1 r4@ ?P + ?Q == C \ ?P - ?Q == D R := (C + D) / 2 | ?P = R. RuleRDCSBICSMEG r1!?Y == 2, ?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 true?P = ?Y, C = 2 r2!?X + ?Y == 3, ?U - ?V == 2, ?U + ?V == 0 ?Y = 2?P = ?X, ?Q = ?Y, C = 3, ?Q.number, R = 1 r4!?U - ?V == 2, ?U + ?V == 0 ?Y = 2, ?X = 1?P = ?U, ?Q = ?V, C = 0, D = 2, R = 1 r3!?U + ?V == 2?Y = 2, ?X = 1, ?U = 1?P = ?U, ?Q = ?V, C = 0, ?P.number, R = -1 true?Y = 2, ?X = 1, ?U = 1, ?V = -1
93
CHR : Abstract Syntax OrAnd Formula connective: enum{or,and} Simpagation Rule Simplification Rule Propagation Rule CHR Base * CHR Rule guard simplified head propagated head body And Formula Atomic Formula Constraint Built-In Constraint Rule Defined Constraint 2..* truefalse 0..1 Built-In Constraint Store Rule Defined Constraint Store * Fired Rule Derivation State * CHR Derivation * {ordered} ** Tried Alternative Body * *
94
CHR : Declarative Semantics in Classical First-Order Logic (CFOL) Simplification rule: sh 1,..., sh i g 1,..., g j | b 1 1,..., b k p ;... ; b 1 1,..., b l q. where: {X 1,..., X n } = vars(sh 1 ... sh i g 1 ... g j ) and {Y 1,..., Y m } = vars(b 1 ... b k ) \ {X 1,..., X n } X 1,..., X n g1 ... gj (sh 1 ... sh i Y 1,..., Y m ((b 1 1 ... b k p ) ... (b 1 1 ... b k q )) Propagation rule: ph 1,..., ph i ==> g 1,..., g j | b 1 1,..., b k p ;... ; b 1 1,..., b l q. where: {X 1,..., X n } = vars(ph 1 ... ph i g 1 ... g j ) and {Y 1,..., Y m } = vars(b 1 ... b k ) \ {X 1,..., X n } X 1,..., X n g 1 ... g j (ph 1 ... ph i Y 1,..., Y m ((b 1 1 ... b k p ) ... (b 1 1 ... b k q ))
95
CHR : Operational Semantics When rule R with disjunctive body B 1 ;... ; B k is fired Update both constraint stores using B 1 Start next matching-updating cycle When BICS = false or when no rule matches the RDCS Backtrack to last alternative body B i Restore both constraint stores to their states prior to their update with B i Update both constraint stores using B i+1 Start next matching-updating cycle Exhaustively try all alternative bodies of all fired rules through backtracking
96
CHR Base Example: Map Coloring Problem 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]).true
97
CHR Base Example: Map Coloring Problem 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m?m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]).true
98
CHR Base Example: Map Coloring Problem 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) true
99
CHR Base Example: Map Coloring Problem 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2?l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6]
100
CHR Base Example: Map Coloring Problem 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] 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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) true
101
CHR Base Example: Map Coloring Problem 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] n?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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) true
102
CHR Base Example: Map Coloring Problem 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] d1a?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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) trueC = C1
103
CHR Base Example: Map Coloring Problem 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] d1a!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) trueC = C1 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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) C1 = r
104
CHR Base Example: Map Coloring Problem 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] d1a!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) trueC = C1 n?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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) C1 = r
105
CHR Base Example: Map Coloring Problem 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] d1a!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) trueC = C1 d1a?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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) C1 = rR = r1, C = C1 Already fired w/ same constraint. Not repeated to avoid trivial non-termination
106
CHR Base Example: Map Coloring Problem 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] d1a!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) trueC = C1 l2?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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) C1 = rR = r7, Rs = [r4,r3,r2,r5,r6], C = C7, Cs = [C4,C3,C2,C5,C6]
107
CHR Base Example: Map Coloring Problem 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] d1a!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) trueC = C1 l2!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) C1 = rR = r7, Rs = [r4,r3,r2,r5,r6], C = C7, Cs = [C4,C3,C2,C5,C6] 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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = r
108
CHR Base Example: Map Coloring Problem d1@ d(r1,C) ==> (C = r ; C = b ; C = g). d7@ d(r7,C) ==> (C = r ; C = b).... 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. 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] d1a!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) trueC = C1 l2!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) C1 = rR = r7, Rs = [r4,r3,r2,r5,r6], C = C7, Cs = [C4,C3,C2,C5,C6] n?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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = rRi = r1, Rj = r7, Ci = C1, Cj = C7, Ci = Cj
109
CHR Base Example: Map Coloring Problem d1@ d(r1,C) ==> (C = r ; C = b ; C = g). d7@ d(r7,C) ==> (C = r ; C = b).... 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] d1a!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) trueC = C1 l2!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) C1 = rR = r7, Rs = [r4,r3,r2,r5,r6], C = C7, Cs = [C4,C3,C2,C5,C6] d7a?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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = rRi = r7, C = C7
110
CHR Base Example: Map Coloring Problem d1@ d(r1,C) ==> (C = r ; C = b ; C = g). d7@ d(r7,C) ==> (C = r ; C = b).... 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] d1a!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) trueC = C1 l2!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) C1 = rR = r7, Rs = [r4,r3,r2,r5,r6], C = C7, Cs = [C4,C3,C2,C5,C6] d7a!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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = rRi = r7, C = C7 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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = r, C7 = r
111
CHR Base Example: Map Coloring Problem d1@ d(r1,C) ==> (C = r ; C = b ; C = g). d7@ d(r7,C) ==> (C = r ; C = b).... 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] d1a!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) trueC = C1 l2!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) C1 = rR = r7, Rs = [r4,r3,r2,r5,r6], C = C7, Cs = [C4,C3,C2,C5,C6] d7a!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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = rRi = r7, C = C7 n?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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = r, C7 = r Ri = r1, Rj = r7, Ci = r, Cj = r
112
CHR Base Example: Map Coloring Problem d1@ d(r1,C) ==> (C = r ; C = b ; C = g). d7@ d(r7,C) ==> (C = r ; C = b).... 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] d1a!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) trueC = C1 l2!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) C1 = rR = r7, Rs = [r4,r3,r2,r5,r6], C = C7, Cs = [C4,C3,C2,C5,C6] d7a!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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = rRi = r7, C = C7 n!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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = r, C7 = r Ri = r1, Rj = r7, Ci = r, Cj = r 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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) false
113
CHR Base Example: Map Coloring Problem d1@ d(r1,C) ==> (C = r ; C = b ; C = g). d7@ d(r7,C) ==> (C = r ; C = b).... 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] d1a!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) trueC = C1 l2!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) C1 = rR = r7, Rs = [r4,r3,r2,r5,r6], C = C7, Cs = [C4,C3,C2,C5,C6] d7a!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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = rRi = r7, C = C7 n!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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = r, C7 = r Ri = r1, Rj = r7, Ci = r, Cj = r btn(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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) false
114
CHR Base Example: Map Coloring Problem d1@ d(r1,C) ==> (C = r ; C = b ; C = g). d7@ d(r7,C) ==> (C = r ; C = b).... 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] d1a!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) trueC = C1 l2!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) C1 = rR = r7, Rs = [r4,r3,r2,r5,r6], C = C7, Cs = [C4,C3,C2,C5,C6] d7b?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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = rRi = r7, C = C7
115
CHR Base Example: Map Coloring Problem d1@ d(r1,C) ==> (C = r ; C = b ; C = g). d7@ d(r7,C) ==> (C = r ; C = b).... 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] d1a!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) trueC = C1 l2!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) C1 = rR = r7, Rs = [r4,r3,r2,r5,r6], C = C7, Cs = [C4,C3,C2,C5,C6] d7b!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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = rRi = r7, C = C7 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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = r, C7 = b
116
CHR Base Example: Map Coloring Problem d1@ d(r1,C) ==> (C = r ; C = b ; C = g). d7@ d(r7,C) ==> (C = r ; C = b).... 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. 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] d1a!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) trueC = C1 l2!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) C1 = rR = r7, Rs = [r4,r3,r2,r5,r6], C = C7, Cs = [C4,C3,C2,C5,C6] d7b!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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = rRi = r7, C = C7 n?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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = r, C7 = b Ri = r1, Rj = r7, Ci = C1, Cj = C7, Ci = Cj
117
CHR Base Example: Map Coloring Problem d1@ d(r1,C) ==> (C = r ; C = b ; C = g). d7@ d(r7,C) ==> (C = r ; C = b).... 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] d1a!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) trueC = C1 l2!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) C1 = rR = r7, Rs = [r4,r3,r2,r5,r6], C = C7, Cs = [C4,C3,C2,C5,C6] d7b!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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = rRi = r7, C = C7 l2?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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = r, C7 = b R = r4, Rs = [r3,r2,r5,r6], C = C4, Cs = [C3,C2,C5,C6]
118
CHR Base Example: Map Coloring Problem d1@ d(r1,C) ==> (C = r ; C = b ; C = g). d7@ d(r7,C) ==> (C = r ; C = b).... 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] d1a!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) trueC = C1 l2!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) C1 = rR = r7, Rs = [r4,r3,r2,r5,r6], C = C7, Cs = [C4,C3,C2,C5,C6] d7b!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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = rRi = r7, C = C7 l2!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), d(r1,C1), d(r7,C7), d(r4,C4), l([r3,r2,r5,r6],[C3,C2,C5,C6]) C1 = r, C7 = b R = r4, Rs = [r3,r2,r5,r6], C = C4, Cs = [C3,C2,C5,C6]
119
CHR Base Example: Map Coloring Problem d1@ d(r1,C) ==> (C = r ; C = b ; C = g). d7@ d(r7,C) ==> (C = r ; C = b).... 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m!m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]),true l2!l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]), 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) trueR = r1, Rs = [r7,r4,r3,r2,r5,r6], C = C1, Cs = [C7,C4,C3,C2,C5,C6] d1a!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) trueC = C1 l2!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), d(r1,C1), l([r7,r4,r3,r2,r5,r6],[C7,C4,C3,C2,C5,C6]) C1 = rR = r7, Rs = [r4,r3,r2,r5,r6], C = C7, Cs = [C4,C3,C2,C5,C6] d7b!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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = rRi = r7, C = C7 l2?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), d(r1,C1), d(r7,C7), l([r4,r3,r2,r5,r6],[C4,C3,C2,C5,C6]) C1 = r, C7 = b
120
CHR Base Example: Map Coloring Problem 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb RuleRDCSBICSMEG m, l([r1,r7,r4,r3,r2,r5,r6],[C1,C7,C4,C3,C2,C5,C6]).true 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), d(r1,C1), d(r2,C2), d(r2,C2), d(r1,C3), d(r4,C4), d(r5,C5), d(r6,C6), d(r7,C7) C1 = g, C2 = b, C3 = r, C4 = r, C5 = g, C6 = r, C7 = b
121
CHR Base Example: Map Coloring Problem % 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). 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). 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). r2r2 bg r6r6 rgt r3r3 rb r4r4 rb r5r5 bg r1r1 rbg r7r7 rb
122
CHR for Deduction: Formulating Logical Entailment as Constraint Simplification How to use CHR to answer KB |= Q? 1.Using only propagation rules, simulating forward chaining Complete only for KB Horn clause conjunction and Q atom conjunction 2.Using only simplification rules, simulating backward chaining Complete only for KB Horn clause conjunction and Q atom conjunction 3.Using only propagation rules, simulating resolution-based refutation proof Complete only for Q atom conjunctions 4.Using only simplification rules, simulating resolution-based refutation proof Complete only for Q atom conjunctions
123
Forward Chaining as CHR Constraint Propagation 1.Put KB in Implicative Normal Form (INF) 2.Map each deductive rule in INF(KB) of the form: p 1 ... p i c 1 ... c j onto a CHR rule of the form: p 1,..., p i ==> c 1 ;... ; c j 3.Map each integrity constraint in INF(KB) of the form: p 1 ... p i false onto a CHR rule of the form: p 1,..., p i ==> false 4.Map all the facts in INF(KB) of the form: true f l onto a single CHR rule of the form: facts ==> f 1,..., f k 5.Map query Q of the form (q 11 ... q 1n ) ... (q m1 ... q mp ) onto the set of CHR constraint {(q 11,..., q 1n ) ;...; (q m1,..., q mp )} 6.Initialize RDCS = facts 7.Simplify RDCS with CHR engine 8.If at the end of the simplification: D RCDS BICS | disjunct(Q) and BICS {false} then KB |= Q BICS = {false}, then the KB was inconsistent Otherwise KB may or may not entail Q
124
Forward Chaining as CHR Constraint Propagation: Is West Criminal? f@ start ==> owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono). s@ owns(nono,W), missile(W) ==> sells(west,nono,W) w@ missile(W) ==> weapon(W) h@ enemy(N,america) ==> hostile(N) c@ american(P), weapon(W), nation(N), hostile(N), sells(P,N,W) ==> criminal(P) RuleRDCSBICSMEG f?starttrue
125
Forward Chaining as CHR Constraint Propagation: Is West Criminal? f@ start ==> owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono). s@ owns(nono,W), missile(W) ==> sells(west,nono,W) w@ missile(W) ==> weapon(W) h@ enemy(N,america) ==> hostile(N) c@ american(P), weapon(W), nation(N), hostile(N), sells(P,N,W) ==> criminal(P ) RuleRDCSBICSMEG f!starttrue start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), true
126
Forward Chaining as CHR Constraint Propagation: Is West Criminal? f@ start ==> owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono). s@ owns(nono,W), missile(W) ==> sells(west,nono,W) w@ missile(W) ==> weapon(W) h@ enemy(N,america) ==> hostile(N) c@ american(P), weapon(W), nation(N), hostile(N), sells(P,N,W) ==> criminal(P ) RuleRDCSBICSMEG f!starttrue s?start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono) truenono=nono, W'=m1
127
Forward Chaining as CHR Constraint Propagation: Is West Criminal? f@ start ==> owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono). s@ owns(nono,W), missile(W) ==> sells(west,nono,W) w@ missile(W) ==> weapon(W) h@ enemy(N,america) ==> hostile(N) c@ american(P), weapon(W), nation(N), hostile(N), sells(P,N,W) ==> criminal(P ) RuleRDCSBICSMEG f!starttrue s!start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), truenono=nono, W'=m1 start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), sells(west,nono,m1) true
128
Forward Chaining as CHR Constraint Propagation: Is West Criminal? f@ start ==> owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono). s@ owns(nono,W), missile(W) ==> sells(west,nono,W) w@ missile(W) ==> weapon(W) h@ enemy(N,america) ==> hostile(N) c@ american(P), weapon(W), nation(N), hostile(N), sells(P,N,W) ==> criminal(P ) RuleRDCSBICSMEG f!starttrue s!start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), truenono=nono, W'=m1 w?start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), sells(west,nono,m1) trueW'=m1
129
Forward Chaining as CHR Constraint Propagation: Is West Criminal? f@ start ==> owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono). s@ owns(nono,W), missile(W) ==> sells(west,nono,W) w@ missile(W) ==> weapon(W) h@ enemy(N,america) ==> hostile(N) c@ american(P), weapon(W), nation(N), hostile(N), sells(P,N,W) ==> criminal(P ) RuleRDCSBICSMEG f!starttrue s!start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), truenono=nono, W'=m1 w!start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), sells(west,nono,m1) trueW'=m1 start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), sells(west,nono,m1), weapon(m1) true
130
Forward Chaining as CHR Constraint Propagation: Is West Criminal? f@ start ==> owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono). s@ owns(nono,W), missile(W) ==> sells(west,nono,W) w@ missile(W) ==> weapon(W) h@ enemy(N,america) ==> hostile(N) c@ american(P), weapon(W), nation(N), hostile(N), sells(P,N,W) ==> criminal(P ) RuleRDCSBICSMEG f!starttrue s!start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), truenono=nono, W'=m1 w!start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), sells(west,nono,m1) trueW'=m1 h?start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), sells(west,nono,m1), weapon(m1) trueN=nono
131
Forward Chaining as CHR Constraint Propagation: Is West Criminal? f@ start ==> owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono). s@ owns(nono,W), missile(W) ==> sells(west,nono,W) w@ missile(W) ==> weapon(W) h@ enemy(N,america) ==> hostile(N) c@ american(P), weapon(W), nation(N), hostile(N), sells(P,N,W) ==> criminal(P) RuleRDCSBICSMEG f!starttrue s!start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), truenono=nono, W'=m1 w!start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), sells(west,nono,m1) trueW'=m1 h!start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), sells(west,nono,m1), weapon(m1) trueN'=nono start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), sells(west,nono,m1), weapon(m1), hostile(nono) true
132
Forward Chaining as CHR Constraint Propagation: Is West Criminal? f@ start ==> owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono). s@ owns(nono,W), missile(W) ==> sells(west,nono,W) w@ missile(W) ==> weapon(W) h@ enemy(N,america) ==> hostile(N) c@ american(P), weapon(W), nation(N), hostile(N), sells(P,N,W) ==> criminal(P ) RuleRDCSBICSMEG f!starttrue s!start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), truenono=nono, W'=m1 w!start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), sells(west,nono,m1) trueW'=m1 h!start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), sells(west,nono,m1), weapon(m1) trueN'=nono c?start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), sells(west,nono,m1), weapon(m1), hostile(nono) trueP'=west, W'=m1, N'=nono
133
Forward Chaining as CHR Constraint Propagation: Is West Criminal? f@ start ==> owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono). s@ owns(nono,W), missile(W) ==> sells(west,nono,W) w@ missile(W) ==> weapon(W) h@ enemy(N,america) ==> hostile(N) c@ american(P), weapon(W), nation(N), hostile(N), sells(P,N,W) ==> criminal(P ) RuleRDCSBICSMEG f!starttrue s!start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), truenono=nono, W'=m1 w!start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), sells(west,nono,m1) trueW'=m1 h!start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), sells(west,nono,m1), weapon(m1) trueN'=nono c!start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), sells(west,nono,m1), weapon(m1), hostile(nono) trueP'=west, W'=m1, N'=nono start, owns(nono,m1), missile(m1), enemy(nono,america), american(west), nation(nono), sells(west,nono,m1), weapon(m1), hostile(nono), criminal(west)
134
Backward Chaining as CHR Constraint Simplification 1.Put Horn KB in Implicative Normal Form (INF) 2.Map each set of deductive rule in INF(KB) of the form: { p 1 1 ... p i l c,..., p n 1 ... p n m c }, i.e., clauses with same positive conclusion onto a CHR rule of the form: c e 1,..., e n | (ng(p 1 1 ),..., ng(p i l )) ;...; (ng(p n 1 ),..., ng(p n l )) where ng(p k ) is a non-ground version of p k with each constant v k substituted by a variable X k and e 1,..., e n are equations of the form X k = v k 3.Map each integrity constraint in INF(KB) of the form: p 1 ... p i false onto a CHR rule of the form: ng(p 1 ),..., ng(p i ) | e 1,..., e n ==> false. 4.Map all the facts in INF(KB) of the form: true f l onto a single CHR rule of the form: f 1,..., f k true. 5.Map query Q of the form (q 11 ... q 1n ) ... (q m1 ... q mp ) onto CHR constraint of the form q: (q 11,..., q 1n ) ;...; (q m1,..., q mp ) 6.Initialize RDCS = q 7.Simplify RDCS with CHR engine 8.If at the end of the simplification: RDCS = and BICS {false}, then KB |= Q BICS = {false}, then KB | Q Otherwise, KB may or may not entail Q
135
Backward Chaining as CHR Constraint Simplification: Is West Criminal? c@ criminal(P) weapon(W), sells(P,N,W), hostile(N), nation(N), american(P). w@ weapon(W) missile(W) s@ sells(P,N,W) P = west, N = nono | owns(N,W), missile(W). h@ hostile(N) A = america | enemy(N,A) f@ owns(N,W), missile(W), enemy(N,A), american(P), nation(N) N = nono, W = m1, A = america, P = West | true RuleRDCSBICSMEG c?criminal(P)true
136
Backward Chaining as CHR Constraint Simplification: Is West Criminal? c@ criminal(P) weapon(W), sells(P,N,W), hostile(N), nation(N), american(P). w@ weapon(W) missile(W) s@ sells(P,N,W) P = west, N = nono | owns(N,W), missile(W). h@ hostile(N) A = america | enemy(N,A) f@ owns(N,W), missile(W), enemy(N,A), american(P), nation(N) N = nono, W = m1, A = america, P = West | true RuleRDCSBICSMEG c!criminal(P)true weapon(W), sells(P,N,W), hostile(N), nation(N), american(P)true
137
Backward Chaining as CHR Constraint Simplification: Is West Criminal? c@ criminal(P) weapon(W), sells(P,N,W), hostile(N), nation(N), american(P). w@ weapon(W) missile(W) s@ sells(P,N,W) P = west, N = nono | owns(N,W), missile(W). h@ hostile(N) A = america | enemy(N,A) f@ owns(N,W), missile(W), enemy(N,A), american(P), nation(N) N = nono, W = m1, A = america, P = West | true RuleRDCSBICSMEG c!criminal(P)true w?weapon(W), sells(P,N,W), hostile(N), nation(N), american(P)true
138
Backward Chaining as CHR Constraint Simplification: Is West Criminal? c@ criminal(P) weapon(W), sells(P,N,W), hostile(N), nation(N), american(P). w@ weapon(W) missile(W) s@ sells(P,N,W) P = west, N = nono | owns(N,W), missile(W). h@ hostile(N) A = america | enemy(N,A) f@ owns(N,W), missile(W), enemy(N,A), american(P), nation(N) N = nono, W = m1, A = america, P = West | true RuleRDCSBICSMEG c!criminal(P)true w!weapon(W), sells(P,N,W), hostile(N), nation(N), american(P)true sells(P,N,W), hostile(N), nation(N), american(P), missile(W)true
139
Backward Chaining as CHR Constraint Simplification: Is West Criminal? c@ criminal(P) weapon(W), sells(P,N,W), hostile(N), nation(N), american(P). w@ weapon(W) missile(W) s@ sells(P,N,W) P = west, N = nono | owns(N,W), missile(W). h@ hostile(N) A = america | enemy(N,A) f@ owns(N,W), missile(W), enemy(N,A), american(P), nation(N) N = nono, W = m1, A = america, P = West | true RuleRDCSBICSMEG c!criminal(P)true w!weapon(W), sells(P,N,W), hostile(N), nation(N), american(P)true s?sells(P,N,W), hostile(N), nation(N), american(P), missile(W)trueP=west, N=nono
140
Backward Chaining as CHR Constraint Simplification: Is West Criminal? c@ criminal(P) weapon(W), sells(P,N,W), hostile(N), nation(N), american(P). w@ weapon(W) missile(W) s@ sells(P,N,W) P = west, N = nono | owns(N,W), missile(W). h@ hostile(N) A = america | enemy(N,A) f@ owns(N,W), missile(W), enemy(N,A), american(P), nation(N) N = nono, W = m1, A = america, P = West | true RuleRDCSBICSMEG c!criminal(P)true w!weapon(W), sells(P,N,W), hostile(N), nation(N), american(P)true s!sells(P,N,W), hostile(N), nation(N), american(P), missile(W)trueP=west, N=nono hostile(N), nation(N), american(P), missile(W), owns(N,W), missile(W).P=west, N=nono
141
Backward Chaining as CHR Constraint Simplification: Is West Criminal? c@ criminal(P) weapon(W), sells(P,N,W), hostile(N), nation(N), american(P). w@ weapon(W) missile(W) s@ sells(P,N,W) P = west, N = nono | owns(N,W), missile(W). h@ hostile(N) A = america | enemy(N,A) f@ owns(N,W), missile(W), enemy(N,A), american(P), nation(N) N = nono, W = m1, A = america, P = West | true RuleRDCSBICSMEG c!criminal(P)true w!weapon(W), sells(P,N,W), hostile(N), nation(N), american(P)true s!sells(P,N,W), hostile(N), nation(N), american(P), missile(W)trueP=west, N=nono h?hostile(N), nation(N), american(P), missile(W), owns(N,W), missile(W).P=west, N=nonoA = america
142
Backward Chaining as CHR Constraint Simplification: Is West Criminal? c@ criminal(P) weapon(W), sells(P,N,W), hostile(N), nation(N), american(P). w@ weapon(W) missile(W) s@ sells(P,N,W) P = west, N = nono | owns(N,W), missile(W). h@ hostile(N) A = america | enemy(N,A) f@ owns(N,W), missile(W), enemy(N,A), american(P), nation(N) N = nono, W = m1, A = america, P = West | true RuleRDCSBICSMEG c!criminal(P)true w!weapon(W), sells(P,N,W), hostile(N), nation(N), american(P)true s!sells(P,N,W), hostile(N), nation(N), american(P), missile(W)trueP=west, N=nono h!hostile(N), nation(N), american(P), missile(W), owns(N,W), missile(W).P=west, N=nonoA = america nation(N), american(P), missile(W), owns(N,W), missile(W), enemy(N,A)P=west, N=nono, A = america
143
Backward Chaining as CHR Constraint Simplification: Is West Criminal? c@ criminal(P) weapon(W), sells(P,N,W), hostile(N), nation(N), american(P). w@ weapon(W) missile(W) s@ sells(P,N,W) P = west, N = nono | owns(N,W), missile(W). h@ hostile(N) A = america | enemy(N,A) f@ owns(N,W), missile(W), enemy(N,A), american(P), nation(N) N = nono, W = m1, A = america, P = West | true RuleRDCSBICSMEG c!criminal(P)true w!weapon(W), sells(P,N,W), hostile(N), nation(N), american(P)true s!sells(P,N,W), hostile(N), nation(N), american(P), missile(W)trueP=west, N=nono h!hostile(N), nation(N), american(P), missile(W), owns(N,W), missile(W). P=west, N=nonoA = america f?nation(N), american(P), missile(W), owns(N,W), missile(W), enemy(N,A) P=west, N=nono, A = america N'= N, W' = W, W' = W, N' = N, A' = A, P' = P, N' = N, N = nono, W = m1, A = america, P = west
144
Backward Chaining as CHR Constraint Simplification: Is West Criminal? c@ criminal(P) weapon(W), sells(P,N,W), hostile(N), nation(N), american(P). w@ weapon(W) missile(W) s@ sells(P,N,W) P = west, N = nono | owns(N,W), missile(W). h@ hostile(N) A = america | enemy(N,A) f@ owns(N,W), missile(W), enemy(N,A), american(P), nation(N) N = nono, W = m1, A = america, P = West | true RuleRDCSBICSMEG c!criminal(P)true w!weapon(W), sells(P,N,W), hostile(N), nation(N), american(P)true s!sells(P,N,W), hostile(N), nation(N), american(P), missile(W)trueP=west, N=nono h!hostile(N), nation(N), american(P), missile(W), owns(N,W), missile(W). P=west, N=nonoA = america f!nation(N), american(P), missile(W), owns(N,W), missile(W), enemy(N,A) P = west, N=nono, A = america N'= N, W' = W, W' = W, N' = N, A' = A, P' = P, N' = N, N = nono, W = m1, A = america, P = west P = west, N=nono, A = america, W = m1
145
Resolution-Based Refutation as CHR Constraint Simplification 1.Put KB in Implicative Normal Form (INF) 2.Map each deductive rule in INF(KB) of the form: p 1 ... p i c 1 ... c j onto a CHR rule of the form: p 1,..., p i ==> c 1 ;... ; c j 3.Map each integrity constraint in INF(KB) of the form: p 1 ... p i false onto a CHR rule of the form: p 1,..., p i ==> false 4.Map all the facts in INF(KB) of the form: true f l onto a single CHR rule of the form: facts f 1,..., f k 5.Map negation of query Q of the form q 11 ... q 1n onto a single CHR rule of the form: q 11,..., q 1n ==> false 6.Initialize RDCS = facts 7.Simplify RDCS with CHR engine 8.If at the end of the simplification: BICS = {false}, then KB |= Q Otherwise, KB | Q
146
CHR V : Practical Applications 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
147
CHR V vs. Production Systems CHR V : 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 Production Systems: Fact base only contains ground Datalog atoms Cumbersome modeling to implement goal-driven reasoning No disjunctions in RHS No built-in 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
148
CHR V vs. Rewriting Systems CHR V : Matching applied to atomic formula conjunctions Rule head is matched with constraint store sub-set, which requires head to be more general than sub-set Propagation rules provide further simplification opportunities Rewriting Systems: Unification of LHS is applied recursively down to sub-terms Rule LHS is unified with sub-term which can thus be more general than LHS All reasoning done through rewriting (no propagation rules) 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
149
CHR V vs. Prolog CFOL semantics of CHR V guardless, single head simplification rule, equivalent to CFOL semantics of pure Prolog clause set sharing same conclusion (Clark's completion) Simplification rule: sh true | b 1 1,..., b k p ;... ; b 1 1,..., b l q. w here: {X 1,..., X n } = vars(sh i ), and {Y 1,..., Y m } = vars(b 1 ... b k ) \ {X 1,..., X n } X 1,..., X n true (sh Y 1,..., Y m ((b 1 1 ... b k p ) ... (b 1 1 ... b k q )) Equivalent Prolog clauses: { sh :- b 1 1,..., b k p.,..., sh :- b 1 1,..., b l q.} Thus, using Clark's completion, any Prolog program can be reformulated into a semantically equivalent CHR V program CHR V extends Prolog with: Conjunctions in the heads Guards Non-ground numerical constraints heads, guards and bodies Propagation rules
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.