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), as fromConstr signatur is: fromConstr :: forall a. (Data a) => Constr -> a I do not know where to add it either. Hope, somebody can enlighten me. Greetings, Mads Lindstrøm
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
Hi Wolfgang Thank you very much for the reply. I added an extra parameter to the function, so the code now looks like: foo :: (Data a) => a -> Constr -> Int foo idType = (numChildren idType) . fromConstr numChildren :: (Data a) => a -> a -> Int numChildren _ x = sum $ gmapQ (\_ -> 1) x and it works. However, it does seem ugly to have an extra argument, just to please the type checker. This extra argument can fortunately be limited to a few functions, in the program I am working on. /Mads Lindstrøm Wolfgang Jeltsch:
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 _______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
Am Freitag, 22. Juli 2005 15:56 schrieb Mads Lindstrøm:
Hi Wolfgang
Thank you very much for the reply. I added an extra parameter to the function, so the code now looks like:
foo :: (Data a) => a -> Constr -> Int foo idType = (numChildren idType) . fromConstr
numChildren :: (Data a) => a -> a -> Int numChildren _ x = sum $ gmapQ (\_ -> 1) x
and it works. However, it does seem ugly to have an extra argument, just to please the type checker.
But somehow you have to tell the compiler what type you want your argument be converted into. If this is always the same type, you can implement foo like this: foo arg = numChildren (fromConstr arg :: YourType)
[...]
Best wishes, Wolfgang
participants (2)
-
Mads Lindstrøm -
Wolfgang Jeltsch