[Haskell-cafe] Metaprogramming in Haskell vs. Ocaml

Jacques Carette carette at mcmaster.ca
Mon Apr 5 19:48:16 EDT 2010

Jason Dagit wrote:
>     While true, there are also 2 other reasons for meta-programmers
>     are not all over Haskell:
>     1. efficiency nuts are already using C++ templates and don't see
>     why they would switch,
>     2. people who care about types use a typed meta-language (like
>     metaocaml) instead of an untyped template layer atop a
>     (fantastic!) typed language. 
> Are you implying that template haskell is not typed?
Indeed.  A template haskell meta-program is not typed at 'compile time'
of the meta-program, but at 'run-time' of the splices.  In other words,
you can't typecheck a TH program without running it.  You do get _some_
typing (because of the quotes have type Q Exp, Q [Dec] or Q Typ, but
that is rather minimalistic.

Compare with metaocaml where if you can compile you meta-program (i.e.
code generator), then you are guaranteed that it can only ever produce
valid, well-typed code.  Not so with TH, where you can easily generate
junk -- which GHC will promptly figure out  and give you an error.  But
only 'late' in the process, which means that it can be fairly difficult
to understand what the cause of the error really is.  It's not nearly as
bad as C++ templates, but it's well short of what metaocaml can do.

Don't get me wrong - as a programming language, Haskell is still my
favourite.  It's just as a meta-programming language that it's not. 
[Unfortunately, I'm writing a lot of meta-programs these days !]


More information about the Haskell-Cafe mailing list