Is there a built-in predicate or a easy way to remove from the knowledge database of prolog a source files that has already been consulted? I've gone through the reference manual and didn't find any thing that could do that.
You can do it with these procedures which use source_file/1
and source_file/2
:
unload_last_source:-
findall(Source, source_file(Source), LSource),
reverse(LSource, [Source|_]),
unload_source(Source).
unload_source(Source):-
ground(Source),
source_file(Pred, Source),
functor(Pred, Functor, Arity),
abolish(Functor/Arity),
fail.
unload_source(_).
unload_source/1
abolishes all predicates defined by the input Source file name. Be warned that it needs to be an absolute path.
unload_last_source/0
will retrieve the last consulted file name and unload it.
After a file has been consulted, it become 'irrelevant' to Prolog. So I think that generally to answer should be no. But SWI-Prolog has a rich set of builtins that allows you to control your prolgram. For instance
?- [stackoverflow].
?- predicate_property(P, file('/home/carlo/prolog/stackoverflow.pl')).
P = yield(_G297, _G298) ;
P = now _G297 ;
P = x(_G297) ;
...
?- abolish(yield/2).
true.
?- predicate_property(P, file('/home/carlo/prolog/stackoverflow.pl')).
P = now _G297 ;
P = x(_G297) ;
...
Note that abolish doesn't require the file name to work, you could delete predicates loaded from other sources files.
clause, clause_property and erase should give more control, but I get an error I don't understand (it's undocumented) when attempting to use erase:
?- clause(strip_spaces(_G297, _G298),X,Y),erase(Y).
ERROR: erase/1: No permission to clause erase `<clause>(0x29acc30)'
if you know the name of the predicate, for example fact/2, you can use:
retractall(fact(_,_)).
assert
, right? –
Voyeurism This will work.
unload_file(+File)
Remove all clauses loaded from File. If File loaded a module, clear the module's export list and disassociate it from the file. File is a canonical filename or a file indicator that is valid for load_files/2. This predicate should be used with care. The multithreaded nature of SWI-Prolog makes removing static code unsafe. Attempts to do this should be reserved for development or situations where the application can guarantee that none of the clauses associated to File are active.
© 2022 - 2024 — McMap. All rights reserved.