rankN從這里使用:
{-# LANGUAGE RankNTypes #-}
rankN :: (forall n. Num n => n -> n) -> (Int, Double)
rankN f = (f 1, f 1.0)
為什么做不到map rankN [negate]?有沒有辦法讓它成為可能?
我想我現在(大部分)理解 RankNTypes,并且看到我可以同時做rankN ( 1)和 rankN negate.
(當我認為沒有涉及任何s時,為什么會map rankN [negate]出現奇怪的錯誤?)Couldn't match type ‘n’ with ‘Integer’Integer
另外,我剛剛檢查了:t map rankN. 它給人一種恐懼Couldn't match type ‘a’ with ‘n -> n’ because type variable ‘n’ would escape its scope. This (rigid, skolem) type variable is bound by...。我看到在某些情況下該錯誤是如何發生的,但并不真正理解為什么它會適用于此。
謝謝,如果這是一個重復,對不起。(但我無法在所有其他 RankNTypes 問題中找到答案。)
uj5u.com熱心網友回復:
默認情況下它不起作用的原因是因為 map 的型別是(a -> b) -> [a] -> [b],但您不能將其實體化為a涉及的型別forall,在這種情況下forall n. Num n => n -> n。這種實體化稱為impredicative,GHC 長期以來不(可靠地)支持它。
從 GHC 9.2.1 開始,有一個新的可靠的ImpredicativeTypes擴展實作,它允許你不可預測地實體化:
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)]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/316848.html
