我正在尝试为向量编写二进制实例。
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as MV
import Data.Binary
instance (Binary a) => Binary (V.Vector a) where
put = vput
get = vget
实现方式是:先输出长度,再输出所有数据点
vput v = (put (V.length v)) >> (put `V.mapM_` v)
问题出在vget
. 我想使用V.create
(我计划输出非常大的向量,语义看起来非常合适。
这是我的尝试:
vget :: (Binary a) => Get (V.Vector a)
vget = do
size <- get
vec <- liftM V.create $ do
v <- (liftM MV.new) size
forM_ [0..(size-1)] $ \i -> do
val <- get
(liftM3 MV.write) v i val
return v -- This is line 22
return vec
错误是
SerializeVector.hs:22:16:
Couldn't match expected type `forall s. ST s (MV.MVector s a0)'
with actual type `Get a10'
我一直试图通过推理并随机插入liftM
s,但无济于事。