Why is the type of mth4
is different based on whether it is defined in a file or directly in GHCi repl?
Contents of test2.hs
:
mth1 x y z = x * y * z
mth2 x y = \z -> x * y * z
mth3 x = \y -> \z -> x * y * z
mth4 = \x -> \y -> \z -> x * y * z
test2.hs
loaded into GHCI repl and the type of mth4
is:
mth4 :: Integer -> Integer -> Integer -> Integer
But if they are entered directly in the GHCi repl then the type of mth4 is:
mth4 :: Num a => a -> a -> a -> a
Loading test2.hs
into GHCi repl:
peti@DESKTOP-0I7S3RB:~/ws/haskell$ ghci test2.hs
GHCi, version 9.6.2: https://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /home/peti/.ghci
[1 of 2] Compiling Main ( test2.hs, interpreted )
Ok, one module loaded.
ghci> :t mth1
mth1 :: Num a => a -> a -> a -> a
ghci> :t mth2
mth2 :: Num a => a -> a -> a -> a
ghci> :t mth3
mth3 :: Num a => a -> a -> a -> a
ghci> :t mth4
mth4 :: Integer -> Integer -> Integer -> Integer
Entering directly in GHCi repl:
peti@DESKTOP-0I7S3RB:~/ws/haskell$ ghci
GHCi, version 9.6.2: https://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from /home/peti/.ghci
ghci> mth1 x y z = x * y * z
ghci> mth2 x y = \z -> x * y * z
ghci> mth3 x = \y -> \z -> x * y * z
ghci> mth4 = \x -> \y -> \z -> x * y * z
ghci> :t mth1
mth1 :: Num a => a -> a -> a -> a
ghci> :t mth2
mth2 :: Num a => a -> a -> a -> a
ghci> :t mth3
mth3 :: Num a => a -> a -> a -> a
ghci> :t mth4
mth4 :: Num a => a -> a -> a -> a
ghci>
This is because monomorphism restriction is switched off by default in GHCi:
The restriction is turned on by default in compiled modules, and turned off by default at the GHCi prompt (since GHC 7.8.1). You can override these defaults by using the MonomorphismRestriction and NoMonomorphismRestriction language pragmas.
There is also this post which asks what that is.