为什么
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>