Newbie: Truth values (three-valued logic)

Olaf Delgado delgado at Mathematik.Uni-Bielefeld.DE
Fri Jun 18 04:55:13 EDT 1999


On Fri, 18 Jun 1999, Tim Peters wrote:

> [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.

I understand that. A new meaning for "and" and "or" would have to be built
into the language, but I don't think the merits would justify the efforts.
The simplest way I see would be to introduce a new type with a single
value "Maybe", similar to "None", which would have to be handled specially
by "if", "not", "and" and "or". It should be relatively simple to
implement this, as far as I can see, but of course it would slow down
things by having to consider all those special cases.

> But in the spirit of overloading "~" instead of "not", consider overloading
> "&" and "|" instead of "and" and "or".  Then you can do whatever you want.

Yes, I was thinking about something like this.


> 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'}
[...]
> This hacks around assigning two values to "maybe", just so that it's closed
> under negation.

That's neat! It's quite intuitive, also. But let's say not overloading
anything but "__nonzero__()", which will return "1" only if there's not
a single bit of doubt. :)

> 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>.

Or I could set __and__(x,y)=x*y and __or__(x,y)=x+y-x*y. Then I only would
have to invent a way to partially execute a block of code. :)

Thanks for all the suggestions!

Truely (or maybe falsely?) yours,
Olaf

-- 
   ////         Olaf Delgado Friedrichs, Uni Bielefeld, Mathematik
   Olaf         Postfach 100131                  (room:  V4-109)
   `='          D-33501 Bielefeld, Germany       (phone: +49 521 106-4765)
                           http://www.mathematik.uni-bielefeld.de/~delgado





More information about the Python-list mailing list