Haskell 中的软件事务内存:无法将预期类型 STM a0 与实际类型 IO () 匹配

Software Transaction Memory in Haskell: Couldn't match expected type STM a0 with actual type IO ()

我有一个小程序,它定义了一个帐户、一个取款功能并尝试从中取款。但是,它没有编译,并抛出以下错误:

Couldn't match expected type ‘(STM a0 -> IO a0)
                                    -> STM () -> IO ()’
                  with actual type ‘IO ()’

编译器似乎无法识别从 STM 到 IO 的转换。任何指针都会很棒。

import System.IO
import Control.Concurrent.STM

type Account = TVar Int

withdraw :: Account -> Int -> STM ()
withdraw acc amount = do
    bal <- readTVar acc    
    writeTVar acc (bal - amount)

good :: Account -> IO ()
good acc = do
    hPutStr stdout "Withdrawing..."
    {-hi-}atomically{-/hi-} (withdraw acc 10)

main = do
    acc <- atomically (newTVar 200)
    good acc 
    hPutStr stdout "\nDone!\n"

{-hi-}{-/hi-} 评论导致 "indenting" automically,因此您写了 hPutStr stdout "Withdrawing..." atomically (withdraw acc 10)。例如,如果你写:

good :: Account -> IO ()
good acc = do
        hPutStr stdout "Withdrawing..."
 {-hi-} atomically (withdraw acc 10)

它工作正常,因为 "noise"({-hi-} 注释)不会导致内联 atomically 函数。

注释在语义上确实没有影响,但可以考虑用空格代替。