I have read many of the other ambiguous type variable questions on the site but was not able to find a solution to the following issue, although I am new to Haskell so may just not have understood the answers to other questions properly. Simplifying to only include the relevant parts of these definitions, I have a type class
class Dist a where
un :: a
and I have a multiparameter type class
class Dist a => UnBin a b where
opu :: b -> b
opb :: a -> b -> a
where in general a
and b
are completely unrelated. However, I also have a data type
data LC a b = LC [(a,b)]
and I make it an instance of UnBin
using
instance Dist a => UnBin a (LC [(a,b)]) where
opu = ...
opb = ...
where in this particular case, the type LC [(a,b)]
already depends on a
and I want the two a
s here to be the same.
What I mean by the two a
s should be the same is that if I define x = LC [(1 :: Int,'a')]
for example (assuming I have made Int
a part of the Dist
typeclass already) then I would want to be able to just write opb un x
and have Haskell automatically infer that I want un :: Int
since a
is already determined by the type of x
however unless I explicitly use a type signature for un
and write opb (un::Int) x
I get the error
Ambiguous type variable ‘a0’ arising from a use of ‘un’
prevents the constraint ‘(Dist a0)’ from being solved.
Probable fix: use a type annotation to specify what ‘a0’ should be.
These potential instance exist:
one instance involving out-of-scope types
(use -fprint-potential-instances to see them all)
Presumably this means that Haskell is treating the two a
s in my instance declaration as unrelated when I want them to really be the same. Is there a way to make Haskell automatically able to infer the correct type of un
without having to give a type signature every time I want to use un
in this way?