Consulting a file multiple times

How does consulting a file (with facts) work internally? When I consult a file containing facts multiple times, even when the file changes, the facts are not duplicated (asserted twice). When I remove a fact in the file and reconsult it, the fact is also gone. Does SWI Prolog keep a reference internally to a file the fact was defined it and removes it on reconsulting? Is this behaviour part of the standard?

I found info (from 1993 :slight_smile: ) here, but I am not sure how much of it applies to SWI. Quote:

There are a couple of problems with getting consult/1 into the standard. One problem is what it means to consult the same file more than once. Quintus Prolog and some others use the “reconsult” semantics (based on the reconsult predicate in DEC10 Prolog) which deletes previous definition of each predicate that appears in the file. This is not quite “right” in the case where a predicate has been deleted from a file and the file is then consulted again, since there may still be calls to the deleted predicate. Another possibility is to have systems “unload” all the predicates that previously were defined in the file before consulting it a second time. This imposes some implementation overhead, and also makes it imperative that the Prolog system can always identify whether a file is one that has been previously consulted, which is not possible in general, especially in cases where Prolog states can be saved and then re-run later, possibly on a different machine.

See SWI-Prolog -- Reloading files, active code and threads

And yes, SWI-Prolog keeps track of where clauses come from, i.e., the file and line number. If the file is included, it also keeps track of the (nested) include locations.

consult/1 is the same as traditional reconsult/1 if the file is already loaded.

You can query all this stuff using clause_property/2, predicate_property/2, source_file_property/2 and module_property/2. See also clause_info/5 from library(prolog_clause)

1 Like