Reputation: 133
I have been reading this tutorial about Prolog list, and how to get specific element from a list, and I came across this example:
?- [_,X,_,Y|_] = [[], dead(z), [2, [b, c]], [], Z].
X = dead(z)
Y = []
Z = _9593
what I don't understand is, where do they get the value _9593 for the variable Z?
Upvotes: 1
Views: 1100
Reputation: 71119
Z = _9593
that's just an automatically generated unique variable name. Variables in rules and facts must be (consistently) renamed, to ensure uniqueness. If we use Z
in two unrelated queries, those two variables are unrelated, so must have different names.
You can see the same effect with a simpler goal,
1 ?- Z=Z.
Z = _G213
Yes
2 ?- write(Z).
_G236
Z = _G236
213
, 9593
is just a reflection of some internal counter maintained by a particular Prolog implementation. The leading underscore usually signals that it is some throwaway variable.
Upvotes: 2
Reputation: 1871
Underscore means we don't care about variable and int your example
[_,X,_,Y|_] = [[], dead(z), [2, [b, c]], [], Z].
We have first underscore assigned to []
, then X
is assigned to dead(z)
then second underscore assigned to [2. [b.c]]
, then Y
assigned to []
and then the tail is underscore which is we don't care what but since you have Z
variable on right hand side it assigns _
to Z
and Prolog way of doing that is introducing arbitrarily variable starting with _something
and in your case its _9593
. After all you shouldn't be concerned about that because you don't care what underscore is.
Upvotes: 1
Reputation: 22803
I am anxious about @Templar's explanation, though the technical details are correct, I worry about getting the terminology right.
There is no assigning going on here. There is only unification. What we're asking Prolog to do here is unify [_, X, _, Y|_]
with [[], dead(z), [2, [b, c]], [], Z]
. It is of no consequence on which side there are values or variables. Rather than imagine Prolog walking down the left side and then the right when it can't go on, it might be safer to imagine the lists being zipped together.
First, Prolog tries to unify _ with []. This trivially succeeds, because the _ means "I don't care". No bindings are established by this unification.
Next, Prolog tries to unify X with dead(z)
. This trivially succeeds, because X was unbound, and this establishes a binding, X = dead(z)
. Because this variable does not start with the underscore, Prolog figures you'd be interested in this binding, so it reports it to you.
Next, Prolog tries to unify _ with [2, [b, c]]
. Again, this trivially succeeds without establishing a binding.
Next, Prolog tries to unify Y with [], again trivially succeeding, but this time establishing the binding Y = [] which it reported to you.
Before the next step, you have to get a notational change. In Prolog, [X|T]
is the list that starts with X and continues with T. So, [X|_]
unifies X with the first element of a list and discards the tail. So in this case the |_]
essentially says, there may be more to this list, but I don't care especially to know what it is. (By the way, the empty list would match.)
Next, Prolog tries to unify the anonymous tail |_
with Z. This, again, trivially succeeds because Z is not bound. Prolog created an anonymous variable and bound it to Z, so this establishes a binding.
So, the same information as @Templar gave, but with slightly different vocabulary, here for your amusement.
Upvotes: 2