-1
consult(File) :-
   retractall(done(_)),
   current_input(Old),
   open(File, read, Stream),
   repeat,
   read(Term),
   try(Term),
   close(Stream),
   set_input(Old),
   !.

try(end_of_file) :- !. % End of file marker read
try((?- Goals)) :- !, call(Goals), !, fail.
try((:- Goals)) :- !. % ignore directives
try(Clause) :-
   head(Clause, Head),
   record_done(Head),
   assertz(Clause),
   fail.

:- dynamic done/1.
record_done(Head) :- done(Head), !.
record_done(Head) :-
   functor(Head, Func, Arity),
   functor(Proc, Func, Arity),
   asserta(done(Proc)),
   retractall(Proc),
   !.

head((A :- B), A) :- !.
head(A, A).

I didn't quite get what this consult predicate does, it was supposed to read in the clauses in a file and erases any existing clauses for predicates defined in that file. However, "read" takes in a user input (I use swi), then I tried to use read/2, and even after that, there still are some incongruences to the original idea for the code. Could someone shed some light on this matter?

false
  • 10,264
  • 13
  • 101
  • 209
  • `head((A :- B), A) :- !.` suffers from the same problem as in https://stackoverflow.com/questions/76889355/why-this-predicate-is-written-in-this-way – brebs Aug 14 '23 at 19:48
  • This is some convoluted attempt at re-implementing fundamental functionality of a Prolog. Without context it is a waste of time to try and guess what does this predicate. Is it some kind of riddle you need to solve? Or why don't you tell us where you found this code and why do you need it? – TA_intern Aug 15 '23 at 06:01
  • What's the "original idea for the code"? It's a bad idea to be using predicate names which *already exist built-in* - https://www.swi-prolog.org/pldoc/man?predicate=consult/1 – brebs Aug 15 '23 at 11:29
  • Please refer [to my last comment](https://stackoverflow.com/questions/76889355/why-this-predicate-is-written-in-this-way/76899813#comment135567429_76899813). Studying such code as a beginner does not make much sense. You will not learn what Prolog is all about with such code. – false Aug 16 '23 at 14:06

0 Answers0