Re: Mathematica daily WTF (101221T)
- To: mathgroup at smc.vnet.net
- Subject: [mg114930] Re: Mathematica daily WTF (101221T)
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Thu, 23 Dec 2010 03:53:26 -0500 (EST)
- References: <201012220734.CAA10375@smc.vnet.net>
Hi kj, FWIW, David Wagner calls heads Evaluate, Unevaluated and Sequence the "magic cookies", that is, elements deeply wired into the language which are hard or impossible to reproduce at the top level. OTOH, rules like the one you used for <sequence> are dangerous because of their generality (lots of BlankNullSequence-s plus UpValues). So I actually think that it is more remarkable that you *can* reproduce some behavior of Sequence than that there are problems in some use cases. The following workaround should work reasonably well (if a bit too restrictive): Clear[sequence] sequence /: expr : w_[x___, sequence[y___], z___] /; ! MatchQ[ Unevaluated[expr], HoldPattern[If[_, sequence[y], t_, sequence[y]]] /; ! FreeQ[t, MakeBoxes[sequence[y], StandardForm]]] := w[x, y, z]; but it of course uses the implementation details. One more thing: you won't be able to easily reproduce the SequenceHold attribute in this approach. Regards, Leonid On Wed, Dec 22, 2010 at 10:34 AM, kj <no.email at please.post> wrote: > Today's daily wonder comes from an innocent attempt to define a > home-grown version of Sequence: > > In[1]:= sequence /: w_[x___, sequence[y___], z___] := w[x, y, z]; > > > It seems to work: > > In[2]:= foo[1, 2, sequence[3, 4, 5], 6, 7] === > foo[1, 2, Sequence[3, 4, 5], 6, 7] > > Out[2]= True > > > But, when evaluated by itself, it causes the Mathematica output > procedure to spill its guts in a most undignified fashion: > > In[3]:= sequence[3, 4, 5] > > During evaluation of In[3]:= If::argb: If called with 8 arguments; between > 2 and 4 arguments are expected. >> > > Out[3]= If[False, 3, 4, 5, > With[{OutputSizeLimit`Dump`boxes$ = > Block[{$RecursionLimit = Typeset`$RecursionLimit}, > MakeBoxes[sequence[3, 4, 5], StandardForm]]}, > OutputSizeLimit`Dump`loadSizeCountRules[]; > If[TrueQ[BoxForm`SizeCount[OutputSizeLimit`Dump`boxes$, 1048576]], > OutputSizeLimit`Dump`boxes$, > OutputSizeLimit`Dump`encapsulateOutput[ > sequence[3, 4, 5], $Line, $SessionID, 5]]], 3, 4, 5] > > > Ironically, the idea for trying out this self-rolled implementation > of sequence as a "universal up-value" comes from a passing remark > by Robby Villegas in an article (http://is.gd/j8V9f) where he > introduces such "universal up-values" as an example for why MakeBoxes > (see Out[3] above) needs to be so "nasty". Point taken, but clearly, > there's a leak, at least in version 7. > > (I hope that someone at Wolfram is making note of reports like this > one, since, as far as I can tell, there's no public bug report page > for Mathematica.) > > ~kj > >
- References:
- Mathematica daily WTF (101221T)
- From: kj <no.email@please.post>
- Mathematica daily WTF (101221T)