examples of functional objects

Pixel pixel@mandrakesoft.com
11 May 2002 20:48:28 +0200

It seems to be hard to have something both abstract and concrete.


The first version (file haskell.listing) is quite simple, but the last
one (file haskell3.listing) gets pretty hairy:

class Show a => Point a where
    translate :: a -> Double -> a
    my_length :: a -> Double
    add :: a -> a -> a
    pack :: a -> Packed_point
    unpack :: a -> Points
    pack = Packed_point

data Packed_point = forall a. Point a => Packed_point a

data Points = TPoint2d Point2d | TPoint3d Point3d
data Point2d = Point2d(Double, Double) deriving Show
data Point3d = Point3d(Double, Double, Double) deriving Show

is there a simpler solution? thanks!

PS: please, don't give me the simple non-OO version, it's already
there (haskell3bis.listing), and I give an explaination why i think
it's not enough:

     non-OO Haskell and OCaml versions are the simplest, but do
     not allow precise enough typing: no distinction between Point2d
     and Point3d is done at compile time. So the only way to have a
     function working only on Point3d's is to raise an error when that
     function is called with a Point2d (see get_z)

(of course, compared to dynamically typed languages versions, it's
already much better)