Haskell枚举器,奇怪的错误

时间:2011-02-14 14:47:00

标签: haskell io enumerator

我正在试图弄清楚枚举器是如何工作的,因此测试了枚举器库。我有一个在我的桌面计算机上编译的片段,但抱怨No instance for MonadIO。我是关于如何使用枚举器库的,还是我的笔记本电脑出了问题?

iterateetests.hs:29:17:
    No instance for (MonadIO (Iteratee Int IO))
      arising from a use of `enumeratorFile' at iterateetests.hs:29:17-32
    Possible fix:
      add an instance declaration for (MonadIO (Iteratee Int IO))
    In the first argument of `(==<<)', namely `enumeratorFile h'
    In the first argument of `run_', namely
        `(enumeratorFile h ==<< summer)'
    In the expression: run_ (enumeratorFile h ==<< summer)

代码

import Data.Enumerator
import qualified Data.Enumerator.List as EL
import System.IO
import Control.Exception.Base
import Control.Monad.Trans

summer :: (Monad m) => Iteratee Int m Int
summer = do
  m <- EL.head
  case m of
       Nothing -> return 0
       Just i -> do
     rest <- summer
     return (i+rest)

enumeratorFile h (Continue k) = do
  e <- liftIO (hIsEOF h)
  if e
     then k EOF
     else do
       l <- liftIO $ hGetLine h
       k (Chunks [read l]) >>== enumeratorFile h
enumeratorFile _ step = returnI  step

main = do
  bracket
    (openFile "numberlist" ReadMode)
    (hClose)
    (\h -> run_ (enumeratorFile h ==<< summer))

1 个答案:

答案 0 :(得分:2)

尝试更改导入:

import Control.Monad.Trans

import Control.Monad.IO.Class

可能是您安装了旧版本的mtl,因此在Control.Monad.Trans和Data.Enumerator之间有不同的MonadIO类型类。