gpt4 book ai didi

haskell - 非多态函数的多态签名 : why not?

转载 作者:行者123 更新时间:2023-12-04 13:12:00 25 4
gpt4 key购买 nike

例如,考虑平凡函数

f :: (Integral b) => a -> b
f x = 3 :: Int

GHC 提示它无法推断 (b ~ Int)。该定义与签名相匹配,因为它返回的东西是 Integral(即 Int)。为什么 GHC 会/应该强制我使用更具体的类型签名?

谢谢

最佳答案

Haskell 中的类型变量是普遍量化的,所以 Integral b => b不仅仅意味着一些Integral类型,表示任意 Integral类型。换句话说,调用者可以选择应该使用哪些具体类型。因此,函数总是返回 Int 显然是类型错误。当类型签名说我应该能够选择任何 Integral类型,例如IntegerWord64 .

有一些扩展允许您使用 existentially quantified type variables ,但它们使用起来更麻烦,因为它们需要一个包装类型(为了存储类型类字典)。大多数时候,最好避免它们。但是如果你确实想使用存在类型,它看起来像这样:

{-# LANGUAGE ExistentialQuantification #-}

data SomeIntegral = forall a. Integral a => SomeIntegral a

f :: a -> SomeIntegral
f x = SomeIntegral (3 :: Int)

使用此函数的代码必须具有足够多态性才能与任何 Integral 一起使用。类型。我们还必须使用 case 进行模式匹配而不是 let以防止 GHC 的大脑爆炸。
> case f True of SomeIntegral x -> toInteger x
3
> :t toInteger
toInteger :: Integral a => a -> Integer

在上面的例子中,你可以想到 x具有类型 exists b. Integral b => b ,即一些未知的 Integral类型。

关于haskell - 非多态函数的多态签名 : why not?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9506173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com