Next: ECLiPSe Macros
Up: Input and Output
Previous: In-memory Streams
  Index
Subsections
Modifying the Output
There are several possible ways to modify the standard way of outputting
terms in ECLiPSe.
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
A depth limit can be sepcified for the printed term by giving an
integer immediately after the % in the format string.
Using those options, 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]).
The output_mode flag
The flag output_mode, set by set_flag/2,
is a string of control characters (as recognised by the %w format
of printf/3).
It is used to specify the format in which the system prints terms
- in the debugger (the o command allows some modifications
of the output_mode flag from within the debugger)
- when printing the answer bindings in the top-level loop
For example, calling set_flag(output_mode, "O.P")
will cause the debugger to write the traced goals without operators,
with dot notation for lists and using the user-defined
predicate portray/1, 2.
The default value is "QPm".
When
:- set_flag(syntax_option, '$VAR').
is set,
terms of the form '$VAR'(N) are printed in a special way by all the predicates
that obey operator declarations (i.e. write, writeq, print and partly printf).
'$VAR'(0) is printed as A, '$VAR'(25) as Z, '$VAR'(26) as A1 and so on.
When the argument is an atom or a string, just this argument is printed.
This option is also used for Quintus compatibility mode.
The print/1, 2 Predicate
When print/1, 2 is used to print a term, the user-definable
predicate portray/1, 2 is called on all its nonvariable subterms
and if it succeeds, it is assumed that it has printed the term,
otherwise the term is printed in the standard way.
The portray/1, 2 predicate is also invoked by printf/2, 3
when the option P is used.
Note that portray/1, 2 is also invoked to print metaterms,
but it is not invoked for variable subterms, unless
the option U in printf/2, 3 or in the output_mode
flag is used.
Next: ECLiPSe Macros
Up: Input and Output
Previous: In-memory Streams
  Index
1999-08-06