Don't understand syntax of clpfd cryptarithmetic puzzle example

Could someone please explain the strange syntax of the first line of the cryptarithmetic puzzle example in the CLP(FD) documentation:
puzzle([S,E,N,D] + [M,O,R,E] = [M,O,N,E,Y]) :- ?

What is this + operator between two lists?
Is = here the unification operator?

?- display(a + b = c).

Long story short, it is just a nested term. + and = are defined as operators which is why you can write it like this. See the documentation for op/3.

This may help:

?- write_canonical([S,E,N,D] + [M,O,R,E] = [M,O,N,E,Y]).

I.e. [S,E,N,D] + [M,O,R,E] = [M,O,N,E,Y] is just a compound term written to take advantage of operator definitions.


OK, so since operator arguments are not typed, X + Y does not generate an error when X and Y are not numbers and = can be used as a term functor between two arguments that obviously can’t unify instead of as a unification predicate. Correct?

Yes, correct.

The way that this is usually explained is by using words like “homoiconic”. The way I understand it is that in Prolog, code and data have the same “shape”. A string like this:

A = B + C

is a Prolog term. Because both = and + are operators, this is parsed as the compound term =(A, +(B, C)). Wnen this term appears in a “data structure” slot, it is just a nested data structure. When the same term appears in the slot for a goal, it is evaluated as if it is code.


?- A = B + C.
A = B+C.

This was now in the slot for a goal. It was evaluated and the variable A was unified with the compound term +(A, B) which is printed by the top level as A+B because + is also defined as an operator.

But also:

?- A = 1, B = 2, C = 3, display(A = B + C).
A = 1,
B = 2,
C = 3.

The last subgoal of the conjunction above is display(A = B + C). display/1 just prints on the top level its argument ignoring any operator definitions. If you decided to interpret this as a mathematical equation it is obviously wrong.

Are you more confused now? You are welcome!