Skip to main content
1 of 2
Edmund
  • 43.2k
  • 3
  • 53
  • 148

It seems that you are complicating things by attempting to over specify the behavior. Unless I have misunderstood your intent I believe the following will do.

ClearAll[patternReplaceRule]
patternReplaceRule[p : Verbatim[Pattern][s_, _]] := p :> <|1 -> s|>

Creating the rules

r1 = patternReplaceRule[x_]
x_ :> Association[1 -> x]
r2 = patternReplaceRule[y_]
y_ :> Association[1 -> y]

Applying the rules produce the expected result.

x /. # & /@ {r1, r2}
{<|1 -> x|>, <|1 -> x|>}
y /. # & /@ {r1, r2}
{<|1 -> y|>, <|1 -> y|>}

Hope this helps.

Edmund
  • 43.2k
  • 3
  • 53
  • 148