Why [] = _. true in Prolog?
_
is a boolean variable, just like X
or anything else that starts with an underscore or an uppercase letter. Free variables (i.e. Variables that are not yet associated with any member) can be combined with anything. A purpose like this [] = X
means "unify X
with []
", whereby, if done, each usage X
would refer to the term []
.
C _
as a variable is the same as for X
, except that it _
is an anonymous variable: it cannot be reused, its name does not matter, and different occurrences _
refer to different variables. Therefore, _
it can never be attached before meeting the target [] = _
. So this union succeeds, which is why you get the answer true
.
_
in itself does not mean "not empty". But you might be confused about using it as a placeholder: L = [_,_,_]
means it L
is a list of three elements (which we don't know anything about). In this sense, it _
means "there is something here." But it must be inside the list for this value.
source to share