19

Bug introduced in 10.0 and fixed in 11.0.0

Dataset is new in 10.0


Note: In order to reproduce the bug, the symbol Dataset may need to be evaluated (to load its definitions) before defining toDataset.

My little function, from pre-10.2 ,to turn a headed table into a Dataset.

toDataset[table_List] := 
  Dataset@(Association /@ (Function[r, Thread[First[r] -> #] & /@ Rest[r]]@table));

In 10.2, ??toDataset shows something very odd indeed (after a big barf of messages).

Mathematica graphics

It appears to be something related to the head Dataset being on the RHS.

Ymareth
  • 4,741
  • 20
  • 28
  • That is the example, the function definition itself is broken. Its not an application of that function that breaks, its defining the function that breaks. – Ymareth Jul 24 '15 at 17:12
  • Yes, I've just tested on another machine and its fine. I jumped the gun. I may have a corrupt install. Thanks for checking. Except that was 10.1... doh! – Ymareth Jul 24 '15 at 17:16
  • Tried on 10.2 on a different machine. When I do ??toDataset I get a spew of TextForm messages and a definition as above. Are you sure your running this on 10.2? Its fine on 10.1. – Ymareth Jul 24 '15 at 17:19
  • I really didn't get it at first. Now I understand. I edited the question a bit for clarity. – Szabolcs Jul 24 '15 at 17:34
  • Sorry for confusing things with 10.1. That is its full glory (gorey?). – Ymareth Jul 24 '15 at 17:35

2 Answers2

16

The problem appears to be due to a v10.2 bug in the Dataset visualization code that generates the box form of a dataset. It is not correctly distinguishing between Dataset being used as a constructor function and Dataset being used as the head of a constructed dataset. It assumes the latter case unconditionally, giving the exhibited messages for the former case.

As a work-around, we can temporarily block the errant definitions while we inspect toDataset:

Block[{Dataset}, Information[toDataset]]

information screenshot

The defect appears to be a subtle interaction bug between Information and Dataset since expressions like the following do not exhibit the bug:

DownValues[toDataset]

(*
{HoldPattern[toDataset[table_List]]:>
   Dataset[Association/@Function[r,(Thread[First[r]->#1]&)/@Rest[r]][table]]}
*)

Update

The problem is more general than within the context of Information. Both of the following expressions also generate many warning messages:

Unevaluated @ Dataset @ (* ...expr from question... *)

Defer @ Dataset @ (* ...expr from question... *)

Furthermore, there appears to be an evaluation leak involved:

Unevaluated @ Dataset[Print["leak!"]]

evaluation leak screenshot

Defer @ Dataset[Print["leak!"]]

evaluation leak screenshot

This behaviour is very much like what happens with Graphics objects. The front-end attempts to render them in output cells, which implies evaluation:

Defer@Graphics[Print["leak!"]]

Graphics leak screenshot

We might just have to live with this behaviour in both cases (although a way to turn it off would be useful, just as the debugger allows for graphics).

WReach
  • 68,832
  • 4
  • 164
  • 269
  • Basically, evaluation control structures like Defer and Unevaluated don't apply to typesetting. Notice that Defer @ Dataset[Print["leak!"]]; doesn't leak. – Stefan R Jul 24 '15 at 21:18
  • 2
    @StefanR Indeed, that was my point about the similarity to Graphics. In fact, Dataset is a little better than Graphics in that it only renders when it is the outermost head. That is why the original problem is surprising: a Dataset is being rendered even though it appears in a non-top-level context, namely the right-hand-side of a definition. This is potentially dangerous, and a regression from v10.1. – WReach Jul 24 '15 at 21:21
  • 1
    I have filed an internal report to get this looked into. – Stefan R Jul 24 '15 at 21:22
  • @StefanR You have an incoming "feedback" email from me as well, CASE:3391689 :) With more details coming. – WReach Jul 24 '15 at 21:23
10

A work around seems to be to not have Dataset as the first Head encountered...

toDataset[table_List]:=Dataset@@{Association/@(Function[r,Thread[First[r]->#]&/@Rest[r]]@table)};

So the first Head the parser sees is Apply and not Dataset.

Ymareth
  • 4,741
  • 20
  • 28