ArticleslgStudy

science

Jensen's device

Jensen's device is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Jensen's device rather than just read about it. In short: Jensen's device is a computer programming technique that exploits call by name. It was devised by Danish computer scientist Jørn Jensen, who worked with Peter Naur at Regnecentralen.

Key takeaways

  • Jensen's device belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Jensen's device to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Jensen's device from memory before moving on to harder problems.

Reference excerpt

Jensen's device is a computer programming technique that exploits call by name. It was devised by Danish computer scientist Jørn Jensen, who worked with Peter Naur at Regnecentralen. They worked on the GIER ALGOL compiler, one of the earliest correct implementations of ALGOL 60. ALGOL 60 used call by name. During his Turing Award speech, Naur mentions his work with Jensen on GIER ALGOL.

Description 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, which is a consequence of the copy rule for procedures. ALGOL introduced call by name. A classic example of Jensen's device is a procedure that computes the sum of a series, ∑k=ℓuak:

real procedure Sum(k, l, u, ak) 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 (side-effected) k. For example, code to compute the sum of the first 100 terms of a [[real]] array V[] would be:

Sum(i, 1, 100, V[i]).

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[i]. Jensen's device is general. A double summation can be done as:

Sum(i, l, m, Sum(j, l, n, A[i,j]))

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(i,1,100,i);, if a sum of squares of integers, then Sum(i,1,100,i*i);, 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 (not the scope of the procedure). 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 (General Problem Solver), described in D. E. Knuth and J. N. Merner's ALGOL 60 confidential.

real procedure GPS(I, N, Z, V); real I, N, Z, V; begin for 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(I, if I=0 then -1.0 else I, P, if I=1 then 1.0 else if GPS(A, I, Z, if A=1 then 1.0 else if entier(A)×(entier(I)÷entier(A))=entier(I) ∧ A<I then 0.0 else Z) = Z then (if P<m then P+1 else I×GPS(A, 1.0, I, -1.0)) else P)

(Note: In the original paper, the expression near the end is GPS(A, 1.0. I, 0.0), due to a corner case in the specification of the semantics of ALGOL 60's for statement.)

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(n) procedure that increases its argument by one; the call increment(A[i]) 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(a, b) 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(i,A[i]) is problematic. Using the Copy Rule leads to the assignments:

temp := i; i := A[i]; A[i] := temp;

The problem is the second assignment changes i, so the A[i] 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 (with b being saved to temp instead of a) then the desired action would result, unless it were invoked as swap(A[i],i). (A safer swap() would do :

temp1 := i; temp2 := A[i]; i := temp2; A[i] := temp1;

.)

See also Call stack, stack frame, static link, and display (closure including environment link) Duff's device Funarg problem of how to pass and return functions as values Man or boy test, environment test

References

External links Standard, Storeless Semantics for ALGOL-Style Block Structure and Call-by-Name

Worked examples

Example 1 — a first encounter with Jensen's device

Start with the simplest possible case. Write down what Jensen's device claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Jensen's device before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Jensen's device ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Jensen's device

In research
Jensen's device appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Jensen's device in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Jensen's device is common in secondary-school and first-year university syllabi. It links to neighbouring topics Programming language design, so understanding it makes those chapters shorter.
In everyday life
Look for Jensen's device outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Jensen's device” →

How to study Jensen's device in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Jensen's device means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Jensen's device out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Jensen's device in simple terms?

Jensen's device is a computer programming technique that exploits call by name. It was devised by Danish computer scientist Jørn Jensen, who worked with Peter Naur at Regnecentralen.

Why does Jensen's device matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Jensen's device?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Jensen's device.

Tags

  • Programming language design

Keep exploring