[Haskell-cafe] [Newbie] Quest for inheritance

Bulat Ziganshin bulatz at HotPOP.com
Tue Jun 7 08:30:21 EDT 2005

Hello Cédric,

Sunday, June 05, 2005, 5:52:13 PM, you wrote:

CP> What interested me in this was the mechanism they used to model
CP> inheritance (described on page 16 & 19), based on data types instead
CP> of classes. The idea being that all constructors of the Employee
CP> datatype have a same parameter, 'inCommon', of type
CP> 'CommonOfEmployees'.
CP> The conclusion I drew from my quick & early findings is that the
CP> latest method is the simplest way to get inheritance in my programs.

OO approach is only one technology of "divide and conquer", just very
popular in last 20 years. moreover, in OO world all the other possible
programming techniques are gone to be modelled via this uniform
mechanism. as a result, programmers with strong OO-only backgound tend
to search solution for any programming problem in terms of classes and
inheritance between them

FP provides your another basic programming block - function call,
or parameterized computation. quick comparision with OO basic block -
class will tell you that function call is a more simple, basic element
while class interface consists of several such function calls. so,
simplest analogy of class interface is just tuple of functions:

createCircle x y r = let draw = ...
                         move x y = ...
                         changeColor c = ...
                     in (draw, move, changeColor)

createRectangle x y w h = let draw = ...
                              move x y = ...
                              changeColor c = ...
                     in (draw, move, changeColor)

such types of interfaces cover 90% of situations where you must use
classes in C++ (well, only 30%. another 60% covered by even simpler

data Shape = Circle x y r
           | Rectangle x y w h

draw (Circle x y r) = ...
draw (Rectangle x y w h) = ...

imho, FP programming require that you think in terms "what operations
i need for this object" and "what data each this operation will need"
instead of OO's "what is a class hierarchy". code reusing is reached
by creating general functions which receive divergent subfunctions as
their parameters:

calcCRC (fOPEN,fREAD,fCLOSE) = do
  h <- fOPEN
  crc <- newIORef 0
  buf <- mallocBytes 65536
  let go = do len <- fREAD h buf 65536
              crc <- updateCRC crc buf len
              when (len>0) go
  fCLOSE h
  readIORef crc

calcFileCRC filename = calcCRC (hOpen filename, hGetBuf, hClose)

calcCompressedDataCRC file algorithm  = do
  calcCRC (startDecompression file algorithm,

You can find more examples of using such technique in my program
(http://freearc.narod.ru), see for example allocator/memoryAllocator,
ByteStream.createFile/createBuffered/create, read_file

Best regards,
 Bulat                            mailto:bulatz at HotPOP.com

More information about the Haskell-Cafe mailing list