Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconsistent behavior with fromIntegral in GHCi

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?

like image 581
Eyal Avatar asked Jan 16 '23 03:01

Eyal


1 Answers

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.

like image 94
sepp2k Avatar answered Jan 28 '23 11:01

sepp2k