# What is the use of a list of replacement rules, when there are no curly braces around each one?

I've noticed the following in the Mathematica documentation for ReplaceAll:

Apply the first matching rule:
In[1]:= x /. {x -> 1, x -> 3, x -> 7}
Out[1]= 1

Apply each rule separately:
In[2]:= x /. {{x -> 1}, {x -> 3}, {x -> 7}}
Out[2]= {1, 3, 7}


What is the purpose of the first use case? It's just ignoring some of the input. Is there a situation where this comes in handy? Why wasn't Mathematica made to simply output {1, 3 ,7} for that one, and {{1}, {3}, {7}} for the second?

A step by step explanation of what's happening here could also be helpful?

• Roughly speaking, it is the difference between applying one set of rules vs. three separate sets of rules. The first of those should not act like the second. Feb 7, 2022 at 22:32

This example should be illuminating:

In[3]:= {x, y, z, t} /. {x -> 1, y -> 3, z -> 7}
Out[3]= {1, 3, 7, t}

In[4]:= {x, y, z, t} /. {{x -> 1}, {y -> 3}, {z -> 7}}
Out[4]= {{1, y, z, t}, {x, 3, z, t}, {x, y, 7, t}}


If you study Solve[] and its output, a single solution is a list of rules, one for each variable:

{x -> 1, y -> 3, ...}


Consequently a solution set is a list of solutions:

{
{x -> 1, y -> 4, ...}
{x -> 2, y -> 3, ...},
...
}


Now if I substitute one solution into an expression, I would expect to get one answer:

2 x + y^2 /. {x -> 1, y -> 4}
(*  18  *)


If I substitute a solution set into an expression, you get the value of each expression at each solution, which I might or might not expect but seems reasonable in hindsight:

2 x + y^2 /. {{x -> 1, y -> 4}, {x -> 2, y -> 3}}
(*  {18, 13}    *)


In this view {x -> 1, x -> 2,...} is meaningless as a solution, because it is not possible to replace x by 1 and 2 simultaneously. One would expect one of the following to happen when it is applied: only the first rule would be applied, only the last rule, or you get an error without any replacement. I have forgotten if I read in the documentation somewhere about which of these happens or not -- I can't find it now -- but try it and you find only the first rule is applied when multiple rules match, even for something like

x /. {_ -> 4, x -> 1} (* _ matches everything *)
(*  4  *)

• Thanks @Michael E2, I accepted Szabolcs's answer because it was posted 1st, and was more to the point. I do appreciate the extra information though. Feb 8, 2022 at 2:31
• My example was pulled straight from the Mathematica documentation, including the specification that the first matching rule is the one that's applied. Feb 8, 2022 at 2:34
• @AllisonB You're welcome. We used to recommend that folks wait a day before accepting, because it gives everyone around the world a chance to answer. After that, the OP can select the answer that best helps them. But people got tired of doing that, myself included. Also, if the question is marked "answered," then you might lose out if someone else who knows a really good solution skips the answered question. Feb 8, 2022 at 2:34
• @AllisonB Oh, which documentation page? Feb 8, 2022 at 2:35
• reference.wolfram.com/language/ref/ReplaceAll.html Feb 8, 2022 at 2:35