Hi, I am implementing genetic programming (GP) in julia. GP needs to evaluate mathematical expressions represented by tree structures, and this is the bottleneck in my implementation.

The following code can evaluate tree structures, but the type is not stable.

```
abstract type Primitive end
abstract type Terminal <: Primitive end
struct PrimitiveFunction <: Primitive
func::Function
arity::Int
end
struct Variable <: Terminal
index::Int
end
struct Const{T <: Real} <: Terminal
value::T
end
struct GPNode{T <: Primitive}
primitive::T
children::Vector{GPNode}
end
GPNode(prim::Terminal) = GPNode(prim, GPNode[])
function evaluate(node::GPNode{PrimitiveFunction}, vars)
args = evaluate.(node.children, Ref(vars))
node.primitive.func(args...)
end
evaluate(node::GPNode{Const{T}}, vars) where T <: Real = node.primitive.value
evaluate(node::GPNode{Variable}, vars) = vars[node.primitive.index]
x = GPNode(Variable(1))
c = GPNode(Const(1.0))
root = GPNode(PrimitiveFunction(+, 2), [x, c])
vars = [2.0]
@code_warntype evaluate(root, vars)
```

And this is the result of `@code_warntype`

.

```
Variables
#self#::Core.Compiler.Const(evaluate, false)
node::GPNode{PrimitiveFunction}
vars::Array{Float64,1}
args::Any
Body::Any
1 ─ %1 = Base.getproperty(node, :children)::Array{GPNode,1}
│ %2 = Main.Ref(vars)::Base.RefValue{Array{Float64,1}}
│ %3 = Base.broadcasted(Main.evaluate, %1, %2)::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1},Nothing,typeof(evaluate),Tuple{Array{GPNode,1},Base.RefValue{Array{Float64,1}}}}
│ (args = Base.materialize(%3))
│ %5 = Base.getproperty(node, :primitive)::PrimitiveFunction
│ %6 = Base.getproperty(%5, :func)::Function
│ %7 = Core._apply_iterate(Base.iterate, %6, args)::Any
└── return %7
```

I think this instability is slowing down the execution. How can I get rid of this instability?