[Haskell-cafe] Subtype polymorphism in Haskell

Daniel Fischer daniel.is.fischer at web.de
Mon Jul 5 09:29:46 EDT 2010


On Monday 05 July 2010 15:22:43, Simon Courtenage wrote:
> Hi,
>
> I am porting a C++ program to Haskell.  My current task is to take a
> class hierarchy and produce something equivalent in Haskell, but I don't
> seem to be able to get a grip on how type classes and instances
> contribute to the solution.  Can anyone enlighten me?
>
> Roughly, the class hierarchy in C++ is of the form
>
> class A {
> public:
>    virtual int do_x(int,int) = 0;
> };
>
> class B {
> public:
>    int do_x(int x,int y) { ...}
> };
>
> class C {
> public:
>    int do_x(int x,int y) { ...}
> };
>
> Any help would be greatly appreciated.

If there's not more to it,

class A where
  do_x :: Int -> Int -> Int

instance A B where
  do_x = whatever

instance A C where
  do_x = somethingElse

would solve it.

>
> Thanks
>
> Simon
> courtenage at gmail.com



More information about the Haskell-Cafe mailing list