Work around GHC bug 4310
[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, modifyWithStream,
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 p = New p
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 modifyWithStream :: (forall s. Mutable v s a -> Stream b -> ST s ())
55 -> New v a -> Stream b -> New v a
56 {-# INLINE_STREAM modifyWithStream #-}
57 modifyWithStream f (New p) s = s `seq` New (do { v <- p; f v s; return v })
58
59 unstream :: Vector v a => Stream a -> New v a
60 {-# INLINE_STREAM unstream #-}
61 unstream s = s `seq` New (MVector.unstream s)
62
63 transform :: Vector v a =>
64 (forall m. Monad m => MStream m a -> MStream m a) -> New v a -> New v a
65 {-# INLINE_STREAM transform #-}
66 transform f (New p) = New (MVector.transform f =<< p)
67
68 {-# RULES
69
70 "transform/transform [New]"
71 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
72 (g :: forall m. Monad m => MStream m a -> MStream m a)
73 p .
74 transform f (transform g p) = transform (f . g) p
75
76 "transform/unstream [New]"
77 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
78 s.
79 transform f (unstream s) = unstream (f s)
80
81 #-}
82
83
84 unstreamR :: Vector v a => Stream a -> New v a
85 {-# INLINE_STREAM unstreamR #-}
86 unstreamR s = s `seq` New (MVector.unstreamR s)
87
88 transformR :: Vector v a =>
89 (forall m. Monad m => MStream m a -> MStream m a) -> New v a -> New v a
90 {-# INLINE_STREAM transformR #-}
91 transformR f (New p) = New (MVector.transformR f =<< p)
92
93 {-# RULES
94
95 "transformR/transformR [New]"
96 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
97 (g :: forall m. Monad m => MStream m a -> MStream m a)
98 p .
99 transformR f (transformR g p) = transformR (f . g) p
100
101 "transformR/unstreamR [New]"
102 forall (f :: forall m. Monad m => MStream m a -> MStream m a)
103 s.
104 transformR f (unstreamR s) = unstreamR (f s)
105
106 #-}
107
108 slice :: Vector v a => Int -> Int -> New v a -> New v a
109 {-# INLINE_STREAM slice #-}
110 slice i n m = apply (MVector.slice i n) m
111
112 init :: Vector v a => New v a -> New v a
113 {-# INLINE_STREAM init #-}
114 init m = apply MVector.init m
115
116 tail :: Vector v a => New v a -> New v a
117 {-# INLINE_STREAM tail #-}
118 tail m = apply MVector.tail m
119
120 take :: Vector v a => Int -> New v a -> New v a
121 {-# INLINE_STREAM take #-}
122 take n m = apply (MVector.take n) m
123
124 drop :: Vector v a => Int -> New v a -> New v a
125 {-# INLINE_STREAM drop #-}
126 drop n m = apply (MVector.drop n) m
127
128 unsafeSlice :: Vector v a => Int -> Int -> New v a -> New v a
129 {-# INLINE_STREAM unsafeSlice #-}
130 unsafeSlice i n m = apply (MVector.unsafeSlice i n) m
131
132 unsafeInit :: Vector v a => New v a -> New v a
133 {-# INLINE_STREAM unsafeInit #-}
134 unsafeInit m = apply MVector.unsafeInit m
135
136 unsafeTail :: Vector v a => New v a -> New v a
137 {-# INLINE_STREAM unsafeTail #-}
138 unsafeTail m = apply MVector.unsafeTail m
139
140 {-# RULES
141
142 "slice/unstream [New]" forall i n s.
143 slice i n (unstream s) = unstream (Stream.slice i n s)
144
145 "init/unstream [New]" forall s.
146 init (unstream s) = unstream (Stream.init s)
147
148 "tail/unstream [New]" forall s.
149 tail (unstream s) = unstream (Stream.tail s)
150
151 "take/unstream [New]" forall n s.
152 take n (unstream s) = unstream (Stream.take n s)
153
154 "drop/unstream [New]" forall n s.
155 drop n (unstream s) = unstream (Stream.drop n s)
156
157 "unsafeSlice/unstream [New]" forall i n s.
158 unsafeSlice i n (unstream s) = unstream (Stream.slice i n s)
159
160 "unsafeInit/unstream [New]" forall s.
161 unsafeInit (unstream s) = unstream (Stream.init s)
162
163 "unsafeTail/unstream [New]" forall s.
164 unsafeTail (unstream s) = unstream (Stream.tail s)
165
166 #-}
167