Juan Carlos Arévalo Baeza
Fri, 31 Aug 2001 11:53:16 -0700
On Fri, 31 Aug 2001 12:56:32 +0300, Cagdas Ozgenc wrote:
>I am a newbie, so I might be making =A0nonsense.
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 =A0 =A0 (=3D=3D) :: a ->=A0a ->=A0 Bool
>Why do we conlude that both of the "a" refer to the =A0same 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.=
class Eq a where =A0 =A0 (=3D=3D) :: a ->=A0a ->=A0 Bool
indicates that if a data type 'a' has been made an instance of=
'Eq', then that data type 'a' has the operation (=3D=3D) as defined=
in that instance. For example, if I define a data type:
data MyEnum =3D Sel1 | Sel2 | Sel3
and then define the instance of Eq:
instance Eq MyEnum where
Sel1 =3D=3D Sel1 =3D True
Sel2 =3D=3D Sel2 =3D True
Sel3 =3D=3D Sel3 =3D True
_ =3D=3D _ =3D False
then it's the same as having defined, in C++, a function such=
bool operator=3D=3D(MyEnum s1, MyEnum s2)
if (s1 =3D=3D Sel1 && s2 =3D=3D Sel1) return true;
if (s1 =3D=3D Sel2 && s2 =3D=3D Sel2) return true;
if (s1 =3D=3D Sel3 && s2 =3D=3D Sel3) return true;
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=
class Ord a where
(<) :: a -> a -> Bool
data Set a =3D Ord a =3D> [...]
I don't know if I'm making sense. I'm not that proficient in=
>Apparently using different types that both belonging to class=
>results in an error. For example
>65 =3D=3D '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 =A0do 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 =A0Type and a=
>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 =A0Haskell?
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.
The Rumblings are back: http://www.JCABs-Rumblings.com