I got into an unfortunate situation, involving fractions that can get changed by evaluation, which occur as (part of) keys in my associations. I will probably steer far away from the entire situation, mostly because of the following quote.
In this answer, with respect to evaluation of keys in associations, Taliesin tells us
Wrap the entire key in Hold, if you really want to keep things that might evaluate in the keys of an association (HoldPattern is a red herring: keys aren't patterns). Alternatively use ToString. But generally this just sounds like a dangerous and confusing game to play, to me.
Strange Example
Anyway, I would still like to know what exactly could be going on in the following example. The evaluations in this question were executed using version 10.3.
assoc = Association[];
assoc[1/2] = "rational?";
assoc[Rational[1, 2]] = "rational exposed";
assoc[Unevaluated@Rational[1, 2]] = "rational unevaluated";
(*FF is short for FullForm*)
assoc[Unevaluated[1/2]] = "big FF";
Note that in the following evaluation, the first two expressions have the same FullForm
, so of course they should evaluate in the same way. Perhaps nothing seems strange yet
assoc[[Unevaluated@Key[1/2]]]
assoc[[Unevaluated@Key[Times[1, Power[2, -1]]]]]
assoc[[Unevaluated@Key[Rational[1, 2]]]]
assoc[1/2]
"big FF" "big FF" "rational unevaluated" "rational exposed"
However, it seems there are two expressions with the same FullForm
in the same association. Here are the InputForm
and FullForm
of the key-value pairs.
List @@@ Hold @@ KeyValueMap[Hold, assoc2] // InputForm
Hold[{1/2, "rational exposed"}, {Rational[1, 2], "rational unevaluated"}, {1/2, "big FF"}]
List @@@ Hold @@ KeyValueMap[Hold, assoc2] // FullForm
Hold[List[Rational[1,2],"rational exposed"], List[Rational[1,2], "rational unevaluated"], List[Times[1,Power[2,-1]],"big FF"]]
Note that 1/2
evaluates to Rational[1,2]
. It can be shown with another test that the definition corresponding to "rational exposed"
has overwritten the one corresponding to "rational?"
.
My best guess for explaining this: Possibly the internal representation of integers is different when we enter the expressions in these different ways.
Question: What can explain this example?
DownValues
works correctly. $\endgroup$FullForm /@ (1/2 // Trace)
? $\endgroup$