[Haskell-cafe] Template Haskell, information about data constructor types

Neil Mitchell ndmitchell at gmail.com
Mon Jun 4 05:45:30 EDT 2007


Hi Simon,

On 6/4/07, Simon Peyton-Jones <simonpj at microsoft.com> wrote:
> | Where typeApp splits a type to find its constructor, ctorTypes gets
> | the types of the fields, and dataCtors gets the constructors in a data
> | type. Unfortunately reify doesn't seem to work on types. Is it
> | possible to do what I am after?
>
> reify takes a Name, not a Type.

I am passing it a name, since I pattern match on the ConT:

       let (ConT c, cs) = typeApp t
       TyConI dat <- reify c

typeApp follows the AppT's to get a vector apply, so ConT is at the
very left of a chain on AppT.

>  Perhaps you mean that reify doesn't work on type constructors?  (E.g. reify ''Maybe).
> It should -- if you think it doesn't can you concoct a test case and submit it?

I'm not sure if I'm doing something wrong, as I haven't managed to get
it working for any type constructors. A test case:

ghci -fth
Prelude> :m Language.Haskell.TH
Language.Haskell.TH> $( reify (mkName "Maybe") >>= error . show )

Results in:

<interactive>:1:3:
    `Maybe' is not in scope at a reify
    In the expression:
        $[splice]((reify (mkName "Maybe")) >>= (error . show))
    In the definition of `it':
        it = $[splice]((reify (mkName "Maybe")) >>= (error . show))

<interactive>:1:3:
    Exception when trying to run compile-time code:
      user error (IOEnv failure)
      Code: let
              >>= = (>>=) Q $dMonad
              $dMonad = Language.Haskell.TH.Syntax.$f20
              show = show Info $dShow
              $dShow = Language.Haskell.TH.Syntax.$f60
            in
              (>>=) [Info, Exp]
                (reify (mkName "Maybe"))
                ((.) [[Char], Q Exp, Info] (error (Q Exp)) show)
    In the expression:
        $[splice]((reify (mkName "Maybe")) >>= (error . show))
    In the definition of `it':
        it = $[splice]((reify (mkName "Maybe")) >>= (error . show))

The initial error is that Maybe is not in scope at reify. Changing to
"Data.Maybe.Maybe" doesn't help, trying "String" doesn't work,
creating a module and declaring a type in there then putting that
fragment in the file doesn't work.

Thanks

Neil


More information about the Haskell-Cafe mailing list