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
6
u/omg_drd4_bbq Dec 02 '24
Yes. Impurity is kind of like undefined behavior, it probably won't reformat your computer and cause demons to come out your nose, but you can't prove that it won't.
You don't know the source of
global g, it might not be a variable at all but in fact reading from an I/O address.I lack the chops to say with 100% confidence but I believe a pure function is provably pure.