Erin
Erin

Reputation: 105

Deleting a predicate from a knowledge base

I would like to pose a question. I have a knowledge base that I am working on and I want the user to delete a predicate that he wants by using delete_predicate.

The knowledge base is something like this:

is_tired(erin).
is_tired(mary).

I have defined delete predicate as

delete_predicate(Predicate):-tell('my_knowledge_base.pl'),
    retract(Predicate),
    told.

Let's say that the user wants to delete the predicate is_tired(mary). He will enter the following query:

?-delete_predicate(is_tired(mary)).

And the given predicate will be deleted form kb. I tried the the aforementioned rule but it deletes all kb's contents!!

I would really appreciate any pointer!

I will show exactly how I need the delete_predicate to work.

Let's assume I have the aforementioned knowledge base enriched with some more predicates:

:-dynamic(tired/1).
tired(kosta).
tired(renata).
tired(jim).
tired(mom).
tired(sister).
tired(mother).
tired(uncle).
tired(me).

If the user wants to delete tired(mom). from the kb he will query this:

?-delete_predicate(tired(mom)).

Then, the knowledge base will have that predicate deleted and it will look like this:

tired(kosta).
tired(renata).
tired(jim).
%here used to be the tired(mom).
tired(sister).
tired(mother).
tired(uncle).
tired(me).

If the user wants to delete tired(me) from the kb then the kb will be:

tired(kosta).
tired(renata).
tired(jim).
%here used to be tired(mom).
tired(sister).
tired(mother).
tired(uncle).
%here used to be tired(me).

And so on...

Upvotes: 0

Views: 2144

Answers (1)

CapelliC
CapelliC

Reputation: 60014

retract/1 actually works:

?- findall(X,is_tired(X),L).
L = [erin, mary].

?- delete_predicate(is_tired(mary)).
true.

?- findall(X,is_tired(X),L).
L = [erin].

but I think you are trying to store in 'my_knowledge_base.pl' the retracted predicates. Because you don't write nothing between tell/told the file remains empty.

You could do instead

delete_predicate(Predicate) :-
    open('my_knowledge_base.pl', append, S),
    (  retract(Predicate)
    -> format(S, '~q.~n', [Predicate])
    ;  true % what about errors?
    ),
    close(S).

then the file will contain the deleted facts.

edit Actually I didn't understood the question: a possibility could be

delete_predicate(Predicate) :-
        open('my_knowledge_base.pl', append, S),
        format(S, ':- retract(~q).~n', [Predicate]),
        close(S).

edit we can test if retract succeed before appending the :- retract(Predicate) to the file. Using the syntax you prefer

delete_predicate(Predicate) :-
  (  retract(Predicate)
  -> append('my_knowledge_base.pl'),
     format(':- retract(~q).~n', [Predicate]),
     told
  ;  true % or fail?=
  ).

Upvotes: 1

Related Questions