De Morgan's Laws
There are eight possible AND expressions with two variables, shown in the table below.
It's not necessary to examine each one closely, but note that there are no possibilities left out: if we add a !
anywhere in one of these expressions, it will be equivalent to one of the other expressions.
For example, one of the entries is x and !y
.
If we add another !
to the y
, making it x AND !!y
, the !!
will cancel and give us x AND y
, which is already in the table.
AND expressions |
---|
x AND y |
!x AND y |
x AND !y |
!x AND !y |
!(x AND y) |
!(!x AND y) |
!(x AND !y) |
!(!x AND !y) |
(Incidentally, there are 8 entries because there are three positions where a !
can go.
Each of those can have a !
or not, giving two possibilities per position.
The positions can have a !
or not independently of the other positions, so we have 2*2*2 = 8 expressions in total.)
Some of these AND expressions are confusing – the last one, for example, with three NOTs. De Morgan's laws are two tools for transforming complex boolean expressions into simpler ones. We can also transform simple expressions into complex ones if we like.
Our table is repeated below with equivalent OR expressions included for each AND expression. Again, there's no need to exhaustively read it, but notice that simple expressions in one column correspond to complex expressions in the other. If this idea is new, try choosing one line of the table, assigning a 0 or 1 value to x and y, and working out the results of both the AND expression and the OR expression. They should be the same!
AND version | OR version |
---|---|
x AND y |
!(!x OR !y) |
!x AND y |
!(x OR !y) |
x AND !y |
!(!x OR y) |
!x AND !y |
!(x OR y) |
!(x AND y) |
!x OR !y |
!(!x AND y) |
x OR !y |
!(x AND !y) |
!x OR y |
!(!x AND !y) |
x OR y |
Memorizing this table would be difficult, but fortunately no one does that.
Instead, there's a process to perform this transformation on any AND or OR expression.
Here are the steps, using the second expression above, !x AND y
, as an example.
- Negate both arguments to the AND or OR. This turns
!x AND y
intox AND !y
. - Negate the whole expression, adding or removing parens. This gives us
!(x AND !y)
. - Switch the AND to an OR or vice-versa. This gives us
!(x OR !y)
.
Looking in the table, that's exactly what we see: !x AND y
is equivalent to !(x OR !y)
.
That's all there is to De Morgan's laws: to get an equivalent expression, we negate the arguments, negate the expression, then flip the operator.
They're called "laws", plural, only because they work for both AND and OR.)
This is occasionally useful in programming. For example, we might end up with the expression below through a series of code changes, months apart, each sensible on its own.
!(!user.is_admin AND !user.is_moderator)
It's much easier to understand if we De Morgan it. With a little practice, it doesn't even require much thought: we notice that there's a lot of negation in the expression, then we mechanically apply the rules above.
user.is_admin OR user.is_moderator
In truth, this doesn't come up as often as computer science professors might wish, but the time saved more than justifies the small time investment to learn it.