Read a file line by line in Prolog
Solution 1
In SWI-Prolog, the cleanest solution is to write a DCG that describes what a "line" is, then call a predicate for each line. Use library(pio) to apply the DCG to a file.
EDIT: As requested, consider:
:- use_module(library(pio)).
lines([]) --> call(eos), !.
lines([Line|Lines]) --> line(Line), lines(Lines).
eos([], []).
line([]) --> ( "\n" ; call(eos) ), !.
line([L|Ls]) --> [L], line(Ls).
Sample usage: ?- phrase_from_file(lines(Ls), 'your_file.txt').
Solution 2
You can use read
to read the stream. Remember to invoke at_end_of_stream
to ensure no syntax errors.
Example:
readFile.pl
main :-
open('myFile.txt', read, Str),
read_file(Str,Lines),
close(Str),
write(Lines), nl.
read_file(Stream,[]) :-
at_end_of_stream(Stream).
read_file(Stream,[X|L]) :-
\+ at_end_of_stream(Stream),
read(Stream,X),
read_file(Stream,L).
myFile.txt
'line 0'.
'line 1'.
'line 2'.
'line 3'.
'line 4'.
'line 5'.
'line 6'.
'line 7'.
'line 8'.
'line 9'.
Thus by invoking main
you will recieve the output:
?- main.
[line 0,line 1,line 2,line 3,line 4,line 5,line 6,line 7,line 8,line 9]
true
Just configure main
. The output here is an example by using write
, of course. Configure to match your request.
I assume that this principle can be applied to answer your question. Good luck.
Igor Marvinsky
Updated on September 01, 2021Comments
-
Igor Marvinsky over 2 years
I'd like to read a plain text file and apply a predicate to each line (the predicates contain
write
which does the output). How would I do that? -
Shon over 9 yearsI know it's been a long time, but I'm trying this method and it seems to take an absurdly long time. Could you provide an example of some performant code using DCGs and library(pio) that will read in a file by lines? Thanks!
-
Shon over 9 yearsThanks so much! I see my error before was using the example in the SWI-Prolog library(pio) documentation for my model. It uses
findall/3
to get all instances of a certain pattern, but I see you just use a dcg that parses the whole file. Out of curiosity, why must we usecall(eos)
instead of a dcg rule? -
mat over 9 years
call//1
(and then,eos/2
) is used to portably refer to the entire implicit DCG arguments from within a DCG rule. You cannot use a DCG rule instead, because DCG rules are subject to translation rules that let them only refer to certain parts of these arguments. "Portable" means that this is independent of how any particular Prolog system actually translates DCG rules to Prolog rules, so that it works in all systems that support DCGs as currently being drafted by ISO. -
Shon over 9 yearsThanks again! I reworked the problem I was attempting with the help of your example, and it ended up in a fairly elegant solution. More importantly, you've helped me bump up my understanding of DCGs to the next level. (I've been studying them casually, off and on, for more than a year, and I still feel like I have a limited grasp. It is such a simple concept, and yet... maybe it's not so simple?).
-
RK_97 over 3 yearsSome time later, but i get an error with this code: 'uncaught exception: error(existence_error(procedure,main/0),top_level/0)'. Know what this means?