2
$\begingroup$

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.

$\endgroup$
1
$\begingroup$

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.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – Andrew Butenko Oct 21 at 6:49
  • $\begingroup$ @AndrewButenko, you can call it a higher-order function if you want, as long as it doesn't cause confusion. $\endgroup$ – D.W. Oct 21 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.