-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Bulk array representations and operators. -- @package repa-array @version 4.0.0.2 module Data.Repa.Nice.Display -- | How a given value should be displayed. data Display Display :: Format -> Int -> Display -- | Common display formats. data Format FormatNumeric :: Format FormatText :: Format -- | Display a string with the given mode. display :: Display -> Text -> Text -- | Examine a string to decide how we should display it. takeDisplay :: Text -> Display -- | Left justify some text in a column of the given width. padL :: Int -> Text -> Text -- | Right justify some text in a column of the given width. padR :: Int -> Text -> Text instance Eq Format instance Show Format instance Eq Display instance Show Display instance Monoid Display module Data.Repa.Fusion.Unpack -- | Unpack the pieces of a structure into a tuple. -- -- This is used in a low-level fusion optimisation to ensure that -- intermediate values are unboxed. class Unpack a t | a -> t unpack :: Unpack a t => a -> t repack :: Unpack a t => a -> t -> a module Data.Repa.Array.RowWise -- | A row-wise layout that maps higher rank indices to linear ones in a -- row-major order. -- -- Indices are ordered so the inner-most coordinate varies most -- frequently: -- --
-- > Prelude.map (fromIndex (RowWise (ish2 2 3))) [0..5] -- [(Z :. 0) :. 0, (Z :. 0) :. 1, (Z :. 0) :. 2, -- (Z :. 1) :. 0, (Z :. 1) :. 1, (Z :. 1) :. 2] ---- --
-- > toList $ rowWise (ish2 3 2) -- [(Z :. 0) :. 0, (Z :. 0) :. 1, -- (Z :. 1) :. 0, (Z :. 1) :. 1, -- (Z :. 2) :. 0, (Z :. 2) :. 1] --rowWise :: sh -> Array (RW sh) sh type DIM1 = RW SH1 type DIM2 = RW SH2 type DIM3 = RW SH3 type DIM4 = RW SH4 type DIM5 = RW SH5 ix1 :: Int -> DIM1 ix2 :: Int -> Int -> DIM2 ix3 :: Int -> Int -> Int -> DIM3 ix4 :: Int -> Int -> Int -> Int -> DIM4 ix5 :: Int -> Int -> Int -> Int -> Int -> DIM5 instance Show (Name (RW sh)) => Show (Name (RW (sh :. Int))) instance Eq (Name (RW sh)) => Eq (Name (RW (sh :. Int))) instance Show (Name (RW Z)) instance Eq (Name (RW Z)) instance Show sh => Show (RW sh) instance Eq sh => Eq (RW sh) instance (Layout (RW sh), Index (RW sh) ~ sh) => Bulk (RW sh) sh instance (Layout (RW sh), Index (RW sh) ~ sh) => Layout (RW (sh :. Int)) instance Layout (RW Z) instance Shape sh => Shape (RW sh) -- | Shapes and Indices module Data.Repa.Array.Index -- | Class of types that can be used as array shapes and indices. class Eq sh => Shape sh rank :: Shape sh => sh -> Int zeroDim :: Shape sh => sh unitDim :: Shape sh => sh intersectDim :: Shape sh => sh -> sh -> sh addDim :: Shape sh => sh -> sh -> sh size :: Shape sh => sh -> Int inShapeRange :: Shape sh => sh -> sh -> sh -> Bool listOfShape :: Shape sh => sh -> [Int] shapeOfList :: Shape sh => [Int] -> Maybe sh -- | Given an array shape and index, check whether the index is in the -- shape. inShape :: Shape sh => sh -> sh -> Bool -- | Nicely format a shape as a string showShape :: Shape sh => sh -> String -- | An index of dimension zero data Z Z :: Z -- | Our index type, used for both shapes and indices. data (:.) tail head (:.) :: !tail -> !head -> (:.) tail head type SH0 = Z type SH1 = SH0 :. Int type SH2 = SH1 :. Int type SH3 = SH2 :. Int type SH4 = SH3 :. Int type SH5 = SH4 :. Int ish0 :: SH0 ish1 :: Int -> SH1 ish2 :: Int -> Int -> SH2 ish3 :: Int -> Int -> Int -> SH3 ish4 :: Int -> Int -> Int -> Int -> SH4 ish5 :: Int -> Int -> Int -> Int -> Int -> SH5 -- | A layout provides a total order on the elements of an index space. -- -- We can talk about the n-th element of an array, independent of its -- shape and dimensionality. class Shape (Index l) => Layout l where data family Name l type family Index l name :: Layout l => Name l create :: Layout l => Name l -> Index l -> l extent :: Layout l => l -> Index l toIndex :: Layout l => l -> Index l -> Int fromIndex :: Layout l => l -> Int -> Index l type LayoutI l = (Layout l, Index l ~ Int) -- | Index space transformation between arrays and slices. module Data.Repa.Array.Index.Slice -- | Select all indices at a certain position. data All All :: All -- | Place holder for any possible shape. data Any sh Any :: Any sh -- | Map a type of the index in the full shape, to the type of the index in -- the slice. -- | Map the type of an index in the slice, to the type of the index in the -- full shape. -- | Class of index types that can map to slices. class Slice ss sliceOfFull :: Slice ss => ss -> FullShape ss -> SliceShape ss fullOfSlice :: Slice ss => ss -> SliceShape ss -> FullShape ss instance Slice sl => Slice (sl :. All) instance Slice sl => Slice (sl :. Int) instance Slice (Any sh) instance Slice Z module Data.Repa.Array.Window data W l Window :: Index l -> Index l -> l -> W l windowStart :: W l -> Index l windowSize :: W l -> Index l windowInner :: W l -> l -- | Class of array representations that can be windowed directly. -- -- The underlying representation can encode the window, without needing -- to add a wrapper to the existing layout. class Bulk l a => Windowable l a window :: Windowable l a => Index l -> Index l -> Array l a -> Array l a -- | Wrap a window around an exiting array. windowed :: Index l -> Index l -> Array l a -> Array (W l) a -- | Wrap a window around an existing array that encompases the entire -- array. entire :: Bulk l a => Array l a -> Array (W l) a instance Show (Name l) => Show (Name (W l)) instance Eq (Name l) => Eq (Name (W l)) instance (Eq l, Eq (Index l)) => Eq (W l) instance (Show l, Show (Index l)) => Show (W l) instance Bulk l a => Windowable (W l) a instance Bulk l a => Bulk (W l) a instance Layout l => Layout (W l) module Data.Repa.Array.Material.Boxed -- | Layout an array as flat vector of boxed elements. -- -- UNSAFE: Indexing into raw material arrays is not bounds checked. You -- may want to wrap this with a Checked layout as well. data B Boxed :: !Int -> B boxedLength :: B -> !Int -- | O(1). Wrap a boxed vector as an array. fromBoxed :: Vector a -> Array B a -- | O(1). Unwrap a boxed vector from an array. toBoxed :: Array B a -> Vector a -- | Scan through an array from front to back. For pairs of successive -- elements, drop the second one when the given predicate returns true. -- -- This function can be used to remove duplicates from a sorted array. -- -- TODO: generalise to other array types. decimate :: (a -> a -> Bool) -> Array B a -> Array B a instance Show a => Show (Array B a) instance Show (Name B) instance Eq (Name B) instance Show B instance Eq B instance Unpack (Buffer s B a) (MVector s a) instance Target B a instance Windowable B a instance Bulk B a instance Layout B module Data.Repa.Array.Delayed -- | Delayed arrays wrap functions from an index to element value. The -- index space is specified by an inner layout, l. -- -- Every time you index into a delayed array the element at that position -- is recomputed. data D l Delayed :: l -> D l delayedLayout :: D l -> l -- | Wrap a function as a delayed array. -- --
-- > toList $ fromFunction (Linear 10) (* 2) -- = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] --fromFunction :: l -> (Index l -> a) -> Array (D l) a -- | Produce the extent of an array, and a function to retrieve an -- arbitrary element. toFunction :: Bulk l a => Array (D l) a -> (l, Index l -> a) -- | Wrap an existing array in a delayed one. delay :: Bulk l a => Array l a -> Array (D l) a -- | Apply a worker function to each element of an array, yielding a new -- array with the same extent. -- -- The resulting array is delayed, meaning every time you index into it -- the element at that index is recomputed. map :: Bulk l a => (a -> b) -> Array l a -> Array (D l) b instance Show (Name l) => Show (Name (D l)) instance Eq (Name l) => Eq (Name (D l)) instance Show l => Show (D l) instance Eq l => Eq (D l) instance (Layout l1, Target l2 a) => Load (D l1) l2 a instance Layout l => Bulk (D l) a instance Layout l => Layout (D l) module Data.Repa.Array.Material.Foreign -- | Layout for Foreign arrays. -- -- UNSAFE: Indexing into raw material arrays is not bounds checked. You -- may want to wrap this with a Checked layout as well. data F Foreign :: Int -> F foreignLength :: F -> Int -- | O(1). Wrap a ForeignPtr as an array. fromForeignPtr :: Storable a => Int -> ForeignPtr a -> Array F a toForeignPtr :: Storable a => Array F a -> (Int, Int, ForeignPtr a) -- | O(1). Convert a storable Vector to a foreign Array fromStorableVector :: Vector a -> Array F a -- | O(1). Convert a foreign array to a storable Vector. toStorableVector :: Array F a -> Vector a -- | O(1). Convert a ByteString to an foreign Array. fromByteString :: ByteString -> Array F Word8 -- | O(1). Convert a foreign Vector to a ByteString. toByteString :: Array F Word8 -> ByteString instance (Storable a, Show a) => Show (Array F a) instance Show (Name F) instance Eq (Name F) instance Show F instance Eq F instance (Eq a, Storable a) => Eq (Array F a) instance Unpack (Buffer s F a) (MVector s a) instance Storable a => Target F a instance Storable a => Windowable F a instance Unpack (Array F a) (Vector a) instance Storable a => Bulk F a instance Layout F module Data.Repa.Array.Material.Unboxed -- | Layout an array as a flat vector of unboxed elements. -- -- This is the most efficient representation for numerical data. -- -- The implementation uses Data.Vector.Unboxed which picks an -- efficient, specialised representation for every element type. In -- particular, unboxed vectors of pairs are represented as pairs of -- unboxed vectors. -- -- UNSAFE: Indexing into raw material arrays is not bounds checked. You -- may want to wrap this with a Checked layout as well. data U Unboxed :: !Int -> U unboxedLength :: U -> !Int class (Vector Vector a, MVector MVector a) => Unbox a -- | O(1). Wrap an unboxed vector as an array. fromUnboxed :: Unbox a => Vector a -> Array U a -- | O(1). Unwrap an unboxed vector from an array. toUnboxed :: Unbox a => Array U a -> Vector a instance (Show a, Unbox a) => Show (Array U a) instance Show (Name U) instance Eq (Name U) instance Show U instance Eq U instance Unpack (Buffer s U a) (MVector s a) instance Unbox a => Target U a instance Unbox a => Windowable U a instance Unpack (Array U a) (Vector a) instance Unbox a => Bulk U a instance Layout U -- | Interface with stream fusion. module Data.Repa.Eval.Stream -- | Produce a Stream for the elements of the given array. streamOfArray :: (Monad m, Bulk l a, Index l ~ Int) => Array l a -> Stream m a module Data.Repa.Array.Material.Nested -- | Nested array represented as a flat array of elements, and a segment -- descriptor that describes how the elements are partitioned into the -- sub-arrays. Using this representation for multidimentional arrays is -- significantly more efficient than using a boxed array of arrays, as -- there is no need to allocate the sub-arrays individually in the heap. -- -- With a nested type like: Array N (Array N (Array U Int)), the -- concrete representation consists of five flat unboxed vectors: two for -- each of the segment descriptors associated with each level of nesting, -- and one unboxed vector to hold all the integer elements. -- -- UNSAFE: Indexing into raw material arrays is not bounds checked. You -- may want to wrap this with a Checked layout as well. data N Nested :: !Int -> N nestedLength :: N -> !Int class (Vector Vector a, MVector MVector a) => Unbox a -- | O(size src) Convert some lists to a nested array. fromLists :: TargetI l a => Name l -> [[a]] -> Array N (Array l a) -- | O(size src) Convert a triply nested list to a triply nested array. fromListss :: TargetI l a => Name l -> [[[a]]] -> Array N (Array N (Array l a)) -- | Apply a function to all the elements of a doubly nested array, -- preserving the nesting structure. mapElems :: (Array l1 a -> Array l2 b) -> Array N (Array l1 a) -> Array N (Array l2 b) -- | O(1). Produce a nested array by taking slices from some array of -- elements. -- -- This is a constant time operation, as the representation for nested -- vectors just wraps the starts, lengths and elements vectors. slices :: Array U Int -> Array U Int -> Array l a -> Array N (Array l a) -- | Segmented concatenation. Concatenate triply nested vector, producing a -- doubly nested vector. -- --
-- > import Data.Repa.Nice -- > nice $ concats $ fromListss U [["red", "green", "blue"], ["grey", "white"], [], ["black"]] -- ["red","green","blue","grey","white","black"] --concats :: Array N (Array N (Array l a)) -> Array N (Array l a) -- | O(len src). Given predicates which detect the start and end of a -- segment, split an vector into the indicated segments. segment :: (BulkI l a, Unbox a) => (a -> Bool) -> (a -> Bool) -> Array l a -> Array N (Array l a) -- | O(len src). Given a terminating value, split an vector into segments. -- -- The result segments do not include the terminator. -- --
-- > import Data.Repa.Nice -- > nice $ segmentOn (== ' ') (fromList U "fresh fried fish ") -- ["fresh "," "," ","fried ","fish "," "] --segmentOn :: (BulkI l a, Eq a, Unbox a) => (a -> Bool) -> Array l a -> Array N (Array l a) -- | O(len src). Like segment, but cut the source array twice. dice :: (BulkI l a, Windowable l a, Unbox a) => (a -> Bool) -> (a -> Bool) -> (a -> Bool) -> (a -> Bool) -> Array l a -> Array N (Array N (Array l a)) -- | O(len src). Given field and row terminating values, split an array -- into rows and fields. diceSep :: (BulkI l a, Windowable l a, Unbox a, Eq a) => a -> a -> Array l a -> Array N (Array N (Array l a)) -- | For each segment of a nested vector, trim elements off the start and -- end of the segment that match the given predicate. trims :: BulkI l a => (a -> Bool) -> Array N (Array l a) -> Array N (Array l a) -- | For each segment of a nested vector, trim elements off the end of the -- segment that match the given predicate. trimEnds :: BulkI l a => (a -> Bool) -> Array N (Array l a) -> Array N (Array l a) -- | For each segment of a nested vector, trim elements off the start of -- the segment that match the given predicate. trimStarts :: BulkI l a => (a -> Bool) -> Array N (Array l a) -> Array N (Array l a) -- | Ragged transpose of a triply nested array. -- --
-- > import Data.Repa.Nice -- > nice $ concats $ fromListss U [["red", "green", "blue"], ["grey", "white"], [], ["black"]] -- ["red","green","blue","grey","white","black"] --concats :: Array N (Array N (Array l a)) -> Array N (Array l a) -- | O(len src). Given predicates which detect the start and end of a -- segment, split an vector into the indicated segments. segment :: (BulkI l a, Unbox a) => (a -> Bool) -> (a -> Bool) -> Array l a -> Array N (Array l a) -- | O(len src). Given a terminating value, split an vector into segments. -- -- The result segments do not include the terminator. -- --
-- > import Data.Repa.Nice -- > nice $ segmentOn (== ' ') (fromList U "fresh fried fish ") -- ["fresh "," "," ","fried ","fish "," "] --segmentOn :: (BulkI l a, Eq a, Unbox a) => (a -> Bool) -> Array l a -> Array N (Array l a) -- | O(len src). Like segment, but cut the source array twice. dice :: (BulkI l a, Windowable l a, Unbox a) => (a -> Bool) -> (a -> Bool) -> (a -> Bool) -> (a -> Bool) -> Array l a -> Array N (Array N (Array l a)) -- | O(len src). Given field and row terminating values, split an array -- into rows and fields. diceSep :: (BulkI l a, Windowable l a, Unbox a, Eq a) => a -> a -> Array l a -> Array N (Array N (Array l a)) -- | For each segment of a nested vector, trim elements off the start and -- end of the segment that match the given predicate. trims :: BulkI l a => (a -> Bool) -> Array N (Array l a) -> Array N (Array l a) -- | For each segment of a nested vector, trim elements off the end of the -- segment that match the given predicate. trimEnds :: BulkI l a => (a -> Bool) -> Array N (Array l a) -> Array N (Array l a) -- | For each segment of a nested vector, trim elements off the start of -- the segment that match the given predicate. trimStarts :: BulkI l a => (a -> Bool) -> Array N (Array l a) -> Array N (Array l a) -- | Ragged transpose of a triply nested array. -- --
-- > import Data.Repa.Array.Material -- > let Just arr = fromListInto (matrix U 10 10) [1000..1099 :: Float] -- -- > :type arr -- arr :: Array (E U (RW DIM2) Float -- -- > arr ! (Z :. 5 :. 4) -- > 1054.0 --data E r l Dense :: r -> l -> E r l -- | Yield a layout for a dense vector of the given length. -- -- The first argument is the name of the underlying linear layout which -- stores the elements. vector :: LayoutI l => Name l -> Int -> E l DIM1 -- | Yield a layout for a matrix with the given number of rows and columns. matrix :: LayoutI l => Name l -> Int -> Int -> E l DIM2 -- | Yield a layout for a cube with the given number of planes, rows, and -- columns. cube :: LayoutI l => Name l -> Int -> Int -> Int -> E l DIM3 instance (Show (Name r), Show (Name l)) => Show (Name (E r l)) instance (Eq (Name r), Eq (Name l)) => Eq (Name (E r l)) instance (Show r, Show l) => Show (E r l) instance (Eq r, Eq l) => Eq (E r l) instance Unpack (Buffer s r a) tBuf => Unpack (Buffer s (E r l) a) (l, tBuf) instance (Layout l, Index r ~ Int, Target r a) => Target (E r l) a instance (Index r ~ Int, Layout l, Bulk r a) => Bulk (E r l) a instance (Index r ~ Int, Layout r, Layout l) => Layout (E r l) module Data.Repa.Array.Delayed2 -- | A delayed array formed from two source arrays. The source arrays can -- have different layouts but must have the same extent. data D2 l1 l2 Delayed2 :: l1 -> l2 -> D2 l1 l2 delayed2Layout1 :: D2 l1 l2 -> l1 delayed2Layout2 :: D2 l1 l2 -> l2 -- | Wrap two existing arrays in a delayed array. delay2 :: (Bulk l1 a, Bulk l2 b, Index l1 ~ Index l2) => Array l1 a -> Array l2 b -> Maybe (Array (D2 l1 l2) (a, b)) -- | Combine two arrays element-wise using the given worker function. -- -- The two source arrays must have the same extent, else Nothing. map2 :: (Bulk l1 a, Bulk l2 b, Index l1 ~ Index l2) => (a -> b -> c) -> Array l1 a -> Array l2 b -> Maybe (Array (D2 l1 l2) c) instance (Show (Name l1), Show (Name l2)) => Show (Name (D2 l1 l2)) instance (Eq (Name l1), Eq (Name l2)) => Eq (Name (D2 l1 l2)) instance (Show l1, Show l2) => Show (D2 l1 l2) instance (Eq l1, Eq l2) => Eq (D2 l1 l2) instance (Layout lSrc1, Layout lSrc2, Target lDst a, Index lSrc1 ~ Index lSrc2) => Load (D2 lSrc1 lSrc2) lDst a instance (Layout l1, Layout l2, Index l1 ~ Index l2) => Bulk (D2 l1 l2) a instance (Layout l1, Layout l2, Index l1 ~ Index l2) => Layout (D2 l1 l2) module Data.Repa.Array.Tuple -- | Tupled arrays where the components are unpacked and can have separate -- representations. data T2 l1 l2 Tup2 :: !l1 -> !l2 -> T2 l1 l2 -- | Tuple two arrays into an array of pairs. -- -- The two argument arrays must have the same index type, but can have -- different extents. The extent of the result is the intersection of the -- extents of the two argument arrays. tup2 :: (Bulk l1 a, Bulk l2 b, Index l1 ~ Index l2) => Array l1 a -> Array l2 b -> Array (T2 l1 l2) (a, b) -- | Untuple an array of tuples in to a tuple of arrays. -- --
-- > toList $ linear 10 -- [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] --linear :: Int -> Array L Int instance Show (Name L) instance Eq (Name L) instance Show L instance Eq L instance Bulk L Int instance Layout L module Data.Repa.Eval.Array -- | Class of manifest array representations that can be constructed in a -- random-access manner. class Layout l => Target l a where data family Buffer s l a unsafeNewBuffer :: (Target l a, PrimMonad m) => l -> m (Buffer (PrimState m) l a) unsafeReadBuffer :: (Target l a, PrimMonad m) => Buffer (PrimState m) l a -> Int -> m a unsafeWriteBuffer :: (Target l a, PrimMonad m) => Buffer (PrimState m) l a -> Int -> a -> m () unsafeGrowBuffer :: (Target l a, PrimMonad m) => Buffer (PrimState m) l a -> Int -> m (Buffer (PrimState m) l a) unsafeSliceBuffer :: (Target l a, PrimMonad m) => Int -> Int -> Buffer (PrimState m) l a -> m (Buffer (PrimState m) l a) unsafeFreezeBuffer :: (Target l a, PrimMonad m) => Buffer (PrimState m) l a -> m (Array l a) unsafeThawBuffer :: (Target l a, PrimMonad m) => Array l a -> m (Buffer (PrimState m) l a) touchBuffer :: (Target l a, PrimMonad m) => Buffer (PrimState m) l a -> m () bufferLayout :: Target l a => Buffer s l a -> l -- | Constraint synonym that requires an integer index space. type TargetI l a = (Target l a, Index l ~ Int) type IOBuffer = Buffer RealWorld -- | Compute all elements defined by a delayed array and write them to a -- manifest target representation. -- -- The instances of this class require that the source array has a -- delayed representation. If you want to use a pre-existing manifest -- array as the source then delay it first. class (Bulk l1 a, Target l2 a) => Load l1 l2 a loadS :: Load l1 l2 a => Array l1 a -> IOBuffer l2 a -> IO () loadP :: Load l1 l2 a => Gang -> Array l1 a -> IOBuffer l2 a -> IO () -- | Sequential computation of delayed array elements. -- -- Elements of the source array are computed sequentially and written to -- a new array of the specified layout. computeS :: (Load lSrc lDst a, Index lSrc ~ Index lDst) => Name lDst -> Array lSrc a -> Array lDst a -- | Like computeS but use the provided desination layout. -- -- The size of the destination layout must match the size of the source -- array, else Nothing. computeIntoS :: Load lSrc lDst a => lDst -> Array lSrc a -> Maybe (Array lDst a) -- | Interface with chain fusion. module Data.Repa.Eval.Chain -- | Produce a Chain for the elements of the given array. The order -- in which the elements appear in the chain is determined by the layout -- of the array. chainOfArray :: (Monad m, Bulk l a) => Array l a -> Chain m Int a -- | Compute the elements of a pure Chain, writing them into a new -- array Array. unchainToArray :: (Target l a, Unpack (IOBuffer l a) t) => Name l -> Chain Id s a -> (Array l a, s) -- | Compute the elements of an IO Chain, writing them to a -- new Array. unchainToArrayIO :: (Target l a, Unpack (IOBuffer l a) t) => Name l -> Chain IO s a -> IO (Array l a, s) -- | NOTE: This is an ALPHA version of Repa 4. The API is not yet complete -- with respect to Repa 3. Some important functions are still missing, -- and the docs may not be up-to-date. -- -- A Repa array is a wrapper around an underlying container structure -- that holds the array elements. -- -- In the type (Array l a), the l -- specifies the Layout of data, which includes the type of the -- underlying container, as well as how the elements should be arranged -- in that container. The a specifies the element type. -- --
-- > import Data.Repa.Array -- > computeS U $ A.map (+ 1) $ A.map (* 2) $ fromList U [1 .. 100 :: Int] ---- -- Lets look at the result of the first map: -- --
-- > :type A.map (* 2) $ fromList U [1 .. 100 :: Int] -- A.map (* 2) $ fromList U [1 .. 100 :: Int] -- :: Array (D U) Int ---- -- In the type Array (D U) Int, the outer D indicates -- that the array is represented as a function that computes each element -- on demand. -- -- Applying a second map layers another element-producing function -- on top: -- --
-- > :type A.map (+ 1) $ A.map (* 2) $ fromList U [1 .. 100 :: Int] -- A.map (+ 1) $ A.map (* 2) $ fromList U [1 .. 100 :: Int] -- :: Array (D (D U)) Int ---- -- At runtime, indexing into an array of the above type involves calling -- the outer D-elayed function, which calls the inner -- D-elayed function, which retrieves source data from the inner -- U-nboxed array. Although this works, indexing into a deep -- stack of delayed arrays can be quite expensive. -- -- To fully evaluate a delayed array, use the computeS function, -- which computes each element of the array sequentially. We pass -- computeS the name of the desired result layout, in this case -- we use U to indicate an unboxed array of values: -- --
-- > :type computeS U $ A.map (+ 1) $ A.map (* 2) $ fromList U [1 .. 100 :: Int] -- computeS U $ A.map (+ 1) $ A.map (* 2) $ fromList U [1 .. 100 :: Int] -- :: Array U Int ---- -- At runtime, each element of the result will be computed by first -- reading the source element, applying (*2) to it, then -- applying (+1) to it, then writing to the result array. Array -- "fusion" is achieved by the fact that result of applying (*2) -- to an element is used directly, without writing it to an intermediate -- buffer. -- -- An added bonus is that during compilation, the GHC simplifier will -- inline the definitions of map and computeS, then -- eliminate the intermediate function calls. In the compiled code all -- intermediate values will be stored unboxed in registers, without any -- overhead due to boxing or laziness. -- -- When used correctly, array fusion allows Repa programs to run as fast -- as equivalents in C or Fortran. However, without fusion the programs -- typically run 10-20x slower (so remember apply computeS to -- delayed arrays). -- --
-- > toList $ linear 10 -- [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] --linear :: Int -> Array L Int -- | A row-wise layout that maps higher rank indices to linear ones in a -- row-major order. -- -- Indices are ordered so the inner-most coordinate varies most -- frequently: -- --
-- > Prelude.map (fromIndex (RowWise (ish2 2 3))) [0..5] -- [(Z :. 0) :. 0, (Z :. 0) :. 1, (Z :. 0) :. 2, -- (Z :. 1) :. 0, (Z :. 1) :. 1, (Z :. 1) :. 2] ---- --
-- > toList $ rowWise (ish2 3 2) -- [(Z :. 0) :. 0, (Z :. 0) :. 1, -- (Z :. 1) :. 0, (Z :. 1) :. 1, -- (Z :. 2) :. 0, (Z :. 2) :. 1] --rowWise :: sh -> Array (RW sh) sh -- | Delayed arrays wrap functions from an index to element value. The -- index space is specified by an inner layout, l. -- -- Every time you index into a delayed array the element at that position -- is recomputed. data D l Delayed :: l -> D l delayedLayout :: D l -> l -- | Wrap a function as a delayed array. -- --
-- > toList $ fromFunction (Linear 10) (* 2) -- = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] --fromFunction :: l -> (Index l -> a) -> Array (D l) a -- | Produce the extent of an array, and a function to retrieve an -- arbitrary element. toFunction :: Bulk l a => Array (D l) a -> (l, Index l -> a) -- | Wrap an existing array in a delayed one. delay :: Bulk l a => Array l a -> Array (D l) a -- | A delayed array formed from two source arrays. The source arrays can -- have different layouts but must have the same extent. data D2 l1 l2 Delayed2 :: l1 -> l2 -> D2 l1 l2 delayed2Layout1 :: D2 l1 l2 -> l1 delayed2Layout2 :: D2 l1 l2 -> l2 -- | Wrap two existing arrays in a delayed array. delay2 :: (Bulk l1 a, Bulk l2 b, Index l1 ~ Index l2) => Array l1 a -> Array l2 b -> Maybe (Array (D2 l1 l2) (a, b)) data W l Window :: Index l -> Index l -> l -> W l windowStart :: W l -> Index l windowSize :: W l -> Index l windowInner :: W l -> l -- | Class of array representations that can be windowed directly. -- -- The underlying representation can encode the window, without needing -- to add a wrapper to the existing layout. class Bulk l a => Windowable l a window :: Windowable l a => Index l -> Index l -> Array l a -> Array l a -- | Wrap a window around an exiting array. windowed :: Index l -> Index l -> Array l a -> Array (W l) a -- | Wrap a window around an existing array that encompases the entire -- array. entire :: Bulk l a => Array l a -> Array (W l) a -- | Tupled arrays where the components are unpacked and can have separate -- representations. data T2 l1 l2 Tup2 :: !l1 -> !l2 -> T2 l1 l2 -- | Tuple two arrays into an array of pairs. -- -- The two argument arrays must have the same index type, but can have -- different extents. The extent of the result is the intersection of the -- extents of the two argument arrays. tup2 :: (Bulk l1 a, Bulk l2 b, Index l1 ~ Index l2) => Array l1 a -> Array l2 b -> Array (T2 l1 l2) (a, b) -- | Untuple an array of tuples in to a tuple of arrays. -- --
-- > import Data.Repa.Array.Material -- > let Just arr = fromListInto (matrix U 10 10) [1000..1099 :: Float] -- -- > :type arr -- arr :: Array (E U (RW DIM2) Float -- -- > arr ! (Z :. 5 :. 4) -- > 1054.0 --data E r l Dense :: r -> l -> E r l -- | Yield a layout for a dense vector of the given length. -- -- The first argument is the name of the underlying linear layout which -- stores the elements. vector :: LayoutI l => Name l -> Int -> E l DIM1 -- | Yield a layout for a matrix with the given number of rows and columns. matrix :: LayoutI l => Name l -> Int -> Int -> E l DIM2 -- | Yield a layout for a cube with the given number of planes, rows, and -- columns. cube :: LayoutI l => Name l -> Int -> Int -> Int -> E l DIM3 -- | O(length src). Construct a linear array from a list of elements. fromList :: TargetI l a => Name l -> [a] -> Array l a -- | O(length src). Construct an array from a list of elements, and give it -- the provided layout. -- -- The length of the provided shape must match the length of the -- list, else Nothing. fromListInto :: Target l a => l -> [a] -> Maybe (Array l a) -- | Convert an array to a list. toList :: Bulk l a => Array l a -> [a] -- | Compute all elements defined by a delayed array and write them to a -- manifest target representation. -- -- The instances of this class require that the source array has a -- delayed representation. If you want to use a pre-existing manifest -- array as the source then delay it first. class (Bulk l1 a, Target l2 a) => Load l1 l2 a -- | Class of manifest array representations that can be constructed in a -- random-access manner. class Layout l => Target l a -- | Sequential computation of delayed array elements. -- -- Elements of the source array are computed sequentially and written to -- a new array of the specified layout. computeS :: (Load lSrc lDst a, Index lSrc ~ Index lDst) => Name lDst -> Array lSrc a -> Array lDst a -- | Like computeS but use the provided desination layout. -- -- The size of the destination layout must match the size of the source -- array, else Nothing. computeIntoS :: Load lSrc lDst a => lDst -> Array lSrc a -> Maybe (Array lDst a) -- | O(1). View the elements of a vector in reverse order. -- --
-- > toList $ reverse $ fromList U [0..10 :: Int] -- [10,9,8,7,6,5,4,3,2,1,0] --reverse :: BulkI l a => Array l a -> Array (D l) a -- | Apply a worker function to each element of an array, yielding a new -- array with the same extent. -- -- The resulting array is delayed, meaning every time you index into it -- the element at that index is recomputed. map :: Bulk l a => (a -> b) -> Array l a -> Array (D l) b -- | Combine two arrays element-wise using the given worker function. -- -- The two source arrays must have the same extent, else Nothing. map2 :: (Bulk l1 a, Bulk l2 b, Index l1 ~ Index l2) => (a -> b -> c) -> Array l1 a -> Array l2 b -> Maybe (Array (D2 l1 l2) c) -- | Like map, but immediately computeS the result. mapS :: (Bulk lSrc a, Target lDst b, Index lSrc ~ Index lDst) => Name lDst -> (a -> b) -> Array lSrc a -> Array lDst b -- | Like map2, but immediately computeS the result. map2S :: (Bulk lSrc1 a, Bulk lSrc2 b, Target lDst c, Index lSrc1 ~ Index lDst, Index lSrc2 ~ Index lDst) => Name lDst -> (a -> b -> c) -> Array lSrc1 a -> Array lSrc2 b -> Maybe (Array lDst c) -- | Keep the elements of an array that match the given predicate. filter :: (BulkI lSrc a, TargetI lDst a) => Name lDst -> (a -> Bool) -> Array lSrc a -> Array lDst a -- | O(len src) Yield Just the index of the first element matching -- the predicate or Nothing if no such element exists. findIndex :: BulkI l a => (a -> Bool) -> Array l a -> Maybe Int -- | O(len result) Concatenate nested arrays. -- --
-- > import Data.Repa.Array.Material -- > let arrs = fromList B [fromList U [1, 2, 3], fromList U [5, 6, 7 :: Int]] -- > toList $ concat U arrs -- [1,2,3,5,6,7] --concat :: ConcatDict lOut lIn tIn lDst a => Name lDst -> Array lOut (Array lIn a) -> Array lDst a -- | O(len result) Concatenate the elements of some nested vector, -- inserting a copy of the provided separator array between each element. -- --
-- > import Data.Repa.Array.Material -- > let sep = fromList U [0, 0, 0] -- > let arrs = fromList B [fromList U [1, 2, 3], fromList U [5, 6, 7 :: Int]] -- > toList $ concatWith U sep arrs -- [1,2,3,0,0,0,5,6,7,0,0,0] --concatWith :: (ConcatDict lOut lIn tIn lDst a, BulkI lSep a) => Name lDst -> Array lSep a -> Array lOut (Array lIn a) -> Array lDst a -- | Perform a concatWith, adding a newline character to the end of -- each inner array. unlines :: ConcatDict lOut lIn tIn lDst Char => Name lDst -> Array lOut (Array lIn Char) -> Array lDst Char -- | O(len result) Insert a copy of the separator array between the -- elements of the second and concatenate the result. -- --
-- > import Data.Repa.Array.Material -- > let sep = fromList U [0, 0, 0] -- > let arrs = fromList B [fromList U [1, 2, 3], fromList U [5, 6, 7 :: Int]] -- > toList $ intercalate U sep arrs -- [1,2,3,0,0,0,5,6,7] --intercalate :: (ConcatDict lOut lIn tIn lDst a, BulkI lSep a) => Name lDst -> Array lSep a -> Array lOut (Array lIn a) -> Array lDst a -- | Dictionaries needed to perform a concatenation. type ConcatDict lOut lIn tIn lDst a = (BulkI lOut (Array lIn a), BulkI lIn a, TargetI lDst a, Unpack (Array lIn a) tIn) -- | Take a desired number of segments, and array of key value pairs where -- the key is the segment number. Partition the values into the stated -- number of segments, discarding values where the key falls outside the -- given range. -- --
-- > import Data.Repa.Array.Material -- > import Data.Repa.Nice -- > nice $ groups U U (fromList U "waaabllle") -- ([(w,1),(a,3),(b,1),(l,3)],Just (e,1)) --groups :: (GroupsDict lElt lGrp tGrp lLen tLen n, Eq n) => Name lGrp -> Name lLen -> Array lElt n -> (Array (T2 lGrp lLen) (n, Int), Maybe (n, Int)) -- | Like groups, but use the given function to determine whether -- two consecutive elements should be in the same group. Also take an -- initial starting group and count. -- --
-- > import Data.Repa.Array.Material -- > import Data.Repa.Nice -- > nice $ groupsWith U U (==) (Just (w, 5)) (fromList U "waaabllle") -- ([(w,6),(a,3),(b,1),(l,3)],Just (e,1)) --groupsWith :: GroupsDict lElt lGrp tGrp lLen tLen n => Name lGrp -> Name lLen -> (n -> n -> Bool) -> Maybe (n, Int) -> Array lElt n -> (Array (T2 lGrp lLen) (n, Int), Maybe (n, Int)) -- | Dictionaries need to perform a grouping. type GroupsDict lElt lGrp tGrp lLen tLen n = (Bulk lElt n, Target lGrp n, Target lLen Int, Index lGrp ~ Index lLen, Unpack (IOBuffer lLen Int) tLen, Unpack (IOBuffer lGrp n) tGrp) -- | Left fold of all elements in an array, sequentially. foldl :: (Bulk l b, Index l ~ Int) => (a -> b -> a) -> a -> Array l b -> a -- | Segmented fold over vectors of segment lengths and input values. -- --
-- > import Data.Repa.Array.Material
-- > import Data.Repa.Nice
-- > let segs = fromList B [("red", 3), ("green", 5)]
-- > let vals = fromList U [0..100 :: Int]
-- > nice $ fst $ folds B U (+) 0 segs vals
-- [("red",3),("green",25)]
--
folds :: FoldsDict lSeg lElt lGrp tGrp lRes tRes n a b => Name lGrp -> Name lRes -> (a -> b -> b) -> b -> Array lSeg (n, Int) -> Array lElt a -> (Array (T2 lGrp lRes) (n, b), Folds Int Int n a b)
-- | Like folds, but take an initial state for the first segment.
--
--
-- > import Data.Repa.Array.Material
-- > import Data.Repa.Nice
-- > let state = Just ("white", 4, 100)
-- > let segs = fromList B [("red", 3), ("green", 5)]
-- > let vals = fromList U [0..100 :: Int]
-- > nice $ fst $ foldsWith B U (+) 0 state segs vals
-- [("white",106),("red",15),("green",45)]
--
foldsWith :: FoldsDict lSeg lElt lGrp tGrp lRes tRes n a b => Name lGrp -> Name lRes -> (a -> b -> b) -> b -> Maybe (n, Int, b) -> Array lSeg (n, Int) -> Array lElt a -> (Array (T2 lGrp lRes) (n, b), Folds Int Int n a b)
-- | Return state of a folds operation.
data Folds sLens sVals n a b :: * -> * -> * -> * -> * -> *
Folds :: SrictNotUnpackedsLens -> SrictNotUnpackedsVals -> SrictNotUnpacked(Option n) -> SrictNotUnpackedInt -> SrictNotUnpackedb -> Folds sLens sVals n a b
-- | State of lengths chain.
_stateLens :: Folds sLens sVals n a b -> SrictNotUnpackedsLens
-- | State of values chain.
_stateVals :: Folds sLens sVals n a b -> SrictNotUnpackedsVals
-- | If we're currently in a segment, then hold its name,
_nameSeg :: Folds sLens sVals n a b -> SrictNotUnpacked(Option n)
-- | Length of current segment.
_lenSeg :: Folds sLens sVals n a b -> SrictNotUnpackedInt
-- | Accumulated value of current segment.
_valSeg :: Folds sLens sVals n a b -> SrictNotUnpackedb
-- | Dictionaries need to perform a segmented fold.
type FoldsDict lSeg lElt lGrp tGrp lRes tRes n a b = (Bulk lSeg (n, Int), Bulk lElt a, Target lGrp n, Target lRes b, Index lGrp ~ Index lRes, Unpack (IOBuffer lGrp n) tGrp, Unpack (IOBuffer lRes b) tRes)
module Data.Repa.Bits.Date32
-- | A date packed into a 32-bit word.
--
-- The bitwise format is:
--
-- -- 32 16 8 0 -- | year | month | day | -- ---- -- Pros: Packing and unpacking a Date32 is simpler than using other -- formats that represent dates as a number of days from some epoch. We -- can also avoid worrying about what the epoch should be, and the -- representation will not overflow until year 65536. -- -- Cons: Computing a range of dates is slower than with representations -- using an epoch, as we cannot simply add one to get to the next valid -- date. type Date32 = Word32 -- | Pack a year, month and day into a Word32. -- -- If any components of the date are out-of-range then they will be -- bit-wise truncated so they fit in their destination fields. pack :: (Word, Word, Word) -> Date32 -- | Inverse of pack. -- -- This function does a simple bit-wise unpacking of the given -- Word32, and does not guarantee that the returned fields are -- within a valid range for the given calendar date. unpack :: Date32 -> (Word, Word, Word) -- | Yield the next date in the series. -- -- This assumes leap years occur every four years, which is valid after -- year 1900 and before year 2100. next :: Date32 -> Date32 -- | Yield an array containing a range of dates, inclusive of the end -- points. range :: TargetI l Date32 => Name l -> Date32 -> Date32 -> Array l Date32 -- | Read a Date32 in ASCII YYYYsMMsDD format, using the given -- separator character s. readYYYYsMMsDD :: BulkI l Char => Char -> Array l Char -> Maybe Date32 module Data.Repa.IO.Array -- | Get data from a file, up to the given number of bytes. -- --
-- > tab [[10, 20, 302], [40, 50], [60, 7001, 80, 90 :: Int]] -- 10 20 302 -- 40 50 -- 60 7001 80 90 ---- -- Deeper layers of nesting are preserved in the output: -- --
-- > tab [[[10], [20, 21]], [[30, 31], [40, 41, 41], [50 :: Int]]] -- [10] [20,21] -- [30,31] [40,41,41] [50] ---- -- By default, strings are printed as lists of characters: -- --
-- > tab [[("red", 10), ("green", 20), ("blue", 30)], [("grey", 40), ("white", 50 :: Int)]]
-- (['r','e','d'],10) (['g','r','e','e','n'],20) (['b','l','u','e'],30)
-- (['g','r','e','y'],40) (['w','h','i','t','e'],50)
--
--
-- If you want double-quotes then wrap the strings with a Str
-- constructor:
--
--
-- > tab [[(Str "red", 10), (Str "green", 20), (Str "blue", 30)], [(Str "grey", 40), (Str "white", 50 :: Int)]]
-- ("red",10) ("green",20) ("blue",30)
-- ("grey",40) ("white",50)
--
--
-- If you don't want any quotes then wrap them with a Tok
-- constructor:
--
-- -- > tab [[(Tok "red", 10), (Tok "green", 20), (Tok "blue", 30)], [(Tok "grey", 40), (Tok "white", 50 :: Int)]] -- (red,10) (green,20) (blue,30) -- (grey,40) (white,50) --tab :: Presentable a => a -> IO () -- | Display a nested value in tabular form. tabulate :: Presentable a => a -> Text -- | Wrapper to indicate a list of characters should be printed as a -- string, including double quotes. data Str Str :: [Char] -> Str -- | Wrapper to indicate a list of characters should be printed as a -- string, without double quotes. data Tok Tok :: [Char] -> Tok