Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swi Prolog, unloading source files

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.

like image 850
Vitor Paisante Avatar asked Jun 13 '12 20:06

Vitor Paisante


People also ask

How do I load a file into SWI-Prolog?

Open a terminal (Ctrl+Alt+T) and navigate to the directory where you stored your program. Open SWI-Prolog by invoking swipl . In SWI-Prolog, type [program] to load the program, i.e. the file name in brackets, but without the ending. In order to query the loaded program, type goals and watch the output.

Which command is used to exit from SWI-Prolog?

If you want to exit SWI-Prolog, issue the command halt., or simply type CTRL-d at the SWI-Prolog prompt.

What does SWI-Prolog do?

SWI-Prolog is a free implementation of the programming language Prolog, commonly used for teaching and semantic web applications.


2 Answers

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.

like image 140
gusbro Avatar answered Sep 24 '22 04:09

gusbro


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)'
like image 43
CapelliC Avatar answered Sep 24 '22 04:09

CapelliC