r/functionalprogramming • u/cizizen • Jan 24 '23
Question Example of a function that has referential transparency but is not pure?
I've read that Functions that have referential transparency can be replaced by their output. And also that Pure functions return the same result for the same input (which makes them referentially transparent) and don't have any effect on the rest of the program.
So what is an example of a function that has referential transparency but is not pure? Does a function which only depends on its inputs but modifies a global variable still have referential transparency? It wouldn't be pure from my understanding because it modifies other parts of the program.
10
u/Migeil Jan 24 '23
According to wikipedia, referential transparency means that you can replace a function call, with its output while not changing the programs behaviour. Imo, this is important.
Some commenters give an example of a function which just logs some string, but is otherwise pure as a referentially transparent function. But it the wikipedia definition, this is not a referentially transparent function because if we just replace the function call with its output, the behaviour of the program changes, i.e. the log doesn't happen anymore.
So no, it does not, because if changed by the output, the behaviour of the program changes.
Incidentally, the wiki page suggests that for a function to be referentially transparent, it needs to be pure. So the answer to your question would be that there is no referentially transparant function which is not pure, because purity is a requirement for referential transparency.
9
u/uppercase_lambda Jan 24 '23
From Wikipedia:
Based on this definition, a function that is referentially transparent must be pure.
16
u/m_verardo Jan 24 '23 edited Jan 24 '23
For a function to be pure, it has to meet 2 requirements: referencial transparency, and be free of side effects.
So, for instance, if a function sums it's arguments, prints the result to the terminal and returns the result to the caller, it has referencial transparency (the sum is always the same for a given set of argument values) but is not free of side effects (it's printing to the terminal), and so is not pure.
3
u/evincarofautumn Jan 24 '23
Most authors don’t really draw a distinction between these terms. “Purely functional” is abbreviated “pure” and often defined as “referentially transparent”, which means that it does not change the program’s semantics if you replace a term with its value, or in other words, any observable effects are commutative.
Notice that this depends on your choice of semantics, and the definition of commutation in that context. Often we’re talking about observational equivalence which means that, if there are any effects, then they are not side effects, in the sense of being observable internally within the system.
For example, GHC Haskell uses thunks to implement “call by need” evaluation—when you evaluate a variable, its value is cached and not recomputed if it’s reused. This is an effect involving mutation, but it’s not observable within plain Haskell code, so it’s not considered a side effect within the language. Yet, in the runtime system (RTS), on the other side of this abstraction boundary, the mutation is very much a side effect, and the RTS needs to deal with it directly so that the program doesn’t have to.
Therefore, whether you consider “merely caching” or “merely logging” to be a side effect depends fundamentally on what you’re trying to prove about the system.
7
u/npafitis Jan 24 '23 edited Jan 24 '23
An example of a referentially transparent function but one that's not pure, is a function that memoizes/caches it's output so when called later with the same input, it does not have to re compute the output, but just look it up from cache. The non-memoized version of this function must be idempotent (not necessarily pure), meaning it should return the same result given the same input.
This function would not be strictly pure per se, but it'd be still referentially transparent and idempotent.
2
u/fbn_ Jan 24 '23
function dirtyinc (x) { console.log('lol'); return x+1 }
2
u/[deleted] Jan 25 '23
theres no such thing. as soon as you add assignment/mutation theres no such thing as referential transparancy
1
u/[deleted] Jan 24 '23
Referential transparency requires the function to be pure.
In other words: a function having referential transparency is equivalent to it being pure.
1
u/[deleted] Jan 24 '23
"Referentially transparent" is not well defined. I advise to not use this term.