[ Content | View menu ]

The Maybe Operator

Mark Mzyk | 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?

4 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 @ 06:30
  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 @ 09:27
  3. Comment by Jimage:

    This came up in discussion during several IRC conversations recently, and I dubbed it the ‘may or may not’ operator. It has great comedic value, but surely there must be some practical applications. We spoke of a language in which assumptions play a major role. Someone observed that the brain relies heavily on assumptions, and it’s about the most powerful computer that exists. Perhaps the maybe operator could play a valuable role in dealing with incomplete data, in a similar way that the brain fills in the gaps in our awareness. Also, it seems to have a certain quantum property about it… something about calculating unobserved data without collapsing it. Such reasoning becomes pseudo-science very quickly without more in-depth knowledge on the subject, but it’s worth considering. Maybe.

    March 4, 2010 @ 00:32
  4. Comment by Mark Mzyk:

    @Jimage

    That’s an interesting thought, the comparison to the brain. I hadn’t considered it, but it brings up the point that the maybe operator and maybe monad likely have a place in artificial intelligence programming.

    It’s possible use around incomplete data is also interesting. However, I don’t know about situations where that is often the case, that a program has incomplete data. Programs today are written so that they have complete data or they fail. If with incomplete data today’s programs generally fail, does that mean having a maybe operator would allow a different path through the code instead of issuing an exception or throwing an error? A third path to deal with the uncertainty instead of just erroring out.

    It’s worth pondering.

    March 4, 2010 @ 12:17