The 91 function was introduced in papers published by Zohar Manna, Amir Pnueli and John McCarthy in 1970. These papers represented early developments towards the application of formal methods to program verification. The 91 function was chosen for being nested-recursive (contrasted with single recursion, such as defining f ( n ) {\displaystyle f(n)} by means of f ( n − 1 ) {\displaystyle f(n-1)} ). The example was popularized by Manna's book, Mathematical Theory of Computation (1974). As the field of Formal Methods advanced, this example appeared repeatedly in the research literature. In particular, it is viewed as a "challenge problem" for automated program verification.
It is easier to reason about tail-recursive control flow, this is an equivalent (extensionally equal) definition:
As one of the examples used to demonstrate such reasoning, Manna's book includes a tail-recursive algorithm equivalent to the nested-recursive 91 function. Many of the papers that report an "automated verification" (or termination proof) of the 91 function only handle the tail-recursive version.
This is an equivalent mutually tail-recursive definition:
A formal derivation of the mutually tail-recursive version from the nested-recursive one was given in a 1980 article by Mitchell Wand, based on the use of continuations.
Example A:
Example B:
Here is an implementation of the nested-recursive algorithm in Lisp:
Here is an implementation of the nested-recursive algorithm in Haskell:
Here is an implementation of the nested-recursive algorithm in OCaml:
Here is an implementation of the tail-recursive algorithm in OCaml:
Here is an implementation of the nested-recursive algorithm in Python:
Here is an implementation of the nested-recursive algorithm in C:
Here is an implementation of the tail-recursive algorithm in C:
Here is a proof that the McCarthy 91 function M {\displaystyle M} is equivalent to the non-recursive algorithm M ′ {\displaystyle M'} defined as:
For n > 100, the definitions of M ′ {\displaystyle M'} and M {\displaystyle M} are the same. The equality therefore follows from the definition of M {\displaystyle M} .
For n ≤ 100, a strong induction downward from 100 can be used:
For 90 ≤ n ≤ 100,
This can be used to show M(n) = M(101) = 91 for 90 ≤ n ≤ 100:
M(n) = M(101) = 91 for 90 ≤ n ≤ 100 can be used as the base case of the induction.
For the downward induction step, let n ≤ 89 and assume M(i) = 91 for all n < i ≤ 100, then
This proves M(n) = 91 for all n ≤ 100, including negative values.
Donald Knuth generalized the 91 function to include additional parameters.1 John Cowles developed a formal proof that Knuth's generalized function was total, using the ACL2 theorem prover.2
Knuth, Donald E. (1991). "Textbook Examples of Recursion". Artificial Intelligence and Mathematical Theory of Computation: 207–229. arXiv:cs/9301113. Bibcode:1993cs........1113K. doi:10.1016/B978-0-12-450010-5.50018-9. ISBN 9780124500105. S2CID 6411737. 9780124500105 ↩
Cowles, John (2013) [2000]. "Knuth's generalization of McCarthy's 91 function". In Kaufmann, M.; Manolios, P.; Strother Moore, J (eds.). Computer-Aided reasoning: ACL2 case studies. Kluwer Academic. pp. 283–299. ISBN 9781475731880. 9781475731880 ↩