-pprintClosureCommand :: Bool -> Bool -> String -> GHCi ()
-pprintClosureCommand bindThings force str = do
- cms <- getSession
- newvarsNames <- io$ do
- uniques <- liftM uniqsFromSupply (mkSplitUniqSupply 'q')
- return$ map (\u-> (mkSysTvName u (mkFastString "a"))) uniques
- mb_ids <- io$ mapM (cleanUp cms newvarsNames) (words str)
- mb_new_ids <- mapM (io . go cms) (catMaybes mb_ids)
- io$ updateIds cms (catMaybes mb_new_ids)
- where
- -- Find the Id
- cleanUp :: Session -> [Name] -> String -> IO (Maybe Id)
- cleanUp cms newNames str = do
- tythings <- GHC.parseName cms str >>= mapM (GHC.lookupName cms)
- return$ listToMaybe [ i | Just (AnId i) <- tythings]
-
- -- Do the obtainTerm--bindSuspensions-refineIdType dance
- -- Warning! This function got a good deal of side-effects
- go :: Session -> Id -> IO (Maybe Id)
- go cms id = do
- mb_term <- obtainTerm cms force id
- maybe (return Nothing) `flip` mb_term $ \term -> do
- term' <- if not bindThings then return term
- else bindSuspensions cms term
- showterm <- printTerm cms term'
- unqual <- GHC.getPrintUnqual cms
- let showSDocForUserOneLine unqual doc =
- showDocWith LeftMode (doc (mkErrStyle unqual))
- (putStrLn . showSDocForUserOneLine unqual) (ppr id <+> char '=' <+> showterm)
+pprintClosureCommand :: GhcMonad m => Bool -> Bool -> String -> m ()
+pprintClosureCommand bindThings force str = do
+ tythings <- (catMaybes . concat) `liftM`
+ mapM (\w -> GHC.parseName w >>=
+ mapM GHC.lookupName)
+ (words str)
+ let ids = [id | AnId id <- tythings]
+
+ -- Obtain the terms and the recovered type information
+ (subst, terms) <- mapAccumLM go emptyTvSubst ids
+
+ -- Apply the substitutions obtained after recovering the types
+ modifySession $ \hsc_env ->
+ hsc_env{hsc_IC = substInteractiveContext (hsc_IC hsc_env) subst}
+
+ -- Finally, print the Terms
+ unqual <- GHC.getPrintUnqual
+ docterms <- mapM showTerm terms
+ liftIO $ (printForUser stdout unqual . vcat)
+ (zipWith (\id docterm -> ppr id <+> char '=' <+> docterm)
+ ids
+ docterms)
+ where
+ -- Do the obtainTerm--bindSuspensions-computeSubstitution dance
+ go :: GhcMonad m => TvSubst -> Id -> m (TvSubst, Term)
+ go subst id = do
+ let id' = id `setIdType` substTy subst (idType id)
+ term_ <- GHC.obtainTermFromId maxBound force id'
+ term <- tidyTermTyVars term_
+ term' <- if bindThings &&
+ False == isUnliftedTypeKind (termType term)
+ then bindSuspensions term
+ else return term