Quantified Constraint Handling Rules

by   Vincent Barichard, et al.
Université d'Angers

We shift the QCSP (Quantified Constraint Satisfaction Problems) framework to the QCHR (Quantified Constraint Handling Rules) framework by enabling dynamic binder and access to user-defined constraints. QCSP offers a natural framework to express PSPACE problems as finite two-players games. But to define a QCSP model, the binder must be formerly known and cannot be built dynamically even if the worst case won't occur. To overcome this issue, we define the new QCHR formalism that allows to build the binder dynamically during the solving. Our QCHR models exhibit state-of-the-art performances on static binder and outperforms previous QCSP approaches when the binder is dynamic.



There are no comments yet.


page 1

page 2

page 3

page 4


Tractability of Quantified Temporal Constraints To The Max

A temporal constraint language is a set of relations that are first-orde...

Constraint compiling into rules formalism constraint compiling into rules formalism for dynamic CSPs computing

In this paper we present a rule based formalism for filtering variables ...

Entropy-Guided Control Improvisation

High level declarative constraints provide a powerful (and popular) way ...

Beyond Q-Resolution and Prenex Form: A Proof System for Quantified Constraint Satisfaction

We consider the quantified constraint satisfaction problem (QCSP) which ...

A Complete Solver for Constraint Games

Game Theory studies situations in which multiple agents having conflicti...

Fractals2019: Combinatorial Optimisation with Dynamic Constraint Annealing

Fractals2019 started as a new experimental entry in the RoboCup Soccer 2...

Justifications in Constraint Handling Rules for Logical Retraction in Dynamic Algorithms

We present a straightforward source-to-source transformation that introd...
This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

1 Introduction

We shift the QCSP (for Quantified Constraint Satisfaction Problems) framework to the QCHR (for Quantified Constraint Handling Rule) framework. Such shift is motivated by the difficulties for developing real applications in QCSP.

QCSP [8, 27, 16, 5, 23, 21, 4] are a generalization of Constraint Satisfaction Problems (CSP) in which variables may be quantified existentially (as in CSP) and universally. A QCSP is an alternation of existentially and universally quantified variables over finite domains, the binder, followed by a CSP. Universally quantified variables represent uncontrollable parameters such as meteorological events. A QCSP may be seen as a two-players game in which the existentially quantified variables stand for a player and universally quantified variables stand for a player . QCSP+ [5] was proposed to make QCSP more practical from the modeling point of view. QCSP+ uses restricted quantification sequences instead of standard quantification sequences. A QCSP/QCSP+ is valid if player has a strategy to win i.e. a strategy for setting the existentially quantified variables such that no matter what setting the player chooses the CSP is true. QCSP/QCSP+ is a rich modeling framework which leads to succinct modeling. But this extension also increases the complexity of solving from NP-complete to PSPACE-complete. To fit a real problem in a QCSP/QCSP+, one has to model every part of the problem and everything must be a priori stated. There are problems (tic-tac-toe, reversi, connect-four) that fit this requirement, but others (checkers, chess) just do not. QCSP/QCSP+ cannot be used to model games and problems whose number of moves is not formerly known. In addition, even if everything can be statically stated, the QCSP model involves all possibles moves and overestimates the number of moves to the worst case. For example, it is difficult to encode games such that some rules constrain future moves depending on past moves [6]: the QCSP will look for solutions for any possible move of player while some of them have been made impossible by previous moves. When a QCSP/QCSP+ is defined, the solving relies on a QCSP solver [16, 4, 27]. A QCSP/QCSP+ solver is a black box program that solves a given model. As most QCSP/QCSP+ solvers are based on CSP solvers, there is no easy way to help the solving process by taking into account the specific properties of a quantified problem. In this work, we propose a new framework to model quantified problems in a dynamic way.

CHR (for Constraint Handling Rules) [10, 11, 15, 12, 13, 14] are a committed-choice language consisting of multiple-heads guarded rules that replace constraints by more simple constraints until they are solved. CHR are a special-purpose language concerned with defining declarative constraints in the sense of

Constraint logic programming

[18, 19, 20]. CHR are a language extension that allows to introduce user-defined constraints, i.e. first-order predicates, into a given host language as Prolog, Lisp, Java, or C/C++. CHR define simplification of user-defined constraints, which replaces constraints by more simple constraints while preserving logical equivalence. CHR define also propagation over user-defined constraints that adds new constraints; this constraints are logically redundant but may cause further simplifications. CHR allow to use guards that are sequences of host language statements. CHR finally define simpagation over user-defined constraints that mixes and subsumes simplification and propagation. CHR (simpagation) rules are applied on multi-sets of constraints. Repeated application of those rules on a multi-set of initial constraints incrementally solves these constraints. The committed-choice principle expresses a don’t care nondeterminism, which leads to efficient implementations. CHR have been extended to CHR [2] that introduces the don’t know nondeterminism in CHR [7]. This nondeterminism is freely offered when the host language is Prolog. This nondeterminism allows to specify easily problems from the NP complexity class but it is not the case for the rest of the Polynomial Hierarchy and, in general, any problem expressed with alternating quantifications (although the formalism is Turing-complete).

We propose in this paper to extend CHR with quantification. We call this new formalism QCHR (for Quantified Constraint Handling Rules). We propose to extend the simpagation rule to an existential simpagation rule but also to a universal simpagation rule. The existential (resp. universal) simpagation has the same conditions to be applied as a simpagation rule but the body is existentially (resp. universally) quantified on a variable over a (finite) domain. We obtain a formalism for which it is not necessary to declare a priori the alternation of quantifiers but where the quantifiers are generated when they are needed. This property offers an ease of programming compared to QCSP/QCSP+ where you always need to declare a finite binder. In QCHR, one can specify some potentially infinite games by recursion.

Section 2 presents intuitively the syntax of our QCHR formalism, illustrates on two emblematic examples why it is not always appropriate to model and solve with QCSP/QCSP+, and shows the ease of modeling and efficiency of solving of our new formalism. Section 3 presents our proposal, the QCHR language, with its proof-theoretical semantics. Section 4 presents a discussion about the link between QCHR and some other related works. Section 5 presents our implementation of the QCHR language into the C++ host language and some experiments. Section 6 concludes and draws some perspectives.

2 Motivating examples

Our main purpose is to be able to model quantified problems when the binder can be built dynamically during the solving. Two well known problems are used: the Nim game and the Connect-four as motivating examples. Informally, CHR formalism is extended with two new rules: the existential (simpagation QCHR) rule

and the universal (simpagation QCHR) rule

The head , the body  and the  of these rules are interpreted in the same way as in the CHR formalism: Constraints are kept like in propagation and constraints are deleted like in simplification; the constraints of the body are the added constraints; if , nothing is added; if , the computation fails. The two symbols  and  denotes, respectively, the lower bound and the upper bound of an integer interval. The variable is supposed to appear in the body . The informal semantics of those rules is as follows: The body  of the existential rule leads to a success (resp. failure) if at least one value (resp. all values) taken in the interval leads the body (where the occurrences of in  are replaced by ) to a success (resp. failure). In the same way, the body  of the universal rule leads to a success (resp. failure) if all values (resp. at least one value) taken in the interval leads the body to a success (resp. failure). If an existential (resp. universal) QCHR rule keeps all the constraints of its multiple-head, it is an existential (resp. universal) propagation rule and is denoted by () (resp. (); if an existential (resp. universal) QCHR rule keeps none of the constraints of its multiple-head, it is an existential (resp. universal) simplification rule and is denoted by () (resp. ().

The Nim game.

The Nim game is a two-players game played with a heap of coins or matches. The object of the game is to take the last match. Each player can take one to three matches. With the Fibonacci variant, the minimum number is one match and the maximum on the first play is one less than the initial number of matches. Then each player may take from one to twice as many as matches as the adversary at the preceding turn. Player begins. For example with 4 matches, player takes 1 match. Then player can take 3 matches but if he does he loses immediately since he cheated. Then player can take 1 or 2 matches. But whatever he takes, he loses since player will take the remaining matches. The longest possible party has 4 turns : each player takes one match at each turn. Then QCSP has 4 quantifiers since the binder is defined statically. With an even number of matches, the QCSP specification is as follows (, resp. , is the number of matches chosen by player , resp. , at turn ): is true (player chooses between 1 and matches) and for all , , and for all , , (each player takes from one to twice as many matches as the adversary at the preceding turn) [4]:

with and Boolean variables. For each initial number of matches, one has to instantiate the general scheme. For example, with , the following QCSP is obtained:

with and Boolean variables. This QCSP is valid if the first player has a strategy for setting the existentially quantified variables such that no matter what setting the adversary chooses for its universally quantified variables, this first player wins (i.e. the CSP is true).

The following QCHR program solves the Nim game with the Fibonacci variant of an even or odd number of matches (

represents the number of matches chosen by a player and represents the remaining number of matches into the heap):

These rules are built in the same way: they are existential/universal simplification rules with an omitted empty guard, the definition of the lower and upper bounds, and the body that expresses that if a player has chosen to take matches, his adversary may only choose between and matches. The first player may freely choose between and the initial number of matches minus one:

The binder is not defined statically as for QCSP but dynamically. Note that the base case of the recursion is hidden into the semantics of the universal rule when the lower bound becomes larger than the upper bound.

The Connect-four game.

The connect-four game is a two-players game in which the players first choose a color. It is played on a vertically suspended grid. At each turn, a player drops one colored coin from the top into a column of the grid. The coins fall straight down, occupying the lowest available slot within the column. The winner is the first player to form a horizontal, vertical, or diagonal line of four of one’s own coins.

The connect-four game can also be modeled with a QCSP. In order to model every game, a grid is built for each player turn. There are as many game turns as there are slots in the grid. As a result, the QCSP model involves grids linked with each others with constraints (see [22] more details about the QCSP model). Furthermore, constraints has to be added to detect full columns, invalid moves and winning grids. But, even if a QCSP can be used, the binder is dynamic (a player may win the game without completely filling the board) and the model is big and not very understandable. In comparison, the QCHR model is more suitable, lightweight and more readable. Let be the number of columns of the grid. The following QCHR model computes a winning strategy if such a strategy exists:

Where , and are built-in constraints: sets a coin to the given column and raises a failure if the column is full; returns if the column is full and otherwise; returns if an alignment of four coins is found and otherwise. These functions rely on a grid which is filled according to the players moves.

The binder is not defined statically as for QCSP but dynamically. As a result, the size of the binder is equal to the number of moves done during the game and not the worst possible case.

3 The QCHR language

A constraint is considered to be a first-order predicate. Only one kind of predefined (built-in) constraint is required: the syntactic equality constraint denoted by with an equality theory denoted  for variables and constants111We do not use functional symbols but the language is richer than simply variables and constants since one can use the statements of the host language that are evaluated before equality is applied on a completely instantiated expression. It is the case for arithmetic operation in the modelling of Nim game or for more ad hoc functions like and in the modelling of the Connect-Four game.. For two sequences of user-defined constraints means and for two constraints means , …, . If  is a set of constraints, [] denotes the set of equality constraints of .

In order to define the (proof-theoretical) semantics of QCHR, two reserved constraints are first defined.

Definition 3.1 ( and  constraints)

The  and  constraints are constituted (in this order) of an integer variable, two integers (the lower and upper bounds) and a sequence of user-defined constraints.

The  system is based on the following kind of sequents:

Definition 3.2 ( sequent)

An  sequent is a quadruple where , the down store, and , the up store, are two stores of constraints,  is a sequence of CHR rules and , the goal, is a sequence of constraints.

The intuitive meaning of a sequent is to try and consume the constraints  with the sequence of CHR rules  thanks to the store . The elements of the store  are the unconsumed constraints: the constraints of  that have not been consumed and those produced by the rules applied over  but not consumed during this production.

The  system is based on ten  inference rules. The following first five rules below are adapted from the sequent calculus system of [26] for CHR. This implies that a QCHR program may contain CHR rules. In the following, some constraints (, , ), some stores of constraints (, , , , , …) and some sequences of constraints (, ) are used.

The Apply inference rule:


  • () a rule of ;

  • , and there exists such that

    • either , ,

    • or , ;

  • ( the set of variables of and );

  •  is a sequence composed of all the elements of .

The Apply inference rule applies a QCHR rule on a constraint since there are two sub-stores and of the store such that modulo the equality constraints of and such that the sequence of host statements and equalities of the guard, , are verified. The solving of the constraint is reduced to the solving of the goal of the CHR rule and eventually the solving of the constraints of in the case that constraints from were not consumed during the process of consumption/production of . A part of the resources is allocated to solve the goal , the rest of the constraints and those produced by  but unconsumed, , are allocated to a sequence  over . Since the  system only applies a QCHR rule if one of the constraints of its head is focused on, the calculus of () is necessary to the completeness of  w.r.t. the semantics of CHR for non-quantified CHR programs (see Example 1 where ). But,  may be empty if all the resources have been consumed (see Example 2 where is empty). If the applied rule is a simplification rule (i.e. is empty) or  is empty then  is empty and the right above sequent is omitted (and ). The Apply inference rule realizes in fact a hidden use of the cut-rule of the linear-logic sequent calculus [17]: A lemma is computed by the left sub-proof and used in the right sub-proof222see [26] for a discussion about the linear-logic properties of this rule.

The Left-elimination-of-conjunction inference rule:

If the current goal is a sequence of constraints, the Left-elimination-of-conjunction inference rule is applied: The first constraint of the sequence is isolated and a part of the resources  are allocated to solve the constraint; the rest of the constraints and those produced by but unconsumed, , are allocated to the remaining sequence of constraints. This inference rule realizes also, in fact, a hidden use of the cut-rule of the linear-logic sequent calculus: The  is a lemma computed by the left sub-proof and used in the right sub-proof.

The Inactivate axiom:

with no QCHR rule () of  such that , (, or , ), , ; and ( the set of variables of and ).

If there is no QCHR rule to consume the current user-defined constraint by an apply rule, the Inactivate axiom stores the constraint into the store.

The  axiom:

If the current goal is the  constraint then no constraint is consumed and the  axiom is applied.

The Equality inference rule:

with a partition of the store such that is the set of identified user-defined constraints that contain either the variables or , a sequence over , and with the proviso that the equality constraint () is consistent with the equivalence classes [] according to .

If the current goal is only an equality constraint then no constraint is consumed and the Equality inference rule is applied: The equality constraint is added to the store of constraints and a sequence of constraints, over all the constraints of the store with occurrences of variables or , is inserted into the goal part of a sequent. Since equivalence classes for the variables are modified, some rules might be applied from now on.

Example 3.1
  1. [label=0,ref=3.1.0]

  2. Let  be the CHR program (), () and the goal then

  3. Let  be the CHR program (), () and the goal then

Below, the five rules that are specific to manage the quantifiers are described.

The -Apply inference rule:


  • () is a QCHR rule of ;

  • , and there exists such that

    • either , ,

    • or , ;

  • ( the set of variables of and ).

The -Apply inference rule applies, in the same conditions as an Apply rule, an existential QCHR rule but introduces only one constraint: an existential constraint since the elimination of a quantified constraint is part of the mechanism of the semantics.

The -Apply inference rule:


  • () is a QCHR rule of ;

  • , and there exists such that

    • either , ,

    • or , ;

  • ( the set of variables of and ).

In the same way, the -Apply inference rule applies, in the same conditions as an Apply rule, an universal QCHR rule but introduces only one constraint: an universal constraint.

The -elimination inference rule:

with , and .

If the goal is a unique existential constraint, then the -elimination inference rule is applied: A value into the interval is chosen and assigned to the variable of the constraints , …, (this is the meaning of ). If then, there is no possible sub-proof from this sequent. The resulting store  is ignored after the proof of the : production and consumption are local.

The -elimination inference rule:

with and , .

If the goal is a unique universal constraint, then the -elimination inference rule is applied and leads to two sub-proof trees: The lower bound of the interval , , is assigned to the variable of the constraints , …, and the sequent () has to be proved; and the lower bound is increased by 1 and the sequent () has also to be proved. The resulting stores and are ignored after the proofs of and : production and consumption are local.

The - axiom:


with .

Finally, if the goal is a unique universal constraint with , then the - axiom is applied since the logic formula is equivalent to .

Now, the  sequent calculus is defined:

Definition 3.3 ( sequent calculus system)

The  sequent calculus system is the given of the ten previous inference rules and axioms.

Restricted to the first four items (ie. Apply, Left-elimination-of-conjunction and Inactivate inference rules and  axiom), the system is equivalent to the proof-theoretical semantics of CHR of [26] lifted to first order thanks to the Equality inference rule.

Example 3.2

The QCHR program  that solves the Nim game, one of the motivating examples of Section 2, is recalled ( represents the number of matches chosen by a player and represents the remaining number of matches into the heap):

and a proof of () is given:


with :


and :


4 Discussion

All the state-of the-art QCSP solvers have the same drawback: they explore much larger combinatorial spaces than the natural search space of the original problem. In [4], the meaning of the “Achilles’heel” notion, initially introduced for Quantified Boolean Formulas (QBF) [9, 24, 29, 30] in [3] as the difficulty to detect that the Boolean constraints are necessarily true under some partial assignment, has been extended for QCSP to the larger problem of how to avoid the exploration of combinatorial spaces that are known to be useless by construction. This definition includes the capture of the illegal actions of the player but also for example the end of the game before the last turn that is also a source of oversized explored search space. Already cited, [5, 28] proposes the new QCSP+ language that use restricted quantification sequences instead of standard quantification sequences. Some other approaches propose to modify more or less the QCSP/QCSP+ language to overcome the different drawbacks: [6] proposes the new Strategic CSP language where universal variables adapt their domain to be compatible with previous choices; [23] proposes also a new language but restricted to Markovian Game-CSP, based explicitly on the notion of state, in order to efficiently model and solve control problems for completely observable and Markovian dynamic systems. Those language are easily and efficiently expressed in QCHR (see Subsection 5 ”The Nim Game” to a discussion about states in QCHR to improve efficiency).

5 Implementation and Experiments

To evaluate the QCHR approach, three well known problems333We used the matrix game, the nimfibo game and the connect-four game to carry our experiments. are modelled and solved in QCSP and QCHR. Then, the results obtained by two solvers are compared: QuaCode [4] for the solving of the QCSP models and our solver for the solving of the QCHR models. is a QCHR solver based on the CHR++ solver444CHR++ is a CHR solver built on the top of the C++ language, it can be downloaded at https://gitlab.com/vynce/chrpp. The latter has been extended by adding two new items to its grammar: exists and forall. Item exists allows you to browse the domain of a variable by successively trying each of its values in search of success. Item forall makes it possible to browse the domain of a variable by making sure that each of the values leads to a success. They implement the -elimination and -elimination rules previously defined.

Each experiment has been run

times and the average of the running times and number of failures are reported in the tables. Notice that there is no random parameter or value used for both solvers. As a consequence, for a given instance, the number of failures is always the same and the standard deviation of the running times is quite low. All experiments have been run on an Intel Xeon-E5, 2.1-3.3GHz, 128GB RAM running Linux. Maximum computation time is set to

seconds. All benchmarks used here are provided with CHR++ sources.

The matrix game

is a two-players game of turns. It is played on a square matrix of size . At each turn, player cuts the matrix in half horizontally and decides to keep the top or bottom part. Player then cuts the matrix in half vertically and keeps the left or right part. If the last cell contains a , player wins.

The matrix game is well suited to the QCSP approach because the binder does not take benefit from being defined dynamically. Indeed, the number of moves of a matrix game is fixed and only depends on the matrix size. Although it does not belong to our motivating examples, it is used as a use case to evaluate the efficiency of our approach on static binders. Let be the input matrix of size such that . The following QCHR program finds a winning strategy to the matrix game:

With and the coordinates of the upper left and lower right corners of the relevant part of the matrix. The relevant part is the remaining part after a player turn. Constraints and are built-in constraints that update the upper left and lower right corners of the relevant part of the matrix. The initial call is .

The matrix game has already been modeled as a QCSP and solved with a QCSP solver. Random matrices are used as input for the instances and computed the average execution time and number of failures of each solver. Results are reported in Table 1.

Depth QuaCode
Failures Time (s) Failures Time (s)
4 14 1 22 1
6 40 1 50 1
8 235 3.9 366 1
9 447 59.53 705 1
10 TL TL 1340 1
Table 1: Results for the Matrix game

Both models are quite equivalent and the solving takes no benefit from propagation of QCSP constraints. The number of failures encountered by QuaCode is smaller than that of . Indeed, the propagation done in QuaCode prevents the last branching steps of search. But the cost is huge compared to the price of propagating the previous choices during the whole search. In comparison, does not propagate the choices thanks to the constraints but its model is quite simple to test and each basic operation is constant in time. Indeed, it only has to update the corner variables and to check at the end if it is a or a . That is why is able to solve the instance of depth while QuaCode reaches the time limit. As a result, we show that the QCHR model combined with is lighter and can be more efficient than a QCSP model with QuaCode.

The Nim game

is our first motivating example which has been introduced in Section 2. It can be modeled in QCHR in a very intuitive way. But the QCHR model can do even more. Indeed, thanks to its dynamic way of modeling, it is possible to make a top-down evaluation with tabling [25]. A top-down evaluation with tabling consists in storing states of sub-trees already explored. If such a sub-tree is encountered another time, its result will be used instead of exploring it again.

Figure 1: Nim execution time

For the experiments, we compare three solving approaches: QuaCode for the QCSP/QCSP+ solving, for the QCHR solving and (Mem) for the solving of the QCHR model with tabling. We first observe on Figure 1 that QuaCode cannot solve instances of more than matches without exceeding the time limit (i.e. ). We notice that QuaCode and encountered the same number of failures. Indeed, they are both based on same model and there is no constraint to propagate. But, as seen on Figure 1, performs better. We now compare with with tabling and see on Figure 1 that all instances are solved in less than second. This huge improvement is explained by the tabling process. Indeed, a Nim game state555A Nim game state is given by the player number, the number of matches can be encountered many times during the search. Recording such states will prevent the algorithm to explore them again in the future and increases a lot the efficiency of the search. As the current state of a Nim game is Markovian (i.e. the current state does not depend on the whole history of events), similar results are achieved in [23].

The connect-four game

is our second motivating example. It has been presented at Section 2. The QCSP+ model for the connect-four is big and not very understandable compared to the QCHR model. The QCHR model involves a dynamic binder which is built during the solving.

Board size QuaCode
Failures Time (s) Failures Time (s)
 4 4 2123 1 28818 1
 4 5 26754 9.53 327561 1
 5 4 312580 105.75 5373028 2.17
 5 5 TL TL 120470758 75.83
Table 2: Connect-four

For the experiments, QuaCode for the QCSP solving and for the QCHR solving are compared. As shown Table 2, the number of failures encountered by QuaCode is smaller than that of . Indeed, as for the matrix game, the propagation done in QuaCode prevents the last branching steps of search. But the cost is huge compared to the benefit of avoiding a few branching steps. The results show that performs better than QuaCode. It is more than times faster and can even find solution before the time limit on the last instance. For larger boards, computing times become enormous, exceeding the maximum allowed time.

6 Conclusion

This paper proposed the new QCHR formalism which is an extension of CHR with quantification. QCHR allows to model dynamic binders. This overcomes one of the main drawbacks of the QCSP framework. We also presented some very intuitive QCHR models and we solved them with a QCHR solver, . The experiments showed that always outperforms the QCSP solver and sometimes by many orders of magnitude.

We believe that this new formalism gives an easier way to model constrained problems with quantifications and offers a new way when the binder cannot be statically deduced. In the future, we plan to tackle problems that cannot be dealt with QCSP such as problems where a static binder cannot be found.


  • [1]
  • [2] S. Abdennadher & H. Schütz (1998): CHR: A Flexible Query Language. In: Proceedings of the 3 International Conference on Flexible Query Answering Systems, pp. 1–14, doi:http://dx.doi.org/10.1007/BFb0055987.
  • [3] C. Ansotegui, C. Gomes & B. Selman (2005): Achilles’ Heel of QBF. In:

    Proceedings of the 20th National Conference on Artificial Intelligence (AAAI’05)

    , pp. 275–281.
  • [4] V. Barichard & I. Stéphan (2014): The cut tool for QCSP. In: Proceedings of the 26th IEEE International Conference on Tools with Artificial Intelligence (ICTAI’14), pp. 883–890, doi:http://dx.doi.org/10.1109/ICTAI.2014.135.
  • [5] M. Benedetti, A. Lallouet & J. Vautard (2007): QCSP made Practical by virtue of Restricted Quantification. In: Proceedings of the 20th International Joint Conference on Artificial Intelligence (IJCAI’07), pp. 38–43.
  • [6] C. Bessiere & G. Verger (2006): Strategic constraint satisfaction problems. In: