☰

*ON THIS PAGE*# Constraint programming over finite domains (new)

**Author(s):** Emilio Jesús Gallego Arias, Rémy Haemmerlé, Jose F. Morales.## Completeness Considerations

## Meta-Constraints

## Example

## Usage and interface

- The Ciao System »

- PART IV - Language extensions »

**Constraint programming over finite domains (new)**▾

This package extends Ciao with constraints over finite domains (FD). The solver is an instance of the Constraint Logic Programming (CLP) scheme as introduced by Jaffar and Lassez [JL87]. It uses classical propagation techniques as described in Van Hentenryck's book [Van89] and Diaz's clp(FD) implementation [CD96].

The package provides predicates for checking consistency of FD constraints. A FD is a small subset of integers, and FD constraints are relations over integer. Hence only integer or variables are allowed in such constraints. FD variables (i.e., variables that occur in an FD constraint) get associated with a domain either explicitly declared by the program or implicitly imposed by the solver. As soon as variables get an empty domain the computation fails, hence forcing backtracking.

The package defines basic operators, automatically imports basic constraints and enumerating predicates form the module `clpfd_rt`, and provides high-level Meta-Constraints through a transparent compilation process.

For efficiency reason, the solver is not complete on non-ground constraints, in the sense that it may not be able to determine that a set of constraints is actually satisfiable. In such cases, the system silently succeeds. To ensure full completeness, the programmer may use the `labeling/2` predicate that uses an automatic backtracking search to find ground solutions for a list of FD variables. Labeling is complete, always terminates, and yields no redundant solutions. See an example of use of labeling in the following Example.

On success, the top-level will display the domain associated with each FD variable remaining free in the query. This domain should not be understood as values permitted for the corresponding variable, but only as values not excluded by the incomplete propagation mechanism of the solver. Note that the answer output by the top-level is by itself incomplete as the remaining constraints are not showed.

There are five meta-constraints, namely `#=/2` (constraint equal), `#\=/2` (constraint not equal), `#</2` (constraint less than), `#=</2` (constraint less or equal), `#>/2` (constraint greater than), and `#>=/2` (constraint greater or equal). These meta-constraints are defined over arithmetic expressions with FD variables (see regular type `fd_expr/1` in module `clpfd_rt`.). Such constraints are "meta" in the sens that their arguments are interpreted at compile-time and all variables occurring free in the arguments will be implicitly constrained to take integer values only. In particular, note that variables constrained in such a way would not be unifiable with complex FD expressions. For instance, the call:

X + Y #> Z.

is not equivalent to the call:

A = X + Y, A #> Z.

While the first call succeeds, the second one will throw an exception to indicate that A cannot be unified with the non-integer term `X + Y`. It is possible to view meta-constraints as a convenient way to define an infinite number of FD constraints. For instance `A #> Z` and `X + Y #> Z` can be considered respectively as binary and ternary constraints over FD variables.

It is possible to delay interpretation of meta-constraints at call time by explicitly prefixing the call with `clpfd_rt`. For instance, the following call will not throw any exception:

A = X + Y, clfd_rt:(A #> Z).

The problem is to put N queens on an NxN chessboard so that there is no pair of queens threatening each other. Each variable is a queen. Each queen has a designated row. The problem is to find a different column for each one.

The main constraint of the problem is that no queen threaten another. This is encoded by the `diff/3` predicate and should hold for any pair of queens.

The main call is `queens(N, L, Lab)` which looks for a solution `L` for the `N` queens problem using labeling `Lab`. Observe the call to `labeling/2` at the end of definition of `queens/3`, which tries to find a solution for the problem.

**Library usage:**`:- use_package(clpfd).`or`:- module(...,...,[clpfd]).`**New operators defined:**`in/2`[700,xfx],`#=/2`[700,xfx],`#\=/2`[700,xfx],`#</2`[700,xfx],`#=</2`[700,xfx],`#>/2`[700,xfx],`#>=/2`[700,xfx],`../2`[550,xfx].**Implicit imports:***System library modules:*`clpfd_rt`.*Packages:*`prelude`,`initial`,`condcomp`,`assertions`,`assertions/assertions_basic`,`regtypes`.

Generated with LPdoc using Ciao