【问题标题】:mapping over RankNTypes functions映射 RankNTypes 函数
【发布时间】:2021-10-05 11:57:19
【问题描述】:

使用来自hererankN

{-# 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

(为什么map rankN [negate] 会给出一个奇怪的错误Couldn't match type ‘n’ with ‘Integer’,而我认为没有涉及任何Integers?)

另外,我刚刚检查了: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 问题中找不到答案。)

【问题讨论】:

    标签: haskell rank-n-types


    【解决方案1】:

    它默认不起作用的原因是因为映射的类型是(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)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-17
      • 2019-02-14
      • 2012-07-26
      • 2018-06-03
      • 1970-01-01
      • 2020-05-16
      • 2011-08-04
      • 1970-01-01
      相关资源
      最近更新 更多