projog

3.98. clause(X,Y) / retract(X) - matches terms to existing clauses.

clause(X,Y) causes X and Y to be matched to the head and body of an existing clause. If no clauses are found for the predicate represented by X then the goal fails. If there are more than one that matches, the clauses will be matched one at a time as the goal is re-satisfied. X must be suitably instantiated that the predicate of the clause can be determined.

retract(X) - remove clauses from the knowledge base. The first clause that X matches is removed from the knowledge base. When an attempt is made to re-satisfy the goal, the next clause that X matches is removed. X must be suitably instantiated that the predicate of the clause can be determined.

Examples

Examples of using "clause":

test(a,b) :- true.
test(1,2) :- true.
test(A,B,C) :- true.
test([_|Y],p(1)) :- true.

?- clause(test(X,Y), Z).
X = a
Y = b
Z = true

yes;
X = 1
Y = 2
Z = true

yes;
X = [_|Y]
Y = p(1)
Z = true

yes

?- clause(test(a,b,c), true).

yes

?- clause(test(1,2,3), true).

yes

?- clause(tset(1,2,3), true).

no

Examples of using "retract":

?- assertz(p(a,b,c)).

yes

?- assertz(p(1,2,3)).

yes

?- assertz(p(a,c,e)).

yes

?- retract(p(x,y,z)).

no

?- retract(p(a,b,e)).

no

?- p(X,Y,Z).
X = a
Y = b
Z = c

yes;
X = 1
Y = 2
Z = 3

yes;
X = a
Y = c
Z = e

yes

?- retract(p(a,Y,Z)).
Y = b
Z = c

yes;
Y = c
Z = e

yes

?- p(X,Y,Z).
X = 1
Y = 2
Z = 3

yes

?- retract(p(X,Y,Z)).
X = 1
Y = 2
Z = 3

yes

?- p(X,Y,Z).

no

retract and clause will fail if predicate does not exist

?- retract(unknown_predicate(1,2,3)).

no

?- clause(unknown_predicate(1,2,3),X).

no

Argument must be suitably instantiated that the predicate of the clause can be determined.

?- retract(X).

Expected an atom or a predicate but got a VARIABLE with value: X

?- clause(X,Y).

Expected an atom or a predicate but got a VARIABLE with value: X

?- retract(true).

Cannot inspect clauses of built-in predicate: true/0

?- clause(true,X).

Cannot inspect clauses of built-in predicate: true/0

?- retract(is(1,2)).

Cannot inspect clauses of built-in predicate: is/2

?- clause(is(1,2),X).

Cannot inspect clauses of built-in predicate: is/2

non_dynamic_predicate(1,2,3).

?- retract(non_dynamic_predicate(1,2,3)).

Cannot retract clause from user defined predicate as it is not dynamic: non_dynamic_predicate/3

?- retract(non_dynamic_predicate(_,_,_)).

Cannot retract clause from user defined predicate as it is not dynamic: non_dynamic_predicate/3

?- retract(non_dynamic_predicate(4,5,6)).

no