[ Content | View menu ]

The Maybe Operator

Mark | December 26, 2007

I doubt I’m going to go off on a purely theoretical tangent like this often, but I’ve had lots of time lately for eating, drinking, and thinking, with not much else going on.

This discussion stems from several events coming together in my brain at once.  First was my previous PHP Insanity post, part I.  In it I talked about PHP’s comparison operators and how I find them lacking.  This lead to a a comment by Chuck that perhaps PHP should have a sort of equal operator, with the syntax ?= .  I call this the maybe operator.  I had even joked with a coworker of mine at work that PHP needed a maybe operator, although we didn’t develop a syntax for it.

All this lead me to wonder just what use would a maybe operator have, if it was built into a language.  The language likely wouldn’t be a binary language, but a ternary language, since there would now be three possible states.  This leads to the realm of ternary logic.

While binary systems rule the day, it is possible to build ternary systems, as a quick check on Wikipedia reveals.

A ternary system would perhaps have 0 = false, 1= true, 2 = maybe (or some other choice).  If might be an interesting exercise to build such a system into an existing language, such as Lisp or Python.  I don’t recommend PHP - it has enough of it’s own idiosyncrasies already and doesn’t need another.
So what uses would a ternary system have?  What benefits does it bring to the table?  Is there a compelling enough reason to build a language from scratch that would be optimized for such a system?  Perhaps not today, but what possibilities lie in the future?

I’m still musing on these ideas myself and don’t have any answers to offer at this time, but I’m curious what any one else thinks.  If you don’t want to comment on a ternary system in general, what do you think about the maybe operator?  Good, bad, or just plain ugly?

2 Comments

Write comment - TrackBack - RSS Comments

  1. Comment by Kevin Smith:

    My gut tells me that implementing maybe has farther reaching implications than just ternary logic. A maybe operator seems to hint at things like promises and forced evaluation. I guess it’s because maybe implies that the value could become “fixed” later depending on conditions and so, you might want to re-evaluate it later. Kinda like the magic eight ball’s answer of “Maybe, try again later”.

    Haskell has a Maybe monad which might be similar to what you’re describing:

    http://en.wikipedia.org/wiki/Monads_in_functional_programming

    December 27, 2007 @ 6:30 am
  2. Pingback from The Maybe Monad (In Ruby) | Programmer’s Paradox:

    [...] the tail end of last year I wrote a piece in which I waxed about a possible maybe operator. I haven’t looked into implementing anything like it because my time is limited and I [...]

    February 15, 2008 @ 9:27 am
Write comment