X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=HsTools.hs;h=0f3e463040a02777dbb068cba06c4769efc9eedd;hb=ce7380ad772e2a81c0329c6ee495e18fa0a62280;hp=a058c84e1e065468d7065349ec1091457407e426;hpb=7351c9c23660a0217c42483e10ac9b7a25cb49b2;p=matthijs%2Fmaster-project%2Fc%CE%BBash.git diff --git a/HsTools.hs b/HsTools.hs index a058c84..0f3e463 100644 --- a/HsTools.hs +++ b/HsTools.hs @@ -66,8 +66,12 @@ import CoreShow -- (==) = Prelude.(==) Int $dInt -- in -- \x = (==) x 1 -toCore :: HsSyn.HsExpr RdrName.RdrName -> GHC.Ghc CoreSyn.CoreExpr -toCore expr = do +toCore :: + [Module.ModuleName] -- ^ The modules that need to be imported before translating + -- this expression. + -> HsSyn.HsExpr RdrName.RdrName -- ^ The expression to translate to Core. + -> GHC.Ghc CoreSyn.CoreExpr -- ^ The resulting core expression. +toCore modules expr = do env <- GHC.getSession let icontext = HscTypes.hsc_IC env @@ -75,6 +79,7 @@ toCore expr = do -- Translage the TcRn (typecheck-rename) monad into an IO monad TcRnMonad.initTcPrintErrors env PrelNames.iNTERACTIVE $ do (tc_expr, insts) <- TcRnMonad.getLIE $ do + mapM importModule modules -- Rename the expression, resulting in a HsExpr Name (rn_expr, freevars) <- RnExpr.rnExpr expr -- Typecheck the expression, resulting in a HsExpr Id and a list of @@ -121,14 +126,16 @@ mkId rdr_name = do -- | Translate a core Type to an HsType. Far from complete so far. coreToHsType :: Type.Type -> HsTypes.LHsType RdrName.RdrName -- Translate TyConApps -coreToHsType (Type.splitTyConApp_maybe -> Just (tycon, tys)) = - foldl (\t a -> SrcLoc.noLoc $ HsTypes.HsAppTy t a) tycon_ty (map coreToHsType tys) - where - tycon_name = TyCon.tyConName tycon - mod_name = Module.moduleName $ Name.nameModule tycon_name - occ_name = Name.nameOccName tycon_name - tycon_rdrname = RdrName.mkRdrQual mod_name occ_name - tycon_ty = SrcLoc.noLoc $ HsTypes.HsTyVar tycon_rdrname +coreToHsType ty = case Type.splitTyConApp_maybe ty of + Just (tycon, tys) -> + foldl (\t a -> SrcLoc.noLoc $ HsTypes.HsAppTy t a) tycon_ty (map coreToHsType tys) + where + tycon_name = TyCon.tyConName tycon + mod_name = Module.moduleName $ Name.nameModule tycon_name + occ_name = Name.nameOccName tycon_name + tycon_rdrname = RdrName.mkRdrQual mod_name occ_name + tycon_ty = SrcLoc.noLoc $ HsTypes.HsTyVar tycon_rdrname + Nothing -> error $ "HsTools.coreToHsType Cannot translate non-tycon type" -- | Evaluate a CoreExpr and return its value. For this to work, the caller -- should already know the result type for sure, since the result value is @@ -148,22 +155,6 @@ execCore expr = do let res = Unsafe.Coerce.unsafeCoerce hval :: Int return $ Unsafe.Coerce.unsafeCoerce hval --- | Evaluate a core Type representing type level int from the TypeLevel --- library to a real int. -eval_type_level_int :: Type.Type -> Int -eval_type_level_int ty = - unsafeRunGhc $ do - -- Automatically import modules for any fully qualified identifiers - setDynFlag DynFlags.Opt_ImplicitImportQualified - - let to_int_name = mkRdrName "Data.TypeLevel.Num.Sets" "toInt" - let to_int = SrcLoc.noLoc $ HsExpr.HsVar to_int_name - let undef = hsTypedUndef $ coreToHsType ty - let app = HsExpr.HsApp (to_int) (undef) - - core <- toCore app - execCore core - -- These functions build (parts of) a LHSExpr RdrName. -- | A reference to the Prelude.undefined function.