read_all([Predicate|T]) :- read(Predicate), Predicate \== end_of_file, !, read_all(T). read_all([]). read_file(File, L) :- see(File), read_all(L), seen.If we call read_file to read "read_file.pl" (spaces added by me):
?- read_file('read_all.pl', L). L = [(read_all([_A|_B]):-read(_A),_A\==end_of_file,!,read_all(_B)), read_all([]), (read_file(_C,_D):-see(_C),read_all(_D),seen)] ? yThe display predicate some prolog interpreters provide gives us more insight on internal representation of terms. We call it on just one rule in order not to clutter the output with list representations:
?- read_file('read_all.pl', [H|_]), display(H). :-(read_all(.(_932,_952)),,(read(_932),,(\==(_932,end_of_file),,(!,read_all(_952)))))And here on the full program (notice... lists are "consed" with the '.' functor):
?- read_file('read_all.pl', L), display(L). .(:-(read_all(.(_900,_920)),,(read(_900),,(\==(_900,end_of_file),,(!,read_all(_920))))),.(read_all([]),.(:-(read_file(_1410,_1430),,(see(_1410),,(read_all(_1430),seen))),[])))Indeed, infix operators have been placed in prefix form like every other predicate (indeed, they are not special, just a parsing trick). Another "useful" predicate (not standard) is portray_clause/1, in order to do some "pretty-printing":
?- read_file('read_all.pl', [H|_]), portray_clause(H). read_all([A|B]) :- read(A), A\==end_of_file, !, read_all(B).
The assert family of predicates can “store” into the Prolog database facts (this is very common, in fact, memoization is often implemented this way) and rules (this is done less frequently – and there may be a slight efficiency penality [CHECK]). Thus we can manipulate terms (indeed that is what Prolog is about) and load them as programs. Notice that I showed the read predicate only to show that “standard” Prolog programs are represented that way, but it is not necessary at all to use programs saved in files.
4 ?- assert((foo(X) :- bar(X), \+ baz(X))). true. 5 ?- assert(bar(1)), assert(bar(2)), assert(baz(2)). true. 6 ?- foo(X). X = 1 .
No comments:
Post a Comment