[Template-haskell] More useful ones

Sean Seefried sseefried at cse.unsw.edu.au
Sat Sep 27 11:25:47 EDT 2003

Often when analysing expressions I want to get all the arguments for a 
function. The following function decomposes such an application chain.

-- Should only be used on expression of form (App e1 e2)
getAppChain :: Exp -> (Exp, [Exp])
getAppChain (AppE e1 e2) = let (f, args) = getAppChain e1
                            in  (f, args ++ [e2])
getAppChain e = (e, [])

-- The dual of getAppChain
mkAppChain :: (Exp, [Exp]) -> Exp
mkAppChain (f, args) = foldl1 AppE (f:args)

putExpQLn :: ExpQ -> IO ()
putExpQLn expQ= do exp <- runQ expQ
		   putStrLn (show exp)

-- Two very useful functions.
-- And these are just good for printing out code.

putQ :: Show a => Q a -> IO ()
putQ xQ = do x <- runQ xQ
              putStr (show x)

putQLn :: Show a => Q a -> IO ()
putQLn xQ = do putQ xQ
	       putStrLn ""

More information about the template-haskell mailing list