简体   繁体   English

理解两个Haskell签名之间的区别,一个使用forall

[英]Understanding the difference between two Haskell signatures, one using forall

In this paper there is a function with the following signature: 本文中,有一个具有以下签名的函数:

vreplicate :: forall a n . SNatI n => a -> Vec a n 

What is the difference between this signature and the one that doesn't have forall : 这个签名和没有forall签名有什么区别:

vreplicate :: SNatI n => a -> Vec a n 

? I was under the impression that with no forall , it implicitly means the same as having forall in front that names all the type variables. 我的印象是,没有forall ,它隐含意味着与前面的forall相同,它命名所有类型变量。

There are two important cases where including a forall makes a difference. 有两个重要的案例,其中包括forall The first is that the location of a forall can change the meaning of a type -- if it comes to the left of an arrow, that means an argument to the function is "more polymorphic" than otherwise. 第一个是forall的位置可以改变一个类型的含义 - 如果它出现在箭头的左边,这意味着函数的参数比其他情况“更多态”。 This difference is a fundamental one; 这种差异是一个基本的差异; however, it does not seem to apply here. 但是,它似乎并不适用于此。

The second difference is a syntactic (rather than fundamental) one, namely: in the presence of ScopedTypeVariables , variables bound by a forall open a typing scope, whereas variables implicitly bound without a forall do not. 第二个区别是语法(而不是基本),即:在存在ScopedTypeVariables的情况下,由forall绑定的变量打开一个打字范围,而没有forall隐式绑定的变量则不会。 Thus, in the body of vreplicate , one can use the type variables a and n and be certain that they refer to the same types as mentioned in the signature of vreplicate . 因此,在vreplicate的主体中,可以使用类型变量an并确保它们引用与vreplicate的签名中提到的相同类型。 Without the forall (or without ScopedTypeVariables ), uses of a and n in the body of vreplicate would introduce fresh universally quantified variables, and it would be the programmer's responsibility to ensure they were unified with the types in the signature of vreplicate if that were a desired. 如果没有forall (或没有ScopedTypeVariables ),在vreplicate主体中使用an会引入新的通用量化变量,并且程序员有责任确保它们与vreplicate签名中的类型统一,如果这是期望。 Further details are available in the documentation . 更多详细信息可在文档中找到

Without reading the paper carefully I can't be sure, but I would strongly bet the latter is happening here. 如果没有仔细阅读本文,我无法确定,但我强烈打赌后者正在这里发生。

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

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