loop_keys(+Edges, -LoopKeys) is det[private]
LoopKeys is an assoc holding the keys of the edges in left position that take part in a loop: a sequence of calls in left position through which a goal may call a variant of itself. Edges is a list of Key-Transition-Edge, where Transition is a term t(Head, Goal, Earlier, Fresh) that describes the call Goal in the clause with head Head. Earlier is a list of heads of the clauses before it that commit unconditionally (see commit_heads/2) and Fresh is the list of variables of Goal that are unbound when Goal is called.

For each transition, we search for a sequence of transitions, unifying the goal of each with the head of the next, that ends in a goal for the predicate we started with that unifies with the head we started with. Unification uses the occurs check and respects the constraints added by derive/4: dif/2 for values that differ from their source and the term kinds derived from type tests, where the fresh variables of the final goal are unbound. Thus, a goal for which the clause passes a proper subterm, a term that contains the argument, a term with a different principal functor, an arithmetic result that differs from the argument or a value that is tested to differ from the argument cannot be a variant. A sequence is pruned if a head is an instance of a head in its Earlier list. The search depth is limited to the number of transitions plus one, at most 10. If the search takes too many steps, all edges in left position are considered part of a loop.