haskell 可能的回归?使用ghc 9.6.2、hmatrix 0.20.2和Vector www.example.com编译时出现类型错误0.13.0.0

pzfprimi  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(63)

使用ghc 9.6.2、hmatrix 0.20.2和Vector 0.13.0.0编译以下代码

import Numeric.LinearAlgebra

import qualified Data.Vector.Storable as V

nnz :: Vector R -> Int
nnz v = size $ V.filter (/= 0) v

avg :: Vector R -> R
avg v = V.sum v / fromIntegral (size v)

获取以下错误:

ghc hvBug.hs
Loaded package environment from /Users/gcolpitts/.ghc/x86_64-darwin-9.6.2/environments/default
[1 of 2] Compiling Main             ( hvBug.hs, hvBug.o )

hvBug.hs:6:32: error: [GHC-83865]
    • Couldn't match expected type: V.Vector t0
                  with actual type: Vector R
      NB: ‘V.Vector’
            is defined in ‘Data.Vector.Storable’ in package ‘vector-0.13.0.0’
          ‘Vector’
            is defined in ‘Data.Vector.Storable’ in package ‘vector-0.13.0.0’
    • In the second argument of ‘V.filter’, namely ‘v’
      In the second argument of ‘($)’, namely ‘V.filter (/= 0) v’
      In the expression: size $ V.filter (/= 0) v
  |
6 | nnz v = size $ V.filter (/= 0) v
  |                                ^

hvBug.hs:9:15: error: [GHC-83865]
    • Couldn't match expected type: V.Vector R
                  with actual type: Vector R
      NB: ‘V.Vector’
            is defined in ‘Data.Vector.Storable’ in package ‘vector-0.13.0.0’
          ‘Vector’
            is defined in ‘Data.Vector.Storable’ in package ‘vector-0.13.0.0’
    • In the first argument of ‘V.sum’, namely ‘v’
      In the first argument of ‘(/)’, namely ‘V.sum v’
      In the expression: V.sum v / fromIntegral (size v)
  |
9 | avg v = V.sum v / fromIntegral (size v)
  |               ^

请注意,https://dis.um.es/~alberto/hmatrix/hmatrixtut.html#types说:
我们还可以导入限定的Data.Storable.Vector作为V,以使用vector包中的任何所需函数。
我相信这段代码用于编译,因此它可能是ghc 9.6.2,hmatrix 0.20.2或Vector中的回归0.13.0.0
https://hackage.haskell.org/package/vector-0.13.0.0/changelog中,我看到:
这种选择的一个后果是,如果a和b名义上是不同的但表示上是相等的类型,则不再可能在Storable.Vector a和Storable.Vector b之间进行强制。我们现在提供了unsafeCoordinate {M}Vector和unsafeCast函数来实现这一点(用户有责任确保在使用这些函数时没有破坏可存储的不变量)。
这是否可能是错误的原因?如果是这样,我如何修改代码,使其编译?

gcmastyq

gcmastyq1#

我能够使用与您相同的GHC,hmatrix和vector包使用Stack nightly-2023-09-09编译您的示例,所以我相信这是全局安装的hmatrixvector包的问题。
一个线索是错误消息的这一部分:

• Couldn't match expected type: V.Vector t0
              with actual type: Vector R
  NB: ‘V.Vector’
        is defined in ‘Data.Vector.Storable’ in package ‘vector-0.13.0.0’
      ‘Vector’
        is defined in ‘Data.Vector.Storable’ in package ‘vector-0.13.0.0’

对于GHC来说,用两个不同的限定名或者一个限定名加一个非限定名来引用同一个包中的同一个标识符是不正常的,就像这里发生的那样。
可能发生的情况是,您的hmatrix包是针对vector-0.13.0.0的一个构建版本构建和安装的,然后vector-0.13.0.0被重新构建。即使这些包具有相同的名称和版本,它们也不会被识别为同一个包,并且从Data.Vector.Storable导入的限定名称V.Vector和从Numeric.LinearAlgebra导入的非限定名称Vector不会被识别为引用相同的类型。
假设你正在尝试使用Cabal来全局安装软件包,我建议你运行:

cabal install --reinstall --lib vector-0.13.0.0
cabal install --reinstall --lib hmatrix-0.20.2

然后尝试重新编译程序。
(其实,我有点担心这不管用。在我的测试中,cabal似乎忽略了--reinstall标志。

相关问题