module Gen where import Language.Haskell.TH f :: (Q Exp -> Q Exp) -> Q Exp {- f g = [| \x -> let y = $(g [|x|]) in $(g [|y|]) |] -} f g = do x <- newName "x" y <- newName "y" rhs1 <- g (return (VarE x)) let dec = ValD (VarP y) (NormalB rhs1) [] rhs2 <- g (return (VarE y)) return $ LamE [VarP x] (LetE [dec] rhs2) h :: Int -> Int h x = x*x use :: Q Exp use = [| $(f (\x -> [| h $x |])) |] {- *Gen> $use 3 Loading package haskell98-1.0 ... linking ... done. Loading package template-haskell-1.0 ... linking ... done. :1:0: :1:0-3: Splicing expression use ======> \ x[a2wK] -> let y[a2wM] = Gen.h x[a2wK] in Gen.h y[a2wM] In a 'do' expression: it <- $[splice]use 3 :1:0: :1:0-3: Splicing expression use ======> \ x[a2x2] -> let y[a2x4] = Gen.h x[a2x2] in Gen.h y[a2x4] In the definition of `it': it = $[splice]use 3 81 -}