3
$\begingroup$

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?

Using the code from my other answer (slightly adjusted):

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.

I've had partial success with

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

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

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

1 Answer 1

4
$\begingroup$

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}
$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – JP-Ellis
    Sep 16, 2020 at 11:55
  • 1
    $\begingroup$ @JP-Ellis I agree currently the document of Verbatim is a bit too brief. $\endgroup$
    – xzczd
    Sep 16, 2020 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.