Search code examples
listprologunificationcons

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 :) )


Solution

  • 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.