1
votes

Does prolog have built-in predicates, which can import and export complete files during runtime?

I need predicates like "assert" and "retract". The problem is that "assert" and "retract" only manipulates a dynamic list, not a complete file.

I know two ways of how to include a file into another:

:- include('file.pl').
:- consult('file.pl').

This mostly happens at the beginning of a code.

Can I use these predicates in the middle of my code? (I think the consult predicate worked, but I had problems with the include predicate...)
And is there any chance how to exclude/"delete" the included file again? (the more important question)

I found the built-in predicate "delete_file/1", which literally deletes the file (from your memory) - this is not what I wanted. But the file still was not deleted from the current program, only from the memory, which was really strange...

I hope someone can help me, because I could not find anything else than the predicates explained above. Thanks!!

2

2 Answers

2
votes

Not all prologs recognize consult (e.g., GNU Prolog doesn't). But include/1 and consult/1 does seem to work mid-file with SWI Prolog, and include/1 in GNU Prolog.

Undoing a file consult is another question. Prolog consults the file, hauling in all the facts and predicates, and the fact that it was all from a particular file is forgotten when the operation is done. It's as if you typed them all in by hand. It has no record that any particular set of predicates or facts were from a particular consulted file. To "undo" any such facts or predicates, you would have to do a retract/1 or retractall/1 on the items asserted as a result of the include or consult. That might be simple if the functors you are consulting are unique, because then you might get away with retractall(my_unique_functor(_,_)). or retract(foo(_)).. But if you have a mix of them (existing and newly asserted) and want to be selected, you'll have to sort them out.

1
votes

Logtalk includes a programming example, "named_databases", that supports the functionality you're looking for:

https://github.com/LogtalkDotOrg/logtalk3/tree/master/examples/named_databases

This example supports ECLiPSe, Lean Prolog, SICStus Prolog, SWI-Prolog, and YAP and it uses in its implementation the module system (except in Lean Prolog, which provides natively most of the functionality) for the actual databases and Logtalk's term-expansion mechanism for optimizing the usage of named database predicates.