[英]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.