Exercises

## 14.8 Exercises

1. Implement a constraint atmost/3

   >     atmost(+N, +List, +V)

   which takes an integer N, an integer V and a list List containing integers or integer domain variables.

   Meaning: at most N elements of List have value V.

   Behaviour: Fail as soon as too many list elements are instantiated to value V. This requires only basic suspension facilities, no domain information needs to be taken into account.

   Tests are provided in the file atmost.tst. You can test your constraint by loading the library lib(test_util) and then calling test(atmost).

2. Implement a constraint offset/3

   >     offset(?X,+Const,?Y)

   which is declaratively like

   >     offset(X,Const,Y) :- Y #= X+Const.

   but maintains domain-arc-consistency (i.e. propagates "holes", while the above definition only maintains bounds-consistency).

   Use suspension built-ins and domain-access primitives from the ic_kernel module. Use not_unify/2 to test whether a value is outside a variable’s domain.

   Tests are provided in the file offset.tst. You can test your constraint by loading the library lib(test_util). and then calling test(offset).