Data.Vector.Binary 与 Binary [a] 实例重叠
在我的应用程序中,我需要序列化包含任意数据类型的向量,在本例中是双精度列表。为了序列化向量,我导入 Data.Vector.Binary。
在 GHCi 中加载模块时出现以下错误:
Overlapping instances for Binary [Double]
arising from a use of `decode' at Statistics.hs:57:33-42
Matching instances:
instance (Data.Vector.Generic.Base.Vector v a, Binary a) =>
Binary (v a)
-- Defined in Data.Vector.Binary
instance (Binary a) => Binary [a] -- Defined in Data.Binary
该列表是 Vector 的实例吗? 我查看了文档但找不到这样的实例。
我该怎么做才能序列化这个结构?
编辑:
我正在使用以下软件包版本:
- vector-0.6.0.2
- vector-binary-instances-0.1.2
- binary-0.5.0.2
这里还有一个显示问题的代码片段,这次带有字符列表:
import Data.Binary
import Data.Vector.Binary
import qualified Data.ByteString.Lazy as L
main = L.writeFile "/tmp/aaa" $ encode "hello"
In my application I need to serialize a vector containing an arbitrary datatype, in this case is a list of Doubles. For serializing the vector I'm importing Data.Vector.Binary.
When loading the module in GHCi the following error arises:
Overlapping instances for Binary [Double]
arising from a use of `decode' at Statistics.hs:57:33-42
Matching instances:
instance (Data.Vector.Generic.Base.Vector v a, Binary a) =>
Binary (v a)
-- Defined in Data.Vector.Binary
instance (Binary a) => Binary [a] -- Defined in Data.Binary
Is the list an instance of Vector?
I looked through the documentation but could not find such instance.
What can I do to be able to serialize this structure?
Edit:
I'm using the following package versions:
- vector-0.6.0.2
- vector-binary-instances-0.1.2
- binary-0.5.0.2
Also here is a snippet that shows the issue, this time with a list of chars:
import Data.Binary
import Data.Vector.Binary
import qualified Data.ByteString.Lazy as L
main = L.writeFile "/tmp/aaa" $ encode "hello"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我想我看到了问题所在。 vector-binary-instances 包定义:
这是非常糟糕的。这个定义意味着“对于任何类型‘v a’,这是一个有效的 Binary 实例”。这意味着此实例可用于与
v a
匹配的任何类型。这包括(但不限于)所有列表、所有函子和所有单子。作为演示,ghci 报告如下:Here the Interpreter is attempts to use this instance for
getChar :: IO Char
,这显然是错误的。简短的回答:暂时不要使用向量二进制实例。该实例已损坏,考虑到实例如何通过 Haskell 代码传播,它将导致问题。在解决此问题之前,您应该为向量编写自己的二进制实例。您应该能够从向量二进制实例复制代码并将其限制为单态向量类型
我相信这适用于作为 Data.Vector.Generic.Vector 实例的任何向量。
您可能还需要联系向量二进制实例维护者来了解此事。
Ok, I think I see the problem here. The vector-binary-instances package defines:
which is very bad. This definition means "for any type 'v a', this is a valid Binary instance". That means this instance is available for any type that matches
v a
. That includes (but is not limited to) all lists, all functors, and all monads. As a demonstration, ghci reports the following:Here the interpreter is attempting to use this instance for
getChar :: IO Char
, which is obviously wrong.Short answer: don't use vector-binary-instances for now. This instance is broken, and given how instances propagate through Haskell code it will cause problems. Until this is fixed, you should write your own binary instances for vectors. You should be able to copy the code from vector-binary-instances and restrict it to a monomorphic vector type
I believe this will work with any Vector which is an instance of Data.Vector.Generic.Vector.
You also may want to contact the vector-binary-instances maintainer about this.