问问题
37 次
1 回答
6
The reason that it does not work by default is because the type of map is (a -> b) -> [a] -> [b]
, but you cannot instantiate that a
to a type involving forall
, in this case forall n. Num n => n -> n
. Such instantiation is called impredicative, which was not supported (reliably) by GHC for a long time.
Since GHC 9.2.1 there is a new reliable implementation of the ImpredicativeTypes
extension, which does allow you to instantiate impredicatively:
GHCi, version 9.2.0.20210821: https://www.haskell.org/ghc/ :? for help
ghci> :set -XImpredicativeTypes
ghci> :t rankN
rankN :: (forall n. Num n => n -> n) -> (Int, Double)
ghci> :t map rankN
map rankN :: [forall n. Num n => n -> n] -> [(Int, Double)]
ghci> :t map rankN [negate]
map rankN [negate] :: [(Int, Double)]
ghci> map rankN [negate]
[(-1,-1.0)]
于 2021-10-05T12:12:18.427 回答