将模块恢复为记录

时间:2012-07-20 01:07:03

标签: haskell module template-haskell ghc-api

假设我有一个任意模块

module Foo where
foo :: Moo -> Goo
bar :: Car -> Far
baz :: Can -> Haz

其中foobarbaz已正确实施,等等。

我想将此模块重新定义为自动生成的数据类型和相应的对象:

import Foo (Moo, Goo, Car, Far, Can, Haz)
import qualified Foo

data FooModule = Foo
  { foo :: Moo -> Goo
  , bar :: Car -> Far
  , baz :: Can -> Haz
  }

_Foo_ = Foo
  { foo = Foo.foo
  , bar = Foo.bar
  , baz = Foo.baz
  }

名称必须与原始模块完全相同。

我可以手动执行此操作,但这非常繁琐,所以我想编写一些代码来为我执行此任务。

我不确定如何处理这样的任务。 Template Haskell是否提供了检查模块的方法?我应该加入一些GHC api吗?或者我还有一个更特别的方法,如刮黑线鳕页?

1 个答案:

答案 0 :(得分:3)

(这适用于GHC-7.4.2;由于Outputable中的某些更改,它可能无法使用HEAD或7.6进行编译)。我没有找到任何东西来检查TH中的模块。

{-# LANGUAGE NoMonomorphismRestriction #-}
{-# OPTIONS -Wall #-}
import GHC
import GHC.Paths -- ghc-paths package
import Outputable
import GhcMonad

main :: IO ()
main = runGhc (Just libdir) $ goModule "Data.Map"

goModule :: GhcMonad m => String -> m ()
goModule modStr = do
  df <- getSessionDynFlags
  _ <- setSessionDynFlags df  
  -- ^ Don't know if this is the correct way, but it works for this purpose

  setContext [IIDecl (simpleImportDecl (mkModuleName modStr))]
  infos <- mapM getInfo =<< getNamesInScope 
  let ids = onlyIDs infos
  liftIO . putStrLn . showSDoc . render $ ids 

onlyIDs :: [Maybe (TyThing, Fixity, [Instance])] -> [Id]
onlyIDs infos = [ i | Just (AnId i, _, _) <- infos ] 

render :: [Id] -> SDoc
render ids = mkFields ids $$ text "------------" $$ mkInits ids 

mkFields :: [Id] -> SDoc
mkFields = vcat . map (\i ->
  text "," <+> pprUnqual i <+> text "::" <+> ppr (idType i))

mkInits :: [Id] -> SDoc
mkInits = vcat . map (\i ->
  text "," <+> pprUnqual i <+> text "=" <+> ppr i)


-- * Helpers

withUnqual :: SDoc -> SDoc
withUnqual  = withPprStyle (mkUserStyle neverQualify AllTheWay)

pprUnqual :: Outputable a => a -> SDoc
pprUnqual = withUnqual . ppr