GHC 用户指南参考以下示例描述了暗示性多态性扩展:
f :: Maybe (forall a. [a] -> [a]) -> Maybe ([Int], [Char])
f (Just g) = Just (g [3], g "hello")
f Nothing = Nothing
但是,当我在文件中定义此示例并尝试调用它时,出现类型错误:
ghci> f (Just reverse)
<interactive>:8:9:
Couldn't match expected type `forall a. [a] -> [a]'
with actual type `[a0] -> [a0]'
In the first argument of `Just', namely `reverse'
In the first argument of `f', namely `(Just reverse)'
In the expression: f (Just reverse)
ghci> f (Just id)
<interactive>:9:9:
Couldn't match expected type `forall a. [a] -> [a]'
with actual type `a0 -> a0'
In the first argument of `Just', namely `id'
In the first argument of `f', namely `(Just id)'
In the expression: f (Just id)
似乎只有undefined
, Nothing
, orJust undefined
满足类型检查器。
因此,我有两个问题:
- 可以
Just f
为任何非底部调用上述函数f
吗? - 有人可以提供一个值的示例,该值只能用暗示性多态性来定义,并且可以以非平凡的方式使用吗?
后者尤其是考虑到有关 Impredicative Polymorphism 的 HaskellWiki 页面,这目前为扩展的存在提供了一个绝对没有说服力的案例。