I have started to dig into the assert/1
and retract/1
meta-predicates of prolog, and now I wonder whether they can be used to implement a "resource-like" evaluation, i.e., consume clauses during evaluation.
The following code describes a minimal, complete, and verifiable example:
:- (dynamic chunk/1).
chunk(dummy1).
inner_failing_rule() :-
chunk(dummy1),
retract(chunk(dummy1)),
% a potential predicate call might happen here in which chunk(dummy1) should be false
fail, % models a failing predicate call (just for the sake of this example)
true.
As noted in my trace below, initially calling chunk(dummy1).
yields true, but yields false once I have called inner_failing_rule().
as the clause gets removed from the database due to the retract
call. However, I would like to have a different behavior, namely, I only want inner_failing_rule()
to retract if the rule evaluates to true in the end. Note that I cannot move retract to the end of the predicate, because there might potentially be predicate calls in which chunk(dummy1)
should yield false.
?- chunk(dummy1).
true.
?- inner_failing_rule().
false.
?- chunk(dummy1).
false. % This shall become true, because inner_failing_rule() was false.
Hence, my question is whether one can re-assert a clause automatically after it got retracted in a failing rule. I have achieved such a behavior using a meta-interpreter, but I was wondering whether this can also be encoded within the respective predicate directly.