Most of the following predicates come in two versions, with or without a stream argument. Predicates without a stream argument operate on the current input or output stream, depending on context. Predicates with a stream argument can take stream reference or an alias in this argument position, the alias being replaced by the stream it was associated with.
Some of these predicates support a notation for terms
containing multiple occurrences of the same subterm (cycles and DAGs).
The notation is @(
Template,
Substitution)
where
Substitution is a list of Var=Term pairs where
the Var occurs in Template or in one of the Terms.
This notation stands for the instance of Template obtained by
binding each Var to its corresponding Term. The
purpose of this notation is to provide a finite printed representation
of cyclic terms. This notation is not used by default, and
@/2
has no special meaning except in this context.
read(
?Term)
ISOread(
+Stream,
?Term)
ISOread(
Stream,
Term)
causes the end of Stream to
be reached, Term is unified with the term
end_of_file
. Further calls to read/2
for the same
stream will then raise an exception, unless the stream is
connected to the terminal. The characters read are subject to
character-conversion, see below.
read_term(
?Term,
+Options)
ISOread_term(
+Stream,
?Term,
+Options)
ISOread/[1,2]
with a list of options to provide extra
control or information about the term. Options is a
list of zero or more of:
syntax_errors(
+Val)
syntax_errors
Prolog flag. The
default is set by that flag.
variables(
?Vars)
variable_names(
?Names)
singletons(
?Names)
cycles(
+Boolean)
true
or false
. If selected, any
occurrences of @/2
in the term read in are replaced by the
potentially cyclic terms they denote as described above.
Otherwise (the default), Term is just unified with the
term read in.
layout(
?Layout)
[]
, if no line number information is available for X.
consume_layout(
+Boolean)
true
or false
. If this option is
true
, read_term/[2,3]
will consume the
layout-text-item that follows the terminating `.' (this
layout-text-item can either be a layout-char or a
comment starting with a `%'). If the option is false
, the
layout-text-item will remain in the input stream, so that
subsequent character input predicates will see it. The default of
the consume_layout
option is true
in sicstus
execution mode, and it is false
in iso
execution mode.
| ?- read_term(T, [layout(L), variable_names(Va), singletons(S)]). |: [ foo(X), X = Y ]. L = [35,[36,36],[36,[37,37,37],38]], S = ['Y'=_A], T = [foo(_B),_B=_A], Va = ['X'=_B,'Y'=_A] | ?- read_term(T, [consume_layout(false)]), get_code(C). |: 1. C = 10, T = 1 | ?- read_term(T, [consume_layout(true)]), get_code(C). |: 1. |: a C = 97, T = 1
char_conversion(
+InChar,
+OutChar)
ISOchar_conversion/2
.
If InChar and OutChar are the same, the effect of
char_conversion/2
is to remove any mapping of InChar from
the character-conversion mapping.
current_char_conversion(
?InChar,
?OutChar)
ISOwrite(
?Term)
ISOwrite(
+Stream,
?Term)
ISOwrite_term([
Stream,]
Term, [numbervars(true)])
.
display(
?Term)
write_term(user,
Term, [ignore_ops(true)])
.
write_canonical(
?Term)
ISOwrite_canonical(
+Stream,
?Term)
ISOwrite(
Stream,
Term)
. The term will
be written according to the standard syntax. The output from
write_canonical/2
can be parsed by read/2
even if the
term contains special characters or if operator
declarations have changed. Same as
write_term([
Stream,]
Term,
[quoted(true),ignore_ops(true)])
.
writeq(
?Term)
ISOwriteq(
+Stream,
?Term)
ISOwrite(
Stream,
Term)
, but the names of
atoms and functors are quoted where necessary to make the
result acceptable as input to read/2
, provided the same
operator declarations are in effect. Same as
write_term([
Stream,]
Term,
[quoted(true),numbervars(true)])
.
print(
?Term)
hookableprint(
+Stream,
?Term)
hookablewrite(
Stream,
Term)
.
user:portray/1
. If this succeeds then it
is assumed that Term has been output.
print/2
is called recursively on the components of
Term, unless Term is atomic in which case it is
written via write/2
.
In particular, the debugging package prints the goals in the tracing messages, and the top-level prints the final values of variables. Thus you can vary the forms of these messages if you wish.
Note that on lists ([_|_]
), print/2
will first give
the whole list to user:portray/1
, but if this fails it will
only give each of the (top-level) elements to user:portray/1
.
That is, user:portray/1
will not be called on all the tails
of the list.
Same as write_term([
Stream,]
Term, [portrayed(true),numbervars(true)])
.
portray(
+Term)
hookuser:portray(
+Term)
write/1
) will print the Term.
portray_clause(
?Clause)
portray_clause(
+Stream,
?Clause)
listing/[0,1]
would have written it. Same as
write_term([
Stream,]
Term,
[quoted(true),numbervars(true),indented(true)])
followed by a period
and a newline
and binding variables to terms of the form '$VAR'(
N)
yielding friendlier variable names.
write_term(
+Term,
+Options)
ISOwrite_term(
+Stream,
+Term,
+Options)
ISOwrite/[1,2]
etc. with a list of options to
provide extra control. This predicate in fact subsumes the above
output predicates except portray_clause/[1,2]
, which
additionally prints a period and a newline, and removes module
prefixes that are redundant wrt. the current type-in
module. Options is a list of zero or more of the following,
where Boolean must be true
or false
(false
is
the default).
quoted(
+Boolean)
read/1
. write_canonical/1
,
writeq/1
, and portray_clause/1
select this.
ignore_ops(
+Boolean)
write_canonical/1
and
display/1
select this.
portrayed(
+Boolean)
user:portray/1
is called for each subterm.
print/1
selects this.
numbervars(
+Boolean)
'$VAR'(
N)
where
N is an integer >= 0, an atom, or a code-list, are
treated specially (see numbervars/3
). print/1
,
write/1
, writeq/1
, and portray_clause/1
select
this.
cycles(
+Boolean)
@/2
notation, as discussed above.
indented(
+Boolean)
portray_clause/1
and listing/[0,1]
.
max_depth(
+Depth)
character_escapes(
+Boolean)
character_escapes
Prolog flag.
float_format(
+Spec)
format/[2,3]
character
sequences for printing floats. The default is `~H'.
priority(
+Prio)
format(
+Format,
:Arguments)
format(
+Stream,
+Format,
:Arguments)
| ?- format("Hello world!\n", []).
has the same effect as
| ?- format('Hello world!\n', []).
no matter which value the double_quotes
Prolog flag has.
format/[2,3]
is the Prolog equivalent to the C stdio
function printf()
.
Arguments is a list of items to be printed. If there are no items then an empty list should be supplied.
The default action on a format character is to print it. The character `~' introduces a control sequence. To print a `~' repeat it:
| ?- format('Hello ~~world!\n', []). Hello ~world!
Unless character escapes have been switched off, the escape sequence (see Escape Sequences) `\c' (c for continue) is useful when formatting a string for readability. It causes all characters up to, but not including, the next non-layout character to be ignored.
| ?- format('Hello \c world!\n', []). Hello world!
The general format of a control sequence is `~NC'. The character C determines the type of the control sequence. N is an optional numeric argument. An alternative form of N is `*'. `*' implies that the next argument in Arguments should be used as a numeric argument in the control sequence. Example:
| ?- format('Hello~4cworld!\n', [0'x]). Helloxxxxworld! | ?- format('Hello~*cworld!\n', [4,0'x]). Helloxxxxworld!
The following control sequences are available.
| ?- Pi=3.14159265, format('~e ~2E ~0E\n', [Pi,Pi,Pi]). 3.141593e+00 3.14E+00 3.0E+00
| ?- Pi=3.14159265, format('~f, ~2F, ~0F\n', [Pi,Pi,Pi]). 3.141593, 3.14, 3.0
| ?- format('~g ~2G ~0G\n', [1.23456789e+10, 3.14159265, 0.0123]). 1.23457e+10 3.1 0.01
| ?- F = 123000.0, G = 0.000123, format('~h ~h ~2h ~2H ~-1H\n', [F,G,F,G,3.14]). 123000.0 0.000123 1.23e+05 1.23E-04 3.14E+00
The intuition is that for numbers like 123000000.0, at most N consecutive zeroes before the decimal point are allowed in `f' notation. Similarly for numbers like 0.000000123.
`E' notation is forced by using `~-1H'.
`F' is forced by using `~999H'.
| ?- format('Hello ~1d world!\n', [42]). Hello 4.2 world! | ?- format('Hello ~d world!\n', [42]). Hello 42 world!
| ?- format('Hello ~1D world!\n', [12345]). Hello 1,234.5 world!
| ?- format('Hello ~2r world!\n', [15]). Hello 1111 world! | ?- format('Hello ~16r world!\n', [15]). Hello f world!
| ?- format('Hello ~16R world!\n', [15]). Hello F world!
| ?- format('Hello ~4s ~4s!\n', ["new","world"]). Hello new worl! | ?- format('Hello ~s world!\n', ["new"]). Hello new world!
| ?- format('Hello ~i~s world!\n', ["old","new"]). Hello new world!
write_canonical/1
(see Term I/O). Example:
| ?- format('Hello ~k world!\n', [[a,b,c]]). Hello .(a,.(b,.(c,[]))) world!
print/1
(see Term I/O). Example:
| ?- assert((portray([X|Y]) :- print(cons(X,Y)))). | ?- format('Hello ~p world!\n', [[a,b,c]]). Hello cons(a,cons(b,cons(c,[]))) world!
writeq/1
(see Term I/O).
Example:
| ?- format('Hello ~q world!\n', [['A','B']]). Hello ['A','B'] world!
write/1
(see Term I/O). Example:
| ?- format('Hello ~w world!\n', [['A','B']]). Hello [A,B] world!
| ?- format('Hello ~@ world!\n', [write(new)]). Hello new world!
| ?- format('Hello ~~ world!\n', []). Hello ~ world!
| ?- format('Hello ~n world!\n', []). Hello world!
The following control sequences set column boundaries and specify padding. A column is defined as the available space between two consecutive column boundaries on the same line. A boundary is initially assumed at line position 0. The specifications only apply to the line currently being written.
When a column boundary is set (`~|' or `~+') and there are fewer characters written in the column than its specified width, the remaining space is divided equally amongst the pad sequences (`~t') in the column. If there are no pad sequences, the column is space padded at the end.
If `~|' or `~+' specifies a position preceding the current position, the boundary is set at the current position.
`
C where C is
the fill character. The default fill character is <SPC>. Any
(`~t') after the last column boundary on a line is ignored.
| ?- format('~`*t NICE TABLE ~`*t~61|~n', []), format('*~t*~61|~n', []), format('*~t~a~20|~t~a~t~20+~a~t~20+~t*~61|~n', ['Right aligned','Centered','Left aligned']), format('*~t~d~20|~t~d~t~20+~d~t~20+~t*~61|~n', [123,45,678]), format('*~t~d~20|~t~d~t~20+~d~t~20+~t*~61|~n', [1,2345,6789]), format('~`*t~61|~n', []).
************************ NICE TABLE ************************* * * * Right aligned Centered Left aligned * * 123 45 678 * * 1 2345 6789 * *************************************************************