5

Is it possible to display a Prolog list in its ./2 format, e.g.

for the list:

| ?- L=[a,b,c].
L = [a,b,c] ? 
yes

Is there a means to display:

L = .(a, .(b, .(c, []))).
4
  • 3
    Check out write_canonical/1 in any ISO-conforming system! write_canonical/1 is very useful every time you are in doubt about the exact form of terms you are reasoning about. Example with SICStus Prolog: ?- write_canonical([a,b,c])., yieding: '.'(a,'.'(b,'.'(c,[]))). Very valuable predicate also for data exchange, intended to yield a truly canonical representation that is easy to parse.
    – mat
    Aug 11, 2015 at 10:50
  • 2
    L=[a,b,c], write_canonical(L). worked a treat! I am using sicstus at the moment, sometimes swi at home. I wonder if there are any other means to achieve the same end?
    – bph
    Aug 11, 2015 at 10:54
  • 3
    write_canonical/1 is the standard and best way to do it. You can of course also implement this yourself, using term inspection and decomposition with arg/3, =../2 etc.
    – mat
    Aug 11, 2015 at 11:37
  • 3
    You can also say write_term(L, [quoted(true), ignore_ops(true)]).
    – false
    Aug 11, 2015 at 11:57

1 Answer 1

4

Normally, write_canonical(List) or ?- write_term(List, [quoted(true), ignore_ops(true)]), as pointed out in the comments. Since SWI-Prolog decided to do things differently, this is not good enough:

?- write_canonical([a]).
[a]
true.

?- write_term([a], [quoted(true), ignore_ops(true)]).
[a]
true.

?- write_term([a], [dotlists(true)]).
.(a,[])
true.

See the documentation on write_term/2, pay attention to the options brace_terms(Bool) and dotlists(Bool). But beware: if you start SWI-Prolog 7 normally, the ./2 is not the list functor any more!

?- L = .(a, []).
ERROR: Type error: `dict' expected, found `a' (an atom) % WHAT?

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

If you start it with swipl --traditional, things are back to normal, sort of:

$ swipl --traditional
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.3.4-32-g9311e51)
Copyright (c) 1990-2015 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- L = .(a, []).
L = [a].

You still cannot use write_canonical(List) or write_term(List, [quoted(true), ignore_ops(true)]).

Read the linked section of the SWI-Prolog documentation for details and rationale. As an advice, if you decide to use SWI-Prolog stick to SWI-Prolog 7 with the defaults and only use write_term(List, [dotlists(true)]) if you need to communicate with another Prolog implementation. The usual list notation, [a, b, ...] should be good enough in most conventional situations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.