Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83840

Re: Comparisons and sorting of a numeric class....

Path csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Rob Gaddi <rgaddi@technologyhighland.invalid>
Newsgroups comp.lang.python
Subject Re: Comparisons and sorting of a numeric class....
Date Thu, 15 Jan 2015 09:54:52 -0800
Organization Highland Technology, Inc.
Lines 129
Message-ID <20150115095452.072d5763@rg.highlandtechnology.com> (permalink)
References <54ABB88A.7070504@r3dsolutions.com> <54ABC52A.1050507@davea.name> <54ABE383.3020801@r3dsolutions.com> <CAPTjJmpktyHfghEr+cjPrhso7JgB4VfS4=5Gdi95gT0aFbSYGA@mail.gmail.com> <54AC97D9.4010504@r3dsolutions.com> <CAPTjJmooTK_+HVoeyeSrqBHN1=YjRYQ2kfDyQtL9JoA9SbTqsQ@mail.gmail.com> <54ACAA04.60801@r3dsolutions.com> <54ADC99F.3020405@stoneleaf.us> <54B44A64.7010105@r3dsolutions.com> <CAPTjJmr0Bqcyj=VBADVSthSZ+pYMXQeNC-j8qLC2xHAVLUmcgA@mail.gmail.com> <mailman.17650.1421114374.18130.python-list@python.org> <54b4aded$0$2738$c3e8da3$76491128@news.astraweb.com> <54B5B486.7080406@r3dsolutions.com> <CALwzidn4t_mVvtUjucKwPLzeq5pgFdYDBLjePMeuEEB7gTvKCQ@mail.gmail.com> <54B72D32.3090209@r3dsolutions.com> <CALwzidmn-WJVMxXmRK=dSw0vWoBLf=rZ-6VhXinQhfn3BjF=TQ@mail.gmail.com> <mailman.17743.1421306724.18130.python-list@python.org>
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
Injection-Info mx02.eternal-september.org; posting-host="00c0529b11392f77bf1ca106a3de87ed"; logging-data="23176"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX196Bw8SClvJvDd5uOeWoOvJ"
X-Newsreader Claws Mail 3.9.3 (GTK+ 2.24.23; x86_64-pc-linux-gnu)
Cancel-Lock sha1:n9QZat7GIF7S/VGChRTKk8cKKTU=
Xref csiph.com comp.lang.python:83840

Show key headers only | View raw


On Wed, 14 Jan 2015 23:23:54 -0800
Andrew Robinson <andrew3@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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Comparisons and sorting of a numeric class.... Andrew Robinson <andrew3@r3dsolutions.com> - 2015-01-12 17:59 -0800
  Re: Comparisons and sorting of a numeric class.... Steven D'Aprano <steve@pearwood.info> - 2015-01-13 05:32 +0000
    Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-13 17:13 +1100
    Re: Comparisons and sorting of a numeric class.... Terry Reedy <tjreedy@udel.edu> - 2015-01-13 05:49 -0500
      Re: Comparisons and sorting of a numeric class.... Marko Rauhamaa <marko@pacujo.net> - 2015-01-13 13:00 +0200
        Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-13 22:20 +1100
        Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-13 11:56 -0700
        Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-14 06:02 +1100
    Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-13 21:56 +1100
    Re: Comparisons and sorting of a numeric class.... Andrew Robinson <andrew3@r3dsolutions.com> - 2015-01-13 16:12 -0800
    Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-14 01:31 -0700
    Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-14 22:26 -0700
      Re: Comparisons and sorting of a numeric class.... Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-01-16 22:23 +1300
    Re: Comparisons and sorting of a numeric class.... Andrew Robinson <andrew3@r3dsolutions.com> - 2015-01-14 23:23 -0800
      Re: Comparisons and sorting of a numeric class.... Steven D'Aprano <steve@pearwood.info> - 2015-01-15 08:41 +0000
        Re: Comparisons and sorting of a numeric class.... Andrew Robinson <andrew3@r3dsolutions.com> - 2015-01-15 17:45 -0800
          Re: Comparisons and sorting of a numeric class.... Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-16 18:33 +1100
          Re: Comparisons and sorting of a numeric class.... Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-01-17 00:07 +1300
          Re: Comparisons and sorting of a numeric class.... Rustom Mody <rustompmody@gmail.com> - 2015-01-16 03:22 -0800
          Re: Comparisons and sorting of a numeric class.... Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-17 23:27 +1100
        Re: Comparisons and sorting of a numeric class.... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-16 02:27 +0000
        Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-15 22:01 -0700
        Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-16 17:58 +1100
      Re: Comparisons and sorting of a numeric class.... Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-01-15 09:54 -0800
        Re: Comparisons and sorting of a numeric class.... Andrew Robinson <andrew3@r3dsolutions.com> - 2015-01-26 15:47 -0800
        Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-26 18:50 -0700
    Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-15 10:05 -0700
    Re: Comparisons and sorting of a numeric class.... Andrew Robinson <andrew3@r3dsolutions.com> - 2015-01-23 03:46 -0800
      Re: Comparisons and sorting of a numeric class.... Rustom Mody <rustompmody@gmail.com> - 2015-01-23 07:31 -0800
        Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-23 09:50 -0700
          Re: Comparisons and sorting of a numeric class.... Rustom Mody <rustompmody@gmail.com> - 2015-01-23 09:03 -0800
            Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-24 04:23 +1100
              Re: Comparisons and sorting of a numeric class.... Rustom Mody <rustompmody@gmail.com> - 2015-01-23 09:29 -0800
            Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-23 11:04 -0700
              Re: Comparisons and sorting of a numeric class.... Rustom Mody <rustompmody@gmail.com> - 2015-01-23 19:13 -0800
              Re: Comparisons and sorting of a numeric class.... Rustom Mody <rustompmody@gmail.com> - 2015-01-23 19:21 -0800
            Re: Comparisons and sorting of a numeric class.... Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-24 16:01 +1100
              Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-24 16:43 +1100
          Re: Comparisons and sorting of a numeric class.... Rustom Mody <rustompmody@gmail.com> - 2015-01-23 09:22 -0800
            Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-24 04:37 +1100
              Re: Comparisons and sorting of a numeric class.... Rustom Mody <rustompmody@gmail.com> - 2015-01-23 09:45 -0800
                Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-24 05:03 +1100
                Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-23 11:13 -0700
      Re: Comparisons and sorting of a numeric class.... Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-24 19:27 +1100
    Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-23 22:58 +1100
    Re: Comparisons and sorting of a numeric class.... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-23 12:07 +0000
    Re: Comparisons and sorting of a numeric class.... Terry Reedy <tjreedy@udel.edu> - 2015-01-23 11:17 -0500
    Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-23 11:00 -0700

csiph-web