Whitespace
[darcs-mirrors/vector.git] / Data / Vector / Generic / New.hs
1 {-# LANGUAGE Rank2Types, FlexibleContexts #-}
2
3 -- |
4 -- Module : Data.Vector.Generic.New
5 -- Copyright : (c) Roman Leshchinskiy 2008-2010
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 module Data.Vector.Generic.New (
16 New(..), create, run, apply, modify,
17 unstream, transform, unstreamR, transformR,
18 slice, init, tail, take, drop,
19 unsafeSlice, unsafeInit, unsafeTail
20 ) where
21
22 import qualified Data.Vector.Generic.Mutable as MVector
23 import Data.Vector.Generic.Mutable ( MVector )
24
25 import Data.Vector.Generic.Base ( Vector, Mutable )
26
27 import Data.Vector.Fusion.Stream ( Stream, MStream )
28 import qualified Data.Vector.Fusion.Stream as Stream
29
30 import Control.Monad.ST ( ST )
31 import Control.Monad ( liftM )
32 import Prelude hiding ( init, tail, take, drop, reverse, map, filter )
33
34 #include "vector.h"
35
36 data New v a = New (forall s. ST s (Mutable v s a))
37
38 create :: (forall s. ST s (Mutable v s a)) -> New v a
39 {-# INLINE create #-}
40 create = New
41
42 run :: New v a -> ST s (Mutable v s a)
43 {-# INLINE run #-}
44 run (New p) = p
45
46 apply :: (forall s. Mutable v s a -> Mutable v s a) -> New v a -> New v a
47 {-# INLINE apply #-}
48 apply f (New p) = New (liftM f p)
49
50 modify :: (forall s. Mutable v s a -> ST s ()) -> New v a -> New v a
51 {-# INLINE modify #-}
52 modify f (New p) = New (do { v <- p; f v; return v })
53
54 unstream :: Vector v a => Stream a -> New v a
55 {-# INLINE_STREAM unstream #-}
56 unstream s = s `seq` New (MVector.unstream s)
57
58 transform :: Vector v a =>
59 (forall m. Monad m => MStream m a -> MStream m a) -> New v a -> New v a
60 {-# INLINE_STREAM transform #-}
61 transform f (New p) = New (MVector.transform f =<< p)
62
63 {-# RULES
64
65 "transform/transform [New]"
66 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
67 (g :: forall m. Monad m => MStream m a -> MStream m a)
68 p .
69 transform f (transform g p) = transform (f . g) p
70
71 "transform/unstream [New]"
72 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
73 s.
74 transform f (unstream s) = unstream (f s)
75
76 #-}
77
78
79 unstreamR :: Vector v a => Stream a -> New v a
80 {-# INLINE_STREAM unstreamR #-}
81 unstreamR s = s `seq` New (MVector.unstreamR s)
82
83 transformR :: Vector v a =>
84 (forall m. Monad m => MStream m a -> MStream m a) -> New v a -> New v a
85 {-# INLINE_STREAM transformR #-}
86 transformR f (New p) = New (MVector.transformR f =<< p)
87
88 {-# RULES
89
90 "transformR/transformR [New]"
91 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
92 (g :: forall m. Monad m => MStream m a -> MStream m a)
93 p .
94 transformR f (transformR g p) = transformR (f . g) p
95
96 "transformR/unstreamR [New]"
97 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
98 s.
99 transformR f (unstreamR s) = unstreamR (f s)
100
101 #-}
102
103 slice :: Vector v a => Int -> Int -> New v a -> New v a
104 {-# INLINE_STREAM slice #-}
105 slice i n m = apply (MVector.slice i n) m
106
107 init :: Vector v a => New v a -> New v a
108 {-# INLINE_STREAM init #-}
109 init m = apply MVector.init m
110
111 tail :: Vector v a => New v a -> New v a
112 {-# INLINE_STREAM tail #-}
113 tail m = apply MVector.tail m
114
115 take :: Vector v a => Int -> New v a -> New v a
116 {-# INLINE_STREAM take #-}
117 take n m = apply (MVector.take n) m
118
119 drop :: Vector v a => Int -> New v a -> New v a
120 {-# INLINE_STREAM drop #-}
121 drop n m = apply (MVector.drop n) m
122
123 unsafeSlice :: Vector v a => Int -> Int -> New v a -> New v a
124 {-# INLINE_STREAM unsafeSlice #-}
125 unsafeSlice i n m = apply (MVector.unsafeSlice i n) m
126
127 unsafeInit :: Vector v a => New v a -> New v a
128 {-# INLINE_STREAM unsafeInit #-}
129 unsafeInit m = apply MVector.unsafeInit m
130
131 unsafeTail :: Vector v a => New v a -> New v a
132 {-# INLINE_STREAM unsafeTail #-}
133 unsafeTail m = apply MVector.unsafeTail m
134
135 {-# RULES
136
137 "slice/unstream [New]" forall i n s.
138 slice i n (unstream s) = unstream (Stream.slice i n s)
139
140 "init/unstream [New]" forall s.
141 init (unstream s) = unstream (Stream.init s)
142
143 "tail/unstream [New]" forall s.
144 tail (unstream s) = unstream (Stream.tail s)
145
146 "take/unstream [New]" forall n s.
147 take n (unstream s) = unstream (Stream.take n s)
148
149 "drop/unstream [New]" forall n s.
150 drop n (unstream s) = unstream (Stream.drop n s)
151
152 "unsafeSlice/unstream [New]" forall i n s.
153 unsafeSlice i n (unstream s) = unstream (Stream.slice i n s)
154
155 "unsafeInit/unstream [New]" forall s.
156 unsafeInit (unstream s) = unstream (Stream.init s)
157
158 "unsafeTail/unstream [New]" forall s.
159 unsafeTail (unstream s) = unstream (Stream.tail s)
160
161 #-}
162