3
$\begingroup$

How can be a fraction inside Hold replaced? Nothing of the following works:

h = Hold[1/2 (2 + 3 + 3/7) 7];

Replace[h, 1/2 -> q, All]
Replace[h, 3/7 -> q, All]
Replace[h, Times[1, Power[2, -1]] -> q, All]
Replace[h, Times[3, Power[7, -1]] -> q, All]

(* same for all *)
Hold[1/2 (2 + 3 + 3/7) 7]

However replacing single number works:

Replace[h, 7 -> q, All]

Hold[1/2 (2 + 3 + 3/q) q]

Update

Another strange behavior:

h = ToExpression["(2+3)/6*9", InputForm, Hold];

h /. HoldPattern[1/6] -> q

Hold[1/6 (2 + 3) 9]

We see that 1/6 was not replaced.

But now copy Hold[1/6 (2 + 3) 9] from previous output paste it in another cell and append that same rule /. HoldPattern[1/6] -> q.

Hold[1/6 (2 + 3) 9] /. HoldPattern[1/6] -> q

Hold[q (2 + 3) 9]

We see that this time 1/6 was replaced but we used the same expression as is stored in h when the replacement did not occure.

$\endgroup$
1
  • $\begingroup$ Maybe h /. HoldPattern[1/2] -> q $\endgroup$ Commented Oct 1, 2025 at 16:33

1 Answer 1

4
$\begingroup$
h = Hold[1/2 (2 + 3 + 3/7) 7];

Using HoldPattern:

Replace[h, HoldPattern[3/7] -> q, All]

Hold[1/2 (2 + 3 + q) 7]

Update 1: Why HoldPattern[3/7] Works

While 3/7 is internally represented as Times[3, Power[7, -1]] (which you correctly saw in FullForm), the pattern HoldPattern[3/7] works because it instructs the pattern matcher to look for the expression that originated as 3/7 , without attempting to evaluate or strictly enforce the internal form on the pattern itself.

Update 2: Copy-Paste Anomaly (Output vs. Input)

Your update is an excellent observation that uncovers a core subtlety: Output Formatting is NOT Input. The difference in behavior is due to how the Frontend (Notebook interface) handles held expressions:

Initial Failure (h reference): When you run h /. HoldPattern[1/6] -> q, the replacement fails because the Kernel's Times sorting (Orderless) groups all factors. 1/6 doesn't exist as a separate replaceable argument; it's just one factor among many within a single Times head, and it's not at the top level of the expression inside Hold.

Internal h:Hold[Times[9,1/6,5]]→No direct match for 1/6 as a subexpression.

Successful Replacement (Copy-Paste): When you copy and paste the output, the Frontend often re-parses the expression and might implicitly change the grouping or re-introduce a Times head, making 1/6 exist as a distinct, replaceable subexpression again:

Pasted Input:Hold[1/6⋅(rest of expression)]→Match succeeds. The expression you pasted looks the same, but its internal tree structure was slightly modified during the copy/paste process, allowing the pattern matcher to finally see 1/6 as an independent part.

$\endgroup$
7
  • 1
    $\begingroup$ But 3/7 is represented by Times[3, Power[7, -1] as can be seen in FullForm of the h. So why HoldPattern[3/7] should much the pattern when the pattern is not 3/7? $\endgroup$ Commented Oct 1, 2025 at 16:46
  • 1
    $\begingroup$ The documentation says: HoldPattern[expr] is equivalent to expr for pattern matching, but maintains expr in an unevaluated form. But HoldPattern[3/7] // FullForm outputs Times[3, Power[7, -1] so evidently the expression inside HoldPattern was evaluated. $\endgroup$ Commented Oct 1, 2025 at 16:51
  • 1
    $\begingroup$ Can you explain also another example I added in OP? $\endgroup$ Commented Oct 1, 2025 at 17:13
  • $\begingroup$ The documentation refers to Standard Evaluation (defs, upvalues, etc.). However, 3/7 is a literal expression and is converted to its canonical form (Times[3,Power[7,−1]]) at the absolute earliest stage - before the evaluation that HoldPattern prevents. So, the pattern matcher actually sees HoldPattern[Times[3,Power[7,−1]]]. It works because HoldPattern is transparent to the matcher, letting the internal canonical form of the pattern match the identical canonical form inside the Hold expression. See the update, please! $\endgroup$ Commented Oct 1, 2025 at 17:18
  • 1
    $\begingroup$ The problem is that h//FullForm is not the same as ToExpression[ToBoxes[h,StandardForm],StandardForm]//FullForm even though the OutputForms are identical. It is likely an implementation accident that ToExpression and ToBoxes are not true inverses in this case. $\endgroup$ Commented Oct 1, 2025 at 18:10

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.