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
12
u/LukaJCB Dec 02 '24
If `g` has no side effects, how is it non-referentially transparent? In my mind, a referential transparent function is the same as a pure function, so either `g` is pure and so is `f`, or they both aren't.
8
u/faiface Dec 02 '24
If you can remove the call to
gwithout changing any behavior, then I’d sayfis pure.6
u/justinhj Dec 02 '24
It’s a thought experiment and a bit nuanced, but if g, for example, calls a function that reads some non-deterministic source (like the value of some internal clock or signal) then it is both not referentially transparent and has no side effects. Under those conditions f is pure still.
4
u/Scf37 Dec 02 '24
I'd say g() is pure in this context because its invocation can be replaced with its result (nothing)
2
u/NullPointer-Except Dec 03 '24
Imo, it is "pure". But it requires a proof (proof that it doesnt alter state, proof that it terminates, and proof that within
f, the call is never bound).There is the issue that
fshouldn't exactly type as a "pure function" since you are still executing an "effectful function" withinf. This can be ignored sincegdoesn't alter anything, it always terminates and you dont bind the result. So the program is semantically equivalent to:(Assuming let expressions are lazy in the language).
Either way, this feels a bit... Artificial? Under these assumptions, you are not doing anything with
g(). Thus it's nice that if you were to type effects, the original definition offdoesn't typechecks. There is no reason for this kind of functions to exists in the code.2
u/no_brains101 Dec 03 '24 edited Dec 03 '24
If all g does is return the value, and then you go on to not use the value in ANY of the possible return values of f, its a nop
Theres no reason to do this, so language writers concerned with the function remaining pure can safely disallow it
(If g does anything to change the outside world) or (the value g returns comes from the outside world and is mutable rather than a compile time constant, and you use its value in f), then it is impure.
Thus, it is either impure, or a nop and can be disallowed for consistency if purity is the goal.
2
u/pi_meson117 Dec 02 '24 edited Dec 02 '24
Pure. If calling g() does nothing, it does nothing. Inputs for f() give specific outputs without fail; we don’t care about what happens inside the “black box”.
I would argue though that changing or creating global variables is an output of the function. In that case, definitely not pure because then another pure function would depend on the global variable. And we’re back to the beginning of why we wanted pure functions in the first place.
If swapping the order of function calls changes the outputs, it’s not pure.
0
u/[deleted] Dec 02 '24
[removed] — view removed comment