简体   繁体   English

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

[英]Broken code with roles in GHC 7.8

Some of my code is broken with the newest version of ghc 7.8.2. 我的一些代码被最新版本的ghc 7.8.2打破了。

I'm using GeneralizedNewtypeDeriving to deriving instances of Data.Vector.Unbox using the following: 我正在使用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)

where VoxelPos have manual rolled instances using (Int, Int, Int) : 其中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) ...
  ...

and this was working with the previous versions of ghc. 这与之前版本的ghc一起使用。 But after upgrading ghc, I get the following error: 但升级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)

which, I think, is because of addition of roles. 我认为,这是因为增加了角色。 I know that roles improves safety when using GeneralizedNewtypeDeriving which is, of course, really good! 我知道使用GeneralizedNewtypeDeriving时角色可以提高安全性,当然,这非常好!

What are the possible solutions to solve this? 有哪些解决方案可以解决这个问题? And what is the most recommended one? 什么是最推荐的?

Having an error here is sensible -- it's possible that the U.MVector instance for FacePos is utterly unrelated to the instance for VoxelPos . 这里有一个错误是明智的-它有可能是U.MVector例如FacePos是完全无关的实例VoxelPos There's a nice way to fix this, though: 但是有一种很好的解决方法:

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

That should get rid of the particular error you're seeing. 这应该摆脱你所看到的特定错误。

However, I think you'll hit another role-related error right away, because other functions (not basicLength , where you're snagged) use the MVector parameters in a way roles can't currently deal with. 但是,我认为你会立即遇到另一个与角色相关的错误,因为其他函数(不是basicLength ,你被basicLength地方)以角色目前无法处理的方式使用MVector参数。

The GHC team is aware of this problem and is working on it: see https://ghc.haskell.org/trac/ghc/ticket/9112 and https://ghc.haskell.org/trac/ghc/ticket/9123 GHC团队意识到了这个问题并正在努力:请参阅https://ghc.haskell.org/trac/ghc/ticket/9112https://ghc.haskell.org/trac/ghc/ticket/9123

In the meantime, I'm afraid my only suggestion is to use unsafeCoerce . 与此同时,我担心我的唯一建议是使用unsafeCoerce :( :(

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

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