Content deleted Content added
m Bot: Migrating 2 interwiki links, now provided by Wikidata on d:q3558387 |
CortexFiend (talk | contribs) Link suggestions feature: 3 links added. |
||
(39 intermediate revisions by 32 users not shown) | |||
Line 1:
{{Short description|Programming language implementation problem}}
In [[computer science]], the '''funarg problem''' ''(function argument problem)'' refers to the difficulty in implementing [[first-class function]]s ([[function (programming)|function]]s as [[first-class object]]s) in programming language implementations
The difficulty only arises if the body of a [[nested function]] refers directly (i.e., not
There are two subtly different versions of the funarg problem. The '''upwards funarg problem''' arises from returning (or otherwise transmitting "upwards") a function from a function call. The '''downwards funarg problem''' arises from passing a function as a parameter to another function call.
==Upwards funarg problem==
When one function calls another during a typical program's execution, the local state of the caller (including [[parameter (computer science)|
One solution to the upwards funarg problem is to simply allocate all activation records from the [[
Some efficiency-minded compilers employ a hybrid approach in which the activation records for a function are allocated from the stack if the [[compiler]] is able to deduce, through [[static program analysis]], that the function creates no upwards funargs. Otherwise, the activation records are allocated from the heap.
Another solution is to simply copy the value of the variables into the closure at the time the closure is created. This will cause a different behavior in the case of mutable variables, because the state will no longer be shared between closures. But if it is known that the variables are constant, then this approach will be equivalent. The [[ML (programming language)|ML]] languages take this approach, since variables in those languages are bound to
Some languages allow the programmer to explicitly choose between the two behaviors. [[PHP]] 5.3's anonymous functions
===Example===
The following [[Haskell (programming language)|Haskell]]-
<code>[[Lambda calculus|λ]]</code> is the operator for constructing a new function, which
The problem in this case exists if the compose function allocates the parameter variables <code>
▲ compose f g = λx → f (g x)
▲<code>[[Lambda calculus|λ]]</code> is the operator for constructing a new function, which -- in this case -- has one argument, <code>''x''</code>, and returns the result of first applying <code>''g''</code> to <code>''x''</code> then applying <code>''f''</code> to that. This λ function carries the functions <code>''f''</code> and <code>''g''</code> (or pointers to them) as internal state.
▲The problem in this case exists if the compose function allocates the parameter variables <code>''f''</code> and <code>''g''</code> on the stack. When <code>''compose''</code> returns, the stack frame containing <code>''f''</code> and <code>''g''</code> is discarded. When the internal function <code>''λx''</code> attempts to access <code>''g''</code>, it will access a discarded memory area.
==Downwards funarg problem==
A downwards funarg may also refer to a function's state when that function is not actually executing. However, because, by definition, the existence of a downwards funarg is contained in the execution of the function that creates it, the
The downwards funarg problem complicates the efficient compilation of [[tail
==Practical implications==
Historically, the upwards funarg problem has proven to be
In [[functional language]]s, functions are first-class values
==See also==
* [[Closure (computer science)]]▼
* [[Functional programming]]
▲*[[Closure (computer science)]]
* [[
* [[
* [[
* [[Referential transparency]]
* [[Scope (programming)]]
* [[
==References==
Line 50 ⟶ 48:
==External links==
* [[Joseph Weizenbaum]]. [http://www.softwarepreservation.org/projects/LISP/MIT/Weizenbaum-FUNARG_Problem_Explained-1968.pdf "The FUNARG Problem Explained"], 1968.
*[http://www.soe.ucsc.edu/classes/cmps203/Winter11/09-lambda.ppt.pdf Bindings, Procedures, Functions, Functional Programming, and the Lambda Calculus]▼
* [[Joel Moses]]. [http://dspace.mit.edu/handle/1721.1/5854 "The Function of FUNCTION in LISP, or Why the FUNARG Problem Should be Called the Environment Problem"]. MIT AI Memo 199, 1970.
▲* [https://web.archive.org/web/20120722020445/http://
[[Category:Compiler construction]]
|