I'm sorry, this has probably been asked before but I can't find a good answer.
I'm writing a Prolog assignment, in which we must write a database with insert, delete, etc. I'm currently stuck on the insert part. I'm trying to use tell, listing and told for this, but the results are often unpredictable, deleting random parts of the file. Here's the full code of my database, banco.pl
:
:- dynamic progenitor/2.
progenitor(maria,joao).
progenitor(jose,joao).
progenitor(maria,ana).
progenitor(jose,ana).
insere(X,Y) :- dynamic progenitor/2, assert(progenitor(X,Y)).
tell('banco.pl'), listing(progenitor), told.
I then run the following on SWI-Prolog:
insere(luiz,luiza).
And get the following result on banco.pl
:
:- dynamic progenitor/2.
progenitor(maria, joao).
progenitor(jose, joao).
progenitor(maria, ana).
progenitor(jose, ana).
Note that the clause I tried to insert isn't even in the file, and the lines defining commit and insere are missing.
How would I do this correctly?
tell
starts writing to the beginning of the file. so you're overwriting everything else that was in the file. you have these options:put your
progenitor
predicate (and just that) in another file.use
append/1
to write to the end of the file withportray_clause
. this only helps forinsert
, but you stated that you wantdelete
too.read the other clauses into a list and reprint them, then use
listing/1
:(text for formatting)
Note that you could use the
read_all_other_clauses
for yourdelete
only, if you change the line with the omit comment. Then you could use the solution proposed in #2 for yourinsere