0
votes

#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?

1
How did you verify that the retract did not work? Which version of Prolog are you using?Scott Hunter
I want to achieve that on my file there will be just this :-dynamic sportsmen/2. sportsmen('Andrew', 'Shaw'). But when i try to retract, there is no change in it. I am using SWI prologuser3568104
You are trying to EDIT a text file?Scott Hunter
You do know that assert/retract only affects the database in memory - it won't affect facts retrieved from a file, right ? So are you pulling in the facts from a file / need to save them to a file, or can everything be done in memory ?magus
yes i am trying to EDIT but not the text file but a .pl fileuser3568104

1 Answers

1
votes

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').


sportsmen
telling(OldStream),
tell('sportsmen.pl'),
write(':- dynamic sportsmen/2.'), nl,
listing(sportsmen),
told,
tell(OldStream).