Reputation: 572
I am learning Prolog, and do not understand why ?- [] = _.
returns true in SWI-Prolog.
[]
means an empty list, and _
means it is not empty, right?
Could someone explain the above logic?
Upvotes: 3
Views: 989
Reputation: 9378
_
is a logical variable, just like X
or anything else that starts with an underscore or a capital letter. Free variables (i.e., variables that are not already bound to some term) can be unified with anything. A goal like [] = X
says "unify X
with []
", with the effect that, if this succeeds, every use of X
will refer to the term []
.
With _
as the variable this is the same as for X
, with the exception that _
is the anonymous variable: It cannot be reused, its name does not matter, and different occurrences of _
refer to different variables. So _
can never be bound before encountering the goal [] = _
. Thus this unification succeeds, which is why you get the answer true
.
_
does not by itself mean "not empty". But you may be confused by its use as a placeholder: L = [_,_,_]
means that L
is a list of three elements (that we know nothing about). In this sense _
means "there is something here". But it has to be inside the list for this meaning.
Upvotes: 11