5.1 Lists are special

As of version 7, SWI-Prolog lists can be distinguished unambiguously at runtime from ./2 terms and the atom ’[]'.

   Traditional list               SWI-Prolog 7 list

       '.'                              '[|]'
      /   \                             /   \
     1    '.'                          1   '[|]'
         /   \                             /   \
        2    '.'                          2   '[|]'
            /   \                             /   \
           3   '[]'                          3     []

           terminated with                   terminated with
           the atom '[]',                    a special constant
           indistinguishable from text       which is printed as []

The constant [] is special constant that is not an atom. It has the following properties:

atom([]).        fails
atomic([]).      succeeds
[] == '[]'.      fails
[] == [].        succeeds

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.

This modification has minimal impact on typical Prolog code. It does affect foreign code (see section 12) that uses the normal atom and compound term interface for manipulating lists. In most cases this can be avoided by using the dedicated list functions. For convenience, the macros ATOM_nil and ATOM_dot are provided by SWI-Prolog.h.

Another place that is affected is write_canonical/1. Impact is minimized by using the list syntax for lists. The predicates read_term/2 and write_term/2 support the option dotlists(true), which causes read_term/2 to read .(a,[]) as [a] and write_term/2 to write [a] as .(a,[]).

5.1.1 Motivating‘[|]’and [] for lists

Representing lists the conventional way using ./2 as list cell and the atom '[]' as list terminator both (independently) pose conflicts, while these conflicts are easily avoided.