hmac / kite

A Haskell-like language for scripting and web apps
BSD 3-Clause "New" or "Revised" License
12 stars 0 forks source link

Staged metaprogramming #9

Open hmac opened 1 year ago

hmac commented 1 year ago

OCaml and Scala both have staged metaprogramming libraries, which provide a powerful way to generate code at compile or runtime. A description of the OCaml approach, called BER MetaOCaml, is here.

The basic idea is that we have a type Code : Type -> Type which represents program fragments. For example, Code Int is a program which, when run, will evaluate to a value of type Int. We have two basic operations to work with Code values: quoting and splicing.

Quoting constructs a Code value. 3 is a value of type int, whereas <3> is a value of type Code Int. Within a quote (and only within a quote) we can splice a value. For example, consider

z1 : Code Int
z1 = let x = 3
        y = 2
     in < x + y >

When evaluated, z1 will execute x + y and yield the result. Since those values are hardcoded, we can do the addition up-front. To do this we splice the result into the quote:

z2 : Code Int
z2 = let x = 3
         y = 2
     in < ~(x + y) >

z2 will then be a code fragment containing just the literal value 5.

We can write any arbitrary (pure) Kite expression inside quotes. MetaOCaml supports effects but we don't have to (maybe in future). There are lots of applications but the main ones I'm interested in are:

A key requirement for staged metaprogramming is to have a compiler for Kite code that can be executed in Kite. This probably means we need to write a compiler for Kite in Kite. I think this is another point in favour of slimming down the language as much as possible.

hmac commented 1 year ago

Moebius: Metaprogramming using contextual types