Thunk |
The term thunk is a contrived word from computer science, and has no known root.
It has two possible meanings:
= Thunk as delayed computation =
==Call by name==
Implementations of the Evaluation strategy#Call by name and Evaluation strategy#Call by need evaluation strategy typically use a thunk to refer to the function s argument. In this context, the thunk is simply a computation that, when executed, returns the value (if any) of the argument.
In call-by-need, after the thunk executes once it is replaced with the value returned by the first evaluation of the thunk. In languages with late binding, 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.
An early implementation of thunks for call-by-name was in early
==Functional programming==
In functional programming, a thunk is a nullary function -- one that takes no arguments. Thunks are frequently used in strict programming language as a means of simulating call-by-name evaluation; the thunk itself delays the computation of a function s argument, and the function forces the thunk to obtain the actual value. In Common Lisp, constant-valued thunks can be created with the constantly function: (constantly 5) evaluates to a nullary function that, when called, always yields the value 5.
= Thunk as compatibility-mapping =
==OS/2 & Windows 16-bit address hack==
A piece of code is executed to provide an address. The most common usage is in the Win16 / Win32 API, where thunking is used to convert a 16 bit address into a 32 bit equivalent or vice versa. One ubiquitous early example was wsock32.dll , a thunking layer added to allow Win32 Internet applications to use the Win16 winsock library originally written for Windows 3.x. Similar thunking was required in many cases in OS/2 2.x -- while most of the operating system was 32-bit, many parts of the kernel and device drivers were 16-bit for compatibility reasons.
==Thunks in dynamic linking==
Certain implementations of relocatable code local thunks to call library functions. Dynamic library calls in the code jump to thunks in a jump table; the jump table is replaced (by a dynamic linker) with short functions that either load the applicable library (as needed) or jump to the appropriate point in an already-loaded library. This form of thunk performs essentially the same task as the thunk-as-delayed-computation in call-by-need evaluation; the difference is largely one of perception.
==Thunks in virtual memory==
Software-based virtual memory systems may use a thunk to perform the mapping from virtual addresses to physical addresses; however, most modern systems do this computation in a specialized memory management unit in hardware.
=See also=
|
|
