The mode_analyser library is a tool that assists in the generation of the mode/1 directive for predicate definitions. This directive informs the compiler that the arguments of the specified predicate will always have the corresponding form when the predicate is called. The compiler utilises this information during compilation of the predicate in order to generate more compact and/or faster code. Specifying the mode of a predicate that has already been compiled has no effect, unless it is recompiled. If the specified procedure does not exist, a local undefined procedure is created.
The mode analyser inserts instrumentation into the clause definitions of predicates during compilation in order to record mode usage of each predicate argument. The code should then be run (as many times as is necessary to capture the most common invocations of each predicate undergoing analysis). Finally, the results of the analysis are requested and the suggested mode annotations for each predicate are displayed.
The usage is as follows:
?- lib(mode_analyser).
?- analyse(queen).
?- queen:queen([1,2,3,4],Out).
?- result([verbose:on])@queen.
This will print the results as follows:
Mode analysis for queen : qdelete / 4: Results for argument 1: -: 23 *: 0 +: 0 ++: 0 Results for argument 2: -: 0 *: 0 +: 0 ++: 23 Results for argument 3: -: 0 *: 0 +: 0 ++: 23 Results for argument 4: -: 0 *: 0 +: 23 ++: 0 qdelete(-, ++, ++, +) Mode analysis for queen : nodiag / 3: Results for argument 1: -: 0 *: 0 +: 0 ++: 62 Results for argument 2: -: 0 *: 0 +: 0 ++: 62 Results for argument 3: -: 0 *: 0 +: 0 ++: 62 nodiag(++, ++, ++) Mode analysis for queen : qperm / 2: Results for argument 1: -: 0 *: 0 +: 0 ++: 41 Results for argument 2: -: 0 *: 0 +: 41 ++: 0 qperm(++, +) Mode analysis for queen : queen / 2: Results for argument 1: -: 0 *: 0 +: 0 ++: 1 Results for argument 2: -: 1 *: 0 +: 0 ++: 0 queen(++, -) Mode analysis for queen : safe / 1: Results for argument 1: -: 0 *: 0 +: 0 ++: 38 safe(++)
NOTE: It is imperative to understand that the results of mode analysis are merely suggestions for the invocation modes of a predicate based on runtime information. If there are potential predicate invocation modes that were not exercised during runtime, the tool is unable to account for them in its analysis. For the mode specifier ’-’ the mode analyser does not determine whether the variable occurs in any other argument (i.e., is aliased), this must be manually verified. In summary, the programmer must verify that the suggested modes are correct before using the directive in the code. If the instantiation of the predicate call violates its mode declaration, no exception is raised and its behaviour is undefined.
For more details about invocation mode analysis see the description of mode_analyser in the Reference Manual.