koka-lang / koka

Koka language compiler and interpreter
http://koka-lang.org
Other
3.15k stars 151 forks source link

Metaprogramming #536

Open TimWhiting opened 1 month ago

TimWhiting commented 1 month ago

While I understand if this isn't a priority or even something that Daan would like for Koka. I do want to explore the idea.

Metaprogramming solutions span a broad spectrum. You can have metaprograms that transform tokens, ones that transform ASTs, function decorators that wrap functions, or even more introspective metaprograms (such as the macros introduced to Dart recently).

I think the following are particularly worthy of mention: Rackets new metaprogramming solution: Rhombus: A New Spin on Macros without All the Parentheses

Leans metaprogramming solution: Beyond notations: Hygienic macro expansion for theorem proving languages

And Darts solution: (there is a proposal and a few examples there) https://github.com/dart-lang/language/tree/8194951697a090bc23f6a84e92044d368a00bf12/working/macros

All of these solutions are worth looking at, though I think Dart made it more complicated to make it really powerful, I don't necessarily think that is the approach that Koka should take.

Of course you need to think about hygiene for macros. I think ideally that macros should be run an environment where syntactic information get's added using algebraic effects, allowing for creating unique symbols based off a name etc. Pattern based macros are also something that would be very nice. In general I think this should wait until the compiler is written in Koka, so we can experiment and use algebraic effect handlers in a macro system, which is something that I haven't heard of being done before.

kuchta commented 1 month ago

Nim also has a quite nice macro/template system:

TimWhiting commented 1 month ago

Thanks for mentioning that. Nim's is one that I've also thought was nicely designed.

complyue commented 3 weeks ago

until the compiler is written in Koka

I quite anticipate that day, that Koka starts to bootstrap itself, with some ergonomic parser/combinator libs digesting innovative new DSL grammars into some IR form, that Koka understands, type-checks, and compiles.

I feel especially comfortable with Julia, for its single runtime accommodating both compile-time and run-time programs, where you have the full ecosystem in doing compile-time programming, though its AST/macro system seems a LISP under disguise, which I don't like that much. Other meta-programming mechanisms I've ever worked with so far are all way more restrictive, i.e. C++ templates, type-level Haskell, Template Haskell. Though Python as a dynamic PL is much easier to work with, wrt meta-programming, and I also baked my own dynamic/scripting PL for some parts of my target system, there still needs static compilers in critical parts for performance.

I see Koka very promising to be a great infrastructure for performant DSL making, meta-programming with full-fledged ecosystem on algebraic effects shall be wonderful experiences I'd expect.