Signature error because the order
Hi, I found than this piece of code causes an error in GHC ( * http://haskell.pastebin.com/m5e3f4a4c* ): data Tree a = Leaf a | Node (Tree a) (Tree a) --prettyShow :: (Show a, Num a) => Tree a -> String -- WORKS prettyShow :: (Num a, Show a) => Tree a -> String -- FAILS prettyShow (Leaf a) = show a prettyShow (Node a b) = (simple a) ++ (simple b) simple :: (Show a, Num a) => Tree a -> String simple (Leaf x) = prettyShow (Leaf x) simple x = "(" ++ prettyShow x ++ ")" With the error: test.lhs:14:0: Couldn't match expected type `Show a' against inferred type `Num a1' When matching the contexts of the signatures for prettyShow :: forall a. (Num a, Show a) => Tree a -> String simple :: forall a. (Show a, Num a) => Tree a -> String The signature contexts in a mutually recursive group should all be identical When generalising the type(s) for prettyShow, simple Failed, modules loaded: none. But if I change the order of *Show* and *Num* in the signature of * prettyShow* it works. It's a GHC problem? it's normal than signature is order-dependent? I use *GHC 6.10.3* Thanks, Luis Cabellos -- LC, ("There is no Dana, only Zuul." - Cazafantasmas [1984])
Hi Luis, Luis Cabellos wrote:
But if I change the order of *Show* and *Num* in the signature of /prettyShow/ it works.
It's a GHC problem? it's normal than signature is order-dependent?
I think this is on purpose. The error specifically says that the contexts must be identical (rather than containing the same classes in arbitrary). I suspect this is how Haskell98 has specified it. If you enable the RelaxedPolyRec language extension, this restriction no longer applies and your code compiles fine. Hope this helps, Martijn.
In Haskell, the context of all functions in a mutually recursive group must be identical. http://haskell.org/onlinereport/decls.html#generalization Simon From: haskell-bounces@haskell.org [mailto:haskell-bounces@haskell.org] On Behalf Of Luis Cabellos Sent: 14 January 2010 11:26 To: haskell@haskell.org Subject: [Haskell] Signature error because the order Hi, I found than this piece of code causes an error in GHC ( http://haskell.pastebin.com/m5e3f4a4c ): data Tree a = Leaf a | Node (Tree a) (Tree a) --prettyShow :: (Show a, Num a) => Tree a -> String -- WORKS prettyShow :: (Num a, Show a) => Tree a -> String -- FAILS prettyShow (Leaf a) = show a prettyShow (Node a b) = (simple a) ++ (simple b) simple :: (Show a, Num a) => Tree a -> String simple (Leaf x) = prettyShow (Leaf x) simple x = "(" ++ prettyShow x ++ ")" With the error: test.lhs:14:0: Couldn't match expected type `Show a' against inferred type `Num a1' When matching the contexts of the signatures for prettyShow :: forall a. (Num a, Show a) => Tree a -> String simple :: forall a. (Show a, Num a) => Tree a -> String The signature contexts in a mutually recursive group should all be identical When generalising the type(s) for prettyShow, simple Failed, modules loaded: none. But if I change the order of Show and Num in the signature of prettyShow it works. It's a GHC problem? it's normal than signature is order-dependent? I use GHC 6.10.3 Thanks, Luis Cabellos -- LC, ("There is no Dana, only Zuul." - Cazafantasmas [1984])
participants (3)
-
Luis Cabellos -
Martijn van Steenbergen -
Simon Peyton-Jones