prologcryptarithmetic-puzzle

Generic prolog cryptarithmetic program


I'm trying to write a program to solve general cryptarithmetic puzzles such as AM+PM=DAY, SEND+MORE=MONEY..

The program I've written is:

gsolve([H1,T1],[H2|T2],[H3|T3]):-
    gsolvehelper([H1,T1],[H2|T2],[H3|T3],[0,1,2,3,4,5,6,7,8,9],0).

gsolvehelper([H1,T1],[H2|T2],[H3|T3],D,C):-
    (   var(H1)->select(H1,D,D1);D1=D),
    (   var(H2)->select(H2,D1,D2);D2=D1),
    (   X1 is H1+H2+C),
    (   H3 is mod(X1,10)),
    (   C1 is X1//10),
    gsolvehelper(T1,T2,T3,D2,C1).

The input is of the form:

gsolve([A,M],[P,M],[D,A,Y]).

The head of the first two lists is added to the carry to find the head of the third list and the new carry and so on.

The error I get is:

ERROR: is/2: Type error: `[]' expected, found `[2,_G3922]' ("x" must hold one character)

I cannot figure out what this error is. Can someone please help me with this?


Solution

  • The error you report is due to what is probably a typo: [H1,T1], appearing in several places. Due to this typo H2 gets -- unintentionally -- instantiated to a list, causing problems in the summation statement. The following reproduces the error in SWI7:

    ?- X is 1 + [1,_] + 3.
    ERROR: is/2: Type error: `[]' expected, found `[1,_G11068]' (a list) ("x" must hold one character)
    

    There are other ways in which you can improve your code:

    Making alterations based upon the above I get:

    gsolve(L1, L2, L3):-
      gsolve(L1, L2, L3, [0,1,2,3,4,5,6,7,8,9], 0).
    
    gsolve([H1|T1], [H2|T2], [H3|T3], D, C):-
      (   var(H1)
      ->  select(H1, D, D1)
      ;   D1 = D
      ),
      (   var(H2)
      ->  select(H2, D1, D2)
      ;   D2=D1
      ),
      X1 is H1 + H2 + C,
      H3 is mod(X1, 10),
      C1 is div(X1, 10),
      gsolve(T1, T2, T3, D2, C1).
    

    This does not solve the puzzle yet... However, since it solves the error you mentioned -- and some other things as well -- you should be able to take it on from here.