TypeFamilies and ScopedTypeVariables inconsistency in default method
-
30-06-2022 - |
سؤال
I'm confused about the behavior of the typechecker in the following code:
{-# 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
The second version (implementing foo
via a slightly simpler method, theBarFoo
) generates the following error:
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)
Why should a value of Bar (Foo a) a
be ambiguous when a
is in scope from the instance head, yet this works fine: a -> Bar (Foo a) a
? Is this a bug?
I'm using GHC 7.6.3.
لا يوجد حل صحيح
لا تنتمي إلى StackOverflow