I can do: runIdentity
, runErrorT
and more to unwrap inner monad.
However, What should I do in case of IO (Either String Int)
? How to unwrap it ?
Asked
Active
Viewed 2,688 times
1
-
You don't 'unwrap' this type - you can 'unwrap' things like `Identity` and `ErrorT` because they are simply newtypes for other types. – user2407038 Apr 19 '16 at 20:44
1 Answers
4
You do not unwrap IO a
actions. Instead, you include them in the main
action (which has an IO
type, hence can use such actions) and the compiler ensures that main
is executed.
You may also teach functions that do not understand IO
how to handle IO
; for example, we have:
fmap :: (a -> b) -> IO a -> IO b
(=<<) :: (a -> IO b) -> IO a -> IO b
Thus if you have a function which consumes an Either String Int
, you may use one of the above functions to teach it how to consume an IO (Either String Int)
instead.
For further reading, you may enjoy The IO Monad for People Who Simply Don't Care. (I also like the monad tutorials You Could Have Invented Monads! (And Maybe You Already Have.) and All About Monads, though they are less directly relevant to this question.)

Daniel Wagner
- 145,880
- 9
- 220
- 380
-
-
@HaskellFun You may teach pure values how to be `IO`-like with `return :: a -> IO a`. – Daniel Wagner Apr 19 '16 at 20:52
-
-
1@HaskellFun You'll have to say a bit more than that if you want help. Why didn't it help you? What did you try? What went wrong? – Daniel Wagner Apr 19 '16 at 20:56
-
-
1@HaskellFun As suggested in my previous comment: use `return`, as in `fmap (+1) (return 21)`. – Daniel Wagner Apr 19 '16 at 20:59
-
-
@HaskellFun Yes, `do`-notation is desugared to calls to `(>>=)` and `return`. See [the Report](https://www.haskell.org/onlinereport/haskell2010/haskellch3.html#x8-470003.14) for details, and recall the definition `m1 >> m2 = m1 >>= \_ -> m2`. – Daniel Wagner Apr 19 '16 at 21:04
-
How to use `fmap` ? I know that I unwrap on Right element closed in IO monad ? I mean: `fmap (+11) (IO (Right 12)) = IO (Right 23)` – Apr 19 '16 at 21:11
-
1@HaskellFun I encourage you to open a fresh top-level thread if you have further questions. Also note that `IO` is a type constructor, but there is *no* term constructor named `IO` -- so `IO (Right 12)` is not a good mental model to hold. Do follow through with the proposed further reading; I think it will clear up many of your misconceptions. – Daniel Wagner Apr 19 '16 at 22:27