4 haskell unification higher-kinded-types higher-order-types
这可能是一个愚蠢的问题,但我无法弄清楚以下行为的基本规则:
foo :: t (f a) -> f a b -- accepted
foo = undefined
bar :: t [f a] -> f a b -- rejected
bar = undefined
Run Code Online (Sandbox Code Playgroud)
分别f应用到a和in会导致某种错误并因此被拒绝,这是完全有道理的。但为什么会被接受呢?a bbarfoo
是那种f.
由于返回类型f a b- 即f应用于两个参数, - 这意味着f :: Type -> Type -> Type.
但接下来f a是用作列表元素——[f a]而列表元素必须是Type,这意味着f a :: Type,这意味着f :: Type -> Type。
不匹配。
foo之所以有效,是因为类型可以部分应用。也就是说,如果f :: Type -> Type -> Type,那么f a :: Type -> Type。然后, typet允许有一个 kind 参数Type -> Type,所以一切都匹配。
重申以上内容:
foo之所以有效,是因为 typet可能有一个 kind 参数Type -> Type。bar不起作用,因为 type [](又名“list”)必须有一个 kind 参数Type。