OOP
Nick Kallen
phantom@earthlink.net
Mon, 10 Nov 1997 15:19:55 -0800
> class Drawable a
> where
> draw :: a Picture -> Picture
>
> instance Drawable Dynamic
> where
> draw :: Dynamic Picture -> Picture
> draw (d :: a | Drawable a) pic = draw d pic
> draw _ pic = pic
First off:
Isn't the (draw _) incorrect? I think that (draw (d :: a | Drawable a)) is
the only correct instance of drawable. If (draw _) were used, wouldn't it
follow that that ANY Dynamic would qualify as a Drawable? (This seems
obvious to me, although perhaps I'm making an error.)
Second:
The whole (instance Drawable Dynamic) seems like an unneccessary burden on
the programmer!
If
instance Drawable Dynamic
where
draw :: Dynamic Picture -> Picture
draw (d :: a | Drawable a) pic = draw d pic
is correct, wouldn't it seem obvious that any class one would want to define
a Dynamic instance for would essentially read the same? For example:
instance AnyPossibleClass Dynamic
where
f1 :: Dynamic Foo -> Bar
f1 (d :: a | AnyPossibleClass a) foo = f1 d bar
.
.
fn :: Dynamic Foo -> Bar
fn (d :: a | AnyPossibleClass a) foo = fn d bar
Any possible Dynamic instance for any possible class (hence
"AnyPossibleClass") would read exactly like that, wouldn't it? Perhaps the
syntactic shortcut should be just:
instance AnyPossibleClass Dynamic
And, if for some strange reason my instance of AnyPossibleClass wouldn't
satisfy your class, you just define the instance instead of using this
shortcut.
> This also solves Nick's problem with specifying 'DrawAll', which now can
>simply be written as:
>
> DrawAll :: [Dynamic] Picture -> Picture
> DrawAll [] pic = pic
> DrawAll [d:ds] pic = draw d (DrawAll ds pic)
My only problem with this is that I like the idea of restricting the class
of Dynamic in the function type. Not all Dynamic are in the domain of
DrawAll, as is obvious. Unfortunately, this is a complaint that applies
elsewhere..
For example: the type of the square root function shouldn't be sqrt ::
Real -> Real; it should be sqrt :: Real -> Real | isPositive Real. Of
course, this requires the constraints that I request in my original "Re:
OOP" message.
>By the way 'DrawAll' is now actually just a specific instance of:
>
> instance Drawable [a] | Drawable a
You're right! I should have come up with a better example.