简体   繁体   English

类型构造函数和存在类型

[英]Types constructors and existential types

Only polymorphic function can be applied to values of existential types. 仅多态函数可以应用于存在类型的值。 Those properties can be expressed by the corresponding quantifiers for expressions, and characterized by natural transformations. 这些属性可以通过用于表达式的相应量词来表示,并可以通过自然变换来表征。

Similarly, when we define a type constructor 同样,当我们定义类型构造函数时

data List a = Nil | Cons a (List a)

This type constructor works for all a whereas type families allows to have non uniform type constructors 此类型构造函数适用于所有a而类型族允许具有非统一的类型构造函数

type family TRes i o
type instance TRes Bool = String
type instance TRes String  = Bool

What natural transformation characterizes precisely this idea of "uniformity" at type level ? 在类型级别上,什么自然变换可以准确地表征这种“均匀性”概念?

Is there an equivalent of forcing naturality like we have at value level with rank-n types ? 是否存在与我们在等级n类型的值级别具有的强迫自然性等效的方法?

ApplyNat :: (forall a. a -> F a) -> b -> F b

I think you've confused a couple of different ideas here. 我认为您在这里混淆了几个不同的想法。

This type constructor works for all a . 这种类型的构造方法适用于所有的a

That's totality . 这就是总数 List :: * -> * produces a valid type of kind * given any argument a of kind * . List :: * -> *产生一种有效的类型*给出任何说法a样的* Haskell 98 datatypes are always total, but, as you point out, in modern Haskell you can write type families which don't cover all possible cases. Haskell 98数据类型始终是总计的,但是,正如您所指出的那样,在现代的Haskell中,您可以编写无法涵盖所有​​可能情况的类型族。 TRes Int is not a "real" type, in the sense that it contains no values, it doesn't reduce to any other type, and it's not equal to any type other than TRes Int . TRes Int不是“真实”类型,从某种意义上说,它不包含任何值,它不会TRes Int为任何其他类型,并且不等于TRes Int以外的任何其他类型。

Haskell has no totality checker at the value level or the type level (apart from the rules about undecidable instances, which are a blunt instrument), so, just as there is no way to rule out undefined values, there is no way to rule out "stuck" type families like TRes Int . Haskell在值级别或类型级别(除了关于不确定实例的规则,这是一种钝器)之外,没有总计检查器,因此,就像没有办法排除undefined值一样,也没有办法排除像TRes Int这样的“卡住”类型的家庭。 (For more on "stuck" type families see this blog post by Richard Eisenberg, the designer of TypeInType .) (有关“卡死”类型系列的更多信息,请参阅TypeInType的设计师Richard Eisenberg的博客文章 。)

Naturality is an altogether different idea. 自然是完全不同的想法。 In value-level Haskell, a natural transformation between f and g is a polymorphic function mapping values of type fx to values of type gx , without knowing anything about x . 在值级Haskell中, fg之间的自然转换是将fx类型的值映射到gx类型的值的多态函数,而无需了解x

type f ~> g = forall x. f x -> g x

With GHC 8 and TypeInType we can talk about kinds using the same language we use to talk about types, because kinds are types. 使用GHC 8和TypeInType我们可以使用与谈论类型相同的语言来谈论类型,因为种类类型。 The type expression forall x. fx -> gx 全部forall x. fx -> gx的类型表达式forall x. fx -> gx forall x. fx -> gx has kind * ( (~>) :: forall k. (k -> *) -> (k -> *) -> * ), so it's a perfectly valid classifier for types as well. forall x. fx -> gx具有种类*(~>) :: forall k. (k -> *) -> (k -> *) -> * ),因此它也是类型的完美有效分类器。 A type with that kind is a polymorphic type function mapping types of kind fx to types of kind gx . 具有这种类型的类型是将类型fx的类型映射为类型gx的多态类型函数。

What would you use a type-level natural transformation for, in the real world? 在现实世界中,您将使用类型级别的自然转换做什么? I dunno. 我不知道。 You wouldn't, probably. 您可能不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM