为什么 ghci 输出 (Num a) => a for :t 4 而不是 (Ord a) => a?
Why does ghci outputs (Num a) => a for :t 4 and not (Ord a) => a?
当我在 ghci 中输入 :t 4
时,我得到
Prelude> :t 4
4 :: Num t => t
我明白为什么 4
不仅是 Int 或 Integer,而且它是自下而上推断的,但我不明白为什么 4
没有显示为 Ord t => t
或更正确的是这样的:
4 :: (Ord t || Num t) => t
因为4
既是Ord
又是Num
但是Ord
和Num
没有联系...
那为什么:t 4
只输出Num
?
并非所有具有 Num
实例的类型也具有 Ord
实例,并且您只需要 Num
的 fromInteger
部分来获得重载的数字文字 Haskell有。例如,Data.Complex
中的 Complex
有一个 Num
实例,但没有 Ord
。在这种情况下,4
是 而不是 和 Ord
。
ghci> import Data.Complex
ghci> let x = 1 :: Complex Double
ghci> let y = 2 :: Complex Double
ghci> x < y
<interactive>
* No instance for (Ord (complex Double)) arising from use of `<'
* In the expression: x < y
In the equation for `it': it = x < y
ghci>
正如@Lee 评论的那样,这是 report 中概述的行为。
当我在 ghci 中输入 :t 4
时,我得到
Prelude> :t 4
4 :: Num t => t
我明白为什么 4
不仅是 Int 或 Integer,而且它是自下而上推断的,但我不明白为什么 4
没有显示为 Ord t => t
或更正确的是这样的:
4 :: (Ord t || Num t) => t
因为4
既是Ord
又是Num
但是Ord
和Num
没有联系...
那为什么:t 4
只输出Num
?
并非所有具有 Num
实例的类型也具有 Ord
实例,并且您只需要 Num
的 fromInteger
部分来获得重载的数字文字 Haskell有。例如,Data.Complex
中的 Complex
有一个 Num
实例,但没有 Ord
。在这种情况下,4
是 而不是 和 Ord
。
ghci> import Data.Complex
ghci> let x = 1 :: Complex Double
ghci> let y = 2 :: Complex Double
ghci> x < y
<interactive>
* No instance for (Ord (complex Double)) arising from use of `<'
* In the expression: x < y
In the equation for `it': it = x < y
ghci>
正如@Lee 评论的那样,这是 report 中概述的行为。