Class TailRecursivePredicateMetaData
java.lang.Object
org.projog.core.predicate.udp.TailRecursivePredicateMetaData
Defines the characteristics of a tail recursive user defined predicate.
Projog uses the following rules to determine if a user defined predicate is "tail recursive" (and therefore suitable
for tail recursion optimisation using a TailRecursivePredicate):
- The user defined predicate must consist of exactly 2 rules.
- It must be possible to detect, at the point that the user defined predicate is defined, that the antecedent of the first rule will never generate multiple solutions per-query.
- If the antecedent of the second rule is not a conjunction, it must be a call to itself (i.e. the user defined predicate being defined) - this is what makes the predicate recursive.
- If the antecedent of the second rule is a conjunction, the final element (i.e. the tail) of the conjunction must be a call to itself (i.e. the user defined predicate being defined) - this is what makes the predicate recursive. It must be possible to detect, at the point that the user defined predicate is defined, that all elements prior to the final element of the conjunction will never generate multiple solutions per-query.
Examples of tail recursive predicates suitable for tail recursion optimisation:
:- list([]). list([X|Xs]) :- list(Xs).
r(N). r(N) :- N > 1, N1 is N-1, r(N1).
writeAndRepeat(N) :- write(N), nl. writeAndRepeat(N) :- N > 1, N1 is N-1, writeAndRepeat(N1).
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ClauseModelprivate final booleanprivate final boolean[]private final boolean[]private final ClauseModel -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncreate(KnowledgeBase kb, List<ClauseModel> clauses) Returns a newTailRecursivePredicateMetaDatarepresenting the user defined predicate defined by the specified clauses ornullif the predicate is not tail recursive.private Termprivate static booleanisAntecedentRecursive(KnowledgeBase kb, ClauseModel secondTerm) booleanbooleanisSingleResultIfArgumentImmutable(int idx) private static booleanprivate static booleanisTailRecursive(KnowledgeBase kb, List<ClauseModel> terms) booleanisTailRecursiveArgument(int idx)
-
Field Details
-
firstClause
-
secondClause
-
isPotentialSingleResult
private final boolean isPotentialSingleResult -
isTailRecursiveArgument
private final boolean[] isTailRecursiveArgument -
isSingleResultIfArgumentImmutable
private final boolean[] isSingleResultIfArgumentImmutable
-
-
Constructor Details
-
TailRecursivePredicateMetaData
- See Also:
-
-
Method Details
-
create
Returns a newTailRecursivePredicateMetaDatarepresenting the user defined predicate defined by the specified clauses ornullif the predicate is not tail recursive.- Parameters:
clauses- the clauses that the user defined predicate consists of- Returns:
- a new
TailRecursivePredicateMetaDataornullif the predicate is not tail recursive
-
isTailRecursive
-
isAntecedentRecursive
-
isTail
-
getFinalFunction
-
getFirstClause
-
getSecondClause
-
isPotentialSingleResult
public boolean isPotentialSingleResult() -
isTailRecursiveArgument
public boolean isTailRecursiveArgument(int idx) -
isSingleResultIfArgumentImmutable
public boolean isSingleResultIfArgumentImmutable(int idx)
-