Dynamic
Marco Pil
marcop@cs.kun.nl
Tue, 11 Nov 1997 12:11:18 +0100
Nick Kallen wrote:
> > 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.)
>
When you define an instance Drawable for dynamics, it is your obligation
to specify a drawing function for ALL dynamics. But for some Dynamics,
a drawing function doesn't make sense (you cannot draw a function, for
instance), so you define a default (the Id on pictures).
I tried to come up with a good eaxmple that illustrates better what I
mean, but it doesn't get any better than this:
class Tailable
where
tail :: a -> a
instance Tailable [a]
where
tail :: [a] -> [a]
tail [x:xs] = xs
tail _ = [] // this is a default just like 'draw _ pic = pic'
> 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.
A good proposal!
Marco Pil