
Hi, I get an error message when I compile the following program with ghc-6.4: \begin{code} class Catalog c where traverse :: c -> Viewer -> IO () instance Catalog Int where traverse i v = viewShowable v i type View a = a -> IO () data Viewer = Viewer { viewShowable :: forall s. Show s => View s, viewCatalog :: forall c. Catalog c => View c } printer :: Viewer --printer = Viewer { -- viewCatalog = \x -> traverse x printer, -- viewShowable = putStrLn . show } printer = Viewer { viewCatalog = printCatalog, viewShowable = putStrLn . show } printCatalog :: forall c. Catalog c => View c printCatalog x = traverse x printer data X = X { cat :: Int } instance Catalog X where traverse x v = do viewCatalog v (cat x) main = do let x = X { cat = 20 } traverse x printer \end{code} This is a stripped-down version of my actual code, BTW. The exact mesage is: ~> ghc -fallow-undecidable-instances -fglasgow-exts Bug.lhs Bug.lhs:23:10: Inferred type is less polymorphic than expected Quantified type variable `c' is mentioned in the environment: printCatalog :: c -> IO () (bound at Bug.lhs:28:0) In the `viewCatalog' field of a record In the record construction: Viewer {viewCatalog = printCatalog, viewShowable = putStrLn . show} In the definition of `printer': printer = Viewer {viewCatalog = printCatalog, viewShowable = putStrLn . show} Bug.lhs:27:0: Contexts differ in length When matching the contexts of the signatures for printer :: Viewer printCatalog :: forall c. (Catalog c) => View c The signature contexts in a mutually recursive group should all be identical The code compiles and works fine if the definition of 'printer' is replaced by the out-commented version, that is, if a lambda expression is used that is identical to the definition of 'printCatalog'. BTW, this does not happen with hugs. It looks like a bug to me, but since I use some non-standard features, maybe there is some subtle explanation for this behavior. Ben

Hi, This is not a bug, the restriction is actually mentioned in the Haskell Report, section 4.5.2. However, the restriction was recently lifted, so your code compiles fine with the current cvs ghc, see
http://www.haskell.org/pipermail/glasgow-haskell-users/2005-July/008786.html
Thomas
On 7/29/05, Benjamin Franksen
Bug.lhs:27:0: Contexts differ in length When matching the contexts of the signatures for printer :: Viewer printCatalog :: forall c. (Catalog c) => View c The signature contexts in a mutually recursive group should all be identical

On Saturday 30 July 2005 01:46, Thomas Jäger wrote:
This is not a bug, the restriction is actually mentioned in the Haskell Report, section 4.5.2. However, the restriction was recently lifted, so your code compiles fine with the current cvs ghc, see
http://www.haskell.org/pipermail/glasgow-haskell-users/2005-July/00 8786.html
Thomas
On 7/29/05, Benjamin Franksen
wrote: Bug.lhs:27:0: Contexts differ in length When matching the contexts of the signatures for printer :: Viewer printCatalog :: forall c. (Catalog c) => View c The signature contexts in a mutually recursive group should all be identical
Thank you for clarifying the cause of the second error message. Is there a connection to the first one? Note, I have a version of the program, where I only get the "Inferred type is less polymorphic than expected" error message and this one also disappears as soon as I use a lambda instead of a top-level function to initialize the record. Ben

Hi,
You're probably refering to the version where you omit the type
signatures of both 'printer' and 'printCatalog'. Here the situation is
different because the compiler needs to infer the types and not just
check. In the explicitely typed version, 'printCatalog' is used
polymorphically, i.e. the 'c' is instantiated to different types
during the recursive call. Thus the example needs polymorphic
recursion, for which type inference is known to be undecidable. It is
therefore reasonable that ghc (as well as hugs) can't compile the code
(in fact, I guess type inference will assume that both contexts are
equal).
Thomas
On 7/29/05, Benjamin Franksen
a connection to the first one? Note, I have a version of the program, where I only get the "Inferred type is less polymorphic than expected" error message and this one also disappears as soon as I use a lambda instead of a top-level function to initialize the record.
Ben

On Saturday 30 July 2005 03:54, Thomas Jäger wrote:
You're probably refering to the version where you omit the type signatures of both 'printer' and 'printCatalog'. Here the situation is different because the compiler needs to infer the types and not just check. In the explicitely typed version, 'printCatalog' is used polymorphically, i.e. the 'c' is instantiated to different types during the recursive call. Thus the example needs polymorphic recursion, for which type inference is known to be undecidable. It is therefore reasonable that ghc (as well as hugs) can't compile the code (in fact, I guess type inference will assume that both contexts are equal).
Hmm. I am not sure I understand this. Are you saying, that the second error causes the first one? Thus, everything is caused by 'printer' being recursively defined? Are contexts with differing length in a recursive definition ok in Hugs (as in the latest version of ghc) and therefore with Hugs I don't get the first message either? Ben

Hi,
On 7/30/05, Benjamin Franksen
On Saturday 30 July 2005 03:54, Thomas Jäger wrote:
You're probably refering to the version where you omit the type signatures of both 'printer' and 'printCatalog'. Here the situation is different because the compiler needs to infer the types and not just check. In the explicitely typed version, 'printCatalog' is used polymorphically, i.e. the 'c' is instantiated to different types during the recursive call. Thus the example needs polymorphic recursion, for which type inference is known to be undecidable. It is therefore reasonable that ghc (as well as hugs) can't compile the code (in fact, I guess type inference will assume that both contexts are equal).
Hmm. I am not sure I understand this. I just described the reason why the "inferred type..." error message appears when there are no type signatures given to either 'printer' or 'printCatalog'.
Are you saying, that the second error causes the first one? Thus, everything is caused by 'printer' being recursively defined? Yes, that's what I believe. I'm not sure why it shows the first error message at all; in my experience, strange things happen when ghc continues after an error message that you shouldn't pay too much attention to. Perhaps ghc-6.4 just assumes identical contexts in recursive groups without checking the type signatures.
Are contexts with differing length in a recursive definition ok in Hugs (as in the latest version of ghc) and therefore with Hugs I don't get the first message either? Yes.
Thomas
participants (2)
-
Benjamin Franksen
-
Thomas Jäger