[Haskell-cafe] Haskell meta-programming

John Lato jwlato at gmail.com
Wed Dec 21 11:49:19 CET 2011


> From: Heinrich Apfelmus <apfelmus at quantentunnel.de>
>
> * Meta-programming / partial evaluation. When designing a DSL, it is
> often the case that you know how to write an optimizing compiler for
> your DSL because it's usually a first-order language. However, trying to
> squeeze that into GHC rules is hopeless. Having some way of compiling
> code at run-time would solve that. Examples:
> ** Conal Elliott's image description language Pan
> ** Henning Thielemann's synthesizer-llvm

I've been thinking about this, and I suspect that meta-programming in
Haskell may not be that far off.  Suppose you have a Meta monad

    data Meta a = Meta { runMeta :: a}

with the magical property that the compiler will optimize/recompile
expressions of type `Meta a` when they are run via `runMeta`.  That
would provide usable metaprogramming, and I think it would have all
the desired type properties etc.

Of course no compiler currently has that facility, but we could use a
different monad, perhaps something like this:

    data ThMeta a = ThMeta { runThMeta :: Language.Haskell.TH.ExpQ }

now we just need to get the compiler to run an arbitrary TH splice and
check that the types match after `runThMeta` is called.  I think this
is already possible via the GHC api.  It would have the undesirable
property that some expressions could be ill-typed, and this wouldn't
be known until run-time, but it's a start.

That's about as far as I got before I discovered a much more
worked-out version on Oleg's site (with Chung-chieh Shan and Yukiyoshi
Kameyama).  Of course they've tackled a lot of the awkward typing
issues that my simple construct rudely pushes onto the user.

I'm probably showing my naivety here, and I haven't fully digested
their papers yet, but I wouldn't be surprised to see applications
using Haskell metaprogramming within a relatively short time.

John L.



More information about the Haskell-Cafe mailing list