问题描述
考虑以下函数:
add1 :: Num a => a - > a
add1 x = x + 1
以及下列应用程式
* Main> add1 2
3
* Main> add1 2.2
3.2
* Main> add1(读取1)
2
add1(读取1.5::浮点数)
2.5
* Main> add1(读为1.5)
***例外:Prelude.read:不分析
为什么浮点数最后一次调用 add1
失败,而它对整数有效?为什么我必须在这种情况下指定类型?
GHCi给它类型
> :t add1(读取1.5)
add1(读取1.5)::(读取a,数字a)=> a
所以,它仍然是多态的。但是,当在GHCi中进行评估时,它隐含地 print
ed,因此GHCi必须为 a
选择一些具体类型。 GHCi使用,并静态选择 a = Integer
。请注意,这个选择是静态完成的(在类型检查过程中,大致),并且仅取决于类型(例如 String
),而不是实际值(例如 1.5\"
)。 Integer
既是 Read
和a Num
,所以所有静态检查通过。
更详细地说,GHCi尝试类型()
,整数
, Double
- 按照该顺序,除非有一个默认的
声明否则。 / p>
然后,在运行时,代码的行为如下:
> add1(读取1.5):: Integer
*** Exception:Prelude.read:no parse
,因为字符串不能被解析。相比之下,这将是罚款:
> add1(读取1.5):: Float
2.5
Consider the following function
add1 :: Num a => a -> a
add1 x = x + 1
and the following applications
*Main> add1 2
3
*Main> add1 2.2
3.2
*Main> add1 (read "1")
2
add1 (read "1.5"::Float)
2.5
*Main> add1 (read "1.5")
*** Exception: Prelude.read: no parse
Why does the last invication of add1
fail for floating point numbers, while it works for integers? Why do I have to specify a type in that case?
GHCi gives it type
> :t add1 (read "1.5")
add1 (read "1.5") :: (Read a, Num a) => a
So, its still polymorphic. However, when evaluated in GHCi, it implicitly print
ed, so GHCi must pick some concrete type for a
. GHCi uses the defaulting rules, and statically chooses a = Integer
. Note that this choice is done statically (during type checking, roughly), and only depends on types (e.g. String
), not actual values (e.g. "1.5"
). Integer
is both a Read
and a Num
, so all static checks pass.
More in detail, GHCi tries types ()
, Integer
, Double
-- in that order, unless there is a default
declaration saying otherwise.
Then, at run-time the code behaves as
> add1 (read "1.5") :: Integer
*** Exception: Prelude.read: no parse
since the string can not be parsed. By comparison, this would be fine:
> add1 (read "1.5") :: Float
2.5
这篇关于haskell:read不分析没有显式类型的浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!