[ANN] MatchMaker.jl: fast symbolic pattern matching for SymbolicUtils

I’ve noticed recently that several contributors to Symbolics.jl and SymbolicUtils.jl like to use @rule for pattern matching, and for good reason. It’s a convenient way to check/rewrite expressions. However, I keep having to discourage this. The implementation of @rule is outdated. It is highly recursive, type-unstable, and slow. It also does not match associative-commutative patterns accurately, has problems with literals in certain positions as well as patterns involving callable structs in the operation slot, and does not scale to large/deep expressions. In short, it was written for a past era of Julia and Symbolics.

MatchMaker.jl fixes all of that! It is algorithmically inspired by MatchPy and related pattern matching literature (cited in the README). The implementation is highly specialized on the way SymbolicUtils.jl does things, and takes advantage of newer data structures such as IRStructure. It matches associative/commutative/associative-commutative patterns, supports single-expression slots, multi-argument “segments”, optional matches, various forms of predicates, and is (compared to @rule) exceptionally fast. If the bot is to be believed, several of the benchmarks in the repository are bottlenecked on expression construction and not the actual pattern matching.

In addition, the package also includes a RuleSet API to compile a list of rules into one data structure that is more efficient than matching each rule sequentially. It also has optimized implementations of replacing all matches in an expression tree, and fixpoint iteration.

The goal for the package is to become the de-facto standard for pattern matching on SymbolicUtils’ expressions. I have plans to refactor parts of Symbolics.jl to leverage this.

Disclaimer: This entire package is AI-generated. I do not currently have the time to dedicate to hand-writing a project like this, but I feel like it’s an important missing piece in the JuliaSymbolics ecosystem. That said, this is not a completely hands-off implementation, which is why it took almost a month. It started with a literature review, a survey of existing tools, then a design document and staged implementation. I have manually reviewed almost all of the source code and a large part of the tests, and several implementation aspects have been iterated on for improved performance. The design document and a rough TODO list are included in the repository. The README also mentions papers/packages used as a reference.

This is also why it is a separate package. I do not feel comfortable throwing a few thousand lines of pure AI code into SymbolicUtils.jl. While this does mean we cannot use it from within SymbolicUtils, I think that that is an acceptable tradeoff.

I plan to add benchmarks to SciMLBenchmarks. If anyone would like to suggest other pattern matching implementations that should be included, please let me know. The package is in the process of being registered, so early adopters will need to manually add it from the git URL.

In case anyone is curious, this took 521M tokens.

Super exciting!!!

Awesome stuff. I opened a few issues after playing with it a bit.

Following your post, I tried MatchMaker on a real rule set: the 80 rules behind trig_reduce (SymbolicUtils 4.56.1, TRIG_REDUCE_RULES: product-to-sum, power reduction, parity/shift cleanup). I ported them rule for rule, in the same order, to mmrule, reusing SymbolicUtils’ own RHS helpers. Inputs are the 108 plain trig_reduce(expr) calls from SymbolicUtils’ test/trig_reduce.jl.

Timings

Julia 1.13.2, Apple Silicon, fresh RuleWorkspace per call, minimum of 5 samples. Baseline is rules-only SymbolicUtils (the same 80 rules). MatchMaker column is BucketIndex. Speedup, memory and allocs are SymbolicUtils / MatchMaker (above 1 = MatchMaker better).

Stage SymbolicUtils MatchMaker Speedup Memory Allocs
Per-site 13.7 ms 2.5 ms 5.4x 1.9x 1.9x
Replace-all, 1 pass 16.3 ms 2.7 ms 6.0x 2.2x 2.2x
Fixpoint (96 inputs where `:post` is correct) 36.0 ms 3.0 ms 12.1x 4.7x 4.7x
Fixpoint (`order=:pre`, all 108) 46.2 ms 6.6 ms 7.0x 3.5x 3.5x
End-to-end (same expand / pass / `simplify_fractions` loop) 123.6 ms 32.5 ms 3.8x 4.5x 4.9x
End-to-end vs the real `trig_reduce` 153.5 ms 32.5 ms 4.7x 5.4x 5.9x

Totals are summed over the 108 inputs. DiscrimNet is slightly faster again (fixpoint 13.4x, end-to-end 3.95x). MatchMaker was faster on all 108 inputs in per-site, replace-all and fixpoint, and on 107/108 end-to-end.

@cryptic.ax I can share you the scripts for benchmark purpose ?

That would be great. Please PR to MatchMaker.jl with changes to the benchmark script.

Would it make sense to have it be called SymbolicMatchMaker or something along those lines to emphasize that it is (at least from my understanding) quite strongly tied to the symbolics ecosystem?

sure.. doing it soon

@cryptic.ax done at Benchmark: add trig_reduce rule-set benchmark - Pull Request #11 - JuliaSymbolics/MatchMaker.jl - GitHub , things are summarised in benchmark/trig_reduce_perf.md , with a probably known issue to you related to fixpoint_rewrite stooping before fix point