2
$\begingroup$

Question about using an association within Replace.

If I have an association which contains symbols like so:

dict = <|Subscript[r, 1] -> 1, Subscript[r, 2] -> 2|>

and I do some operation using these variables for e.g.

lst = f[#1[[1]], #[[2]]] & /@ {{Subscript[r, 1], 1},{Subscript[r, 2],2}}

Then, if I try to use the association defined in dict within Replace, it doesn't seem to recognize the variable.

lst /. f[x_, y_] -> dict[x]

and throws a KeyAbsent error:

{Missing["KeyAbsent", Subscript[r, 1]], 
 Missing["KeyAbsent", Subscript[r, 2]]}

Here's a minimal working example to reproduce the error:

Block[{dict, lst},
dict = <|Subscript[r, 1] -> 1, Subscript[r, 2] -> 2|>;
lst = f[#1[[1]], #[[2]]] & /@ {{Subscript[r, 1], 1},{Subscript[r,2], 2}};
lst /. f[x_, y_] -> dict[x]
]

Why is the output not {1,2} ?

$\endgroup$
4
  • $\begingroup$ Hi @Szabolcs, all good points. I went a bit overboard as a result of my frustrations. I've modified the post. $\endgroup$
    – v1j4y
    Aug 12, 2021 at 8:55
  • 1
    $\begingroup$ Thanks for the edit, this addresses my concerns. Regarding why I removed the bugs tag: we try to add this tag only after a bug was confirmed by the community (see tag description). $\endgroup$
    – Szabolcs
    Aug 12, 2021 at 8:55
  • $\begingroup$ Ah right ! Thanks. Just out of curiosity, how does one go about verifying that a result is a bug ? Is it posted in the chat first ? $\endgroup$
    – v1j4y
    Aug 12, 2021 at 9:01
  • 1
    $\begingroup$ The idea is that if you post a new question, you should not use bugs. Most of the time, you should not use it at all for your own questions. Feel free to add this tag to other people's questions if after a careful look, you have become convinced that it really is a bug (and not a misunderstanding by the asker). You can think of it as "volunteer peer review". $\endgroup$
    – Szabolcs
    Aug 12, 2021 at 9:03

1 Answer 1

3
$\begingroup$

This is not a bug. Compare the output of these two lines:

lst /. f[x_, y_] -> dict[x]

and

lst /. f[x_, y_] :> dict[x]

In the first case, dict[x] evaluates to Missing["KeyAbsent", x] and then a value is substituted for x.

In the second case, first a value such as Subscript[r,1] is substituted for x in dict[x], obtaining dict[ Subscript[r,1] ]. Evaluation proceeds afterwards.

$\endgroup$
3
  • $\begingroup$ Perfect ! Thanks. Why is it that after 5 years of consistent MMA use, I still fall for these "trivial" oversight ? I wonder if its just my stupidity or whether this notations is unnatural. $\endgroup$
    – v1j4y
    Aug 12, 2021 at 8:57
  • $\begingroup$ @v1j4y The output Missing["KeyAbsent", Subscript[r, 1]] really is misleading. Consider how we always use := instead of = in function definitions, even though technically it is necessary only sometimes. I think it would make sense to default to :> in replacement rules as well, unless there is a clear reason to use ->. For some reason, we don't conventionally do this, as a community. Maybe it's time to change. $\endgroup$
    – Szabolcs
    Aug 12, 2021 at 9:00
  • 1
    $\begingroup$ Right ! So the -> signifies: evaluate then replace and :> signifies: replace and hold right ? Similar to the = and the hold version :=. $\endgroup$
    – v1j4y
    Aug 12, 2021 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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