0

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.

false
  • 10,264
  • 13
  • 101
  • 209
jboockmann
  • 1,011
  • 2
  • 11
  • 27
  • 1
    While I am totally confused by what you are trying to achieve, maybe this might be what you seek. [Prolog planning using retract and assert](https://stackoverflow.com/q/55512086/1243762) – Guy Coder Apr 18 '19 at 14:12
  • 1
    It sounds to me like you're describing [Constraint Handling Rules (CHR)](http://www.swi-prolog.org/pldoc/man?section=chr), which definitely has this consumption-like evaluation scheme. – Daniel Lyons Apr 18 '19 at 14:21
  • 1
    Are you looking for an implementation of linear and intuitionistic assumptions? If so, see if the following example and bibliographic reference is relevant: https://github.com/LogtalkDotOrg/logtalk3/tree/master/examples/assumptions – Paulo Moura Apr 18 '19 at 17:52

0 Answers0