1 {-# OPTIONS_GHC -funbox-strict-fields #-}
4 -----------------------------------------------------------------------------
7 -- Copyright : (c) 2001-2002 Manuel M T Chakravarty & Gabriele Keller
8 -- License : see libraries/base/LICENSE
10 -- Maintainer : Manuel M. T. Chakravarty <chak@cse.unsw.edu.au>
11 -- Stability : internal
12 -- Portability : non-portable (GHC Extensions)
14 -- Basic implementation of Parallel Arrays.
16 -- This module has two functions: (1) It defines the interface to the
17 -- parallel array extension of the Prelude and (2) it provides a vanilla
18 -- implementation of parallel arrays that does not require to flatten the
19 -- array code. The implementation is not very optimised.
21 --- DOCU ----------------------------------------------------------------------
23 -- Language: Haskell 98 plus unboxed values and parallel arrays
25 -- The semantic difference between standard Haskell arrays (aka "lazy
26 -- arrays") and parallel arrays (aka "strict arrays") is that the evaluation
27 -- of two different elements of a lazy array is independent, whereas in a
28 -- strict array either non or all elements are evaluated. In other words,
29 -- when a parallel array is evaluated to WHNF, all its elements will be
30 -- evaluated to WHNF. The name parallel array indicates that all array
31 -- elements may, in general, be evaluated to WHNF in parallel without any
32 -- need to resort to speculative evaluation. This parallel evaluation
33 -- semantics is also beneficial in the sequential case, as it facilitates
34 -- loop-based array processing as known from classic array-based languages,
37 -- The interface of this module is essentially a variant of the list
38 -- component of the Prelude, but also includes some functions (such as
39 -- permutations) that are not provided for lists. The following list
40 -- operations are not supported on parallel arrays, as they would require the
41 -- availability of infinite parallel arrays: `iterate', `repeat', and `cycle'.
43 -- The current implementation is quite simple and entirely based on boxed
44 -- arrays. One disadvantage of boxed arrays is that they require to
45 -- immediately initialise all newly allocated arrays with an error thunk to
46 -- keep the garbage collector happy, even if it is guaranteed that the array
47 -- is fully initialised with different values before passing over the
48 -- user-visible interface boundary. Currently, no effort is made to use
49 -- raw memory copy operations to speed things up.
51 --- TODO ----------------------------------------------------------------------
53 -- * We probably want a standard library `PArray' in addition to the prelude
54 -- extension in the same way as the standard library `List' complements the
55 -- list functions from the prelude.
57 -- * Currently, functions that emphasis the constructor-based definition of
58 -- lists (such as, head, last, tail, and init) are not supported.
60 -- Is it worthwhile to support the string processing functions lines,
61 -- words, unlines, and unwords? (Currently, they are not implemented.)
63 -- It can, however, be argued that it would be worthwhile to include them
64 -- for completeness' sake; maybe only in the standard library `PArray'.
66 -- * Prescans are often more useful for array programming than scans. Shall
67 -- we include them into the Prelude or the library?
69 -- * Due to the use of the iterator `loop', we could define some fusion rules
72 -- * We might want to add bounds checks that can be deactivated.
76 -- [::], -- Built-in syntax
78 mapP, -- :: (a -> b) -> [:a:] -> [:b:]
79 (+:+), -- :: [:a:] -> [:a:] -> [:a:]
80 filterP, -- :: (a -> Bool) -> [:a:] -> [:a:]
81 concatP, -- :: [:[:a:]:] -> [:a:]
82 concatMapP, -- :: (a -> [:b:]) -> [:a:] -> [:b:]
83 -- head, last, tail, init, -- it's not wise to use them on arrays
84 nullP, -- :: [:a:] -> Bool
85 lengthP, -- :: [:a:] -> Int
86 (!:), -- :: [:a:] -> Int -> a
87 foldlP, -- :: (a -> b -> a) -> a -> [:b:] -> a
88 foldl1P, -- :: (a -> a -> a) -> [:a:] -> a
89 scanlP, -- :: (a -> b -> a) -> a -> [:b:] -> [:a:]
90 scanl1P, -- :: (a -> a -> a) -> [:a:] -> [:a:]
91 foldrP, -- :: (a -> b -> b) -> b -> [:a:] -> b
92 foldr1P, -- :: (a -> a -> a) -> [:a:] -> a
93 scanrP, -- :: (a -> b -> b) -> b -> [:a:] -> [:b:]
94 scanr1P, -- :: (a -> a -> a) -> [:a:] -> [:a:]
95 -- iterate, repeat, -- parallel arrays must be finite
96 singletonP, -- :: a -> [:a:]
98 replicateP, -- :: Int -> a -> [:a:]
99 -- cycle, -- parallel arrays must be finite
100 takeP, -- :: Int -> [:a:] -> [:a:]
101 dropP, -- :: Int -> [:a:] -> [:a:]
102 splitAtP, -- :: Int -> [:a:] -> ([:a:],[:a:])
103 takeWhileP, -- :: (a -> Bool) -> [:a:] -> [:a:]
104 dropWhileP, -- :: (a -> Bool) -> [:a:] -> [:a:]
105 spanP, -- :: (a -> Bool) -> [:a:] -> ([:a:], [:a:])
106 breakP, -- :: (a -> Bool) -> [:a:] -> ([:a:], [:a:])
107 -- lines, words, unlines, unwords, -- is string processing really needed
108 reverseP, -- :: [:a:] -> [:a:]
109 andP, -- :: [:Bool:] -> Bool
110 orP, -- :: [:Bool:] -> Bool
111 anyP, -- :: (a -> Bool) -> [:a:] -> Bool
112 allP, -- :: (a -> Bool) -> [:a:] -> Bool
113 elemP, -- :: (Eq a) => a -> [:a:] -> Bool
114 notElemP, -- :: (Eq a) => a -> [:a:] -> Bool
115 lookupP, -- :: (Eq a) => a -> [:(a, b):] -> Maybe b
116 sumP, -- :: (Num a) => [:a:] -> a
117 productP, -- :: (Num a) => [:a:] -> a
118 maximumP, -- :: (Ord a) => [:a:] -> a
119 minimumP, -- :: (Ord a) => [:a:] -> a
120 zipP, -- :: [:a:] -> [:b:] -> [:(a, b) :]
121 zip3P, -- :: [:a:] -> [:b:] -> [:c:] -> [:(a, b, c):]
122 zipWithP, -- :: (a -> b -> c) -> [:a:] -> [:b:] -> [:c:]
123 zipWith3P, -- :: (a -> b -> c -> d) -> [:a:]->[:b:]->[:c:]->[:d:]
124 unzipP, -- :: [:(a, b) :] -> ([:a:], [:b:])
125 unzip3P, -- :: [:(a, b, c):] -> ([:a:], [:b:], [:c:])
127 -- overloaded functions
129 enumFromToP, -- :: Enum a => a -> a -> [:a:]
130 enumFromThenToP, -- :: Enum a => a -> a -> a -> [:a:]
132 -- the following functions are not available on lists
134 toP, -- :: [a] -> [:a:]
135 fromP, -- :: [:a:] -> [a]
136 sliceP, -- :: Int -> Int -> [:e:] -> [:e:]
137 foldP, -- :: (e -> e -> e) -> e -> [:e:] -> e
138 fold1P, -- :: (e -> e -> e) -> [:e:] -> e
139 permuteP, -- :: [:Int:] -> [:e:] -> [:e:]
140 bpermuteP, -- :: [:Int:] -> [:e:] -> [:e:]
141 dpermuteP, -- :: [:Int:] -> [:e:] -> [:e:] -> [:e:]
142 crossP, -- :: [:a:] -> [:b:] -> [:(a, b):]
143 crossMapP, -- :: [:a:] -> (a -> [:b:]) -> [:(a, b):]
144 indexOfP -- :: (a -> Bool) -> [:a:] -> [:Int:]
151 import GHC.ST ( ST(..), runST )
152 import GHC.Base ( Int#, Array#, Int(I#), MutableArray#, newArray#,
153 unsafeFreezeArray#, indexArray#, writeArray#, (<#), (>=#) )
157 infix 4 `elemP`, `notElemP`
160 -- representation of parallel arrays
161 -- ---------------------------------
163 -- this rather straight forward implementation maps parallel arrays to the
164 -- internal representation used for standard Haskell arrays in GHC's Prelude
165 -- (EXPORTED ABSTRACTLY)
167 -- * This definition *must* be kept in sync with `TysWiredIn.parrTyCon'!
169 data [::] e = PArr Int# (Array# e)
172 -- exported operations on parallel arrays
173 -- --------------------------------------
175 -- operations corresponding to list operations
178 mapP :: (a -> b) -> [:a:] -> [:b:]
179 mapP f = fst . loop (mapEFL f) noAL
181 (+:+) :: [:a:] -> [:a:] -> [:a:]
182 a1 +:+ a2 = fst $ loop (mapEFL sel) noAL (enumFromToP 0 (len1 + len2 - 1))
183 -- we can't use the [:x..y:] form here for tedious
184 -- reasons to do with the typechecker and the fact that
185 -- `enumFromToP' is defined in the same module
190 sel i | i < len1 = a1!:i
191 | otherwise = a2!:(i - len1)
193 filterP :: (a -> Bool) -> [:a:] -> [:a:]
194 filterP p = fst . loop (filterEFL p) noAL
196 concatP :: [:[:a:]:] -> [:a:]
197 concatP xss = foldlP (+:+) [::] xss
199 concatMapP :: (a -> [:b:]) -> [:a:] -> [:b:]
200 concatMapP f = concatP . mapP f
202 -- head, last, tail, init, -- it's not wise to use them on arrays
204 nullP :: [:a:] -> Bool
208 lengthP :: [:a:] -> Int
209 lengthP (PArr n# _) = I# n#
211 (!:) :: [:a:] -> Int -> a
214 foldlP :: (a -> b -> a) -> a -> [:b:] -> a
215 foldlP f z = snd . loop (foldEFL (flip f)) z
217 foldl1P :: (a -> a -> a) -> [:a:] -> a
218 foldl1P f [::] = error "Prelude.foldl1P: empty array"
219 foldl1P f a = snd $ loopFromTo 1 (lengthP a - 1) (foldEFL f) (a!:0) a
221 scanlP :: (a -> b -> a) -> a -> [:b:] -> [:a:]
222 scanlP f z = fst . loop (scanEFL (flip f)) z
224 scanl1P :: (a -> a -> a) -> [:a:] -> [:a:]
225 scanl1P f [::] = error "Prelude.scanl1P: empty array"
226 scanl1P f a = fst $ loopFromTo 1 (lengthP a - 1) (scanEFL f) (a!:0) a
228 foldrP :: (a -> b -> b) -> b -> [:a:] -> b
229 foldrP = error "Prelude.foldrP: not implemented yet" -- FIXME
231 foldr1P :: (a -> a -> a) -> [:a:] -> a
232 foldr1P = error "Prelude.foldr1P: not implemented yet" -- FIXME
234 scanrP :: (a -> b -> b) -> b -> [:a:] -> [:b:]
235 scanrP = error "Prelude.scanrP: not implemented yet" -- FIXME
237 scanr1P :: (a -> a -> a) -> [:a:] -> [:a:]
238 scanr1P = error "Prelude.scanr1P: not implemented yet" -- FIXME
240 -- iterate, repeat -- parallel arrays must be finite
242 singletonP :: a -> [:a:]
243 {-# INLINE singletonP #-}
244 singletonP e = replicateP 1 e
247 {- NOINLINE emptyP #-}
248 emptyP = replicateP 0 undefined
251 replicateP :: Int -> a -> [:a:]
252 {-# INLINE replicateP #-}
253 replicateP n e = runST (do
254 marr# <- newArray n e
257 -- cycle -- parallel arrays must be finite
259 takeP :: Int -> [:a:] -> [:a:]
260 takeP n = sliceP 0 (n - 1)
262 dropP :: Int -> [:a:] -> [:a:]
263 dropP n a = sliceP n (lengthP a - 1) a
265 splitAtP :: Int -> [:a:] -> ([:a:],[:a:])
266 splitAtP n xs = (takeP n xs, dropP n xs)
268 takeWhileP :: (a -> Bool) -> [:a:] -> [:a:]
269 takeWhileP = error "Prelude.takeWhileP: not implemented yet" -- FIXME
271 dropWhileP :: (a -> Bool) -> [:a:] -> [:a:]
272 dropWhileP = error "Prelude.dropWhileP: not implemented yet" -- FIXME
274 spanP :: (a -> Bool) -> [:a:] -> ([:a:], [:a:])
275 spanP = error "Prelude.spanP: not implemented yet" -- FIXME
277 breakP :: (a -> Bool) -> [:a:] -> ([:a:], [:a:])
278 breakP p = spanP (not . p)
280 -- lines, words, unlines, unwords, -- is string processing really needed
282 reverseP :: [:a:] -> [:a:]
283 reverseP a = permuteP (enumFromThenToP (len - 1) (len - 2) 0) a
284 -- we can't use the [:x, y..z:] form here for tedious
285 -- reasons to do with the typechecker and the fact that
286 -- `enumFromThenToP' is defined in the same module
290 andP :: [:Bool:] -> Bool
291 andP = foldP (&&) True
293 orP :: [:Bool:] -> Bool
294 orP = foldP (||) True
296 anyP :: (a -> Bool) -> [:a:] -> Bool
297 anyP p = orP . mapP p
299 allP :: (a -> Bool) -> [:a:] -> Bool
300 allP p = andP . mapP p
302 elemP :: (Eq a) => a -> [:a:] -> Bool
303 elemP x = anyP (== x)
305 notElemP :: (Eq a) => a -> [:a:] -> Bool
306 notElemP x = allP (/= x)
308 lookupP :: (Eq a) => a -> [:(a, b):] -> Maybe b
309 lookupP = error "Prelude.lookupP: not implemented yet" -- FIXME
311 sumP :: (Num a) => [:a:] -> a
314 productP :: (Num a) => [:a:] -> a
315 productP = foldP (*) 1
317 maximumP :: (Ord a) => [:a:] -> a
318 maximumP [::] = error "Prelude.maximumP: empty parallel array"
319 maximumP xs = fold1P max xs
321 minimumP :: (Ord a) => [:a:] -> a
322 minimumP [::] = error "Prelude.minimumP: empty parallel array"
323 minimumP xs = fold1P min xs
325 zipP :: [:a:] -> [:b:] -> [:(a, b):]
328 zip3P :: [:a:] -> [:b:] -> [:c:] -> [:(a, b, c):]
329 zip3P = zipWith3P (,,)
331 zipWithP :: (a -> b -> c) -> [:a:] -> [:b:] -> [:c:]
332 zipWithP f a1 a2 = let
335 len = len1 `min` len2
337 fst $ loopFromTo 0 (len - 1) combine 0 a1
339 combine e1 i = (Just $ f e1 (a2!:i), i + 1)
341 zipWith3P :: (a -> b -> c -> d) -> [:a:]->[:b:]->[:c:]->[:d:]
342 zipWith3P f a1 a2 a3 = let
346 len = len1 `min` len2 `min` len3
348 fst $ loopFromTo 0 (len - 1) combine 0 a1
350 combine e1 i = (Just $ f e1 (a2!:i) (a3!:i), i + 1)
352 unzipP :: [:(a, b):] -> ([:a:], [:b:])
353 unzipP a = (fst $ loop (mapEFL fst) noAL a, fst $ loop (mapEFL snd) noAL a)
354 -- FIXME: these two functions should be optimised using a tupled custom loop
355 unzip3P :: [:(a, b, c):] -> ([:a:], [:b:], [:c:])
356 unzip3P a = (fst $ loop (mapEFL fst3) noAL a,
357 fst $ loop (mapEFL snd3) noAL a,
358 fst $ loop (mapEFL trd3) noAL a)
367 instance Eq a => Eq [:a:] where
368 a1 == a2 | lengthP a1 == lengthP a2 = andP (zipWithP (==) a1 a2)
371 instance Ord a => Ord [:a:] where
372 compare a1 a2 = case foldlP combineOrdering EQ (zipWithP compare a1 a2) of
373 EQ | lengthP a1 == lengthP a2 -> EQ
374 | lengthP a1 < lengthP a2 -> LT
377 combineOrdering EQ EQ = EQ
378 combineOrdering EQ other = other
379 combineOrdering other _ = other
381 instance Functor [::] where
384 instance Monad [::] where
385 m >>= k = foldrP ((+:+) . k ) [::] m
386 m >> k = foldrP ((+:+) . const k) [::] m
390 instance Show a => Show [:a:] where
391 showsPrec _ = showPArr . fromP
393 showPArr [] s = "[::]" ++ s
394 showPArr (x:xs) s = "[:" ++ shows x (showPArr' xs s)
396 showPArr' [] s = ":]" ++ s
397 showPArr' (y:ys) s = ',' : shows y (showPArr' ys s)
399 instance Read a => Read [:a:] where
400 readsPrec _ a = [(toP v, rest) | (v, rest) <- readPArr a]
402 readPArr = readParen False (\r -> do
406 (do { (":]", t) <- lex s; return ([], t) }) ++
407 (do { (x, t) <- reads s; (xs, u) <- readPArr2 t; return (x:xs, u) })
410 (do { (":]", t) <- lex s; return ([], t) }) ++
411 (do { (",", t) <- lex s; (x, u) <- reads t; (xs, v) <- readPArr2 u;
414 -- overloaded functions
417 -- Ideally, we would like `enumFromToP' and `enumFromThenToP' to be members of
418 -- `Enum'. On the other hand, we really do not want to change `Enum'. Thus,
419 -- for the moment, we hope that the compiler is sufficiently clever to
420 -- properly fuse the following definitions.
422 enumFromToP :: Enum a => a -> a -> [:a:]
423 enumFromToP x y = mapP toEnum (eftInt (fromEnum x) (fromEnum y))
425 eftInt x y = scanlP (+) x $ replicateP (y - x + 1) 1
427 enumFromThenToP :: Enum a => a -> a -> a -> [:a:]
428 enumFromThenToP x y z =
429 mapP toEnum (efttInt (fromEnum x) (fromEnum y) (fromEnum z))
431 efttInt x y z = scanlP (+) x $
432 replicateP (abs (z - x) `div` abs delta + 1) delta
436 -- the following functions are not available on lists
439 -- create an array from a list (EXPORTED)
442 toP l = fst $ loop store l (replicateP (length l) ())
444 store _ (x:xs) = (Just x, xs)
446 -- convert an array to a list (EXPORTED)
448 fromP :: [:a:] -> [a]
449 fromP a = [a!:i | i <- [0..lengthP a - 1]]
451 -- cut a subarray out of an array (EXPORTED)
453 sliceP :: Int -> Int -> [:e:] -> [:e:]
455 fst $ loopFromTo (0 `max` from) (to `min` (lengthP a - 1)) (mapEFL id) noAL a
457 -- parallel folding (EXPORTED)
459 -- * the first argument must be associative; otherwise, the result is undefined
461 foldP :: (e -> e -> e) -> e -> [:e:] -> e
464 -- parallel folding without explicit neutral (EXPORTED)
466 -- * the first argument must be associative; otherwise, the result is undefined
468 fold1P :: (e -> e -> e) -> [:e:] -> e
471 -- permute an array according to the permutation vector in the first argument
474 permuteP :: [:Int:] -> [:e:] -> [:e:]
476 | isLen /= esLen = error "GHC.PArr: arguments must be of the same length"
477 | otherwise = runST (do
478 marr <- newArray isLen noElem
482 noElem = error "GHC.PArr.permuteP: I do not exist!"
483 -- unlike standard Haskell arrays, this value represents an
488 -- permute an array according to the back-permutation vector in the first
489 -- argument (EXPORTED)
491 -- * the permutation vector must represent a surjective function; otherwise,
492 -- the result is undefined
494 bpermuteP :: [:Int:] -> [:e:] -> [:e:]
495 bpermuteP is es = fst $ loop (mapEFL (es!:)) noAL is
497 -- permute an array according to the permutation vector in the first
498 -- argument, which need not be surjective (EXPORTED)
500 -- * any elements in the result that are not covered by the permutation
501 -- vector assume the value of the corresponding position of the third
504 dpermuteP :: [:Int:] -> [:e:] -> [:e:] -> [:e:]
506 | isLen /= esLen = error "GHC.PArr: arguments must be of the same length"
507 | otherwise = runST (do
508 marr <- newArray dftLen noElem
509 trans 0 (isLen - 1) marr dft copyOne noAL
513 noElem = error "GHC.PArr.permuteP: I do not exist!"
514 -- unlike standard Haskell arrays, this value represents an
520 copyOne e _ = (Just e, noAL)
522 -- computes the cross combination of two arrays (EXPORTED)
524 crossP :: [:a:] -> [:b:] -> [:(a, b):]
525 crossP a1 a2 = fst $ loop combine (0, 0) $ replicateP len ()
531 combine _ (i, j) = (Just $ (a1!:i, a2!:j), next)
533 next | (i + 1) == len1 = (0 , j + 1)
534 | otherwise = (i + 1, j)
536 {- An alternative implementation
537 * The one above is certainly better for flattened code, but here where we
538 are handling boxed arrays, the trade off is less clear. However, I
539 think, the above one is still better.
544 x1 = concatP $ mapP (replicateP len2) a1
545 x2 = concatP $ replicateP len1 a2
550 -- |Compute a cross of an array and the arrays produced by the given function
551 -- for the elements of the first array.
553 crossMapP :: [:a:] -> (a -> [:b:]) -> [:(a, b):]
556 segd = mapP lengthP bs
557 as = zipWithP replicateP segd a
559 zipP (concatP as) (concatP bs)
561 {- The following may seem more straight forward, but the above is very cheap
562 with segmented arrays, as `mapP lengthP', `zipP', and `concatP' are
563 constant time, and `map f' uses the lifted version of `f'.
565 crossMapP a f = concatP $ mapP (\x -> mapP ((,) x) (f x)) a
569 -- computes an index array for all elements of the second argument for which
570 -- the predicate yields `True' (EXPORTED)
572 indexOfP :: (a -> Bool) -> [:a:] -> [:Int:]
573 indexOfP p a = fst $ loop calcIdx 0 a
575 calcIdx e idx | p e = (Just idx, idx + 1)
576 | otherwise = (Nothing , idx )
579 -- auxiliary functions
580 -- -------------------
582 -- internally used mutable boxed arrays
584 data MPArr s e = MPArr Int# (MutableArray# s e)
586 -- allocate a new mutable array that is pre-initialised with a given value
588 newArray :: Int -> e -> ST s (MPArr s e)
589 {-# INLINE newArray #-}
590 newArray (I# n#) e = ST $ \s1# ->
591 case newArray# n# e s1# of { (# s2#, marr# #) ->
592 (# s2#, MPArr n# marr# #)}
594 -- convert a mutable array into the external parallel array representation
596 mkPArr :: Int -> MPArr s e -> ST s [:e:]
597 {-# INLINE mkPArr #-}
598 mkPArr (I# n#) (MPArr _ marr#) = ST $ \s1# ->
599 case unsafeFreezeArray# marr# s1# of { (# s2#, arr# #) ->
600 (# s2#, PArr n# arr# #) }
602 -- general array iterator
604 -- * corresponds to `loopA' from ``Functional Array Fusion'', Chakravarty &
607 loop :: (e -> acc -> (Maybe e', acc)) -- mapping & folding, once per element
608 -> acc -- initial acc value
609 -> [:e:] -- input array
612 loop mf acc arr = loopFromTo 0 (lengthP arr - 1) mf acc arr
614 -- general array iterator with bounds
616 loopFromTo :: Int -- from index
618 -> (e -> acc -> (Maybe e', acc))
622 {-# INLINE loopFromTo #-}
623 loopFromTo from to mf start arr = runST (do
624 marr <- newArray (to - from + 1) noElem
625 (n', acc) <- trans from to marr arr mf start
626 arr <- mkPArr n' marr
629 noElem = error "GHC.PArr.loopFromTo: I do not exist!"
630 -- unlike standard Haskell arrays, this value represents an
633 -- actual loop body of `loop'
635 -- * for this to be really efficient, it has to be translated with the
636 -- constructor specialisation phase "SpecConstr" switched on; as of GHC 5.03
637 -- this requires an optimisation level of at least -O2
639 trans :: Int -- index of first elem to process
640 -> Int -- index of last elem to process
641 -> MPArr s e' -- destination array
642 -> [:e:] -- source array
643 -> (e -> acc -> (Maybe e', acc)) -- mutator
644 -> acc -- initial accumulator
645 -> ST s (Int, acc) -- final destination length/final acc
647 trans from to marr arr mf start = trans' from 0 start
649 trans' arrOff marrOff acc
650 | arrOff > to = return (marrOff, acc)
652 let (oe', acc') = mf (arr `indexPArr` arrOff) acc
653 marrOff' <- case oe' of
654 Nothing -> return marrOff
656 writeMPArr marr marrOff e'
658 trans' (arrOff + 1) marrOff' acc'
660 -- Permute the given elements into the mutable array.
662 permute :: MPArr s e -> [:Int:] -> [:e:] -> ST s ()
663 permute marr is es = perm 0
667 | otherwise = writeMPArr marr (is!:i) (es!:i) >> perm (i + 1)
672 -- common patterns for using `loop'
675 -- initial value for the accumulator when the accumulator is not needed
680 -- `loop' mutator maps a function over array elements
682 mapEFL :: (e -> e') -> (e -> () -> (Maybe e', ()))
683 {-# INLINE mapEFL #-}
684 mapEFL f = \e a -> (Just $ f e, ())
686 -- `loop' mutator that filter elements according to a predicate
688 filterEFL :: (e -> Bool) -> (e -> () -> (Maybe e, ()))
689 {-# INLINE filterEFL #-}
690 filterEFL p = \e a -> if p e then (Just e, ()) else (Nothing, ())
692 -- `loop' mutator for array folding
694 foldEFL :: (e -> acc -> acc) -> (e -> acc -> (Maybe (), acc))
695 {-# INLINE foldEFL #-}
696 foldEFL f = \e a -> (Nothing, f e a)
698 -- `loop' mutator for array scanning
700 scanEFL :: (e -> acc -> acc) -> (e -> acc -> (Maybe acc, acc))
701 {-# INLINE scanEFL #-}
702 scanEFL f = \e a -> (Just a, f e a)
704 -- elementary array operations
707 -- unlifted array indexing
709 indexPArr :: [:e:] -> Int -> e
710 {-# INLINE indexPArr #-}
711 indexPArr (PArr n# arr#) (I# i#)
712 | i# >=# 0# && i# <# n# =
713 case indexArray# arr# i# of (# e #) -> e
714 | otherwise = error $ "indexPArr: out of bounds parallel array index; " ++
715 "idx = " ++ show (I# i#) ++ ", arr len = "
718 -- encapsulate writing into a mutable array into the `ST' monad
720 writeMPArr :: MPArr s e -> Int -> e -> ST s ()
721 {-# INLINE writeMPArr #-}
722 writeMPArr (MPArr n# marr#) (I# i#) e
723 | i# >=# 0# && i# <# n# =
725 case writeArray# marr# i# e s# of s'# -> (# s'#, () #)
726 | otherwise = error $ "writeMPArr: out of bounds parallel array index; " ++
727 "idx = " ++ show (I# i#) ++ ", arr len = "
730 #endif /* __HADDOCK__ */