haskell - 不同类型的重载/多态函数

我正在学习 Haskell 并遇到了一个 Unresolved 问题:

来 self 正在学习的类(class):

(+) :: Num a => a -> a -> a

For any numeric type a, (+) takes 2 values of type a and returns a value of type a.

根据示例:

1 + 1
-- 2 # type a is Int
3.0 + 4.0
-- 7.0 type a is Float
'a' + 'b' 
-- Type Error: Char is not a Numeric type

这很有道理,但我最终还是不明白后台发生了什么:

1 + 3.0

类型推断系统是否自动将我的 Int 转换为 Float,因为它知道它会返回 Float?

最佳答案

您应该在 ghci 中调查这些类型的问题。这是一种宝贵的学习资源:

$ ghci
GHCi, version 9.0.1: https://www.haskell.org/ghc/  :? for help
ghci> :t 1
1 :: Num p => p
ghci> :t 3.0
3.0 :: Fractional p => p
ghci> :t 1 + 3.0
1 + 3.0 :: Fractional a => a

第一课:数字文字是多态的。 1 不是 Int,它是多态的。它可以是编译代码所需的 Num 的任何实例。 3.0 不是 Float,它是编译代码所必需的 Fractional 的任何实例。 (不同之处在于文字中的小数点 - 它限制了允许的类型。)

第二课:当你将事物组合成一个表达式时,类型就会统一。当您统一 NumFractional 约束时,您会得到一个 Fractional 约束。这是因为 Fractional 被定义为要求它的所有实例也是 Num 的实例。

关于更多信息,让我们打开警告并查看它们提供的额外信息。

ghci> :set -Wall
ghci> 1

<interactive>:5:1: warning: [-Wtype-defaults]
    • Defaulting the following constraints to type ‘Integer’
        (Show a0) arising from a use of ‘print’ at <interactive>:5:1
        (Num a0) arising from a use of ‘it’ at <interactive>:5:1
    • In a stmt of an interactive GHCi command: print it
1
ghci> 1 + 3.0

<interactive>:6:1: warning: [-Wtype-defaults]
    • Defaulting the following constraints to type ‘Double’
        (Show a0) arising from a use of ‘print’ at <interactive>:6:1-7
        (Fractional a0) arising from a use of ‘it’ at <interactive>:6:1-7
    • In a stmt of an interactive GHCi command: print it
4.0

当打印一个值时,ghci 要求类型有一个 Show 实例。幸运的是,这里的细节不是太重要,但这就是默认警告引用 Show 的原因。

这里要观察的教训是,如果推理不需要更具体的东西,则带有 Num 实例的东西的默认类型是 Integer,而不是 Int 。如果推理不需要更具体的东西,则带有 Fractional 实例的东西的默认类型是 Double,而不是 Float。 (Float 基本上从不使用。忘记它的存在。)

因此,当推理运行时,表达式 1 + 3.0 被推断为类型 Fractional a => a。在没有对该类型的进一步要求的情况下,默认启动并说“a is Double”。然后该信息通过 (+) 流回到它的参数,并要求它们中的每一个也是 Double。幸运的是,每个参数都是一个多态文字,可以采用 Double 类型。类型检查成功,选择实例,进行添加,打印结果。

数字文字的多态性对于这个过程非常重要。 Haskell 没有任何类型对之间的隐式转换。尤其不是数字类型。如果您想真正将值从一种类型转换到另一种类型,则必须调用一个函数来执行您想要的转换。 (fromIntegralroundfloorceilingrealToFrac 是最常见的数字转换函数。)但是当值是多态时,这意味着推理可以选择匹配类型而不需要转换。

https://stackoverflow.com/questions/69700650/

相关文章:

java - org.gradle.api.internal.tasks.testing.TestS

c++ - std::vector 和 move 语义

spring-boot - 应用程序通过 Spring Boot Run 而不是通过 Intelli

r - 如何在 ifelse 中设置 FALSE 条件以使其保持原始值

java - 将 Enum 类名本身作为 String 获取的最佳方法

html - 是否可以使用纯 CSS 为仅包含一个元素的列表设置样式,而不是为包含多个元素的列表设置

typescript - 类型错误 : EventEmitter is not a construc

r - 如何调整饼图上的 ggrepel 标签?

typescript - 如何从Nuxt 3服务器获取路由参数

r - 在 download.file 中关闭 SSL 验证