To illustrate finite domain constraints we consider the classic "SEND MORE MONEY" prob- lem where the three words represent a sum (SEND + MORE = MONEY) and each letter a variable which takes one of the integers 0 to 9.
Traditionally this problem takes a considerable amount of search time to find the solution by considering all possible integer values for each variable in turn. Using constraints, the values are propagated and the solution is calculated directly.
:- import(const_domain).send([[S,E,N,D], [M,O,R,E], [M,O,N,E,Y]]) :- Digits = [S,E,N,D,M,O,R,Y], Carries = [C1,C2,C3,C4], Digits in 0..9, Carries in 0..1,
M ?= C4, O + 10 * C4 ?= M + S + C3, N + 10 * C3 ?= O + E + C2, E + 10 * C2 ?= R + N + C1, Y + 10 * C1 ?= E + D,
M ?>= 1, S ?>= 1, all_distinct(Digits), label(Digits).
[user] ?- send(X). X = [[9,5,6,7],[1,0,8,5],[1,0,6,5,2]] yes
| scroll to top |
|