Haskell: class system extension proposal

I'm solving some ploblem on generating ancestor instances in Haskell. Recently I found this on Haskell wiki: Class system extension proposal. So, I would like to know, are there any solutions for this proposal already?

Here are the examples from the Proposal:

The current class system in Haskell is based on the idea that you can often provide default implementations for class methods at the same time as defining the class, by using other methods of the class or its ancestors. However consider the following hierarchy, adapted from Functor hierarchy proposal and The Other Prelude:

class Functor m where
    fmap :: (a -> b) -> m a -> m b

class Functor m => Applicative m where
    return :: a -> m a

    apply :: m (a -> b) -> m a -> m b

    (>>) :: m a -> m b -> m b
    ma >> mb = (fmap (const id) ma) `apply` mb

class Applicative m => Monad m where
    (>>=) :: m a -> (a -> m b) -> m b

For all concrete instances of Monad we can define fmap, apply, and (>>)in terms of return and (>>=) as follows:

fmap f ma = ma >>= (a -> return (f a))

apply mf ma = mf >>= f -> ma >>= a -> return (f a)

ma >> mb = ma >>= _ -> mb

In other words, we'd like to be able to write:

class Applicative m => Monad m where
    (>>=) :: m a -> (a -> m b) -> m b

    fmap f ma = ma >>= (a -> return (f a))

    apply mf ma = mf >>= f -> ma >>= a -> return (f a)

    ma >> mb = ma >>= _ -> mb

and be able to define new instances of Monad just by supplying definitions for return and (>>=) by writing:

instance Monad T where
    ma >>= a_mb = ... -- some definition

    return a = ... -- some definition

Explicit import/export of instances

This is needed so that large programs can be built without fear of colliding instance declarations between different packages. A possible syntax could be:

module M
     -- exported instances 
   ( instance Monad T
   , instance Functor (F a) hiding (Functor (F Int), Functor (F Char))
   , F(..)
   ) where

import Foo (instance Monad a hiding Monad Maybe)

data T a
data F a b

where the context is elided because this isn't used in instance selection (at the moment). The import directive tells the compiler to use all Monad instances exported by Foo except for the Monad Maybe instance (it doesn't matter whether or not Foo actually does export a Monad Maybe instance - all that matters here is that we don't want it if there is one).


Yes, the DefaultSignatures extension allows this. For example, for the Functor / Applicative example, one could write

{-# LANGUAGE DefaultSignatures #-}
class Functor f where
    fmap :: (a -> b) -> f a -> f b
    default fmap :: Applicative f => (a -> b) -> f a -> f b
    fmap = liftA
链接地址: http://www.djcxy.com/p/43336.html

上一篇: fmap和Haskell的“平面地图”

下一篇: Haskell:类系统扩展建议