-- | Utilities related to Monad and Applicative classes-- Mostly for backwards compatibility.moduleMonadUtils(Applicative(..),(<$>),MonadFix(..),MonadIO(..),liftIO1 ,liftIO2 ,liftIO3 ,liftIO4 ,zipWith3M ,zipWith3M_ ,zipWith4M ,zipWithAndUnzipM ,mapAndUnzipM,mapAndUnzip3M ,mapAndUnzip4M ,mapAndUnzip5M ,mapAccumLM ,mapSndM ,concatMapM ,mapMaybeM ,fmapMaybeM ,fmapEitherM ,anyM ,allM ,orM ,foldlM ,foldlM_ ,foldrM ,maybeMapM ,whenM ,unlessM ,filterOutM )where--------------------------------------------------------------------------------- Imports-------------------------------------------------------------------------------importGhcPrelude importControl.ApplicativeimportControl.MonadimportControl.Monad.FiximportControl.Monad.IO.Class--------------------------------------------------------------------------------- Lift combinators-- These are used throughout the compiler--------------------------------------------------------------------------------- | Lift an 'IO' operation with 1 argument into another monadliftIO1::MonadIOm =>(a ->IOb )->a ->m b liftIO1 =(.)liftIO-- | Lift an 'IO' operation with 2 arguments into another monadliftIO2::MonadIOm =>(a ->b ->IOc )->a ->b ->m c liftIO2 =((.).(.))liftIO-- | Lift an 'IO' operation with 3 arguments into another monadliftIO3::MonadIOm =>(a ->b ->c ->IOd )->a ->b ->c ->m d liftIO3 =((.).((.).(.)))liftIO-- | Lift an 'IO' operation with 4 arguments into another monadliftIO4::MonadIOm =>(a ->b ->c ->d ->IOe )->a ->b ->c ->d ->m e liftIO4 =(((.).(.)).((.).(.)))liftIO--------------------------------------------------------------------------------- Common functions-- These are used throughout the compiler-------------------------------------------------------------------------------zipWith3M::Monadm =>(a ->b ->c ->m d )->[a ]->[b ]->[c ]->m [d ]zipWith3M _[]__=return[]zipWith3M__[]_=return[]zipWith3M___[]=return[]zipWith3Mf (x :xs )(y :ys )(z :zs )=do{r <-f x y z ;rs <-zipWith3M f xs ys zs ;return$r :rs }zipWith3M_::Monadm =>(a ->b ->c ->m d )->[a ]->[b ]->[c ]->m ()zipWith3M_ f asbs cs =do{_<-zipWith3M f asbs cs ;return()}zipWith4M::Monadm =>(a ->b ->c ->d ->m e )->[a ]->[b ]->[c ]->[d ]->m [e ]zipWith4M _[]___=return[]zipWith4M__[]__=return[]zipWith4M___[]_=return[]zipWith4M____[]=return[]zipWith4Mf (x :xs )(y :ys )(z :zs )(a :as)=do{r <-f x y z a ;rs <-zipWith4M f xs ys zs as;return$r :rs }zipWithAndUnzipM::Monadm =>(a ->b ->m (c ,d ))->[a ]->[b ]->m ([c ],[d ]){-# INLINABLEzipWithAndUnzipM#-}-- See Note [flatten_many performance] in TcFlatten for why this-- pragma is essential.zipWithAndUnzipM f (x :xs )(y :ys )=do{(c ,d )<-f x y ;(cs ,ds )<-zipWithAndUnzipM f xs ys ;return(c :cs ,d :ds )}zipWithAndUnzipM___=return([],[])-- | mapAndUnzipM for triplesmapAndUnzip3M::Monadm =>(a ->m (b ,c ,d ))->[a ]->m ([b ],[c ],[d ])mapAndUnzip3M _[]=return([],[],[])mapAndUnzip3Mf (x :xs )=do(r1 ,r2 ,r3 )<-f x (rs1 ,rs2 ,rs3 )<-mapAndUnzip3M f xs return(r1 :rs1 ,r2 :rs2 ,r3 :rs3 )mapAndUnzip4M::Monadm =>(a ->m (b ,c ,d ,e ))->[a ]->m ([b ],[c ],[d ],[e ])mapAndUnzip4M _[]=return([],[],[],[])mapAndUnzip4Mf (x :xs )=do(r1 ,r2 ,r3 ,r4 )<-f x (rs1 ,rs2 ,rs3 ,rs4 )<-mapAndUnzip4M f xs return(r1 :rs1 ,r2 :rs2 ,r3 :rs3 ,r4 :rs4 )mapAndUnzip5M::Monadm =>(a ->m (b ,c ,d ,e ,f ))->[a ]->m ([b ],[c ],[d ],[e ],[f ])mapAndUnzip5M _[]=return([],[],[],[],[])mapAndUnzip5Mf (x :xs )=do(r1 ,r2 ,r3 ,r4 ,r5 )<-f x (rs1 ,rs2 ,rs3 ,rs4 ,rs5 )<-mapAndUnzip5M f xs return(r1 :rs1 ,r2 :rs2 ,r3 :rs3 ,r4 :rs4 ,r5 :rs5 )-- | Monadic version of mapAccumLmapAccumLM::Monadm =>(acc ->x ->m (acc ,y ))-- ^ combining function->acc -- ^ initial state->[x ]-- ^ inputs->m (acc ,[y ])-- ^ final state, outputsmapAccumLM _s []=return(s ,[])mapAccumLMf s (x :xs )=do(s1 ,x' )<-f s x (s2 ,xs' )<-mapAccumLM f s1 xs return(s2 ,x' :xs' )-- | Monadic version of mapSndmapSndM::Monadm =>(b ->m c )->[(a ,b )]->m [(a ,c )]mapSndM _[]=return[]mapSndMf ((a ,b ):xs )=do{c <-f b ;rs <-mapSndM f xs ;return((a ,c ):rs )}-- | Monadic version of concatMapconcatMapM::Monadm =>(a ->m [b ])->[a ]->m [b ]concatMapM f xs =liftMconcat(mapMf xs )-- | Applicative version of mapMaybemapMaybeM::Applicativem =>(a ->m (Maybeb ))->[a ]->m [b ]mapMaybeM f =foldrg (pure[])whereg a =liftA2(maybeid(:))(f a )-- | Monadic version of fmapfmapMaybeM::(Monadm )=>(a ->m b )->Maybea ->m (Maybeb )fmapMaybeM _Nothing=returnNothingfmapMaybeMf (Justx )=f x >>=(return.Just)-- | Monadic version of fmapfmapEitherM::Monadm =>(a ->m b )->(c ->m d )->Eithera c ->m (Eitherb d )fmapEitherM fl _(Lefta )=fl a >>=(return.Left)fmapEitherM_fr (Rightb )=fr b >>=(return.Right)-- | Monadic version of 'any', aborts the computation at the first @True@ valueanyM::Monadm =>(a ->m Bool)->[a ]->m BoolanyM _[]=returnFalseanyMf (x :xs )=dob <-f x ifb thenreturnTrueelseanyM f xs -- | Monad version of 'all', aborts the computation at the first @False@ valueallM::Monadm =>(a ->m Bool)->[a ]->m BoolallM _[]=returnTrueallMf (b :bs )=(f b )>>=(\bv ->ifbv thenallM f bs elsereturnFalse)-- | Monadic version of ororM::Monadm =>m Bool->m Bool->m BoolorM m1 m2 =m1 >>=\x ->ifx thenreturnTrueelsem2 -- | Monadic version of foldlfoldlM::(Monadm )=>(a ->b ->m a )->a ->[b ]->m a foldlM =foldM-- | Monadic version of foldl that discards its resultfoldlM_::(Monadm )=>(a ->b ->m a )->a ->[b ]->m ()foldlM_ =foldM_-- | Monadic version of foldrfoldrM::(Monadm )=>(b ->a ->m a )->a ->[b ]->m a foldrM _z []=returnz foldrMk z (x :xs )=do{r <-foldrM k z xs ;k x r }-- | Monadic version of fmap specialised for MaybemaybeMapM::Monadm =>(a ->m b )->(Maybea ->m (Maybeb ))maybeMapM _Nothing=returnNothingmaybeMapMm (Justx )=liftMJust$m x -- | Monadic version of @when@, taking the condition in the monadwhenM::Monadm =>m Bool->m ()->m ()whenM mb thing =do{b <-mb ;whenb thing }-- | Monadic version of @unless@, taking the condition in the monadunlessM::Monadm =>m Bool->m ()->m ()unlessM condM acc =do{cond <-condM ;unlesscond acc }-- | Like 'filterM', only it reverses the sense of the test.filterOutM::(Applicativem )=>(a ->m Bool)->[a ]->m [a ]filterOutM p =foldr(\x ->liftA2(\flg ->ifflg thenidelse(x :))(p x ))(pure[])

AltStyle によって変換されたページ (->オリジナル) /