[Edit]
The general question seems incredibly hard to solve. Here is a significantly restricted version of this question.
How do I determine equality of functions?
lets say we have
function f() {
// black box code.
}
function g() {
// black box code.
}
We take a mathematical definition of a function. So
if for all x in domain, f(x) === g(x) then f === g
- How do we handle domains?
- How can we otherwise determine if
f === g
Checks by source code is silly because
function f(i) {
return i % 2;
}
function g(i) {
var returnVal = i % 2;
return returnVal;
}
Are obvouisly equal. These are trivial examples but you can imagine more complex functions being equal but not source-equal.
You may assume that f
and g
have no side effects that we care about.
[Edit]
As @Pointy mentioned it's probably best to constrain the domain. Rather then having the equality function try and guess the domain, the user of the equality function should supply a domain.
It doesn't make sense to ask whether two functions are equal without defining their domain somewhere.
To simply the problem we can assume to domain is the set of all integers or a subset of that so we need a function:
function equal (f, g, domain) {
}
The structure of the domain is irrelevant and can be made to make the problem as easy as possible. You can also assume that f
and g
act nicely on the domain of integers and don't crash&burn.
You may assume that f
and g
halt!
Again @Pointy points out a good example of non-deterministic functions
What if we limit f
& g
to be deterministic.
f
&g
halt. – Mitrailleuse