Re: Pure functions?
- To: mathgroup at smc.vnet.net
- Subject: [mg93248] Re: [mg93204] Pure functions?
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Sat, 1 Nov 2008 05:07:39 -0500 (EST)
- References: <200810310805.DAA10553@smc.vnet.net> <3F585F49-E845-4DB9-9227-CB310FC67A5C@mimuw.edu.pl>
In case what I wrote was not entirely clear: the point about Church's and Mathematica's notion of "pure function" is that you can "apply it" at "the same time" as "defining it" as in : Function[x, x^2][3] 9 As you can see - there was no separate function definition and application. Compare this with f[x_] := x^2 f[3] 9 It is not the absence of name but the absence of separation between definition and application that matters. Andrzej Kozlowski On 31 Oct 2008, at 23:34, Andrzej Kozlowski wrote: > Mathematica's notion of "pure-function" is the same as the > mathematical (or logical) notion of function in Alonzo Church's > lambda-calculus. Such a function is determined by a set of bound > variables and a body which may contain free variables (same as what > you call parameters). This always has the form: > Function[{variables},body]. Mathematica's function > Function[{x,y},x^2+y^2] (2 bound variables) or Function[{x,y},x^2+ k > y^2] (2 bound variables and one free variable) are exactly the same > as functions in lambda-calculus. This (though not necessarily the > name "pure function") has been standard in logic since 1936, rather > longer than computers and programming languages. > Wikipedia's definition refers only to one meaning of pure function > in connection with computers - this meaning is much newer and much > less established. > > Anonymous functions are a somewhat different matter. They are merely > a matter of convenience and allow one to avoid having to choose > names both for the functions themselves and for bound variables. > Using names for bound variables is not only inconvenient but also > somewhat deceptive, since Function[x, x^2] and Function[y, y^2] are > exactly the same function, but different as Mathematica expressions. > Using Function[#^2] or #^2 & is not only more compact but also less > ambiguous (in the case of complex expression using named bound > variables it may not be easy to see if they are the same functions > or not. Note that evaluating > Function[x, x^2] == Function[y, y^2] > will not tell you that. > > I do not know how standard is the Mathematica usage of "pure" in > this context. In other functional languages pure functions are > usually simply called functions, but that is because they normally > have only "pure functions". Mathematica, however, also has > "functios" defined by means of patterns > > f[x_]:=x^2 > > so there was a need to distinguish the two kinds. The word > "anonymous" does refer to this distinction, in fact a pure function > may have a name: > > f=Function[x,x^2] > > and, in any case, "anonymous" normally refers not only to the fact > that the function does not have a name but that the bound variables > have unique standard names #1, #2,.... > > That's all, I think, there is to this issue and the Wikipedia > article seems to me a bit or a red herring. > > > Andrzej Kozlowski > > > > > > On 31 Oct 2008, at 17:05, AES wrote: > >> This is a follow-on to the thread "Notation using # with exponents >> and >> &", renamed to focus on the question in the Subject line. >> >> In asking questions about the term pure function, I'm not trying to >> be >> contentious or argumentative. I'm just trying to learn a bit about >> the >> concept. >> >> When I search on this term in Google, Wikipedia is the first hit that >> comes up, with the opening statement: >> >> -------------------- >> In computer programming, a function may be described as pure >> if both these statements about the function hold: >> >> 1. The function always evaluates the same result value given >> the same argument value(s). The function result value cannot >> depend on any hidden information or state that may change as >> program execution proceeds, nor can it depend on any external >> input from I/O devices. >> >> 2. Evaluation of the result does not cause any semantically >> observable side effect or output, such as mutation of mutable >> objects or output to I/O devices. >> >> The result value need not depend on all (or any) of the argument >> values. However, it must depend on nothing other than the >> argument values. >> --------------------- >> >> I appreciate that Wikipedia is not always authoritative; and its >> coverage of this particular topic is neither lengthy nor particularly >> detailed. Still, it's what's in there, at the moment. >> >> The Mathematica documentation for & opens with: >> >> --------------------- >> 'body &' is a pure function. The formal parameters are >> # (or #1), #2, etc. >> --------------------- >> >> and then shortly thereafter: >> >> --------------------- >> When Function[body] or body& is applied to a set of arguments, >> # (or #1) is replaced by the first argument, #2 by the second, >> and so on. #0 is replaced by the function itself. >> --------------------- >> >> So let's consider the constructs: >> >> f1 = #1^3 + #2^4 & >> f2 = #1^x + #2^y & >> >> both of which function perfectly well (I think) as pure functions >> in the >> Mathematica sense. >> >> My initial (and admittedly naive) interpretation is that f1 is also a >> pure function per the Wiki definition, because it "always >> evaluates the >> same result value given the same argument value(s)" and "the function >> result value [does not] depend on any hidden information or state >> that >> may change as program execution proceeds" (unless of course someone >> goes so far as to redefine the meanings of "3" or of "4"!). >> >> The function f2 is not pure in the Wiki sense, however (at least, >> that's >> my again admittedly naive interpretation), because f2[arg1,arg2] can >> give very different results when used in different parts of a >> program, >> since its result depends on "hidden" information (hidden in some >> sense, >> anyway) about the values that may have been assigned to the >> parameters x >> and y elsewhere in the program. >> >> And, given the quotes from the Mathematica definition, it seems clear >> that x and y are not "formal parameters" or accessible "arguments" of >> the function f2. So, the result of f2 does clearly "depend on >> [something] other than [just] the argument values". >> >> Finally, Mathematica seems to put some focus on the _unnamed_ (or >> "anonymous") property of its pure functions, while the Wiki statement >> makes no mention at all of this. >> >> So, what _is_ the real story on "pure functions"? >> >