在"任意等级类型的实用类推理"中,作者讨论了包含:
我尝试在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
Run Code Online (Sandbox Code Playgroud)
我还没有达到理解论文的程度,但是,我仍然担心我误解了一些东西.这个类型检查应该吗?我的Haskell类型错了吗?
Cir*_*dec 17
类型检查器不知道何时应用包含规则.
您可以使用以下功能告诉它.
Prelude> let u :: ((f a -> f a) -> c) -> ((forall b. f b -> f b) -> c); u f n = f n
Run Code Online (Sandbox Code Playgroud)
这说明,给定特定类型转换的函数,我们可以从自然转换中创建函数forall b. f b -> f b
.
然后我们可以在第二个例子上成功尝试.
Prelude> :t g (u k2)
g (u k2) :: Int
Run Code Online (Sandbox Code Playgroud)
第一个例子现在也提供了更多信息.
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)'
Run Code Online (Sandbox Code Playgroud)
我不知道我们是否可以写一个更通用的版本u
; 我们需要一个具有较少多态性的约束级概念来编写类似的东西let s :: (a :<: b) => (a -> c) -> (b -> c); s f x = f x
归档时间: |
|
查看次数: |
546 次 |
最近记录: |