prologunificationiso-prologunify

Why does SWI-Prolog unify a quoted and unquoted string (without spaces) to the same rule?


Assume I have the following rules:

unify('test', 'this is a test').
run :- write('Enter something: '), 
       read(X), 
       unify(X, Y), 
       write('The answer is '), write(Y).

And then I run it as follows:

?- ['unify.pl'].
% unify.pl compiled 0.00 sec, -48 bytes
true.

?- run.
Enter something: test.
The answer is this is a test
true.

?- run.
Enter something: 'test'.
The answer is this is a test
true.

Why does SWI-Prolog unify both test and 'test' to unify('test', 'this is a test').? I came across this while answering a Prolog question on SO. While I was able to answer the person's question, I couldn't explain this particular behavior, and I was wondering if any one else could.


Solution

  • While atoms in SWI-PROLOG can be denoted using single quotes, e.g, 'This is an atom', single quotes are not needed when the SWI-PROLOG parser can identify an atom from a sequence of characters, usually starting with a lowercase alphabetic character, such as test. If the sequence contained whitespace (or some other characters), you'd need the single quotes to denote an atom properly. Alphanumeric characters and certain punctuation characters like underscore _ are fine, e.g., test5_6.

    If the character sequence without single quotes were to start with anything else, such as a number 6k, the parser will treat it as a number; if it were an uppercase alphabetic character such as Test, the parser will treat it as a variable.