Last Updated: February 25, 2016
·
704
· haiqus

de Morgans Laws

A warm-up:

>> nil && true
=> nil
>> nil || true
=> true
>> !(nil && true)
=> true
>> !nil || !true
=> true

Let's dial it up a bit:

>> !!(nil && true)
=> false
>> !(!nil || !true)
=> false
>> !(!nil && !true)
=> true

Take a break. Make sure you've got your head wrapped around that last one. Ready? Let's blow some minds:

>> !nil || !true === false
=> true
>> !nil || !true === !false
=> true

Now, satori:

>> nil === false
=> false
>> nil === true
=> false
>> nil && true === false
=> nil
>> nil && true === true
=> nil

What's happening in these four sets of examples? De Morgan's Laws define the behavior of the core boolean operators ! (not), && (and), and || (or). "Not both" is logically equivalent to "Not one nor another", and "both" is logically equivalent to "not neither". These are cornerstones of boolean algebra.

However, logicians, and some programming languages like Ruby, can do more with additional logics. In the last set, don't think of nil, true, and false as typical boolean values. Think of them as quantifiers over elements in a set: nil means no elements in a set, true means some elements in a set, and false means (possibly) an other set of elements, complementary to some set.

The comparison of nil to true or to false will ask whether those elements encompassed by the empty set (which is to say, none) are the same elements in the compared set. They are not.

What about the final two examples? I'm not quite positive this is correct, but I like to think that the intersection of the empty set and some set is a different empty set than that contained in another, inclusive or exclusive. But that's a borderline metaphysical interpretation, and I wonder if there's a more logical or computationally accurate assessment.

(Clarification: seasoned Rubyists will - correctly - take issue with this last statement. Just to be clear, it's a statement about logic, not about Ruby. In Ruby, the soft comparison will check whether two objects have the same 'truthiness', which is to say, the same truth-functional value as defined by the programming language. The strict comparison will check whether two ojects are, in fact, the same Ruby Object.)

See: http://mathworld.wolfram.com/deMorgansLaws.html

Note: This post was written with a baby assaulting the keyboard.

Major revision: 05/02/2013