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
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.
)causes the end of Stream to be reached, Term is unified with the term
end_of_file. Further calls to
read/2for 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/[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_errorsProlog flag. The default is set by that flag.
false. If selected, any occurrences of
@/2in 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.
, if no line number information is available for X.
false. If this option is
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
sicstusexecution mode, and it is
| ?- 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
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.
). The term will be written according to the standard syntax. The output from
write_canonical/2can be parsed by
read/2even if the term contains special characters or if operator declarations have changed. Same as
), 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
user:portray/1. If this succeeds then it is assumed that Term has been output.
print/2is called recursively on the components of Term, unless Term is atomic in which case it is written via
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 will not be called on all the tails
of the list.
write/1) will print the Term.
listing/[0,1]would have written it. Same as
, [quoted(true),numbervars(true),indented(true)])followed by a period and a newline and binding variables to terms of the form
)yielding friendlier variable names.
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
falseis the default).
user:portray/1is called for each subterm.
)where N is an integer >= 0, an atom, or a code-list, are treated specially (see
@/2notation, as discussed above.
format/[2,3]character sequences for printing floats. The default is `~H'.
| ?- 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
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', ). Hello 4.2 world! | ?- format('Hello ~d world!\n', ). Hello 42 world!
| ?- format('Hello ~1D world!\n', ). Hello 1,234.5 world!
| ?- format('Hello ~2r world!\n', ). Hello 1111 world! | ?- format('Hello ~16r world!\n', ). Hello f world!
| ?- format('Hello ~16R world!\n', ). 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 * *************************************************************