type error
Simon Peyton-Jones
simonpj@microsoft.com
Thu, 12 Jun 2003 12:17:52 +0100
You have two instance declarations:
instance SubType a (Either a x) where
instance (SubType a b) =3D> SubType a (Either x b) where
Which one should GHC use to resolve the constraint?
(SubType x Value)
It can't make an unambiguous choice, hence the error message.
Simon
| -----Original Message-----
| From: glasgow-haskell-users-admin@haskell.org
[mailto:glasgow-haskell-users-admin@haskell.org]
| On Behalf Of Ed Komp
| Sent: 02 June 2003 15:43
| To: glasgow-haskell-users@haskell.org
| Subject: type error
|=20
|=20
| I have encountered an unexpected type error of the form:
|=20
| >> Could not unambiguously deduce <type> from the context
<context>
|=20
| I was able to generate a quite simple test case to demonstrate
| the problem, that I include at the end of this message.
|=20
| I am using Version 5.04.2 of the Glasgow Compiler, with options:
|=20
| -fglasgow-exts -fallow-overlapping-instances
-fallow-undecidable-instances
|=20
| Here is the output generated when I attempt to load the file, test.hs,
| (test.hs appears at the end of this message):
|=20
| >> fleck [61] % ghci --version
| >> The Glorious Glasgow Haskell Compilation System, version 5.04.2
| >> fleck [62] % ghci -fglasgow-exts -fallow-overlapping-instances
| -fallow-undecidable-instances test.hs
| >> ___ ___ _
| >> / _ \ /\ /\/ __(_)
| >> / /_\// /_/ / / | | GHC Interactive, version 5.04.2, for
Haskell 98.
| >> / /_\\/ __ / /___| | http://www.haskell.org/ghc/
| >> \____/\/ /_/\____/|_| Type :? for help.
| >>
| >> Loading package base ... linking ... done.
| >> Loading package haskell98 ... linking ... done.
| >> Compiling Test ( test.hs, interpreted )
| >>
| >> test.hs:26:
| >> Could not unambiguously deduce (SubType x Value)
| >> from the context (SubType x BaseType)
| >> The choice of (overlapping) instance declaration
| >> depends on the instantiation of `x'
| >> Probable fix:
| >> Add (SubType x Value)
| >> to the existential context of a data constructor
| >> Or add an instance declaration for (SubType x Value)
| >> arising from use of `inj' at test.hs:26
| >> In the definition of `test': inj x
| >> Failed, modules loaded: none.
| >> Prelude>
|=20
| I do not understand why the compiler cannot deduce
| (SubType x Value) from (SubType x BaseType)
|=20
| since:
|=20
| type Value =3D (Either Double BaseType)
|=20
| and
| instance (SubType a b) =3D> SubType a (Either x b) where
|=20
| The message actually says it cannot "unambiguously" make the
| deduction.
|=20
| Can someone explain to me why this is, and more importantly
| how I can resolve the issue?
|=20
| The suggestion,
| >> Add (SubType x Value)
| >> to the existential context of a data constructor
|=20
| will work in this simple example (I tested this);
| but in the code where I originally encountered the problem
| this is not a viable alternative since it would generate
| circular dependencies.
|=20
| Thanks in advance for your help !
|=20
| Ed Komp
| komp@ittc.ku.edu
|=20
| -- -----------------------------------------------------------
| -- test.hs
| -- -----------------------------------------------------------
| module Test where
|=20
| import Control.Monad.Error
|=20
| class SubType a b where
| inj :: a -> b
| prj :: b -> Maybe a
|=20
| instance SubType a (Either a x) where
| inj =3D Left
| prj =3D either Just (const Nothing)
|=20
| instance (SubType a b) =3D> SubType a (Either x b) where
| inj =3D Right . inj
| prj =3D either (const Nothing) prj
|=20
| -- -----------------------------------------------------------
|=20
| type BaseType =3D Either Integer ( Either Bool () )
|=20
| type Value =3D (Either Double BaseType)
|=20
| data Foo =3D forall x. (SubType x BaseType) =3D> MkFoo x
|=20
| test :: Foo -> Value
| test (MkFoo x) =3D inj x
|=20
| _______________________________________________
| Glasgow-haskell-users mailing list
| Glasgow-haskell-users@haskell.org
| http://www.haskell.org/mailman/listinfo/glasgow-haskell-users