i'm solving ploblem on generating ancestor instances in haskell. found on haskell wiki: class system extension proposal. so, know, there solutions proposal already?
here examples proposal:
the current class system in haskell based on idea can provide default implementations class methods @ same time defining class, using other methods of class or ancestors. consider following hierarchy, adapted functor hierarchy proposal , other prelude:
class functor m fmap :: (a -> b) -> m -> m b class functor m => applicative m return :: -> m apply :: m (a -> b) -> m -> m b (>>) :: m -> m b -> m b ma >> mb = (fmap (const id) ma) `apply` mb class applicative m => monad m (>>=) :: m -> (a -> m b) -> m b
for concrete instances of monad can define fmap, apply, , (>>)in terms of return , (>>=) 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 able write:
class applicative m => monad m (>>=) :: m -> (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 able define new instances of monad supplying definitions return , (>>=) writing:
instance monad t ma >>= a_mb = ... -- definition return = ... -- definition
explicit import/export of instances
this needed large programs can built without fear of colliding instance declarations between different packages. possible syntax be:
module m -- exported instances ( instance monad t , instance functor (f a) hiding (functor (f int), functor (f char)) , f(..) ) import foo (instance monad hiding monad maybe) data t data f b
where context elided because isn't used in instance selection (at moment). import directive tells compiler use monad instances exported foo except monad maybe instance (it doesn't matter whether or not foo export monad maybe instance - matters here don't want if there one).
yes, defaultsignatures
extension allows this. example, functor
/applicative
example, 1 write
{-# language defaultsignatures #-} class functor f fmap :: (a -> b) -> f -> f b default fmap :: applicative f => (a -> b) -> f -> f b fmap = lifta
Comments
Post a Comment