Newbie: Truth values (three-valued logic)

Tim Peters tim_one at email.msn.com
Fri Jun 18 06:55:01 CEST 1999


[Olaf Delgado]
> ...
> It came to my mind, though, that redefining negation alone will not
> help. Next time I'll want to redefine 'and' and 'or'. So, unless
> python 2 implements three-valued logic <hint, hint :)>, I'll have
> to come up with a routine for myself to handle these truth values
> consistently.

Well, "and" and "or" do short-ciruit evaluation (i.e., they don't evaluate
their right-hand operand at all if the value of their left-hand operand
suffices to determine the result).  For that reason, they're more properly
viewed as control structures than operators, and so unlikely to get "opened
up" to user intervention.

But in the spirit of overloading "~" instead of "not", consider overloading
"&" and "|" instead of "and" and "or".  Then you can do whatever you want.
If I were you I'd consider not overloading anything, and using ~ & |
directly with a naming trick, like so:

f, t, m = 0, -1, 42
name = {0: 'false', -1: 'true', 42: 'maybe', ~42: 'maybe'}

for x in f, t, m:
   print "not", name[x], "=", name[~x]

for x in f, t, m:
    for y in f, t, m:
        print name[x], "and", name[y], "=", name[x & y]

for x in f, t, m:
    for y in f, t, m:
        print name[x], "or", name[y], "=", name[x | y]

This hacks around assigning two values to "maybe", just so that it's closed
under negation.

Or use false=0, true=1, maybe=0.5; map __invert_(x)=1-x;
__and__(x,y)=min(x,y); and __or__(x,y)=max(x,y).  Then you get a nice little
MV logic with a whole bunch of possible "maybe" values.  Once you go beyond
the first distinction (true/false), I don't know why you'd ever want to stop
<wink>.

the-notion-that-something-is-true-is-as-silly-as-the-notion-that-
    something-is-false-ly y'rs  - tim






More information about the Python-list mailing list