1 {-# OPTIONS_GHC -fparr -funbox-strict-fields #-}
3 -----------------------------------------------------------------------------
6 -- Copyright : (c) 2001-2002 Manuel M T Chakravarty & Gabriele Keller
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : Manuel M. T. Chakravarty <chak@cse.unsw.edu.au>
10 -- Stability : internal
11 -- Portability : non-portable (GHC Extensions)
13 -- Basic implementation of Parallel Arrays.
15 -- This module has two functions: (1) It defines the interface to the
16 -- parallel array extension of the Prelude and (2) it provides a vanilla
17 -- implementation of parallel arrays that does not require to flatten the
18 -- array code. The implementation is not very optimised.
20 --- DOCU ----------------------------------------------------------------------
22 -- Language: Haskell 98 plus unboxed values and parallel arrays
24 -- The semantic difference between standard Haskell arrays (aka "lazy
25 -- arrays") and parallel arrays (aka "strict arrays") is that the evaluation
26 -- of two different elements of a lazy array is independent, whereas in a
27 -- strict array either non or all elements are evaluated. In other words,
28 -- when a parallel array is evaluated to WHNF, all its elements will be
29 -- evaluated to WHNF. The name parallel array indicates that all array
30 -- elements may, in general, be evaluated to WHNF in parallel without any
31 -- need to resort to speculative evaluation. This parallel evaluation
32 -- semantics is also beneficial in the sequential case, as it facilitates
33 -- loop-based array processing as known from classic array-based languages,
36 -- The interface of this module is essentially a variant of the list
37 -- component of the Prelude, but also includes some functions (such as
38 -- permutations) that are not provided for lists. The following list
39 -- operations are not supported on parallel arrays, as they would require the
40 -- availability of infinite parallel arrays: `iterate', `repeat', and `cycle'.
42 -- The current implementation is quite simple and entirely based on boxed
43 -- arrays. One disadvantage of boxed arrays is that they require to
44 -- immediately initialise all newly allocated arrays with an error thunk to
45 -- keep the garbage collector happy, even if it is guaranteed that the array
46 -- is fully initialised with different values before passing over the
47 -- user-visible interface boundary. Currently, no effort is made to use
48 -- raw memory copy operations to speed things up.
50 --- TODO ----------------------------------------------------------------------
52 -- * We probably want a standard library `PArray' in addition to the prelude
53 -- extension in the same way as the standard library `List' complements the
54 -- list functions from the prelude.
56 -- * Currently, functions that emphasis the constructor-based definition of
57 -- lists (such as, head, last, tail, and init) are not supported.
59 -- Is it worthwhile to support the string processing functions lines,
60 -- words, unlines, and unwords? (Currently, they are not implemented.)
62 -- It can, however, be argued that it would be worthwhile to include them
63 -- for completeness' sake; maybe only in the standard library `PArray'.
65 -- * Prescans are often more useful for array programming than scans. Shall
66 -- we include them into the Prelude or the library?
68 -- * Due to the use of the iterator `loop', we could define some fusion rules
71 -- * We might want to add bounds checks that can be deactivated.
75 -- [::], -- Built-in syntax
77 mapP, -- :: (a -> b) -> [:a:] -> [:b:]
78 (+:+), -- :: [:a:] -> [:a:] -> [:a:]
79 filterP, -- :: (a -> Bool) -> [:a:] -> [:a:]
80 concatP, -- :: [:[:a:]:] -> [:a:]
81 concatMapP, -- :: (a -> [:b:]) -> [:a:] -> [:b:]
82 -- head, last, tail, init, -- it's not wise to use them on arrays
83 nullP, -- :: [:a:] -> Bool
84 lengthP, -- :: [:a:] -> Int
85 (!:), -- :: [:a:] -> Int -> a
86 foldlP, -- :: (a -> b -> a) -> a -> [:b:] -> a
87 foldl1P, -- :: (a -> a -> a) -> [:a:] -> a
88 scanlP, -- :: (a -> b -> a) -> a -> [:b:] -> [:a:]
89 scanl1P, -- :: (a -> a -> a) -> [:a:] -> [:a:]
90 foldrP, -- :: (a -> b -> b) -> b -> [:a:] -> b
91 foldr1P, -- :: (a -> a -> a) -> [:a:] -> a
92 scanrP, -- :: (a -> b -> b) -> b -> [:a:] -> [:b:]
93 scanr1P, -- :: (a -> a -> a) -> [:a:] -> [:a:]
94 -- iterate, repeat, -- parallel arrays must be finite
95 singletonP, -- :: a -> [:a:]
97 replicateP, -- :: Int -> a -> [:a:]
98 -- cycle, -- parallel arrays must be finite
99 takeP, -- :: Int -> [:a:] -> [:a:]
100 dropP, -- :: Int -> [:a:] -> [:a:]
101 splitAtP, -- :: Int -> [:a:] -> ([:a:],[:a:])
102 takeWhileP, -- :: (a -> Bool) -> [:a:] -> [:a:]
103 dropWhileP, -- :: (a -> Bool) -> [:a:] -> [:a:]
104 spanP, -- :: (a -> Bool) -> [:a:] -> ([:a:], [:a:])
105 breakP, -- :: (a -> Bool) -> [:a:] -> ([:a:], [:a:])
106 -- lines, words, unlines, unwords, -- is string processing really needed
107 reverseP, -- :: [:a:] -> [:a:]
108 andP, -- :: [:Bool:] -> Bool
109 orP, -- :: [:Bool:] -> Bool
110 anyP, -- :: (a -> Bool) -> [:a:] -> Bool
111 allP, -- :: (a -> Bool) -> [:a:] -> Bool
112 elemP, -- :: (Eq a) => a -> [:a:] -> Bool
113 notElemP, -- :: (Eq a) => a -> [:a:] -> Bool
114 lookupP, -- :: (Eq a) => a -> [:(a, b):] -> Maybe b
115 sumP, -- :: (Num a) => [:a:] -> a
116 productP, -- :: (Num a) => [:a:] -> a
117 maximumP, -- :: (Ord a) => [:a:] -> a
118 minimumP, -- :: (Ord a) => [:a:] -> a
119 zipP, -- :: [:a:] -> [:b:] -> [:(a, b) :]
120 zip3P, -- :: [:a:] -> [:b:] -> [:c:] -> [:(a, b, c):]
121 zipWithP, -- :: (a -> b -> c) -> [:a:] -> [:b:] -> [:c:]
122 zipWith3P, -- :: (a -> b -> c -> d) -> [:a:]->[:b:]->[:c:]->[:d:]
123 unzipP, -- :: [:(a, b) :] -> ([:a:], [:b:])
124 unzip3P, -- :: [:(a, b, c):] -> ([:a:], [:b:], [:c:])
126 -- overloaded functions
128 enumFromToP, -- :: Enum a => a -> a -> [:a:]
129 enumFromThenToP, -- :: Enum a => a -> a -> a -> [:a:]
131 -- the following functions are not available on lists
133 toP, -- :: [a] -> [:a:]
134 fromP, -- :: [:a:] -> [a]
135 sliceP, -- :: Int -> Int -> [:e:] -> [:e:]
136 foldP, -- :: (e -> e -> e) -> e -> [:e:] -> e
137 fold1P, -- :: (e -> e -> e) -> [:e:] -> e
138 permuteP, -- :: [:Int:] -> [:e:] -> [:e:]
139 bpermuteP, -- :: [:Int:] -> [:e:] -> [:e:]
140 dpermuteP, -- :: [:Int:] -> [:e:] -> [:e:] -> [:e:]
141 crossP, -- :: [:a:] -> [:b:] -> [:(a, b):]
142 crossMapP, -- :: [:a:] -> (a -> [:b:]) -> [:(a, b):]
143 indexOfP -- :: (a -> Bool) -> [:a:] -> [:Int:]
150 import GHC.ST ( ST(..), STRep, runST )
151 import GHC.Exts ( Int#, Array#, Int(I#), MutableArray#, newArray#,
152 unsafeFreezeArray#, indexArray#, writeArray#, (<#), (>=#) )
156 infix 4 `elemP`, `notElemP`
159 -- representation of parallel arrays
160 -- ---------------------------------
162 -- this rather straight forward implementation maps parallel arrays to the
163 -- internal representation used for standard Haskell arrays in GHC's Prelude
164 -- (EXPORTED ABSTRACTLY)
166 -- * This definition *must* be kept in sync with `TysWiredIn.parrTyCon'!
168 data [::] e = PArr Int# (Array# e)
171 -- exported operations on parallel arrays
172 -- --------------------------------------
174 -- operations corresponding to list operations
177 mapP :: (a -> b) -> [:a:] -> [:b:]
178 mapP f = fst . loop (mapEFL f) noAL
180 (+:+) :: [:a:] -> [:a:] -> [:a:]
181 a1 +:+ a2 = fst $ loop (mapEFL sel) noAL (enumFromToP 0 (len1 + len2 - 1))
182 -- we can't use the [:x..y:] form here for tedious
183 -- reasons to do with the typechecker and the fact that
184 -- `enumFromToP' is defined in the same module
189 sel i | i < len1 = a1!:i
190 | otherwise = a2!:(i - len1)
192 filterP :: (a -> Bool) -> [:a:] -> [:a:]
193 filterP p = fst . loop (filterEFL p) noAL
195 concatP :: [:[:a:]:] -> [:a:]
196 concatP xss = foldlP (+:+) [::] xss
198 concatMapP :: (a -> [:b:]) -> [:a:] -> [:b:]
199 concatMapP f = concatP . mapP f
201 -- head, last, tail, init, -- it's not wise to use them on arrays
203 nullP :: [:a:] -> Bool
207 lengthP :: [:a:] -> Int
208 lengthP (PArr n# _) = I# n#
210 (!:) :: [:a:] -> Int -> a
213 foldlP :: (a -> b -> a) -> a -> [:b:] -> a
214 foldlP f z = snd . loop (foldEFL (flip f)) z
216 foldl1P :: (a -> a -> a) -> [:a:] -> a
217 foldl1P f [::] = error "Prelude.foldl1P: empty array"
218 foldl1P f a = snd $ loopFromTo 1 (lengthP a - 1) (foldEFL f) (a!:0) a
220 scanlP :: (a -> b -> a) -> a -> [:b:] -> [:a:]
221 scanlP f z = fst . loop (scanEFL (flip f)) z
223 scanl1P :: (a -> a -> a) -> [:a:] -> [:a:]
224 scanl1P f [::] = error "Prelude.scanl1P: empty array"
225 scanl1P f a = fst $ loopFromTo 1 (lengthP a - 1) (scanEFL f) (a!:0) a
227 foldrP :: (a -> b -> b) -> b -> [:a:] -> b
228 foldrP = error "Prelude.foldrP: not implemented yet" -- FIXME
230 foldr1P :: (a -> a -> a) -> [:a:] -> a
231 foldr1P = error "Prelude.foldr1P: not implemented yet" -- FIXME
233 scanrP :: (a -> b -> b) -> b -> [:a:] -> [:b:]
234 scanrP = error "Prelude.scanrP: not implemented yet" -- FIXME
236 scanr1P :: (a -> a -> a) -> [:a:] -> [:a:]
237 scanr1P = error "Prelude.scanr1P: not implemented yet" -- FIXME
239 -- iterate, repeat -- parallel arrays must be finite
241 singletonP :: a -> [:a:]
242 {-# INLINE singletonP #-}
243 singletonP e = replicateP 1 e
246 emptyP = error "emptyP in GHC.PArr: not yet implemented"
248 replicateP :: Int -> a -> [:a:]
249 {-# INLINE replicateP #-}
250 replicateP n e = runST (do
251 marr# <- newArray n e
254 -- cycle -- parallel arrays must be finite
256 takeP :: Int -> [:a:] -> [:a:]
257 takeP n = sliceP 0 (n - 1)
259 dropP :: Int -> [:a:] -> [:a:]
260 dropP n a = sliceP n (lengthP a - 1) a
262 splitAtP :: Int -> [:a:] -> ([:a:],[:a:])
263 splitAtP n xs = (takeP n xs, dropP n xs)
265 takeWhileP :: (a -> Bool) -> [:a:] -> [:a:]
266 takeWhileP = error "Prelude.takeWhileP: not implemented yet" -- FIXME
268 dropWhileP :: (a -> Bool) -> [:a:] -> [:a:]
269 dropWhileP = error "Prelude.dropWhileP: not implemented yet" -- FIXME
271 spanP :: (a -> Bool) -> [:a:] -> ([:a:], [:a:])
272 spanP = error "Prelude.spanP: not implemented yet" -- FIXME
274 breakP :: (a -> Bool) -> [:a:] -> ([:a:], [:a:])
275 breakP p = spanP (not . p)
277 -- lines, words, unlines, unwords, -- is string processing really needed
279 reverseP :: [:a:] -> [:a:]
280 reverseP a = permuteP (enumFromThenToP (len - 1) (len - 2) 0) a
281 -- we can't use the [:x, y..z:] form here for tedious
282 -- reasons to do with the typechecker and the fact that
283 -- `enumFromThenToP' is defined in the same module
287 andP :: [:Bool:] -> Bool
288 andP = foldP (&&) True
290 orP :: [:Bool:] -> Bool
291 orP = foldP (||) True
293 anyP :: (a -> Bool) -> [:a:] -> Bool
294 anyP p = orP . mapP p
296 allP :: (a -> Bool) -> [:a:] -> Bool
297 allP p = andP . mapP p
299 elemP :: (Eq a) => a -> [:a:] -> Bool
300 elemP x = anyP (== x)
302 notElemP :: (Eq a) => a -> [:a:] -> Bool
303 notElemP x = allP (/= x)
305 lookupP :: (Eq a) => a -> [:(a, b):] -> Maybe b
306 lookupP = error "Prelude.lookupP: not implemented yet" -- FIXME
308 sumP :: (Num a) => [:a:] -> a
311 productP :: (Num a) => [:a:] -> a
312 productP = foldP (*) 1
314 maximumP :: (Ord a) => [:a:] -> a
315 maximumP [::] = error "Prelude.maximumP: empty parallel array"
316 maximumP xs = fold1P max xs
318 minimumP :: (Ord a) => [:a:] -> a
319 minimumP [::] = error "Prelude.minimumP: empty parallel array"
320 minimumP xs = fold1P min xs
322 zipP :: [:a:] -> [:b:] -> [:(a, b):]
325 zip3P :: [:a:] -> [:b:] -> [:c:] -> [:(a, b, c):]
326 zip3P = zipWith3P (,,)
328 zipWithP :: (a -> b -> c) -> [:a:] -> [:b:] -> [:c:]
329 zipWithP f a1 a2 = let
332 len = len1 `min` len2
334 fst $ loopFromTo 0 (len - 1) combine 0 a1
336 combine e1 i = (Just $ f e1 (a2!:i), i + 1)
338 zipWith3P :: (a -> b -> c -> d) -> [:a:]->[:b:]->[:c:]->[:d:]
339 zipWith3P f a1 a2 a3 = let
343 len = len1 `min` len2 `min` len3
345 fst $ loopFromTo 0 (len - 1) combine 0 a1
347 combine e1 i = (Just $ f e1 (a2!:i) (a3!:i), i + 1)
349 unzipP :: [:(a, b):] -> ([:a:], [:b:])
350 unzipP a = (fst $ loop (mapEFL fst) noAL a, fst $ loop (mapEFL snd) noAL a)
351 -- FIXME: these two functions should be optimised using a tupled custom loop
352 unzip3P :: [:(a, b, c):] -> ([:a:], [:b:], [:c:])
353 unzip3P a = (fst $ loop (mapEFL fst3) noAL a,
354 fst $ loop (mapEFL snd3) noAL a,
355 fst $ loop (mapEFL trd3) noAL a)
364 instance Eq a => Eq [:a:] where
365 a1 == a2 | lengthP a1 == lengthP a2 = andP (zipWithP (==) a1 a2)
368 instance Ord a => Ord [:a:] where
369 compare a1 a2 = case foldlP combineOrdering EQ (zipWithP compare a1 a2) of
370 EQ | lengthP a1 == lengthP a2 -> EQ
371 | lengthP a1 < lengthP a2 -> LT
374 combineOrdering EQ EQ = EQ
375 combineOrdering EQ other = other
376 combineOrdering other _ = other
378 instance Functor [::] where
381 instance Monad [::] where
382 m >>= k = foldrP ((+:+) . k ) [::] m
383 m >> k = foldrP ((+:+) . const k) [::] m
387 instance Show a => Show [:a:] where
388 showsPrec _ = showPArr . fromP
390 showPArr [] s = "[::]" ++ s
391 showPArr (x:xs) s = "[:" ++ shows x (showPArr' xs s)
393 showPArr' [] s = ":]" ++ s
394 showPArr' (y:ys) s = ',' : shows y (showPArr' ys s)
396 instance Read a => Read [:a:] where
397 readsPrec _ a = [(toP v, rest) | (v, rest) <- readPArr a]
399 readPArr = readParen False (\r -> do
403 (do { (":]", t) <- lex s; return ([], t) }) ++
404 (do { (x, t) <- reads s; (xs, u) <- readPArr2 t; return (x:xs, u) })
407 (do { (":]", t) <- lex s; return ([], t) }) ++
408 (do { (",", t) <- lex s; (x, u) <- reads t; (xs, v) <- readPArr2 u;
411 -- overloaded functions
414 -- Ideally, we would like `enumFromToP' and `enumFromThenToP' to be members of
415 -- `Enum'. On the other hand, we really do not want to change `Enum'. Thus,
416 -- for the moment, we hope that the compiler is sufficiently clever to
417 -- properly fuse the following definitions.
419 enumFromToP :: Enum a => a -> a -> [:a:]
420 enumFromToP x y = mapP toEnum (eftInt (fromEnum x) (fromEnum y))
422 eftInt x y = scanlP (+) x $ replicateP (y - x + 1) 1
424 enumFromThenToP :: Enum a => a -> a -> a -> [:a:]
425 enumFromThenToP x y z =
426 mapP toEnum (efttInt (fromEnum x) (fromEnum y) (fromEnum z))
428 efttInt x y z = scanlP (+) x $
429 replicateP (abs (z - x) `div` abs delta + 1) delta
433 -- the following functions are not available on lists
436 -- create an array from a list (EXPORTED)
439 toP l = fst $ loop store l (replicateP (length l) ())
441 store _ (x:xs) = (Just x, xs)
443 -- convert an array to a list (EXPORTED)
445 fromP :: [:a:] -> [a]
446 fromP a = [a!:i | i <- [0..lengthP a - 1]]
448 -- cut a subarray out of an array (EXPORTED)
450 sliceP :: Int -> Int -> [:e:] -> [:e:]
452 fst $ loopFromTo (0 `max` from) (to `min` (lengthP a - 1)) (mapEFL id) noAL a
454 -- parallel folding (EXPORTED)
456 -- * the first argument must be associative; otherwise, the result is undefined
458 foldP :: (e -> e -> e) -> e -> [:e:] -> e
461 -- parallel folding without explicit neutral (EXPORTED)
463 -- * the first argument must be associative; otherwise, the result is undefined
465 fold1P :: (e -> e -> e) -> [:e:] -> e
468 -- permute an array according to the permutation vector in the first argument
471 permuteP :: [:Int:] -> [:e:] -> [:e:]
473 | isLen /= esLen = error "GHC.PArr: arguments must be of the same length"
474 | otherwise = runST (do
475 marr <- newArray isLen noElem
479 noElem = error "GHC.PArr.permuteP: I do not exist!"
480 -- unlike standard Haskell arrays, this value represents an
485 -- permute an array according to the back-permutation vector in the first
486 -- argument (EXPORTED)
488 -- * the permutation vector must represent a surjective function; otherwise,
489 -- the result is undefined
491 bpermuteP :: [:Int:] -> [:e:] -> [:e:]
492 bpermuteP is es = fst $ loop (mapEFL (es!:)) noAL is
494 -- permute an array according to the permutation vector in the first
495 -- argument, which need not be surjective (EXPORTED)
497 -- * any elements in the result that are not covered by the permutation
498 -- vector assume the value of the corresponding position of the third
501 dpermuteP :: [:Int:] -> [:e:] -> [:e:] -> [:e:]
503 | isLen /= esLen = error "GHC.PArr: arguments must be of the same length"
504 | otherwise = runST (do
505 marr <- newArray dftLen noElem
506 trans 0 (isLen - 1) marr dft copyOne noAL
510 noElem = error "GHC.PArr.permuteP: I do not exist!"
511 -- unlike standard Haskell arrays, this value represents an
517 copyOne e _ = (Just e, noAL)
519 -- computes the cross combination of two arrays (EXPORTED)
521 crossP :: [:a:] -> [:b:] -> [:(a, b):]
522 crossP a1 a2 = fst $ loop combine (0, 0) $ replicateP len ()
528 combine _ (i, j) = (Just $ (a1!:i, a2!:j), next)
530 next | (i + 1) == len1 = (0 , j + 1)
531 | otherwise = (i + 1, j)
533 {- An alternative implementation
534 * The one above is certainly better for flattened code, but here where we
535 are handling boxed arrays, the trade off is less clear. However, I
536 think, the above one is still better.
541 x1 = concatP $ mapP (replicateP len2) a1
542 x2 = concatP $ replicateP len1 a2
547 -- |Compute a cross of an array and the arrays produced by the given function
548 -- for the elements of the first array.
550 crossMapP :: [:a:] -> (a -> [:b:]) -> [:(a, b):]
553 segd = mapP lengthP bs
554 as = zipWithP replicateP segd a
556 zipP (concatP as) (concatP bs)
558 {- The following may seem more straight forward, but the above is very cheap
559 with segmented arrays, as `mapP lengthP', `zipP', and `concatP' are
560 constant time, and `map f' uses the lifted version of `f'.
562 crossMapP a f = concatP $ mapP (\x -> mapP ((,) x) (f x)) a
566 -- computes an index array for all elements of the second argument for which
567 -- the predicate yields `True' (EXPORTED)
569 indexOfP :: (a -> Bool) -> [:a:] -> [:Int:]
570 indexOfP p a = fst $ loop calcIdx 0 a
572 calcIdx e idx | p e = (Just idx, idx + 1)
573 | otherwise = (Nothing , idx )
576 -- auxiliary functions
577 -- -------------------
579 -- internally used mutable boxed arrays
581 data MPArr s e = MPArr Int# (MutableArray# s e)
583 -- allocate a new mutable array that is pre-initialised with a given value
585 newArray :: Int -> e -> ST s (MPArr s e)
586 {-# INLINE newArray #-}
587 newArray (I# n#) e = ST $ \s1# ->
588 case newArray# n# e s1# of { (# s2#, marr# #) ->
589 (# s2#, MPArr n# marr# #)}
591 -- convert a mutable array into the external parallel array representation
593 mkPArr :: Int -> MPArr s e -> ST s [:e:]
594 {-# INLINE mkPArr #-}
595 mkPArr (I# n#) (MPArr _ marr#) = ST $ \s1# ->
596 case unsafeFreezeArray# marr# s1# of { (# s2#, arr# #) ->
597 (# s2#, PArr n# arr# #) }
599 -- general array iterator
601 -- * corresponds to `loopA' from ``Functional Array Fusion'', Chakravarty &
604 loop :: (e -> acc -> (Maybe e', acc)) -- mapping & folding, once per element
605 -> acc -- initial acc value
606 -> [:e:] -- input array
609 loop mf acc arr = loopFromTo 0 (lengthP arr - 1) mf acc arr
611 -- general array iterator with bounds
613 loopFromTo :: Int -- from index
615 -> (e -> acc -> (Maybe e', acc))
619 {-# INLINE loopFromTo #-}
620 loopFromTo from to mf start arr = runST (do
621 marr <- newArray (to - from + 1) noElem
622 (n', acc) <- trans from to marr arr mf start
623 arr <- mkPArr n' marr
626 noElem = error "GHC.PArr.loopFromTo: I do not exist!"
627 -- unlike standard Haskell arrays, this value represents an
630 -- actual loop body of `loop'
632 -- * for this to be really efficient, it has to be translated with the
633 -- constructor specialisation phase "SpecConstr" switched on; as of GHC 5.03
634 -- this requires an optimisation level of at least -O2
636 trans :: Int -- index of first elem to process
637 -> Int -- index of last elem to process
638 -> MPArr s e' -- destination array
639 -> [:e:] -- source array
640 -> (e -> acc -> (Maybe e', acc)) -- mutator
641 -> acc -- initial accumulator
642 -> ST s (Int, acc) -- final destination length/final acc
644 trans from to marr arr mf start = trans' from 0 start
646 trans' arrOff marrOff acc
647 | arrOff > to = return (marrOff, acc)
649 let (oe', acc') = mf (arr `indexPArr` arrOff) acc
650 marrOff' <- case oe' of
651 Nothing -> return marrOff
653 writeMPArr marr marrOff e'
655 trans' (arrOff + 1) marrOff' acc'
657 -- Permute the given elements into the mutable array.
659 permute :: MPArr s e -> [:Int:] -> [:e:] -> ST s ()
660 permute marr is es = perm 0
664 | otherwise = writeMPArr marr (is!:i) (es!:i) >> perm (i + 1)
669 -- common patterns for using `loop'
672 -- initial value for the accumulator when the accumulator is not needed
677 -- `loop' mutator maps a function over array elements
679 mapEFL :: (e -> e') -> (e -> () -> (Maybe e', ()))
680 {-# INLINE mapEFL #-}
681 mapEFL f = \e a -> (Just $ f e, ())
683 -- `loop' mutator that filter elements according to a predicate
685 filterEFL :: (e -> Bool) -> (e -> () -> (Maybe e, ()))
686 {-# INLINE filterEFL #-}
687 filterEFL p = \e a -> if p e then (Just e, ()) else (Nothing, ())
689 -- `loop' mutator for array folding
691 foldEFL :: (e -> acc -> acc) -> (e -> acc -> (Maybe (), acc))
692 {-# INLINE foldEFL #-}
693 foldEFL f = \e a -> (Nothing, f e a)
695 -- `loop' mutator for array scanning
697 scanEFL :: (e -> acc -> acc) -> (e -> acc -> (Maybe acc, acc))
698 {-# INLINE scanEFL #-}
699 scanEFL f = \e a -> (Just a, f e a)
701 -- elementary array operations
704 -- unlifted array indexing
706 indexPArr :: [:e:] -> Int -> e
707 {-# INLINE indexPArr #-}
708 indexPArr (PArr n# arr#) (I# i#)
709 | i# >=# 0# && i# <# n# =
710 case indexArray# arr# i# of (# e #) -> e
711 | otherwise = error $ "indexPArr: out of bounds parallel array index; " ++
712 "idx = " ++ show (I# i#) ++ ", arr len = "
715 -- encapsulate writing into a mutable array into the `ST' monad
717 writeMPArr :: MPArr s e -> Int -> e -> ST s ()
718 {-# INLINE writeMPArr #-}
719 writeMPArr (MPArr n# marr#) (I# i#) e
720 | i# >=# 0# && i# <# n# =
722 case writeArray# marr# i# e s# of s'# -> (# s'#, () #)
723 | otherwise = error $ "writeMPArr: out of bounds parallel array index; " ++
724 "idx = " ++ show (I# i#) ++ ", arr len = "
727 #endif /* __HADDOCK__ */