[Haskell-cafe] (no subject)

Thomas Hartman thomas.hartman at db.com
Thu Aug 9 12:12:27 EDT 2007


In the following code which uses template haskell, how can I get back the 
macro-expanded code generated from 

$(inferStartState ''MyState)

I *can* recover the macro-expanded code for 

$(cnst 1 "x") 

using a debugging technique bulat describes on his tutorial at 
http://www.haskell.org/bz/th3.htm

You can see what's going on in the function debugTemplates below.

I'm trying to do this actually, to better understand how HAppS deals with 
state. It's a bit opaque now since the example on the tutorial uses TH. I 
think I would understand it better if I had code that didn't depend on TH.

(MyState is from the happs tutorial at 
http://www.haskell.org/haskellwiki/HAppS_tutorial )

thanks!

thomas.

{-# OPTIONS -fglasgow-exts -fth  #-}
module MyState where
import HAppS
import HAppS.Protocols.SimpleHTTP2
 
import Data.Monoid
import Data.Typeable
import Control.Monad.State (get, put)

import Language.Haskell.TH
import Language.Haskell.TH.Syntax
 
data MyState = MySt { appVal :: Int } deriving (Read, Show, Typeable)
 
instance Serialize MyState where
  encodeStringM = defaultEncodeStringM
  decodeStringM = defaultDecodeStringM
 
instance Monoid MyState where
  mempty = MySt 0
  mappend (MySt x) (MySt y) = MySt (x+y)
 
-- in ghci...  -fth, :m +

-- ghci... :t (inferStartState ''MyState) :: (Quasi m) => m [Dec]
$(inferStartState ''MyState) -- boilerplate that will eventually be SYB

-- ghci... :t cnst 1 "x" :: (Monad m) => m Exp
cnst n s = return (LamE (replicate n WildP) (LitE (StringL s)))

dumpSplice splice = runQ splice >>= putStrLn . pprint
debugTemplates = do dumpSplice (cnst 1 "x")
                    dumpSplice (inferStartState ''MyState)

{-
*MyState> debugTemplates
\_ -> "x"
Template Haskell error: Can't do `reify' in the IO monad
*** Exception: user error (Template Haskell failure)
-}



---

This e-mail may contain confidential and/or privileged information. If you 
are not the intended recipient (or have received this e-mail in error) 
please notify the sender immediately and destroy this e-mail. Any 
unauthorized copying, disclosure or distribution of the material in this 
e-mail is strictly forbidden.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070809/cb795cd4/attachment.htm


More information about the Haskell-Cafe mailing list