Search code examples
prologprolog-toplevel

Why `X=1,X=1.` prints `X=1` instead of `true`


I'm learning the basics of Prolog and I was wondering why the following line prints X = 1 instead of true?

?- X=1,1=X.
X = 1.

--

The first X=1 in my command is an assignment, and the second one will be a check of equality.


Solution

  • There are no assignments or equality tests in your query, only unification of terms. The query succeeds by unifying the variable X with 1 and that's what the top-level reports: it tells which variable bindings makes the query true.

    After the first goal in the conjunction, X = 1, succeeds, the second goal is the unification 1 = 1, which trivially succeeds.

    P.S. Also note that Prolog systems differ in the way they report successful queries. Some print true, others print yes (the traditional way that successful queries are reported).