简体   繁体   English

为什么`forall`需要在数据定义中具有多态类型?

[英]Why is `forall` required to have a polymorphic type in data definition?

It seems that I need to explicitly say forall to have a parametric type in data definition. 似乎我需要明确地说forall在数据定义中具有参数类型。 For example, this 例如,这个

data A = A (forall s. ST s (STUArray s Int Int))

will work while this 这将是有效的

data A = A (ST s (STUArray s Int Int))

won't. 惯于。

Maybe I'm asking something too obvious, but the reason for this is not clear to me because in most other cases you don't need an explicit forall to specify a parametric type; 也许我会问一些太明显的东西,但是我不清楚这个原因,因为在大多数其他情况下你不需要明确的forall来指定参数类型; the compiler does it instead. 编译器会这样做。 So what is the difference here? 那么这里的区别是什么?

There are two different places the forall could go, only one of which is what you intend. forall有两个不同的地方,其中只有一个是你想要的。 The other is 另一个是

data A = forall s . A (ST s (STUArray s Int Int))

Which is equivalent to the GADT syntax 这相当于GADT语法

data A where
  A :: ST s (STUArray s Int Int) -> A

This can be thought of as a box holding an ST state transformer with some s or other—completely useless. 这可以被认为是一个装有ST状态变压器的盒子,有些s或其他完全没用。 But the type checker isn't specially tailored to the needs of ST , and similar types are useful in other contexts. 但是类型检查器并不是专门针对ST的需求而定制的,类似的类型在其他环境中也很有用。

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

相关问题 为什么要在数据类型定义中对函数进行编码? - Why encode function in data type definition? 为什么多态类型同义词在实例中不能像多态数据声明那样工作? - Why do polymorphic type synonyms not work like polymorphic data declarations in instances? 为什么id的类型不能专门用于(forall a.a - > a) - >(forall b.b - > b)? - Why can't the type of id be specialised to (forall a. a -> a) -> (forall b. b -> b)? 为什么是forall a。 一个不被认为是Int的子类型,而我可以使用类型forall a的表达式。 预计会有任何一种类型的Int? - Why is forall a. a not considered a subtype of Int while I can use an expression of type forall a. a anywhere one of type Int is expected? 为什么这个类型定义是非法的? - Why is this type definition illegal? 如果在数据定义中声明,为什么我必须在 function 中指定类型类? - Why do I have to specify typeclass in function if it was declared in data definition? 使用类型同义词的函数定义“比预期的更少多态” - Function definition using type synonym is “less polymorphic than expected” 数据类型定义的限制 - Restriction on the data type definition 在上下文中使用数据构造函数 - forall in Context with Data constructor 如何使用类型类转换为多态数据类型? - How to use a typeclass into a polymorphic data type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM