"Could not unambiguously deduce"..
Simon Peyton-Jones
simonpj@microsoft.com
Wed, 6 Nov 2002 16:51:09 -0000
| What I don't understand, however, is why adding that extra
| constraint helps in any way? Shouldn't the addition of new
| things in the context only make more options available? Why
| does it make some of the existing, amgibuous options go away?
With the extra constraint, the instance decl can build a C (T a)
dictionary from
a (C a, C [a]) dictionary, by choosing the expl method from the C [a]
dictionary it is passed (ignoring the C a one). That defers the choice
of which of the overlapping instance decls we are going to use. Perhaps
it'll be postponed to a point at which 'a' is known, in which case the
choice is easy. The point is that we don't want to have to choose
between the C String and C [a] instance decls until we know enough about
'a' to choose the right one.
Simon
| data T a =3D D [a]
|=20
| class C t where
| expl :: t -> String
| expl x =3D "default"
|=20
| instance C String where expl s =3D "String"
| instance C a =3D> C [a] where expl l =3D "[a]"
|=20
| instance (C a {- ,C [a] -} ) =3D> C (T a) where
| expl (D xs) =3D expl xs
|=20
| main =3D putStrLn $ expl "hi"
|=20
| ------------
|=20
| As is, both ghc and hugs reject the program, whereas
| both accept it with the extra constraint in the C (T a)
| instance.. Now, I think I can see how the right-hand-side
| expl could come either from the C String or from the C [a]
| instance - hence ghc's message:
|=20
| $ ghc --make Tst.hs
| c:\ghc\ghc-5.04\bin\ghc.exe: chasing modules from: Tst.hs
| Compiling Main ( Tst.hs, ./Tst.o )
|=20
| Tst.hs:15:
| Could not unambiguously deduce (C [a])
| from the context (C (T a), C a)
| The choice of (overlapping) instance declaration
| depends on the instantiation of `a'
| Probable fix:
| Add (C [a]) to the class or instance method `expl'
| Or add an instance declaration for (C [a])
| arising from use of `expl' at Tst.hs:15
| In the definition of `expl': expl xs
|=20
|=20
| Confused,
| Claus
|=20
| PS. Perhaps related, but why does Hugs seem to ignore the
| C a constraint in the context of the original version?
|=20
| $ hugs -98 Tst.hs
| __ __ __ __ ____ ___
| _________________________________________
| || || || || || || ||__ Hugs 98: Based on the Haskell 98
| standard
| ||___|| ||__|| ||__|| __|| Copyright (c) 1994-2001
| ||---|| ___|| World Wide Web:
| http://haskell.org/hugs
| || || Report bugs to:
| hugs-bugs@haskell.org
| || || Version: December 2001
| _________________________________________
|=20
| Hugs mode: Restart with command line option +98 for Haskell 98
mode
|=20
| Reading file "c:\Program Files\Hugs98\\lib\Prelude.hs":
| Reading file "Tst.hs":
| Type checking
| ERROR "Tst.hs":15 - Cannot justify constraints in instance member
| binding
| *** Expression : expl
| *** Type : C (T a) =3D> T a -> String
| *** Given context : C (T a)
| *** Constraints : C [a]
|=20
| Prelude>
| _______________________________________________
| Glasgow-haskell-bugs mailing list
| Glasgow-haskell-bugs@haskell.org
| http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs