next up previous index
Next: The output_mode flag Up: Modifying the Output Previous: Modifying the Output

The printf/2, 3 Predicate

  This predicate subsumes all other output predicates, and it also offers formatted printing. Its syntax is similar to the C printf(3) function, but it also has further Prolog-specific options. For example, the sequence

write('The result is '),
writeq(T),
write(', which is '),
write(P),
write('% better than '),
write(R),
nl,
flush(output).
can be written with
printf("The result is %q, which is %d%% better than %w\n%b", [T, P, R]).
In the printf/2,3 predicate, several options for printing Prolog terms can be specified by using the following option characters in the %w format string:
O
ignore operator declarations
D
disregard depth limit for nested terms
.
print lists as ./2 structures
Q
print quotes around functors when needed
v
print variables as unique numbers, e.g. _g123
V
print variables as names and numbers, e.g. X_g123
P
use portray/1,2 if defined
U
use portray/1,2 even for variables
m
print metaterm attributes using user-defined handlers
M
print metaterm attributes in a standard form
G
print term as a goal, i.e. apply goal write macros
T
don't apply write macro transformations

Using those, the other I/O predicates can be defined in terms of printf/2 as follows:          
write(X)           :- printf("%w",    [X]).
writeq(X)          :- printf("%QDTMvw", [X]).
print(X)           :- printf("%Pw",   [X]).
display(X)         :- printf("%O.w",  [X]).
write_canonical(X) :- printf("%O.QDTMvw", [X]).


next up previous index
Next: The output_mode flag Up: Modifying the Output Previous: Modifying the Output



Micha Meier
Mon Mar 4 12:11:45 MET 1996