Haskell (1990) 相当明显,但纯度并不明确。 GCC 的 C 对于各种不同级别的“纯”具有各种函数属性。
几本书:C 编程语言的基本原理 (1990) 使用该术语,编程语言及其定义(1984)。然而,两者显然都只使用了一次! Programming the IBM Personal Computer, Pascal(也是 1984 年)使用了这个术语,但从 Google 的有限观点来看,并不清楚 Pascal 编译器是否支持它。 (我怀疑不是。)
一个有趣的说明是,Ada 的前身 Green,实际上有相当严格的“函数”定义 - 甚至不允许内存分配。然而,它在成为 Ada 之前就被放弃了,其中函数可以有副作用(I/O 或全局变量),但不能修改它们的参数。
To answer your first question, mathematical functions have often been described as "pure" in terms of some specified variables. e.g.:
the first term is a pure function of x and the second term is a pure function of y
Because of this, I don't think you'll find a true "first" occurrence.
For programming languages, a little searching shows that Ada 95 (pragma Pure), High Performance Fortran (1993) (PURE) and VHDL-93 (pure) all contain formal notions of 'pure functions'.
Haskell (1990) is fairly obvious, but purity isn't explicit. GCC's C has various function attributes for various differing levels of 'pure'.
A couple of books: Rationale for the C programming language (1990) uses the term, as does Programming Languages and their Definitions (1984). However, both apparently only use it once! Programming the IBM Personal Computer, Pascal (also 1984) uses the term, but it isn't clear from Google's restricted view whether or not the Pascal compiler had support for it. (I suspect not.)
An interesting note is that Green, the predecessor to Ada, actually had a fairly strict 'function' definition - even memory allocation was disallowed. However, this was dropped before it became Ada, where functions can have side-effects (I/O or global variables), but can't modify their arguments.
C28-6571-3 (the first PL/I reference manual, written before the compiler) shows that PL/I had support for pure functions, in the form of the REDUCIBLE (= pure) attribute, as far back as 1966 - when the compiler was first released. (This also answers your third question.)
This last document specifically notes that it includes REDUCIBLE as a new change since document C28-6571-2. So REDUCIBLE, which is possibly the first incarnation of formal pure functions in programming languages, appeared somewhere between January and July 1966.
Update: The earliest instance of "pure function" on Google Groups in this sense is from 1988, which easily postdates the book references.
命令式编程语言本来可以抵制这种趋势。但是,一旦 C 决定废除“过程”的概念并称其为“空函数”,他们就没有什么立足之地了。
A couple of myths:
The term "pure functional" doesn't come from mathematics, where all functions are by nature "pure" and, so, there was never any need to call anything a "pure function".
The term doesn't come from imperative programming. The early imperative programming languages, Fortran, Algol 60, Pascal etc., always had two kinds of abstractions: "functions" that produced results based on their inputs and "procedures" which took some inputs and did an action. It was considered good programming practice for "functions" not to have side effects. There was no need for them to have side effects because one could always use procedures instead.
So, where else could the term "pure functional" have come from? The answer is - sort of- obvious. It came from impure functional programming languages, the foremost among them being Lisp. Lisp was designed sometime between 1958 and 1960 (between the first and second reports of Algol 60, whose design McCarthy was involved in, but didn't feel satisfied with). Lisp's design was based fundamentally on functional programming. However, it also allowed side-effects as a pragmatic choice. It did not have a notion of a command or a procedure. So, in Lisp, one mostly wrote "pure functions", but occasionally, one wrote "impure functions," i.e., functions with side-effects, to get something done. The terms "pure Lisp" or "purely functional subset of Lisp" have been in use for a long time. Slowly, by osmosis, this idea of "purity" has come to invade all our space.
The imperative programming languages could have resisted the trend. But, once C decided to abolish the idea of "procedures" and call them "void functions" instead, they didn't have much of a leg to stand on.
of a branch of science, done for its own sake instead of serving another branch of science.
It should be obvious that the behavior of interacting functions is easiest to reason about when they are influenced only by their inputs, and they themselves influence only their outputs. Therefore it is inevitable that these kinds of functions will be noticed and classified. Now what word could we use to describe a function with such properties? "free of foreign material or pollutants" and "free of immoral behavior or qualities" seem to describe this rather well.
Who first used the word "pure" in that way, and when?
I am much too young to answer this with any degree of confidence. I argue, however, that it was inevitable that the word pure (or some very close synonym) would be used to describe functions that behave in this way.
Are there other words that mean roughly the same thing?
You said it yourself: "referentially transparent". However, you seem to suggest that "referential transparency" encompasses only part of the meaning of the phrase "pure function". I disagree; I feel it is entirely synonymous. From Wikipedia > Referential Transparency:
An expression is said to be referentially transparent if it can be replaced with its value without changing the behavior of a program. (emphasis mine)
The Haskell community sometimes uses the adjective "safe" in a similar manner. (See the Safe library, made to avoid throwing exceptions. Contrast with unsafePerformIO)
The concept of a function originated in mathematics. The mathematical concept of a function is more-or-less a mapping from one set onto another. In this sense it's impossible for functions to have side effects; not because they're "better" that way or because they're specifically defined as to not have side effects, but because the concept of "having side effects" doesn't make any sense with this definition of a function. Mathematical functions aren't a series of steps that execute, so how could any of those steps somehow "affect" other mathematical objects you're talking about?
When people started studying computation, they became interested in machine-implementable algorithms for computing the values of mathematical functions given their inputs. People started talking about computable functions. But functions as implemented in a computer (in imperative languages at least, which are what programmers first worked with) are a series of executable steps, which obviously can have side effects.
So it became natural for programmers to think about functions as algorithms, not as mathematical functions. So then a pure function is one that is purely a mathematical function, to which all the hundreds of years of theory about functions applies, as opposed to the generalised programmer's function, which can't be reasoned about that way.
发布评论
评论(5)
为了回答你的第一个问题,数学函数通常在某些指定变量方面被描述为“纯”函数。例如:
因此,我认为你不会找到真正的“第一个” ”的发生。
对于编程语言,稍微搜索一下就会发现 Ada 95 (
pragma纯
), 高性能 Fortran (1993) (PURE< /代码>) 和 VHDL-93 (
pure
) 都包含“纯粹”的正式概念功能”。Haskell (1990) 相当明显,但纯度并不明确。 GCC 的 C 对于各种不同级别的“纯”具有各种函数属性。
几本书:C 编程语言的基本原理 (1990) 使用该术语,编程语言及其定义(1984)。然而,两者显然都只使用了一次! Programming the IBM Personal Computer, Pascal(也是 1984 年)使用了这个术语,但从 Google 的有限观点来看,并不清楚 Pascal 编译器是否支持它。 (我怀疑不是。)
一个有趣的说明是,Ada 的前身 Green,实际上有相当严格的“函数”定义 - 甚至不允许内存分配。然而,它在成为 Ada 之前就被放弃了,其中函数可以有副作用(I/O 或全局变量),但不能修改它们的参数。
C28-6571-3(第一个 PL/I 参考手册(在编译器之前编写)表明 PL/I 支持纯函数,其形式为
REDUCIBLE
(= pure) 属性,早在 1966 年 - 当编译器首次发布时。 (这也回答了您的第三个问题。)最后一个文档特别指出,它包含
REDUCIBLE
作为自文档 C28-6571-2 以来的新更改。因此,REDUCIBLE
(这可能是编程语言中正式纯函数的第一个化身)出现在 1966 年 1 月到 7 月之间。更新:Google 网上论坛上最早的“纯函数”实例是这个意义上的来自 1988 年,轻松地更新书籍参考文献。
To answer your first question, mathematical functions have often been described as "pure" in terms of some specified variables. e.g.:
Because of this, I don't think you'll find a true "first" occurrence.
For programming languages, a little searching shows that Ada 95 (
pragma Pure
), High Performance Fortran (1993) (PURE
) and VHDL-93 (pure
) all contain formal notions of 'pure functions'.Haskell (1990) is fairly obvious, but purity isn't explicit. GCC's C has various function attributes for various differing levels of 'pure'.
A couple of books: Rationale for the C programming language (1990) uses the term, as does Programming Languages and their Definitions (1984). However, both apparently only use it once! Programming the IBM Personal Computer, Pascal (also 1984) uses the term, but it isn't clear from Google's restricted view whether or not the Pascal compiler had support for it. (I suspect not.)
An interesting note is that Green, the predecessor to Ada, actually had a fairly strict 'function' definition - even memory allocation was disallowed. However, this was dropped before it became Ada, where functions can have side-effects (I/O or global variables), but can't modify their arguments.
C28-6571-3 (the first PL/I reference manual, written before the compiler) shows that PL/I had support for pure functions, in the form of the
REDUCIBLE
(= pure) attribute, as far back as 1966 - when the compiler was first released. (This also answers your third question.)This last document specifically notes that it includes
REDUCIBLE
as a new change since document C28-6571-2. SoREDUCIBLE
, which is possibly the first incarnation of formal pure functions in programming languages, appeared somewhere between January and July 1966.Update: The earliest instance of "pure function" on Google Groups in this sense is from 1988, which easily postdates the book references.
一些误解:
术语“纯函数”并非来自数学,所有函数本质上都是“纯”的,因此,从来没有必要将任何东西称为“纯函数”。
该术语并非来自命令式编程。早期的命令式编程语言,Fortran、Algol 60、Pascal 等,总是有两种抽象:根据输入产生结果的“函数”和接受一些输入并执行操作的“过程”。 “函数”不产生副作用被认为是良好的编程实践。它们不需要产生副作用,因为人们总是可以使用过程来代替。
那么,“纯函数”这个词还能从哪里来呢?答案是——有点——显而易见。它来自不纯的函数式编程语言,其中最重要的是 Lisp。 Lisp 是在 1958 年至 1960 年之间设计的(Algol 60 的第一个和第二个报告之间,麦卡锡参与了该设计,但对其并不满意)。 Lisp 的设计基本上基于函数式编程。然而,作为一种务实的选择,它也允许产生副作用。它没有命令或过程的概念。因此,在 Lisp 中,人们大多会编写“纯函数”,但偶尔也会编写“不纯函数”,即带有副作用的函数,以完成某件事。术语“纯 Lisp”或“Lisp 的纯函数子集”已经使用了很长时间。慢慢地,通过渗透,这种“纯粹”的想法已经侵入我们所有的空间。
命令式编程语言本来可以抵制这种趋势。但是,一旦 C 决定废除“过程”的概念并称其为“空函数”,他们就没有什么立足之地了。
A couple of myths:
The term "pure functional" doesn't come from mathematics, where all functions are by nature "pure" and, so, there was never any need to call anything a "pure function".
The term doesn't come from imperative programming. The early imperative programming languages, Fortran, Algol 60, Pascal etc., always had two kinds of abstractions: "functions" that produced results based on their inputs and "procedures" which took some inputs and did an action. It was considered good programming practice for "functions" not to have side effects. There was no need for them to have side effects because one could always use procedures instead.
So, where else could the term "pure functional" have come from? The answer is - sort of- obvious. It came from impure functional programming languages, the foremost among them being Lisp. Lisp was designed sometime between 1958 and 1960 (between the first and second reports of Algol 60, whose design McCarthy was involved in, but didn't feel satisfied with). Lisp's design was based fundamentally on functional programming. However, it also allowed side-effects as a pragmatic choice. It did not have a notion of a command or a procedure. So, in Lisp, one mostly wrote "pure functions", but occasionally, one wrote "impure functions," i.e., functions with side-effects, to get something done. The terms "pure Lisp" or "purely functional subset of Lisp" have been in use for a long time. Slowly, by osmosis, this idea of "purity" has come to invade all our space.
The imperative programming languages could have resisted the trend. But, once C decided to abolish the idea of "procedures" and call them "void functions" instead, they didn't have much of a leg to stand on.
它来自“函数”的数学定义,函数不可能有副作用。
It comes from the mathematical definition of "function", where it is not possible for functions to have side effects.
为什么用“纯”这个词来描述具有这些属性的函数?
来自维基词典 >纯粹#形容词
显然,当交互函数仅受输入影响且它们本身仅影响输出时,它们的行为最容易推理。因此,这类功能受到关注和分类是不可避免的。现在我们可以用什么词来描述具有这些属性的函数呢? “没有异物或污染物”和“没有不道德行为或品质”似乎很好地描述了这一点。
谁第一次以这种方式使用“纯粹”这个词,什么时候?
我还太年轻,无法以任何程度的自信回答这个问题。然而,我认为“纯粹”这个词(或一些非常接近的同义词)不可避免地会被用来描述以这种方式运行的函数。
还有其他词的意思大致相同吗?
你自己说过:“参照透明”。但是,您似乎建议“引用透明度”仅包含短语“纯函数”的部分含义。我不同意;我觉得这完全是同义词。来自 维基百科 >参考透明度:
Haskell 社区有时以类似的方式使用形容词“安全”。 (请参阅 Safe 库,以避免与
unsafePerformIO
对比)我现在想不出任何其他同义词。
Why is the word "pure" used to describe functions with those properties?
From Wiktionary > pure # adjective
It should be obvious that the behavior of interacting functions is easiest to reason about when they are influenced only by their inputs, and they themselves influence only their outputs. Therefore it is inevitable that these kinds of functions will be noticed and classified. Now what word could we use to describe a function with such properties? "free of foreign material or pollutants" and "free of immoral behavior or qualities" seem to describe this rather well.
Who first used the word "pure" in that way, and when?
I am much too young to answer this with any degree of confidence. I argue, however, that it was inevitable that the word pure (or some very close synonym) would be used to describe functions that behave in this way.
Are there other words that mean roughly the same thing?
You said it yourself: "referentially transparent". However, you seem to suggest that "referential transparency" encompasses only part of the meaning of the phrase "pure function". I disagree; I feel it is entirely synonymous. From Wikipedia > Referential Transparency:
The Haskell community sometimes uses the adjective "safe" in a similar manner. (See the Safe library, made to avoid throwing exceptions. Contrast with
unsafePerformIO
)I can't think of any other synonyms right now.
函数的概念起源于数学。函数的数学概念或多或少是从一个集合到另一个集合的映射。从这个意义上说,函数不可能有副作用;不是因为它们这样“更好”,也不是因为它们被明确定义为没有副作用,而是因为“有副作用”的概念对于函数的这种定义没有任何意义。数学函数不是一系列执行的步骤,那么这些步骤中的任何一个如何以某种方式“影响”您正在谈论的其他数学对象呢?
当人们开始研究计算时,他们对机器可实现的算法感兴趣,这些算法用于计算给定输入的数学函数的值。人们开始谈论可计算函数。但是在计算机中实现的函数(至少在命令式语言中,这是程序员首先使用的)是一系列可执行步骤,显然可以< /em> 有副作用。
因此,程序员很自然地将函数视为算法,而不是数学函数。因此,纯函数是纯粹的数学函数,数百年来有关函数的理论都适用于它,而不是广义的程序员的函数,后者无法推理出这一点方式。
The concept of a function originated in mathematics. The mathematical concept of a function is more-or-less a mapping from one set onto another. In this sense it's impossible for functions to have side effects; not because they're "better" that way or because they're specifically defined as to not have side effects, but because the concept of "having side effects" doesn't make any sense with this definition of a function. Mathematical functions aren't a series of steps that execute, so how could any of those steps somehow "affect" other mathematical objects you're talking about?
When people started studying computation, they became interested in machine-implementable algorithms for computing the values of mathematical functions given their inputs. People started talking about computable functions. But functions as implemented in a computer (in imperative languages at least, which are what programmers first worked with) are a series of executable steps, which obviously can have side effects.
So it became natural for programmers to think about functions as algorithms, not as mathematical functions. So then a pure function is one that is purely a mathematical function, to which all the hundreds of years of theory about functions applies, as opposed to the generalised programmer's function, which can't be reasoned about that way.