MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Bug with Sequence and Assignment by Part...

  • To: mathgroup at smc.vnet.net
  • Subject: [mg121636] Re: Bug with Sequence and Assignment by Part...
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Fri, 23 Sep 2011 03:43:58 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201109221126.HAA26759@smc.vnet.net>

I agree with your analysis, but I disagree with your conclusions.


> Surely I am not the first to encounter this,


Indeed:

http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread/thread/b7303eafccee6d5a



> and surely it constitutes
> at least one bug, (and surely it is shouldn't be in Mathematica 8!).


I wouldn't be so sure. Using Sequence to insert several elements is not a
standard form of assignment.
The way I understand idiomatic Mathematica, it is a hack. The fact that you can sort
of do it does not mean that all
aspects of it must conform to your expectations. Every decent language
allows many more things than
those for which it was primarily designed. If you are exploring the darker
corners, be prepared to face the
consequences. As for Mathematica, the observed behavior is in complete agreement
with the semantics of
Set / SetDelayed (both being SequenceHold) and Sequence, and the evaluation
procedure.

So, IMO, this is not a bug.  In my reply in the quoted thread, I gave some
arguments in support of this behavior.
One point was that assignments using Sequence in this manner could not be
made generally efficient,
given that lists are implemented as arrays in Mathematica . It is one thing to assign
a pointer to the Sequence
object in a pointer array  (don't need resizing), and another to shift an
array of pointers to accommodate
new elements. There is no way around the fact that insertion in an array is
a linear-time operation in
the size of an array - to avoid that you'd need lists implemented as  linked
lists.

Also, from the language design perspective, adding a special case to Set -
Sequence (and complicating the
evaluation semantics even more), and silently expanding / shifting arrays
(and giving assignment a linear
time complexity without giving a clue to the user) would IMO be a terribly
bad design decision, for at least two
reasons. First,  Sequence is a general head, and its semantics are higher
level than those of the memory
operations needed to truly insert new elements into a list. Second, in Mathematica 
(being so high - level) it is not always
easy to estimate the complexity of code, and I'd at least like to be sure
that a single element assignment is
always a constant - time operation.




>  I mean,
> it seems to me that FullForm and Length should also detect the
> presence of Sequence and act differently than they do here.
>

IMO, this would also be bad. Sequence splicing is a result of evaluation (a
step in evaluation sequence). What you
request again requires exceptions to the main evaluation sequence. For such
a system as Mathematica,
you pay a huge price for exceptions  -  even in its current general form,
evaluation sequence is  complex enough.
I actually think that it is this generality and consistency of evaluator
(and, more generally, the languagte) that
makes possible much of further Mathematica development and component integration.
Start adding ad hoc stuff that
*looks* convenient, and you get what many other systems offer - a bunch of
not so well integrated functionality
and inelegant languages full of magic and special cases.


Regards,
Leonid



  • Prev by Date: Re: Calculus and InterpolatingFunction
  • Next by Date: Re: Plot axis length and size ratio (TwoPlot revive)
  • Previous by thread: Bug with Sequence and Assignment by Part...
  • Next by thread: Re: Bug with Sequence and Assignment by Part...