Problems instancing a class

So, consider this code: import Data.HashTable as HT class MyClass a where htLookup :: a -> String -> IO String type Context = HT.HashTable String String instance MyClass Context where htLookup h var = do result <- HT.lookup h var case result of Nothing -> return "" Just s -> return s This doesn't compile. GHC says: Illegal instance declaration for `MyClass Context' (The instance type must be of form (T a b c) where T is not a synonym, and a,b,c are distinct type variables) In the instance declaration for `MyClass Context' If I use "data" instead of "type", it works: data Context = C (HT.HashTable String String) instance MyClass Context where htLookup (C h) var = do result <- HT.lookup h var case result of Nothing -> return "" Just s -> return s Why? What's going on here? JCAB

Juan Carlos Arevalo Baeza wrote:
type Context = HT.HashTable String String [snip] Illegal instance declaration for `MyClass Context' (The instance type must be of form (T a b c) where T is not a synonym, and a,b,c are distinct type variables) In the instance declaration for `MyClass Context'
"type" introduce a type synonym, and Haskell98 forbids these in instances, so GHC complains. GHC also lifts this restriction when invoked with -fglasgow-exts . http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html...
If I use "data" instead of "type", it works:
data Context = C (HT.HashTable String String)
This is fine: data (and newtype) declarations do not introduce type synonyms, but a genuine new type. Regards, Roberto Zunino.

"type" introduce a type synonym, and Haskell98 forbids these in instances, so GHC complains. GHC also lifts this restriction when invoked with -fglasgow-exts . http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html...
Flexible Instances will probably be added to HaskellPrime: http://hackage.haskell.org/trac/haskell-prime/wiki/FlexibleInstances This page: http://cvs.haskell.org/Hugs/pages/users_guide/class-extensions.html#FLEXIBLE... says that ultimately you would turn the type language pretty much into Prolog (which would allow more expressive power---and less inconvenience as we both would like--but make general type checking undecidable). Instead they do a more conservative extension with a fixed depth of constraints it will check so the compiler will terminate. Jared.

Mark Mark Jones has (some time ago) also written a very detailed e-mail about this topic: http://www.haskell.org/pipermail/haskell/2000-October/006128.html Grt
"type" introduce a type synonym, and Haskell98 forbids these in instances, so GHC complains. GHC also lifts this restriction when invoked with -fglasgow-exts . http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html...
Flexible Instances will probably be added to HaskellPrime: http://hackage.haskell.org/trac/haskell-prime/wiki/FlexibleInstances This page: http://cvs.haskell.org/Hugs/pages/users_guide/class-extensions.html#FLEXIBLE... says that ultimately you would turn the type language pretty much into Prolog (which would allow more expressive power---and less inconvenience as we both would like--but make general type checking undecidable). Instead they do a more conservative extension with a fixed depth of constraints it will check so the compiler will terminate. Jared. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Gerrit van den Geest wrote:
Mark Mark Jones has (some time ago) also written a very detailed e-mail about this topic:
http://www.haskell.org/pipermail/haskell/2000-October/006128.html
I really don't understand anything spoken about in this message. I guess I need it translated into plain English :) I'd contend that, if Haskell is so good (and I believe it is), it should be more accessible to your average Joe, even if some might disagree.
Grt
"type" introduce a type synonym, and Haskell98 forbids these in instances, so GHC complains. GHC also lifts this restriction when invoked with -fglasgow-exts . http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html...
Thanx all for the replies. And here I thought I was compiling with extensions enabled. Blame it on using a compile script. Not that I had made the connection with this error... What really got me is that if I don't use a type synonym: instance MyClass (HT.HashTable String String) where htLookup h var = do result <- HT.lookup h var case result of Nothing -> return "" Just s -> return s it still complains: Illegal instance declaration for `MyClass (HashTable String String)' (The instance type must be of form (T a b c) where T is not a synonym, and a,b,c are distinct type variables) In the instance declaration for `MyClass (HashTable String String)' even though the documentation in GHC states that HashTable is declared as "data". I haven't had time to read all the documentation you guys have forwarded to me, but still, I have one question. I was reading here: http://hackage.haskell.org/trac/haskell-prime/wiki/TypeSynonymInstances and it says "The proposal is to allow type synonyms (fully applied, as ever) in instance heads. These would be fully expanded before any other restrictions on instance heads were checked", which is what sounds logical to me. And then it says "Not very useful without either FlexibleInstances or UndecidableInstances ". I don't understand why. This extension would not depend on those other more involved extensions. Later down it says "Cons - Since constraints on the instance head refer to the expanded type, errors can be more obscure". Is that the reason? Because the other extensions would make errors clearer? I'd like to ask someone in the know to provide a good example that shows the reasons behind those statements are there. Preferably added to the Wiki :) How exactly is type undecidable if this is allowed? I found here: http://hackage.haskell.org/trac/haskell-prime/wiki/FlexibleInstances an example of a non-terminating instance: instance C b b => C (Maybe a) b I don't see how/where/when this would be non-terminating. I mean... if you now define an instance "C Int Int", then you actually also have "C (Maybe Int) Int", C (Maybe (Maybe Int)) Int" and so on, I can see that. But how exactly is that a problem? Can you show me a use of C that cannot work with that instance? Thanx! JCAB
participants (4)
-
Gerrit van den Geest
-
Jared Updike
-
Juan Carlos Arevalo Baeza
-
Roberto Zunino