
On Fri, 31 Aug 2001 12:56:32 +0300, Cagdas Ozgenc wrote:
I am a newbie, so I might be making nonsense.
You actually make much sense, and you betray your OO background (C++, maybe?). Programming in Haskell requires a very different way of thinking.
In the following definiton:
class Eq a where (==) :: a -> a -> Bool
Why do we conlude that both of the "a" refer to the same type?
In Haskell, a class is a very different concept than what you find in OO languages like C++. In OO languages, a class is the definition of a data type and its operations. In Haskell, it is an abstract set of operations but without a concrete data type. So: class Eq a where (==) :: a -> a -> Bool indicates that if a data type 'a' has been made an instance of 'Eq', then that data type 'a' has the operation (==) as defined in that instance. For example, if I define a data type: data MyEnum = Sel1 | Sel2 | Sel3 and then define the instance of Eq: instance Eq MyEnum where Sel1 == Sel1 = True Sel2 == Sel2 = True Sel3 == Sel3 = True _ == _ = False then it's the same as having defined, in C++, a function such as: bool operator==(MyEnum s1, MyEnum s2) { if (s1 == Sel1 && s2 == Sel1) return true; if (s1 == Sel2 && s2 == Sel2) return true; if (s1 == Sel3 && s2 == Sel3) return true; return false; } One advantage is that, in Haskell, you can specifically assert for instances of classes, while in C++ you have to rely in compiler errors that can be disorienting at times. For example, std::set<val> requires that the parameter type have a '<' operator, but you can't assert on it. In Haskell you can, using contexts: class Ord a where (<) :: a -> a -> Bool data Set a = Ord a => [...] I don't know if I'm making sense. I'm not that proficient in Haskell myself.
Apparently using different types that both belonging to class Eq results in an error. For example
65 == 'a'
As Ketil said, this can be done only using a non-standard extension of GHC. I believe there are a lot of people that wish it were standard, so I guess it'll be some day.
It doesn't make much sense at first, however, how do you go by implementing multiple polymorphism?
There are several kinds of polymorphism. For example, you could say Haskell supports multiple type polymorphism (as in multiple template arguments in C++).
Secondly, why does Haskell distinguish between a Type and a Class? There seems to be no benefit of such approach.
Oh, there's benefit. It does require a serious change in perspective, though. It can't be so easily explained.
Thirdly, is it possible to define meta-classes in Haskell?
Could you give an example of what you mean? If you mean meta-programming, no, Haskell doesn't really have any of that. You can do some things using polymorphic types, and the fact that Haskell is pure functional means that often the line between compile-time and run-time computations can blur a lot. Salutaciones, JCAB email: jcab@roningames.com ICQ: 101728263 The Rumblings are back: http://www.JCABs-Rumblings.com