
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.

Hi Ilya, Ilya Portnov wrote:
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.
That would be a good thing to have. But it might be quite hard to implement. For example, I guess you might want to have functions like this one: apply :: Exp (a -> b) -> Exp a -> Exp b This function takes two typed expressions and produces an application. The types ensure that the generated application will typecheck. Cool. But can you do the same thing for lambdas? Lambdas create functions, so the type would be something like the following: lambda :: ... -> Exp (a -> b) But what would you put instead of the ...? I fear that overall, you would have to reimplement Haskell's type system in Haskell's type system. Which sounds like a cool thing to do, but maybe not so easily. Tillmann

Maybe take a look at http://hackage.haskell.org/trac/ghc/blog/Template%20Haskell%20Proposal | -----Original Message----- | From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe- | bounces@haskell.org] On Behalf Of Tillmann Rendel | Sent: 23 May 2012 18:20 | To: Haskell Café | Subject: Re: [Haskell-cafe] Typed TemplateHaskell? | | Hi Ilya, | | Ilya Portnov wrote: | > 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. | | That would be a good thing to have. But it might be quite hard to | implement. For example, I guess you might want to have functions like | this one: | | apply :: Exp (a -> b) -> Exp a -> Exp b | | This function takes two typed expressions and produces an application. | The types ensure that the generated application will typecheck. Cool. | | But can you do the same thing for lambdas? Lambdas create functions, so | the type would be something like the following: | | lambda :: ... -> Exp (a -> b) | | But what would you put instead of the ...? | | I fear that overall, you would have to reimplement Haskell's type system | in Haskell's type system. Which sounds like a cool thing to do, but | maybe not so easily. | | Tillmann | | _______________________________________________ | Haskell-Cafe mailing list | Haskell-Cafe@haskell.org | http://www.haskell.org/mailman/listinfo/haskell-cafe

Its a much simpler thing, but I would like to see a template haskell
library and quasi-quoter that used a monad transformer instead of just Q.
On Thu, May 24, 2012 at 1:47 AM, Simon Peyton-Jones
Maybe take a look at http://hackage.haskell.org/trac/ghc/blog/Template%20Haskell%20Proposal
| -----Original Message----- | From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe- | bounces@haskell.org] On Behalf Of Tillmann Rendel | Sent: 23 May 2012 18:20 | To: Haskell Café | Subject: Re: [Haskell-cafe] Typed TemplateHaskell? | | Hi Ilya, | | Ilya Portnov wrote: | > 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. | | That would be a good thing to have. But it might be quite hard to | implement. For example, I guess you might want to have functions like | this one: | | apply :: Exp (a -> b) -> Exp a -> Exp b | | This function takes two typed expressions and produces an application. | The types ensure that the generated application will typecheck. Cool. | | But can you do the same thing for lambdas? Lambdas create functions, so | the type would be something like the following: | | lambda :: ... -> Exp (a -> b) | | But what would you put instead of the ...? | | I fear that overall, you would have to reimplement Haskell's type system | in Haskell's type system. Which sounds like a cool thing to do, but | maybe not so easily. | | Tillmann | | _______________________________________________ | Haskell-Cafe mailing list | Haskell-Cafe@haskell.org | http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (4)
-
David Fox
-
Ilya Portnov
-
Simon Peyton-Jones
-
Tillmann Rendel