Data.Vector.Binary与Binary [a]实例重叠 [英] Data.Vector.Binary overlaps Binary [a] instance

查看:105
本文介绍了Data.Vector.Binary与Binary [a]实例重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我需要序列化包含任意数据类型的向量,在这种情况下,该向量是Doubles的列表.为了序列化向量,我导入了Data.Vector.Binary.

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.

在GHCi中加载模块时,会出现以下错误:

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

列表是Vector的实例吗? 我浏览了文档,但找不到此类实例.

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?

我正在使用以下软件包版本:

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"

推荐答案

好的,我想我在这里看到了问题. vector-binary-instances包定义:

Ok, I think I see the problem here. The vector-binary-instances package defines:

instance (Data.Vector.Generic.Base.Vector v a, Binary a) => Binary (v a)

这是非常糟糕的.该定义的意思是对于任何类型'v a',这是有效的Binary实例".这意味着该实例可用于与v a匹配的任何类型.这包括(但不限于)所有列表,所有函子和所有monad.作为演示,ghci报告了以下内容:

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:

Prelude Data.Binary Data.Vector.Binary Data.ByteString.Lazy> :t getChar
getChar :: IO Char
Prelude Data.Binary Data.Vector.Binary Data.ByteString.Lazy> encode getChar
<interactive>:1:0:
    No instance for (Data.Vector.Generic.Base.Vector IO Char)
      arising from a use of `encode' at <interactive>:1:0-13
    Possible fix:
      add an instance declaration for
      (Data.Vector.Generic.Base.Vector IO Char)
    In the expression: encode getChar
    In the definition of `it': it = encode getChar

在这里,解释程序正在尝试将此实例用于getChar :: IO Char,这显然是错误的.

Here the interpreter is attempting to use this instance for getChar :: IO Char, which is obviously wrong.

简短的回答:暂时不要使用vector-binary-instances.该实例已损坏,并且鉴于实例如何通过Haskell代码传播,将导致问题.在解决此问题之前,您应该为向量编写自己的二进制实例.您应该能够从vector-binary-instances复制代码并将其限制为单态向量类型

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

instance (Binary a) => Binary (Vector a) where

我相信这将适用于作为Data.Vector.Generic.Vector实例的任何Vector.

I believe this will work with any Vector which is an instance of Data.Vector.Generic.Vector.

您可能还需要与vector-binary-instances维护者联系.

You also may want to contact the vector-binary-instances maintainer about this.

这篇关于Data.Vector.Binary与Binary [a]实例重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆