Does GHCi not allow the default declaration to resolve type ambiguity?

Being relatively new to Haskell, I'm trying to tilt my head towards the following inconsistency (there is a good reason for that, I'm sure). Perhaps my question simply stems from a misunderstanding of GHCi, but I'd rather sleep at night when I can calm down.

Here. If after binding a name foo

to some integral expression (just 1

, here) in a script and compiling that script, I load the latter into GHCi, :t

tells me the type foo

is Integer

.

$ printf %s\\n "foo=1" > foo.hs
$ ghci
Ξ»> :l foo.hs
[1 of 1] Compiling Main             ( foo.hs, interpreted )
Ok, modules loaded: Main.
Ξ»>  :t foo
foo :: Integer

      

As I understand it, the ambiguity in the type has been foo

removed thanks to some default declaration , which I assume is somewhere in Prelude

:

default (Integer, Double)

      

So far so good. However, when I perform the seemingly equivalent binding let

directly inside GHCi, :t

it tells me that the latter still treats foo

as a polymorphic numeric constant, rather than as Integer

:

$ ghci
Ξ»> let foo=1
Ξ»> :t foo
foo :: Num a => a

      

How can this discrepancy be explained? What is the reason for this? Does GHCi allow default declaration to disambiguate type? And if so, in what situations does it do?

(I'm using GHC 7.8.3 for information.)

+3


source to share


1 answer


Does ghci use the default declaration for type ambiguity resolution?

In GHC 7.8 or higher, the monomorphism constraint is indeed in the GHCi wrapper, so declarations in the interactive wrapper will not apply the Num values ​​of the rule for top-level expressions other than lambda bindings (on the monomorphic constraint). The monomorphism constraint will still apply to loadable external modules.



You can toggle this with :set -XMonomorphismRestriction

.

+4


source







All Articles