Update base for latest Safe Haskell.
[packages/base.git] / Control / Monad / Zip.hs
1 {-# LANGUAGE Safe #-}
2
3 -----------------------------------------------------------------------------
4 -- |
5 -- Module : Control.Monad.Zip
6 -- Copyright : (c) Nils Schweinsberg 2011,
7 -- (c) George Giorgidze 2011
8 -- (c) University Tuebingen 2011
9 -- License : BSD-style (see the file libraries/base/LICENSE)
10 -- Maintainer : libraries@haskell.org
11 -- Stability : experimental
12 -- Portability : portable
13 --
14 -- Monadic zipping (used for monad comprehensions)
15 --
16 -----------------------------------------------------------------------------
17
18 module Control.Monad.Zip where
19
20 import Prelude
21 import Control.Monad (liftM)
22
23 -- | `MonadZip` type class. Minimal definition: `mzip` or `mzipWith`
24 --
25 -- Instances should satisfy the laws:
26 --
27 -- * Naturality :
28 --
29 -- > liftM (f *** g) (mzip ma mb) = mzip (liftM f ma) (liftM g mb)
30 --
31 -- * Information Preservation:
32 --
33 -- > liftM (const ()) ma = liftM (const ()) mb
34 -- > ==>
35 -- > munzip (mzip ma mb) = (ma, mb)
36 --
37 class Monad m => MonadZip m where
38
39 mzip :: m a -> m b -> m (a,b)
40 mzip = mzipWith (,)
41
42 mzipWith :: (a -> b -> c) -> m a -> m b -> m c
43 mzipWith f ma mb = liftM (uncurry f) (mzip ma mb)
44
45 munzip :: m (a,b) -> (m a, m b)
46 munzip mab = (liftM fst mab, liftM snd mab)
47 -- munzip is a member of the class because sometimes
48 -- you can implement it more efficiently than the
49 -- above default code. See Trac #4370 comment by giorgidze
50
51 instance MonadZip [] where
52 mzip = zip
53 mzipWith = zipWith
54 munzip = unzip
55