[Haskell-beginners] Cure me of my OOP ways

David Gordon drg at david.gordon.name
Tue Feb 2 21:43:30 EST 2010


Hi Folks,

I am redesigning a system previously implemented in C++ using Haskell. I am
keen to adopt a native programming style, but certain things about the
original implementation still make perfect sense to me, including the
C++-style subtype polymorphism. I'd be grateful if someone could take a look
at the following sample and tell me if I'm missing some neater possible
implementation in Haskell.

{-# OPTIONS -fglasgow-exts #-}
-- for existential types

-- all subclasses take the same input format and can be created
factory-pattern style
data Input = Input Int Int

-- this is the class we have to implement when creating a 'subtype'
class InternalAPI i where
    doitImpl :: i -> Input -> Int

    -- factory function, everything is initialised by giving an "i" and the
Input
    create :: i -> Input -> PublicAPI
    create a b = PublicAPI a b

-- this is the object clients see, regardless of the underlying
implementation
data PublicAPI = forall p. InternalAPI p => PublicAPI p Input
doit (PublicAPI p i) = doitImpl p i

-- Here's an example implementation of something trivial
data Adder = Adder
instance InternalAPI Adder where
    doitImpl _ (Input a b) = a + b  -- first argument is superfluous

-- There's the factory function in use:
test = create Adder (Input 1 2)
result = doit test

I'm actually pretty happy with this. I'm not trying for a complete OOP
implementation but it lets me:
- have the 'base class' (InternalAPI) implement default versions of methods
with full access to the 'Input' structure containing the member data
- do further inheritance, since I can select the implementation of a method
using that first argument (e.g. on doitImpl)
- base and derived classes see the same member data
- Implementations of 'InternalAPI' can reference each other through the
PublicAPI interface.

Admittedly it's a rather vague question. I wonder if there are any articles
out there showing how to reformulate problems solved using C++-style object
models into Haskell programs? After all, it's hardly as if the C++ model is
particularly elegant...

thanks,

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100202/b6738d4c/attachment.html


More information about the Beginners mailing list