[Haskell] Dynamic binding

Lennart Augustsson lennart at augustsson.net
Thu Jun 23 10:30:16 EDT 2005


Andrew Ward wrote:
> Hi All,
> In Simon Thompson's The Craft of Functional Programming Second Edition, 
> page 226, it is mentioned that Laufer (1996) describes a Haskell 
> extension to allow dynamic binding. I was wondering if this has been 
> implemented as an extension in any of the haskell compilers, or variants?
> I am a c++ programmer by trade, only dabbling in Haskell when I was at 
> university, so it seems a disadvantage to me to not have dynamic binding 
> in Haskell 98.
> What would be the normal way for a Haskell programmer to handle the 
> typical shape example in beginner OO tutorials?


Unlike previous posters that have shown various ways to simulate
object oriented programming I'm going to try and answer the
question. :)

Here is what I would do:

----------------------
data Shape
	= Circle Point Radius
	| Square Point Size

draw :: Shape -> Pict
draw (Circle p r) = drawCircle p r
draw (Square p s) = drawRectangle p s s

moveTo :: Shape -> Point -> Shape
moveTo (Circle _ r) p = Circle p r
moveTo (Square _ s) p = Square p s

shapes :: [Shape]
shapes = [Circle (0,0) 1, Square (1,1) 2]

shapes' :: [Shape]
shapes' = map (moveTo (2,2)) shapes
----------------------

This is in rather stark contrast to the object oriented way of doing the
same things.  For reference, here's how you could do it :

----------------------
class IsShape shape where
     draw :: shape -> Pict
     moveTo :: Point -> shape -> shape

data Shape = forall a . (IsShape a) => Shape a

data Circle = Circle Point Radius
instance IsShape Circle where
     draw (Circle p r) = drawCircle p r
     moveTo p (Circle _ r) = Circle p r

data Square = Square Point Size
instance IsShape Square where
     draw (Square p s) = drawRectangle p s s
     moveTo p (Square _ s) = Square p s

shapes :: [Shape]
shapes = [Shape (Circle (0,0) 10), Shape (Square (1,1) 2)]

shapes' :: [Shape]
shapes' = map (moveShapeTo (2,2)) shapes
   where moveShapeTo p (Shape s) = Shape (moveTo p s)
----------------------

Both ways of doing it contains the same information, it's just that
it's organized in different ways.

The "functional way" centers around the type Shape.  You can find out
all about what shapes exist by looking at the type definition.  For
each operation on shapes (draw & moveTo) you describe what they do for
the different shapes.

The object oriented way centers more around the objects (surprise,
surprise).  For each kind of object you say that it's a shape and how
the shape operations work.


So, which is better?  I don't think you can say one is better than the
other.  They have different strengths.  With the object oriented way it
is easy to answer questions like "What is a Circle?", whereas with the
functional way it is easy to answer "How do you draw a Shape"?
Likewise, with the object oriented way it's easier to add a new kind
of shape and with the functional way it's easier to add a new
operation.

To me it's a matter of taste.  I like the functional taste; my brain
has been warped over many years.

	-- Lennart


More information about the Haskell mailing list