繁体   English   中英

在GHC 7.8中具有角色的代码损坏

[英]Broken code with roles in GHC 7.8

我的一些代码被最新版本的ghc 7.8.2打破了。

我正在使用GeneralizedNewtypeDeriving使用以下方法派生Data.Vector.Unbox实例:

data VoxelPos     = VoxelPos
                    {-# UNPACK #-} !Int
                    {-# UNPACK #-} !Int
                    {-# UNPACK #-} !Int
                  deriving (Show, Eq, Ord)

newtype FacePos = FacePos VoxelPos deriving ( Eq, Hashable, NFData, G.Vector U.Vector, M.MVector U.MVector, U.Unbox)

其中VoxelPos使用(Int, Int, Int)手动滚动实例:

newtype instance U.MVector s VoxelPos = MV_VoxelPos (U.MVector s (Int, Int, Int))
newtype instance U.Vector    VoxelPos = V_VoxelPos  (U.Vector    (Int, Int, Int))
instance U.Unbox VoxelPos
instance M.MVector U.MVector VoxelPos where
  basicLength (MV_VoxelPos v) ...
  ...

这与之前版本的ghc一起使用。 但升级ghc后,我收到以下错误:

Could not coerce from ‘U.MVector s (Int, Int, Int)’ to ‘U.MVector
                                                              s FacePos’
      because the second type argument of ‘U.MVector’ has role Nominal,
      but the arguments ‘(Int, Int, Int)’ and ‘FacePos’ differ
      arising from the coercion of the method ‘M.basicLength’ from type
                   ‘forall s. U.MVector s VoxelPos -> Int’ to type
                   ‘forall s. U.MVector s FacePos -> Int’
    Possible fix:
      use a standalone 'deriving instance' declaration,
        so you can specify the instance context yourself
    When deriving the instance for (M.MVector U.MVector FacePos)

我认为,这是因为增加了角色。 我知道使用GeneralizedNewtypeDeriving时角色可以提高安全性,当然,这非常好!

有哪些解决方案可以解决这个问题? 什么是最推荐的?

这里有一个错误是明智的-它有可能是U.MVector例如FacePos是完全无关的实例VoxelPos 但是有一种很好的解决方法:

newtype instance U.MVector s FacePos = MV_FacePos (U.MVector s VoxelPos)

这应该摆脱你所看到的特定错误。

但是,我认为你会立即遇到另一个与角色相关的错误,因为其他函数(不是basicLength ,你被basicLength地方)以角色目前无法处理的方式使用MVector参数。

GHC团队意识到了这个问题并正在努力:请参阅https://ghc.haskell.org/trac/ghc/ticket/9112https://ghc.haskell.org/trac/ghc/ticket/9123

与此同时,我担心我的唯一建议是使用unsafeCoerce :(

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM