type class VS struct/functor

Hongwei Xi hwxi@cs.bu.edu
Tue, 22 Jan 2002 13:15:47 -0500 (EST)


On Sat, 19 Jan 2002, [EUC-KR] ¾È±â¿µ wrote:

>>I prefer Haskell style type classes because
>>using them one can overload functions.
>>e.g.
>>
>>\begin{code}
>>class Container a where
>>  celem :: Eq b => b -> a b -> Bool
>>  cmap :: (Eq b, Eq c) => (b->c) -> a b -> a c
>>
>>data MyList a = MyList [a] deriving Show
>>
>>instance Container MyList where
>>  celem x (MyList l) = elem x l
>>  cmap f (MyList l) = MyList (map f l)
>>\end{code}
>>
>>What does ML struct/functor have anything
>>better than type classes ? For the user type
>>classes feels like implicit functor istantiations
>>to specific sturucture, and struct/functor seems
>>just bugglling the user to do more typing which
>>can be automated by using type classes.
>>
>>Type classes are more than automatic functor
>>instantiation since it can construct hierarcy
>>between types and you can implement implicit
>>type conversoins.
>>
>>I think ML, especially nML - the Korean dialact of ML
>>being developed in ROPAS(http://ropas.kaist.ac.kr/) -
>>should throw away struct/functor or deprecate them
>>and move to type classes.
>>
>>p.s. Am I asking ML to metamorph into eager Haskell ? :-)

Automation comes with a stiff price: at most one implementation
for each interface. In the above case, if I have a different
implementation for MyList, how could it possibly be automatically
determined which should be chosen?

\begin{code}
instance Container MyList where
 (a different implementation)
\end{code}

In Haskell, I guess that the one implemented later is always chosen.
Why can't I have two different implementations for an interface?

Another problem with Haskell classes is that there is currently
no way of hiding type information. For instance, suppose that
I want to implement a module for operations on sets but I do
not want to reveal what data representation I use for sets. Is
there a way of doing this in Haskell?

--Hongwei

\~~~~/ \\   //  \\    //    @
C-o^o,  ))__||   \\__//_  // \\
(  ^ )  ))__||    \--/-\\     \\
/ \V\   ))  ||     //   \\     \\
------ //   || o  //     \\     \\//
Computer Science Department
Boston University
111 Cummington Street
Boston, MA 02215

Email: hwxi@cs.bu.edu
Url: http://www.cs.bu.edu/~hwxi
Tel: +1 617 358 2511 (office)
Fax: +1 617 353 6457 (department)