Problems instancing a class

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Problems instancing a class

Juan Carlos Arevalo Baeza-2
   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

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Problems instancing a class

Roberto Zunino
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#type-synonyms

 >   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.
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Problems instancing a class

Jared Updike
> "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#type-synonyms
>

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-INSTANCES

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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Problems instancing a class

Gerrit van den Geest
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#type-synonyms
>

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-INSTANCES

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
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Problems instancing a class

Juan Carlos Arevalo Baeza-2
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#type-synonyms 
>>

   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 </trac/haskell-prime/wiki/FlexibleInstances> or
UndecidableInstances </trac/haskell-prime/wiki/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

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Problems instancing a class

Ben Franksen-2
In reply to this post by Gerrit van den Geest
On Friday 17 February 2006 21:03, Juan Carlos Arevalo Baeza wrote:

>    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".

(Guessing) Maybe because 'String' is not a type variable?

>    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?

What is (or better: might be) non-terminating is the process (algorithm)
of /type inference/. This:

http://www.haskell.org//pipermail/haskell-prime/2006-February/000609.html

message contains a detailed explanation.

Ben
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Problems instancing a class

Stefan Holdermans
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

>> 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".
>
> (Guessing) Maybe because 'String' is not a type variable?

*Exactly* because of that.

Cheers,

   Stefan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Darwin)

iD8DBQFD93DyX0lh0JDNIpwRAuuWAJ9tT1n0RqDTP7yfJKGLLVDv6Nxf6wCfX6um
NsEcRI/1PSif8RVkK28SOjI=
=AhuP
-----END PGP SIGNATURE-----
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe