JuliaSymbolics / Metatheory.jl

Makes Julia reason with equations. General purpose metaprogramming, symbolic computation and algebraic equational reasoning library for the Julia programming language: E-Graphs & equality saturation, term rewriting and more.
https://juliasymbolics.github.io/Metatheory.jl/dev/
MIT License
362 stars 47 forks source link

is this the best way to `reverse(r::Rule)`? #126

Open anandijain opened 2 years ago

anandijain commented 2 years ago

I haven't really read through this repo, but I didn't see an easy way to take @rule ~x => exp(~x) to @rule exp(~x) => ~x. This was the hack I came up with

myreverse(r::Metatheory.Rules.AbstractRule) = myreverse(r.expr)

function myreverse(rex)
    args = rex.args
    newex = Expr(:call, args[[1, 3, 2]]...)
    eval(:(@rule $newex))
end

it seems to work fine but it might be a bad way of doing it

0x0f0f0f commented 2 years ago

Hi anand. This is possible and fairly easy. I can add a function overloading Base.inv, but you have to know that inverting a rule that has more variables on the lhs. than the rhs will result in an error. (e.g., @rule f(~x, ~y, ~z) => g(~x) is not invertible)