[Python-ideas] Predicate Sets

David Mertz mertz at gnosis.cx
Mon Jan 20 21:10:14 CET 2014


Although a cute point, I'm not too concerned about the Russell's Paradox
issue.  The obvious implementation will get a "RuntimeError: maximum
recursion depth exceeded" in that case.  But then, no predicate is
guaranteed to halt, so that's not really special to the russellset.

On the other hand, even though I think the idea of a 'predicateset' is cute
mathematically, I'm not really sure what it actually gets you, even in
readability.

I am perfectly happy spelling this:

  mypset = predicateset(somefunc)
  if x in mypset: ...

As:

  if somefunc(x): ...

Even for the set operators, set comprehensions seem pretty much equally
elegant:

  such_that = {1, 2, 3} & mypset  # Looks nice, I agree

But then, this looks pretty nice also:

  such_that = {x for x in {1, 2, 3} if somefunc(x)}

OK, sure the predicateset version might save a few characters, but not all
that many.

If you want to combine predicate sets that's really just like combining
predicates.  It *does* sort of remind me that I'd like some standard HOFs
as builtins or in the standard library (probably in functools).  But still,
where you might write:

  in_both_sets = mypset & mypset2

It's not bad to write a small support module:

  # combinators.py
  def allP(*fns):
      return lambda x: all(f(x) for f in fns)

  def anyP(*fns):
      return lambda x: any(f(x) for f in fns)

Then express the intersection as:

  in_both_pred = allP(somefunc, somefunc2)

>From there, you can just use the predicate 'in_both_pred' as above.
 Similarly for union, define:

  in_either_pred = anyP(somefunc, somefunc2)





On Mon, Jan 20, 2014 at 11:05 AM, Georg Brandl <g.brandl at gmx.net> wrote:

> Am 20.01.2014 08:56, schrieb Andrew Barnert:
> > From: Daniel da Silva <var.mail.daniel at gmail.com> Sent: Sunday, January
> 19,
> > 2014 3:41 PM
> >
> >
> >> Overview: Sets in mathematics can be defined by a list of elements
> without
> >> repetitions, and alternatively by a predicate (function) that determines
> >> inclusion.
> >
> > The whole point of modern set theory is that sets cannot be defined by a
> > predicate alone; only by a predicate _and a set to apply it over_. Which
> we
> > already have in set comprehensions.
> >
> > And your suggestion has the exact same problem that naive set theory had:
> >
> > >>> myset = predicateset(lambda s: s.startswith('a'))
> > >>> 'xyz' in myset
> > False
> >
> > >>> russellset = predicateset(lambda s: s not in s)
> > >>> russellset in russelset
> >
> > Presumably this should cause the computer to scream "DOES NOT COMPUTE!"
> and
> > blow up...
>
> I think it will just raise a NameError...
>
> SCNR,
> Georg
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140120/b0bacc3a/attachment-0001.html>


More information about the Python-ideas mailing list