SafeHaskell: Added SafeHaskell to base
[packages/base.git] / Control / Monad / Zip.hs
1 {-# LANGUAGE Safe #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module : Control.Monad.Zip
5 -- Copyright : (c) Nils Schweinsberg 2011,
6 -- (c) University Tuebingen 2011
7 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : experimental
10 -- Portability : portable
11 --
12 -- Monadic zipping (used for monad comprehensions)
13 --
14 -----------------------------------------------------------------------------
15
16 module Control.Monad.Zip where
17
18 import Prelude
19 import Control.Monad (liftM)
20
21 -- | `MonadZip` type class. Minimal definition: `mzip` or `mzipWith`
22 --
23 -- Instances should satisfy the laws:
24 --
25 -- * Naturality :
26 --
27 -- > liftM (f *** g) (mzip ma mb) = mzip (liftM f ma) (liftM g mb)
28 --
29 -- * Information Preservation:
30 --
31 -- > liftM (const ()) ma = liftM (const ()) mb
32 -- > ==>
33 -- > munzip (mzip ma mb) = (ma, mb)
34 --
35 class Monad m => MonadZip m where
36
37 mzip :: m a -> m b -> m (a,b)
38 mzip = mzipWith (,)
39
40 mzipWith :: (a -> b -> c) -> m a -> m b -> m c
41 mzipWith f ma mb = liftM (uncurry f) (mzip ma mb)
42
43 instance MonadZip [] where
44 mzip = zip
45
46 munzip :: MonadZip m => m (a,b) -> (m a, m b)
47 munzip mab = (liftM fst mab, liftM snd mab)