Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 将衍生过孔与幻像类型一起使用_Haskell_Deriving_Coerce - Fatal编程技术网

Haskell 将衍生过孔与幻像类型一起使用

Haskell 将衍生过孔与幻像类型一起使用,haskell,deriving,coerce,Haskell,Deriving,Coerce,很抱歉我的复述太长了,但我没能把它缩短。以下代码在最后一行之前编译良好: {-# LANGUAGE RankNTypes #-} {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, UndecidableInstances #-} {-# LANGUAGE DerivingVia, DerivingStrategies, StandaloneDeriving #-} {-# LANGUAGE ScopedTypeVariables #-}

很抱歉我的复述太长了,但我没能把它缩短。以下代码在最后一行之前编译良好:

{-# 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(phantom2da)(phantom1da)
它似乎在说它不能强迫“Via&c”变成“vector2da”,这很奇怪,因为它实际上是一个两层深的新类型,而且效果很好


我做错了什么?

通过新类型派生,但您编写了

data Via a b = Via a   -- should be newtype

我是个白痴。谢谢你。