10 Prolog I/O

Most programs need to communicate with the outside world. There are a wide variety of outside entities with which a program can communicate, ranging from the screen, keyboard, and files to other programs over networks. This section describes the facilities which ALS Prolog provides for input/output (I/O) communication. The initial sections describe the fundamental stream-based communication facilities of ALS Prolog. Following this, the earlier so-called DEC-10-style facilities are described; these are implemented in terms of the stream-based facilities. Except for the most primitive aspects, the I/O system for ALS Prolog is entirely implemented in ALS Prolog itself. Almost all of this code is contained in the following builtins files: sio.pro, sio_wt.pro, sio_rt.pro, and blt_io.pro.

10.1 Streams, Sources, and Sinks.

No matter how sophisticated the point of view one chooses to take, at bottom, computer communication is based on the notion of sequences of characters. Such sequences are generated by typing on keyboards, can appear on screens, can be recorded in files, can be transmitted across networks, etc. These sequences of characters are called streams, or, when more precision is necessary, character streams.

The word ‘stream’ is used in this context both because it conveys a sense of motion and because it also conveys a sense of direction, as with the natural notion of stream illustrated in the Figure below (Natural Streams).

Figure. Natural Streams.

In addition, natural streams have a notion of source (the ‘headwaters’) and sink or ultimate destination (the ‘estuary’). Finally, a natural stream also conveys the notion of a point on the bank where one can stand and watch the stream flow by. All of these natural notions have corresponding concepts in the computer notion of character streams.

From the logical point of view, the notions of stream, source, and sink are fundamental notions. However, as indicated above, a stream is a finite or potentially infinite sequence of characters. Every stream is associated with a source and a sink. When a stream is manipulated by a program, the program itself is either the source or the sink of the stream. If the program is consuming the stream, the program is then the sink for the stream. If the program is producing or generating the stream, the program is then the source of the stream. (It is also possible for the same program to be both source and sink for a stream.)

When the program is the sink for the stream, in general some other entity in the computing environment is the source for the stream. Possible external sources include files, keyboards, devices such as tapes cd-roms, and other programs communicating with the program in question via interprocess communication facilities, either locally or remotely. When the program is the source for the stream, some other entity is normally the sink for the stream. In this case, the normal possible external sinks include files, screens (or windows on screens), devices, and again, other programs. The notions of source and sink correspond to the notion of direction of flow for natural streams. The characters in a computer stream flow from the source to the sink.

Streams always have a beginning, but have an end only if they are finite. In prinicple, there is a sense of location, called the stream position, for all streams. This sense of location, or stream position, corresponds to the natural notion of the point on the bank of the stream where one stands and watches the stream flow by. In the natural world, one can sometimes change the stream position by running along the bank, thereby either viewing an earlier portion of the stream (the water) or advancing to a later portion. Some kinds of character streams allow this sort of repositioning, while others do not.

A fundamental principle underlying the notion of stream is this:

    A program manipulating a stream need have no knowledge 
    of what lies at the other end of the stream.

Thus, a program which is the source of a stream (is producing a stream) need have no knowledge of what is consuming the stream, and a program which is the sink for a stream (is consuming a stream) need have no knowledge of what is producing the stream.

Internally, a stream consists of an open source or sink of characters (e.g., a file, a socket, a window, etc.), a pointer to the next place to read or write, and a buffer for holding information until it’s reasonable to transmit. The internal components illustrated in the Figure below are the following:

Figure. A Look Inside a Stream to a File.

The operations for dealing with streams fall into three groups:

The only time a program must concern itself with what is at the other end of the stream is when it must open or create the stream. At this time, the program must specify what is to be at the other end of the stream. That is, it must specify what is to be the source or the sink for the stream. Thereafter, whether manipulating or closing the stream, the program need not worry about the nature or identity of the source or sink at the other end of the stream. It is the job of the underlying implementation of the stream facilities (provided by ALS Prolog) to take care of all of the details of moving the character stream between the program and the source or sink.

Under normal conditions, every ALS Prolog program automatically has two I/O character streams automatically opened for it by the underlying ALS Prolog system. These are called the default I/O streams. One of them is the default input stream and is normally connected to the keyboard, while the other is the default output stream, and is normally connected to the computer screen, or to a particular window on the screen when running under a graphical operating system. These will be discussed in more detail in a later section. All other streams which the program seeks to manipulate must be explicitly opened by the program, and should also be closed by the program when they are no longer required.
Under normal circumstances, all streams which remain open when an ALS Prolog program exits to the operating system are automatically closed.

All streams have both a mode and a type. The mode of a stream basically reflects the direction of flow of information in the stream. The two fundamental stream modes are read and write. In read mode, the program is the consuming the stream’s information, so that the program is the sink for the stream. In write mode, the program is producing the information on the stream, so that the program is the stream’s source. Other modes are possible and will be discussed later.

The type of a stream reflects deeper information about the computing environment. The great majority of computing systems and programming languages (including earlier versions of Prolog and ALS Prolog) identify characters with bytes. However, this does not provide good support for alphabets with larger numbers of characters, and so a distinction is made between characters and bytes in the Prolog standard. Characters are internally represented as Prolog atoms, while bytes, as normally done, are represented by small integers. At bottom, a computer really moves bytes around, not characters. So at bottom, what a program thinks of as a stream of characters is really a stream of bytes. Since there is now a distinction between the notions of characters and bytes, a program can choose to view a data stream as either a stream of characters or as a stream of bytes. The type of a stream indicates this distinction. A text stream is a stream that is being viewed as a stream of characters. A binary stream is a stream that is being viewed as a stream of bytes. Facilities are provided for opening streams either as text or binary, and for manipulating them in both modes.

10.2 Opening and Closing Streams.

To open (or create) a stream, a program must indicate a source/sink for the stream (the other end from the program), must indicate a mode for the stream, and possible should indicate some options for the creation of the stream. Moreover, the process of opening or creating the stream should provide the program with some descriptor or handle with which to refer to the created stream for future manipulation.

10.2.1 Stream opening predicates.

There are two predicates used for opening streams:

open(SourceSink, Mode, Descriptor)
open(+, +, -)

open(+, +, -, +)

The three-argument version is simply definable in terms the four-argument version by:

open(SourceSink, Mode, Descriptor)
    :- open(SourceSink, Mode, Descriptor, []).

The arguments for open/4 are described below.

10.2.2 SourceSink Terms.

A sourcesink term describes a target ‘other end’ of a stream. (The Prolog program of course holds on to ‘this end’ of the stream.) The particular sourcesink terms correspond to the various kinds of entities which can act as sources or sinks.


If the target source or sink is to be a file, the sourcesink term is a Prolog atom which is a name for the file, possibly including path information. (And conversely, an atom in the sourcesink position can only indicate a file as target source or sink.) As with consult/1, the file name may be either an abolute path name or a relative path name. (On most operating systems, the ‘raw’ keyboard and screen are handled more or less as special files. They will be discussed later.)


Prolog strings (lists of characters) are acceptable sources and sinks for streams. When used as a source, a Prolog string S must be ground (fully instantiated), while when used as a sink, the string S must be an uninstantiated variable (which could be the tail of a larger list). In these cases, the sourcesink term is of the form


When used as a sink, a string S (initially an uninstantiated variable) is viewed as a potentially infinite stream, which grows as characters are written to it.

Atoms and UIAs.

Atoms and UIAs are also acceptable sources and sinks for streams. In this case, the sourcesink term is of the form


where A is the atom or UIA in question. When used as a sink, the atom or UIA A is a stream of finite length, and any characters initially contained in A are gradually overwritten by the output process.


Sockets may also be used as source or sinks. The sourcesink term is of one of the following two forms:

socket(Target)     socket(DescriptionList)

In the first case, Target is any atom. This first case is a shorthand for a particular instance of the second case, namely,


In the second case, which is the general case, DescriptionList is a list of equations, each of which is of the form

Tag = Value.

The possible values Tag may take on are:

domain type protocol port target.

The expressions which Value may take on are determined by the value of Tag. The only required tag which must appear is the target tag. Default values are supplied for all other tags if no equation is present. The socket tags, the range of corresponding values, and their defaults, are described below.

domain: values = [af_unix, af_inet]; default = af_unix

type: values = [sock_stream, sock_dgram]; default = sock_stream

protocol: values = [0, ip, icmp, tcp, udp]; default = ip

    values = any acceptable port number; 
    default: sock_stream = 1599; sock_dgram = 1598.

    values = an atom which is an acceptable machine name;

When the socket is a read (incoming) socket, the null atom ‘’ can be used. Here are several simple examples:

socket([target='', domain=af_inet, type=sock_stream])
socket([target=jarrett, domain=af_inet, type=sock_dgram])


When running the Tcl/Tk windowed version of ALS Prolog, text windows are acceptable sources and sinks for streams. In this case, the sourcesink term is of the form

tk_win(Interp, Name)

where Interp is the Tcl/Tk intepreter under which the text window has already been created, and Name is an atom which is a name for the target window (see the Sections on use of the Tcl/Tk interface in the Development Tools part of the manuals).

Null Streams.

For various design reasons, it is sometimes convenient to utilize null streams: i.e., virtual emtpy or infinite streams which are not attached to any system resource. Such streams are analogous to the erzatz device /dev/null on Unix. These null streams are availabe on all computing platforms in ALS Prolog, and are available both for output and for input. To utilize a null stream for output, execute the goal

open(null_stream(Name),write, S, Options),

where Name is an atom (which is not taken as an alias for the stream). Then arbitrarily much output can be written to the stream S with no effect on computing resources. Similarly, to utilize a null stream for input, execute

open(null_stream(Name), read, S, Options),

where Name is an atom. Then any input operations from stream S will always encounter the end_of_file condition without errors.

URLs: Internet Streams.

URLs may be used as a source or sink for a stream. When is a proper url, and is a list of Curl options as described in Section 10.11,

url(<URL>, <CurlOptionList>)

is a sourcesink term. The expression url() is equivalent to url(, []).

A stream S opened with

open(url(<URL>, <CurlOptionList>), read, S, <possible generic stream options>)

will contain the characters returned by a GET to , conditioned by and . A stream S opened with

open(url(<URL>, <CurlOptionList>), write, S, <possible generic stream options>)

will capture all characters written to S (write,nl,printf, etc.), and when S is closed, the sequence of characters in S will be POSTed to , conditioned by . WARNING: The implementation of open(url(, ), write, S, …) utilizes the freeze construct. Everything will work correctly so long as no gc (garbage collection) takes place; otherwise, Issue #87 will cause incorrect and unpredictable behavior.

For both read and write url streams, the Curl options available are exactly the same as for http/3 or curl/1-3 in Section 10.11.


?- open(url('http://example.com/',[response_code(RC),total_time(TTT),result=_]), read, _,[alias(myStream)]), get_line(myStream,L),close(myStream).

L='<!doctype html>'


?- open(url('https://postman-echo.com/post', []), write, S, []), write(S, abcdef), close(S).



10.2.3 Immediate versus Delayed Streams.

A file sourcesink is said to be immediate in the sense that (normally) all of the characters (data) which will make up the stream are immediately available once the stream is opened to the file. In contrast, a socket sourcesink is called delayed in the sense that access to some (possibly) all of the characters making up the stream may be delayed to the consuming process. We say that a stream is either immediate or dealyed if its corresponding sourcesink is immediate or delayed, respectively. This distinction is of particular significance for term-level I/O reading from this stream, but also has effects for character-level consumption of a delayed stream. Consider the situation sketched in the Figure below .

Figure. A Delayed Stream in a Delayed State.

When the Prolog system attempts to fill the read buffer (on the left) in response to the read_term call, it is unable to obtain any characters from the underlying OS machinery. If the sourcesink for the reading processes were a file, this situation would be interpreted as end of file. However, that is not what we want to do for a socket (for example), since the writing process will presumably fill and/or flush the buffer sooner or later, and more characters will become available to the reading process. The refinements we introduce for this situation are detailed below.

Character Input from Delayed Streams.

either returns (the code of) a character, which is a non-negative integer, or returns -1 to signify end of file. We extend get_char/2 to return -2 when the stream is a delayed stream which is still open, the stream has not reached end of stream, but no characters are available for processing. get_char/2

Term Input from Delayed Streams.

The behavior of read_term/3 for a delayed stream is governed by the read option blocking(Bool), where Bool is either true or false. Note that the default behavior is blocking(true) – a blocking read. Under the default blocking(true) option, read_term/3 behaves just as it does for immediate streams. The difference in behavior occurs for delayed streams. Suppose that the situation in the Delayed Streams Figure (above) came about as follows. The read_term(…) goal was initiated, and at that time, some charazcters were available. But before read_term can finish parsing a complete Prolog term, the characters are consumed. If the stream were an immediate stream, we only run out of characters at end of stream, and in that case, the system would raise an exception, indicating that end_of_file (if it were a file) was encountered improperly.

However, when the stream S is a delayed stream which is in the delayed state of Delayed Streams Figure (above) , the goal

read_term(S, T, [blocking(false)]),

succeeds, and binds T to the distinguished term


The tokens which were read out of the stream’s buffer in attempting this read_term are saved in the stream data structure. Subsequent calls

read_term(S, T, [blocking(false)]),

will attempt to again read from the stream, beginning with the tokens which were saved, and then continuing with any (possibly) new characters which have arrived since the last unfinished read.

10.2.4 Modes.

There are four possible modes for a stream:


However, only file streams support the read_write and append modes at the present time; all other streams only support the read or write modes. The mode names rather clearly indicate the manner in which the streams to which they apply will be used:

10.2.5 Options.

The options argument of open/4 is a list whose elements are acceptable stream open options. These provide additional information to the open/4 procedure for refined control of the stream being created. These latter are differnt sorts of Prolog terms. Some of the options are applicable to all streams, while others apply only to streams connected to particular kinds or sources or sinks, or of particular modes or types.

text vs binary

At most one of text or binary can be present on the options list. When text is present, the stream is treated as a character stream. When binary is present, the stream is treated as a byte stream. If neither is present, the stream is treated as a character stream, so that the default is text.


An alias is an atom which acts as a global name for the stream to be opened. Once a stream has been opened, the stream descriptor (returned in the third argument of open/3 and the fourth argument of open/4) can be passed around between procedures and used during stream manipulations (typically reads and writes). However, if an alias is assigned to the stream, the stream descriptor which is returned by open/[3,4] can be ignored, and all stream manipulations (e.g. reads and writes) can refer to the stream by using the alias instead of the stream descriptor. To indicate that Atom is to be an alias for a stream to be opened, the expression


should be included on the options list of the call to open/4 which creates the stream. (There are also procedures for dynamically assigning an alias to a stream after stream creation; these will be discussed later.)


Some streams can be repositioned during program execution. These options indicate the type of repositionion which is requested for the stream. Not all streams support such repositioning. It the stream to be opened does not support the requested repositioning, the repositioning request generates an exception from open/4.. There are two types of repositioning requests:


The first indicates that the stream should support repositioning to any meaningful position (The ISO standard only requires that the stream should support repositioning to any previously occupied position). The second indicates simply that repositioning is not requested.


These options allow the programmer to control the coordination between the stream’s buffer and the ultimate external source or destination of the data. These options are most meaningful for file streams, but also have some significance for some other kinds of streams. A buffering option is indicated by a term of the form


where BOption is one of the following three atoms:


These options determine how often data is moved between the stream buffer and the source or destination. The first, byte, indicates that data should be moved on every character or byte (according as the stream is text or binary) handled by the program; in essence, this specifies no buffering should be used. The second, line, indicates that data should be moved on a line-by-line basis. The third option, block, essentially indicates that data should be moved in the largest units possible, as determined by the buffer size of the stream, and the block or buffer size of the external source or sink. The default is block buffering since it is generally the most efficient.


This option allows the programmer to control the size of the buffer assigned to the stream. The option expressed by a term of the form


where N is an integer (which should be a power of 2). The default is 1024. The maximum value is determined by the largest contiguous area available on the Prolog heap.


This option is useful when a pair of streams, one in read and one in write mode, are being used for interactive communication, say to a window, or to the keyboard and screen. However, this option strictly applies only to a single stream, and is meaningful only if the stream is in read mode. The option is of the form


where Goal is a Prolog term which indicates a goal which can be run. Whenever the buffer of the stream to which this option applies (which must be in read mode) is empty, before attempting to refill the stream’s buffer from the external source, the system will first execute the goal Goal. Here is how this option is normally used. Suppose we wish to use a read mode and write mode stream to window myWin, and that whenever the read stream buffer is empty, we want a prompt to be printed on the window. Consider the following code fragment:

open(window(myWin),write, [buffering(line),text], OutStream),
open(window(myWin), read, [prompt_goal(user_prompt_goal(OutStream))], InStream),

Both streams are opened with line buffering; the write stream is explicitly indicated to be a text stream, while the read stream is a text stream by default (this is just by way of example). In addition, the read stream has a prompt_goal option applied. Whenever the buffer of the read stream is empty, the system will first run the generic goal

get_user_prompt(Prompt), put_atom(Stream,Prompt), flush_output(Stream).

In this case,


The code defining this goal could be:

    :- put_atom(Stream, '>>'),

Here the desired prompt is ‘»’ . The I/O routines put_atom/2 and flush_output/1 will be described in later sections.


[Not yet implemented.] For a stream which is opened in read or read_write mode, this option indicates what action the system should take if the program attempts to read beyond the end of the stream. The option is expressed by a term of the form


where Action is one of the following atoms:


The interpretations of these action options are as follows:


This option allow the programmer to control which end-of-line (eoln) characters are output by nl/1. A write end-of-line option is indicated by a term of the form


where Type is one of the following three atoms:


These options determine what characters are output by nl/1. The first, cr, indicates that a carriage return (“\r”) should be output. The second, lf, indicates that a line feed (“\n”) should be output. The third, crlf, indicates that a carriage return followed by a line feed (“\r\n”) should be output.

The default for this option varies depending on the operating system being used. MacOS uses cr, unix systems use lf, and MS DOS and Win32 use crlf.


This option allows the programmer to control which characters should be used to detect an end-of-line (eoln) by read/2 and get_line/3. A read end-of-line option is indicated by a term of the form read_eoln_type(Type) where Type is one of the following four atoms: cr lf crlf universal These options determine what read/2 and get_line/3 recognize as an endof-line. The first, cr, indicates that a carriage return (“\r”) should be interpreted as an end-of-line. The second, lf, indicates that a line feed (“\n”) should be interpreted as an end-of-line. The third, crlf, indicates that a carriage return followed by a line feed (“\r\n”) should be interpreted as an end-of-line. The fourth, universal, indicates that any of the end-of-line types (cr, lf, crlf) should be interpreted as an end-of-line. The default is universal since this allows the correct end-of-line interpretation for text files on all operating systems.

Socket options

connects(N) - N is the number of connection requests which can be queued by the system while it waits for the server to execute the accept system call; the default value for N is 1; on most systems, the maximum is 5.

10.2.6 Stream descriptors.

A stream descriptor is the term which is returned when a stream is opened (in the third argument of open/3 and the fourth argument of open/4). It is a complex Prolog term. However, programmers should not attempt to ‘look inside’ of it nor attempt to rely on any of its structure. Appropriate predicates are supplied (see the following sections) for accessing and updating it as necessary. A stream descriptor is what the Prolog standard describes as implementation dependent. As such, implementations are free to change the structure and nature of such terms. Conceivably, this could happen to stream descriptors in future releases of ALS Prolog. The stream descriptor is used simply as a means of referring to the stream in the predicates described in the following sections.

10.2.7 Closing Streams.


Streams are very easily closed. One simply uses the unary predicate close/1 applied either to the stream descriptor or to an alias for the stream:


10.3 Stream Environment.

The stream environment of an ALS Prolog program is made up of the collection of streams which are open, together with the special roles which have been assigned to some of them. This section describes this environment together with predicates for querying and altering its state (besides the basic predicates for opening and closing streams described in the last section), together with predicates for querying the state(s) of individual streams.

10.3.1 Standard Streams.

Two streams are automatically opened for every ALS Prolog program. Both are text streams, and one is in read mode while the other is in write mode. The read mode stream is automatically assigned the alias user_input while the write mode stream is automatically assigned the alias user_output. In addition, for compatibility with older versions of Prolog, both streams are (ambiguously) assigned the alias user.

When ALS Prolog is run as a TTY-style program under operating systems such as Unix or DOS, the read mode stream user_input is connected to the process’s standard input (stdin), while the write mode stream user_output is connected to the process’s standard output (stdout). When ALS Prolog is run in one of its windowing versions (e.g., Motif), both user_input and user_output are connected to the ALS Prolog Worksheet window.

10.3.2 Current Streams.

No matter what streams have been opened (whether automatically or by the program), ALS Prolog always maintains a notion of the current input and output streams. The current input and output streams serve as defaults for all of the input/ ouput operations to be described in the following sections. Thus, if a a version of an I/O operation is used without a stream argument (e.g. a read or a write without a stream argument), the operation is applied to the appropriate current input or output stream.

When ALS Prolog starts up, the current input and output streams are automatically set to the standard input and output streams. However, the program can change the settings of the current input and output streams. The following predicates are used for manipulating the current streams.


The goal


is true iff the stream Stream is the current input stream. Operationally, this means that current_input unifies Stream with the stream descriptor of the current input stream. Note that this means that current_input applies to stream descriptors. In particular, even if Alias is an alias for the current input stream, calling current_input(Alias) will fail.


The goal


is true iff the stream Stream is the current output stream. Operationally, this means that current_output unifies Stream with the stream descriptor of the current output stream. Note that this means that current_output applies to stream descriptors. In particular, even if Alias is an alias for the current output stream, calling current_output(Alias) will fail.


set_input/1 is used to set the current input stream. If Stream_or_alias is instantiated to either a stream descriptor of a stream in either read or read_write mode, or is instantiated to an alias for such a stream, then a call to set_input(Stream_or_alias) changes the current input stream to be the stream associated with Stream_or_alias. If Stream_or_alias is inappropriate for any reason, set_input/1 raises an exception. Thus set_input(S_or_a) cannot fail. Either it succeeds or it raises an exception, in which case the current input stream remains unchanged.


set_output/1 is used to set the current output stream. If Stream_or_alias is instantiated to either a stream descriptor of a stream in either write, read_write, or append mode, or is instantiated to an alias for such a stream, then a call to


changes the current output stream to be the stream associated with Stream_or_alias. If Stream_or_alias is inappropriate for any reason, set_output/1 raises an exception. Thus set_output(S_or_a) cannot fail. Either it succeeds or it raises an exception, in which case the current output stream remains unchanged. .

10.3.3 Stream Charcteristics

stream_property(Stream, Property)
stream_property(?, ?)

stream_property/2 is used to determine whether or not a given property applies to a given stream; It can also be used to enumerate or generate the (open) streams possessing a certain property. Declaratively,

stream_property(Stream, Property)

is true if and only if Property holds of Stream. The possible values for Property include all of the expressions which may appear on the Options list passesd to open/4, together with the following:

input, output, and mode(M),

where M is one of text, binary.

From a more procedural point of view, the action of stream_property is as follows. If both Stream is instantiated to the stream descriptor of a currently open stream, and Property is instantiated to a property descriptor which is true of Stream, then

stream_property(Stream, Property)

succeeds. Either or both of Stream or Property may be uninstantiated. In this case,

stream_property(Stream, Property)

is resatisfiable under backtracking. Thus, the action of in this case is effectively to compute the pairs S,P such that S is a currently open stream which has property P, in some undetermined order, and to unify Stream with S and Property with P.

For example, consider the goal

stream_property(S, output).

If S is instantiated to a stream descriptor, this goal will check whether output is permitted on this stream. If S is uninstantiated, under backtracking, S will successively be instantiated to all streams currently open for output.

As another example, consider the goal

stream_property(S, file_name(F)).

If S is instantiated to a stream descriptor, then if the source or sink for S is a file, F will be unified with the name of the file to which S is connected; otherwise, the goal will fail. If S is uninstantiated, but F is instantiated to an atom (which is the name of a file), then if some currently open stream has file F as its source or sink, S will be unified with that stream’s descriptor. Finally, if both S and F are uninstantiated, this goal, under backtracking, will compute all the pairs S,F where F is a file name and S is a currently open stream connected to F.

When used in non-determinate ways, stream_property exhibits a “logical” semantics for state changes of the stream environment. For example, consider the goal

stream_property(S,P), write(S:P), nl, close(S), fail.

This goal will enumerate all the properties for all streams which were open before this goal was run. Note that this example may call close(S) several times for each stream S, but this does not cause any problem since close simply succeeds if called on a stream which is already closed.

is_stream(Stream_or_alias, Stream)
is_stream(+, ?)

Succeeds when Stream_or_alias is a stream or alias and will bind Stream to the corresponding stream.

assign_alias(Alias, Stream_or_alias)
assign_alias(+, +)

If Stream_or_alias is associated with the stream S, and if Alias is a term, associates Alias to S as an alias. Note that a given stream can carry more than one alias, and that Alias can be a compound term.


If Alias is currently associated with stream S as an alias, removes the alias association between Alias and S.

reset_alias(Alias, Stream_or_alias)
reset_alias(+, +)

If Alias is currently associated with stream S as an alias, and if Stream_or_alias is associated with stream S’, first removes the association between Alias and stream S, and then associates Alias to stream S’ as an alias.


Succeeds iff Alias is an alias which is associated with the stream Stream.

10.3.4 Stream Positions


Consider a stream S which has been opened for input. If the stream S is of finite length, it is possible to reach a state in which all the characters or bytes in the stream S have been read by input routines

(such as get_byte, get_code, get_char, or read),

or when set_stream_position/2 has been used to move directly to the end of the stream. At such a point, it is still valid to call an input routine. Each of the input routines returns a specific value to indicate that end of stream has been reached: get_code and get_byte return -1; get_char and read each return the atom end_of_file. When one of these terminating values has been read, the stream is said to be past the end of the stream, while the stream is said to be at the end of stream when no more characters or bytes are available for input, but no such input call has been made.


succeeds if and only the stream associated with Stream_or_alias is either at end of stream or is past end of stream.

The predicate at_end_of_stream/0 determines whether the current_input stream is at end of stream The predicate at_end_of_stream(Stream) still succeeds when called in the past end of stream state. A stream need not have an end, in which case this predicate would never succeed for that stream. If the source for S_or_a is a device such as a terminal, and if there is no input currently available on that device, then at_end_of_stream will wait for input just as get_code would do.


These predicates determine whether a stream is at positioned at the end of a line, in an elementary way. They simply attempt to perform a peek_char on the stream, and determine if the character returned is identical with newline (i.e., the character with code 0’\n). The are defined by:

    :- get_current_input_stream(Stream),

    :- peek_char(Alias_or_stream,0'\n).

If the stream associated with Stream_or_alias is a currently open output stream, then any output which is currently buffered by the system for that stream is (physically) sent to that stream, and flush_output(Stream_or_alias) succeeds.

flush_output/0 flushes the current output stream; it is defined by

    :- current_output(Stream),

If the stream associated with Stream_or_alias is a currently open input stream, then any input which is currently buffered by the system for this stream is discarded, and flush_input(Stream_or_alias) succeeds. flush_input/0 flushes the current input stream; it is defined by

    :- current_input(Stream),
stream_position(Stream_or_alias, Current_position, New_position)
stream_position(+, ?, ?)

If the stream associated with Stream_or_alias supports repositioning, then the call to stream_position/3 causes Current_position to be unified with the current stream position of the stream, and, as a side effect, the stream position of this stream is set to the position represented by New_position. New_position may be one of the following values:

10.4 Byte Input/Output.


Unifies Byte with the next byte obtained from the current input stream.

get_byte(Alias_or_stream, Byte)
get_byte(+, ?)

Unifies Byte with the next byte obtained from the stream associated with Alias_or_stream.


Outputs the byte Byte to the current output stream.


Outputs the byte Byte to the stream associated with Alias_or_stream.

10.5 Character Input/Output.

The predicates in this section perform character-level input and output on streams. While these predicates are primarily inteded for use on streams opened in text mode, they have meaning for streams opened in binary mode, unless otherwise indicated. There are related byte-oriented predicates for streams opened in binary mode. (Note that ALS Prolog is more relaxed than the ISO standard; the latter states that character operations cannot be performed on binary streams, and that byte operations cannot be performed on character streams)


get_char(S_or_a, Char)
get_char(+, ?)

get_char(Char) is equivalent to get_char(S, Char) where S is the current input stream; that is, get_char/1 is effectively defined by:

    :- current_input(Stream),
       get_char(Stream, Char).

Let Stream_or_alias be properly instantiated, and let S be the stream associated with Stream_or_alias. Then if S is a text stream,

get_char(Stream_or_alias, Char)

is true iff Char unifies with the next character to be read from S, while if S is a binary stream, it is true iff Char unifies with the next byte to be read from S. If S is at or past end of stream, the ‘eof action’ associated with S is performed. If S is a delayed stream, and is in the delayed state when the goal is issued, Char is bound to -2.



get_nonblank_char(Stream, Char)
get_nonblank_char(+, -)

get_nonblank_char/1 is defined by

    :- get_current_input_stream(Stream),

get_nonblank_char(Stream, Char) unifies Char with the next nonwhitespace character obtained from the input stream associated with Alias_or_stream, if such a character occurs before the next end of line, and unifies Char with the atom end_of_line otherwise.


get_atomic_nonblank_char(Stream, Char)
get_atomic_nonblank_char(+, -)

get_atomic_nonblank_char/1 is defined by

    :- get_current_input_stream(Stream),

get_atomic_nonblank_char(Stream, Char) unifies Char with the atomic form of the next non-whitespace character obtained from the input stream associated with Alias_or_stream, if such a character occurs before the next end of line, and unifies Char with the atom


otherwise. It is defined by

    :- get_nonblank_char(Stream,Char0),
       (Char0 = end_of_line ->
            Char0 = Char
            name(Char, [Char0])

peek_char(Alias_or_Stream, Char)
peek_char(+, -)

peek_char(Char) unifies Char with the next character obtained from the default input stream. However, the character is not consumed. peek_char(Alias_or_Stream, Char) unifies Char with the next character obtained from the stream associated with Alias_or_Stream. However, the character is not consumed.


put_char(Stream_or_alias, Char)

put_char(Char) is equivalent to put_char(S, Char) where S is the current output stream; that is, put_char/1 is effectively defined by

    :- current_output(Stream),
       put_char(Stream, Char).

If Stream_or_alias is properly instantiated, S is the stream associated with Stream_or_alias, and Char is a character, then

put_char(Stream_or_alias, Char)

outputs the character Char to S, and changes the stream position on S to take account of the character which has been output.



put_string(Alias_or_stream, String)
put_string(+, +)

put_string(String) is defined by

    :- get_current_output_stream(Stream),
       put_string(Stream, String).

If String is a Prolog string (i.e., a list of character codes), put_string(Alias_or_stream, String) recursively applies put_char to String to output the characters associated with String to Alias_or_stream.


Outputs the atom Atom to the stream associated with Alias_or_stream. Very efficient.


Attempts to read a number of the given type InputType from the stream associated with Alias_or_stream, and if successful, unifies the result with Number. The possible values for InputType are:

InputType       Type of Number
=========       ==============
byte            signed byte (8 bit)
ubyte           unsigned byte (8 bit)
char            signed byte (8 bit) (synonymous with byte)
uchar           unsigned byte (8 bit) (synonymous with byte)
short           signed short integer (16 bit)
ushort          unsignged short integer (16 bit)
int             signed integer (32 bit)
uint            unsignged integer (32 bit)
long            signed integer (32 bit)
ulong           unsignged integer (32 bit)
float           floating point (32 bit)
double          floating point (64 bit)

put_number(Alias_or_stream,OutputType,Number) outputs the number Number as OutputType to the stream associated with Stream_or_alias. OutputType may take on the following values:

byte   short   long   float   double.

get_line(Line) is defined by

    :- get_current_input_stream(Stream),
get_line(Stream_or_Alias, Line)
get_line(+, ?)

Reads the current line or remaining portion thereof from the stream associated with Stream_or_Alias into a UIA, and unifies this UIA with Line. If end-of-file is encountered before any characters, this predicate will fail. If end-of-file is encountered before the newline, then this predicate will unify Line with the UIA containing the characters encountered up until the end-of-file.



put_line(Line) is defined by

    :- get_current_output_stream(Stream),

put_line(Stream, Line) is defined by

    :- put_atom(Stream,Line),

skip_line/0 is defined by

    :- get_current_input_stream(Stream),

If Alias_or_stream is open for (text) input, skip_line(Alias_or_stream) skips to the next line of input for the stream associated with Alias_or_stream.


nl is equivalent to nl(S) where S is the current output stream; that is,

nl : -

nl(Stream_or_alias) causes the current line or record on the stream associated with Stream_or_alias to be terminated.

10.6 Character Code Input/Output

These predicates provide a means of directly manipulating streams at the character code level.


get_code(Stream_or_alias, Int)

get_code(Code) is equivalent to get_code(S, Code) where S is the current input stream; i.e., get_code/1 is defined by:

    :- current_input(Stream),

Assume that Stream_or_alias is instantiated to a stream descriptor or to the alias of a stream descriptor, and let S be the stream associated with Stream_or_alias. If S is a text stream then

get_code(Stream_or_alias, Code)

is true iff Code unifies with the character code corresponding to the next character to be read from Stream, else if S is a binary stream it is true iff Code unifies with the next byte to be read from S.


put_code(Code) is equivalent to put_code(S, Code) where S is the current output stream; i.e., put_code/1 is defined by:

    :- current_output(Stream),
       put_code(Stream, Code).
put_code(Stream_or_alias, Code)

put_code(Stream_or_alias, Code) outputs the character with code Code to the stream associated with Stream_or_alias, and changes the stream position on the stream associated with Stream_or_alias to take account of the character which has been output.

10.7 Term Input/Output.

ALS Prolog provides a rich array of predicates for I/O at the term level. The predicates in this section provide means for reading Prolog terms from input streams and for writing Prolog terms to output streams. For both input of terms and output of terms, there are a variety of options which can be requested. These options are controlled through the use of options lists which are passed to the various term-level I/O predicates.

10.7.1 Read options

A read options list is a list of read options, where the possible read options are defined as follows. Let T be the term which is (to be) read from the input stream.

    Vars is unified with a list of the variables encountered 
    in a left to right traversal of the term T.

    VNs is unified with a list of elements of the form
    V=A, where V is variable which occurs in the
    term T and A is the associated name of that variable; 
    the elements V=A occur in the order in which the 
    variables V are encountered in the term T when T is 
    scanned left to right.

    Vars is unified with a list of the variables encountered 
    in a left to right traversal of the term T; Names is a 
    list of the associated names of the variables. '_' is 
    the only variable name which may occur more than once on 
    the list Names.

    Vars is unified with the singleton variables (those
    occuring only once) in the term T; Names is unified 
    with a corresponding list consisting of the names of 
    those singleton variables. Variables with name '_' are 
    excluded from these lists.

    Val indicates how the system is to handle any
    syntax errors which occur durin g the reading of
    T. The possible values of Val and their interpretation are:

        Occurrence of a sysntax error will cause the system 
        to raise an exception, which includes outputing a
        warning message. This is the default.

        Occurrence of a syntax error will cause the attempt 
        to read T to fail, and and error message will be output.

        Occurrence of a syntax error will cause the attempt to 
        read T to fail quietly, with no message output.

        Occurrence of a syntax error will cause the attempt to read 
        T to output an error message, to skip over the offending input 
        charaters, and attempt to re-read T from the source stream.

    For streams, such as socket streams or IPC queue streams, it 
    is possible for the stream to remain open, yet there be no 
    characters available at the time a read is issued. The value 
    of Type controls the behavior of the read in this setting. 
    The values of Type are as follows:

        In this type of read, the read suspends or waits until 
        enough characters are available to parse as a valid Prolog term.

        In this type of read, if no characters are available, the read 
        will immediately return with success, unifying the 'read term argument'
        (which is argument 1 for read_term/2 and is argumen 2 for read_term/3) 
        with the term unfinished_read; any tokens consumed in the read attempt 
        are saved in the stream data structure, and subsequent attempts to read
        from this stream begin with these tokens, followed by tokens created
        from further characters which arrive at later times.

    This option is not handled in satisfy_options/4.  Read terms are considered 
    to be clauses and debugging information is attached. This is likely to
    of little direct use to the application programmer.

    This option determines if a fullstop is added to the tokens comprising a 
    the term to be read. It is most | useful when used in conjunction with atom
    or list streams.
read_term(Term, Options)
read_term(?, +)

read_term(Term, Options) is equivalent to read_term(S, Term, Options) where S is the current input stream; i.e., read_term/2 is defined by:

read_term(Term, Options)
    :- current_input(Stream),
       read_term(Stream, Term, Options).
read_term(Stream_or_alias, Term, Options)
read_term(+, ?,+)

read_term(Stream_or_alias, Term, Options) inputs a sequence TT of tokens from the stream associated with Stream_or_alias until an end token has been read. It is a syntax error if end of stream is reached before an end token is found. TT is then parsed as a Prolog term which is then unified with Term.

A ‘sequence of tokens’ implies that single quotes and double quotes (if included in the standard) are balanced. That is, an apparent end token appearing inside any kind of quotes is not an end token. However, parentheses and square brackets need not be balanced. The effect of this predicate may be modified by clauses of the special user-defined procedure char_conversion/2.


read(Stream_or_alias, Term)

read(Term) is equivalent to read_term(S, Term, []) where S is the current input stream; that is, read/1 is defined by:

    :- current_input(Stream),
       read_term(Stream, Term, []).

read(Stream_or_alias, Term) is equivalent to

read_term(Stream_or_alias, Term, []),

so that read/2 is defined by:

read(Stream_or_alias, Term)
    :- read_term(Stream_or_alias, Term, []).

The following convenience predicates are quite useful:





These are defined as follows:

    :- atomread(Atom,Term,[]).

    :- open(atom(Atom),read,Stream),
       read_term(Stream,Term, [attach_fullstop(true)|Options]),

    :- bufread(String,Term,[]).

    :- open(string(String),read,Stream),
       read_term(Stream,Term, [attach_fullstop(true)|Options]),

10.7.2 Write options.

Just as the reading of terms from input streams can be affected by read options, the writing of terms to ouptut streams can be controlled by write options. The terms written are output in a pretty-printed format which breaks lines before wrapping, and uses indenting for legibility. Also, by default, the variables occurring in a term are represented using identifiers of the forms A, …., Z, A1,….,A2,……. A write options list is a list of write options, which are one of the terms defined below. The default setting for each of these options is indicated in square brackets following each write option term. Let T be the term being written out, and let the expression Bool take on one of the values true or false.

quoted(Bool) [default: Bool = false]
    If Bool = true, forces all symbols in T to be written out in such a manner that 
    read_term/[2,3] may be used to read them back in. Bool = false indicates that 
    symbols should be written out without any special quoting. In the latter case, 
    embedded control characters will be written out to the output device as is.

ignore_ops(Bool) [default: Bool = false]
    If Bool = true, operators in T will be output in function notation (i.e., 
    operators are ignored.) If Bool = false, operators will be printed out 

    not implemented yet

numbervars(Bool) [default: Bool = true]
    If Bool = true, terms of the form $VAR(N) where N is an integer will print out 
    as a letter.

lettervars(Bool) [default: Bool = true]
    If Bool = true, variables occurring in T will be printed out as letters 
    A, B, ...., or letters followed by numbers: A1,...,A2,... If Bool = false, 
    variables will be printed as _N where N is computed via the internal address 
    of the variable. This latter mode will be more suited to debugging purposes 
    where correspondences between variables in various calls is required.

maxdepth(N,Atom1,Atom2) [default: maxdepth(20000,*,...)]
    N is the maximum depth to which to print; Atom1 is the atom to output when 
    this maximum depth has been reached in printing any term. Atom2 is the atom 
    to output when this depth has been reached at the tail of a list.

maxdepth(N) [default: N = 20000]
    Equivalent to maxdepth(N,*,...).

line_length(N) [default: N = 78]
    N is the length in characters of the output line.  The pretty printer will 
    attempt to put attempt to break lines before they exceed the given line length.

indent(N) [default: N = 0]
    N is the initial indentation to use.

    If Bool = true, lists of suitably small integers will print out as a double 
    quoted string. If Bool = false, these lists will print out as lists of small numbers.

depth_computation(Val) [default: Val = nonflat]
    Val may be either flat or nonflat. This setting determines the nature of the pretty 
    printer's "depth in term" computation. If Val = flat, all arguments of a term or list 
    will be treated as being at the same depth. If Val = nonflat, then each subsequent 
    argument in a term (or each sebsequent element of a list) will be considered to be at a 
    depth one greater than the depth of the preceding structure argument (or list element).

line_end(Bool) [default: Bool = true]
    When Bool = true, nl(_) is normal; when Bool = false, line-breaks (new lines) are preceeded by a \ .
write_term(Term, Options)
write_term(+, +)

write_term(Stream_or_alias, Term, Options)

write_term(Term, Options) is equivalent to write_term(Out, Term, Options) where Out is the current output stream; that is, write_term/2 is defined by:

write_term(Term, Options)
    :- current_output(Stream),
       write_term(Stream, Term, Options).

write_term(Stream_or_alias, Term, Options) outputs Term to the stream associated with Stream_or_alias in a form which is defined by the write-options list Options.


write_term(S, [1,2,3]) ==> [1,2,3]
write_term(S, [1,2,3], [ignoreops(true)]) ==> .(1, .(2, .(3, [])))
write_term(S, '1 < 2' ) ==> 1<2
write_term(S, '1 < 2', [quoted(true)] ) ==> '1 < 2'
write_term(S, 'VAR'(0),[numbervars(true)]) ==> A
write_term(S, 'VAR'(1),[numbervars(true)]) ==> B
write_term(S, 'VAR'(28),[numbervars(true)]) ==> C1

write(Stream_or_alias, Term)

write(Term) is equivalent to write(Out, Term) where Out is the current output stream; that is, write/1 can be defined by:

    :- current_output(Stream),
       write(Stream, Term).

write(Stream_or_alias, Term) is equivalent to

write_term(Stream_or_alias, Term, [numbervars(true)]);

Thus, write/2 can be defined by:

write(Stream_or_alias, Term)
    :- write_term(Stream_or_alias, Term, [numbervars(true)]).


write(S, [1,2,3]) ==> [1,2,3]
write(S, 1 < 2 )  ==> 1<2
write(S, 'VAR'(0) < 'VAR(1) ) ==> A<B

writeq(Stream_or_alias, Term)

writeq(Term) is equivalent to writeq(Out, Term) where Out is the current output stream; i.e, writeq/1 can be defined by:

    :- current_output(Stream),
       writeq(Stream, Term).

writeq(Stream_or_alias, Term) is equivalent to

write_term(Stream_or_alias, Term, [quoted(true), numbervars(true)]);

that is, writeq/2 can be defined by:

writeq(Stream_or_alias, Term)
    :- write_term(Stream_or_alias, Term, [quoted(true), numbervars(true)]).


writeq(S, [1, 2, 'A' ]) ==> [1, 2, 'A' ]
writeq(S, '1 < 2' ) ==> '1 < 2'
writeq(S, 'VAR'(0) < 'VAR(1) ) ==>  A<B

write_canonical(Stream_or_alias, Term)

write_canonical(T) is equivalent to write_canonical(S, T) where S is the current output stream; that is, write_canonical/1 can be defined by:

    :- current_output(Stream),
       write_canonical(Stream, T).

write_canonical(Stream_or_alias, Term) is equivalent to

write_term(Stream_or_alias, Term, [quoted(true), ignore_ops(true)]);

that is, write_canonical/2 can be defined by:

write_canonical(Stream_or_alias, Term)
    :- write_term(Stream_or_alias, Term, [quoted(true), ignore_ops(true)]).


write_canonical(S, [1, 2, 3 ]) ==> '.'(1, '.'(2, '.'(3, [] )))
write _canonical(S, 1 < 2 ) ==> < (1, 2 )
write _canonical(S, '1 < 2' ) ==> '1<2'
write_canonical(S, 'VAR'(0) < 'VAR'(1) ) ==> < ('VAR' (0), 'VAR' (1))

write_clause(Alias_or_stream, Clause)
write_clause(+, +)

write_clause(Alias_or_stream, Clause, Options)
write_clause(+, +, +)

These convenience predicates are defined by:

    :- get_current_output_stream(Stream),
       write_clause(Stream, Clause).
write_clause(Stream, Clause)
    :- write_clause(Stream, Clause, []).
write_clause(Stream, Clause, Options)
    :- write_term(Stream,Clause, Options),
       put_char(Stream, 0'.),

Since one often must ouput sequences of clauses, the following predicates are useful:


write_clauses(Alias_or_stream, Clauses)
write_clauses(+, +)

These predicates are defined by:

    :- get_current_output_stream(Stream),
       write_clauses(Stream, Clauses, []).

write_clauses(Stream, Clauses)
    :- write_clauses(Stream, Clauses, []).
write_clauses(Alias_or_stream, Clauses, Options)
write_clauses(+, +, +)

If Clauses is a list of terms (to be viewed as clauses), write_clauses/3 recursively applies write_clause/3 to the elements of Clauses.






The printf/[…] group of predicates provides a powerful formatted printing facility closely related to the corresponding facilities in the C programming language. printf/[…] accepts a format string together with a list of arguments to print, possibly a stream to print to, and possibly options. The format string contains characters to be printed, characters to control the formats of the items being printed, and argument placeholders. The figure below illustrates the general structure of the printf predicate.

Figure. printf formatting

As a simple example, the following clause defines a predicate for adding two numbers and printing the result:

    :- Z is X + Y,
       printf("Solution: %t + %t = %t\n",[X,Y,Z]).

The %t placeholder tells printf/2 that it should take the next argument from the argument list, and print it as a Prolog term. The \n at the end of the format string causes a newline character to be printed. The following shows the result of calling add/2:

add( 7, 8 ) ==> Solution: 7 + 8 = 15

The same effect could have been obtained without printf/2. It is instructive to see how it is done:

    :- Z is X + Y,
       write(' + '),
       write(' = '),

The second version of the predicate is longer and somewhat harder to read. If no arguments must be supplied to printf (i.e., the string contains no placeholder characters), the unary version, printf/1, can be used. The fundamental formatted output predicate is printf/4. The first four predicates above are convenience predicates and can be defined as follows:

    :- current_output(Stream),

    :- current_output(Stream),

    :- current_output(Stream),

    :- printf(Alias_or_stream,Format,ArgList,[]).

printf/4 is closely related to the C language printf function. Roughly, the formats supported by printf/4 are the same as those allowed by the C language printf, with the inclusion of several additional combinations, in particular, ‘%t’, which indicates that the corresponding Prolog term should be output at that point. And where one would call the C language function in the form

printf(Format, A1, A2, ..., An),

one calls the Prolog printf/2 in the form

printf(Format, [A1,A2,...,An]).

More precisely, formats are specified as follows. An extent expression consist of either a sequence of digits, or of two sequences of digits separated by a period(.); in addition, an extent expression may be prefixed with a minus sign (-). If K is an extent expression, an active format element is one of the following expressions:

%K s
%K d
%Ke %Kf %Kg

The last five elements in this list are also called C format elements. A printf format is a single-quoted string, ie., an atom. (Using double quoted strings for formats is accepted for bacwards compatibility; however, it is much more wasteful of storage.) Any printf format contains zero or more active format elements, together with other text (possibly none). The behavior of printf/[..] for the format elements %Ks, %Kd, %Ke, %Kf, and %Kg is completely in accord with the C printf, since in these cases, the argument (appropriately converted) is simply passed to the C printf. As noted above, for %t, the argument is printed on the output stream as a Prolog term. Finally, the format allows the programmer to take control of the formatting process as follows. Suppose that the format is %p, that Stream is the stream argument to printf/4, and that PArg is the argument corresponding to %p. Then the action of printf/4 is determined by:

(PArg = Stream^PrintGoal0 ->
        (PArg = [Stream, Options]^PrintGoal0 ->

printf/4 is effectively defined as follows:

If Format = [],
    printf(Alias_or_stream, Format, ArgList, Options)
succeeds; otherwise,
    printf(Alias_or_stream, Format, ArgList, Options)
holds provided that:
    Format = ["%t" | FormatTail] and ArgList = [T | ArgListTail],
    print_term(Alias_or_stream, T, Options) and
    printf(Alias_or_stream, FormatTail,ArgListTail,Options)
else if
    Format = ["%p" | FormatTail] & ArgList = [T | ArgListTail],
then if
    T = S^PG & S=Alias_or_stream
    then call(PG)
    else if
        T = [S,O]^PG & S=Alias_or_stream & O=Options
        then call(PG)
            call(T) and
else if Format = ["%%" | FormatTail] ,
    output the character % to Alias_or_stream and
else if
    Format = [Head | FormatTail] & Head is a C active format string
    & ArgList = [T | ArgListTail],
    output T to Alias_or_stream in format Head in the manner of C printf,
    & printf(Alias_or_stream,FormatTail,ArgListTail,Options)
    Format = [C | FormatTail] &
    put_char(Alias_or_stream, C) &



These very useful convenience predicates are defined by

    :- open(string(Output),write,Stream),

    :- open(string(String),write,Stream),
           maxdepth(20000), quoted_strings(false)]),

    :- open(string(String),write,Stream),
          [line_length(10000), quoted(true),
           maxdepth(20000), quoted_strings(false)]),

10.8 Operator Declarations

op(Priority, Op_specifier, Operator)
op(+, +, +)

op/3 is used to specify Operator as a syntactic operator (for the Prolog parser) according to the specifications of Priority and Op_specifier.

current_op(Priority, Op_specifier, Operator)
current_op(?, ?, ?)

current_op(Priority, Op_specifier, Operator) is true iff Operator is an operator with properties defined by specifier Op_specifier and precedence Priority.


currentop(P, xfy, OP).Succeeds three times if the predefined operators have not been altered, producing the following bindings:

P ==> 1100        OP ==> ;

P ==> 1050        OP ==> ->

P ==> 1000        OP ==> ,

The order in which the solutions are produced is implementation dependent.

10.9 DEC10-Style I/O Predicates

The full details of the definitions of the DEC10-style I/O predicates are presented in the file sio_d10.pro which is in the alsdir subdirectory. This file should be loaded whenever one wishes to use the DEC10- style I/O system (apart from simple calls to read/1 and write/1). Note that the predicates listed here as DEC10-style predicates have been added to the ALS Library, and so the file sio_d10.pro is automatically loaded by the development environment whenever one of them is called.

Below, we present conceptual definitions (which simply suppress some of the detail) of the DEC10 predicates.

        :- 'is input stream'(Alias_or_stream,Stream),
        :- open(FileName,read,[alias(FileName)],Stream),

        :- current_input(Stream),
           current_alias(Alias,Stream), !.
        :- current_input(Stream).

        :- current_input(Stream),

        :- 'is output stream'(Alias_or_stream,Stream), !,
        :- open(FileName,write,[alias(FileName)],Stream),

        :- current_output(Stream),
           current_alias(Alias,Stream), !.
        :- current_output(Stream).

        :- current_output(Stream),

        :- current_input(Stream),
           get_code(Stream,Byte), !.

        :- get0(Byte0),

        :- Byte0 =< 0' ', !,

        :- get0(Byte0),

        :- get0(Byte0),

        :- current_output(Stream),
           put_code(Stream,Byte), !.

        :- N =< 0, !.
        :- NN is N-1,
           put(0' '),

        :- flush_output.

        :- write_term(X,[quoted(false),ignore_ops(true),numbervars(true)]).

10.10 The user file

The file user represents both the keyboard (stream user_input) and the display (stream user_output). The system automatically opens stream user_input to be stdin, and opens stream user_output to be stdout. .

The above information is useful if you use operating system shell commands to change the standard input and output to be other than the keyboard and display. This will work properly if ALS Prolog was invoked with the -g and -b command line switches, and if the operating system supports such redirection (e.g., the Macintosh does not).

The user file (user_input, user_output) cannot be closed with close/1. However, you can signal end-of-file from the console on various operating systems as follows:

10.11 REST-inspired Internet Connections.

Inspired by the REST approach to internet file transfer,

http(RESTVerb, URL, Options)

provides REST-inspired transfer services. At present, the following RESTVerbs are implemented:

get, post put, delete, head, options, patch.

The available Options depend on the RESTVerb.

First, a comment about implementation. At the bottom of the plumbing, the so-called “Easy interface” of the Curl library is used to build the implementation. In this approach, everything about a call http(RESTVerb, URL, Options) is converted into a single list of Easy-oriented Curl option equations, and that list is then passed to a C program built over the CURL Easy interface API.

For all equations on the list passed to the C program, the left side of every equation is first converted to uppercase. With the exception of the special equation tags ‘RESULT’, ‘RESULTFILE’, and ‘UPLOADDATA’ (see below), the left side L of every equation L=R passed to the C plumbing is prefixed with either ‘CURLOPT_’ or ‘CURLINFO_’. The result of this prefixing must be either an <a href=“https://curl.haxx.se/libcurl/c/curl_easy_setopt.html”>easy option</a> (cf. an <a href=“https://curl.haxx.se/libcurl/c/easy_setopt_options.html”>alphabetical listing</a>) or an <a href=“https://curl.haxx.se/libcurl/c/easy_getinfo_options.html”>info option</a>.

Several simple transformations are applied to the elements of the list:

i) Unary prolog expressions <F>(A) are allowed on the Options list. These are converted to equations FF = A, where FF is the uppercase version of <F>.

ii) Equations E = A are converted to EE = A where EE is the uppercase version of E.

Common options:

can appear on Options. Whatever HTTP result is produced from the underlying Curl call (GET or POST or whatever) is turned into a UIA, and is unified with Expr, which can be any prolog expression, including an unbound variable.

can also appear on Options. Whatever HTTP result is produced from the underlying Curl call (GET, POST or whatever) is written into the file at <local file path>. Note that both result = Expr and resultfile = ‘<local file path>’ can occur together on an Options list: the HTTP result will be unified (as a UIA) with Expr and also written into the file at <local file path>.


?- http(get,'http://example.com', [result=RR,response_code(RC),total_time(TTT)]).
?- http(get,'http://example.com', [response_code(RC),total_time=TTT,file='./myfile.txt']).

For RESTVerb = post, data for POSTing can be supplied in one of two forms: 1) structured data such as is uploaded from Web forms (e.g., ‘name=admin&shoesize=12’) and 2) free-form text data (e.g., ‘lorem ipsum doler’). Either type of data can be supplied either directly in an equation on the options lisit, or in a file specified on the options list.


?- http(post, 'https://postman-echo.com/post', [fields='name=admin&shoesize=12', result=RR]).
?- http(post, 'https://postman-echo.com/post', [fieldsfile='./foofields.txt', result=RR, eol='&']).
?- http(post, 'https://postman-echo.com/post', [datafile='./lorem.txt', eolcode=32,result=RR]).


An alternate, more direct, way of invoking the immediate interface from prolog to Curl is provided by the predicates curl/1-3. Below, Options must be list of Curl options as described above.

Cleans up option capitalization and invokes the low-level interface to Curl.

curl(URL, Target)
    curl(URL, [], Target).

curl(URL, Options, Target)
    XOptions = [url=URL, result=Target | Options],


?- curl('http://example.com', [response_code=RC], RR).
?- curl('http://example.com', RR).
?- curl([url='http://example.com', response_code(RC), result=RR]).