[Haskell-cafe] Re: Template Haskell: exchanging information between 'splicers'

oleg at okmij.org oleg at okmij.org
Sat Nov 27 10:43:37 CET 2010


Jean-Christophe Mincke wrote:

> Does anyone know a clean solution to pass information between 2 executions
> of splicers.
>
> $(splicer ....)  -- first invocation gather and store some data
> ....
> $(splicer ...) -- second one use the data gathered above.

Just the other day I wrote such code; I'm not sure it qualifies as a
`clean solution' as it was meant for a nefarious purpose.

> e2 = runIO $ do
>   varref <- newIORef undefined
>   let put v = runIO $ do
> 		       putStrLn "put"
> 		       writeIORef varref v
> 		       runQ [|$v + 1|]
>   let get v = runIO $ do
> 		       putStrLn "get"
> 		       vold <- readIORef varref
> 		       runQ [|$v + $vold|]
>
>   c1 <- runQ [|\x -> $(put [|x|]) + 1::Int|]
>   c2 <- runQ [|\x -> $(get [|x|]) + 1::Int|]
>   runQ $ [| ($(return c1),$(return c2)) |]
>
> show_code cde = runQ cde >>= putStrLn . pprint
> te2 = show_code e2

One may even try to splice e2 at the top level (in a different
module).

The code does exchange data between two splicers -- one may argue, it
exchanges too much data. 



More information about the Haskell-Cafe mailing list