# Can an incrementally tabled predicate depend on itself?

**URL:** <https://swi-prolog.discourse.group/t/can-an-incrementally-tabled-predicate-depend-on-itself/3632>\
**Category:** General\
**Created:** [February 22, 2021, 1:16am UTC](https://swi-prolog.discourse.group/t/can-an-incrementally-tabled-predicate-depend-on-itself/3632 "2021-02-22T01:16:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![stassa.p](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/stassa.p/32/712_2.png) [@stassa.p](https://swi-prolog.discourse.group/u/stassa.p)\
**Post date:** [February 22, 2021, 1:16am UTC](https://swi-prolog.discourse.group/t/can-an-incrementally-tabled-predicate-depend-on-itself/3632/1 "2021-02-22T01:16:04Z")

</div>

Hi everyone,

Suppose I have the following predicate definitions:

```nohighlight
[1] p(X,Y):- p(X,Z), r(Z,Y). 
[2] p(X,Y):- r(X,Y).
[3] r(a,b). 
[4] r(b,c). 

```

Now, I would like to do the following:  
a) Retract clause [1] and call the goal `p(a,c)`.  
b) If `p(a,c)` succeeds, re-assert clause [1].  
c) Retract clause [2] and call the goal `p(a,c)`.  
d) If `p(a,c)` succeeds, re-assert clause [2].

The purpose is to find out which clauses of p/2 do not entail `p(a,c)`.

My problem is that p/2 is left-recursive and because of the ordering of its  
clauses will not terminate. If I table p/2, it will terminate, but then  
everytime I call `p(a,c)` after retracting a clause I will just get the result  
that was stored in a table in the first call to `p(a,c)`. So every single call  
will succeed- even the ones that depend on clauses removed from the database.

What can I do? I think that what I would like is to update the table for p/2  
everytime I call `p(a,c)`, so the query only succeeds if the _current_ definition  
(i.e. the clauses remaining in the database) of p/2 entails `p(a,c)`. Is something  
like that possible?

---

<div class="post-metadata">

**Author:** ![stassa.p](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/stassa.p/32/712_2.png) [@stassa.p](https://swi-prolog.discourse.group/u/stassa.p)\
**Post date:** [February 22, 2021, 1:23am UTC](https://swi-prolog.discourse.group/t/can-an-incrementally-tabled-predicate-depend-on-itself/3632/2 "2021-02-22T01:23:40Z")

</div>

Ah, never mind. I figured it out- I can declare p/2 itself as dynamic and incremental.

```prolog
...
table(p/2 as incremental)
,(dynamic([p/2], [incremental(true)]))
...

```

This seemed to do the trick for me anyway.

I’ll leave this here in case anyone else needs it 🙂
