“where”中的类型声明——发生什么事了?

发布于 2024-12-08 07:43:24 字数 775 浏览 0 评论 0原文

在阅读 QuickCheck 手册时,我遇到了以下示例

prop_RevRev xs = reverse (reverse xs) == xs
  where types = xs::[Int]

:手册继续说:

属性必须具有单态类型。 “多态”属性,例如上面的属性,必须限制为用于测试的特定类型。通过在 a 中声明一个或多个参数的类型可以很方便地做到这一点

其中类型 = (x1 :: t1, x2 :: t2, ...)

条款。请注意,类型不是关键字;而是类型。这只是一个本地声明,它提供了一个方便的地方来限制 x1、x2 等的类型。

我以前从未在 Haskell 中见过这样的技巧。这就是我真正遇到的问题:

  1. 为什么这种类型声明语法存在?它能为我做哪些以下事情不能做的事情?

    prop_RevRev :: [Int] ->;布尔
    prop_RevRev xs = 反向 (反向 xs) == xs
    
  2. 这种使用 where 是否构成类型声明的“特殊”语法?或者它是否一致且合乎逻辑(如果是,如何?)?

  3. 这个用法是标准的还是传统的 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:

  1. 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
    
  2. Does this use of where constitute 'special' syntax for type declarations? Or is it consistent and logical (and if so, how?)?

  3. Is this usage standard or conventional Haskell?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海未深 2024-12-15 07:43:24

where 不是类型声明的特殊语法。例如,这样可以:

prop_RevRev :: [Int] -> Bool
prop_RevRev xs = ys == xs
  where ys = reverse (reverse xs)

这样也可以:

prop_RevRev xs = ys == xs
  where ys = reverse (reverse xs)
        ys :: [Int]

where type = (xs :: [Int]) 相对于 prop_RevRev :: [Int] -> 的优势Bool 是在后一种情况下你必须指定返回类型,而在前一种情况下编译器可以为你推断它。如果您有一个非布尔属性,这会很重要,例如:

prop_positiveSum xs = 0 < length xs && all (0 <) xs ==> 0 < sum xs
  where types = (xs :: [Int])

where is not special syntax for type declarations. For example, this works:

prop_RevRev :: [Int] -> Bool
prop_RevRev xs = ys == xs
  where ys = reverse (reverse xs)

and so does this:

prop_RevRev xs = ys == xs
  where ys = reverse (reverse xs)
        ys :: [Int]

The advantage of where type = (xs :: [Int]) over prop_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:

prop_positiveSum xs = 0 < length xs && all (0 <) xs ==> 0 < sum xs
  where types = (xs :: [Int])
世俗缘 2024-12-15 07:43:24

它不是特殊的语法,有时您只是需要它,就像下面的情况一样:

foo :: String -> String
foo s = show (read s)

就目前情况而言,无法键入 this,因为无法识别值 read 的类型。所知道的是它必须是 Show 和 Read 的实例。但这种类型根本不会出现在类型签名中,因此也不可能将其保留在类型签名中并推断出受约束的类型。 (只是没有可以约束的类型变量。)

有趣的是,read s 所做的事情完全取决于为 read s 提供的类型签名,例如例如:

read "Just True" :: (Maybe Bool)

会成功,而

read "Just True" :: (Maybe Int)

不会。

It is no special syntax, and sometime you just need it, like in the following case:

foo :: String -> String
foo s = show (read s)

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 to read s, for example:

read "Just True" :: (Maybe Bool)

will succeed, while

read "Just True" :: (Maybe Int)

will not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文