RE: [Haskell-cafe] Is there anyone out there who can translate C# generics into Haskell?

[snip]
-- C#: interface IX1 { String foo1(int); } class IX1 obj where foo1 :: Int -> obj -> String
Yep...I think that's what I'd do....though I would have done... "foo1 :: obj -> Int -> String" Does that matter?
-- C#: interface IX2<A> { String foo2(A); } class IX2 obj a where foo2 :: a -> obj -> String
Ok same here
--C#: interface IX3<A> where A : IY { String foo3(A); } class IY a where {- ... -} class IY a => IX3 obj a where foo3 :: a -> obj -> String
Yep I think again I would have guessed at that
--C#: interface IX4<A> : IZ where A : IY class IZ a where {- ... -} class (IY a, IZ obj) => IX4 obj a where foo4 :: a -> obj -> String
Hmmmm...this would have been one of my guesses....but let me have a go...
This assumes your "objects" are immutable, otherwise you would have to return (obj,String) instead of just String and then you most likely want to use the state monad and "do notation" to make functional programming look more like imperative programming.
This is fine....my oop is largely immutable.
You really have to drop the OO way of thinking, which I find the hardest :) Haskell's type classes are more powerful in some sense than C# interfaces; for example, in C# you can't attach an interface to any class (take for example the Point struct), it has to be your own class, while in Haskell, you can implement an instance of a type class for any datatype!
OK but I was going to go onto Interface IX<A> where A : IX<A> { } And Interface IX where A : B { } Where I cannot see a way to do the above in Haskell at all....as interfaces effectively operator on type classes not types....which seems inherently more powerful But if these could be done in Haskell the see what could be made of stuff like....which is obviously problematic in C# it obviously doesn't work....but potentially does make 'sense'. Interface IX<A> : A { }
Hope this helps a bit. As I'm new myself to Haskell, so take this with a "grain of salt".
It does...I will have a go with your sample answers.
Once you bite the bullet, I found Haskell a lot more fun than C#, although C# of course comes with a gigantic .NET framework and tools...
I'm looking at Haskell because of the formality of it's type system....but I'm actually not convinced it is as powerful as an OO one....i.e. OO ones operatate principally (in Haskell speak) on "type classes" not "types"
Peter
-----Original Message----- From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of Nicholls, Mark Sent: Wednesday, January 02, 2008 5:41 PM To: haskell-cafe@haskell.org Subject: [Haskell-cafe] Is there anyone out there who can translate C# generics into Haskell? I'm trying to translate some standard C# constucts into Haskell... some of this seems easy.... Specifically 1) Interface IX { } 2) Interface IX<A> { } 3) Interface IX<A> Where A : IY { } 4) Interface IX<A> : IZ Where A : IY { } I can take a punt at the first 2....but then it all falls apart _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi Mark,
"foo1 :: Int -> obj -> String" Yep...I think that's what I'd do....though I would have done... "foo1 :: obj -> Int -> String" Does that matter?
Well, it's a good habit in Haskell to move the "most important" parameter to the end of the argument list. See e.g. http://www.haskell.org/haskellwiki/Parameter_order.
OK but I was going to go onto Interface IX<A> where A : IX<A> {} and Interface IX where A : B {}
No, I would not know how to that in Haskell using type classes. It seems Haskell does not allow cycles in type class definitions. But as I'm new, this does not mean it's not possible. It's more important to know *what* you are trying to do, than to give a solution in a different language, since OO and FP are kind of orthogonal languages.
Where I cannot see a way to do the above in Haskell at all....as interfaces effectively operator on type classes not types....which seems inherently more powerful
Yeah, kind of makes sense. I liked interfaces in C# a lot, but when I started doing everything with interfaces, I found the lack of support for "mixins" or "default implementations" problematic. This ended up in a lot of copy/paste or encapsulating the implementations into a static class with plain functions, a mess.
But if these could be done in Haskell the see what could be made of stuff like....which is obviously problematic in C# it obviously doesn't work....but potentially does make 'sense'. Interface IX<A> : A {}
Ah! That's one of the bigger restrictions in C# yes! C++ can do that; ATL uses it a lot, and I also like that approach. You can emulate "mixins" with that, and still stay in the single inheritance paradigm. In Haskell you don't do that at all of course, since you avoid thinking about "objects and inheritance" in the first place. OO is strange. They offer you the nice concept of inheritance, and then the guidelines tell you: "don't use too many levels of inheritance"... Although I've build huge projects using OO, it always felt a bit like unsafe hacking. I don't really have that feeling with Haskell, but that could also be because I'm too new to the language ;-)
I'm looking at Haskell because of the formality of it's type system....but I'm actually not convinced it is as powerful as an OO one....i.e. OO ones operatate principally (in Haskell speak) on "type classes" not "types"
Maybe you are right, I don't know, my theoritical skills are not high enough to answer that. Haskell just "feels" better to me, although the lack of a friendly productive IDE and large standard framework remains a bit of a burden. Good luck, Peter -----Original Message----- From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of Nicholls, Mark Sent: Wednesday, January 02, 2008 5:41 PM To: haskell-cafe@haskell.org Subject: [Haskell-cafe] Is there anyone out there who can translate C# generics into Haskell? I'm trying to translate some standard C# constucts into Haskell... some of this seems easy.... Specifically 1) Interface IX { } 2) Interface IX<A> { } 3) Interface IX<A> Where A : IY { } 4) Interface IX<A> : IZ Where A : IY { } I can take a punt at the first 2....but then it all falls apart _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Ahh ok I see what is meant by the parameter order.... -----Original Message----- From: Peter Verswyvelen [mailto:peter.vers@telenet.be] On Behalf Of Peter Verswyvelen Sent: 03 January 2008 12:02 To: Nicholls, Mark Cc: haskell-cafe@haskell.org Subject: RE: [Haskell-cafe] Is there anyone out there who can translate C# generics into Haskell? Hi Mark,
"foo1 :: Int -> obj -> String" Yep...I think that's what I'd do....though I would have done... "foo1 :: obj -> Int -> String" Does that matter?
Well, it's a good habit in Haskell to move the "most important" parameter to the end of the argument list. See e.g. http://www.haskell.org/haskellwiki/Parameter_order.
OK but I was going to go onto Interface IX<A> where A : IX<A> {} and Interface IX where A : B {}
No, I would not know how to that in Haskell using type classes. It seems Haskell does not allow cycles in type class definitions. But as I'm new, this does not mean it's not possible. It's more important to know *what* you are trying to do, than to give a solution in a different language, since OO and FP are kind of orthogonal languages.
Where I cannot see a way to do the above in Haskell at all....as interfaces effectively operator on type classes not types....which seems inherently more powerful
Yeah, kind of makes sense. I liked interfaces in C# a lot, but when I started doing everything with interfaces, I found the lack of support for "mixins" or "default implementations" problematic. This ended up in a lot of copy/paste or encapsulating the implementations into a static class with plain functions, a mess.
But if these could be done in Haskell the see what could be made of stuff like....which is obviously problematic in C# it obviously doesn't work....but potentially does make 'sense'. Interface IX<A> : A {}
Ah! That's one of the bigger restrictions in C# yes! C++ can do that; ATL uses it a lot, and I also like that approach. You can emulate "mixins" with that, and still stay in the single inheritance paradigm. In Haskell you don't do that at all of course, since you avoid thinking about "objects and inheritance" in the first place. OO is strange. They offer you the nice concept of inheritance, and then the guidelines tell you: "don't use too many levels of inheritance"... Although I've build huge projects using OO, it always felt a bit like unsafe hacking. I don't really have that feeling with Haskell, but that could also be because I'm too new to the language ;-)
I'm looking at Haskell because of the formality of it's type system....but I'm actually not convinced it is as powerful as an OO one....i.e. OO ones operatate principally (in Haskell speak) on "type classes" not "types"
Maybe you are right, I don't know, my theoritical skills are not high enough to answer that. Haskell just "feels" better to me, although the lack of a friendly productive IDE and large standard framework remains a bit of a burden. Good luck, Peter -----Original Message----- From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of Nicholls, Mark Sent: Wednesday, January 02, 2008 5:41 PM To: haskell-cafe@haskell.org Subject: [Haskell-cafe] Is there anyone out there who can translate C# generics into Haskell? I'm trying to translate some standard C# constucts into Haskell... some of this seems easy.... Specifically 1) Interface IX { } 2) Interface IX<A> { } 3) Interface IX<A> Where A : IY { } 4) Interface IX<A> : IZ Where A : IY { } I can take a punt at the first 2....but then it all falls apart _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

"Peter Verswyvelen"
Well, it's a good habit in Haskell to move the "most important" parameter to the end of the argument list. See e.g. http://www.haskell.org/haskellwiki/Parameter_order.
I must say I like these recommendations. As for the Data.Map examples, the parameter order was changed compared to the old FiniteMap, and I was sure there was some rationale given? -k -- If I haven't seen further, it is by standing in the footprints of giants

Should be straight forward....simplest example is... class A a data D = D1 instance A D fine.....D is declared to be a member of type class A.... what about..... class A a type T = (forall x.Num x=>x) instance A T error!... " Illegal polymorphic or qualified type: forall x. (Num x) => x In the instance declaration for `A T'" I am simply trying to state that all members of typeclass Num are of typeclass A.... Doesn't like it. Does this mean that instance only operates on 'atomic' (for want of a better word) types? -----Original Message----- From: Peter Verswyvelen [mailto:peter.vers@telenet.be] On Behalf Of Peter Verswyvelen Sent: 03 January 2008 12:02 To: Nicholls, Mark Cc: haskell-cafe@haskell.org Subject: RE: [Haskell-cafe] Is there anyone out there who can translate C# generics into Haskell? Hi Mark,
"foo1 :: Int -> obj -> String" Yep...I think that's what I'd do....though I would have done... "foo1 :: obj -> Int -> String" Does that matter?
Well, it's a good habit in Haskell to move the "most important" parameter to the end of the argument list. See e.g. http://www.haskell.org/haskellwiki/Parameter_order.
OK but I was going to go onto Interface IX<A> where A : IX<A> {} and Interface IX where A : B {}
No, I would not know how to that in Haskell using type classes. It seems Haskell does not allow cycles in type class definitions. But as I'm new, this does not mean it's not possible. It's more important to know *what* you are trying to do, than to give a solution in a different language, since OO and FP are kind of orthogonal languages.
Where I cannot see a way to do the above in Haskell at all....as interfaces effectively operator on type classes not types....which seems inherently more powerful
Yeah, kind of makes sense. I liked interfaces in C# a lot, but when I started doing everything with interfaces, I found the lack of support for "mixins" or "default implementations" problematic. This ended up in a lot of copy/paste or encapsulating the implementations into a static class with plain functions, a mess.
But if these could be done in Haskell the see what could be made of stuff like....which is obviously problematic in C# it obviously doesn't work....but potentially does make 'sense'. Interface IX<A> : A {}
Ah! That's one of the bigger restrictions in C# yes! C++ can do that; ATL uses it a lot, and I also like that approach. You can emulate "mixins" with that, and still stay in the single inheritance paradigm. In Haskell you don't do that at all of course, since you avoid thinking about "objects and inheritance" in the first place. OO is strange. They offer you the nice concept of inheritance, and then the guidelines tell you: "don't use too many levels of inheritance"... Although I've build huge projects using OO, it always felt a bit like unsafe hacking. I don't really have that feeling with Haskell, but that could also be because I'm too new to the language ;-)
I'm looking at Haskell because of the formality of it's type system....but I'm actually not convinced it is as powerful as an OO one....i.e. OO ones operatate principally (in Haskell speak) on "type classes" not "types"
Maybe you are right, I don't know, my theoritical skills are not high enough to answer that. Haskell just "feels" better to me, although the lack of a friendly productive IDE and large standard framework remains a bit of a burden. Good luck, Peter -----Original Message----- From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of Nicholls, Mark Sent: Wednesday, January 02, 2008 5:41 PM To: haskell-cafe@haskell.org Subject: [Haskell-cafe] Is there anyone out there who can translate C# generics into Haskell? I'm trying to translate some standard C# constucts into Haskell... some of this seems easy.... Specifically 1) Interface IX { } 2) Interface IX<A> { } 3) Interface IX<A> Where A : IY { } 4) Interface IX<A> : IZ Where A : IY { } I can take a punt at the first 2....but then it all falls apart _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Jan 10, 2008 1:03 PM, Nicholls, Mark
Should be straight forward....simplest example is...
class A a
data D = D1
instance A D
fine.....D is declared to be a member of type class A....
what about.....
class A a
type T = (forall x.Num x=>x)
instance A T
error!...
" Illegal polymorphic or qualified type: forall x. (Num x) => x In the instance declaration for `A T'"
I am simply trying to state that all members of typeclass Num are of typeclass A....
Ahh, you want: instance Num a => A a Sorry to lead you on, but that actually is not legal (and -fallow-undecidable-instances will make it legal, but you don't want that, because instances of this particular form are very likely to lead to an infinite loop). Adding supertypes like this is not possible in Haskell. I really want it to be, but alas... Luke
Doesn't like it.
Does this mean that instance only operates on 'atomic' (for want of a better word) types?
-----Original Message----- From: Peter Verswyvelen [mailto:peter.vers@telenet.be] On Behalf Of Peter Verswyvelen Sent: 03 January 2008 12:02 To: Nicholls, Mark Cc: haskell-cafe@haskell.org Subject: RE: [Haskell-cafe] Is there anyone out there who can translate C# generics into Haskell?
Hi Mark,
"foo1 :: Int -> obj -> String" Yep...I think that's what I'd do....though I would have done... "foo1 :: obj -> Int -> String" Does that matter?
Well, it's a good habit in Haskell to move the "most important" parameter to the end of the argument list. See e.g. http://www.haskell.org/haskellwiki/Parameter_order.
OK but I was going to go onto Interface IX<A> where A : IX<A> {} and Interface IX where A : B {}
No, I would not know how to that in Haskell using type classes. It seems Haskell does not allow cycles in type class definitions. But as I'm new, this does not mean it's not possible. It's more important to know *what* you are trying to do, than to give a solution in a different language, since OO and FP are kind of orthogonal languages.
Where I cannot see a way to do the above in Haskell at all....as interfaces effectively operator on type classes not types....which seems inherently more powerful
Yeah, kind of makes sense. I liked interfaces in C# a lot, but when I started doing everything with interfaces, I found the lack of support for "mixins" or "default implementations" problematic. This ended up in a lot of copy/paste or encapsulating the implementations into a static class with plain functions, a mess.
But if these could be done in Haskell the see what could be made of stuff like....which is obviously problematic in C# it obviously doesn't work....but potentially does make 'sense'. Interface IX<A> : A {}
Ah! That's one of the bigger restrictions in C# yes! C++ can do that; ATL uses it a lot, and I also like that approach. You can emulate "mixins" with that, and still stay in the single inheritance paradigm. In Haskell you don't do that at all of course, since you avoid thinking about "objects and inheritance" in the first place.
OO is strange. They offer you the nice concept of inheritance, and then the guidelines tell you: "don't use too many levels of inheritance"... Although I've build huge projects using OO, it always felt a bit like unsafe hacking. I don't really have that feeling with Haskell, but that could also be because I'm too new to the language ;-)
I'm looking at Haskell because of the formality of it's type system....but I'm actually not convinced it is as powerful as an OO one....i.e. OO ones operatate principally (in Haskell speak) on "type classes" not "types"
Maybe you are right, I don't know, my theoritical skills are not high enough to answer that. Haskell just "feels" better to me, although the lack of a friendly productive IDE and large standard framework remains a bit of a burden.
Good luck, Peter
-----Original Message----- From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of Nicholls, Mark Sent: Wednesday, January 02, 2008 5:41 PM To: haskell-cafe@haskell.org Subject: [Haskell-cafe] Is there anyone out there who can translate C# generics into Haskell?
I'm trying to translate some standard C# constucts into Haskell... some of this seems easy....
Specifically
1)
Interface IX { }
2)
Interface IX<A> { }
3)
Interface IX<A> Where A : IY { }
4)
Interface IX<A> : IZ Where A : IY { }
I can take a punt at the first 2....but then it all falls apart _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Thanks for your response, I think you helped me on one of my previous
abberations.
Hmmm....this all slightly does my head in....on one hand we have
types....then type classes (which appear to be a relation defined on
types)....then existential types...which now appear not to be treated
quite in the same way as 'normal' types....and in this instance the
syntax even seems to change....does
"instance Num a => A a"
Mean the same thing as
"instance A (forall a.Num a=>a)"
seems like a weird questions, as you're saying my version doesn't mean
anything....but
does it mean that "forall types 'a', if 'a' is a member of the class
Num, then 'a' is a member of class 'A'"....
and secondly in what way can this construct lead to "undecidable
instances"
What are the instances, and what about them is undecidable....seems
pretty decidable to me?
What is the ramifications of turning this option on?
-----Original Message-----
From: Luke Palmer [mailto:lrpalmer@gmail.com]
Sent: 10 January 2008 13:14
To: Nicholls, Mark
Cc: haskell-cafe@haskell.org
Subject: Re: [Haskell-cafe] confusion about 'instance'....
On Jan 10, 2008 1:03 PM, Nicholls, Mark
Should be straight forward....simplest example is...
class A a
data D = D1
instance A D
fine.....D is declared to be a member of type class A....
what about.....
class A a
type T = (forall x.Num x=>x)
instance A T
error!...
" Illegal polymorphic or qualified type: forall x. (Num x) => x In the instance declaration for `A T'"
I am simply trying to state that all members of typeclass Num are of typeclass A....
Doesn't like it.
Does this mean that instance only operates on 'atomic' (for want of a better word) types?
-----Original Message----- From: Peter Verswyvelen [mailto:peter.vers@telenet.be] On Behalf Of Peter Verswyvelen Sent: 03 January 2008 12:02 To: Nicholls, Mark Cc: haskell-cafe@haskell.org Subject: RE: [Haskell-cafe] Is there anyone out there who can
C# generics into Haskell?
Hi Mark,
"foo1 :: Int -> obj -> String" Yep...I think that's what I'd do....though I would have done... "foo1 :: obj -> Int -> String" Does that matter?
Well, it's a good habit in Haskell to move the "most important" parameter to the end of the argument list. See e.g. http://www.haskell.org/haskellwiki/Parameter_order.
OK but I was going to go onto Interface IX<A> where A : IX<A> {} and Interface IX where A : B {}
No, I would not know how to that in Haskell using type classes. It seems Haskell does not allow cycles in type class definitions. But as I'm new, this does not mean it's not possible. It's more important to know *what* you are trying to do, than to give a solution in a different language, since OO and FP are kind of orthogonal languages.
Where I cannot see a way to do the above in Haskell at all....as interfaces effectively operator on type classes not types....which seems inherently more powerful
Yeah, kind of makes sense. I liked interfaces in C# a lot, but when I started doing everything with interfaces, I found the lack of support for "mixins" or "default implementations" problematic. This ended up in a lot of copy/paste or encapsulating the implementations into a static class with plain functions, a mess.
But if these could be done in Haskell the see what could be made of stuff like....which is obviously problematic in C# it obviously doesn't work....but potentially does make 'sense'. Interface IX<A> : A {}
Ah! That's one of the bigger restrictions in C# yes! C++ can do that; ATL uses it a lot, and I also like that approach. You can emulate "mixins" with that, and still stay in the single inheritance paradigm. In Haskell you don't do that at all of course, since you avoid thinking about "objects and inheritance" in the first place.
OO is strange. They offer you the nice concept of inheritance, and
Ahh, you want: instance Num a => A a Sorry to lead you on, but that actually is not legal (and -fallow-undecidable-instances will make it legal, but you don't want that, because instances of this particular form are very likely to lead to an infinite loop). Adding supertypes like this is not possible in Haskell. I really want it to be, but alas... Luke translate then
the guidelines tell you: "don't use too many levels of inheritance"... Although I've build huge projects using OO, it always felt a bit like unsafe hacking. I don't really have that feeling with Haskell, but that could also be because I'm too new to the language ;-)
I'm looking at Haskell because of the formality of it's type system....but I'm actually not convinced it is as powerful as an OO one....i.e. OO ones operatate principally (in Haskell speak) on "type classes" not "types"
Maybe you are right, I don't know, my theoritical skills are not high enough to answer that. Haskell just "feels" better to me, although the lack of a friendly productive IDE and large standard framework remains a bit of a burden.
Good luck, Peter
-----Original Message----- From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of Nicholls, Mark Sent: Wednesday, January 02, 2008 5:41 PM To: haskell-cafe@haskell.org Subject: [Haskell-cafe] Is there anyone out there who can translate C# generics into Haskell?
I'm trying to translate some standard C# constucts into Haskell... some of this seems easy....
Specifically
1)
Interface IX { }
2)
Interface IX<A> { }
3)
Interface IX<A> Where A : IY { }
4)
Interface IX<A> : IZ Where A : IY { }
I can take a punt at the first 2....but then it all falls apart _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hello Mark, Thursday, January 10, 2008, 4:25:20 PM, you wrote: "instance Num a =>> A a"
Mean the same thing as
"instance A (forall a.Num a=>a)"
programmers going from OOP world always forget that classes in Haskell doesn't the same as classes in C++. *implementation* of this instance require to pass dictionary of Num class along with type. now imagine the following code: f :: A a => a -> a f cannot use your instance because it doesn't receive Num dictionary of type `a`. it is unlike OOP situation where every object carries the generic VMT which includes methods for every class/interface that object supports as usual, i suggest you to study http://haskell.org/haskellwiki/OOP_vs_type_classes first and especially two papers mentioned in References there -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

On Jan 10, 2008 1:36 PM, Bulat Ziganshin
Hello Mark,
Thursday, January 10, 2008, 4:25:20 PM, you wrote:
"instance Num a => A a"
Mean the same thing as
"instance A (forall a.Num a=>a)"
programmers going from OOP world always forget that classes in Haskell doesn't the same as classes in C++. *implementation* of this instance require to pass dictionary of Num class along with type. now imagine the following code:
f :: A a => a -> a
f cannot use your instance because it doesn't receive Num dictionary of type `a`. it is unlike OOP situation where every object carries the generic VMT which includes methods for every class/interface that object supports
I'm not sure that's a good argument. It doesn't need a Num dictionary, it only needs an A dictionary. That's what it says. You only need a Num dictionary in order to construct an A dictionary, which seems perfectly reasonable. Luke

-----Original Message----- From: Bulat Ziganshin [mailto:bulat.ziganshin@gmail.com] Sent: 10 January 2008 13:36 To: Nicholls, Mark Cc: Luke Palmer; haskell-cafe@haskell.org Subject: Re[2]: [Haskell-cafe] confusion about 'instance'....
Hello Mark,
Thursday, January 10, 2008, 4:25:20 PM, you wrote:
"instance Num a =>> A a"
Mean the same thing as
"instance A (forall a.Num a=>a)"
programmers going from OOP world always forget that classes in Haskell doesn't the same as classes in C++. *implementation* of this instance require to pass dictionary of Num class along with type. now imagine the following code:
My confusion is not between OO classes and Haskell classes, but exactly are the members of a Haskell type class...I'd naively believed them to be types (like it says on the packet!)...but now I'm not so sure.
f :: A a => a -> a
f cannot use your instance because it doesn't receive Num dictionary of type `a`. it is unlike OOP situation where every object carries the generic VMT which includes methods for every class/interface that object supports
as usual, i suggest you to study http://haskell.org/haskellwiki/OOP_vs_type_classes first and especially two papers mentioned in References there
I have done....learning is not an atomic operation....i.e. I can only believe what I understand...academic papers are especially beyond me at this point. I can translate OO into mathematical logic pretty easily, I was trying to do the same thing (informally of course) with Haskell....but things are not quite what they appear....not because of some OO hang up (which I probably have many)...but because of what "type class" actually means. So you may be right, I think I need to understand more about the sematics of Haskell...I was hoping to stay (initially) ignorant. I will try the postscript doc and see if it makes any sense.
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

On Jan 10, 2008 2:04 PM, Nicholls, Mark
I can translate OO into mathematical logic pretty easily, I was trying to do the same thing (informally of course) with Haskell....but things are not quite what they appear....not because of some OO hang up (which I probably have many)...but because of what "type class" actually means.
But you can think of a type class as a set of types! The problem is that if we allow certain kinds of instances (such as the Foo instance I gave earlier) then the set is allowed to be non-recursive (only recursively enumerable), so determining whether a particular type is a member of it would be undecidable. Luke

-----Original Message----- From: Luke Palmer [mailto:lrpalmer@gmail.com] Sent: 10 January 2008 14:12 To: Nicholls, Mark Cc: Bulat Ziganshin; haskell-cafe@haskell.org Subject: Re: Re[2]: [Haskell-cafe] confusion about 'instance'....
On Jan 10, 2008 2:04 PM, Nicholls, Mark
wrote: I can translate OO into mathematical logic pretty easily, I was
to do the same thing (informally of course) with Haskell....but
are not quite what they appear....not because of some OO hang up (which I probably have many)...but because of what "type class" actually means.
But you can think of a type class as a set of types! The problem is
Someone said something about having 2 instances of the type in the typeclass.....maybe I misinterpreted it. trying things that
if we allow certain kinds of instances (such as the Foo instance I gave earlier) then the set is allowed to be non-recursive (only recursively enumerable), so determining whether a particular type is a member of it would be undecidable.
Luke

Nicholls, Mark wrote:
My confusion is not between OO classes and Haskell classes, but exactly are the members of a Haskell type class...I'd naively believed them to be types (like it says on the packet!)...but now I'm not so sure.
Which packet? Classes are not types. Classes are groups of types. Sets of types. Classifications of types. For any type, you can ask the quesiton "is this type a member of this class, or not?" Without wishing to split hairs too finely, I find it a useful intuition not to consider the class context "part of the type" somehow. So, when you see this: (Num a, Eq b) => a -> b -> a Rather than thinking of that whole thing as a type, it helps to think of the part on the right of the => as the 'actual type' and the part on the left of the => as "some extra constraints on the type". So you might say this has the type "a -> b -> a", providing that a is a Num and b is an Eq. Jules

-----Original Message----- From: Jules Bean [mailto:jules@jellybean.co.uk] Sent: 10 January 2008 14:22 To: Nicholls, Mark Cc: Bulat Ziganshin; haskell-cafe@haskell.org Subject: Re: [Haskell-cafe] confusion about 'instance'....
Nicholls, Mark wrote:
My confusion is not between OO classes and Haskell classes, but
exactly
are the members of a Haskell type class...I'd naively believed them to be types (like it says on the packet!)...but now I'm not so sure.
Which packet?
The packet labelled "type class"....you're from .co.uk....you should understand my English idioms. :-)
Classes are not types.
Yep.
Classes are groups of types. Sets of types. Classifications of types.
I had them down as an n-ary relation on types....someone's said something somewhere that's made me question that...but I think I misinterpreted them....so I may default back to n-ary relation.
For any type, you can ask the quesiton "is this type a member of this class, or not?"
yep
Without wishing to split hairs too finely, I find it a useful
not to consider the class context "part of the type" somehow.
So, when you see this:
(Num a, Eq b) => a -> b -> a
Rather than thinking of that whole thing as a type, it helps to think of the part on the right of the => as the 'actual type' and the part on
intuition the
left of the => as "some extra constraints on the type".
Hmmm...I'm not sure that helps....it may just make me more confused.
So you might say this has the type "a -> b -> a", providing that a is
a
Num and b is an Eq.
Jules

Nicholls, Mark wrote:
Classes are groups of types. Sets of types. Classifications of types.
I had them down as an n-ary relation on types....someone's said something somewhere that's made me question that...but I think I misinterpreted them....so I may default back to n-ary relation.
Yes, and 1-ary relations are sets. And haskell98 only supports unary type classes, and all our discussion so far had been about unary type classes. But, since you mention it, yes MPTCs are n-ary relations. Jules

On 10 Jan 2008, at 6:04 AM, Nicholls, Mark wrote:
-----Original Message----- From: Bulat Ziganshin [mailto:bulat.ziganshin@gmail.com] Sent: 10 January 2008 13:36 To: Nicholls, Mark Cc: Luke Palmer; haskell-cafe@haskell.org Subject: Re[2]: [Haskell-cafe] confusion about 'instance'....
Hello Mark,
Thursday, January 10, 2008, 4:25:20 PM, you wrote:
"instance Num a =>> A a"
Mean the same thing as
"instance A (forall a.Num a=>a)"
programmers going from OOP world always forget that classes in Haskell doesn't the same as classes in C++. *implementation* of this instance require to pass dictionary of Num class along with type. now imagine the following code:
My confusion is not between OO classes and Haskell classes, but exactly are the members of a Haskell type class...I'd naively believed them to be types (like it says on the packet!)...but now I'm not so sure.
A type class *is* a set of types. But, in Haskell, types like (forall a. Num a => a) aren't quite first-class feeling. A typical example of an expression of this type might be (3 + 5), but if I say x :: Double x = 3 + 5 the compiler won't complain. Furthermore, if the compiler sees instance A Double where somewhere in the code, when it sees foo (3 + 5), for some method foo of the class, it may decide to take (3 + 5) :: Double, not (3 + 5) :: forall a. Num a => a. In that case, you'll get the wrong methods called: class A a where foo :: a -> String instance A Double where foo x = "Double" instance A (forall a. Num a => a) where foo x = "number" If the compiler sees the first instance but not the second, then it will think that foo (3 + 5) = "Double". Adding the second will give foo (3 + 5) = "number". Haskell 98's rules for type classes are chosen so that legal code never changes its meaning when you add an instance (well, this is a bad example --- but the general point is sound). GHC relaxes these rules in quite a few cases, but in this one it's easy enough (in GHC) to get a type isomorphic to forall a. Num a => a that can be an instance of a type class that GHC hasn't bothered relaxing this particular rule. (And paying the subsequent cost in confusion when working code bitrots because somebody added an instance somewhere). jcc

On Jan 10, 2008 1:25 PM, Nicholls, Mark
Thanks for your response, I think you helped me on one of my previous abberations.
Hmmm....this all slightly does my head in....on one hand we have types....then type classes (which appear to be a relation defined on types)....then existential types...which now appear not to be treated quite in the same way as 'normal' types....and in this instance the syntax even seems to change....does
"instance Num a => A a"
Mean the same thing as
"instance A (forall a.Num a=>a)"
Uh... that second one is pretty much nonsensical to me. I could imagine it meaning the type (forall a.Num a => a) itself is an instance of A, but not specializations of it (like Int). But without an identity in the type system, the meaning of that would be convoluted. It's kind of off topic, but just for the interested, here are two similar, legal constructions: Existential: newtype Numeric = forall a. Num a => Numeric a Universal: newtype Numeric' = Numeric' (forall a. Num a => a) Both of which are easily declared to be instances of Num. They're not what you want though, because Haskell doesn't support what you want :-(. Anyway, if you have a value of type Numeric, you know it contains some value of a Num type, but you don't know what type exactly (and you can never find out). If you have a value of type Numeric', then you can produce a value of any Num type you please (i.e. the value is built out of only operations in the Num class, nothing more specific). But that was a digression; ignore at your leisure (now that you've already read it :-).
and secondly in what way can this construct lead to "undecidable instances"
Okay, read: instance A a => B b (where a and be might be more complex expressions) not as "b is an instance of B whenever a is an instance of A", but rather as "b is an instance of B, and using it as such adds a constraint of A a". Let's look at a slightly more complex (and contrived) example: class Foo a where foo :: a -> a instance (Foo [a]) => Foo a where foo x = head $ foo [x] Then when checking the type of the expression foo (0::Int), we'd have to check if Foo Int, Foo [Int], Foo [[Int]], Foo [[[Int]]], ad infinitum.
What are the instances, and what about them is undecidable....seems pretty decidable to me?
What is the ramifications of turning this option on?
Theoretically, compile time fails to guarantee to ever finish. Practically, ghc will give you a very-difficult-to-reason-about message when constraint checking stack overflows. Luke

Thanks for your response, I think you helped me on one of my previous abberations.
Hmmm....this all slightly does my head in....on one hand we have types....then type classes (which appear to be a relation defined on types)....then existential types...which now appear not to be treated quite in the same way as 'normal' types....and in this instance the syntax even seems to change....does
"instance Num a => A a"
Mean the same thing as
"instance A (forall a.Num a=>a)"
Uh... that second one is pretty much nonsensical to me. I could imagine it meaning the type (forall a.Num a => a) itself is an instance of A, but not specializations of it (like Int). But without an identity in the type system, the meaning of that would be convoluted. It's kind of off topic, but just for the interested, here are two similar, legal constructions:
ok
Existential: newtype Numeric = forall a. Num a => Numeric a
My compiler doesn't like this...." A newtype constructor cannot have an existential context,"
Universal: newtype Numeric' = Numeric' (forall a. Num a => a)
Not so sure I understand the difference here.....
Both of which are easily declared to be instances of Num. They're not what you want though, because Haskell doesn't support what you want :-(. Anyway, if you have a value of type Numeric, you know it contains some value
of a
Num type, but you don't know what type exactly (and you can never find out). If you have a value of type Numeric', then you can produce a value of any Num type you please (i.e. the value is built out of only operations in the Num class, nothing more specific).
But that was a digression; ignore at your leisure (now that you've already read it :-).
Makes little sense to me...."Numeric" looks reasonable...I think... "Numeric'"...seems weird....and I'm not sure I understood the explanation.
and secondly in what way can this construct lead to "undecidable instances"
Okay, read:
instance A a => B b
(where a and be might be more complex expressions) not as "b is an instance of B whenever a is an instance of A", but rather as "b is an instance of
B,
and using it as such adds a constraint of A a". Let's look at a slightly more complex (and contrived) example:
class Foo a where foo :: a -> a
instance (Foo [a]) => Foo a where foo x = head $ foo [x]
Then when checking the type of the expression foo (0::Int), we'd have to check if Foo Int, Foo [Int], Foo [[Int]], Foo [[[Int]]], ad infinitum.
Ooo blimey....that sort of makes sense.
What are the instances, and what about them is undecidable....seems pretty decidable to me?
What is the ramifications of turning this option on?
Theoretically, compile time fails to guarantee to ever finish. Practically, ghc will give you a very-difficult-to-reason-about message when
constraint
checking stack overflows.
Luke

On Thu, 10 Jan 2008, Nicholls, Mark wrote:
Existential: newtype Numeric = forall a. Num a => Numeric a
My compiler doesn't like this...." A newtype constructor cannot have an existential context,"
Universal: newtype Numeric' = Numeric' (forall a. Num a => a)
Not so sure I understand the difference here.....
Looks like http://www.haskell.org/haskellwiki/Generic_number_type

Nicholls, Mark wrote:
Thanks for your response, I think you helped me on one of my previous abberations.
Hmmm....this all slightly does my head in....on one hand we have types....then type classes (which appear to be a relation defined on types)....then existential types...which now appear not to be treated quite in the same way as 'normal' types....and in this instance the syntax even seems to change....does
"instance Num a => A a"
This means: Given any type "a". Any type at all. Yes, ANY type "a", we accept that it might be an instance of A, and we add a "Num" context to the current inference. So, supposing: f :: (A a) => a -> b and we're trying to type check: f x We first try to unify x's type with the type variable "a", which is easy. Then we impose the constraint "A a". At some later stage we will try to resolve this constraint. When we try to resolve it, we find that all types "a" are instances of A, but you have to add a Num constraint. So we add the Num constraint. This behaviour is not what everyone wants, but it is a consequence of they type classes are specified. GHC lets you turn off this behaviour somewhat with overlapping and undecidable instances but that's not really an ideal solution either. The ideal solution to this precise case is probably just to make the Num class a superclass of A. That seems to do what you want. Jules
participants (8)
-
Bulat Ziganshin
-
Henning Thielemann
-
Jonathan Cast
-
Jules Bean
-
Ketil Malde
-
Luke Palmer
-
Nicholls, Mark
-
Peter Verswyvelen