[Haskell-cafe] confusion about 'instance'....

Nicholls, Mark Nicholls.Mark at mtvne.com
Thu Jan 10 08:49:18 EST 2008


> > class A a
> > type T = (forall x.Num x=>x)
> > instance A T
> 
> "type" declares a synonym, like #define in C - but working only on
types.
> So, essentially, you wrote

Yep that's fine..

> 
> instance A (forall x.Num x => x)
> 

Yep

> 
> which is not very Haskelly.
> 

Hmmm...
 
> 
> 
> > I am simply trying to state that all members of typeclass Num are of
> 
> > typeclass A....
> 
> 
> You can't do that. 

Because it wont let me...or because it makes no sense?

> But, if there would not be any other instances of A,
> then you don't need it at all, you can just use Num class. And if
there
> are some, for example

Ok but there may be....I'm just trying to get my head around Haskells
type system.... 

> 
> data D = D
> 
> instance A D
> 
> 

Yep.

D is a member of A

> 
> then it can happen (well, it's unlikely, but possible) that you or
some
> other developer working on your code would declare
> 
> instance Num D where ...
> 

D is a member of Num

(and I'm assuming that we've got....All Nums are also members of
A....which is fine...so far).

So...Num x implies A x....
So...D is a member of A....

Fine.

> 
> 
> After that, you would have two instances of A for the type D, one
defined
> explicitly and one derived from the Num instance. 

I would have 2 declarations that D is a member of A....both consistent.

> That's not a problem for
> this empty class, but if class A is not empty, say
> 
> 
> 
> class A x where a :: x
> 
> 
> 
> then the compiler would be unable to decide which instance (and which
"a")
> to choose. So allowing that leads to non-obvious bugs.
> 

This slightly bamboozles me..... 
I only have 1 type.

If I say "my name is mark" twice, it doesn't mean I belong to set of
objects called Mark twice.... 

which makes me think that type classes are not simple relations on types
after all....they appear to be relations on declarations of types being
members of a class.

So in my example...there exists two instances of me claiming my name is
Mark.


> 
> 
> However, if you trust yourself enough, you can do what you want to in
this
> way:
> 
> 
> 
> {-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-}
> 
> class A a
> 
> instance Num a => A a

Hmmm...OK...

It all seems a little odd....


More information about the Haskell-Cafe mailing list