On Sat, Mar 04, 2000 at 08:05:08PM +0300, S.D.Mechveliani wrote:
> John Meacham  <[EMAIL PROTECTED]>  writes on 4 Mar 2000 
> 
> > [..]
> > It is VERY useful to allow reuseability of code in ways the original 
> > author did not anticipate. 
> > for an example from algebra imagine you are given a class Num a 
> > which basically represents a field and you need a Group class and 
> > would like to reuse the standard operators in Num as well as have all
> > types (Num a) automatically work as Groups. With supertyping you can 
> > without modifying or accessing the original code at all. you simply 
> > declare the following:
> >
> > (ex. in psuedo-haskell)
> >
> > class Num a <= Group a where
> >     (+) :: a -> a -> a 
> >     negate :: a -> a
> >
> > note the <=, this decares the new class Group from which the 
> > existing Num is derived.
> > this would create the new class Group and add it to the class 
> > inheritance tree above Num, note that the operations in Group must 
> > be a subset of what is currently contained in Num, the effect is 
> > the exact same as if Num had derived from Group in the first place. 
> > [..]
> 
> 
> Having the program             class B a where f :: a -> a
>                                                g :: a -> Bool,
> you can replace it with the program
>                                class A a        where f :: a -> a
>                                class A a => B a where g :: a -> Bool
> and thus, introduce the needed class A.
> This is not possible when  B  is of Prelude, or the source program 
> with  B  is not given.
> Is this the motive for introducing `<=' ?

yes, exactly. or any other reason you might want to modify the orignal source
(perhaps you want to keep the original source pristine or your new base doesnt
belong in a general purpose library but you need it for a specific purpose in a
single module or something)

> If yes, then how easily this can be supported?
> Probably, the existing implementations put the class declarations 
> into the interface modules. And it remains to observe 
>                               class B a <= A a where f :: a -> Bool
> 
> and replace in the interface the class B with the two new classes
> A a  and  A a => B a. 
> There remain some details. For example, any instance of old B causes
> the instance declaration for A, the latter should, probably, be 
> considered as implicit ...

I dont know enough about the internals of the compilers off the top of my head
to know for sure... I imagine the compilers would just build the entire class
hierchy graph first for the purposes of type inference, then it does not matter
where the classes are inserted. In any case the other class must be in scope to
supertype it so the compiler should have enough info to do the right thing...
anybody familier with the internals of the various compilers care to comment?

   John 

-- 
--------------------------------------------------------------
John Meacham   http://synergy.foo.net/~john/   [EMAIL PROTECTED]
California Institute of Technology, Student.
--------------------------------------------------------------

Reply via email to