fast-reflexes
fast-reflexes

Reputation: 5176

Silly detail enquiry about Prolog unification

In Prolog:

?-P=[A|B], P=[1,_].
P = [1, _G1091],
A = 1,
B = [_G1091]

B is shown as [_G1091] showing it's an uninstantiated variable. However, if I change a tiny bit...

?-P=[A|B], P=[1|_].
P = [1,B],
A = 1,

All of a sudden it's not interested in showing me that B is uninstantiated but still a variable ready to unify with anything.. how come? (I like to focus on weird details sometimes :) )

Upvotes: 1

Views: 86

Answers (3)

false
false

Reputation: 10102

The precise details of Prolog syntax are sometimes quite subtle. To get used to it use write_canonical/1 which shows you the term in functional notation:

?- write_canonical([A|B]).
'.'(_1,_2)
   true.
?- write_canonical([1,_]).
'.'(1,'.'(_1,[]))
   true.

May I recommend a "drill"-exercise to get used to Prolog's list notation:

Take some list like [[1,2],3] and now try to write it down in as many variants you can imagine.

?- [[1,2],3] == [[1,2],3|[]].
   true.

etc.

In many Prologs the toplevel lets you take the last input (often: cursor-up) such that you can re-edit the right-hand side rapidly.

Upvotes: 2

antlersoft
antlersoft

Reputation: 14786

If you look at the second part of each query, the first amounts to

P=.(1,.(_,[]))

while the second amounts to

P=.(1,_)

In the first, B is bound to .(_,[]); that is, a list that contains an uninstantiated variable

In the second, B is bound to an uninstantiated variable

When a variable is just bound to an uninstantiated variable, there's no point in showing it; in the first example it's bound to something with some additional structure, so there is a point in showing it.

Upvotes: 1

gusbro
gusbro

Reputation: 22585

In the first case:

?-P=[A|B], P=[1,_].

you are stating that P is a list with two elements, the first one being the number 1 (unified to variable A). Therefore, B has to be a list with one element (an unnamed variable).

On the other hand, in the second case:

?-P=[A|B], P=[1|_].

you are stating that P is a list with at least one element (1 again unified to A) but you are not stating anything else. B can be either an empty list, or a list with any amount of elements.

Upvotes: 2

Related Questions