Metatheory.jl
Metatheory.jl copied to clipboard
Scope of classic @rule matcher
Same issue as reported in SymbolicUtils.jl: reversing the order of two differential operators
# test_simple_rewrite.jl
using Symbolics, Metatheory.Rewriters
begin
@variables x, t, f(x, t)
Dx = Differential(x)
Dt = Differential(t)
rule = @rule Dt(Dx(~g)) => Dx(Dt(~g))
rewriter = Chain([rule])
func = Symbolics.unwrap(Dt(Dx(f)))
rewriter(func)
end
returns the correct reorder Differential(x)(Differential(t)(f(x, t)))
However, inserting the above code into a function
# test_simple_rewrite_main.jl
using Symbolics, Metatheory.Rewriters
function main()
@variables x, t, f(x, t)
Dx = Differential(x)
Dt = Differential(t)
rule = @rule Dt(Dx(~g)) => Dx(Dt(~g))
rewriter = Chain([rule])
func = Symbolics.unwrap(Dt(Dx(f)))
rewriter(func)
end
begin
main()
end
returns the incorrect Differential(t)(Differential(x)(f(x, t)))
.
The differential operators Dt and Dx are not reordered.
As noted by bowenszhu, placing the construction of the differential operators outside of the function
# test_simple_rewrite_main.jl
using Symbolics, Metatheory.Rewriters
@variables x, t, f(x, t)
Dx = Differential(x)
Dt = Differential(t)
function main()
rule = @rule Dt(Dx(~g)) => Dx(Dt(~g))
rewriter = Chain([rule])
func = Symbolics.unwrap(Dt(Dx(f)))
rewriter(func)
end
begin
main()
end
again returns the correct reorder Differential(x)(Differential(t)(f(x, t)))
Julia 1.8.0 Symbolics.jl 4.10.4 Metatheory.jl 1.3.4