I am struggling with ambiguous type variables in classes in Haskell:
This code brings my problem to the basics:
class Problem ab where foo :: a -> a bar :: b -> b baz :: a -> b -> (a, b) solve :: Problem ab => a -> b -> (a, b) solve ab = baz (foo a) (bar b)
It does not compile, saying that type variables that I do not use when calling 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 ab = 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 ab = baz (foo a) (bar b)
This works when I have three separate class classes for foo, bar and baz, and define the solution as:
solve :: FooProblem a => BarProblem b => BazProblem ab => a -> b -> (a, b)
But it is very cumbersome. Why do I have these three functions in one class? Well, they are all connected, and I always use all three together.
I tried putting type signatures around foo and bar and using forall (admittedly somewhat randomly through desperation). I looked over existing ambiguous type variables and donβt see the fix that matters to me.
How can i solve this?
Many thanks.
source share