1 -----------------------------------------------------------------------------
3 -- Module : Control.Monad.Zip
4 -- Copyright : (c) Nils Schweinsberg 2011,
5 -- (c) University Tuebingen 2011
6 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : portable
11 -- Monadic zipping (used for monad comprehensions)
13 -----------------------------------------------------------------------------
15 module Control.Monad.Zip where
18 import Control.Monad (liftM)
20 -- | `MonadZip` type class. Minimal definition: `mzip` or `mzipWith`
22 -- Instances should satisfy the laws:
26 -- > liftM (f *** g) (mzip ma mb) = mzip (liftM f ma) (liftM g mb)
28 -- * Information Preservation:
30 -- > liftM (const ()) ma = liftM (const ()) mb
32 -- > munzip (mzip ma mb) = (ma, mb)
34 class Monad m => MonadZip m where
36 mzip :: m a -> m b -> m (a,b)
39 mzipWith :: (a -> b -> c) -> m a -> m b -> m c
40 mzipWith f ma mb = liftM (uncurry f) (mzip ma mb)
42 instance MonadZip [] where
45 munzip :: MonadZip m => m (a,b) -> (m a, m b)
46 munzip mab = (liftM fst mab, liftM snd mab)