module Language.Haskell.Session.Hint.Eval (
interpret,
interpretTyped,
) where
import Data.Typeable (Typeable)
import qualified Data.Typeable as Typeable
import qualified GHC
import qualified GHC.Exts as Exts
import qualified Language.Haskell.Session.Hint.Util as Util
interpret :: (GHC.GhcMonad m, Typeable a) => String -> m a
interpret expr = interpret' expr wit where
wit :: a
wit = undefined
interpret' :: (GHC.GhcMonad m, Typeable a) => String -> a -> m a
interpret' expr wit = interpretTyped expr typeStr where
typeStr = show $ Typeable.typeOf wit
interpretTyped :: GHC.GhcMonad m => String -> String -> m a
interpretTyped expr typeStr = do
let typedExpr = concat [parens expr, " :: ", typeStr]
exprVal <- GHC.compileExpr typedExpr
return (Exts.unsafeCoerce# exprVal :: a)
parens :: String -> String
parens s = concat ["(let {", foo, " =\n", s, "\n",
" ;} in ", foo, ")"]
where foo = Util.safeBndFor s