Class TailRecursivePredicate
- All Implemented Interfaces:
Predicate
Predicate that are tail recursive.
It is common for Prolog developers to define predicates using recursion. Although recursive programs can be concise
and elegant they do require increased stack space for each iteration - which after many iterations will cause a
java.lang.StackOverflowError. Where it can determine it is safe to do, Projog converts recursive user defined
predicates into iterative versions - requiring a constant stack space regardless of the number of iterations. This
technique is known as tail recursion optimisation or last call optimisation. The algorithm for
implementing tail recursion optimisation is encapsulated in TailRecursivePredicate. Subclasses of
TailRecursivePredicate can implement the logic of evaluating the clauses of a specific user defined predicate
without having to redefine the tail recursion optimisation algorithm.
For a user defined predicate to be implemented using TailRecursivePredicate it must be judged as eligible for
tail recursion optimisation using the criteria used by TailRecursivePredicateMetaData.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidBacktracks the arguments to before the last attempt to match the first rule.final booleanevaluate()Attempts to satisfy the goal this instance represents.protected abstract voidlogCall()protected abstract voidlogExit()protected abstract voidlogFail()protected abstract voidlogRedo()protected abstract booleanMatch the first rule of the tail recursive predicate.protected abstract booleanMatch the second rule of the tail recursive predicate.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Predicate
couldReevaluationSucceed
-
Constructor Details
-
TailRecursivePredicate
public TailRecursivePredicate()
-
-
Method Details
-
evaluate
public final boolean evaluate()Description copied from interface:PredicateAttempts to satisfy the goal this instance represents.Calling this method multiple times on a single instance allows all possible answers to be identified. An attempt to find a solution carries on from where the last successful call finished.
If
PredicateFactory.isRetryable()returnsfalsethen this method should only be called once per individual query (no attempt should be made to find alternative solutions).If
PredicateFactory.isRetryable()returnstruethen, in order to find all possible solutions for an individual query, this method should be recalled on backtracking until it returnsfalse. -
matchFirstRule
protected abstract boolean matchFirstRule()Match the first rule of the tail recursive predicate.If the head of the first rule is matched then the rule has been successfully evaluated.
- Returns:
trueif the first rule is matched, elsefalse
-
matchSecondRule
protected abstract boolean matchSecondRule()Match the second rule of the tail recursive predicate.If the second rule is matched then the attempt at evaluating the rule continues for another level of recursion.
- Returns:
trueif the second rule is matched, elsefalse
-
backtrack
protected abstract void backtrack()Backtracks the arguments to before the last attempt to match the first rule. -
logCall
protected abstract void logCall() -
logRedo
protected abstract void logRedo() -
logExit
protected abstract void logExit() -
logFail
protected abstract void logFail()
-