Question

#Retract doesn't change the database#

Hi i have a database in Prolog consisting of simple facts:

:-dynamic sportsmen/2.
sportsmen('Andrew', 'Shaw').
sportsmen('Patrick', 'Kane').

Now i want to retract some facts from it. Problem is, when i call retract(sportsmen(_,'Kane')) it returns true, but there is no change in my database, i tried to find a solution here and i found some guy that has this problem a was adviced to look at predicate tell/1. I know what this predicate is doing but i cant figure out how can i write back to database all the remaining facts excepts of the one i want to delete.

Can anyone help?

Was it helpful?

Solution

Retract works. It retracts from the database in memory. It does not remove facts from your file:

$ cat foo.pl

:-dynamic( sportsmen/2 ).

sportsmen('Andrew', 'Shaw').
sportsmen('Patrick', 'Kane').

Run prolog:

$ gprolog
GNU Prolog 1.4.2
By Daniel Diaz
Copyright (C) 1999-2012 Daniel Diaz

Load the foo.pl file:

| ?- [foo].
compiling /home/mark/src/prolog/_play_/foo.pl for byte code...
/home/mark/src/prolog/_play_/foo.pl compiled, 6 lines read - 398 bytes written, 13 ms

(1 ms) yes

Note that the facts are present:

| ?- listing.

% file: /home/mark/src/prolog/_play_/foo.pl

sportsmen('Andrew', 'Shaw').
sportsmen('Patrick', 'Kane').

(1 ms) yes

Retract one of the facts:

| ?- retract(sportsmen(_, 'Kane')).

yes

Note that the fact was retracted:

| ?- listing.

% file: /home/mark/src/prolog/_play_/foo.pl

sportsmen('Andrew', 'Shaw').

yes

Exit prolog:

| ?- ^D

Note that the facts are still all present in the file:

$ cat foo.pl

:-dynamic( sportsmen/2 ).

sportsmen('Andrew', 'Shaw').
sportsmen('Patrick', 'Kane').


If you want to maintain some facts in a file, you would do your database manipulation in memory, as shown above, and then use Prolog file I/O predicates to write out the updated facts. For example, to save a given set of sportsmen facts, using the Edinburgh style I/O predicates:

telling(OldStream),
tell('sportsmen.pl'),
write(':- dynamic sportsmen/2.'), nl,
listing(sportsmen),
told,
tell(OldStream).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top