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

**URL:** <https://discourse.julialang.org/t/symbolics-jl-interfacing-building-an-expression-tree-to-use-custom-rules/85264>\
**Category:** Modelling & Simulations\
**Tags:** question, symbolics, symbolic-utils\
**Created:** [August 3, 2022, 8:08pm UTC](https://discourse.julialang.org/t/symbolics-jl-interfacing-building-an-expression-tree-to-use-custom-rules/85264 "2022-08-03T20:08:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Audrius-St](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/audrius-st/32/24175_2.png) [@Audrius-St](https://discourse.julialang.org/u/Audrius-St)\
**Post date:** [August 3, 2022, 8:08pm UTC](https://discourse.julialang.org/t/symbolics-jl-interfacing-building-an-expression-tree-to-use-custom-rules/85264/1 "2022-08-03T20:08:52Z")

</div>

Hello,

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

```julia
# 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.

end

begin
    main()

end

```

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`?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 3, 2022, 10:45pm UTC](https://discourse.julialang.org/t/symbolics-jl-interfacing-building-an-expression-tree-to-use-custom-rules/85264/2 "2022-08-03T22:45:16Z")

</div>

This is probably best as an issue.

---

<div class="post-metadata">

**Author:** ![Audrius-St](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/audrius-st/32/24175_2.png) [@Audrius-St](https://discourse.julialang.org/u/Audrius-St)\
**Post date:** [August 4, 2022, 12:00am UTC](https://discourse.julialang.org/t/symbolics-jl-interfacing-building-an-expression-tree-to-use-custom-rules/85264/3 "2022-08-04T00:00:29Z")

</div>

Done.

[https://github.com/JuliaSymbolics/Symbolics.jl/issues/687](https://github.com/JuliaSymbolics/Symbolics.jl/issues/687)

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?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [August 4, 2022, 12:35am UTC](https://discourse.julialang.org/t/symbolics-jl-interfacing-building-an-expression-tree-to-use-custom-rules/85264/4 "2022-08-04T00:35:05Z")

</div>

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 😅 I was just pointing you in the right direction to get a quicker response.

---

<div class="post-metadata">

**Author:** ![Audrius-St](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/audrius-st/32/24175_2.png) [@Audrius-St](https://discourse.julialang.org/u/Audrius-St)\
**Post date:** [August 4, 2022, 12:44am UTC](https://discourse.julialang.org/t/symbolics-jl-interfacing-building-an-expression-tree-to-use-custom-rules/85264/5 "2022-08-04T00:44:09Z")

</div>

Thank you.
