“where”中的类型声明——发生什么事了?
在阅读 QuickCheck 手册时,我遇到了以下示例
prop_RevRev xs = reverse (reverse xs) == xs
where types = xs::[Int]
:手册继续说:
属性必须具有单态类型。 “多态”属性,例如上面的属性,必须限制为用于测试的特定类型。通过在 a 中声明一个或多个参数的类型可以很方便地做到这一点
其中类型 = (x1 :: t1, x2 :: t2, ...)
条款。请注意,类型不是关键字;而是类型。这只是一个本地声明,它提供了一个方便的地方来限制 x1、x2 等的类型。
我以前从未在 Haskell 中见过这样的技巧。这就是我真正遇到的问题:
为什么这种类型声明语法存在?它能为我做哪些以下事情不能做的事情?
prop_RevRev :: [Int] ->;布尔 prop_RevRev xs = 反向 (反向 xs) == xs
这种使用
where
是否构成类型声明的“特殊”语法?或者它是否一致且合乎逻辑(如果是,如何?)?这个用法是标准的还是传统的 Haskell?
While reading the QuickCheck Manual, I came across the following example:
prop_RevRev xs = reverse (reverse xs) == xs
where types = xs::[Int]
The manual goes on to say:
Properties must have monomorphic types. `Polymorphic' properties, such as the one above, must be restricted to a particular type to be used for testing. It is convenient to do so by stating the types of one or more arguments in a
where types = (x1 :: t1, x2 :: t2, ...)
clause. Note that types is not a keyword; this is just a local declaration which provides a convenient place to restrict the types of x1, x2 etc.
I have never seen such a trick in Haskell before. Here's what I'm really having problems with:
Why does this syntax for type declarations even exist? What can it do for me that the following couldn't?
prop_RevRev :: [Int] -> Bool prop_RevRev xs = reverse (reverse xs) == xs
Does this use of
where
constitute 'special' syntax for type declarations? Or is it consistent and logical (and if so, how?)?Is this usage standard or conventional Haskell?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
where
不是类型声明的特殊语法。例如,这样可以:这样也可以:
where type = (xs :: [Int])
相对于prop_RevRev :: [Int] -> 的优势Bool
是在后一种情况下你必须指定返回类型,而在前一种情况下编译器可以为你推断它。如果您有一个非布尔属性,这会很重要,例如:where
is not special syntax for type declarations. For example, this works:and so does this:
The advantage of
where type = (xs :: [Int])
overprop_RevRev :: [Int] -> Bool
is that in the latter case you have to specify the return type, while in the former case the compiler can infer it for you. This would matter if you had a non-Boolean
property, for example:它不是特殊的语法,有时您只是需要它,就像下面的情况一样:
就目前情况而言,无法键入 this,因为无法识别值
read
的类型。所知道的是它必须是 Show 和 Read 的实例。但这种类型根本不会出现在类型签名中,因此也不可能将其保留在类型签名中并推断出受约束的类型。 (只是没有可以约束的类型变量。)有趣的是,
read s
所做的事情完全取决于为read s
提供的类型签名,例如例如:会成功,而
不会。
It is no special syntax, and sometime you just need it, like in the following case:
As it stands, this cannot be typed because the type of the value
read s
cannot be identified. All that is known is that it must be an instance of Show and of Read. But this type does not appear in the type signature at all, so it is also not possible to leave it at that and infer a constrained type. (There is just no type variable that could be constrained.)It is interesting to note that what
read s
does depends entirely on the type signature one gives toread s
, for example:will succeed, while
will not.