Page 1

A Comparison of Constraint Handling Rules with

Equivalent Transformation Rules

Yoshinori Shigeta

SoC Research and Development Center

Toshiba Corporation

Kawasaki, Japan

Kiyoshi Akama

Information Initiative Center

Hokkaido University

Sapporo, Japan

Pongtawat Chippimolchai

Computer Science and Information

Management Program

Asian Institute of Technology

Pathumthani, Thailand

Takahiko Ishikawa

Information Initiative Center

Hokkaido University

Sapporo, Japan

Abstract

We have proposed a novel computation framework

called Equivalent Transformation (ET). The ET frame-

work is a rewriting system in which computation is exe-

cuted by rewriting rules. Rewriting rules of the ET frame-

work are called Equivalent Transformation Rules (ETRs).

The theory of ET guarantees the correctness of the com-

putation by ETRs. Multi-head ETRs which have multiple

atoms in their head part are valid ETRs.

Constraint Handling Rules (CHR) are declarative lan-

guage extension designed for writing user-defined con-

straints. CHR allows to use multi-head guarded rules for

its computation.

This paper compares the rewriting rules of ET and CHR,

especially multi-head rules. We show that all CHR rules can

be simply transformed into correct ETRs preserving their

logical meanings. Examples of the correct ETRs which can

not be represented as CHR rules are also given. Therefore

CHR rules can be regarded as proper subclass of ETRs.

Keywords: CHR, Equivalent Transformation, ETRs.

1. Introduction

Many computation models have been proposed, e.g.

Logic Programming (LP)[5], Constraint Logic Program-

ming (CLP)[6], Functional Programming (FP)[7], Func-

tional Logic Programming (FLP)[8] and Equivalent Trans-

formation (ET)[1, 2].

It is considered that almost of these computation mod-

els are in a same class of functions based on the theory of

computable functions. Although they are equivalent to each

other in view of computable functions, they have their own

theoretical basis of declarative semantics. We have been re-

searching the differences between their declarative seman-

tics and have compared the elements of the computation

models with those of the ET framework[3, 4].

The ET framework is a rewriting system in which com-

putation is executed by rewriting rules. Rewriting rules of

the ET framework are called Equivalent Transformation

Rules (ETRs). The theory of ET guarantees the correct-

ness of computation by ETRs. Multi-head ETRs which have

multiple atoms in their head part are valid ETRs.

Constraint Handling Rules (CHR)[9] is a high-level lan-

guage designed to write user-defined constraint solvers.

CHR is a committed-choice language consisting of multi-

head guarded rules. In the rest of this paper, we use the

terms CHR and CHRs for the CHR framework and for the

rewriting rules of the CHR framework respectively.

In this paper we compare multi-head rewriting rules of

CHR with those of ET in term of their syntax, seman-

tics, correctness and expressive power. It is also shown that

CHRs can be regarded as a proper subset of ETRs.

Section 2 briefly reviews the syntax and semantics of

CHRs. Section 3 reviews the ET framework showing the

syntax of ETRs and how they rewrite clauses. Section 4

discusses the semantics and correctness of ETRs. Section

5 compares the Logic Programming and CHR with the

ET framework. Section 6 compares CHRs with ETRs. A

method to correctly transform CHRs into equivalent ETRs

is given. It is shown that CHRs are ETRs and some ETRs

are not CHRs. Section 7 concludes this paper.

Page 2

2. Constraint Handling Rules (CHR)

2.1. Syntax of CHRs

There are three kinds of CHRs. A simplification rule, a

propagation rule and a simpagation rule. These CHRs are

of the form

Simplification:

X1,...,Xi<=> Z1,...,Zj | Y1,...,Yk

Propagation:

X1,...,Xi==> Z1,...,Zj | Y1,...,Yk

Simpagation:

X1,...,Xl\ Xl+1,...,Xi<=> Z1,...,Zj | Y1,...,Yk

where i > 0,j ≥ 0, k ≥ 0, 0 < l < i.

The multi-head X1,...,Xiis a non-empty sequence of CHR

(user-defined)constraints,theguardZ1,...,Zjisasequence

of built-in (pre-defined) constraints and the body Y1,...,Yk

is a sequence of built-in and CHR constraints.

A propagation rule is an abbreviation of a simplification

rule :

X1,...,Xi<=> Z1,...,Zj | X1,...,Xi,Y1,...,Yk

A simpagation rule is a combination of a simplification

and a propagation rule. A simpagation rule is equivalent to

a simplification rule :

X1,...,Xl,Xl+1,...,Xi

<=> Z1,...,Zj | X1,...,Xl,Y1,...,Yk

2.2. Applying CHRs

A CHR rule is applicable if its guard(Z1,...,Zj) is satis-

fied. A simplification rule replaces constraints (X1,...,Xi)

with simpler constraints (Y1,...,Yk) which are logically

equivalent (preserving logical meaning), while a propaga-

tion rule introduces new constraints (Y1,...,Yk) that are

logically redundant.

2.3. Correctness of CHRs

Define Var(S) to be a set of all variables appearing in the

atoms in a set of atoms S. Let x be a set of variables in the

head X1,...,Xi, z a set of variables in the guard Z1,...,Zj

that does not occurs in x, and y a set of variables in the body

Y1,...,Ykwhich occurs in neither x nor z. That is,

x=Var({X1,...,Xi})

z=Var({Z1,...,Zj}) - x

y=Var({Y1,...,Yk}) - x - z

A simplification rules is correct iff its head and its body

are equivalent when the guard is true. More precisely,

∀x∀z(Z1∧...∧Zj→(X1∧...∧Xi↔∃y(Y1∧...∧Yk))).

3. Equivalent Transformation Framework

3.1. Problem-Solving in ET Framework

In the Equivalent Transformation framework (ET frame-

work), a problem or a program specification is formulated

as a declarative description[1](or just a description when it

is unambiguous). A declarative description is a set of defi-

nite clauses. A declarative description consists of two parts:

a definition part and a query part. A definition part provides

general and common knowledge about the problem domain.

A query part contains the queries to the definition part.

From the definition part, a set of ETRs for transform-

ing a declarative descriptions is prepared. This set of ETRs

is called an ET program. The problem is then solved by

transforming the query part successively, using the prepared

ETRs, into another description. The answer of the problem

can be obtained from the transformed query part.

An ETR is correct if its application always results in a

correct computation step, that is, if a description D ∪ Q0is

transformed by an ETR into D ∪ Q1, the logical meaning of

D ∪ Q0and D ∪ Q1must be equivalent. If an ET program

consists of only correct ETRs, its correctness is guaranteed.

A computation in the ET framework is done by succes-

sively rewriting a query part of a description, using ETRs

prepared from a definition part, into a simpler but equiva-

lent form in which answer to the query can be readily ob-

tained.

A process to solve a problem in the ET framework (Fig-

ure 1) is as follows:

1. A problem is given.

2. A declarative description (a set of definite clauses) is

formulated to represent the problem.

3. A ET program (a set of ETRs) are generated automati-

cally and/or manually from the declarative description.

4. A query part is transformed equivalently and repeat-

edly using the ET program.

5. The answer is obtained from the query part which is

transformed.

3.2. Syntax of ETRs

An Equivalent Transformation Rule (ETR) is of the

form:

Hs ?Gs?

→?Es1? Bs1;

...

→?Esn? Bsn.

where n ≥0.

Page 3

Description?

D + Q? 0?

Program?

(ETRs)?

Description?

D + Q? 1?

Description?

D + Q? n?

Problem?

Formulation?

Generation?

Equivarent Transformation?

Equivarent Transformation?

Equivarent Transformation?

Rewriting?

Rewriting?

Answer?

Extraction from Qn?

Figure 1. Problem-solving in ET framework

A set of atoms Hs and Gs are called a head part and a condi-

tion part of a ETR, respectively. A set of deterministic pro-

cedures Esiand atoms Bsi(where 0 ≤i ≤n) are called an

execution part and a replacement part, respectively. A pair

of an execution part and a replacement part is called a body

part. Gs and Esican be omitted if they are unnecessary.

If Hs is a singleton set (contains exactly one element)

the ETR is called a single-head ETR, otherwise it is called

a multi-head ETR.

An ETR may have zero or more body parts. A ETR with

more than one body parts (n ≥ 2) is called a multi-body

ETR. A multi-body ETR is one of the most important dif-

ferences between ETRs and CHRs.

Example 1. (single-head ETRs)

Single-head ETRs for finding a greatest common divisor

(GCD) of two numbers using the Euclidean algorithm are

as follows:

(r1) gcd(A,0,X) →?X=A?.

(r2) gcg(A,B,X) ?B>0? →?C := A mod B? gcd(B,C,X).

These two ETRs clearly describe the algorithm. The rule r1

means that a GCD of any number A and 0 is the number it-

self (A). The rule r2says that a GCD of two numbers A and

B, when B is positive, equals to a GCD of B and a remain-

der C of A divided by B.

Example 2. (multi-head ETR)

A multi-head ETR for the rule of anti-symmetry is as fol-

lows:

lte(X,Y), lte(Y,X) →eq(X,Y).

where lte(X,Y) means that X is less than or equal to Y,

eq(X,Y) means that X equals to Y.

Example 3. (multi-body ETR)

Thefollowing multi-body ETR appends twolist X and Y.The

result will be in Z. [H | T] denotes a non-empty list with a

head element H and a tail list T. This ETR has two body

parts.

append(X,Y,Z)

→?X=[], Y=Z?;

→?Z=[H | T], X=[H | X1]? append(X1,Y,T).

3.3. Applying ETRs

An ETR is said to be applicable to a clause C at a set of

body atoms Bs if its head part is matched (can be unified)

with Bs and all conditions in the condition part of the ETR

is evaluated to true. When a ETR is applied, it rewrites the

clause C into a (possibly empty) set of clauses Cs. For each

body of the ETR, the deterministic procedures in the execu-

tion part are executed first. If they succeed then a new clause

is created from C by replacing Bs with the replacement part

of the body. If the execution part fails, no new clause is cre-

ated from the body. In case of a multi-body ETR, multiple

new clauses are created.

Formally, let R be an ETR of the following form (shown

in Section3.2):

(R) Hs ?Gs?

→?Es1? Bs1;

...

→?Esn? Bsn.

Let C be a clause as follows:

(C) A ←B1 ∪ B2

Let S be a set of all possible substitutions, the ETR R is

applicable to C at B1if there exists a substitution θ∈S such

that Hsθ= B1and all conditions in Gsθare evaluated to true

with a substitution δ∈S . The result of applying R to C is a

set of clauses consisting of the following clauses, for each

i-th body of R,

Aσ←Bsiθδσ ∪ B2σ

3.4. Computation by ETRs

A computation in the ET framework is done by succes-

sively rewriting a query part of a declarative description

which corresponds to a specification of a problem, using

ETRs prepared from a definition part, into a simpler but

equivalent form in which the answer to the query can be

readily obtained.

Page 4

Example 4. Refer to the ETR r1and r2for finding GCD in

the previous section. Let C1be a clause:

(C1)ans(X) ←gcd(18,15,X).

r1is not applicable to C1while r2is applicable and results

in the following clause C2:

(C2)ans(X) ←gcd(15,3,X).

C2is rewritten again by r2into C3:

(C3) ans(X) ←gcd(3,0,X).

r1is applicable to C3while r2is not (the condition part is

false). Applying r1results C4which is the answer:

(C4) ans(3) ←.

Example 5. Consider a problem to find A, B and C which

satisfy C ≤A, A ≤B and B ≤C. This problem can be formu-

lated as a description with a definition part D and a query

part Q as follows:

D = {m ≤n ← | m ∈N , n ∈N , m ≤n} ∪

{m = m ← | m ∈N }

where N is a set of natural numbers.

Q = {ans(A,B,C)←C≤A, A≤B, B ≤C}

In order to solve this problem, the following three rules

describing reflexivity, anti-symmetry and transitivity, re-

spectively, are introduced.

(r5) reflexivity

X≤X→.

(r6) anti-symmetry

X≤Y,Y≤X →X=Y.

(r7) transitivity

X≤Y,Y≤Z →X≤Y,Y≤Z,X≤Z.

The reflexivity rule means that an atom X ≤X can be

eliminated from any clause without changing its semantic

meaning. The anti-symmetry rule is a multi-head rule which

means a set of the atoms {X ≤Y, Y ≤X} can be simplified

into an equality of X and Y. The transitivity rule is also a

multi-head rule. It means that a set of the atoms {X ≤Y, Y

≤Z} causes an introduction of a new atom X ≤Z.

In addition, the following rules are generated from the

description part D:

(r8) X≤Y →?X≤Y?.

(r9) X=Y →?X=Y?.

The query part is transformed by the above ETRs as fol-

lows:

ans(A,B,C)←C≤A,A≤B,B≤C.

ans(A,B,C)←C≤A,A≤B, C≤B,B≤C. by(r7)

ans(A,B,C)←B≤A,A≤B,C=B.

ans(A,B,B)←B≤A,A≤B.

ans(A,B,B)←B=A.

ans(A,A,A)←.

by(r6)

by(r9)

by(r6)

by(r9)

This result means that only A = B = C satisfies C ≤A, A ≤B

and B ≤C.

4. Program and Specification

4.1. Problem-Solving in Logic Programming and

CHR

In the logic programming[5], a program specification

and a program itself are not distinguished. A program spec-

ification is written using logical formulas which are also

used as a program. Thus writing a program specification

and writing a program is done simultaneously and become

the same task. For example a CHR specification written us-

ing logical formulas also become a CHR program.

Althoughregardingaprogramspecificationasaprogram

may look promising, it has many disadvantages. For ex-

ample, multiple programs cannot be written to solve the

same problem specified by a program specification. Mak-

ing change to a program means making change to a pro-

gram specification (Figure 2).

Moreover, its expressive power can be limited because a

program is limited to contain only logical formulas. For ex-

ample, cut or variable which are extra-logical cannot be

used in a program. Otherwise correctness of a program is

hard to be guaranteed.

In the ET framework, it is possible (and desirable) to im-

prove a program without changing its program specifica-

tion, because a program and its specification are clearly sep-

arated. A program can be improved by simply adding more

efficient but correct ETRs to the program (Figure 3).

4.2. Program and Specification

The concept of a program specification should be defi-

nitely separated from that of a program, because a program

specification is associated with declarative semantics, while

a program is associated with operational semantics. A pro-

gram specification should provide an enough detailed de-

scription of a problem from which a program can be con-

structed.

Separating programs and program specifications has two

significant advantages as well as multiple programs for sin-

gle program specification. One is that extra-logical proce-

dures which make programs efficient can be used in pro-

grams. Correctness of programs is verified by their program

specifications, even if extra-logical procedures are used in

the programs. Another is that various control strategies of

program execution can be employed because they are in-

dependent from correctness of programs. Proper control

strategies can improve execution of programs.

Page 5

Formulation?

Specification 0?

Problem?

Program 0?

Program 1?

Program N?

Specification 1?

Specification N?

Improvement?

Improvement?

Modification?

Identical to?

Modification?

Improvement?Modification?

Figure 2. Program Improvement in Logic Pro-

gramming

5. Correctness of ETRs

5.1. Meaning of Declarative Descriptions

Let G be a set of all ground atoms. Let head(C) denotes

the head atoms and body(C) denotes a set of body atoms

of a clause C. Let Gclause(P) denote a set of all ground in-

stances of the clauses in a declarative description P. Define

a mapping TP: 2G→ 2Gfor any set x of ground atoms as

follows:

TP(x)

def

= {head(C) | body(C)⊆ x, C ∈Gclause(P)}.

The meaning of a declarative description P, denoted by

M(P), is defined as follows:

M(P)

where ∅

[TP]n(∅) = TP([TP]n−1(∅)) for each n > 1.

Informally speaking, let S be a set of all possible substi-

tutions, the meaning M(P) of a description P can be com-

puted by:

def

=

?∞

is the empty set, [TP]1(∅)

n=1[TP]n(∅).

=

TP(∅) and

• For a unit clause(A←) in P and a substitution θ∈S ,

Aθ∈M(P) if Aθ is ground.

• For a non-unit clause(A←B) in P and a substitution

θ∈S, Aθ∈M(P) if Aθ is ground and Bθ⊆M(P).

5.2. Correctness of ETRs

The ET framework enables a development of a solid the-

oretical basis for determining the correctness of rewriting

Specification?

(Description)?

Problem?

Program 0?

(ETRs 0)?

Program 1?

(ETRs 1)?

Program N?

(ETRs N)?

Formulation?

Improvement?

Improvement?

Generation?

Improvement?

Figure 3. Program Improvement in ET frame-

work

rules of various kinds. As long as correct rewriting rules are

used throughout a transformation process, a correct compu-

tation is always obtained.

A rewriting rule is correct, if and only if its application

always results in a correct transformation step. Let D and Q

∪ {C} be a definition part and a query part of a declarative

description P. A rewriting rule R which rewrites C into a set

of clauses {C1,..., Cn} is correct iff M(D ∪ Q ∪ {C}) =

M(D ∪ Q ∪ {C1,..., Cn}).

A correct rewriting rule will be referred to as an ETR. If

all rules employed in computation are ETRs, it can be eas-

ily seen that the answers obtained by the ETRs are guaran-

teed to be correct.

6. Comparison of CHRs with ETRs

6.1. Translating CHRs into ETRs

The three kinds of CHRs can be translated into three

ETRs using the following method.

Let CHR1, CHR2and CHR3be a simplification CHR, a

propagation CHR and a simpagation CHR which are of the

forms:

Simplification:

(CHR1) X1,...,Xi<=> Z1,...,Zj| Y1,...,Yk

Propagation:

(CHR2) X1,...,Xi==> Z1,...,Zj| Y1,...,Yk

Simpagation:

(CHR3) X1,...,Xl\ Xl+1,...,Xi

<=>Z1,...,Zj| Y1,...,Yk