Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous type variables in Haskell when using multiple parameter type classes

Tags:

haskell

I'm struggling with ambiguous type variables in typeclasses in Haskell:

This code boils down my problem to the basics:

class Problem a b where
    foo :: a -> a
    bar :: b -> b
    baz :: a -> b -> (a, b)

solve :: Problem a b => a -> b -> (a, b)
solve a b = baz (foo a) (bar b)

It doesn't compile, saying that the type variables that I'm not using when I call foo and bar are ambiguous:

    Ambiguous type variable `b0' in the constraint:
      (Problem a b0) arising from a use of `foo'
    Probable fix: add a type signature that fixes these type variable(s)
    In the first argument of `baz', namely `(foo a)'
    In the expression: baz (foo a) (bar b)
    In an equation for `solve': solve a b = baz (foo a) (bar b)

    Ambiguous type variable `a0' in the constraint:
      (Problem a0 b) arising from a use of `bar'
    Probable fix: add a type signature that fixes these type variable(s)
    In the second argument of `baz', namely `(bar b)'
    In the expression: baz (foo a) (bar b)
    In an equation for `solve': solve a b = baz (foo a) (bar b)

It works when I have three separate type classes for foo, bar and baz, and define solve as:

solve :: FooProblem a => BarProblem b => BazProblem a b => a -> b -> (a, b)

But this is very cumbersome. Why do I have these three functions in one typeclass in the first place? Well they're all related and I always use all three together.

I've tried putting type signatures around foo and bar and using forall (admittedly, somewhat at random through desperation). I've looked at the existing ambiguous type variable questions and don't see a fix that is relevant to me.

How can I solve this?

Thanks very much.

like image 671
john banson Avatar asked Sep 05 '11 11:09

john banson


1 Answers

From the use of foo, the compiler can't know which instance of Problem to choose. There might be several instances with the same a but different bs. You can tell the compiler that this isn't the case with functional dependencies, but in this case I think there's a simpler solution:

class (FooProblem a, BarProblem b) => BazProblem a b where
  baz :: a -> b -> (a, b)

Then the type for solve is clean:

solve :: BazProblem a b => a -> b -> (a, b)
like image 192
Sjoerd Visscher Avatar answered Nov 09 '22 23:11

Sjoerd Visscher