Operations on expressions

The principle which all @force users must keep in mind is this: the goal is to extend Base methods locally without affecting the global method table by type piracy, and to be able to import them into another package to have the same local effect. In order to avoid the type piracy, one must define a new local complementary n-ary method that will fall back on the base method with Any arguments. Then there will be a tiered alternative dispatch layer within the local package scope that redirects to the default Base dispatch generically. This has many advantages, including a 2x-improvement in precompile time (since the precompilation is now tiered), which actually makes a big difference if this technique is applied to a large number of operations (on the order of 50-100 in Reduce).

module ExtendedPackage
+(x...) = Base.:+(x...)
+(x::Symbol,y::Number...) = Expr(:call,:+,x,y...)
end

Then you can use it locally with the @force macro, which automatically forces imports of all exported methods one by one;

module NewScope
using ForceImport
@force using ExtendedPackage
end

You will now have the property

julia> Base.:+ == NewScope.:+
false

where the + in the NewScope is now the extended plus that falls back on Base, which is also different from the + in Main. All it takes is the application of this principle, which has been pioneered with the development of the Reduce.Algebra module (defined in src/args.jl and src/unary.jl).

The main difficulty lies in properly designing the redirection from the tiered method dispatch so that the infix operations work naturally with a variety of syntax.

2 Likes