Grid Displays of Data Structures In LINQPad and Mathematica

One of the many beautiful things about LINQPad is its display facility, artfully hidden in an extension method called Dump. This is a pass-through method that can be applied to any object. It reproduces its input object, and, by side-effect, produces a fantastically useful display in LINQPad’s “Results” window pane. This facility was so useful that I was inspired to emulate it in Mathematica, which is a system I use every day, even to design code in other programming languages. I consider Mathematica to be the world’s most advanced software-development system. This, despite being pidgeonholed as a computer-math system roughly on par with Maple, Matlab, Sage, Reduce, Derive, and Magma. That’s because, at heart, Mathematica is a flexible conditional term-rewriting system, and term rewriting is a very powerful programming paradigm. But, back to displays:

For an appreciation of the problem and its solution, consider the following snippet of C# code:

ParameterExpression x = Expression.Parameter(typeof(int), "x"); 
Expression<Func<int, int, int>> twiceE = 
 Expression.Lambda<Func<int, int, int>>(
 body: Expression.Multiply( x, Expression.Constant(2) ), 
 parameters: new [] {x, Expression.Parameter(typeof(int), "y")}).Dump();

This is a bit of code that builds a LINQ Expression Tree that represents the anonymous function that multiplies its first argument by two and ignores its second argument. As a C# lambda expression, that function would be

(x, y) => x + 2

That’s a lot easier on the eyes than the C# snippet that represents it, or even than the DebugView in Visual Studio that lets you inspect C#’s internal tree representing this, namely

  System.Int32 $x, System.Int32 $y) {$x * 2}

This representation has the disadvantages that it’s

  • incomplete — lots of data is simply not shown
  • imprecise — the actual structure of the tree is lost in the serialized printout
  • littered — there is too much type information mixed in with the information about data and about structure

In other words, it’s not terribly helpful when you just want to understand the data, the types, and the structure of this object.

LINQPad to the rescue. It displays the following, which perfectly organizes all the kinds of information we want to see. Especially notable is its use of vertical alignment and colors. It’s hard to imagine getting a lot better than this:

LINQPad’s display of a complex tree structure

This kind of visualization capability can eliminate a lot of test code because it greatly improves the understanding of the developer and can lead to proofs of correctness. It’s another case of superior design and implementation technique reducing the load on Quality Assurance.

A similar situation obtains in Mathematica. It has magnificent display capabilities, for all kinds of data, but I’m not aware of a generalized display mechanism for its internal representations of data structures.

Consider, for example, the following definition of a made-up function

SetAttributes[f, {HoldAll, OneIdentity, Flat, Orderless}];
f /: h_ * f[x_] := Exp[x + h];
f /: h_ + f[x_] := Exp[h*x];
f[x_] := Sqrt[x];
f[x_, y_] := Log[x + y];

If you want to look at Mathematica’s internal representation of this function, you do something like this


which yields this

UpValues->{HoldPattern[f[x_] h_]:>Exp[x+h],HoldPattern[f[x_]+h_]:>Exp[h x]},

which is so hard to read that, in practice, you take out pen and paper and start to draw little charts out of it. But this is really sad, because one of Mathematica’s greatest strengths is its awesome Graphics functionality. Let’s put it to work automating the diagramming we would do by hand, and generate this:

Grid display of complex data Structure in Mathematica

Now we can really see all the deep structure.

The code I created to generate this display in Mathematica is in the public domain, and you may feel free to copy it from here and use it in your own work.


~ by rebcabin on August 2, 2012.

One Response to “Grid Displays of Data Structures In LINQPad and Mathematica”

  1. […] using the gridRules utility from here. […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: