egraphs-good / egglog

egraphs + datalog!
https://egraphs-good.github.io/egglog/
MIT License
458 stars 54 forks source link

Multi-Pattern Example #423

Closed jafioti closed 1 month ago

jafioti commented 2 months ago

Are there any examples of using multi-pattern rewrites? I couldn't find any, but the paper claims this is possible

yihozhang commented 2 months ago

Take the multi-pattern rewrite rule in Figure 2 of this paper for example. It can be expressed in egglog as

(datatype Expr 
    (matmul Expr Expr) 
    (split0 Expr) 
    (split1 Expr) 
    (split i64 Expr) 
    (concat i64 Expr Expr))

(rule (
    (= e1 (matmul i1 i2))
    (= e2 (matmul i1 i3))
    (!= i2 i3)
) (
    (let fused (matmul i1 (concat 1 i2 i3)))
    (let new-e1 (split0 (split 1 fused)))
    (let new-e2 (split1 (split 1 fused)))
    (union new-e1 e1)
    (union new-e2 e2)
))