Why does the MaybeT (State <type>) () ignore state change?
Short version: when I use runMaybeT, and then runStatein a type monad MaybeT (State <type>) (), it looks like no state changes occur even if the result Maybeis equal Just (). Why?
Full version: I am writing a program to solve the Tower of Hanoi. I present the solution as a list of monads Statethat, when sequenced, control the initial state Towers:
data Towers = Towers [Int] [Int] [Int]
deriving (Show)
type Move = State Towers ()
towerMoves :: Int -> Rod -> Rod -> [Move]
towerMoves 1 r1 r2 = [pop r1 >>= push r2]
towerMoves n r1 r2 = topToTemp ++ (towerMoves 1 r1 r2) ++ topToFinal
where
r3 = other r1 r2
topToTemp = towerMoves (n - 1) r1 r3
topToFinal = towerMoves (n - 1) r3 r2
moves = towerMoves 5 First Third
initTowers = Towers [1,2,3,4,5] [] []
main = print $ snd $ runState (sequence_ moves) initTowers
So far, this program has produced the correct output:
Towers [] [] [1,2,3,4,5]
, , , ( ) . - Move, MaybeT, :
verifiedMoves :: [MaybeT (State Towers) ()]
verifiedMoves = map ((>> verify) . return) moves
where
check :: [Int] -> Bool
check [] = True
check [_] = True
check (x:y:ys) = (x < y) && check (y:ys)
verify :: MaybeT (State Towers) ()
verify = do
(Towers xs ys zs) <- lift get
guard (check xs && check ys && check zs)
, main:
main = maybe (putStrLn "violation") (const $ print finalTowers) v
where
(v, finalTowers) = runState (runMaybeT $ sequence_ verifiedMoves) initTowers
, , :
Towers [1,2,3,4,5] [] []
, . , , Move , , "".
, runMaybeT, runState, (Just (), Towers [1,2,3,4,5] [] [])?
, . get put pop push , .
import Control.Monad
import Data.Functor.Identity
import Control.Monad.State
import Control.Monad.Trans.Maybe
import qualified Data.Map as M
data Rod = First | Second | Third
deriving (Show)
other :: Rod -> Rod -> Rod
other First Second = Third
other Second First = Third
other First Third = Second
other Third First = Second
other Second Third = First
other Third Second = First
getRod :: Towers -> Rod -> [Int]
getRod (Towers x y z) First = x
getRod (Towers x y z) Second = y
getRod (Towers x y z) Third = z
setRod :: Rod -> Towers -> [Int] -> Towers
setRod First t ds = Towers ds r2 r3
where
r2 = t `getRod` Second
r3 = t `getRod` Third
setRod Second t ds = Towers r1 ds r3
where
r1 = t `getRod` First
r3 = t `getRod` Third
setRod Third t ds = Towers r1 r2 ds
where
r1 = t `getRod` First
r2 = t `getRod` Second
pop :: Rod -> State Towers Int
pop r = do
t <- get
let ds = t `getRod` r
d = head ds
load = setRod r
put $ t `load` (tail ds)
return d
push :: Rod -> Int -> State Towers ()
push r d = do
t <- get
let ds = t `getRod` r
load = setRod r
put $ t `load` (d:ds)
+4