This section describes the basic term reading and writing predicates. The predicates format/[1,2] and writef/2 provide formatted output. Writing to Prolog data structures such as atoms or code-lists is supported by with_output_to/2 and format/3.
Reading is sensitive to the Prolog flag character_escapes,
which controls the interpretation of the
character in quoted atoms and strings.
\
ignore
(ignore the attribute), dots
(write the attributes as {...}
), write
(simply
hand the attributes recursively to write_term/2)
and portray
(hand the attributes to attr_portray_hook/2).string
causes string
objects to be printed between back quotes and symbol_char
causes the backquote to be printed unquoted. In all other cases the
backquote is printed as a quoted atom.true
(default), write {}(X)
as {X}
.
See also
dotlists
and ignore_ops
.portray
,
portray/1
is called for each blob encountered. See section
10.4.7.true
and quoted(true)
is active, special
characters in quoted atoms and strings are emitted as ISO escape
sequences. Default is taken from the reference module (see below).true
(default), cyclic terms are written as
@(Template, Substitutions)
, where Substitutions
is a list
Var = Value. If cycles
is false
,
max_depth
is not given, and Term is cyclic, write_term/2
raises a domain_error
.79The
cycles option and the cyclic term representation using the @-term are
copied from SICStus Prolog. However, the default in SICStus is set to false
and SICStus writes an infinite term if not protected by, e.g., the depth_limit
option. See also the cycles
option in
read_term/2.true
(default false
), write lists using the
dotted term notation rather than the list notation.80Copied
from ECLiPSe. Note that as of version 7, the list
constructor is
'[|]'
. Using dotlists(true)
, write_term/2
writes a list using `.' as constructor. This is intended for
communication with programs such as other Prolog systems, that rely on
this notation.true
(default false
), add a fullstop token
to the output. The dot is preceeded by a space if needed and followed by
a space (default) or newline if the nl(true)
option is also
given.81Compatible with ECLiPSetrue
, the generic term representation (<functor>(<args>
... )) will be used for all terms. Otherwise (default), operators will
be used where appropriate.82In
traditional systems this flag also stops the syntactic sugar notation
for lists and brace terms. In SWI-Prolog, these are controlled by the
separate options dotlists
and brace_terms
.?- write_term(a(s(s(s(s(0)))), [a,b,c,d,e,f]), [max_depth(3)]). a(s(s(...)), [a, b|...]) true.
Used by the top level and debugger to limit screen output. See also the Prolog flags answer_write_options and debugger_write_options.
user
). This defines
the default value for the character_escapes
option as well as the operator definitions to use. See also op/3.fullstop
option.true
, terms of the format $VAR(N)
, where N
is a non-negative integer, will be written as a variable name. If N
is an atom it is written without quotes. This extension allows for
writing variables with user-provided names. The default is false
.
See also numbervars/3
and the option variable_names
.true
(default false
), do not reset the
logic that inserts extra spaces that separate tokens where needed. This
is intended to solve the problems with the code below. Calling write_value(.
)
writes ..
, which cannot be read. By adding partial(true)
to the option list, it correctly emits . .
. Similar
problems appear when emitting operators using multiple calls to write_term/3.
write_value(Value) :- write_term(Value, [partial(true)]), write('.'), nl.
portrayed(Bool)
. Deprecated.portray(true)
, but calls Goal rather
than the predefined hook portray/1. Goal
is called through call/3,
where the first argument is Goal, the second is the term to
be printed and the 3rd argument is the current write option list. The
write option list is copied from the write_term call, but the list is
guaranteed to hold an option priority
that reflects the
current priority.true
, the hook portray/1
is called before printing a term that is not a variable. If portray/1
succeeds, the term is considered printed. See also print/1.
The default is false
. This option is an extension to the
ISO write_term options.format('~w = ', [VarName]), write_term(Value, [quoted(true), priority(699)])
true
, atoms and functors that need quotes will be
quoted. The default is false
.standard
, adding only space
where needed for proper tokenization by read_term/3.
Currently, the only other value is next_argument
, adding a
space after a comma used to separate arguments in a term or list.The implementation binds the variables from List to a term
'$VAR'
(Name). Like write_canonical/1,
terms that where already bound to '$VAR'
(X)
before write_term/2
are printed normally, unless the option numbervars(true)
is
also provided. If the option numbervars(true)
is used, the
user is responsible for avoiding collisions between assigned names and
numbered names. See also the variable_names
option of
read_term/2.
Possible variable attributes (see section 7) are ignored. In most cases one should use copy_term/3 to obtain a copy that is free of attributed variables and handle the associated constraints as appropriate for the use-case.
ignore_ops
,
quoted
and numbervars
after numbervars/4
using the
singletons
option.
Note that due to the use of numbervars/4,
non-ground terms must be written using a single write_canonical/1
call. This used to be the case anyhow, as garbage collection between
multiple calls to one of the write predicates can change the _G
<NNN>
identity of the variables.
write(Term), nl.
. The output stream is
locked, which implies no output from other threads can appear between
the term and newline.write(Stream, Term), nl(Stream).
. The output
stream is locked, which implies no output from other threads can appear
between the term and newline.print(Term) :- current_prolog_flag(print_write_options, Options), !, write_term(Term, Options). print(Term) :- write_term(Term, [ portray(true), numbervars(true), quoted(true) ]).
The print/1
predicate is used primarily through the ~p
escape sequence
of format/2,
which is commonly used in the recipies used by
print_message/2
to emit messages.
The classical definition of this predicate is equivalent to the ISO
predicate write_term/2
using the options portray(true)
and
numbervars(true)
. The portray(true)
options
allows the user to implement application-specific printing of terms
printed during debugging to facilitate easy understanding of the output.
See also
portray/1
and library(portray_text)
. SWI-Prolog adds quoted(true)
to facilitate copy/paste of terms not affected by portray/1
and better distinguishing of e.g., 42
, '42'
and "42"
, a number, atom and string.
end_of_file
.dec10
(report and restart).true
(default), call
prolog:comment_hook(Comments, TermPos, Term)
if this
multifile hook is defined (see prolog:comment_hook/3). This is used to
drive PlDoc.process_comment(false)
.
The singletons
option of read_term/3
is initialised from the active style-checking mode. The module
option is initialised to the current compilation module (see prolog_load_context/2).
true
, read `
...`
to a string
object (see
section 5.2). The default depends
on the Prolog flag
back_quotes.\
escape sequences in quoted atoms. See
the Prolog flag character_escapes
in current_prolog_flag/2.
(SWI-Prolog).true
(default false
), re-instantiate
templates as produced by the corresponding write_term/2
option. Note that the default is false
to avoid
misinterpretation of @(Template, Substutions)
, while the
default of write_term/2
is true
because emitting cyclic terms without using the
template construct produces an infinitely large term (read: it will
generate an error after producing a huge amount of output).true
(default false
), read .(a,[])
as a list, even if lists are internally nor constructed using the dot as
functor. This is primarily intended to read the output from write_canonical/1
from other Prolog systems. See section
5.1.quasi_quotation(+Syntax,
+Quotation, +VarDict, -Result)
, where Syntax is the
term in
{|Syntax||
, Quotation is a list of character
codes that represent the quotation, VarDict is a list of
Name=Variable and Result is a variable
that shares with the place where the quotation must be inserted. This
option is intended to support tools that manipulate Prolog source text.variable_names
, but only reports the variables occurring
only once in the Term read. Variables starting with an
underscore (`_
') are not included in this list. (ISO). If Vars
is the constant warning
, singleton variables are reported
using
print_message/2.
The variables appear in the order they have been read.error
(default), throw an exception on a syntax error.
Other values are fail
, which causes a message to be printed
using
print_message/2,
after which the predicate fails, quiet
which causes the
predicate to fail silently, and dec10
which causes syntax
errors to be printed, after which read_term/[2,3]
continues reading the next term. Using dec10
, read_term/[2,3]
never fails. (Quintus, SICStus)."
).{...}
, as used in DCG rules. Arg
describes the argument.|
<TailTerm> , Tail
is unified with the term position of the tail, otherwise with the atom none
.variable_names
, but allows for history
substitutions. read_history/6
is used by the top level to read the user's actions. Show is
the command the user should type to show the saved events. Help
is the command to get an overview of the capabilities. Special
is a list of commands that are not saved in the history. Prompt
is the first prompt given. Continuation prompts for more lines are
determined by prompt/2.
A
%w
in the prompt is substituted by the event number. See
section 2.7 for available
substitutions.
SWI-Prolog calls read_history/6 as follows:
read_history(h, '!h', [trace], '%w ?- ', Goal, Bindings)