为什么函数的类型会根据是在文件中定义还是在 GHCi repl 中定义而不同?

问题描述 投票:0回答:1

为什么

mth4
的类型根据是在文件中定义还是直接在 GHCi repl 中定义而不同?

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
加载到 GHCI repl 中,
mth4
的类型为:

mth4 :: Integer -> Integer -> Integer -> Integer

但是如果它们直接输入到 GHCi repl 中,那么 mth4 的类型是:

mth4 :: Num a => a -> a -> a -> a

test2.hs
加载到 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

直接进入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>
haskell ghci
1个回答
0
投票

这是因为 单态限制 在 GHCi 中默认处于关闭状态:

该限制在已编译模块中默认打开,并在 GHCi 提示符下默认关闭(自 GHC 7.8.1 起)。您可以使用 MonomorphismRestriction 和 NoMonomorphismRestriction 语言编译指示来覆盖这些默认值。

还有这篇文章询问那是什么。

© www.soinside.com 2019 - 2024. All rights reserved.