]> Git — Sourcephile - haskell/symantic-parser.git/blob - src/Language/Haskell/TH/Show.hs
clean warnings
[haskell/symantic-parser.git] / src / Language / Haskell / TH / Show.hs
1 {-# OPTIONS_GHC -Wno-missing-methods #-} -- For TH.Quasi
2 -- | This module enables to 'showCode'
3 -- without requiring to be in 'IO'.
4 module Language.Haskell.TH.Show where
5
6 import Data.Function (($), (.))
7 import Data.String (String)
8 import Prelude (Integer, error, succ)
9 import Control.Applicative (Applicative(..))
10 import Control.Monad (Monad(..))
11 import Data.Functor (Functor)
12 import qualified Control.Monad as CM
13 import qualified Control.Monad.IO.Class as CM
14 import qualified Control.Monad.Trans.State as MT
15 import qualified Language.Haskell.TH as TH
16 import qualified Language.Haskell.TH.Syntax as TH
17
18 newtype ShowQ a = ShowQ { unShowQ :: MT.State Integer a }
19 deriving (Functor, Applicative, Monad)
20
21 runShowQ :: ShowQ a -> a
22 runShowQ = (`MT.evalState` 0) . unShowQ
23
24 showCode :: TH.CodeQ a -> String
25 showCode q = runShowQ $ do
26 texp <- TH.runQ (TH.examineCode q)
27 return $ TH.pprint $ TH.unType texp
28
29 -- | The whole point of ShowQ is to remove the need for IO,
30 -- but GHC's 'TH.Quasi' class forces it...
31 instance CM.MonadIO ShowQ
32 instance CM.MonadFail ShowQ where
33 fail = error
34 -- | Only 'TH.qNewName' is needed and thus implemented.
35 instance TH.Quasi ShowQ where
36 qNewName n = ShowQ $ do
37 i <- MT.get
38 MT.put (succ i)
39 return (TH.mkNameU n i)