很抱歉长时间的复制,但我一直未能使它更短。下面的代码编译良好,直到最后一行:
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, UndecidableInstances #-}
{-# LANGUAGE DerivingVia, DerivingStrategies, StandaloneDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Repro where
import Prelude hiding ((+))
class (Additive a) where
(+) :: a -> a -> a
data Vector2D u = Vector2D {
x :: u,
y :: u
}
addVector2 :: (Additive a) => Vector2D a -> Vector2D a -> Vector2D a
addVector2 Vector2D { x = x1, y = y1 } (Vector2D { x = x2, y = y2 }) =
Vector2D { x = x1 + x2, y = y1 + y2 }
instance (Additive a) => Additive (Vector2D a) where
(+) = addVector2
newtype Phantom1 d a = Phantom1 (Vector2D a) --Axial
deriving via (Vector2D a) instance forall d . (Additive a) => (Additive (Phantom1 d a))
data Via a b = Via a
class IsoEvidence a b where
convertTo :: a -> b
convertFrom :: b -> a
instance forall a b . (IsoEvidence a b, Additive b) => (Additive (Via a b)) where
(Via x) + (Via y) = Via $ convertFrom $ (convertTo x :: b) + (convertTo y :: b)
newtype Phantom2 d a = Phantom2 (Vector2D a) --Offset
instance (IsoEvidence (Phantom1 d a) (Phantom2 d a)) where
convertTo (Phantom1 x) = Phantom2 x
convertFrom (Phantom2 x) = Phantom1 x
deriving via (Via (Phantom2 d a) (Phantom1 d a))
instance (Additive a, IsoEvidence (Phantom1 d a) (Phantom2 d a)) =>
(Additive (Phantom2 d a))在此之后,我将得到以下错误:
Vector2D a类型的表示与Via (Phantom2 d a) (Phantom1 d a)的表示相匹配这似乎是说,它不能强迫“通过&c”"Vector2D a",这是奇怪的,因为它实际上是一个新类型的二级深度,这是很好的工作。
我在这里做错什么了?
发布于 2018-11-27 22:11:05
DerivingVia通过新类型工作,但您编写了
data Via a b = Via a -- should be newtypehttps://stackoverflow.com/questions/53508755
复制相似问题