On Saturday, October 11, 2014 4:49:50 PM UTC+2, Erik Schnetter wrote:
>
>
> No, they only know the types with which the function is called. In
> general, the types of new expressions that are generated cannot be
> known until they are evaluated.
>
I'm wondering about some construction like this:
immutable TypedExpr{T}
args::Array{Union(TypedExpr, Symbol)}
noncommuting::Set{Symbol}
end
function TypedExpr(sym::Symbol, args::Union(Symbol,TypedExpr)...;
noncommuting=Set{Symbol}())
TypedExpr{sym}([args...], noncommuting)
end
I'm not that sure if this is a correct usage of parametric types, but this
seems to work.
One could construct the expression TypedExpr(:*, :x, :y, :z,
noncommuting=Set{Symbol}([:y])), the noncommuting symbol gets stored into
it. This replaces Expr(:call, :*, :x, :y, :z)
Mathematical simplification at this point can be handled by multiple
dispatch, for example, the conglomeration
- x+x ===> 2*x
- x*x ===> x^2
could be handled by
- glomm(expr::TypedExpr{:+}) = ... additive conglomeration ...
- glomm(expr::TypedExpr{:*}) = ... multiplicative conglomeration ...