Mystery Unification

I have this DCG:

mdy(M,D,Y) --> integer(M),`/`,integer(D),`/`,integer(Y).

At first, I didn’t think it needed to be tested on its own, but I did it anyway. This is as expected:

?- phrase(mdy(M,D,Y),`07/06/2019`,_).
M = 7,
D = 6,
Y = 2019.

When I do this I get an unexpected unification:

?- phrase(mdy(M,D,Y),`07/07/2019`,_).
M = D, D = 7,
Y = 2019.

So far, I haven’t found anything wrong with it, but it does concern me that it makes a unification simply because two different variables just happen to equal the same value. Why would it do this?


Writing a binding as

V1 = V2, V2 = V3, .... Vn-1 = Vn, Vn = <value>

is just (SWI-)Prolog’s way to say that two variables have exactly the same value (as in ==). In Prolog is generally not observable whether two terms are equal because they have been unified or simply because they happen to have the same value. Thus, X = 7, X = Y is internally exactly the same as X = 7, Y = 7.

1 Like

With Discourse, which this site runs on, the topics use Markdown for easily formatting the text into HTML.

The most common use of Markdown here is to use ```prolog before some prolog code and ``` after the code.


mdy(M,D,Y) --> integer(M), `/` ,integer(D), `/` ,integer(Y).

in the editor will be seen here as

mdy(M,D,Y) --> integer(M), / ,integer(D), / ,integer(Y).

In the editor it needs to have the Markdown as such

mdy(M,D,Y) --> integer(M), `/` ,integer(D), `/` ,integer(Y).

to be seen here as

mdy(M,D,Y) --> integer(M), `/` ,integer(D), `/` ,integer(Y).