Comparisons and sorting of a numeric class....

Rob Gaddi rgaddi at technologyhighland.invalid
Thu Jan 15 12:54:52 EST 2015


On Wed, 14 Jan 2015 23:23:54 -0800
Andrew Robinson <andrew3 at r3dsolutions.com> wrote:

> 
> > Boolean algebra has two values: true and false, or 1 and 0, or humpty
> > and dumpty, or whatever you like to call them.
> You're speaking to an Electrical engineer.  I know there are 10 kinds of 
> people, those who know binary and those who don't.  But you're way off 
> base if you think most industrial quality code for manipulating boolean 
> logic uses (or even can be restricted to use) only two values and still 
> accomplish their tasks correctly and within a finite amount of time.
> 

[snip]

Absolutely it does and can.  You store anything that's non-boolean in a
non-boolean value, and keep it as fuzzy as you like.  But at the end of
the day, an if statement has no "kinda".  You do, or you don't.  1 or
0.  And so you must ultimately resolve to a Boolean decision.

> >
> > Can you name any other language that *does* allow subclassing of
> > booleans or creation of new boolean values?
> Yes. Several off the top of my head -- and I have mentioned these 
> before.  They generally come with the extra subclasses pre-created and 
> the user doesn't get to create the classes, but only use them; none the 
> less -- they have more than two values with which to do logic equations 
> with.
> 
> VHDL, Verilog, HDL, Silos III, and there are IEEE variants also.
> C/C++ historically allowed you to do it with instances included, 
> although I am not sure it still does.
> 

Incorrect, at least in VHDL.  If I've got "signal x : boolean;", then x
is defined on the range (true, false).  I can ask VHDL "if x then; ...;
end if;"

What you're talking about is not at all a subclass of boolean, it's a
std_logic.  It's a separate enumerated type, with a set of resolution
rules defined in a function the source of which is available in
std_logic_1164.vhd.  And according to the strict rules of VHDL (up
until VHDL2008 decided to forfeit some purity for simplicity's), you
can't simply have:

	signal x: std_logic
	...
	if x then

You have to ask

	if x = '1'
or
	if (x = '1') or (x = 'H')

Because you are comparing one value of an enumerated type against
others, the result of that '=' operation being, in fact, a boolean,
defined again on the range (true, false).

> [snip]
> 
> We've discovered that we live in a quantum-mechanical universe -- yet 
> people still don't grasp the pragmatic issue that basic logic can be 
> indeterminate at least some of the time ?!
> 

But actions can't be. You're not asking the software about it's
feelings, you're telling it to follow a defined sequence of
instructions.  Do this, or don't do this.

> I don't know what you mean about composition vs. sub-classing.
> Would you care to show an example of how it would solve the problem and 
> still allow hierarchical sorting ?
> 
> I don't see how you can get pre-existing python functions (like sort, 
> max, and min) to accept a complex bool value and have that value 
> automatically revert to a True or False in an intelligent manner without 
> overloading the operators defined in some class -- somewhere -- to 
> operate on the data the class contains.
> 
> How do you intend to achieve that with this -- composition -- thing you 
> mentioned ?
> 
> 

You'd do it something like this.

class _clogic(object):
	"""Represents 'continuous' logic.  For any given instance,
there is a threshold value between 0 and 1 that delineates True from
False, with 0 being entirely False and 1 being entirely True.
	"""
	
	def __init__(self, value, threshold=0.5):
		self.value = value
		self.threshold = threshold
		
	def __bool__(self):
		return value > threshold
	__nonzero__ = __bool__
	
	def __eq__(self, other):
		if other in (True, False):
			return bool(self) == other
			
		try:
			return self.value == other.value
		except AttributeError:
			return self.value == other
		
	def __and__(self, other):
		return self.__class__(
			min(self.value, other.value),
			self.threshold)

	def __or__(self, other):
		return self.__class__(
			max(self.value, other.value),
			self.threshold)

No need to subclass bool, you just use the __bool__ (or __nonzero__)
methods to say "Alright, well when you do finally have to make a
decision on this thing, here's how you make it.  And obviously, you can
add on as many additional data members to carry additional information
as your heart desires.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.



More information about the Python-list mailing list