r/functionalprogramming • u/Echoes1996 • Dec 02 '24
Question Is this function pure?
Consider a function f(x) that invokes an impure function g(), which is not referentially transparent, though it has no side effects. Then, function f decides to do nothing with the result returned by function g and goes on to return a value based on its argument x. My question is: is f pure?
Example:
global y
def g():
# Not referentially transparent, though it does not
# alter the "outside world".
return y
def f(x: int):
_ = g() # Invoke non-referentially transparent function g.
return x + 1 # Return result solely based on input x.The output of f is completely dependent on its input, and it has no side effects, as g has no side effects as either. So, is f pure or not?
7
Upvotes
View all comments
Show parent comment
3
u/MysteriousGenius Dec 03 '24
Then they're both impure :)
Result of
g()(again - result as "final outcome of execution", not as "return value") can be different depends on what value that global variable takes... unless it doesn't in which case the question is - why does it access the global variable in the first place.In the end, I agree it can rather philosopical question and most conversations boil down to two options:
MVar-like type - you simply cannot avoidIO(or other similar effects).I believe all people who give here different answers just belong to different camps. I personally in the former one, I like impurity to be statically visible.