11
$\begingroup$

What can internally be happening here? Is the evaluator just messing with us and going non-standard because it's a Bag? Or am I just not seeing how something like this could be done in Mathematica?

AppendTo[$ContextPath, "Internal`"];

In[19]:= ClearAll[x, y];
{x, y} = {Bag[{1, 2, 3}], Bag[{4, 5}]};

Heads are

In[21]:= Head /@ {x, y}

Out[21]= {Bag, Bag}

But they are just a head, no depth. Depths

In[22]:= Depth /@ {x, y}

Out[22]= {1, 1}

However, they are different. I take their second elements, or I print them in InputForm

In[23]:= BagPart[#, 2] & /@ {x, y}
InputForm /@ {x, y}

Out[23]= {2, 5}

Out[24]= {InputForm[Internal`Bag[{1, 2, 3}]], InputForm[Internal`Bag[{4, 5}]]}
$\endgroup$

2 Answers 2

15
$\begingroup$

This is not related to evaluation. Internal`Bag, like many other special types, is an atomic object. You can verify this using AtomQ[x]. This is despite its InputForm suggesting a structure.

This is no different from how Graph, Rational, Complex or SparseArray behave. (Though for SparseArray, most list manipulation functions are implemented, so it's much more difficult to notice that it is atomic).


A similar example using Rational:

In[206]:= rats={1/2,2/3}
Out[206]= {1/2,2/3}

In[208]:= Head/@rats
Out[208]= {Rational,Rational}

In[209]:= Depth/@rats
Out[209]= {1,1}

In[210]:= FullForm[rats]
Out[210]//FullForm= List[Rational[1,2],Rational[2,3]]
$\endgroup$
3
  • 2
    $\begingroup$ I am starting to hate to vote for your answers because I will never catch up, but their quality compels me. +1 yet again $\endgroup$
    – Mr.Wizard
    Commented Jan 28, 2012 at 17:11
  • 1
    $\begingroup$ I would make a distinction between Graphs and other objects. For most atomic objects in the past, it was possible to deconstruct and reconstruct them by using their InputForm (FullForm), the feature which I consider essential. This plays well with immutability. For Graph-s, a decision was made to introduce mutable state (properties), and their InputForm is deceiving. I dislike this feature, and also think that one should distinguish these two situations. You are certainly well-aware of this discussion: stackoverflow.com/questions/5964469/… $\endgroup$ Commented Jan 28, 2012 at 17:13
  • 2
    $\begingroup$ Actually, I gave the wrong link. This is the one I meant: stackoverflow.com/questions/4301833/…, and also I share the point of @WReach which he expressed here: stackoverflow.com/questions/7363253/… $\endgroup$ Commented Jan 28, 2012 at 17:25
8
$\begingroup$

I think Internal`Bag is a monolithic object which is only constructed using the Bag[list] syntax. Note that the same is true for Graph:

Graph[{1<->2,2<->3}]//Head
(*
==> Graph
*)
Graph[{1<->2,2<->3}]//Depth
(*
==> 1
*)

Also note the output of the objects when typing directly in the kernel:

Internal`Bag[{2,3}]
(*
==> Internal`Bag[<2>]
*)
Graph[{1<->2,2<->3}]
(*
==> Graph[<3>, <2>]
*)

Even FullForm doesn't give more information, which shows that on the Mathematica expression level that's all there is.

$\endgroup$
5
  • $\begingroup$ It is not that surprising that FullForm is broken with Bag since Bag is not really meant for user consumption.. $\endgroup$
    – Szabolcs
    Commented Jan 28, 2012 at 17:12
  • $\begingroup$ @Szabolcs: But Graph is definitely meant for user consumption, and it shows exactly the same behaviour. $\endgroup$
    – celtschk
    Commented Jan 28, 2012 at 17:15
  • $\begingroup$ Regarding Graphs, see my comment below @Szabolcs's answer. $\endgroup$ Commented Jan 28, 2012 at 17:18
  • $\begingroup$ Of course, I just meant that the edges are not as polished as for the other types, e.g. FullForm on Graph does work even though it's atomic (but pattern matching doesn't --- while it works for older types like Rational) $\endgroup$
    – Szabolcs
    Commented Jan 28, 2012 at 17:21
  • $\begingroup$ Maybe it's a version thing; as I've shown in my post, Graph for me shows the exact same behaviour for FullForm as Bag (parameters showing up as <n>). $\endgroup$
    – celtschk
    Commented Jan 28, 2012 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.