# ReplaceAll causing infinite recursion

I have two expressions

expressionA = (-B*a + b)/M
expressionB = (-A*c + d)/M


expressionA depends on B and vice versa. What I would like to do is replace B in expressionA with expressionB.

I tried doing this by using the ReplaceAll operator /. which works, but only if I don't assign the result to a new variable, i.e

expressionA /. B -> expressionB
expressionB /. A -> expressionA


gives the expected outputs:

(b - (a (-A c + d))/M)/M
(d - ((b - a B) c)/M)/M


However, if I now want to define a new variable as

A = expressionA /. B -> expressionB


I get the following error:

$RecursionLimit: "Recursion depth of 1024 exceeded during evaluation of b-(a(-Ac+d))/M". This happens not only if I try to define A as above but also if copy paste the correct output, i.e. if I try A=(b - (a (-A c + d))/M)/M  I get the same error as above. Does anyone know how to fix this problem or if there is an alternative way to achieve my goal? • What is your goal? What is wrong with the first approach? What do you need to do next? You do understand why you're getting the error message, don't you? Nov 12, 2023 at 17:48 • My goal is to replace B in expressionA with expressionB. The problem is that I need these expressions with the replacements later on and whenever I try to do something with the results I get the error message. Also, I do not understand why I get the error message. This is actually my first project with Mathematica so I might be missing something trivial... Nov 12, 2023 at 17:58 • Regarding the error... evaluate this x = x + 1. Formally, there's no problem here. We even get OwnValues (OwnValues[x] gives {HoldPattern[x]:>1+x}). However, when we now want to evaluate x+1, we must first figure out how to replace x, which is simple enough and gives us x+1+1, but now we need to replace x, which is still simple: x+1+1+1, but now we need to replace x...you see where this is going. Nov 12, 2023 at 18:58 • "My goal is to replace B in expressionA with expressionB." I was afraid you would say this. Yes, I understand that's how you communicated your goal, but I'm asking for some more context. You already figured out one way to achieve that specific goal, but you weren't satisfied for some reason. So, before I wanted to propose a solution, I wanted to understand where this whole thing was headed. You might need some sort of Hold or HoldForm. You might want a Template of some sort. You might want to use Rule instead of Set. Maybe you need Equals instead of Set. Nov 12, 2023 at 19:03 • At this point, all I can tell you is that a recursive definition with Set isn't going to work. Nov 12, 2023 at 19:03 ## 1 Answer $Version

(* "13.3.1 for Mac OS X ARM (64-bit) (July 24, 2023)" *)

Clear["Global*"]


You can never Set a variable to an expression that contains that variable. This would always produce an infinite recursion. Instead, modify the meanings of expressionA and expressionB

eqns = {
expressionA == (-B*a + b)/M,
expressionB == (-A*c + d)/M};

rules = Thread[{A, B} -> eqns[[All, 2]]]

{A -> (b - a B)/M, B -> (-A c + d)/M}

(eqns /. rules)

{expressionA == (b - (a (-A c + d))/M)/M,
expressionB == (d - ((b - a B) c)/M)/M}


Iterating the replacements:

NestList[# /. rules &, eqns, 3] // Column
`