我在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 中使用这些共享变量(存储在 ghosts 中):
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 buff 的问题?