Apply function to the second level if the first level is integral from zero to infinity, otherwise, not evaluate

How can I make a function, say, R[expression] such that it works on arguments like this Integrate[f[x],{x,0,Infinity}], applying some operator r[] to the function under integral, and leaving the epression unevaluated if the first level is not an integral from zero to infinity?

For instance, R[Integrate[Sin[x],x]] should produce Integrate[r[Sin[x]],x]] but R[Sin[x]] should be left unevaluated.

I have tried to match the pattern this way but it did not work:

R[Integrate[f_[x_], {x_, 0, Infinity}]] := Integrate[r[f[x]], {x, 0, Infinity}]

• Your function R needs to hold its arguments, otherwise the argument will be evaluated before R can do anything. So use SetAttributes[R, HoldFirst], for example. Jul 12, 2022 at 11:36

To elaborate on my comment:

ClearAll[R]
SetAttributes[R, HoldFirst]
R[Integrate[f_[x_], {x_, 0, Infinity}]] := Integrate[r[f[x]], {x, 0, Infinity}]

R[Integrate[Sin[x], {x, 0, Infinity}]]
R[Integrate[Sin[x], {x, 0, 1}]]
R[Integrate[Sin[x], x]]
R[Sin[x]]


You specified that the expression should remain unevaluated if it's not an integral from 0 to Infinity, so I assume the 2nd and 3rd lines should not evaluate.

• Thanks! This works well! Jul 12, 2022 at 11:44
• Hmm, it does not work if under the integral a simple expression of x... Jul 12, 2022 at 12:42

Not at my computer but the following should give slightly more general solution.

ClearAll[rInt]
SetAttributes[rInt, HoldFirst]
rInt[Integrate[f_, x:{_Symbol,0,Infinity}..]] :=Integrate[r[f], x]


Then the following give the expected result

rInt[Sin[x]]

rInt[Integrate[Sin[u],u]]

rInt[Integrate[Sin[y],{y,0,Infinity}]]

rInt[Integrate[u^2 - Log[v],{u,0,Infinity},{v,0,Infinity}]]


Hope this helps.