DeMorgan's laws are always useful for reasoning about programs, especially loops and conditional statements. Here is one of DeMorgan's laws.
a b | ( ! ( a & b ) ) = ( ( ! a ) | ( ! b ) )
---------------------------------------------
0 0 | ( 1 ( 0 0 0 ) ) 1 ( ( 1 0 ) 1 ( 1 0 ) )
0 1 | ( 1 ( 0 0 1 ) ) 1 ( ( 1 0 ) 1 ( 0 1 ) )
1 0 | ( 1 ( 1 0 0 ) ) 1 ( ( 0 1 ) 1 ( 1 0 ) )
1 1 | ( 0 ( 1 1 1 ) ) 1 ( ( 0 1 ) 0 ( 0 1 ) )
The extended truth table constitutes a proof of the law since all values under the "
=" are
1.
DeMorgan's laws can be used to flip the then part and else part of an if construct in an algebraic manner. Here are some equivalent ways.
if (a & b) { p2 } else { p1 }
if (! (a & b)) { p1 } else { p2 }
if ((!a) | (!b)) { p2 } else { p1 }
Here is another way to express DeMorgan's laws.
a b | ( ! ( a | b ) ) = ( ( ! a ) & ( ! b ) )
---------------------------------------------
0 0 | ( 1 ( 0 0 0 ) ) 1 ( ( 1 0 ) 1 ( 1 0 ) )
0 1 | ( 0 ( 0 1 1 ) ) 1 ( ( 1 0 ) 0 ( 0 1 ) )
1 0 | ( 0 ( 1 1 0 ) ) 1 ( ( 0 1 ) 0 ( 1 0 ) )
1 1 | ( 0 ( 1 1 1 ) ) 1 ( ( 0 1 ) 0 ( 0 1 ) )
The extended truth table constitutes a proof of the law since all values under the "
=" are
1.
DeMorgan's laws can be used to flip the then part and else part of an if construct in an algebraic manner. Here are some equivalent ways.
if (a | b) { p 2 } else { p1 }
if (! (a | b)) { p1 } else { p2 }
if ((!a) & (!b)) { p2 } else { p1 }