Let's say I have two python functions f and g:
def f(x):
y = x**2 + 1
return y
def g(x):
a = x**2
b = a + 1
return b
These two functions are clearly functionally equivalent (both return x**2 + 1).
My definition of functionally equivalent is as follows:
If two functions f and g always produce the same output given the same input, then f and g are functionally equivalent.
Further, let's say no global variables are involved in f and g.
Is it possible to automatically determine (without human inspection) if python functions f and g are functionally equivalent?
fandgfrom the example above would compile down to the same bytecode?def r1(): return random.random()-def r2(): return random.random().dis.dis(myfunction). If two functions do compile to the same bytecode, they must be functionally equivalent (according to the compiler, at least). However, as I mention above and Slater notes, there could be false negatives - different bytecode for two functions that are functionally equivalent.