Search code examples
prologcryptarithmetic-puzzleeclipse-clp

How to use the labeling function of Prolog (ECLIPSE program) within the SEND+MORE = MONEY program?


So I managed to write the SEND + MORE = MONEY program for Prolog and I'm having trouble labeling the results. Any ideas on how to do that? I keep using the labeling function but it still wouldn't work. I'm lost here.

:- lib(ic).

puzzle(List) :-
    List = [S, E, N, D, M, O, R, Y],
    List :: 0..9,
    diff_list(List),
                    1000*S + 100*E + 10*N + D
       +           1000*M + 100*O + 10*R + E
      $= 10000*M + 1000*O + 100*N + 10*E + Y,
    S $\= 0, M $\= 0,
    shallow_backtrack(List).

shallow_backtrack(List) :-
    ( foreach(Var, List) do once(indomain(Var)) ).

diff_list(List) :-
    ( fromto(List, [X|Tail], Tail, []) do
            ( foreach(Y, Tail), param(X) do
                        X $\= Y
            )
    ).

Results:

?- puzzle(X).
X = [9, 5, 6, 7, 1, 0, 8, 2]
Yes (0.00s cpu)

Any help would be appreciated! Thanks!


Solution

  • Here is a variant of your program that uses labeling:

    :- lib(ic).
    
    puzzle(List) :-
        List = [S, E, N, D, M, O, R, Y],
        List :: 0..9,
        alldifferent(List),
        1000*S + 100*E + 10*N + D
           +           1000*M + 100*O + 10*R + E
          $= 10000*M + 1000*O + 100*N + 10*E + Y,
        S $\= 0, M $\= 0,
        labeling(List).