Why is a list with an empty list true?

While playing around on the swipl repl, I tried out the following:

?- [X|X].
X = [].
?- [[]|[]].
true.
?- [[]].
true.

I don’t really understand this behavior. What is happening here? Why is a list containing one empty list true?

1 Like

I think the reason is in here:

See consult/1. I can’t be certain, but the sentence:

Abbreviation using ?- [file1,file2]. does not work for the empty list ( [] ).

probably means that this doesn’t work:

?- [].

If I read the code in init.pl correctly:

?- [[]].

becomes

?- consult(user:[]).

which calls load_files/2 which calls ‘$load_files’/3 which has the clause:

'$load_files'([], _, _) :- !.

which is why ?- [[]]. succeeds without choice points.

This is the “what happens”. The answer to “why” is beyond me.

2 Likes

As seen by ?- you are at the top level and so the top level is parsing the input.

[ ] is a command used by the top level for loading source code and not a list.

Boris took the other side of the coin but the answer is the same.

See: Getting started quickly