Haskell中的软件事务内存:无法';t将预期类型STM a0与实际类型IO()匹配
我有一个小程序,它定义了一个帐户、一个取款函数并试图从中取款。但是,它不会编译due并抛出以下错误:Haskell中的软件事务内存:无法';t将预期类型STM a0与实际类型IO()匹配,haskell,stm,Haskell,Stm,我有一个小程序,它定义了一个帐户、一个取款函数并试图从中取款。但是,它不会编译due并抛出以下错误: Couldn't match expected type ‘(STM a0 -> IO a0) -> STM () -> IO ()’ with actual type ‘IO ()’ 编译器似乎无法识别从STM到IO的转换。任何指点都很好 import System.
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"
import System.IO
导入控制.Concurrent.STM
类型帐户=TVar Int
取款::账户->内部->STM()
提取acc金额=do
bal IO()
好的acc=do
hPutStr标准输出“正在撤消…”
{-hi-}原子{-/hi-}(撤回acc 10)
main=do
acc注释{-hi-}
和{-/hi-}
导致自动缩进,因此,您编写了hPutStr stdout“撤回…”自动(撤回acc 10)
。例如,如果您编写:
good :: Account -> IO ()
good acc = do
hPutStr stdout "Withdrawing..."
{-hi-} atomically (withdraw acc 10)
它工作得很好,因为“noise”(noise)({-hi-}
注释)不会导致以原子方式内联函数
注释实际上没有语义上的影响,但可以考虑用空白替换。< /P>可以发布完整的回溯吗?(现在很难找到错误的位置)看起来,由于您的
{-hi-}
s等原因,您以原子方式内联了。我从未意识到注释会干扰缩进,主要是因为在同一行代码之前看到注释是很少见的。