我对以下代码中的类型检查器的行为感到困惑:
{-# LANGUAGE ScopedTypeVariables
, TypeFamilies , MultiParamTypeClasses, FlexibleContexts #-}
class (BarClass (Foo a) a)=> FooClass a where
type Foo a
foo :: Foo a -> a
-- THIS WORKS:
theBarFooOf :: a -> Bar (Foo a) a
foo = bar $ theBarFooOf (undefined :: a)
-- THIS DOES NOT:
-- theBarFoo :: Bar (Foo a) a
-- foo = bar (theBarFoo :: Bar (Foo a) a)
class BarClass t r where
type Bar t r
bar :: (Bar t r) -> t -> r
第二个版本(通过稍微简单的方法foo
实现theBarFoo
)会产生以下错误:
Could not deduce (Bar (Foo a) a ~ Bar (Foo a0) a0)
from the context (FooClass a)
bound by the class declaration for `FooClass'
at demo.hs:(5,1)-(15,42)
NB: `Bar' is a type function, and may not be injective
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Expected type: Bar (Foo a) a
Actual type: Bar (Foo a) a
In the first argument of `bar', namely
`(theBarFoo :: Bar (Foo a) a)'
In the expression: bar (theBarFoo :: Bar (Foo a) a)
In an equation for `foo': foo = bar (theBarFoo :: Bar (Foo a) a)
当Bar (Foo a) a
位于实例头的范围内时,为什么a
的值不明确,但这样可以正常工作:a -> Bar (Foo a) a
?这是一个错误吗?
我正在使用GHC 7.6.3。