让我们假设内置的 read_line_to_codes/2 可用:那么您可以应用前行一行:
process_file(Path) :-
open(Path, read, In),
read_line_to_codes(In, Line1),
read_line_to_codes(In, Line2), % assume not empty
process_lines(Line2, [Line1], In, Facts),
maplist(writeln, Facts). % just for test
process_lines(end_of_file, LastFactDef, In, [LastFact]) :-
lines_fact(LastFactDef, LastFact),
close(In).
process_lines([U|Us], LastFactDef, In, [LastFact|Facts]) :-
upper_lower(U, _),
lines_fact(LastFactDef, LastFact),
read_line_to_codes(In, Line),
process_lines(Line, [[U|Us]], In, Facts).
process_lines(Last, Lines, In, Facts) :-
read_line_to_codes(In, Line),
process_lines(Line, [Last|Lines], In, Facts).
lines_fact(Lines, Fact) :-
reverse(Lines, [FunctorUpper|ArgCodes]),
maplist(make_lower, FunctorUpper, FunctorCodes),
maplist(atom_codes, [Functor|Args], [FunctorCodes|ArgCodes]),
Fact =.. [Functor|Args].
% if uppercase get lowercase
upper_lower(U, L) :-
between(0'A, 0'Z, U), L is 0'a + U - 0'A.
make_lower(C, L) :- upper_lower(C, L) ; L = C.
在 SWI-Prolog 中运行测试(默认情况下我们有可用的 read_line_to_codes/2 和 between/3):
?- process_file('/home/carlo/test/file.txt').
functor1(arg1 ,arg2)
functor2(arg1,arg2,arg3)
functor3(arg1,arg2,arg3,arg4)
true