]> Git — Sourcephile - haskell/symantic-parser.git/blob - src/Language/Haskell/TH/Show.hs
rename Symantic.{Univariant => Typed}
[haskell/symantic-parser.git] / src / Language / Haskell / TH / Show.hs
1 -- | This module enables to 'showCode'
2 -- without requiring to be in 'IO'.
3 module Language.Haskell.TH.Show where
4
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
16
17 newtype ShowQ a = ShowQ { unShowQ :: MT.State Integer a }
18 deriving (Functor, Applicative, Monad)
19
20 runShowQ :: ShowQ a -> a
21 runShowQ = (`MT.evalState` 0) . unShowQ
22
23 showCode :: TH.CodeQ a -> String
24 showCode q = runShowQ $ do
25 TH.runQ (TH.examineCode q) >>= return . TH.pprint . TH.unType
26
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
31 fail = error
32 -- | Only 'TH.qNewName' is needed and thus implemented.
33 instance TH.Quasi ShowQ where
34 qNewName n = ShowQ $ do
35 i <- MT.get
36 MT.put (succ i)
37 return (TH.mkNameU n i)