matchpy
matchpy copied to clipboard
Show ReplacementRule while using `replace()` in ManyToOneReplacer
Is it possible to know/display the ReplacementRule if the replace() is able apply the rule?
You can use the underlying matcher to find out which rules match: replacer.matcher.match(subject). If you want to see the replacement steps that are taken, there is currently no function for that. A "debug" function sounds like a good idea though.
I have used the following code:
matches = rubi.matcher.match(expr)
for matched_pattern, substitution in sorted(map(lambda m: (str(m[0]), str(m[1])), matches)):
print('{} matched with {}'.format(matched_pattern, substitution))
which returns:
<function rubi_object.<locals>.<lambda> at 0x10bf93d08> matched with {x ↦ x}
What do I need to do in order to print the ReplacementRule?
Sorry, I didn't realize that the labels used by the ManyToOneReplacer are the replacement lambda functions and do not contain the patterns. Here is a workaround you can try until I can provide debugging functionality:
matches = rubi.matcher.match(expr)
for _ in matches._match(rubi.matcher.root):
for pattern_index in matches.patterns:
renaming = rubi.matcher.pattern_vars[pattern_index]
substitution = matches.substitution.rename({renamed: original for original, renamed in renaming.items()})
pattern = rubi.matcher.patterns[pattern_index][0]
print('{} matched with {}'.format(pattern , substitution))
I will try to add some debugging functionality to the ManyToOneReplacer as soon as possible.
Thanks
It would be nice to have API something like this:
result, applied_rule = replacer.replace(expression, showsteps=True)
Otherwise we would have to compute the result and applied_rule seperately.