HOME

TheInfoList



OR:

The man or boy test was proposed by computer scientist
Donald Knuth Donald Ervin Knuth ( ; born January 10, 1938) is an American computer scientist, mathematician, and professor emeritus at Stanford University. He is the 1974 recipient of the ACM Turing Award, informally considered the Nobel Prize of computer ...
as a means of evaluating implementations of the
ALGOL 60 ALGOL 60 (short for ''Algorithmic Language 1960'') is a member of the ALGOL family of computer programming languages. It followed on from ALGOL 58 which had introduced code blocks and the begin and end pairs for delimiting them, representing a k ...
programming language. The aim of the test was to distinguish
compiler In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
s that correctly implemented "
recursion Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematic ...
and non-local references" from those that did not.


Knuth's example

In
ALGOL 60 ALGOL 60 (short for ''Algorithmic Language 1960'') is a member of the ALGOL family of computer programming languages. It followed on from ALGOL 58 which had introduced code blocks and the begin and end pairs for delimiting them, representing a k ...
: begin real procedure A(k, x1, x2, x3, x4, x5); value k; integer k; real x1, x2, x3, x4, x5; begin real procedure B; begin k := k - 1; B := A := A(k, B, x1, x2, x3, x4) end; if k ≤ 0 then A := x4 + x5 else B end; outreal(A(10, 1, -1, -1, 1, 0)) end; This creates a tree of ''B'' call frames that refer to each other and to the containing ''A'' call frames, each of which has its own copy of ''k'' that changes every time the associated ''B'' is called. Trying to work it through on paper is probably fruitless, but for ''k'' = 10, the correct answer is −67, despite the fact that in the original article Knuth conjectured it to be −121. The survey article by
Charles H. Lindsey Charles Hodgson Lindsey was a British computer scientist, most known for his involvement with the programming language ALGOL 68. He was an editor of the ''Revised Report on Algol 68'', and co-wrote a ground breaking book on the language ''An In ...
mentioned in the references contains a table for different starting values. Even modern machines quickly run out of
stack Stack may refer to: Places * Stack Island, an island game reserve in Bass Strait, south-eastern Australia, in Tasmania’s Hunter Island Group * Blue Stack Mountains, in Co. Donegal, Ireland People * Stack (surname) (including a list of people ...
space for larger values of ''k'', which are tabulated below ().See "Performance and Memory" on the Rosetta Code
Man or Boy Page
.


Explanation

There are three Algol features used in this program that can be difficult to implement properly in a compiler: #
Nested function In computer programming, a nested function (or nested procedure or subroutine) is a function which is defined within another function, the ''enclosing function''. Due to simple recursive scope rules, a nested function is itself invisible outside ...
definitions: Since ''B'' is being defined in the local context of ''A'', the body of ''B'' has access to symbols that are local to ''A'' — most notably ''k'', which it modifies, but also ''x1'', ''x2'', ''x3'', ''x4'', and ''x5''. This is straightforward in the Algol descendant
Pascal Pascal, Pascal's or PASCAL may refer to: People and fictional characters * Pascal (given name), including a list of people with the name * Pascal (surname), including a list of people and fictional characters with the name ** Blaise Pascal, Frenc ...
, but not possible in the other major Algol descendant C (without manually simulating the mechanism by using C's address-of operator, passing around pointers to local variables between the functions). # Function references: The ''B'' in the recursive call A(k, B, x1, x2, x3, x4) is not a call to ''B'', but a reference to ''B'', which will be called only when ''k'' is greater than zero. This is straightforward in standard Pascal ( ISO 7185), and also in C. Some variants of Pascal (e.g. older versions of
Turbo Pascal Turbo Pascal is a software development system that includes a compiler and an integrated development environment (IDE) for the Pascal programming language running on CP/M, CP/M-86, and DOS. It was originally developed by Anders Hejlsberg at ...
) do not support procedure references, but when the set of functions that may be referenced is known beforehand (in this program it is only ''B''), this can be worked around. # Constant/function dualism: The ''x1'' through ''x5'' parameters of ''A'' may be numeric constants or references to the function ''B'' — the x4 + x5 expression must be prepared to handle both cases as if the formal parameters ''x4'' and ''x5'' had been replaced by the corresponding actual parameter (
call by name In a programming language, an evaluation strategy is a set of rules for evaluating expressions. The term is often used to refer to the more specific notion of a ''parameter-passing strategy'' that defines the kind of value that is passed to the ...
). This is probably more of a problem in
statically typed In computer programming, a type system is a logical system comprising a set of rules that assigns a property called a type to every "term" (a word, phrase, or other set of symbols). Usually the terms are various constructs of a computer program ...
languages than in dynamically typed languages, but the standard workaround is to reinterpret the constants 1, 0, and −1 in the main call to ''A'' as functions without arguments that return these values. These things are, however, not what the test is about; they are merely prerequisites for the test to at all be meaningful. What the test is ''about'' is whether the different references to ''B'' resolve to the ''correct'' instance of ''B'' — one that has access to the same ''A''-local symbols as the ''B'' that created the reference. A "boy" compiler might, for example, instead compile the program so that ''B'' always accesses the topmost ''A'' call frame.


See also

*
Funarg problem In computer science, the funarg problem ''(function argument problem)'' refers to the difficulty in implementing first-class functions (functions as first-class objects) in programming language implementations so as to use stack-based memory allocat ...
* Jensen's device


References


External links


Man or boy test
examples in many programming languages {{Donald Knuth navbox Articles with example ALGOL 60 code Programming language design Compiler construction Donald Knuth Programming language folklore Test items in computer languages Computer-related introductions in 1964 ALGOL 60 implementation