бекпаком можно стразу группу инстансов генерить по имплементации сигнатуры, из имплементации тайпкласса не получится, рекурсия нужна. ну и с кайдами проблемы (хотя и тут может можно извернуться) {-# LANGUAGE TypeSynonymInstances #-} unit monad-sups where signature MonadicType where data M a point :: a -> M a bind :: M a -> (a -> M b) -> M b signature MonadicType2 where data M2 c a point2 :: a -> M2 c a bind2 :: M2 c a -> (a -> M2 c b) -> M2 c b module MonadSups where import MonadicType2 import MonadicType import Control.Monad instance Functor M where fmap = liftM instance Applicative M where pure = return (<*>) = ap instance Monad M where (>>=) = bind return = point instance Functor (M2 a) where fmap = liftM instance Applicative (M2 a) where pure = return (<*>) = ap instance Monad (M2 a) where (>>=) = bind2 return = point2 unit option-impl where module OptionsOptions where data Option a = None | Some a deriving Show type M = Option point :: a -> M a point = Some bind :: M a -> (a -> M b) -> M b bind (Some x) k = k x bind None _ = None unit streams-impl where module StreamsStreams where data Stream a b = Done b | Yield a (Stream a b) deriving Show type M2 = Stream point2 :: a -> M2 c a point2 = Done bind2 :: M2 c a -> (a -> M2 c b) -> M2 c b bind2 (Yield a k) f = Yield a (k `bind2` f) bind2 (Done b) f = f b unit main where dependency streams-impl dependency option-impl dependency monad-sups[MonadicType2=streams-impl:StreamsStreams ,MonadicType=option-impl:OptionsOptions] module Main where import StreamsStreams import OptionsOptions import MonadSups main = do print $ do x <- Some 2 y <- Some 3 return (x + 3) print (pure () :: Stream Int ())
Обсуждают сегодня