Help us understand the problem. What is going on with this article?

```newtype ReaderT r m a = ReaderT { runReaderT :: r -> m a }
```

モナドの定義は以下の通りです。

```instance (Monad m) => Monad (ReaderT r m) where
return   = lift . return   -- 右辺のreturn :: r -> m r
m >>= k  = ReaderT \$ \ r -> do
(>>) = (*>)
fail msg = lift (fail msg)
```

# 2. 補助関数 - lift, ask

```-- | Fetch the value of the environment.

liftReaderT m = ReaderT (const m)     -- (const m) :: r -> m a
```

つまり ReaderT r m a　モナドの m に ExceptT を使います。環境変数の値として掛け算か足し算を表す '*' or '+' を渡すようにします。

```import Data.Functor.Identity

--  runState (runExceptT (runReaderT mainCalc '*') ) []
--  runState (runExceptT (runReaderT mainCalc '+') ) [8]
--  runState (runExceptT (runReaderT mainCalc '*') ) [8,7]
--  runState (runExceptT (runReaderT mainCalc '+') ) [8,7..0]

type Stack = [Int]

lift :: Monad m => m a -> ExceptT e m a
lift = ExceptT . liftM Right

calc :: ReaderT Char (ExceptT String (StateT Stack Identity)) Int
calc = do
s <- liftReaderT \$ lift \$ get
case s of
[]  -> liftReaderT \$ throwE "Error: Stack is empty !"
[_] -> liftReaderT \$ throwE "Error: Stack has only one element !"
_  -> do
a <- liftReaderT \$ lift \$ state \$ \(x:xs) -> (x, xs) -- a <- pop
b <- liftReaderT \$ lift \$ state \$ \(x:xs) -> (x, xs) -- b <- pop
case op of
'*' -> return (a*b)
'+' -> return (a+b)
_   -> return 0

mainCalc :: ReaderT Char (ExceptT String (StateT Stack Identity)) Int
mainCalc = do
a <- calc    -- pointA
liftReaderT \$ lift \$ state \$ \xs -> ((), a:xs)       -- push a
return a
```

```*Main> runState (runExceptT (runReaderT mainCalc '*') ) []
(Left "Error: Stack is empty !",[])
*Main> runState (runExceptT (runReaderT mainCalc '+') ) [8]
(Left "Error: Stack has only one element !",[8])
*Main> runState (runExceptT (runReaderT mainCalc '*') ) [8,7]
(Right 56,[56])
*Main> runState (runExceptT (runReaderT mainCalc '+') ) [8,7..0]
(Right 15,[15,6,5,4,3,2,1,0])
*Main> runState (runExceptT (runReaderT mainCalc '*') ) [8,7..0]
(Right 56,[56,6,5,4,3,2,1,0])
```

Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away