Symbolics.jl | Interfacing - building an expression tree to use custom rules


I would like to use the SymbolicUtils composing rewriter Fixpoint(rw) which

returns a rewriter which applies rw repeatedly until there are no changes to be made

to apply a custom rule to a Symbolics.jl expression until nothing is returned.

However, rereading the SymbolicUtils.jl and TermInterface.jl documentation it is not clear to me how to do this - specifically how to set up the interface between Symbolics.jl and SymbolicUtils.jl.

The MNWE is

# test_symbolics_main.jl

using Symbolics

function main()

    @variables x, t
    @variables Φ(x, t)

    Dx = Differential(x)
    D2x = Dx * Dx

    Dt = Differential(t)

    # The rule that I would like to apply
    r = @rule Dt(Dx(~Φ)) => Dx(Dt(~Φ))

    # The test expression to which I would like to apply the rule
    ∂2Φ∂t2 = Dt(Dx(Dx(Φ))) - 2*Dt(Φ)*Φ

    # The rewrite I would like to perform
    γ2 = Fixpoint(r)(∂2Φ∂t2)

    # The problem
    @show istree(∂2Φ∂t2) # returns "false" . Not an expression tree.




The SymbolicUtils.jl documentation states

In particular, you should define methods from TermInterface.jl for an expression tree type T with symbol types S to work with SymbolicUtils.jl

and the TermInterface.jl documentation states

You should define the following methods for an expression tree type T with symbol types S to work with TermInterface.jl . . .

istree(x::T) or istree(x::Type{T})

Without an example, it is not clear to me, a Symbolics neophyte, what is meant and how to proceed.

In the above MNWE,
should @variables be replaced by @syms?
should ∂2Φ∂t2 be defined as ∂2Φ∂t2::Symbolics.Term?

This is probably best as an issue.


Is this an issue that can be addressed with the current release versions of
Symbolics.jl / SymbolicUtils.jl / TermInterface.jl or would it require additional deveopment work?

Not sure. I can’t say I looked too closely, but this seems more like a @shashi question and I know he doesn’t read this as often as the issues list so :sweat_smile: I was just pointing you in the right direction to get a quicker response.

Thank you.