If you take the โtabulationโ approach of previous conclusions and arguments with respect to the direct chain in order to derive new conclusions, a recursive โdepthโ is not required.
Keep in mind that Datalog requires some restrictions on the rules and variable, which guarantee the final completion and therefore the final number of conclusions. For example, variables must have a finite range of possible values.
Suppose your example refers to constants, not variables:
P(x,y) <- Q(x,y). Q(x,y) <- P(x,z), A(y).
One wrinkle is that you want A/1 be implemented as an extended stored procedure or external code. To do this, I would suggest mapping all the results of calling A to all possible arguments (finite number). All this is among the conclusions (provable statements) of your system.
Once this is done, the inference of the direct chain continues iteratively, not recursively. At each step, consider each rule, applying it with the assumptions (right-hand sides) of the conclusions that were previously obtained (deduced), if they give a new conclusion. If no rule gives new output in the current step, stop. The proof procedure is completed.
In your example, the evidence ends after all the facts of A are added, because there are no conclusions sufficient to apply any rule to get new conclusions.
source share