prologswi-prolog

How does SWI Prolog handle lists under the hood?


Why is this not working?

?- '.'(a,[]).
ERROR: Unknown procedure: ('.')/2
ERROR:     However, there are definitions for:
ERROR:         ('.')/3
false.

?- .(a,[]).
ERROR: Unknown procedure: ('.')/2
ERROR:     However, there are definitions for:
ERROR:         ('.')/3
false.

?- .(a,[]) == [a].
ERROR: Type error: `dict' expected, found `a' (an atom)
ERROR: In:
ERROR:   [13] throw(error(type_error(dict,a),_13228))
ERROR:   [10] '<meta-call>'(user:user: ...) <foreign>
ERROR:    [9] toplevel_call(user:user: ...) at /usr/lib/swi-prolog/boot/toplevel.pl:1158
ERROR: 
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.

Not sure how relevant this is practically but according to the literature this should be correct. Does SWI have a different predicate/procedure?

Update

The internal representation is also revealed by means of display:

?- display([1,2,3]).
'[|]'(1,'[|]'(2,'[|]'(3,[])))

Solution

  • As linked in the comments, the SWI Prolog documentation says:

    The ‘cons' operator for creating list cells has changed from the pretty atom ‘.’ to the ugly atom ‘[|]’, so we can use the ‘.’ for other purposes, notably functional notation on dicts. See section 5.4.1.

    That means the equivalent to your traditional .(a,.(b,[])) in SWI 7+ is:

    ?- X = '[|]'(a, '[|]'(b, [])).
    
    X = [a, b]