ArticleslgStudy

science

Constraint Handling Rules

Constraint Handling Rules is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Constraint Handling Rules rather than just read about it. In short: Constraint Handling Rules (CHR) is a declarative, rule-based programming language, introduced in 1991 by Thom Frühwirth at the time with European Computer-Industry Research Centre (ECRC) in Munich, Germany. Originally intended for constraint programming, CHR finds applications in grammar induction, type systems, abductive reasoning, multi-agent systems, natural language processing, compilation, scheduling, spatial-t…

Key takeaways

  • Constraint Handling Rules belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Constraint Handling Rules to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Constraint Handling Rules from memory before moving on to harder problems.

Reference excerpt

Constraint Handling Rules (CHR) is a declarative, rule-based programming language, introduced in 1991 by Thom Frühwirth at the time with European Computer-Industry Research Centre (ECRC) in Munich, Germany. Originally intended for constraint programming, CHR finds applications in grammar induction, type systems, abductive reasoning, multi-agent systems, natural language processing, compilation, scheduling, spatial-temporal reasoning, testing, and verification. A CHR program, sometimes called a constraint handler, is a set of rules that maintain a constraint store, a multi-set of logical formulas. Execution of rules may add or remove formulas from the store, thus changing the state of the program. The order in which rules "fire" on a given constraint store is non-deterministic, according to its abstract semantics and deterministic (top-down rule application), according to its refined semantics. Although CHR is Turing complete, it is not commonly used as a programming language in its own right. Rather, it is used to extend a host language with constraints. Prolog is by far the most popular host language and CHR is included in several Prolog implementations, including SICStus and SWI-Prolog, although CHR implementations also exist for Haskell, Java, C, SQL, and JavaScript. In contrast to Prolog, CHR rules are multi-headed and are executed in a committed-choice manner using a forward chaining algorithm.

Language overview The concrete syntax of CHR programs depends on the host language, and in fact programs embed statements in the host language that are executed to handle some rules. The host language supplies a data structure for representing terms, including logical variables. Terms represent constraints, which can be thought of as "facts" about the program's problem domain. Traditionally, Prolog is used as the host language, so its data structures and variables are used. The rest of this section uses a neutral, mathematical notation that is common in the CHR literature. A CHR program, then, consists of rules that manipulate a multi-set of these terms, called the constraint store. Rules come in three types:

Simplification rules have the form h1,…,hn⟺g1,…,gm|b1,…,bo. When they match the heads h1,…,hn and the guards g1,…,gm hold, simplification rules may rewrite the heads into the body b1,…,bo. Propagation rules have the form h1,…,hn⟹g1,…,gm|b1,…,bo. These rules add the constraints in the body to the store, without removing the heads. Simpagation rules combine simplification and propagation. They are written h1,…,hℓ∖hℓ+1,…,hn⟺g1,…,gm|b1,…,bo. For a simpagation rule to fire, the constraint store must match all the rules in the head and the guards must hold true. The ℓ constraints before the ∖ are kept, as a in a propagation rule; the remaining n−ℓ constraints are removed. Since simpagation rules subsume simplification and propagation, all CHR rules follow the format

Hk∖Hr⟺G|B where each of Hk,Hr,G,B is a conjunction of constraints: Hk,Hr and B contain CHR constraints, while the guards G are built-in. Only one of Hk,Hr needs to be non-empty. The host language must also define built-in constraints over terms. The guards in rules are built-in constraints, so they effectively execute host language code. The built-in constraint theory must include at least true (the constraint that always holds), fail (the constraint that never holds, and is used to signal failure) and equality of terms, i.e., unification. When the host language does not support these features, they must be implemented along with CHR. Execution of a CHR program starts with an initial constraint store. The program then proceeds by matching rules against the store and applying them, until either no more rules match (success) or the fail constraint is derived. In the former case, the constraint store can be read off by a host language program to look for facts of interest. Matching is defined as "one-way unification": it binds variables only on one side of the equation. Pattern matching can be easily implemented when as unification when the host language supports it.

Example program The following CHR program, in Prolog syntax, contains four rules that implement a solver for a less-or-equal constraint. The rules are labeled for convenience (labels are optional in CHR).

The rules can be read in two ways. In the declarative reading, three of the rules specify the axioms of a partial ordering:

Reflexivity: X ≤ X Antisymmetry: if X ≤ Y and Y ≤ X, then X = Y Transitivity: if X ≤ Y and Y ≤ Z, then X ≤ Z All three rules are implicitly universally quantified (upper-cased identifiers are variables in Prolog syntax). The idempotence rule is a tautology from the logical viewpoint, but has a purpose in the second reading of the program. The second way to read the above is as a computer program for maintaining a constraint store, a collection of facts (constraints) about objects. The constraint store is not part of this program, but must be supplied separately. The rules express the following rules of computation:

Reflexivity is a simplification rule: it expresses that, if a fact of the form X ≤ X is found in the store, it may be removed. Antisymmetry is also a simplification rule, but with two heads. If two facts of the form X ≤ Y and Y ≤ X can be found in the store (with matching X and Y), then they can be replaced with the single fact X = Y. Such an equality constraint is considered built in, and implemented as a unification that is typically handled by the underlying Prolog system. Transitivity is a propagation rule. Unlike simplification, it does not remove anything from the constraint store; instead, when facts of the form X ≤ Y and Y ≤ Z (with the same value for Y) are in the store, a third fact X ≤ Z may be added. Idempotence, finally, is a simpagation rule, a combined simplification and propagation. When it finds duplicate facts, it removes them from the store. Duplicates may occur because constraint stores are multi-sets of facts. Given the query

A leq B, B leq C, C leq A

the following transformations may occur:

The transitivity rule adds A leq C. Then, by applying the antisymmetry rule, A leq C and C leq A are removed and replaced by A = C. Now the antisymmetry rule becomes applicable on the first two constraints of the original query. Now all CHR constraints are eliminated, so no further rules can be applied, and the answer A = B, A = C is returned: CHR has correctly inferred that all three variables must refer to the same object.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Constraint Handling Rules

Start with the simplest possible case. Write down what Constraint Handling Rules claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Constraint Handling Rules before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Constraint Handling Rules ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Constraint Handling Rules

In research
Constraint Handling Rules appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Constraint Handling Rules in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Constraint Handling Rules is common in secondary-school and first-year university syllabi. It links to neighbouring topics Concurrent programming languages, Constraint logic programming, Constraint programming languages, so understanding it makes those chapters shorter.
In everyday life
Look for Constraint Handling Rules outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Constraint Handling Rules” →

How to study Constraint Handling Rules in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Constraint Handling Rules means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Constraint Handling Rules out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Constraint Handling Rules in simple terms?

Constraint Handling Rules (CHR) is a declarative, rule-based programming language, introduced in 1991 by Thom Frühwirth at the time with European Computer-Industry Research Centre (ECRC) in Munich, Germany. Originally intended for constraint programming, CHR finds applications in grammar induction…

Why does Constraint Handling Rules matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Constraint Handling Rules?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Constraint Handling Rules.

Tags

  • Concurrent programming languages
  • Constraint logic programming
  • Constraint programming languages
  • Declarative programming languages

Keep exploring