2016-12-05 34 views
3

这个工作在GHC 7.8.4罚款,但未能在7.10.3:NEWTYPE与GHC 7.10

{-# LANGUAGE DeriveGeneric    #-} 
{-# LANGUAGE GeneralizedNewtypeDeriving #-} 

module Foo where 

import qualified Data.Array.Unboxed  as A 
import   GHC.Generics    (Generic) 

newtype Elt = Elt Int deriving (Eq, Ord, Show, Num, Integral, Real, Enum, A.IArray A.UArray, Generic) 
type MyArr = A.UArray Int Elt 

有大量的消息像

/tmp/my.hs:9:75: 
    Couldn't match type ‘Int’ with ‘Elt’ 
    arising from the coercion of the method ‘Data.Array.Base.numElements’ 
     from type ‘forall i. A.Ix i => A.UArray i Int -> Int’ 
     to type ‘forall i. A.Ix i => A.UArray i Elt -> Int’ 
    Relevant role signatures: 
     type role A.Ix nominal 
     type role A.UArray nominal nominal 
    When deriving the instance for (A.IArray A.UArray Elt) 

虽然7.10的发布说明没有提到它,我看到 https://ghc.haskell.org/trac/ghc/ticket/9220#comment:11承认其突破性的变化。但是,解决方案是什么 - 我是否真的必须为MyArr创建一个新类型的包装器,并为每种用法使用辅助函数?

+0

有趣。也许'UArray'从元素的表现转化为名义角色?我想知道为什么。 – chi

+1

是的,看起来像你会(另一个'vector'的参数 - 获得'UnBox'实例更容易)。即使你想通过'IArray UArray Elt'实例'unsafeCoerce',你也不能导出你需要实现的方法。虽然我明白为什么我们不应该在这里接地,但我们无法手写我们的'IArray'实例的事实让我有点不高兴...... – Alec

+1

您可以从'Data.Array.Base'中导入方法。 –

回答

1

您不必为MyArr创建包装,但是您必须手动写出您之前派生的实例。有点蛮力的解决方案是unsafeCoerce你手动通过IArray实例的方式(不能coerce的原因是相同的,你不能派生)。

{-# LANGUAGE InstanceSigs, ScopedTypeVariables, MultiParamTypeClasses #-} 

import Data.Array.Base 
import Data.Array.IArray 
import Data.Array.Unboxed 
import Unsafe.Coerce 

instance IArray UArray Elt where 
    bounds :: forall i. Ix i => UArray i Elt -> (i, i) 
    bounds arr = bounds (unsafeCoerce arr :: UArray i Int)                                           

    numElements :: forall i. Ix i => UArray i Elt -> Int 
    numElements arr = numElements (unsafeCoerce arr :: UArray i Int) 

    unsafeArray :: forall i. Ix i => (i,i) -> [(Int, Elt)] -> UArray i Elt 
    unsafeArray lu ies = unsafeCoerce (unsafeArray lu [ (i,e) | (i,Elt e) <- ies ] :: UArray i Int) :: UArray i Elt 

    unsafeAt :: forall i. Ix i => UArray i Elt -> Int -> Elt 
    unsafeAt arr ix = Elt (unsafeAt (unsafeCoerce arr :: UArray i Int) ix)