[Haskell-cafe] Typed TemplateHaskell?

Ilya Portnov portnov at iportnov.ru
Wed May 23 13:45:01 CEST 2012


Hello Haskell world.

One well-known missfeature of TemplateHaskell is it's «untypedness». I 
mean, that if you have say an expression (Exp or Q Exp), you do not know 
it's type (does it represent String or Int or whatever). And GHC does 
not know too. So, one could easily construct bad-typed expressions using 
TH, and GHC will complain only on generated code, not on TH code.

Such a missfeature was a reasoned decision, because adding some typing 
into TH will add much complexity to compiler and to language.

As far as can I see, using features of last GHC one could write typed 
TH library relatively easily, and saving backwards compatibility.

For example, now we have Q monad and Exp type in "template-haskell" 
package. Let's imagine some new package, say "typed-template-haskell", 
with new TQ monad and new polymorphic type Exp :: * -> *. Using last 
GHC's features, one will easily write something like "expr :: Exp 
String", which will mean that "expr" represents a string expression. And 
we will need a new function, say runTQ :: TQ a -> Q a (or some more 
complicated type), which will turn TypedTemplateHaskell's constructs 
into plain TH.

One question will be on quotations and antiquotations: they are 
implemented to work untyped TH constructs, how to implement 
compatibility with new typed TH? We could add some support of them to 
GHC... Or, maybe define a phantom type (say, Any), and some conversion 
functions, say untyped :: Language.Haskell.TH.Syntax.Exp -> 
Language.Haskell.TH.Typed.Syntax.Exp Any.

Main idea is: seems new GHC enables us to implement "typed TH" without 
modifying GHC itself, only by writing a library. Is it so?

Any further thoughts?

With best regards,
Ilya Portnov.



More information about the Haskell-Cafe mailing list