41

A pure function is one that has no side effects -- it cannot do any kind of I/O and it cannot modify the state of anything -- and it is referentially transparent -- when called multiple times with the same inputs, it always gives the same outputs.

Why is the word "pure" used to describe functions with those properties? Who first used the word "pure" in that way, and when? Are there other words that mean roughly the same thing?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Tyler
  • 21,762
  • 11
  • 61
  • 90

5 Answers5

24

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.

porges
  • 30,133
  • 4
  • 83
  • 114
  • I think you mean "on usenet", because Google Groups didn't exist in 1988. Unless current Google Groups aggregates old (mailing list?) archives beyond usenet newsgroups from that era, in this case it's just a tool for searching usenet archives. – Peter Cordes Mar 05 '19 at 09:48
  • No, I really do mean on Google Groups. I searched Google Groups, I did not search Usenet. I do not have a personal archive of Usenet, nor do I presume that Google Groups has a comprehensive archive of every Usenet post ever made. Google Groups also includes its own non-Usenet forums, so the sets of 'posts to Usenet' and 'posts in Google Groups' do not coincide. – porges Mar 06 '19 at 22:55
  • Also, it's worth noting that as of 2015 Google removed the search-by-date functionality so it's no longer possible to do this search: https://motherboard.vice.com/en_us/article/jp5a77/google-a-search-company-has-made-its-internet-archive-impossible-to-search – porges Mar 06 '19 at 22:56
  • Ok, that's a fair point, the wording just sounds a bit strange. – Peter Cordes Mar 06 '19 at 23:23
15

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.

Uday Reddy
  • 5,334
  • 1
  • 18
  • 18
  • 1
    Hm, I'm not convinced. Functions in Algol, Pascal, etc weren't generally pure, so there was already room for the distinction. I find it more plausible to think that the term was used among imperative programmers already than believing that it got adopted from something as esotoric (at the time) as functional programming. It's not like it is a non-obvious word to use anyway... The real question seems to be, when did people start calling procedures 'functions'? – Andreas Rossberg Mar 24 '12 at 09:29
  • My point is that there is no idea of "purity" in imperative programming. If there were one, it would have to refer to "pure imperative programming". What in heaven is that? On the other hand, the idea of "pure Lisp" took hold as soon as people started using Lisp. See [McCarthy](http://www-formal.stanford.edu/jmc/history/lisp.ps) for instance. "Purity" and "impurity" are functional programming ideas. They have nothing to do with imperative programming. – Uday Reddy Mar 24 '12 at 19:30
  • When did people start calling procedures 'functions'? Well, Lisp did, as I just mentioned. It seems that the London-Cambridge-MIT circles didn't care to make the distinction between the two. Christopher Strachey's "Varieties of Programming Language" has this cryptic remark about PAL (Pedagogical Algorithmic Language) designed at MIT: "*There is only one sort of procedure (or function) in PAL*." – Uday Reddy Mar 24 '12 at 20:03
  • @AndreasRossberg: Note that functions in Algol and Pascal are legally expected to be side-effect-free (meaning "pure"?), because these languages do not define an evaluation order for subexpressions. The London-Cambridge-MIT circles (ISWIM, BCPL, PAL) did define a precise evaluation order and thus allowed side-effects. So, I suppose this is probably where the distinction between procedures and functions got lost. – Uday Reddy Mar 24 '12 at 21:16
  • I might have been unkind in ascribing side effects to "London-Cambridge" circles. The [paper on CPL](http://comjnl.oxfordjournals.org/content/6/2/134.full.pdf+html) has the following paragraph in describing "result of" expressions: "Other local variables may be defined, and the body may not contain operations, such as assignments to non-local variables, which would cause side effects." So, only some people in London and Cambridge may have been responsible for tolerating side effects, not all. On the other hand, side effects seem to have been perfectly normal at MIT. – Uday Reddy Mar 28 '12 at 11:01
7

It comes from the mathematical definition of "function", where it is not possible for functions to have side effects.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 10
    In math, the word "function" means what programmers would call a "pure function." But in many imperative programming languages, "function" means "procedure" -- an action that takes some input (or not), then possibly changes some state or does I/O, then returns some output (or not). So "pure function" is a way of saying "function as in math function". But my question is, why "pure"? Why not "mathematical function" or "non-I/O function" or something like that? – Tyler Oct 13 '11 at 07:38
  • I don't know the historical origins of using "pure" wrt programming, but I wonder if it's also related to pure vs applied mathematics. – ivanm Oct 13 '11 at 08:27
  • 2
    @MatrixFrog, this is because the math is pure, simple and beautiful, and the global state monad is ugly and dirty. That's why functions are "pure" and procedures are not quite so. – SK-logic Oct 13 '11 at 09:25
  • @MatrixFrog: Your proposed alternatives are suboptimal: a function that takes a string and returns an all caps version is pure but not very mathematical, while a function that generates pseudo-random numbers may do no I/O but is still impure. –  Oct 13 '11 at 11:37
  • @Rahul: why "not very mathematical"? It's an isomorphism from the free monoid on lowercase letters to the free monoid on uppercase characters :) – Tom Crockett Oct 13 '11 at 17:21
  • @pelotom: Sure, and in response I could probably start coming up with examples about employee databases and tax rates. :) But my point is that if you start calling categorizing functions as "mathematical" and "non-mathematical", it sounds like a matter of whether they do numerical calculations or not. –  Oct 13 '11 at 17:30
  • 2
    @Rahul: whatever numerical connotations you may attach to the word "mathematical", the meaning of "mathematical function" is unambiguous, and doesn't have anything to do with numbers. – Tom Crockett Oct 13 '11 at 17:39
3

Why is the word "pure" used to describe functions with those properties?

From Wiktionary > pure # adjective

  • free of flaws or imperfections; unsullied
  • free of foreign material or pollutants
  • free of immoral behavior or qualities; clean
  • 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)

I can't think of any other synonyms right now.

Dan Burton
  • 53,238
  • 27
  • 117
  • 198
2

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.

Ben
  • 68,572
  • 20
  • 126
  • 174
  • +1 I'm not old enough to really know what I'm babbling about, but my hunch was that "pure" arose from 1. computer subroutines in general are (mathematical) functions *plus* sideffects, and 2. subroutines which abstain from producing sideffects are thus "subroutines *minus* sideffects", as in "oh, that one is purely a mathematical function." purely <-> simply, pure <-> simple. – just somebody Dec 15 '11 at 00:19
  • Historically, algorithms originated in mathematics and they probably predate "functions" by at least a millennium. And, functions predate "sets" by another millennium. How many of the mathematical algorithms might have been "functional" and how many of them might have involved state change is not yet fully known. But, certainly, mathematical calculations were done on abacus, and those algorithms involved state change. The picture you have painted is a reflection of how our own education proceeds in the modern age, but it has no historical basis. – Uday Reddy Jul 29 '12 at 11:10
  • @UdayReddy At the time when the study of computation emerged, the mathematical notion of a function was already well known in its modern meaning. My point was that the modern mathematical function predates the (imperative) programmer's notion of a function, and this is why programmer's functions that more closely model mathematical ones are called "pure". Whether mathematicians of antiquity used algorithms involving state or not is completely irrelevant to both what I wrote in this answer, and to the the topic under discussion. – Ben Jul 30 '12 at 01:04
  • @Ben: Thanks. Re-reading your answer I find it quite unexceptionable, except for a minor reference to imperative languages. I suggested an edit. – Uday Reddy Jul 30 '12 at 08:21