関数でインスタンス化してリストに収集し、値を書き込むために別の関数に与えるhaskellでトランザクション変数を使用しています。
step player ghosts info = do let unblocked = allPaths (source info) (target info) (graph info)
buff <- atomically $ newTVar [[]]
atomically $ put buff unblocked
let create = do return newGhostVal
let ghosts' = zip (map (\g -> newGhostVal) ghosts) ghosts
mapM_ (\g -> forkIO(atomically $ moveGhost buff (fst g) (graph info) (snd g))) ghosts'
putStrLn "launched"
関数 moveGhost でこれらの共有変数 (ゴーストに格納されている) を使用します。
moveGhost buff res graph ghost =
do notBlocked <- get buff
...
writeTVar buff notBlocked'
writeTVar res (Just ghost'')
return()
共有変数 buff と res の両方に同じ戦略を使用していますが、次の使用でエラーが発生しますTvar res
。
Couldn't match expected type `TVar (Maybe Ghost)'
with actual type `STM (TVar (Maybe a0))'
Expected type: [(TVar (Maybe Ghost), Ghost)]
Actual type: [(STM (TVar (Maybe a0)), b0)]
In the second argument of `mapM_', namely ghosts'
In a stmt of a 'do' block:
mapM_
(\ g
-> forkIO
(atomically $ moveGhost buff (fst g) (graph info) (snd g)))
ghosts'
この TVar の何が問題なのか、なぜ TVar 愛好家にとって問題にならないのか誰か知っていますか?