Project "Contraintes" Prolog Web Pages: IO in standard Prolog.
A major change in the Prolog language that is aprt of the standard
was the introduction of a completely new set of predicates for input and output.
In the standard these replace the see and tell predicates of
This page summarises the new predicates. A more detailed description of the I/O predicates is available with these pages.
The new I/O predicates introduce the notion of a stream. Streams are associated by a call to the open/4 predicate. sources
(sinks) from (to) which IO is done. Streams can be identified either
by a stream-term which is an implementation dependent ground term
created as a result of a call to open/4 or by use of an alias which is assigned by the call to open/4.
An input/output mode is an atom which defines in a call of open/4 the operations that may be performed on a source/sink. The required modes, which have the obvious meanings are:
Two streams are predefined and open during the execution of any goal:
At any time there is a current input stream and a current output stream,
by default these are initially the standard input stream and the standard output stream respectively. Predicates are provided for changing the current input or output stream.
- the standard input stream, and
- the standard output stream.
The standard makes a distinction between text streams and binary streams,
with distinct predicates for
For somke implementations it may be possible to set the position on a
source/sink. Whether this is possbile for a given stream is determined by the
call to open/4 and whether the appropriate stream option is set. A standard conforming program shall make no assumptions about the form of a stream position term except thst it is ground and uniquely identifies a
position in the stream.
At the end of a stream a specific value shall be returned to indicate that the end of the stream has been reached. The terms indicating end-of-stream and
past-end-of-stream shall be implemenetation defined.
When a strem is opened using open/4 the fourth argument specifies a list of options for the stream so created. The following options are requied by the standard.
A close option modifies the effect of close/2
if an error condition occurs while attempting to close a stream.
The required options are:
- Specifies whether the stream is a text stream or a binary stream: T may be:
The default is text.
- text -- a text stream
- binary -- a binary stream
- If Boolean is true then it shall be
possible to reposition the stream, else if it is false it is implementation defined whether it is possible to reposition.
- Specifies that the atom A shall be an
alias for the stream.
- This determines the effect of attempting to input form a stream whose stream position is past-end-of-stream. The required values for Action are:
It is implementation defined which Action is the default.
- error -- A permission error shall be raised;
- eof_code -- The result of input shall be as if the stream
position were end-of-stream;
- reset -- The stream position shall be rest to end-of-stream
and another attempt made to input form the source to
which the stream is attached.
The properties of a stream can be discovered using the predicate stream_property(Stream, Property). The stream properties shall include:
- force(false) -- The default,
if there is an error the stream is not clsed.
- force(true) -- If a Resource Error or
a System Error
is satisfied, there shall be no error; instead the stream is closed and the goal
A read-options list is a list of options which affects the behaviour of read_term/3.
The read options shall include
- file_name(F) -- When the the stream is connected to a
source/sink which is a file, F shall be an implementation
defined term which identifies this file.
- mode(M) -- M is unified with the
input/output mode which was specified when the source/sink was opened.
- input -- the stream is connected to a source.
- output -- the stream is conencted to a sink.
- alias(A) -- If the stream has an alias then A shall be that alias.
- position(P) -- If the stream has a a reposition property, P shall be the current stream position,
- end_of_stream(E) If the stream position is end-of-stream then E is unified with at else if the position is past-end-of stream it is unified with past, else it is unified with
- eof_action(A) -- A is unified with the eof_action for the stream;
- reposition(Boolean) If repositioning is possible
for the stream then boolean is unified with true else it is unified with false
- type(T) -- T is unified with text or binary depending on the type of the stream.
A write-options list affects write_term/3.
These shall include:
- variables(Vars) -- After inputting a term
Vars is a list of the varialbes in the term that was input, in left-to-right order.
- variable_names(VN_List) -- After a term is input
VN_List is unified with a list of elements each of which has the form
A = V and V is a named variable of the term,
A is an atom whose name is the characters of V.
- singletons(VS_List) -- when a term is inout,
VS_List shall be unified with a list of elements of the form
A = V where V is a named variable of the term,
which occurs only once in the term and A is an atom whose name is the characters of V.
Many prologs contain a predicate numbervars/3 that causes variables
appearing in terms to be converted to this special form.
Live examples of the write options.
- quoted(Boolean) -- If Boolean is true each atom and functor is quoted if this would be necessary for the term to be
correctly input by read_term.
- ignore_ops(Boolean) -- If Boolean is true
each compound term is output in functional notation.
- numbervars(Bool) -- If Boolean is true then a term of the form '$VAR(N)' where N is an integer is output
as a variable name consiting of a capital letter possible followed byu a letter.
The scheme works as follows
| "$VAR(0)'|| is written as || A
| "$VAR(1)'|| is written as || B
|| "$VAR(25)'|| is written as || Z
| "$VAR(26)'|| is written as || A1
|| "$VAR(27)'|| is written as || B1
| "$VAR(52)'|| is written as || A2
Return to Home Page of the INRIA ISO Prolog Web
Author: J.P.E. Hodgson
Saint Joseph's University
Philadelphia PA 19131
Last Changed: 1 janvier 2003