1 -- | This module enables to 'showCode'
2 -- without requiring to be in 'IO'.
3 module Language.Haskell.TH.Show where
5 import Data.Function (($), (.))
6 import Data.String (String, IsString(..))
7 import Prelude (Integer, error, succ)
8 import Control.Applicative (Applicative(..))
9 import Control.Monad (Monad(..))
10 import Data.Functor (Functor)
11 import qualified Control.Monad as CM
12 import qualified Control.Monad.IO.Class as CM
13 import qualified Control.Monad.Trans.State as MT
14 import qualified Language.Haskell.TH as TH
15 import qualified Language.Haskell.TH.Syntax as TH
17 newtype ShowQ a = ShowQ { unShowQ :: MT.State Integer a }
18 deriving (Functor, Applicative, Monad)
20 runShowQ :: ShowQ a -> a
21 runShowQ = (`MT.evalState` 0) . unShowQ
23 showCode :: TH.CodeQ a -> String
24 showCode q = runShowQ $ do
25 TH.runQ (TH.examineCode q) >>= return . TH.pprint . TH.unType
27 -- | The whole point of ShowQ is to remove the need for IO,
28 -- but GHC's 'TH.Quasi' class forces it...
29 instance CM.MonadIO ShowQ
30 instance CM.MonadFail ShowQ where
32 -- | Only 'TH.qNewName' is needed and thus implemented.
33 instance TH.Quasi ShowQ where
34 qNewName n = ShowQ $ do
37 return (TH.mkNameU n i)