Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Found some interesting GHC API calls

  • Loading branch information...
commit 84519a325257fdbe49617344ff3b11abfddbf5eb 1 parent 3282092
@jystic authored
Showing with 44 additions and 1 deletion.
  1. +6 −0 examples/A.hs
  2. +2 −0  quick-version.cabal
  3. +36 −1 src/Main.hs
View
6 examples/A.hs
@@ -0,0 +1,6 @@
+module A where
+
+f :: Int -> Int -> Int
+f x y = x + y
+
+data SomeData = NotReally | JustOne Int | JustTwo Int Double
View
2  quick-version.cabal
@@ -19,4 +19,6 @@ executable quickver
build-depends:
base == 4.*
+ , ghc == 7.2.*
+ , ghc-paths == 0.1.*
, QuickCheck == 2.4.*
View
37 src/Main.hs
@@ -1,4 +1,39 @@
module Main (main) where
+import qualified DynFlags as G
+import Data.Maybe (catMaybes)
+import qualified DataCon as G
+import qualified GHC as G
+import GHC.Paths (libdir)
+import qualified Outputable as G
+import qualified TyCon as G
+import qualified Var as G
+
main :: IO ()
-main = putStrLn "Quick Version 0.1"
+main = do
+ res <- example
+ mapM_ (putStrLn . G.showSDoc . G.ppr) res
+
+example :: IO [G.Type]
+example =
+ G.defaultErrorHandler G.defaultLogAction $ do
+ G.runGhc (Just libdir) $ do
+ dflags <- G.getSessionDynFlags
+ G.setSessionDynFlags dflags
+ target <- G.guessTarget "examples/A.hs" Nothing
+ G.setTargets [target]
+ G.load G.LoadAllTargets
+
+ m <- G.findModule (G.mkModuleName "A") Nothing
+ (Just info) <- G.getModuleInfo m
+
+ exports <- mapM (G.lookupName) (G.modInfoExports info)
+
+ return $ map extractType $ catMaybes exports
+
+extractType :: G.TyThing -> G.Type
+extractType (G.AnId var) = G.varType var
+extractType (G.ATyCon tycon) = G.tyConKind tycon
+extractType (G.ADataCon dcon) = G.dataConRepType dcon
+extractType (G.ACoAxiom _) = error "extractType: ACoAxiom"
+extractType (G.AClass _) = error "extractType: AClass"
Please sign in to comment.
Something went wrong with that request. Please try again.