| Did you know ... | Search Documentation: |
| library(prolog_call_cycles): 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:
system are never included.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.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).
M:Name/Arity.
files(Files), these
are the predicates with clauses in Files and the dynamic predicates
declared in Files, including the files they include.library(ugraphs) graph.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(true) is given.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.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.true if this is a call to the same predicate and some
argument is a proper subterm of the same argument in the head.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.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.