PEP 218 Re: ANN: set-0.1 module available

James J. Besemer jb at cascade-sys.com
Fri May 17 22:19:00 EDT 2002


Fernando Pérez wrote:

> I beg to differ. Mathematically, there's nothing in the idea of a set that
> makes it immutable. I know that Python is not a mathematics-only language,
> but much of the cleanliness of its design does come from following abstract
> ideas as much as is reasonable. And enforcing immutability on a set is one
> hell of a breakage for an object as basic to many mathematical ideas as a set
> is.

I beg to differ.  Mathematically, there's no notion of assignment like we have in
programming.  From a traditional mathematics view " x = x + 1" is a nonsensical
statement.  At best it's a contradiction in terms.  In Mathematics, you don't
have assignment that can be done over and over.  You can only make statements
about equality or inequality.  At bottom all mathematics is defined in terms of
sets and predicates about sets.

In the context of mathematics I would argue that ALL objects are immutable.  When
you say "2 + 3 = 5", the 2 and the 3 don't cease to exist.  Numbers are
immutable.  Similarly, when you say B union C you are defining a third, new set,
composed of the union of the first two.  The first two don't go away.  They
continue to exist (necessarily) to maintain the definition of the result.  You
don't destroy or reuse the previous ones.  In set theory you can say (assume + is
union)

    A = B + C

but you cannot say

    A += B

or

    A = {1,2,3} ; A = A + {4}

There is no ";" sequential operation in mathematics.  Closest would be to
introduce a new variable, say somethig like:

    A = {1,2,3} ; A' = A + {4}

I've studied the mathematical subject of 'semantics' and modelling things like
assignment is extremely complicated.  You define sets of all possible states of
your computation and then you make statements about the particular states before
and after, say, a particular assignment (multiple IMMUTABLE sets.))

This is complexity of the underlying mathematics is the theoretical justification
some purists (ones who know what they are talking about) use to argue to
eliminate assignment and looping altogether from programming in preference for
lambda and recursion.

It may be the case that sets in Python should be mutable but you can't argue
there's some "mathematical" basis for doing so.  Mathematics suggests the exact
opposite.

> I guess if you want to insist on sets being usable as keys you might come up
> with a pair of set-like types just like we have lists/tuples.

That would seem more complicated than it needs.  I would prefer to say intrinsic
sets are immutable and there's a library function for a mutable version, ala
String.

Alternatively, implement a useful library, let everybody use it for a while and
see how far it gets us.  Maybe nobody want's to use sets as array indicies.

> In my mind, that's like saying that you add integers to a language but you
> can't
> do arithmetic with them ;)

I beg to differ.  ;o)

Fact of the matter, integers, strings and longs presently ARE immutable and you
still CAN do arbitrary arithmetic with them.  E.g.,

    x = 1L << 100000
    x += 1

    s = string.zfill( "1", 10000 )
    s += ".0"

Being immutable in the context of Python does NOT restrict in any fashion the
types of "arithmetic" you can do on objects.  It just means the result is a new
object.  The runtime system can even implement whatever optimization is required
to economize object space or storage or whatever, if performance is a concern.

Regards

--jb

--
James J. Besemer  503-280-0838 voice
http://cascade-sys.com  503-280-0375 fax
mailto:jb at cascade-sys.com







More information about the Python-list mailing list