prolog

Retract by partial match?


Is there a way to retractall clauses by partial match .. let say i have the following grammar :

s3-->[a, b, r, a].
s4-->[a, b].
s5-->s4, s4.

how can I delete all clauses starting with 's' ?


exists(Prefix, Max, X) :- between(1,Max,I), atom_concat(Prefix,I,X),current_predicate(X/2).
exists(X) :- exists('s', 10, X).

clean :- findall(X,(exists(X), abolish(X/2)), R).

Solution

  • Sten, you are looking for predicate to backtrack through all predicates with a constraint on its name. In swi_prolog, it's the current_predicate family.

    look_for_s_predicates :-
        current_predicate(PredicateName, P),  
        atom_chars(PredicateName, [s|_]),
        \+ predicate_property(P, built_in), /*ignore built-ins*/
        write(PredicateName), nl, fail.
    look_for_s_predicates. 
    

    Edit: added P in current_predicate to allow ignoring built-in predicates that start with 's'.

    In the above sample, I'm restricting predicates to those starting with s. So if I have defined

    s1(a,b,c) :- test(a,b).
    s2(a,b,c).
    test(a,b).
    

    look_for_s_predicates/0 will list s1 and s2. Now you can retract them if you wish.

    Have I answered your question ?