當我在ghci中輸入:t 4
我得到爲什麼ghci輸出(Num a)=> a for:t 4而不是(Ord a)=> a?
Prelude> :t 4
4 :: Num t => t
我明白爲什麼4
不僅是int或一個整數,它是infered底了,但我不明白爲什麼4
不作爲的Ord t => t
或更正確的是這樣的:
4 :: (Ord t || Num t) => t
因爲4
既是Ord
和Num
但Ord
和Num
有沒有關係。
那麼爲什麼:t 4
只輸出Num
?
這在[haskell報告]中描述(https://www.haskell.org/onlinereport/haskell2010/haskellch6.html#x13-1360006.4.1) – Lee
假設我給了'4'類型'Ord t = > t'。現在我注意到'Bool'是'Ord'的一個實例。因此'4 :: Bool'。 「4 :: Bool」應該是「真」還是「假」,爲什麼? –
@李Btw,有類似的問題關於類型違約規則:https://stackoverflow.com/questions/39251728/ghci-randomio-type-inference我試圖回答它,理解報告,但仍然沒有明確的看法。也許更有經驗的_Haskell_用戶可以幫助:) – Shersh