[Haskell] Ambiguity problem

Wolfgang Jeltsch wolfgang at jeltsch.net
Fri Jul 22 09:25:58 EDT 2005


Am Freitag, 22. Juli 2005 14:58 schrieb Mads Lindstrøm:
> Hi
>
> I am struggling with an ambiguity problem. I have the following code:
> > foo :: Constr -> Int
> > foo = numChildren . fromConstr
> >
> > numChildren :: (Data a) => a -> Int
> > numChildren x = sum $ gmapQ (\_ -> 1) x
>
> which I thought would work, but the GHC-6.4 says:
> > Ambiguous type variable `a' in the constraint:
> > `Data a' arising from use of `numChildren' at AutoForm.hs:87:6-16
> > Probable fix: add a type signature that fixes these type variable(s)
>
> I cannot see why I need to add (Data a),

(Data a) is a context, not a type signature.

I think, the problem is that fromConstr converts to a value of an unknown type 
which is converted by numChildren afterwards.  The compiler cannot know which 
type it has to use for the intermediate value, i.e., which concrete 
implementation of numChildren and fromConstr it has to use.

> [...]

Best wishes,
Wolfgang


More information about the Haskell mailing list