I saw this question that shows it is impossible to programatically determine if a javascript function is pure, but is it sometimes possible to affirm that a function is pure - so something like...
function sq_a(x){ return x * x; }; // no side effects, all variables local
function sq_b(x){ return pow(x, 2); }; // if `pow` is pure, this function is pure
function sq_c(x){ return mathlib.pow(x, 2); }; // not pure, because mathlib.pow could be changed
function definitelyPure(fn){
if(/* criteria to check if we can know for sure fn is pure */){
return true;
} else {
return false;
}
}
function isPure(fn){
if(/* fn is pure */){
return 0;
} else if(/* fn might be pure */) {
return -1;
} else { // fn is not pure
return 1;
}
}
definitelyPure(sq_a); // true
definitelyPure(sq_b); // false
definitelyPure(sq_c); // false
isPure(sq_a); // 0
isPure(sq_b); // -1
isPure(sq_c); // 1
Clarification:
I know it is not possible to determine if any javascript function is pure as answered in linked to question, but I am asking if it is possible to programatically affirm purity of some functions - as in I expect that it is possible for example sq_a
using some code analysis, but I understand it is not possible for sq_b
.
I am interested to know if there are any tools out there to do this already, and what criteria determine if it is possible to programatically declare a function as either pure, unknown, or impure.