A typecheck errors in deriving wrapper for Linear.V
I am trying to make a newtype wrapper for the Linear.V type and derive useful instances. I was trying this:
{-# LANGUAGE DataKinds, PolyKinds, ScopedTypeVariables,
StandaloneDeriving, FlexibleContexts, UndecidableInstances,
GeneralizedNewtypeDeriving, PartialTypeSignatures, TypeFamilies #-}
import Linear.V
import Control.Lens.At
data Foo = Foo1 | Foo2 deriving (Show, Eq)
Attempt 1 - I would think GeneralizedNewtypeDeriving would do, but nope:
newtype Bar n = Bar {
getBar :: V n Foo
} deriving (Show, Eq, Ixed)
I get this error:
• Couldn't match representation of type ‘f (V n Foo)’
with that of ‘f (Bar n)’
arising from the coercion of the method ‘ix’
from type ‘Index (V n Foo)
-> Control.Lens.Type.Traversal' (V n Foo) (IxValue (V n Foo))’
to type ‘Index (Bar n)
-> Control.Lens.Type.Traversal' (Bar n) (IxValue (Bar n))’
NB: We cannot know what roles the parameters to ‘f’ have;
we must assume that the role is nominal
• When deriving the instance for (Ixed (Bar n))
I have made attempt 2 using standalone deriving like this:
newtype Bar n = Bar {
getBar :: V n Foo
} deriving (Show, Eq)
type instance Index (Bar n) = Int
type instance IxValue (Bar n) = Foo
deriving instance Ixed (V n Foo) => Ixed (Bar n)
But then I got a different error:
• Couldn't match representation of type ‘f1 (V n Foo)’
with that of ‘f1 (Bar n)’
arising from a use of ‘GHC.Prim.coerce’
NB: We cannot know what roles the parameters to ‘f1’ have;
we must assume that the role is nominal
• In the expression:
GHC.Prim.coerce
@(Index (V n Foo)
-> Control.Lens.Type.Traversal' (V n Foo) (IxValue (V n Foo)))
@(Index (Bar n)
-> Control.Lens.Type.Traversal' (Bar n) (IxValue (Bar n)))
ix
In an equation for ‘ix’:
ix
= GHC.Prim.coerce
@(Index (V n Foo)
-> Control.Lens.Type.Traversal' (V n Foo) (IxValue (V n Foo)))
@(Index (Bar n)
-> Control.Lens.Type.Traversal' (Bar n) (IxValue (Bar n)))
ix
When typechecking the code for ‘ix’
in a derived instance for ‘Ixed (Bar n)’:
To see the code I am typechecking, use -ddump-deriv
In the instance declaration for ‘Ixed (Bar n)’
• Relevant bindings include
ix :: Index (Bar n)
-> Control.Lens.Type.Traversal' (Bar n) (IxValue (Bar n))
(bound at a.hs:12:1)
I am unsure why either of the errors actually happens. Could this be done somehow? I am not so experienced with the advanced type level features and so far I was also unable to actually write this particular instance definition manually, so I would consider that a solution as well. But I would prefer to use the deriving
mechanism somehow, since it seems more reusable.
EDIT: I have tried this manual instance decalaration:
type instance Index (Bar n) = Int
type instance IxValue (Bar n) = Foo
instance Ixed (Bar n) where
ix i f (Bar v) = ix i f v
But that yield the following error:
• Couldn't match type ‘V n Foo’ with ‘Bar n’
Expected type: f (Bar n)
Actual type: f (V n Foo)
• In the expression: ix i f v
In an equation for ‘ix’: ix i f (Bar v) = ix i f v
In the instance declaration for ‘Ixed (Bar n)’
• Relevant bindings include
v :: V n Foo (bound at a.hs:14:15)
f :: IxValue (Bar n) -> f (IxValue (Bar n)) (bound at a.hs:14:8)
i :: Index (Bar n) (bound at a.hs:14:6)
ix :: Index (Bar n)
-> Control.Lens.Type.Traversal' (Bar n) (IxValue (Bar n))
(bound at a.hs:14:3)
Which seems to me like that the compiler can't figure out that the Index
of both V n Foo
and Bar n
is Int
. But I am not sure about that.
You are almost there. The remaining problem is transforming the ix
traversal for the underlying V n Foo
, which ultimately returns a function V n Foo -> f (V n Foo)
, into an ix
traversal for the wrapper type Bar n
, which should ultimately return a function Bar n -> f (Bar n)
. We have to "unpack" the definition of Traversal'
to know this.
In your code, ix ifv
has type f (V n Foo)
, so it is enough to fmap
with the Bar
constructor:
type instance Index (Bar n) = Int
type instance IxValue (Bar n) = Foo
instance Ixed (Bar n) where
ix i f (Bar v) = fmap Bar (ix i f v)
链接地址: http://www.djcxy.com/p/43462.html