1 {-# LANGUAGE CPP, ParallelArrays, MagicHash, UnboxedTuples #-}
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 -- !!!THIS FILE IS ABOUT TO GO AWAY!!!
16 -- [::] -- Built-in syntax
17 emptyPArr, replicatePArr, singletonPArr, indexPArr, lengthPArr
24 import GHC.ST ( ST(..), runST )
25 import GHC.Base ( Int#, Array#, Int(I#), MutableArray#, newArray#,
26 unsafeFreezeArray#, indexArray#, {- writeArray#, -} (<#), (>=#) )
29 -- representation of parallel arrays
30 -- ---------------------------------
32 -- this rather straight forward implementation maps parallel arrays to the
33 -- internal representation used for standard Haskell arrays in GHC's Prelude
34 -- (EXPORTED ABSTRACTLY)
36 -- * This definition *must* be kept in sync with `TysWiredIn.parrTyCon'!
38 data [::] e = PArr Int# (Array# e)
41 {-# NOINLINE emptyPArr #-}
42 emptyPArr = replicatePArr 0 undefined
44 replicatePArr :: Int -> a -> [:a:]
45 {-# NOINLINE replicatePArr #-}
46 replicatePArr n e = runST (do
50 singletonPArr :: a -> [:a:]
51 {-# NOINLINE singletonPArr #-}
52 singletonPArr e = replicatePArr 1 e
54 indexPArr :: [:e:] -> Int -> e
55 {-# NOINLINE indexPArr #-}
56 indexPArr (PArr n# arr#) (I# i#)
57 | i# >=# 0# && i# <# n# =
58 case indexArray# arr# i# of (# e #) -> e
59 | otherwise = error $ "indexPArr: out of bounds parallel array index; " ++
60 "idx = " ++ show (I# i#) ++ ", arr len = "
63 lengthPArr :: [:a:] -> Int
64 {-# NOINLINE lengthPArr #-}
65 lengthPArr (PArr n# _) = I# n#
67 -- auxiliary functions
68 -- -------------------
70 -- internally used mutable boxed arrays
72 data MPArr s e = MPArr Int# (MutableArray# s e)
74 -- allocate a new mutable array that is pre-initialised with a given value
76 newArray :: Int -> e -> ST s (MPArr s e)
77 {-# INLINE newArray #-}
78 newArray (I# n#) e = ST $ \s1# ->
79 case newArray# n# e s1# of { (# s2#, marr# #) ->
80 (# s2#, MPArr n# marr# #)}
82 -- convert a mutable array into the external parallel array representation
84 mkPArr :: Int -> MPArr s e -> ST s [:e:]
86 mkPArr (I# n#) (MPArr _ marr#) = ST $ \s1# ->
87 case unsafeFreezeArray# marr# s1# of { (# s2#, arr# #) ->
88 (# s2#, PArr n# arr# #) }