Just to show you where I need this:
This solves SEND+MORE=MONEY and any other puzzle of this kind.
It is the Prolog way how I learned addition in elementary school.
#!/usr/bin/python
import janus_swi as janus
def print_prolog(query_input):
print("Query: %s" % query_input)
query = janus.query(query_input)
anzahl = 0
while result := query.next():
print(result)
anzahl += 1
print("Number:", anzahl)
query.close()
for x in range(10):
for y in range(10): # range(x, 10)
digit = (x + y) % 10
over = (x + y) // 10
# print("charsum('0', '%i', '%i', '%i', '%i')" % (x, y, digit, over))
janus.query_once("assertz(charsum('0', '%i', '%i', '%i', '%i'))" % (x, y, digit, over))
janus.consult("x", """
charsum('1', X, Y, Z, Cout) :-
charsum('0', X, Y, Z1, C1),
charsum('0', Z1, '1', Z, C2),
charsum('0', C1, C2, Cout, '0').
sum_intern('0', [], [], [], '0').
sum_intern('1', [], [], ['1'], '0').
sum_intern(Cin, [], [Y | Yrest], [Z | Zrest], Cout) :-
charsum(Cin, '0', Y, Z, Carry),
sum_intern(Carry, [], Yrest, Zrest, Cout).
sum_intern(Cin, [X | Xrest], [], [Z | Zrest], Cout) :-
charsum(Cin, X, '0', Z, Carry),
sum_intern(Carry, Xrest, [], Zrest, Cout).
sum_intern(Cin, [X | Xrest], [Y | Yrest], [Z | Zrest], Cout) :-
charsum(Cin, X, Y, Z, Carry),
sum_intern(Carry, Xrest, Yrest, Zrest, Cout).
sum_rev(X, Y, Z) :-
sum_intern('0', X, Y, Z, '0').
different([]).
different([X | Rs]) :-
not(member(X, Rs)),
different(Rs).
""")
print()
print_prolog("""
sum_rev([D, N, E, S], [E, R, O, M], [Y, E, N, O, M]),
different([S, E, N, D, M, O, R, Y])
""")