O'Haskell OOP Polymorphic Functions
Tue, 16 Jan 2001 14:41:52 -0800
Ashley Yakeley wrote:
> At 2001-01-16 14:04, Tom Pledger wrote:
> >The subtyping (struct Derived < Base ...) makes the two instances
> >overlap, with 'instance TheValue Derived' being strictly more specific
> >than 'instance TheValue Base'. If the system preferred the less
> >specific one, the more specific one would never be used.
> >This is quite similar to the way overlapping instances are handled
> >when they occur via substitutions for type variables (e.g. 'instance C
> >[Char]' is strictly more specific than 'instance C [a]') in
> >implementations which support than language extension.
> Subtyping-overlapping is quite different from type-substitution
> 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?
This program is rejected on the grounds that it lacks an instance
delaration for TheValue M. Declare one and you will be fine.
Or (when the upcoming release becomes available) remove the instance
decl for either D1 or D2 and the remaining one will be chosen.