Message Boards Message Boards

6
|
1997 Views
|
0 Replies
|
6 Total Likes
View groups...
Share
Share this post:

Cycling Association through various representations evaluates keys

Posted 10 years ago

I just sent the following message to Wolfram Support. I thought that it would be a good idea to share it here as well because knowing about this problem can prevent introducing some difficult to discover bugs.


I found a behaviour of Association that is likely to become the source of bugs and problems.

First, notice this behaviour:

In[1]:= a = 1
Out[1]= 1

In[2]:= ass = <|a -> 1|>
Out[2]= <|1 -> 1|>

We see that the key is evaluated before the association is created.

In[3]:= a =.

In[4]:= ass = <|a -> 1|>
Out[4]= <|a -> 1|>

In[5]:= a = 1
Out[5]= 1

In[6]:= ass
Out[6]= <|a -> 1|>

Once the association has been created, the key doesn't evaluate further when the association itself is evaluated again.

So why is this a problem? Where can it lead to trouble?

It immediately gives us the idea that maybe the key is going to get evaluated when the association is cycled through various representations. The atomic association object is going to get converted to a compound expression with head Association when cycled through various representations, which is going to cause the keys to get evaluated.

Let's test this:

Compress does it. This can potentially cause trouble:

In[7]:= Uncompress@Compress[ass]
Out[7]= <|1 -> 1|>

Sending it through MathLink connections does it. This is very likely to cause trouble.

In[8]:= LaunchKernels[1]
Out[8]= {KernelObject[1, "local"]}

In[9]:= DistributeDefinitions[a, ass] (* ParallelEvaluate doesn't auto-distribute *)
Out[9]= {a,ass}

In[10]:= ParallelEvaluate[ass]
Out[10]= {<|1 -> 1|>}

In[11]:= ParallelEvaluate[Print[ass]]

(kernel 1) <|1->1|>

Out[11]= {Null}

Cycling through various file formats does it. This can again cause problems when using symbols as keys and the symbol is already present in memory with a value when loading the file.

In[12]:= ImportString[ExportString[ass, "WDX"], "WDX"]
Out[12]= <|1 -> 1|>

In[13]:= ImportString[ExportString[ass, "Package"], "Package"]
Out[13]= <|1 -> 1|>

(The latter one obviously does it, and makes it more clear why it happens in other cases too. Save[] does it too.)

Since MX (I think) stores directly the internal binary representation of expressions (and thus keeps the association atomic), it does not do it:

In[14]:= ImportString[ExportString[ass, "MX"], "MX"]
Out[14]= <|a -> 1|>

Note:

In[15]:= $Version    
Out[15]= "10.0 for Mac OS X x86 (64-bit) (June 29, 2014)"

This is version 10.0.0.

POSTED BY: Szabolcs Horvát
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract