PublicShow sourceprolog_call_cycles.pl -- Find recursion in the static call graph

This library builds the static call graph of loaded Prolog code using library(prolog_codewalk) and finds the recursion in it: the strongly connected components (SCCs) of the graph and, within these, the left recursion. A call is in left position if, on some execution path through the clause body, no call to a non-built-in predicate precedes it, no built-in predicate with side effects such as reading input precedes it and no argument of the call is guaranteed to differ from the head argument it is derived from. Such arguments are proper subterms of a head argument, either directly or obtained using arg/3 or =../2, values computed from a head argument using an arithmetic operation that cannot return its input, such as N-1, and values that are tested to differ from a head argument, e.g., using \==/2. A call that is not left cannot be a variant of the calling goal. It may still recurse forever, but tabling does not help in that case.

Left recursion is a loop of calls in left position through which a goal may call a variant of itself. Such a loop runs forever under Prolog's SLD resolution. Loops are found by unfolding: starting from a call in left position, the goal of each call is unified with the head of a clause that has a call in left position, until we obtain a goal for the predicate we started with that unifies with the head we started with. Unification uses the occurs check and takes type tests such as atom/1 or var/1 into account. Clauses are skipped if an earlier clause that commits by a cut directly after the head or using single sided unification (=>) applies. Thus, p(count) :- p(sum(1)) or p(X) :- atom(X), !, p([X]) are not left recursive, while p(a) :- p(b) together with p(b) :- p(a) is.

The subterm condition makes recursion that consumes input, such as grammar rules that start with a terminal, not left recursive. Unifications (=/2) are taken into account, which is needed for the code produced by the DCG translation. Unifications inside a branch of a disjunction only affect the calls in that branch, where the unifications in the condition of an if-then-else also hold in its then-branch. If a closure is called on the elements of a list, as in maplist/2, the elements of a list that is derived from the head are subterms. For example, the recursive call in the first rule below is not in left position, while it is in the second:

ws --> [C], { code_type(C, space) }, !, ws.
lr --> lr, "a".

Each call edge inside an SCC is annotated with the clause and source location of the call and with an abstraction of the call's arguments. This information is used by library(prolog_auto_table) to decide which predicates to table, but is also useful for program analysis tools.

The scope of the analysis is defined by the options below. Only calls between predicates in scope become edges of the graph:

goal(:Goal)
Analyse the predicates that are reachable from Goal. If files or module are given as well, these restrict the reachable predicates to those defined in the given files or modules. Predicates in modules of class system are never included.
files(+FileOrFiles)
Analyse the predicates that have clauses in the given file(s), the dynamic predicates declared there and the foreign predicates of the modules they define. Files included using include/1 are part of the file that includes them: clauses in an included file belong to the file that loads it, and an included file given as FileOrFiles stands for the file(s) that load it.
module(+ModuleOrModules)
Analyse the predicates defined in Module. A list of modules is allowed.
predicates(+PIs)
Analyse the given (qualified) predicate indicators.
external(+Boolean)
If true (default false), also return the edges from predicates in scope to predicates outside the scope in the external key of the graph.

Other options are passed to prolog_walk_code/1, notably autoload(Bool), walk_meta_predicates(Bool) and infer_meta_predicates(Bool).

Source call_graph(-Graph:dict, +Options) is det
Compute the static call graph for the predicates selected by Options. Graph is a dict with the keys below. Predicates are represented by qualified predicate indicators, M:Name/Arity.
vertices
Sorted list of predicates in scope. For files(Files), these are the predicates with clauses in Files and the dynamic predicates declared in Files, including the files they include.
ugraph
The graph as a library(ugraphs) graph.
edges
List of edges between predicates in scope. Each edge is a dict edge{from:PI, to:PI, clause:ClauseRef, location:Dict}, where Location is the location dict of prolog_walk_code/1 (option on_edge). If a predicate calls another one multiple times, there is an edge for each call.
external
List of edges from predicates in scope to other predicates. Only filled if the option external(true) is given.
Source call_graph_sccs(-SCCs:list(dict), +Options) is det
Source call_graph_sccs(+Graph, -SCCs:list(dict), +Options) is det
Compute the strongly connected components of the call graph that contain at least one cycle, i.e., that have more than one member or a single member that calls itself. Each SCC is a dict with the keys below. The first form computes the graph using call_graph/2 from Options.
members
Sorted list of predicate indicators in the SCC.
edges
List of edges between members of the SCC. Each edge is a dict as described with call_graph/2, extended with the keys below.
left
true if the call is in left position and may take part in a loop (see module header). Note that a call with a subterm, computed or distinct argument is never in left position.
args
List of argument abstractions for the call. Each element is one of fresh (a variable that does not appear earlier in the clause), pass (a variable from the head), bound_earlier (a variable that appears in an earlier goal of the body), ground (a ground term), subterm (a proper subterm of a head argument, also if obtained using arg/3 or =../2), computed (the result of an arithmetic operation on a head argument that differs from its input, such as N-1), distinct (a value that is tested to differ from a head argument, e.g., using X \== Y) or constructed (a term with variables that is not a subterm of a head argument, e.g., an accumulator). If the call could not be analysed, this is unknown.
decreasing
true if this is a call to the same predicate and some argument is a proper subterm of the same argument in the head.
pruned
true if a cut can prune the call: a cut after the call in its clause, the cut of the if-then-else whose condition holds the call, \+/1, once/1, ignore/1 or forall/2. If the callee is tabled and part of the same SCC, such a cut raises a permission error.
barrier
none or the predicate through whose goal argument the call is made if that is a barrier for shift/1: findall/3 and the other predicates that collect all solutions, aggregate_all/3, foldall/4 and predicates defined in C. A tabled goal cannot suspend through a barrier to wait for an incomplete table.

If the option locations(true) is given (default), the location of the edges in the SCCs is refined by walking the involved clauses with source information.

Source left_transparent(:Goal) is semidet[multifile]
Multifile hook that is true if a call to Goal does not end the left position of the calls that follow it. This is intended for non built-in predicates that do not bind arguments and do not fail, such as debug/3.
Source left_recursion(-LeftRecursion:dict, +Options) is nondet
Source left_recursion(+SCCs, -LeftRecursion:dict, +Options) is nondet
Enumerate the left recursion in the call graph. For each set of predicates that are mutually recursive through calls in left position (see module header), LeftRecursion is a dict with the keys below. The first form computes the SCCs using call_graph_sccs/2 from Options.
members
Sorted list of predicate indicators involved.
cycle
List of edges (see call_graph_sccs/2) that form a shortest cycle through the first member.
edges
All left edges between the members.