Friday, 13 April 2012

Tri-State Logic in C#

First, a Story About Trees

Certain friends and colleagues, asking me about the origin of my "Stanislaw" Facebook persona, have already been introduced to the shady night dweller "Stanislaw the Philosopher". Found in and around 1980 sweating over some maths and engineering textbooks in the wee small hours, listening to Mike McLean Through the Night on Radio Clyde, and occasionally sending in record links in the form of unbearably awful puns. Like this:
The king's gardener feared for his life. Long and hard he had toiled in the royal grounds, trying desperately to make the castle's fir trees thrive in the dry, dusty and too-barren earth. Nothing would work to save them. And now that his life was forfeit, he had begun to see ghosts during his nightly rounds. One of these came up to him, holding forth a small cloth pouch, and saying "These are the droppings of the kestrel hawk. Lime for this climate, bone for these flora. You must feed it to their roots, one and all. For either you and they shall die, or else neither."

The gardener did as he was bid. Three nights later, walking through the grounds in the dark of an otherwise still night, he now sensed those trees rising up before and around him, moment by moment blocking off a little more of the violet horizon. Was this truly and solely an imagination? Then too, he began hearing something wholly unaccustomed and chilling on the air, what was that sound? The raising of a distant breeze, the creaking of old bones and branches, the arrival of some new monster unseen?

Or kestrel manure firs in the dark?

On another occasion, Stanislaw the Philosopher posed a riddle from Raymond Smullyan's book of logical puzzles, What Is The Name Of This Book? The hardback book itself was sent to the radio station as a prize. Stanislaw then sent in the correct answer under the guise of his good friend Tom, who was subsequently baffled to receive such a book, from a radio station he never listened to, as a prize in a competition he'd never heard of.

But Seriously...

This whole confession is prompted by Eric Lippert's blog Fabulous Adventures in Coding, and in particular by today's entry, null is not false, part two. Eric starts off by using Dr Smullyan's deductive logic puzzle books to introduce the concept of multi-valued logic systems, including the nullable boolean. This 3-state scheme has the possible values: true, false, and null.

The rules of null are quite straightforward and obvious, if you think of replacing it everywhere with "unknown". So for example in arithmetic, we would have
X + null = null,
which translates as the self-evident "Anything plus unknown, equals unknown." Now in our 3-term logic,
false AND Y = false,
since whatever the second term might represent (including null), the first false makes the expression false. But this also implies that
X AND false = false,
which can be a problem for computer languages featuring short-circuit boolean evaluation. Eric considers the cases of the C# operators && and ||, which are specified as strictly short-circuiting operators, and asks the question, how should we evaluate
X && Y
when X is null? The truth table gives unambiguous answers: false if Y is false, otherwise null. But to obtain that answer we had to violate the specification that Y is only evaluated when X is true. Eric closes by saying "...either we sometimes evaluate Y when we shouldn't, or we sometimes return a value that does not match the value that X&Y would have produced. The way out of this dilemma is to cut the feature entirely."

So, the operators && and || are not "lifted to nullable". That seems a shame! I'd have preferred the alternative way out of that dilemma, where we allow nullable overrides of these operators, and simply replace the pair of statements
In the expression X && Y we evaluate Y if and only if X is true
In the expression X || Y we evaluate Y if and only if X is false
In the expression X && Y we evaluate Y if and only if X isn't false
In the expression X || Y we evaluate Y if and only if X isn't true

1 comment: