Package org.projog.core.predicate.udp
Class TailRecursivePredicateMetaData
- java.lang.Object
-
- org.projog.core.predicate.udp.TailRecursivePredicateMetaData
-
public final class TailRecursivePredicateMetaData extends java.lang.ObjectDefines 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:
TailRecursivePredicate
-
-
Field Summary
Fields Modifier and Type Field Description private ClauseModelfirstClauseprivate booleanisPotentialSingleResultprivate boolean[]isSingleResultIfArgumentImmutableprivate boolean[]isTailRecursiveArgumentprivate ClauseModelsecondClause
-
Constructor Summary
Constructors Modifier Constructor Description privateTailRecursivePredicateMetaData(java.util.List<ClauseModel> clauses)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static TailRecursivePredicateMetaDatacreate(KnowledgeBase kb, java.util.List<ClauseModel> clauses)Returns a newTailRecursivePredicateMetaDatarepresenting the user defined predicate defined by the specified clauses ornullif the predicate is not tail recursive.private TermgetFinalFunction(Term t)ClauseModelgetFirstClause()ClauseModelgetSecondClause()private static booleanisAntecedentRecursive(KnowledgeBase kb, ClauseModel secondTerm)booleanisPotentialSingleResult()booleanisSingleResultIfArgumentImmutable(int idx)private static booleanisTail(Term list, Term term)private static booleanisTailRecursive(KnowledgeBase kb, java.util.List<ClauseModel> terms)booleanisTailRecursiveArgument(int idx)
-
-
-
Field Detail
-
firstClause
private final ClauseModel firstClause
-
secondClause
private final ClauseModel secondClause
-
isPotentialSingleResult
private final boolean isPotentialSingleResult
-
isTailRecursiveArgument
private final boolean[] isTailRecursiveArgument
-
isSingleResultIfArgumentImmutable
private final boolean[] isSingleResultIfArgumentImmutable
-
-
Constructor Detail
-
TailRecursivePredicateMetaData
private TailRecursivePredicateMetaData(java.util.List<ClauseModel> clauses)
- See Also:
create(KnowledgeBase, List)
-
-
Method Detail
-
create
public static TailRecursivePredicateMetaData create(KnowledgeBase kb, java.util.List<ClauseModel> clauses)
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
private static boolean isTailRecursive(KnowledgeBase kb, java.util.List<ClauseModel> terms)
-
isAntecedentRecursive
private static boolean isAntecedentRecursive(KnowledgeBase kb, ClauseModel secondTerm)
-
getFirstClause
public ClauseModel getFirstClause()
-
getSecondClause
public ClauseModel getSecondClause()
-
isPotentialSingleResult
public boolean isPotentialSingleResult()
-
isTailRecursiveArgument
public boolean isTailRecursiveArgument(int idx)
-
isSingleResultIfArgumentImmutable
public boolean isSingleResultIfArgumentImmutable(int idx)
-
-