[英]Haskell PolyKinds extension and type families
我在 Haskell 中研究类型族以深入了解该主题,并尝试同时使用多态类型和类型族。
例如,文件的开头具有以下语言扩展名(文件中的内容比此处显示的要多):
{-# LANGUAGE TypeFamilies,
StandaloneKindSignatures,
RankNTypes,
PolyKinds,
DataKinds,
TypeOperators,
TypeApplications,
KindSignatures,
ScopedTypeVariables,
UndecidableInstances,
MultiParamTypeClasses,
AllowAmbiguousTypes #-}
然后我在类型声明中使用多态类型:
data Proxy (a :: k) = Proxy
效果很好。 但当时我正试图在具有更丰富定义的类型家族中使用它们:
type family PK (a :: k) :: Type where
PK Int = Char
GHC 抛出错误:
• Expected kind ‘k’, but ‘Int’ has kind ‘*’
• In the first argument of ‘PK’, namely ‘Int’
In the type family declaration for ‘PK’.
有解决办法吗? GHC 版本是 8.10.7。 感谢您提前提供任何想法和帮助。
我建议您使用StandaloneKindSignatures
:
..
{-# Language StandaloneKindSignatures #-}
type Id :: k -> k
type Id a = a
type Proxy :: k -> Type
data Proxy a = Proxy
type
PK :: k -> Type
type family
PK a where
PK Int = Char
kind 参数是不可见的,但您可以在类型族PK @Type Int = Char
中显式编写它(需要TypeApplications
)。
使用 GADT,您可以编写Proxy
type Proxy :: k -> Type
data Proxy a where
Proxy :: Proxy @k a
有一些建议允许在声明头中显示(种类)应用程序:
type Id :: k -> k
type Id @k a = a
type Proxy :: k -> Type
data Proxy @k a = Proxy
type
PK :: k -> Type
type family
PK @k a where
PK @Type Int = Char
我们可以在forall->
的种类中使用“可见的依赖量化”,而不是(隐式的)不可见的forall.
type Id :: forall k -> k -> k
type Id k a = a
type Proxy :: forall k -> k -> Type
data Proxy k a = Proxy
type
PK :: forall k -> k -> Type
type family
PK k a where
PK Type Int = Char
你是一种语言扩展。 如果您也启用了CUSKs
扩展,那么您编写的内容将满足您的需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.