# Replace Condition in Replacement Rules

In finding an answer to my other question, I'm finding myself needing to manipulate the conditions imposed on replacement rules, but this is proving to be a little difficult. In particular, how should I go about removing instances of Condition and PatternTest?

f[x_?InexactNumberQ] := x^2;
g[x_] /; FooQ[x] := x^3;

Attributes[ExpandValues] = {HoldAll};
ExpandValues[symbol_] := Join @@ Through[
{OwnValues, DownValues, UpValues, SubValues, DefaultValues, NValues}[symbol]
];
ExpandValues[symbol_, symbols__] := Join[ExpandValues[symbol], ExpandValues[symbols]];


then the replacement rules that need to be modified are:

{
HoldPattern[f[(x_)?InexactNumberQ]] :> x^2,
HoldPattern[g[x_] /; FooQ[x]] :> x^3
}


I would like these to be modified to not have any conditions on the arguments, but trying to replace the condition and pattern test is proving difficult. This for example does not work:

{
HoldPattern[Condition[p_, q_]] :> p,
HoldPattern[PatternTest[p_, q_]] :> p
}


I've also tried using Verbatim which the documentation suggests is useful to transform other transformation rules, but I have not gotten them to work. I'm also thinking that the use of Verbatim won't work generally because it is too literal.

{
Condition -> (#1 &),
PatternTest -> (#1 &)
}


but when the replacement appears within a HoldPattern, the resulting rule after replacement does not work.

• {HoldPattern[f[(x_)?InexactNumberQ]] :> x^2, HoldPattern[g[x_] /; FooQ[x]] :> x^3} /. (Condition | PatternTest)[ p_, q_] :> p works.
– kglr
Sep 16, 2020 at 17:05
• Interesting that this works, but if you split the two alternatives into their own rules then it doesn't work. Sep 17, 2020 at 2:24

Well, not sure how you used it, but I think what you need is exactly Verbatim:

{HoldPattern[f[(x_)?InexactNumberQ]] :> x^2, HoldPattern[g[x_] /; FooQ[x]] :> x^3} /.
{Verbatim[Condition][p_, q_] :> p, Verbatim[PatternTest][p_, q_] :> p}

(*
{HoldPattern[f[x_]] :> x^2, HoldPattern[g[x_]] :> x^3}
*)


Inspired by kglr's comment above, another approach is to place Condition and PatternTest into Pattern:

{HoldPattern[f[(x_)?InexactNumberQ]] :> x^2, HoldPattern[g[x_] /; FooQ[x]] :> x^3} /.
{(a : Condition)[p_, q_] :> p, (a : PatternTest)[p_, q_] :> p}

• Wow, that's simple... I was indeed using Verbatim wrong by applying to the whole condition (Verbatim[Condition[p_, q_]]). I didn't realise it could be applied to just the head. Sep 16, 2020 at 11:55
• @JP-Ellis I agree currently the document of Verbatim is a bit too brief. Sep 16, 2020 at 12:04