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)
[ISO]
read(
+Stream,
?Term)
[ISO]
The next term, delimited by a full-stop (i.e. a ., possibly
followed by layout text), is read from Stream and is unified with
Term. The syntax of the term must agree with current operator
declarations. If a call read(
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)
[ISO]
read_term(
+Stream,
?Term,
+Options)
[ISO]
Same as read/[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 which 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
is 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] ? yes | ?- read_term(T, [consume_layout(false)]), get_code(C). |: 1. C = 10, T = 1 ? yes | ?- read_term(T, [consume_layout(true)]), get_code(C). |: 1. |: a C = 97, T = 1 ? yes
char_conversion(
+InChar,
+OutChar)
[ISO]
InChar and OutChar should be one-char atoms. If they are not
the same, then the mapping of InChar to OutChar is added to
the character-conversion mapping. This means that in all
subsequent term and program input operations any unquoted
occurrence of InChar will be replaced by OutChar. The
rationale for providing this facility is that in some extended character
sets (such as Japanese JIS character sets) the same character can appear
several times and thus have several codes, which the users normally
expect to be equivalent. It is advisable to always quote the arguments
of char_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)
[ISO]
The character of one-char atom InChar is mapped to
that of the one-char atom OutChar in the current
character-conversion mapping. Enumerates all such pairs on backtracking.
write(
?Term)
[ISO]
write(
+Stream,
?Term)
[ISO]
The term Term is written onto Stream according to current
operator declarations.
Same as write_term([
Stream,]
Term, [numbervars(true)])
.
display(
?Term)
The term Term is displayed onto the standard output stream
(which is not necessarily the current output stream) in standard
parenthesized prefix notation.
Same as write_term(user,
Term, [ignore_ops(true)])
.
write_canonical(
?Term)
[ISO]
write_canonical(
+Stream,
?Term)
[ISO]
Similar to write(
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)
[ISO]
writeq(
+Stream,
?Term)
[ISO]
Similar to write(
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)
[Hookable]
print(
+Stream,
?Term)
[Hookable]
Prints Term onto Stream. This predicate provides a handle for user defined pretty printing:
write(
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)
[Hook]
user:portray(
+Term)
This should either print the Term and
succeed, or do nothing and fail. In the latter case, the default
printer (write/1
) will print the Term.
portray_clause(
?Clause)
portray_clause(
+Stream,
?Clause)
Writes the clause Clause onto Stream
exactly as 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
% removing redundant module prefixes
and binding variables to terms of the form '$VAR'(
N)
yielding friendlier variable names.
write_term(
+Term,
+Options)
[ISO]
write_term(
+Stream,
+Term,
+Options)
[ISO]
Same as write/[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 list of character codes,
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(
N)
format(
+Format,
:Arguments)
format(
+Stream,
+Format,
:Arguments)
Prints Arguments onto Stream according to format Format. Format is a list of formatting characters or character codes. If Format is an atom then is will be used to translate it into a list of character codes. Thus:
| ?- format("Hello world!", []).
has the same effect as
| ?- format('Hello world!', []).
no matter which value the double_quotes
Prolog flag has.
format/2
and format/3
is a Prolog interface to the C
stdio
function printf()
. It is modeled after and
compatible with Quintus Prolog.
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!', []).
will result in
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!', []).
will result in
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!', [0'x]).
and
| ?- format('Hello~*cworld!', [4,0'x]).
both produce
Helloxxxxworld!
The following control sequences are available.
~a
~
Nc
~
Ne
~
NE
~
Nf
~
Ng
~
NG
printf()
function as
printf("%.Ne", Arg) printf("%.NE", Arg) printf("%.Nf", Arg) printf("%.Ng", Arg) printf("%.NG", Arg)
respectively.
If N is not supplied the action defaults to
printf("%e", Arg) printf("%E", Arg) printf("%f", Arg) printf("%g", Arg) printf("%G", Arg)
respectively.
~
Nd
| ?- format('Hello ~1d world!', [42]). Hello 4.2 world! | ?- format('Hello ~d world!', [42]). Hello 42 world!
~
ND
~
Nd
except that ,
will separate groups of three
digits to the left of the decimal point. Example:
| ?- format('Hello ~1D world!', [12345]). Hello 1,234.5 world!
~
Nr
a-z
will denote digits larger
than 9. Example:
| ?- format('Hello ~2r world!', [15]). Hello 1111 world! | ?- format('Hello ~16r world!', [15]). Hello f world!
~
NR
~
Nr
except that the letters A-Z
will denote digits
larger than 9. Example:
| ?- format('Hello ~16R world!', [15]). Hello F world!
~
Ns
| ?- format('Hello ~4s ~4s!', ["new","world"]). Hello new worl! | ?- format('Hello ~s world!', ["new"]). Hello new world!
~i
| ?- format('Hello ~i~s world!', ["old","new"]). Hello new world!
~k
write_canonical/1
(see Term I/O). Example:
| ?- format('Hello ~k world!', [[a,b,c]]). Hello .(a,.(b,.(c,[]))) world!
~p
print/1
(see Term I/O). Example:
| ?- assert((portray([X|Y]) :- print(cons(X,Y)))). | ?- format('Hello ~p world!', [[a,b,c]]). Hello cons(a,cons(b,cons(c,[]))) world!
~q
writeq/1
(see Term I/O). Example:
| ?- format('Hello ~q world!', [['A','B']]). Hello ['A','B'] world!
~w
write/1
(see Term I/O). Example:
| ?- format('Hello ~w world!', [['A','B']]). Hello [A,B] world!
~@
| ?- format('Hello ~@ world!', [write(new)]). Hello new world!
~~
~
. Example:
| ?- format('Hello ~~ world!', []). Hello ~ world!
~
Nn
| ?- format('Hello ~n world!', []). Hello world!
~N
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.
~
N|
~
N+
~
Nt
`
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 * *************************************************************