[Haskell-cafe] Type question in instance of a class
J. Garrett Morris
jgmorris at cecs.pdx.edu
Sun Nov 16 16:56:02 EST 2008
On Sun, Nov 16, 2008 at 1:32 PM, Maurício <briqueabraque at yahoo.com> wrote:
> Hi,
>
> Why is this wrong?
>
> ----
> class MyClass r where function :: r -> s
>
> data MyData u = MyData u
>
> instance MyClass (MyData v) where function (MyData a) = a
> ----
>
> GHC says that the type of the result of 'function' is both determined by
> the "rigid type" from MyClass and the "rigid type" from MyData. But why
> can't both be the same?
As Bulat said, your type signature is equivalent to:
function :: forall r s. r -> s
whereas the result you're producing can't produce any s, but only
particular s's. In essence, the result type is determined by the
input type. One way to code this would be to use functional
dependencies:
class MyClass r s | r -> s where function :: r -> s
data MyData u = MyData u
instance MyClass (MyData v) v where function (MyData a) = a
/g
--
I am in here
More information about the Haskell-Cafe
mailing list