[Haskell-cafe] Splicing type signature in TH
Hugo Pacheco
hpacheco at gmail.com
Thu Oct 16 12:52:20 UTC 2014
Hi,
Yes, Michael's hack would do part of the trick, but I forgot to exemplify
part of my question in the example code.
The whole idea is that I would like to be able to splice the generated Type
into a TH quotation. Consider one of the world's most complicated identity
functions:
{-# LANGUAGE TemplateHaskell #-}
import Data.Typeable
import Language.Haskell.Meta.Parse (parseType)
import Language.Haskell.TH
f :: Q Type -> Q Exp
f m = m >>= \t -> SigE (VarE 'id) (FunT t t)
liftT :: Typeable a => a -> Q Type
liftT x = either fail return $ parseType $ show $ typeOf x
-- separate files
splicef :: Typeable a => a -> a
splicef x = $(f (liftT x)) x
This code does not work because liftT depends on the value of x, exposing
TH's state restrictions, but since we only need the type of x to evaluate
liftT, we should be fine.
Thanks,
hugo
On Thu, Oct 16, 2014 at 1:22 AM, Michael Sloan <mgsloan at gmail.com> wrote:
> Here's a hacky solution to this, using haskell-src-meta to parse the type:
>
> {-# LANGUAGE TemplateHaskell #-}
>
> import Data.Typeable
> import Language.Haskell.Meta.Parse (parseType)
> import Language.Haskell.TH
>
> liftT :: Typeable a => a -> Type
> liftT x = either error id $ parseType $ show $ typeOf x
>
> This doesn't handle qualification properly, as the instance of Show
> for TypeRep doesn't qualify names. A proper solution would involve
> directly writing a (TypeRep -> Type) function.
>
> On Wed, Oct 15, 2014 at 9:06 PM, Hugo Pacheco <hpacheco at gmail.com> wrote:
> > Hi list,
> >
> > I am 99.9% sure that this is currently not possible, but I might as well
> > ask:
> >
> > Is there a way to lift type variables into Template Haskell type splices?
> >
> > What I had in mind would be something like this (erroneous) code:
> >
> > liftT :: a -> Q Type
> > liftT (_::a) = [t| $a |]
> >
> > I have no idea how hard it would be to implement such a feature, or if
> it is
> > remotely doable.
> > Naively, it seems to me that TH would have to delay evaluating the splice
> > until the type variable is fully expanded, but all the necessary
> information
> > would still be available at some point during compilation.
> >
> > Cheers,
> > hugo
> >
> > --
> > www.cs.cornell.edu/~hpacheco
> >
> > _______________________________________________
> > Haskell-Cafe mailing list
> > Haskell-Cafe at haskell.org
> > http://www.haskell.org/mailman/listinfo/haskell-cafe
> >
>
--
www.cs.cornell.edu/~hpacheco
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20141016/f19ba5dc/attachment.html>
More information about the Haskell-Cafe
mailing list