phases.h -> vector.h
[darcs-mirrors/vector.git] / Data / Vector / Generic / New.hs
1 {-# LANGUAGE Rank2Types #-}
2
3 -- |
4 -- Module : Data.Vector.Generic.New
5 -- Copyright : (c) Roman Leshchinskiy 2008-2009
6 -- License : BSD-style
7 --
8 -- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
9 -- Stability : experimental
10 -- Portability : non-portable
11 --
12 -- Purely functional interface to initialisation of mutable vectors
13 --
14
15 #include "vector.h"
16
17 module Data.Vector.Generic.New (
18 New(..), run, unstream, transform, accum, update, reverse,
19 slice, init, tail, take, drop,
20 unsafeSlice
21 ) where
22
23 import qualified Data.Vector.Generic.Mutable as MVector
24 import Data.Vector.Generic.Mutable ( MVector, MVectorPure )
25
26 import Data.Vector.Fusion.Stream ( Stream, MStream )
27 import qualified Data.Vector.Fusion.Stream as Stream
28
29 import Control.Monad ( liftM )
30 import Prelude hiding ( init, tail, take, drop, reverse, map, filter )
31
32 newtype New a = New (forall m mv. MVector mv m a => m (mv a))
33
34 run :: MVector mv m a => New a -> m (mv a)
35 {-# INLINE run #-}
36 run (New p) = p
37
38 apply :: (forall mv a. MVectorPure mv a => mv a -> mv a) -> New a -> New a
39 {-# INLINE apply #-}
40 apply f (New p) = New (liftM f p)
41
42 modify :: New a -> (forall m mv. MVector mv m a => mv a -> m ()) -> New a
43 {-# INLINE modify #-}
44 modify (New p) q = New (do { v <- p; q v; return v })
45
46 unstream :: Stream a -> New a
47 {-# INLINE_STREAM unstream #-}
48 unstream s = New (MVector.unstream s)
49
50 transform :: (forall m. Monad m => MStream m a -> MStream m a) -> New a -> New a
51 {-# INLINE_STREAM transform #-}
52 transform f (New p) = New (MVector.transform f =<< p)
53
54 {-# RULES
55
56 "transform/transform [New]"
57 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
58 (g :: forall m. Monad m => MStream m a -> MStream m a)
59 p .
60 transform f (transform g p) = transform (f . g) p
61
62 "transform/unstream [New]"
63 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
64 s.
65 transform f (unstream s) = unstream (f s)
66
67 #-}
68
69 slice :: New a -> Int -> Int -> New a
70 {-# INLINE_STREAM slice #-}
71 slice m i n = apply (\v -> MVector.slice v i n) m
72
73 unsafeSlice :: New a -> Int -> Int -> New a
74 {-# INLINE_STREAM unsafeSlice #-}
75 unsafeSlice m i n = apply (\v -> MVector.unsafeSlice v i n) m
76
77 init :: New a -> New a
78 {-# INLINE_STREAM init #-}
79 init m = apply (\v -> MVector.slice v 0 (MVector.length v - 1)) m
80
81 tail :: New a -> New a
82 {-# INLINE_STREAM tail #-}
83 tail m = apply (\v -> MVector.slice v 1 (MVector.length v - 1)) m
84
85 take :: Int -> New a -> New a
86 {-# INLINE_STREAM take #-}
87 take n m = apply (\v -> MVector.slice v 0 (min n (MVector.length v))) m
88
89 drop :: Int -> New a -> New a
90 {-# INLINE_STREAM drop #-}
91 drop n m = apply (\v -> MVector.slice v n (max 0 (MVector.length v - n))) m
92
93 {-# RULES
94
95 "slice/unstream [New]" forall s i n.
96 slice (unstream s) i n = unstream (Stream.extract s i n)
97
98 "unsafeSlice/unstream [New]" forall s i n.
99 unsafeSlice (unstream s) i n = unstream (Stream.extract s i n)
100
101 "init/unstream [New]" forall s.
102 init (unstream s) = unstream (Stream.init s)
103
104 "tail/unstream [New]" forall s.
105 tail (unstream s) = unstream (Stream.tail s)
106
107 "take/unstream [New]" forall n s.
108 take n (unstream s) = unstream (Stream.take n s)
109
110 "drop/unstream [New]" forall n s.
111 drop n (unstream s) = unstream (Stream.drop n s)
112
113 #-}
114
115 accum :: (a -> b -> a) -> New a -> Stream (Int, b) -> New a
116 {-# INLINE_STREAM accum #-}
117 accum f m s = modify m (\v -> MVector.accum f v s)
118
119 update :: New a -> Stream (Int, a) -> New a
120 {-# INLINE_STREAM update #-}
121 update m s = modify m (\v -> MVector.update v s)
122
123 reverse :: New a -> New a
124 {-# INLINE_STREAM reverse #-}
125 reverse m = modify m (MVector.reverse)
126