Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use assert and retract for anti-elements.

Status
Not open for further replies.

niro1

Programmer
Mar 15, 2004
1
GB
Hi everyone,

I am trying to implement a program in Prolog, that does the following:
If my knowledgebase contains a fact or rule that makes a query succeed,
then when the query is run with the parameter of my program it should
retract the clauses or facts that makes the query succeed from the
knowledge base, so that the query fails.
Let say my program is called anti(P):- ......
The following is my knowledgebase:

Code:
child_of(joe, ralf).
child_of(mary, joe).
child_of(steve, joe).

descendent_of(X, Y) :-
        child_of(X, Y).
descendent_of(X, Y) :-
        child_of(Z, Y),
        descendent_of(X, Z).[\code]

when the query ?child_of(joe,ralf). is run the answer is obviously yes.
But when ?anti(child_of(joe,ralf)). is run I want it to retract the facts
and rules that makes it succeed, in this case child_of(joe,ralf)., so that
the query becomes false.
Furthermore is it possible to show the facts and clauses that were retracted, as an answer.


I have done a bit of coding so far, but thats quite specific for the above mentioned program, plus it doesn't show what facts or clauses that were retracted in order to falsify the query. But anyway here is what I've done so far.

 [code]:- dynamic fact/1.
        
        fact(child(joe, ralf)). 
        fact(child_of(mary, joe)).
        fact(child_of(steve, joe)).

        rule(descendent(X, Y), [child(X,Y)]).
        rule(descendent(X, Y), [child(Z,Y),descendent(X,Z)]).

        interpret(Query) :-
            fact(Query).
        interpret(Query) :-
            rule(Query, Body),
            interpret_body(Body).

        interpret_body([]).
        interpret_body([Goal|Goals]) :-
            interpret(Goal),
            interpret_body(Goals).

interpret(Query, Refs) :-
            interpret(Query, Refs, []).

        interpret(Query, [Ref|Refs], Refs) :-
            clause(fact(Query), true, Ref).
        interpret(Query, Refs0, Refs) :-
            rule(Query, Body),
            interpret_body(Body, Refs0, Refs).

        interpret_body([], Refs, Refs).
        interpret_body([Goal|Goals], Refs0, Refs) :-
            interpret(Goal, Refs0, Refs1),
            interpret_body(Goals, Refs1, Ref).
anti(Query) :-
            interpret(Query, Refs),
            sort(Refs, UniqueRefs),
            ( member(Ref, UniqueRefs), erase(Ref), fail ; true ). [\code]



 I hope that one of you can help me with this matter. The logic term for
the rule I am trying to implement is called anti-elements, just in case
that should help.

 

Thanks

Niro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top