# Finite domain solver runtime

**Author(s):**Emilio Jesús Gallego Arias, Rémy Haemmerlé.

This module provides Finite Domain (FD) constraints and enumerating predicates for FD variables. See `clpfd_doc` for more details about the Ciao FD solver.

This module provides two kinds of contraints: *basic constraints* (such as `domain/3`, `all_different/1`, ...) that deal with FD variables or lists of FD variables, and *meta-contraints* (such as `#=/2`, `#=</2`, ...) that deal with arithmetic expressions over FD variables, called in the following FD expressions. Meta-constraints of this module behave as describes in package `clpfd`, except that their arguments are interpreted at run time.

## Usage and interface

**Library usage:**`:- use_module(library(clpfd_rt)).`**Exports:***Predicates:*`in/2`,`#=/2`,`#\=/2`,`#</2`,`#=</2`,`#>/2`,`#>=/2`,`domain/3`,`in/2`,`all_different/1`,`labeling/2`,`indomain/1`,`label/1`,`labeling/2`,`wrapper/2`.*Regular Types:*`fdvar/1`,`fd_range_expr/1`,`fd_expr/1`.*Multifiles:*`attr_rt:unify_hook/3`,`attr_rt:attribute_goals/4`.

**Imports:***System library modules:*`lists`,`clpfd/clpfd_debug_rt`,`sort`,`clpfd/fd_term`,`clpfd/fd_constraints`,`clpfd/fd_labeling`,`clpfd/fd_optim`,`attr/attr_rt`.*Packages:*`prelude`,`nonpure`,`assertions`,`regtypes`,`fsyntax`,`dcg`,`clpfd(clpfd_debug)`,`clpfd(clpfd_options)`,`condcomp`,`debugpred`,`attr`.

## Documentation on exports

**Usage:**`in(Var,Range)`

Constrains Var to take its value in the domain described by Range.

*The following properties should hold at call time:*

(clpfd_rt:fdvar/1)Var is a variable or an integer.

(clpfd_rt:fd_range_expr/1)Range is a range expression.

fd_range_expr(I) :- integer(I). fd_range_expr(..(Min,Max)) :- integer(Min), integer(Max). fd_range_expr(A\/B) :- fd_range_expr(A), fd_range_expr(B).an integer stands for a singleton range, Min..Max for the closed interval from Min to Max, and A\/B for the union of ranges A and B. Range expressions are used by the

`in/2`predciate.

**Usage:**`fd_range_expr(Expr)`

Expr is a range expression.

fd_expr(Var) :- var(Var). fd_expr(I*Exp) :- int(I), fd_expr(Exp). fd_expr(Exp*I) :- int(I), fd_expr(Exp). fd_expr(Exp1+Exp2) :- fd_expr(Exp1), fd_expr(Exp2). fd_expr(Exp1-Exp2) :- fd_expr(Exp1), fd_expr(Exp2). fd_expr(-Exp) :- fd_expr(Exp).FD expressions are used by meta-constraints.

**Usage:**`fd_expr(Expr)`

Expr is an FD expression.

**Usage:**`#=(A,B)`

Constrains the interpretation of A to be equal to the interpretation of B.

*The following properties should hold at call time:*

(clpfd_rt:fd_expr/1)A is an FD expression.

(clpfd_rt:fd_expr/1)B is an FD expression.

**Usage:**`#\=(A,B)`

Constrains the interpretation of A to be different from the interpretation of B

*The following properties should hold at call time:*

(clpfd_rt:fd_expr/1)A is an FD expression.

(clpfd_rt:fd_expr/1)B is an FD expression.

**Usage:**`#<(A,B)`

Constrains the interpretation of A to be smaller than the interpretation of B.

*The following properties should hold at call time:*

(clpfd_rt:fd_expr/1)A is an FD expression.

(clpfd_rt:fd_expr/1)B is an FD expression.

**Usage:**`#=<(A,B)`

Constrains A to be smaller or equal to B.

*The following properties should hold at call time:*

(clpfd_rt:fd_expr/1)A is an FD expression.

(clpfd_rt:fd_expr/1)B is an FD expression.

**Usage:**`#>(A,B)`

Constrains the interpretation of A to be greater than the interpretation of B.

*The following properties should hold at call time:*

(clpfd_rt:fd_expr/1)A is an FD expression.

(clpfd_rt:fd_expr/1)B is an FD expression.

**Usage:**`#>=(A,B)`

Constrains the interpretation of A to be greater or equal than the interpretation of B.

*The following properties should hold at call time:*

(clpfd_rt:fd_expr/1)A is an FD expression.

(clpfd_rt:fd_expr/1)B is an FD expression.

**Usage:**`domain(Vars,Min,Max)`

Constrains each element of Vars to take its value between Min and Max (included). This predicate is generally used to set the initial domain of an interval

*The following properties should hold at call time:*

(basic_props:list/2)Vars is a list of fdvars.

(basic_props:int/1)Min is an integer.

(basic_props:int/1)Max is an integer.

**Usage:**`in(Var,Range)`

Constrains Var to take its value in the domain described by Range.

*The following properties should hold at call time:*

(clpfd_rt:fdvar/1)Var is a variable or an integer.

(clpfd_rt:fd_range_expr/1)Range is a range expression.

**Usage:**`all_different(Vars)`

Constrains all elements in Vars to take distinct values. This is equivalent to posting an inequality constraint for each pair of variables. This constraint is triggered when a variable becomes ground, removing its value from the domain of the other variables.

*The following properties should hold at call time:*

(basic_props:list/2)Vars is a list of fdvars.

**Usage:**`labeling(Options,Vars)`

Assigns a value to each variable in Vars according to the labeling options given by Options. This predicate is re-executable on backtracking.

The different options are :

- []: the leftmost variables is selected first. Its values are enumerating from the smallest to the greatest.
- [ff]: the variable with the smallest number of elements in its domain is selected first. Its values are then enumerated from the smallest to the greatest.
- [step]: the variable with the smallest number of elements in its domain is selected first. The minimal value of the domainis assigned, on bactracking the value is pruned form the domain and a new variable is selected.

*The following properties should hold at call time:*

(basic_props:list/2)Vars is a list of fdvars.

**Usage:**`labeling(Options,Vars)`

Assigns a value to each variable in Vars according to the labeling options given by Options. This predicate is re-executable on backtracking.

The different options are :

- []: the leftmost variables is selected first. Its values are enumerating from the smallest to the greatest.
- [ff]: the variable with the smallest number of elements in its domain is selected first. Its values are then enumerated from the smallest to the greatest.
- [step]: the variable with the smallest number of elements in its domain is selected first. The minimal value of the domainis assigned, on bactracking the value is pruned form the domain and a new variable is selected.

*The following properties should hold at call time:*

(basic_props:list/2)Vars is a list of fdvars.

*Meta-predicate*with arguments:

`minimize(goal,?)`.

*Meta-predicate*with arguments:

`minimize(goal,?)`.

## Documentation on multifiles

*multifile*.

*multifile*.