# How would you model Rust procedural macros?

In Rust programming language one can write a compiler extension function that works on abstract syntax tree, effectively modifying source code before it gets converted into machine instructions. In other words, macro function has signature

 Abstract Syntax Tree -> Abstract Syntax Tree


Can these be thought of as higher order functions? Usually higher order functions use function composition to produce their output, not source code manipulation.

I wouldn't consider it a higher-order function. A higher-order function takes a function as input, or yields a function as its output. In contrast, it sounds to me like your compiler extension is accepting data as input (the abstract syntax tree) and producing data on its output.

I suppose you could consider the AST a specification of a function, but that seems like a stretch to me.

I'm not sure that what name you attach to it matters much, from a computer science perspective. A rose by any other name, and all that.

• Thank you for your reply. I can see how operations on AST are merely data manipulation. I was looking for formalism capable of capturing the fact that the result of AST manipulation is executable code. A function. Fn->AST-><Modify AST>->AST->Fn which simplifies to Fn->Fn – Andrew Butenko Oct 21 '20 at 6:49
• @AndrewButenko, you can call it a higher-order function if you want, as long as it doesn't cause confusion. – D.W. Oct 21 '20 at 7:00