Writing a simple macro for pretty differentiation

Trying to write my first macro and I’m surprised at just how hard it really is.

My goal is simple. I would like to be able to write something like the following


and have it mean

 AD.derivative(z -> f(z,x,y), t)

where AD stands for the AbstractDifferentiation module and z stands for some macro-hygienic anonymous variable.

I’ve not been able to make this syntax work, however. I’ve tried to have @∂(t) translate to some other macro that would then apply the necessary transformations to f, but the way macros are processed has left me deeply confused.

This seems like a pretty simple and obvious use for a macro, right? Can anyone share any guidance on how I could do it?

This won’t work because it parses as two expressions that are multiplied:

julia> :(@∂(t)f(t,x,y))
:(#= REPL[16]:1 =# @∂(t) * f(t, x, y))

You need something after the macro call @∂ that parses as a single expression.

1 Like