Jensen's device exploits call by name and side-effects. Call by name is an argument passing convention that delays the evaluation of an argument until it is actually used in the procedure. Algol introduced call by name. A classic example of Jensen's device is a procedure that computes the sum of a series, : real procedure Sum value l, u; integer k, l, u; real ak; comment k and ak are passed by name; begin real s; s := 0; for k := l step 1 until u do s := s + ak; Sum := s end; In the procedure, the index variable k and summation term ak are passed by name. Call by name enables the procedure to change the value of the index variable during execution of the for loop. Call by name also causes the ak argument to be reevaluated during each iteration of the loop. Typically, ak will depend upon the changing k. For example, code to compute the sum of the first 100 terms of a real array V would be: Sum. During the execution of Sum, the actual argument i will increment during each step of the for loop, and each of the procedure's evaluations of ak will use the current value of i to access the successive array elements V. Jensen's device is general. A double summation can be done as: Sum The Sum function can be employed for arbitrary functions merely by employing the appropriate expressions. If a sum of integers were desired the expression would be just Sum;, if a sum of squares of integers, then Sum;, and so on. A slight variation would be suitable for initiating a numerical integration of an expression by a method very similar to that of Sum. The evaluation of ak is implemented with a thunk, which is essentially a subroutine with an environment. The thunk is a closure with no arguments. Each time a procedure needs the value of its formal argument, it simply calls the thunk. The thunk evaluates the actual argument in the scope of the calling code. In the absence of this pass-by-name facility, it would be necessary to define functions embodying those expressions to be passed according to the protocols of the computer language, or to create a compendium function along with some arrangement to select the desired expression for each usage.
GPS
Another example is GPS, described in D. E. Knuth and J. N. Merner's ALGOL 60 confidential. realprocedure GPS; real I, N, Z, V; beginfor I := 1 step 1 until N do Z := V; GPS := 1 end; Following is a single statement which finds m-th prime using GPS. I := GPS×÷entier)=entier ∧ Athen 0.0 else Z) = Z then else P)
Criticism
Jensen's device relies on call by name, but call by name is subtle and has some problems. Consequently, call by name is not available in most languages. Knuth comments that ALGOL 60 cannot express an increment procedure that increases its argument by one; the callincrement does not do the expected action if i is a functional that changes with each access. Knuth says, "The use of 'macro' definition facilities to extend language, instead of relying solely on procedures for this purpose, results in a more satisfactory running program." Others point out that a call by name procedure that swaps its argument can have subtle problems. An obvious swapping procedure is: procedure swap integer a, b; begin integer temp; temp := a; a := b; b := temp; end;''' The procedure does the right thing for many arguments, but the invocation of swap is problematic. Using the Copy Rule leads to the assignments: temp := i; i := A; A := temp; The problem is the second assignment changes i, so the A in the third assignment probably will not be the same array element as at the start. If on the other hand the procedure were to be coded the other way around then the desired action would result, unless it were invoked as swap