【发布时间】:2014-04-15 06:34:57
【问题描述】:
最新版本的 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 时角色可以提高安全性,这当然非常好!
有什么可能的解决方案来解决这个问题?什么是最推荐的?
【问题讨论】:
-
根据文档 (haskell.org/ghc/docs/7.8.2/html/users_guide/roles.html) ,您必须更改数据类型的角色,但我相信这只能在数据类型的定义中完成。我不确定角色如何与数据系列交互 - 也许您可以在每个实例的基础上定义角色 - 但我对此表示怀疑。
-
但是要更改
MVector的角色,我应该在vector库上更改它。也许我应该向vector包的维护者报告。