Рабочий код:
:- use_module(library(clpfd)).
%calculate correct multiplier
%For example if AM, multipler of A is 10 and of M is 1
multiple(1,10).
multiple(N,F) :-
N>0,
N1 is N-1,
multiple(N1,F1),
F is 10 * F1.
%convert accepts input as a list. For instance [A,M]
%outputs a constraint of the form A*10 + M*1
convert([H|T], Ans):-
length(T, Len),
Len = 0,
Ans = H * 1.
convert([H|T], Ans):-
length([H|T], Len1),
Len2 is Len1-1,
multiple(Len2,Multiplier),
convert(T, Ans1),
Ans = Ans1 + H * Multiplier.
puzzle1(As + Bs = Cs) :-
append([As,Bs,Cs],Ds),
term_variables(Ds,Var), %% this will get all Var
Var ins 0..9,
all_different(Var),
convert(As, Ans1),
convert(Bs, Ans2),
convert(Cs, Ans3),
%Generates a constraint depending on the input
%For instance if input is puzzle1([A,M] + [P,M] = [D,A,Y])
%Output constraint = A*10 + M + P*10 + M = D*100 + A*10 + Y
Ans1 + Ans2 #= Ans3,
As = [H1|_],
Bs = [H2|_],
H1 #\=0,
H2 #\=0,
label(Var),
write("Solution: "),
write(As + Bs = Cs).
%puzzle1([A,M]+[P,M]=[D,A,Y]).
%puzzle1([A,M]+[I]=[O,K]).
%puzzle1([Y,O,U]+[A,R,E]=[D,O,N,E]).
%puzzle1([S,E,N,D]+[M,O,R,E]=[M,O,N,E,Y]).
%puzzle1([R,O,A,D]+[I,N]=[U,S,A]).
%puzzle1([B,I,K,E]+[F,O,R]=[R,I,D,E]).