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