Inconsistent behavior with fromIntegral in GHCi

时间秒杀一切 提交于 2019-12-12 11:23:50

问题


I was hoping someone could explain the following behavior in GHCi, when using the function fromIntegral:

Prelude> let x = 1 :: Integer                                                                                                                                                    
Prelude> :t x                                                                                                                                                                    
x :: Integer                                                                                                                                                                     
Prelude> sqrt $ fromIntegral x                                                                                                                                                   
1.0                                                                                                                                                                              
Prelude> let y = fromIntegral x                                                                                                                                                  
Prelude> sqrt y                                                                                                                                                                  

<interactive>:181:1:                                                                                                                                                             
No instance for (Floating Integer)                                                                                                                                           
  arising from a use of `sqrt'                                                                                                                                               
Possible fix: add an instance declaration for (Floating Integer)                                                                                                             
In the expression: sqrt y                                                                                                                                                    
In an equation for `it': it = sqrt y                                                                                                                                         

Why does it matter whether I set y and then take its sqrt or just directly take the sqrt?


回答1:


fromIntegral is polymorphic in its return type. So the type of y in your code could be expected to be Num a => a. This type would allow you to use y as the argument to sqrt without problem.

However due to the monomorphism restriction, the type of y is not allowed to be polymorphic. Therefore it is defaulted to the default Num type, which is Integer.

When you do sqrt $ fromIntegral x the monomorphism restriction does not apply because it only applies to global variables and you don't store the result of fromIntegral in a variable this time.

You can fix this issue by either adding a type signature to y (let y :: Num a => a; y = fromIntegal x) or by disabling the monomorphism restriction.



来源:https://stackoverflow.com/questions/11439163/inconsistent-behavior-with-fromintegral-in-ghci

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!