if (a && b) { // original
X;
}
else {
if (!b && !c)
Y;
else
X;
}
which your partner has suggested simplifying to:
if (a && b || c) // suggested simplification X; else Y;Are these two pieces of code equivalent, or are there circumstances in which one does "X" but the other does "Y"?
The original code does "X" when "a && b" is true and also when "a && b" is false and "!b && !c" is also false. Otherwise it does "Y". The suggested simplification does "X" when "a && b || c" is true, and does "Y" in all other cases. So, let's check whether the "X" cases are the same for both (we'll write it using C++'s boolean operators where we can):
Is
(a && b || !(a && b) && !(!a && !c))
equivalent to (a && b || c)?
Of course truth tables give us a tool to use to check this:
From the truth table we see that (a,b,c)=(true,false,false), the two programs do different things - one does "X" but the other does "Y". Therefore, your partner's "simplification" is invalid! Silly partner.
ACTIVITYif (x > 0 || y == 3) ; else x = abs(x/(y - 3));Although the code works, you are (rightfully!) embarrassed to have that empty then-block to the if. Rewrite as an equivalent "if" with no "else" block. Explicitly show how the rewriting is done (i.e. justify steps) so you can be 100% sure the new "if" is equivalent to the old!
Deduction is a big deal!
Logical deduction is concerned with deriving new facts from facts you already know. We have rules of deduction that provide a mechanism for doing this. In propositional logic, the tautologies with implication as the top-most operator are the deduction rules. Here's an example with what is arguably the most important rule of deduction: modus ponens.
Modus ponens: For any two formulas $F$ and $G$, if $F\Rightarrow G$ and $F$ are both known to be true then you may deduce (by modus ponens) that $G$ is also true. More precisely, under any interpretation for which $F\Rightarrow G$ is true and $F$ is true, $G$ is also true.
The justification for this is that $(a \Rightarrow b) \wedge a \Rightarrow b$ is a tautology, i.e. it is true for any interpretation. Let's see what we can do with this one rule of deduction.
Example 1:
Suppose that both turbo ⇒ ¬stockbrakes and
strockbrakes are known to be true. What can we prove?
Things I know are true How I know they are true 1: turbo ⇒ ¬stockbrakes [Given] 2: strockbrakes [Given] 3: ¬¬stockbrakes ⇒ ¬turbo [Equivalence of contrapositive applied to 1 (rewriting rule)] 4: stockbrakes ⇒ ¬turbo [double negation applied to 3] 5: ¬turbo [Modus ponens with 4 and 2]So we have deduced the new fact:
¬turbo.
In fact, the above is a proof of ¬turbo
given the facts turbo ⇒ ¬stockbrakes and strockbrakes.
In other words, under any interpretation: if turbo ⇒ ¬stockbrakes and strockbrakes
are both true then ¬turbo is true.
In case you are sceptical, we can verify this with truth
tables. In fact, there are two different ways to do so.
| Version 1: verify our proof with truth tables | Version 2: verify our proof with truth tables |
Consider the truth table for turbo ⇒
¬stockbrakes
The only interpretation in which turbo ⇒
¬stockbrakes and strockbrakes
are both true (the second row) is the interpretation in
which turbo |
We have proved that: if turbo ⇒ ¬stockbrakes and strockbrakes
are both true then ¬turbo is true.
Translating the if-then into a formula gives
(turbo⇒¬stockbrakes)∧stockbrakes⇒¬turbo
and we can verify that this statement is true for all
interpretation with a truth table:
Clearly this is a tautology.
|
Example 2: >Given: (u & ~v) => (w | z), (w | z) => (x & y), and u & ~v, prove x & y
Things I know are true How I know they are true 1: (u & ~v) => (w | z) [Given] 2: (w | z) => (x & y) [Given] 3: u & ~v [Given] 4: w | z [Modus ponens with 1 and 3] 5: x & y [Modus ponens with 2 and 4]From the given facts we made two deductions, both from Modus Ponens, ultimately deriving the new fact
x & y.
So what's new here? I mean, couldn't we have proved this by
construting the truth table for
((u & ~v) => (w | z)) & ((w | z) => (x & y)) & (u & ~v) => x & y
and checking that we have a tautology? Yes, but the truth table
would have 64 rows, instead of the two deduction steps we just used.
So deduction allowed us to prove this new result in many fewer
steps.
But also, deduction doesn't just verify or prove things we've
already hypothesized for ourselves: deduction produces new
facts!
For example, we learned that w | z follows from the
givens, which we didn't know or hypothesize before.
Most importantly, deduction becomes the primary tool when we
move to higher level logics, when we make logical arguments in
math and CS proofs, and when we reason logically in other
aspects of life.