我建议您使用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
)。
使用GADTs,您可以编写
Proxy
。
type Proxy :: k -> Type
data Proxy a where
Proxy :: Proxy @k a
有提议允许在声明头中使用可见的(kind)应用程序:
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
Proxy
的区别在于它是定义为统一数据类型(非GADT或者俚语:“传统”语法)还是一个GADT。它们是等价的(在旧版本的GHC 8.10上进行了测试),除了k之外。
- (
forall.
)指定的=可以使用TypeApplications
来指定, 如果未指定,将自动推断。
- (
forall{}.
)推断的=TypeApplications
跳过它,不能直接指定。
这适用于类型构造函数Proxy
和命名为P
的数据构造函数,因为两者都是多态的。Proxy
可以通过量化k
的方式来指定Proxy @Nat 42
或推断Proxy 42
。
Proxy :: forall (k :: Type). k -> Type
Proxy :: forall {k :: Type}. k -> Type
根据 P
中的量化方式,可以指定 k 为 P @Nat @42
或推断为 P @42
:
P :: forall {k :: Type} (a :: k). Proxy @k a
P :: forall {k :: Type} (a :: k). Proxy a
P :: forall (k :: Type) (a :: k). Proxy @k a
P :: forall (k :: Type) (a :: k). Proxy a
这会产生几个结果
- k 在两者中推断出来:
P @42 :: 代理 42
(P @42 :: 代理 42
)
data Proxy a = P
data Proxy a where
P :: Proxy a
- k 在
Proxy
中被指定但在 P
中被推断出来 (P @42 :: Proxy @Nat 42
)
data Proxy (a :: k) where
P :: Proxy a
type Proxy :: k -> Type
data Proxy a where
P :: Proxy a
- 在
Proxy
和P
中都指定了k(P @Nat @42 :: Proxy @Nat 42
)
data Proxy (a :: k) = P
type Proxy :: k -> Type
data Proxy a = P
type Proxy :: forall k. k -> Type
data Proxy a = P
type Proxy :: forall k. k -> Type
data Proxy (a :: k) = P
type Proxy :: k -> Type
data Proxy a where
P :: Proxy @k a
我正在等待许多与量化和范围有关的新变化定型,这可能已经过时了。
Id
类型同义词是否可以等效地转换为类型族? - A. Gtype family Id a where Id a = a
,并使用相同的类型签名。我相信这是等效的,但要注意类型族可能会有微妙之处(请参见关于类型族的元数)。 - Iceland_jackGADTs
扩展定义的Proxy
数据类型是否等同于简单定义的(没有先定义其种类):data Proxy a = Proxy
? - A. G