> I have an idea that the "espresso" algorithm could be used to minimize not only electronic circuits but general boolean expressions for any programming language... I think it would do for a useful refactoring tool.
I think the goal for most boolean expressions in code is for the reader to be able to conceptually grasp what's being checked for, not for the expression to be as short as possible. Distributive properties can make an expression shorter while simultaneously making the concepts behind the expression much more obscure.
As a compiler writer I can tell you that this is a complex optimisation with very limited use.
N.B. the || (logical or) operator is an "early out" operator whereas the | (binary or) operator is NOT
The compiler would need to look for special cases of value1, value2 and value3... such as: are any of them volatile, perhaps one refers to a memory mapped I/O device, perhaps one if actually a function call, or array dereference, or pointer dereference, what about value1 being a "char" which needs to be promoted before it can be or'd, what about any of the "values" being a constant < 0 which would short circuit the "if" statement and remove the need for any tests.
As I said, this is not as simple as it first appears.
That optimization fudges the difference between boolean or (comparing truth values) and bitwise or. It happens to work under the safe assumption that value1, value2, and value3 use sign bits (as all two's-complement integers do), but logically it's a disaster.
monocasa isn't saying this should be in source code, but rather the compiler should create machine code that does this. Compilers know the types, so they will know whether they're signed (if they're not signed, then < 0 can be optimized out as impossible). Compilers have no goal of creating assembly that would look good in source code.
Good point, although the idea would be you get to choose one for or the other. I think sometimes the simplified expression makes it easier to see the complement (else branch).
btw, I started to think about "boolean refactoring" when I saw J. Edwards' schematic tables video [1].
I think the goal for most boolean expressions in code is for the reader to be able to conceptually grasp what's being checked for, not for the expression to be as short as possible. Distributive properties can make an expression shorter while simultaneously making the concepts behind the expression much more obscure.