Why is an explicit type NOT required for basic Prelude arithmetic?

I was answering a question and wrote code to do this work.

isPrime :: Int -> Bool isPrime n = primeCheck n $ floor $ sqrt $ (fromIntegral n :: Double) 

I suggested that an explicit type signature is required, as explained in my answer. Then I checked it in both GHC and GHCi and found that I did not need an explicit type for the conversion, despite the fact that floor and sqrt are polymorphic. I know GHCi does some kind of default , but I don't know anything about GHC. Obviously, both Float and Double would be the right choice, why does the GHC choose one by one? What type is set by default, and why (presumably) is GHC default in this case?

+6
source share
2 answers

GHC also introduces a default type, at least whenever you export a module, it will monomorphize any ambiguous numeric types for types in default types for the module, which defaults to:

 default (Integer, Double) 

See "4.3.4 Ambiguous Types and Default Values ​​for Overloaded Numeric Operations" in the Haskell Specification .

You can disable this with pragma {-# LANGUAGE NoMonomorphismRestriction #-} in the module you want to export, with toplevel numeric polymorphic types left untouched.

+7
source

Am I guessing the default keyword?

It allows you to specify what to use if a signature is not specified. There is also a default default value, which is Double .

I forget how it all works; it's a rather obscure corner of the tongue ...

0
source

All Articles