4.17.1 Predefined stream aliases
Each thread has five stream aliases: user_input
,
user_output
, user_error
, current_input
,
and
current_output
. Newly created threads inherit these stream
aliases from their parent. The user_input
, user_output
and user_error
aliases of the main
thread are
initially bound to the standard operating system I/O streams (stdin,
stdout and stderr, normally bound to the POSIX file
handles 0, 1 and 2). These aliases may be re-bound, for
example if standard I/O refers to a window such as in the swipl-win.exe
GUI executable for Windows. They can be re-bound by the user using
set_prolog_IO/3
and set_stream/2
by setting the alias of a stream (e.g,
set_stream(S, alias(user_output))
). An example of rebinding
can be found in library library(prolog_server)
, providing a telnet
service. The aliases current_input
and current_output
define the source and destination for predicates that do not take a
stream argument (e.g., read/1, write/1, get_code/1,
... ). Initially, these are bound to the same stream as user_input
and
user_error
. They are re-bound by see/1, tell/1, set_input/1
and
set_output/1.
The current_output
stream is also temporary re-bound by with_output_to/2
or format/3
using e.g.,
format(atom(A), ...
. Note that code which explicitly writes
to the streams user_output
and user_error
will
not be redirected by with_output_to/2.
Compatibility
Note that the ISO standard only defines the user_*
streams. The‘current’streams can be accessed using current_input/1
and
current_output/1.
For example, an ISO compatible implementation of
write/1
is
write(Term) :- current_output(Out), write_term(Out, Term).
while SWI-Prolog additionally allows for
write(Term) :- write(current_output, Term).