next up previous contents
Next: Pruning Operators: Cut Up: The Prolog Language Previous: Structure Inspection

Input/Output

The easiest way of doing input/output in Prolog is using the so-called DEC-10 predicates. They are based on the idea of having a current input and output, which can be redirected to write to and read from files. The basic DEC-10 I/O predicates are shown in Table 4.4.


 
Table 4.4: DEC-10 I/O predicates
Predicate Explanation
write(X) Write the term X on the current output stream.
nl Start a new line on the current output stream.
read(X) Read a term (finished by a full stop) from the current input stream and unify it with X.
put(N) Write the ASCII character code N. N can be a string of length one.
get(N) Read the next character code and unify its ASCII code with N.
see(File) File becomes the current input stream.
seeing(File) The current input stream is File.
seen Close the current input stream.
tell(File) File becomes the current output stream.
telling(File) The current output stream is File.
told Close the current output stream.
 

There are more sophisticated I/O predicates based on opening and closing streams explicitly: handles to the files are returned, which can be passed to the I/O predicates. The interface is similar to what is provided by most operating systems, and available in many programming languages.

All I/O predicates perform side-effects: they change the state of the world (changing the contents of the screen or a disk file, in this case; broadcasting messages over the net, if writing / reading is made on a socket stream) in such a way that persists even after backtracking. Side-effects predicates are not easily formalized from a logical point of view, because the state of the whole world has to be taken into account.


next up previous contents
Next: Pruning Operators: Cut Up: The Prolog Language Previous: Structure Inspection
MCL
1998-12-03