KnowledgeWorks and Prolog User Guide > 6 Advanced Topics > 6.2 Optimization > 6.2.3 Backward Chaining

NextPrevUpTopContentsIndex

6.2.3.2 Tail Recursion

The KnowledgeWorks Backward Chainer supports the transformation of "tail-recursive" calls into jumps. Thus, stack overflow can be avoided without resorting to "repeat, fail" loops in most cases. For example, given the definition:

(defrule run-forever :backward
                    ((run-forever)
                    <--
                     (run-forever)))

the call: (run-forever) will run forever without generating a stack overflow. Note that this optimization is not limited to recursive calls to the same rule. The last call of any rule will be compiled as a jump, drastically reducing stack usage.


KnowledgeWorks and Prolog User Guide (Macintosh version) - 22 Dec 2009

NextPrevUpTopContentsIndex