Why must we use state monad instead of passing state directly?

后端 未结 3 500
有刺的猬
有刺的猬 2021-02-05 23:46

Can someone show a simple example where state monad can be better than passing state directly?

bar1 (Foo x) = Foo (x + 1)

vs

b         


        
3条回答
  •  梦如初夏
    2021-02-06 00:12

    As an example to my comment above, you can write code using the State monad like

    {-# LANGUAGE OverloadedStrings #-}
    {-# LANGUAGE TemplateHaskell #-}
    
    import Data.Text (Text)
    import qualified Data.Text as Text
    import Control.Monad.State
    
    data MyState = MyState
        { _count :: Int
        , _messages :: [Text]
        } deriving (Eq, Show)
    makeLenses ''MyState
    
    type App = State MyState
    
    incrCnt :: App ()
    incrCnt = modify (\my -> my & count +~ 1)
    
    logMsg :: Text -> App ()
    logMsg msg = modify (\my -> my & messages %~ (++ [msg]))
    
    logAndIncr :: Text -> App ()
    logAndIncr msg = do
        incrCnt
        logMsg msg
    
    app :: App ()
    app = do
        logAndIncr "First step"
        logAndIncr "Second step"
        logAndIncr "Third step"
        logAndIncr "Fourth step"
        logAndIncr "Fifth step"
    

    Note that using additional operators from Control.Lens also lets you write incrCnt and logMsg as

    incrCnt = count += 1
    
    logMsg msg = messages %= (++ [msg])
    

    which is another benefit of using State in combination with the lens library, but for the sake of comparison I'm not using them in this example. To write the equivalent code above with just argument passing it would look more like

    incrCnt :: MyState -> MyState
    incrCnt my = my & count +~ 1
    
    logMsg :: MyState -> Text -> MyState
    logMsg my msg = my & messages %~ (++ [msg])
    
    logAndIncr :: MyState -> Text -> MyState
    logAndIncr my msg =
        let incremented = incrCnt my
            logged = logMsg incremented msg
        in logged
    

    At this point it isn't too bad, but once we get to the next step I think you'll see where the code duplication really comes in:

    app :: MyState -> MyState
    app initial =
        let first_step  = logAndIncr initial     "First step"
            second_step = logAndIncr first_step  "Second step"
            third_step  = logAndIncr second_step "Third step"
            fourth_step = logAndIncr third_step  "Fourth step"
            fifth_step  = logAndIncr fourth_step "Fifth step"
        in fifth_step
    

    Another benefit of wrapping this up in a Monad instance is that you can use the full power of Control.Monad and Control.Applicative with it:

    app = mapM_ logAndIncr [
        "First step",
        "Second step",
        "Third step",
        "Fourth step",
        "Fifth step"
        ]
    

    Which allows for much more flexibility when processing values calculated at runtime compared to static values.

    The difference between manual state passing and using the State monad is simply that the State monad is an abstraction over the manual process. It also happens to fit several other widely used more general abstractions, like Monad, Applicative, Functor, and a few others. If you also use the StateT transformer then you can compose these operations with other monads, such as IO. Can you do all of this without State and StateT? Of course you can, and there's no one stopping you from doing so, but the point is that State abstracts this pattern out and gives you access to a huge toolbox of more general tools. Also, a small modification to the types above makes the same functions work in multiple contexts:

    incrCnt :: MonadState MyState m => m ()
    logMsg :: MonadState MyState m => Text -> m ()
    logAndIncr :: MonadState MyState m => Text -> m ()
    

    These will now work with App, or with StateT MyState IO, or any other monad stack with a MonadState implementation. It makes it significantly more reusable than simple argument passing, which is only possible through the abstraction that is StateT.

提交回复
热议问题