New.inplace -> New.transform
[darcs-mirrors/vector.git] / Data / Vector / MVector / New.hs
1 {-# LANGUAGE Rank2Types, ScopedTypeVariables #-}
2
3 #include "phases.h"
4
5 module Data.Vector.MVector.New (
6 New(..), run, unstream, transform, update, reverse
7 ) where
8
9 import qualified Data.Vector.MVector as MVector
10 import Data.Vector.MVector ( MVector )
11
12 import Data.Vector.Fusion.Stream ( Stream, MStream )
13 import qualified Data.Vector.Fusion.Stream as Stream
14
15 import qualified Data.Vector.Fusion.Stream.Monadic as MStream
16
17 import Control.Monad ( liftM )
18 import Prelude hiding ( reverse, map, filter )
19
20 newtype New a = New (forall m mv. MVector mv m a => m (mv a))
21
22 run :: MVector mv m a => New a -> m (mv a)
23 {-# INLINE run #-}
24 run (New p) = p
25
26 modify :: New a -> (forall m mv. MVector mv m a => mv a -> m ()) -> New a
27 {-# INLINE modify #-}
28 modify (New p) q = New (do { v <- p; q v; return v })
29
30 unstream :: Stream a -> New a
31 {-# INLINE_STREAM unstream #-}
32 unstream s = New (MVector.unstream s)
33
34 transform :: (forall m. Monad m => MStream m a -> MStream m a) -> New a -> New a
35 {-# INLINE_STREAM transform #-}
36 transform f (New p) = New (MVector.transform f =<< p)
37
38 {-# RULES
39
40 "transform/transform [New]"
41 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
42 (g :: forall m. Monad m => MStream m a -> MStream m a)
43 p .
44 transform f (transform g p) = transform (f . g) p
45
46 #-}
47
48 update :: New a -> Stream (Int, a) -> New a
49 {-# INLINE_STREAM update #-}
50 update m s = modify m (\v -> MVector.update v s)
51
52 reverse :: New a -> New a
53 {-# INLINE_STREAM reverse #-}
54 reverse m = modify m (MVector.reverse)
55