Alex Ferguson wrote:
    
 | What I was looking for (or at least, wondering about) was the more
 | specific case I mentioned: a supertype instance which can be defined
 | entirely in terms of a "default" from a subtype, as can be done in
 | this case.  This would require a much less general mechanism to
 | implement, and would certainly not seem to require giving up separate
 | compilation, as overlapping instances would in general.  But if it also   
 | occurs much more rarely, then there may be little purpose.

In this case we could allow the programmer giving a default declaration
for the superclass methods during the class definition of the subclass. An
example would be: 

  class Functor f where
    map :: (a -> b) -> m a -> m b

  class Functor m => Monad m where
    (>>=)  :: m a -> (a -> m b) -> m b
    return :: a -> m a
    
    map f m = m >>= (return . f)
  
Or even:
  
  class Functor f where         
    map :: (a -> b) -> m a -> m b
  
  class Functor m => Monad m where
    (>>=)  :: m a -> (a -> m b) -> m b
    join   :: m (m a) -> m a
    return :: a -> m a

    m >>= k = join (map k m)
    join mm = mm >>= id
    map f m = m >>= (return . f)

The Eq example would be:

  class Eq a where
    (==) :: a -> a -> Bool
    
  class Eq a => Ord a where
    (<=) :: a -> a -> Bool
    
    a == b = a <= b && b <= a

---------------------------------------------------------------------------
  
We can apply the same idea at instance declarations. We could allow the
following notation:
    
  instance Monad W where   ====>   instance Functor W where
    m >>= k  = ..           for      map f m = ..
    return x = ..
    map f m  = ..                  instance Monad W where
                                     m >>= k  = ..
                                     return x = ..

Where we can see the left hand just as shorthand for the right hand side. 
We allow a definition of map inside an instance of Monad because Functor
is a superclass of Monad.

In combination with the class declaration extensions discussed above, we
could even omit map. Note how nicely this coincides with that. 

To give a more extreme example, we could even say:

  instance MonadPlus K where
    m >>= k  = ..
    return x = ..
    map f m  = .. 
    zero     = ..
    m ++ k   = ..

Where this list gets expanded to the right instance declarations with
the right class names. 

I think these could be two nice extensions to the syntax of Haskell
class/instance declarations. The second one (about instance declarations) 
is clearly just syntactic sugar; the first one has further implications
(that I can't oversee at the moment), but very nicely coincides with the
second one. 

---------------------------------------------------------------------------

Regards,
Koen.

--
|  Koen Claessen,                   [EMAIL PROTECTED]  |
|                   http://www.cse.ogi.edu/~kcclaess/  |
|------------------------------------------------------|
|  Visiting student at OGI,    Portland, Oregon, USA.  |



Reply via email to