简体   繁体   中英

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) -> ma -> mb class Functor m => Applicative m where return :: a -> ma apply :: m (a -> b) -> ma -> mb (>>) :: ma -> mb -> mb ma >> mb = (fmap (const id) ma) `apply` mb class Applicative m => Monad m where (>>=) :: ma -> (a -> mb) -> mb 

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 (fa)) apply mf ma = mf >>= \\f -> ma >>= \\a -> return (fa) ma >> mb = ma >>= \\_ -> mb 

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

 class Applicative m => Monad m where (>>=) :: ma -> (a -> mb) -> mb fmap f ma = ma >>= (\\a -> return (fa)) apply mf ma = mf >>= \\f -> ma >>= \\a -> return (fa) 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 ab 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM