2.10 Simple I/O

Prolog uses the following predicates for reading data

 

                       seeing(File) 
                       see(File) 
                       read(Data) 
                       seen 

File='user' will select the keyboard for the input source. Otherwise File should be the name of an existing disk file (together with a path designation, if needed). For example, here is a small program to read data from a file and print it to the screen.

 
browse(File) :- 
          seeing(Old),      /* save for later */ 
          see(File),        /* open this file */ 
          repeat, 
          read(Data),       /* read from File */ 
          process(Data),    
          seen,             /* close File */ 
          see(Old),         /*  previous read source */ 
          !.                /* stop now */ 
 
process(end-of-file) :- !. 
process(Data) :-  write(Data), nl, fail. 

For example, to inspect the source file for 'browse' (assuming its name is browse.pro), one could issue the goal

 
?- browse('browse.pro'). 

Actually, when Prolog satisfies 'seeing(Old)', the variable Old will be bound to the port (stream or buffer) from which read-input is currently coming. And when Prolog satisfies 'see(File)', the file whose name is bound as value to 'File' is opened and a read port or stream is associated with the file. Subsequent reading is through that buffer; and then the 'seen' goal closes the file.

The 'Data' variable in the program should take values which are Prolog terms. For example, the 'File' could contain a Prolog program, or a sequence of numbers. A file of numbers would have to be formatted as in the following display.

 

              32.2.       /* = real 32.2 */ 
              31.         /* = integer 31 */ 
              4.0.        /* = real 4.0 */                   
Here is an interactive version of the browse program.

 
browse :- 
        seeing(Old),      /* save for later */ 
        see(user), 
        write('Enter name of file to browse: '), read(File), 
        see(File),        /* open this file */ 
        repeat, 
        read(Data),       /* read from File */ 
        process(Data),    
        seen,             /* close File */ 
        see(Old)          /*  previous read source */ 
        !.                /* stop now */ 

Here is how to use this version pay attention to the form that user supplies the filename:

 
?- browse. 
Enter name of file to browse: 'foo.pl'. 
... 

Prolog uses the following predicates to write data

 

                       telling(File) 
                       tell(File) 
                       write(Data) 
                       nl 
                       writeln(Data) 
                       told 

File='user' will select the screen as the output destination. For example, here is a little program that saves the current Prolog program in memory to a file.

 
my_save(ToFile) :-      
       telling(Old),      /* current write output */ 
       tell(ToFile),      /* open this file */ 
       listing,           /* list all clauses in memory */ 
       told,              /* close ToFile */ 
       tell(Old).         /* resume this output */ 

Actually, when Prolog satisfies 'telling(Old)', the variable Old will be bound to the port associated with the current write-output destination. When Prolog satisfies 'tell(ToFile)' the file name bound to 'ToFile' will be opened (if it exists) or created and an output port or stream will be associated with the file. Subsequent writing goes to that file via the port or stream. Then satisfying 'told' closes the file and the association of the port or stream with the file.

Exercise 2.10 Design a Prolog program that can open and copy a file of Prolog objects from one file to another file.

Our last example illustrates a way to load a file of Prolog terms into a list. The file is assumed to contain only Prolog terms (or comments, which are ignored). This is a handy way to convert a file of terms into a list of terms (e.g., Prolog clauses) for further processing. Operators in the file need to be defined in the Prolog environment calling file_to_list/2.

%%
%% Load a file of Prolog terms into a List.
%%
file_to_list(FILE,LIST) :- 
   see(FILE), 
   inquire([],R), % gather terms from file
   reverse(R,LIST),
   seen.

inquire(IN,OUT):-
   read(Data), 
   (Data == end_of_file ->   % done
      OUT = IN 
        ;    % more
      inquire([Data|IN],OUT) ) . 


Prolog Code for this section.
Prolog Tutorial Contents