[Haskell] State Monad和狀態(tài)轉(zhuǎn)移過(guò)程

初識(shí)狀態(tài)

先看一個(gè)小游戲,這個(gè)小游戲用字符串來(lái)控制,最后得到總分。
c用來(lái)啟動(dòng)和停止計(jì)分,在啟動(dòng)計(jì)分狀態(tài)下a加一分,在停止計(jì)分狀態(tài)下b減一分。
本例中用abcaaacbbcabbab控制,最終得分為2

module Main where
import Control.Monad.State

-- Example use of State monad
-- Passes a string of dictionary {a,b,c}
-- Game is to produce a number from the string.
-- By default the game is off, a C toggles the
-- game on and off. A 'a' gives +1 and a b gives -1.
-- E.g 
-- 'ab'    = 0
-- 'ca'    = 1
-- 'cabca' = 0
-- State = game is on or off & current score
--       = (Bool, Int)

type GameValue = Int
type GameState = (Bool, Int)
 
playGame :: String -> State GameState GameValue
playGame []     = do
    (_, score) <- get
    return score
 
playGame (x:xs) = do
    (on, score) <- get
    case x of
         'a' | on -> put (on, score + 1)
         'b' | on -> put (on, score - 1)
         'c'      -> put (not on, score)
         _        -> put (on, score)
    playGame xs
 
startState = (False, 0)
 
main = print $ evalState (playGame "abcaaacbbcabbab") startState

定義State Monad

-- 's' is the state, 'a' is the value
newtype State s a = State {
    runState :: s -> (a, s)
}

returnState :: a -> State s a
returnState a = State $ \s -> (a, s)

bindState :: State s a -> (a -> State s b) -> State s b
bindState m k = State $ \s -> runState (k a) s'
    where (a, s') = runState m s

instance Monad (State s) where
    m >>= k = bindState m k
    return a = returnState a

注意其中某些值的類型:

runState :: State s a -> s -> (a, s)
m :: State s a
k :: a -> State s b

還需注意,State s a中的s是類型,而\s -> ...中的s是值。

單步狀態(tài)轉(zhuǎn)移

-- runState (return 'a') 1 = ('a', 1)
return :: a -> State s a
return x = State $ \s -> (x, s)

-- runState get 1 = (1, 1)
get :: State s a
get = State $ \s -> (s, s)

-- runState (put 5) 1 = ((), 5)
put :: a -> State s ()
put x = State $ \s -> ((), x)

-- evalState (return 'a') 1 = 'a'
evalState :: State s a -> s -> a
evalState s = fst . runState s

-- execState (return 'a') 1 = 1
execState :: State s a -> s -> s
execState s = snd . runState s

用do-notation組合多步轉(zhuǎn)移

因?yàn)?code>(State s)是Monad的實(shí)例,
所以,類型State s a的值是一個(gè)monad value(也稱為action
action中包裝了類型為a的值。

do-notation可以把各個(gè)action串聯(lián)起來(lái)。
還可以提取各action中包裝的值,進(jìn)行傳遞。

-- runState (do {put 5; return 'a'}) 1 = ('a', 5)
-- runState (do {x <- get; put (x+1); return x) 1 = (1, 2)
-- runState (do {x <- get; put (x-1); get) 1 = (0, 0)

這里詳細(xì)解釋一下runState (do {put 5; return 'a'}) 1的執(zhí)行過(guò)程,

-- put 5 = State $ \s -> ((), 5)
-- return 'a' = State $ \s -> ('a', s)

-- do {put 5; return 'a'} 
-- = (put 5) >>= (\_ -> (return 'a'))
-- = (State $ \s -> ((), 5)) >>= (\_ -> (State $ \s -> ('a', s)))
-- = bindState (State $ \s -> ((), 5)) (\_ -> (State $ \s -> ('a', s)))
-- = State $ \s -> runState ((\_ -> (State $ \s -> ('a', s))) a) s' where (a, s') = runState (State $ \s -> ((), 5)) s
-- = State $ \s -> ('a', 5)

-- runState (do {put 5; return 'a'}) 1
-- = runState (State $ \s -> ('a', 5)) 1
-- = ('a', 5)

總結(jié)

evalState 狀態(tài)轉(zhuǎn)移過(guò)程 初始狀態(tài),從初始狀態(tài)出發(fā),最終得到一個(gè)結(jié)果值
execState 狀態(tài)轉(zhuǎn)移過(guò)程 初始狀態(tài),從初始狀態(tài)出發(fā),最終得到一個(gè)結(jié)果狀態(tài),
runState 狀態(tài)轉(zhuǎn)移過(guò)程 初始狀態(tài),從初始狀態(tài)出發(fā),最終得到一個(gè)元組(結(jié)果值, 結(jié)果狀態(tài))。

狀態(tài)轉(zhuǎn)移過(guò)程可以是狀態(tài)的單步轉(zhuǎn)移,也可以是用do-notation串聯(lián)起來(lái)的多步轉(zhuǎn)移。
因此,State Monad的做法,可以看做將狀態(tài)轉(zhuǎn)移過(guò)程固化下來(lái),最后再一次性從初始狀態(tài)轉(zhuǎn)移到最終狀態(tài)。

參考:

State Monad - HaskellWiki

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,692評(píng)論 19 139
  • 本文為翻譯,個(gè)人學(xué)習(xí)之用,原地址 程序狀態(tài) 如果你以前有其他語(yǔ)言的編程經(jīng)驗(yàn),你可能寫過(guò)一些函數(shù)或者方法來(lái)控制程序的...
    ParkinWu閱讀 3,252評(píng)論 0 3
  • 一. 增強(qiáng)學(xué)習(xí)簡(jiǎn)介 1.1 什么是增強(qiáng)學(xué)習(xí)? 機(jī)器學(xué)習(xí)的算法可以分為三類:監(jiān)督學(xué)習(xí),非監(jiān)督學(xué)習(xí)和增強(qiáng)學(xué)習(xí)。 增強(qiáng)學(xué)...
    阿阿阿阿毛閱讀 31,701評(píng)論 0 25
  • 今天要扒的還是周冬雨。 希望你們不要審美疲勞。 其實(shí)明星的套路還是深情,我們并不是那么關(guān)心。 好吧,讓我們進(jìn)入主題...
    堂姐珠寶家閱讀 8,244評(píng)論 0 1
  • 對(duì)于這種劇情 一直到十多年后的今天 還是有很多人難以忘懷 我從四月四號(hào)到今天 四月十六號(hào) 刷了一遍 并且還在念念不...
    炫酷杏仙兒閱讀 227評(píng)論 0 0

友情鏈接更多精彩內(nèi)容