[Git][ghc/ghc][master] Add an example to liftIO and explain its purpose
Marge Bot
gitlab at gitlab.haskell.org
Fri Apr 10 03:10:52 UTC 2020
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
045139f4 by Hécate at 2020-04-09T23:10:44-04:00
Add an example to liftIO and explain its purpose
- - - - -
1 changed file:
- libraries/base/Control/Monad/IO/Class.hs
Changes:
=====================================
libraries/base/Control/Monad/IO/Class.hs
=====================================
@@ -30,8 +30,42 @@ module Control.Monad.IO.Class (
class (Monad m) => MonadIO m where
-- | Lift a computation from the 'IO' monad.
+ -- This allows us to run IO computations in any monadic stack, so long as it supports these kinds of operations
+ -- (i.e. 'IO' is the base monad for the stack).
+ --
+ -- === __Example__
+ --
+ --
+ -- > import Control.Monad.Trans.State -- from the "transformers" library
+ -- >
+ -- > printState :: Show s => StateT s IO ()
+ -- > printState = do
+ -- > state <- get
+ -- > liftIO $ print state
+ --
+ --
+ -- Had we omitted @'liftIO'@, we would have ended up with this error:
+ --
+ -- > • Couldn't match type ‘IO’ with ‘StateT s IO’
+ -- > Expected type: StateT s IO ()
+ -- > Actual type: IO ()
+ --
+ -- The important part here is the mismatch between @StateT s IO ()@ and @'IO' ()@.
+ --
+ -- Luckily, we know of a function that takes an @'IO' a@ and returns an @(m a)@: @'liftIO'@,
+ -- enabling us to run the program and see the expected results:
+ --
+ -- @
+ -- > evalStateT printState "hello"
+ -- "hello"
+ --
+ -- > evalStateT printState 3
+ -- 3
+ -- @
+ --
liftIO :: IO a -> m a
-- | @since 4.9.0.0
instance MonadIO IO where
liftIO = id
+
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/045139f40089f288866c1c59c7379be82ecdaf34
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/045139f40089f288866c1c59c7379be82ecdaf34
You're receiving this email because of your account on gitlab.haskell.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20200409/29941fb2/attachment-0001.html>
More information about the ghc-commits
mailing list