Symbolic constraints
Author: Daniel Diaz
Keywords: GNU Prolog, manual, Prolog, compiler, constraints, finite domains

### 9.8 Symbolic constraints

- fd_all_different/1
- fd_element/3
- fd_element_var/3
- fd_atmost/3, fd_atleast/3, fd_exactly/3
- fd_relation/2, fd_relationc/2

#### 9.8.1 fd_all_different/1

Templates

  fd_all_different(+fd_variable_list)

Description

fd_all_different(List) constrains all variables in List 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.

Errors

List is a partial list |  | instantiation_error
List is neither a partial list nor a list |  | type_error(list, List)
an element E of the List list is neither a variable nor an integer nor an FD variable |  | type_error(fd_variable, E)

Portability

GNU Prolog predicate.

#### 9.8.2 fd_element/3

Templates

  fd_element(?fd_variable, +integer_list, ?fd_variable)

Description

fd_element(I, List, X) constraints X to be equal to the Ith integer (from 1) of List.

Errors

I is neither a variable nor an FD variable nor an integer |  | type_error(fd_variable, I)
X is neither a variable nor an FD variable nor an integer |  | type_error(fd_variable, X)
List is a partial list or a list with an element E which is a variable |  | instantiation_error
List is neither a partial list nor a list |  | type_error(list, List)
an element E of the List list is neither a variable nor an integer |  | type_error(integer, E)

Portability

GNU Prolog predicate.

#### 9.8.3 fd_element_var/3

Templates

  fd_element_var(?fd_variable, +fd_variable_list, ?fd_variable)

Description

fd_element_var(I, List, X) constraints X to be equal to the Ith variable (from 1) of List. This constraint is similar to fd_element/3 (section 9.8.2) but List can also contain FD variables (rather than just integers).

Errors

I is neither a variable nor an FD variable nor an integer |  | type_error(fd_variable, I)
X is neither a variable nor an FD variable nor an integer |  | type_error(fd_variable, X)
List is a partial list |  | instantiation_error
List is neither a partial list nor a list |  | type_error(list, List)
an element E of the List list is neither a variable nor an integer nor an FD variable |  | type_error(fd_variable, E)

Portability

GNU Prolog predicate.

#### 9.8.4 fd_atmost/3, fd_atleast/3, fd_exactly/3

Templates

  fd_atmost(+integer, +fd_variable_list, +integer)
  fd_atleast(+integer, +fd_variable_list, +integer)
  fd_exactly(+integer, +fd_variable_list, +integer)

Description

fd_atmost(N, List, V) posts the constraint that at most N variables of List are equal to the value V.

fd_atleast(N, List, V) posts the constraint that at least N variables of List are equal to the value V.

fd_exactly(N, List, V) posts the constraint that at exactly N variables of List are equal to the value V.

These constraints are special cases of fd_cardinality/2 (section 9.7.4) but their implementation is more efficient.

Errors

N is a variable |  | instantiation_error
N is neither a variable nor an integer |  | type_error(integer, N)
V is a variable |  | instantiation_error
V is neither a variable nor an integer |  | type_error(integer, V)
List is a partial list |  | instantiation_error
List is neither a partial list nor a list |  | type_error(list, List)
an element E of the List list is neither a variable nor an FD variable nor an integer |  | type_error(fd_variable, E)

Portability

GNU Prolog predicates.

#### 9.8.5 fd_relation/2, fd_relationc/2

Templates

  fd_relation(+integer_list_list, ?fd_variable_list)
  fd_relationc(+integer_list_list, ?fd_variable_list)

Description

fd_relation(Relation, Vars) constraints the tuple of variables Vars to be equal to one tuple of the list Relation. A tuple is represented by a list.

Example: definition of the boolean AND relation so that X AND Y ⇔ Z:

      and(X,Y,Z):-
              fd_relation([[0,0,0],[0,1,0],[1,0,0],[1,1,1]], [X,Y,Z]).

fd_relationc(Columns, Vars) is similar to fd_relation/2 except that the relation is not given as the list of tuples but as the list of the columns of the relation. A column is represented by a list.

Example:

      and(X,Y,Z):-
              fd_relationc([[0,0,1,1],[0,1,0,1],[0,0,0,1]], [X,Y,Z]).

Errors

Relation is a partial list or a list with a sub-term E which is a variable |  | instantiation_error
Relation is neither a partial list nor a list |  | type_error(list, Relation)
an element E of the Relation list is neither a variable nor an integer |  | type_error(integer, E)
Vars is a partial list |  | instantiation_error
Vars is neither a partial list nor a list |  | type_error(list, Vars)
an element E of the Vars list is neither a variable nor an integer nor an FD variable |  | type_error(fd_variable, E)

Portability

GNU Prolog predicates.

Copyright (C) 1999-2021 Daniel Diaz Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved. More about the copyright