Thunk (functional programming)

Thunk (functional programming)

In computer science, a thunk (also suspension, suspended computation or delayed computation) is a parameterless closure created to prevent the evaluation of an expression until forced at a later time. In lazy languages thunks are created and forced implicitly. In eager languages thunks can be created explicitly by wrapping an expression in a parameterless anonymous function and forced by calling it in order to emulate lazy evaluation. Some functional languages require functions to take at least one parameter, requiring the anonymous function to take a dummy parameter, usually of unit type, instead.

Contents

Call by name

Implementations of the call by name and call by need evaluation strategies typically use a thunk to refer to an incompletely evaluated expression. In this context, the thunk is simply a placeholder for a part of computation which, when executed, returns either the fully evaluated expression, or returns a tag, which shows that there is a part of the expression which is yet to be instantiated, in which case blind, mindless computation cannot yet be performed (because, for example, some variable in the above-mentioned algebraic formula is not yet mapped to a number — hence the formula does not yet reduce to a concrete number).

Call by need

In call-by-need, the thunk is replaced by its return value after its first complete execution. In languages with late binding (for example, Haskell), the "computation" performed by the thunk may include a lookup in the run-time context of the program to determine the current binding of a variable.

Thunks were invented by Peter Zilahy Ingerman[1] in 1961. According to the inventor, the name "thunk" came about because it could be optimized by the compiler by "thinking about it", so "thunk", according to its inventor, "is the past tense of 'think' at two in the morning"[2]

An early implementation of thunks for call-by-name was in Algol 60.

begin
   integer idx;
   real procedure sum (i, lo, hi, term);
      value lo, hi;
      integer i, lo, hi;
      real term;
      comment term is passed by-name, and so is i;
   begin
      real temp;
      temp := 0;
      for i := lo step 1 until hi do
         temp := temp + term;
      sum := temp
   end;
   print (sum (idx, 1, 100, 1/idx))
end

The above example (see Jensen's Device) relies on the fact that the actual parameters idx and 1/idx are passed "by name", so that the program is equivalent to the "inlined" version

begin
   integer idx;
   real sum;
   begin
      real temp;
      temp := 0;
      for idx := 1 step 1 until 100 do
         temp := temp + 1/idx;
      sum := temp
   end;
   print (sum)
end

Notice that the expression 1/idx is not evaluated at the point of the call to sum; instead, it is evaluated anew each time the formal parameter term is mentioned in the definition of sum. A compiler using thunks to implement call-by-name would process the original code as if it had been written using function pointers or lambdas (represented below in Algol-like pseudocode):

begin
   integer idx;
   real procedure sum (i_lvalue, lo, hi, term_rvalue);
      value lo, hi;
      integer lo, hi;
      thunk i_lvalue;
      thunk term_rvalue;
   begin
      real temp;
      temp := 0;
      for i_lvalue() := lo step 1 until hi do
         temp := temp + term_rvalue();
      sum := temp
   end;
   procedure lvalue_of_idx ();
   begin
       lvalue_of_idx := address of idx
   end;
   procedure rvalue_of_1_over_idx ();
   begin
       rvalue_of_1_over_idx := 1/idx
   end;
   print (sum (lvalue_of_idx, 1, 100, rvalue_of_1_over_idx))
end

The procedures lvalue_of_idx and rvalue_of_1_over_idx would be generated automatically by the compiler whenever a call-by-name actual parameter was encountered. These automatically generated procedures would be called thunks.

See also

References

External links

  • Thunk at the Haskell Wiki

Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • Thunk — The word thunk has at least three related meanings in computer science. A thunk may be: * a piece of code to perform a delayed computation (similar to a closure) * a feature of some virtual function table implementations (similar to a wrapper… …   Wikipedia

  • Lambda calculus — In mathematical logic and computer science, lambda calculus, also written as λ calculus, is a formal system designed to investigate function definition, function application and recursion. It was introduced by Alonzo Church and Stephen Cole… …   Wikipedia

  • Continuation-passing style — In functional programming, continuation passing style (CPS) is a style of programming in which control is passed explicitly in the form of a continuation. Gerald Jay Sussman and Guy L. Steele, Jr. coined the phrase in AI Memo 349 (1975), which… …   Wikipedia

  • Lazy evaluation — In computer programming, lazy evaluation (or delayed evaluation) is the technique of delaying a computation until such time as the result of the computation is known to be needed.The actions of lazy evaluation include: performance increases due… …   Wikipedia

  • Evaluation strategy — Evaluation strategies Strict evaluation Applicative order Call by value Call by reference Call by sharing Call by copy restore Non strict evaluation Normal order Call by name Call by need/Lazy evaluation …   Wikipedia

  • Windows API — The Windows API, informally WinAPI, is Microsoft s core set of application programming interfaces (APIs) available in the Microsoft Windows operating systems. It was formerly called the Win32 API; however, the name Windows API more accurately… …   Wikipedia

  • Extensible Firmware Interface — The Extensible Firmware Interface (EFI) is a specification that defines a software interface between an operating system and platform firmware. EFI is intended as a significantly improved replacement of the old legacy BIOS firmware interface… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”