繁体   English   中英

多态类型的包含

[英]Subsumption in polymorphic types

“任意等级类型的实用类推理”中 ,作者讨论了包含

3.3包容

我尝试在GHCi中测试的东西,但是即使g k2是为了进行类型检查,当我尝试使用GHC 7.8.3时它也不会:

λ> :set -XRankNTypes
λ> let g :: ((forall b. [b] -> [b]) -> Int) -> Int; g = undefined
λ> let k1 :: (forall a. a -> a) -> Int; k1 = undefined
λ> let k2 :: ([Int] -> [Int]) -> Int; k2 = undefined
λ> :t g k1

<interactive>:1:3: Warning:
    Couldn't match type ‘a’ with ‘[a]’
      ‘a’ is a rigid type variable bound by
          the type forall a1. a1 -> a1 at <interactive>:1:3
    Expected type: (forall b. [b] -> [b]) -> Int
      Actual type: (forall a. a -> a) -> Int
    In the first argument of ‘g’, namely ‘k1’
    In the expression: g k1
g k1 :: Int
λ> :t g k2

<interactive>:1:3: Warning:
    Couldn't match type ‘[Int] -> [Int]’ with ‘forall b. [b] -> [b]’
    Expected type: (forall b. [b] -> [b]) -> Int
      Actual type: ([Int] -> [Int]) -> Int
    In the first argument of ‘g’, namely ‘k2’
    In the expression: g k2
g k2 :: Int

我还没有达到理解论文的程度,但是,我仍然担心我误解了一些东西。 这个类型检查应该吗? 我的Haskell类型错了吗?

类型检查器不知道何时应用包含规则。

您可以使用以下功能告诉它。

Prelude> let u :: ((f a -> f a) -> c) -> ((forall b. f b -> f b) -> c); u f n = f n

这说明,给定特定类型转换的函数,我们可以从自然转换中获得函数forall b. fb -> fb forall b. fb -> fb

然后我们可以在第二个例子上成功尝试。

Prelude> :t g (u k2)
g (u k2) :: Int

第一个例子现在也提供了更多信息。

Prelude> :t g (u k1)
    Couldn't match type `forall a. a -> a' with `[a0] -> [a0]'
    Expected type: ([a0] -> [a0]) -> Int
      Actual type: (forall a. a -> a) -> Int
    In the first argument of `u', namely `k1'
    In the first argument of `g', namely `(u k1)'

我不知道我们是否可以写一个更通用的版本的u ; 我们需要一个更少多态的约束级概念来编写类似let s :: (a :<: b) => (a -> c) -> (b -> c); sfx = fx let s :: (a :<: b) => (a -> c) -> (b -> c); sfx = fx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM