O'Haskell OOP Polymorphic Functions
Tom Pledger
Tom.Pledger@peace.com
Wed, 17 Jan 2001 11:36:06 +1300
Ashley Yakeley writes:
[...]
> Subtyping-overlapping is quite different from type-substitution
> overlapping.
Different, but with some similarities.
> Consider:
>
> struct B
>
> struct D1 < Base =
> a1 :: Int
>
> struct D2 < Base =
> a2 :: Int
>
> class TheValue a where theValue :: a -> Int
> instance TheValue B where theValue _ = 0
> instance TheValue D1 where theValue _ = 1
> instance TheValue D2 where theValue _ = 2
>
> struct M < D1,D2
>
> m = struct
> a1 = 0
> a2 = 0
>
> f = theValue m
>
> What's the value of f?
Undefined, because neither of the overlapping instances is strictly
more specific than the other. I hope that would cause a static error,
anywhere that 'instance TheValue D1', 'instance TheValue D2', and
'struct M' are all in scope together.
Here's a similar example using type-substitution overlapping:
instance TheValue Char where ...
instance Monad m => TheValue (m Char) where ...
instance TheValue a => TheValue (Maybe a) where ...
trouble = theValue (Just 'b')
Regards,
Tom