logically Boolean

avi.e.gross at gmail.com avi.e.gross at gmail.com
Sat Jan 28 12:31:48 EST 2023


The topic has somewhat modified to asking what is a BOOLEAN.

 

The right answer is that the question is too simple. It is what YOU want it
to be in your situation. And it is also what the LANGUAGE designers and
implementers have chosen within their domain.

 

Mathematically, as part of classical logic, there is a concept of having two
sets with the union of the two being a null set. The sets are disjoint and
in some cases, cover all possibilities and in other cases the possibilities
are narrowed so that just two distinct groupings remain. Using 0 and 1 makes
sense for a binary bit, albeit in either order for True versus False. No
other numbers can be stored in a single binary digit so this is a trivial
but useful use of Booleans in some languages. In hardware or storage
mediums, there are not literal zeroes or ones. You can have a high or low
value or a magnetic field or spin or whatever you want to implement it, and
sometimes it fails anyway and you get indeterminate values or a bit seems to
flip. That is an implementation detail but Boolean logic remains a
mathematical concept.

 

UNIX programs did not return a Boolean value on exit so that discussion is a
tad off the point. They returned a STATUS of sorts. Mostly the status was
zero for all forms of success and you could signal many forms of failure
using any other number. But I can imagine designing a program like grep to
return 0 if nothing was found but the program did not fail, and a positive
number up to some limit that specified how many lines matched and perhaps -1
or other numbers on failure. A return status need not be seen as Boolean,
albeit in my made-up example, any non-zero exit status up to the limit would
be in the set of SUCCESS and -1 or whatever would be in the set for FAILURE.

 

 

Consider how a Python function can return OR throw an error. Many different
errors can be thrown and some propagate up from deeper levels if not caught
and handled. Generally success simply means no errors BUT you can also
design a function that throws various success "errors" and indirectly
returns one of several kinds of values in the payload, as well as errors
meant to be treated as bad. Heck, you can have some errors be warnings and
the caller of the function may have to call it from a "try" and specify what
to catch and how to deal with it. I repeat, not everything must be Boolean
when it comes to status types of things.

 

As noted, many other schemes have been used to represent a Boolean variable.

And given some implementations, something emerges that can confuse some
people. Grant mentioned even/odd. On most machines this simply means you can
safely ignore a longer clustering of bits such as with a 32-bit integer and
simply examine the least significant bit which determines if it is even or
odd. Heck, you can even store other useful info in the remaining bits, if
you wish.

 

As has been said repeatedly, it does not matter what the OP expects, as many
implementations are possible including some that do not follow the above
suggestions.

 

Much of my work has included other possibilities often using data that is
not always able to be analyzed in a truly Boolean fashion. Modern
Programming languages have to grapple with items that look like they can
have three or more values. I mean you can have a variable that can be TRUE
or FALSE but also one of many forms of Not Available or more. In a language
like R, their version of Boolean can have a value of NA but actually in some
contexts you have to specify which kind of NA such as NA_integer_ or
NA_character_ so obviously a vector of Booleans cannot be implemented using
a single bit for each. I have seen packages that can import data with many
kinds of missing values as there are other programs that allow you to tag
missing values to mean things like "missing: child did not show up to  take
test" or "missing: test not completed, will re-test" or "missing: caught
cheating" or "missing: identity of test taker not verified"  and so on. I
helped with one such package to create a type within R that does not discard
the other info so it supports multiple NA variants while at the same time it
can be processed as if every NA was the same.

 

Python has concepts along the same lines such as np.nan so there has to be a
way of storing data with perhaps a wrapper around things that can specify if
the contents are what is expected, or maybe something else. 

 

And I won't get into fuzzy logic. But just plain classical Boolean logic  is
a mathematical concept that can be expressed many ways. Some of the ways in
python look compatible with integers but that was a design choice. If they
had chosen to store a character containing either "T" or "F" then perhaps
they would allow Booleans to be treated as characters and let them be
concatenated to strings and so on.

 

-----Original Message-----

From: Python-list <python-list-bounces+avi.e.gross=gmail.com at python.org
<mailto:python-list-bounces+avi.e.gross=gmail.com at python.org> > On Behalf Of
Grant Edwards

Sent: Saturday, January 28, 2023 12:35 AM

To: python-list at python.org <mailto:python-list at python.org> 

Subject: Re: bool and int

 

On 2023-01-27, Mark Bourne <nntp.mbourne at spamgourmet.com
<mailto:nntp.mbourne at spamgourmet.com> > wrote:

 

> So Python is even flexible enough to be made to deal with insane 

> situations where False is 2!

 

IIRC, in VMS DCL even numbers were false and odd numbers were true.

 

In Unix shells, a return code of 0 is true and non-0 is false.

 

Though that's not really the same environment that Python lives in...

 

 

--

Grant

--

https://mail.python.org/mailman/listinfo/python-list

 



More information about the Python-list mailing list