例如,尝试编译以下代码
{-# LANGUAGE StandaloneDeriving, KindSignatures, DataKinds, GADTs#-}
data ExprTag = Tag1 | Tag2
data Expr (tag :: ExprTag) where
Con1 :: Int -> Expr tag
Con2 :: Expr tag -> Expr tag
Con3 :: Expr tag -> Expr Tag2
deriving instance Eq (Expr a)
给出类型错误
Could not deduce (tag1 ~ tag)
from the context (a ~ 'Tag2)
bound by a pattern with constructor
Con3 :: forall (tag :: ExprTag). Expr tag -> Expr 'Tag2,
in an equation for `=='
at Bar.hs:11:1-29
or from (a ~ 'Tag2)
bound by a pattern with constructor
Con3 :: forall (tag :: ExprTag). Expr tag -> Expr 'Tag2,
in an equation for `=='
at Bar.hs:11:1-29
`tag1' is a rigid type variable bound by
a pattern with constructor
Con3 :: forall (tag :: ExprTag). Expr tag -> Expr 'Tag2,
in an equation for `=='
at Bar.hs:11:1
`tag' is a rigid type variable bound by
a pattern with constructor
Con3 :: forall (tag :: ExprTag). Expr tag -> Expr 'Tag2,
in an equation for `=='
at Bar.hs:11:1
Expected type: Expr tag1
Actual type: Expr tag
In the second argument of `(==)', namely `b1'
In the expression: ((a1 == b1))
When typechecking the code for `=='
in a standalone derived instance for `Eq (Expr a)':
To see the code I am typechecking, use -ddump-deriv
我可以看到为什么这不起作用,但是有一些解决方案不需要我手动编写 Eq(和 Ord)实例吗?