我有一个简单的函数,一次读取一个字节的二进制文件。它得到一个编译时错误,如下所示。问题似乎是bs2
,得到的 ByteStringBSSC.length
具有未知类型。我错过了类型约束r
吗?
import qualified Data.ByteString.Streaming.Char8 as BSSC
main :: IO ()
main = runResourceT $ dump $ BSSC.readFile "filename"
dump :: (MonadIO m) => BSSC.ByteString m r -> m ()
dump bs = do
len :> bs2 <- BSSC.length bs -- Data.Functor.Of (:>)
if len <= 1 then return ()
else dump $ BSSC.putStr $ BSSC.splitAt 1 bs2
编译时错误:
Main.hs:166:46: error:
• Couldn't match expected type ‘BSSC.ByteString
(BSSC.ByteString m) r0’
with actual type ‘r’
‘r’ is a rigid type variable bound by
the type signature for:
dump :: forall (m :: * -> *) r.
MonadIO m =>
BSSC.ByteString m r -> m ()
at Main.hs:162:9
• In the second argument of ‘BSSC.splitAt’, namely ‘bs2’
In the second argument of ‘($)’, namely ‘BSSC.splitAt 1 bs2’
In the second argument of ‘($)’, namely
‘BSSC.putStr $ BSSC.splitAt 1 bs2’
• Relevant bindings include
bs2 :: r (bound at Main.hs:164:12)
bs :: BSSC.ByteString m r (bound at Main.hs:163:6)
dump :: BSSC.ByteString m r -> m () (bound at Main.hs:163:1)