Skip to content

Commit

Permalink
Remove Unpack class and extra type param on Build
Browse files Browse the repository at this point in the history
  • Loading branch information
benl23x5 committed Mar 13, 2016
1 parent 42b8907 commit b12c63f
Show file tree
Hide file tree
Showing 33 changed files with 84 additions and 359 deletions.
8 changes: 2 additions & 6 deletions repa-array/Data/Repa/Array/Auto/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ where
import Data.Repa.Array.Material.Auto (A(..))
import qualified Data.Repa.Array.Generic as G
import qualified Data.Repa.Array.Meta.Window as A
import qualified Data.Repa.Array.Internals.Target as G
import qualified Data.Repa.Fusion.Unpack as F



-- | Arrays of elements that are automatically layed out into some
Expand All @@ -32,7 +29,6 @@ type Elem a

-- | Class of elements where arrays of those elements can be constructed
-- in arbitrary order.
type Build a t
type Build a
= ( G.Bulk A a
, G.Target A a
, F.Unpack (G.Buffer A a) t)
, G.Target A a)
61 changes: 29 additions & 32 deletions repa-array/Data/Repa/Array/Auto/Operator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ import qualified Data.Repa.Array.Meta.Window as A
import qualified Data.Repa.Array.Meta.Delayed as A
import qualified Data.Repa.Array.Meta.Delayed2 as A
import qualified Data.Repa.Array.Internals.Bulk as G
import qualified Data.Repa.Fusion.Unpack as F
import qualified Data.Repa.Chain as C
import qualified Data.Vector.Unboxed as U
import Prelude
Expand Down Expand Up @@ -176,15 +175,15 @@ init = A.init

-- Construction -----------------------------------------------------------------------------------
-- | O(1). An empty array of the given layout.
empty :: Build a at
empty :: Build a
=> Array a
empty = G.empty A
{-# INLINE empty #-}


-- | O(1). Create a new empty array containing a single element.
singleton
:: Build a at
:: Build a
=> a -> Array a
singleton = G.singleton A
{-# INLINE singleton #-}
Expand All @@ -193,7 +192,7 @@ singleton = G.singleton A
-- | Like `generateS` but use a function that produces Maybe an element.
-- If any element returns `Nothing`, then `Nothing` for the whole array.
generateMaybeS
:: Build a at
:: Build a
=> Int -> (Int -> Maybe a)
-> Maybe (Array a)
generateMaybeS = G.generateMaybeS A
Expand All @@ -203,7 +202,7 @@ generateMaybeS = G.generateMaybeS A
-- | Apply a function to every element of an array,
-- if any application returns `Nothing`, then `Nothing` for the whole result.
mapMaybeS
:: (Elem a, Build b bt)
:: (Elem a, Build b)
=> (a -> Maybe b)
-> Array a
-> Maybe (Array b)
Expand All @@ -215,7 +214,7 @@ mapMaybeS = G.mapMaybeS A
-- or an element. If any element returns `Nothing`, then `Nothing` for
-- the whole array.
generateEitherS
:: Build a at
:: Build a
=> Int -> (Int -> Either err a)
-> Either err (Array a)
generateEitherS = G.generateEitherS A
Expand All @@ -225,7 +224,7 @@ generateEitherS = G.generateEitherS A
-- | Apply a function to every element of an array,
-- if any application returns `Left`, then `Left` for the whole result.
mapEitherS
:: (Elem a, Build b bt)
:: (Elem a, Build b)
=> (a -> Either err b)
-> Array a
-> Either err (Array b)
Expand All @@ -235,21 +234,21 @@ mapEitherS = G.mapEitherS A

-- Conversion -------------------------------------------------------------------------------------
-- | Convert a list to an array.
fromList :: Build a at
fromList :: Build a
=> [a] -> Array a
fromList = G.fromList A
{-# INLINE fromList #-}


-- | Convert a nested list to an array.
fromLists :: Build a at
fromLists :: Build a
=> [[a]] -> Array (Array a)
fromLists xs = convert $! N.fromLists A xs
{-# INLINE fromLists #-}


-- | Convert a triply nested list to a triply nested array.
fromListss :: Build a at
fromListss :: Build a
=> [[[a]]] -> Array (Array (Array a))
fromListss xs = convert $! N.fromListss A xs
{-# INLINE fromListss #-}
Expand Down Expand Up @@ -283,15 +282,15 @@ toListss = G.toListss
-- [10,9,8,7,6,5,4,3,2,1,0]
-- @
--
reverse :: Build a at => Array a -> Array a
reverse :: Build a => Array a -> Array a
reverse arr = G.computeS A $! A.reverse arr
{-# INLINE reverse #-}


-- Replicating ------------------------------------------------------------------------------------
-- | Segmented replicate.
replicates
:: (Elem a, Build a at)
:: (Elem a, Build a)
=> Array (Int, a) -> Array a

replicates arr
Expand All @@ -301,7 +300,7 @@ replicates arr

-- Mapping ----------------------------------------------------------------------------------------
-- | Apply a function to all the elements of a list.
map :: (Elem a, Build b bt)
map :: (Elem a, Build b)
=> (a -> b) -> Array a -> Array b
map f arr
= G.computeS A $! A.map f arr
Expand All @@ -312,7 +311,7 @@ map f arr
--
-- If the arrays don't have the same length then `Nothing`.
--
map2 :: (Elem a, Elem b, Build c ct)
map2 :: (Elem a, Elem b, Build c)
=> (a -> b -> c) -> Array a -> Array b -> Maybe (Array c)
map2 f xs ys
= liftM (G.computeS A) $! A.map2 f xs ys
Expand Down Expand Up @@ -378,7 +377,7 @@ std = G.std
-- prefix is correlated.
--
correlate
:: ( Elem a, Floating a)
:: (Elem a, Floating a)
=> Array a -> Array a -> a
correlate = G.correlate
{-# INLINE correlate #-}
Expand All @@ -392,7 +391,7 @@ correlate = G.correlate
-- vector of segment lengths or elements was too short relative to the
-- other.
--
folds :: (Elem a, Build n nt, Build b bt)
folds :: (Elem a, Build n, Build b)
=> (a -> b -> b) -- ^ Worker function.
-> b -- ^ Initial state when folding segments.
-> Array (n, Int) -- ^ Segment names and lengths.
Expand All @@ -407,7 +406,7 @@ folds f z lens vals
-- | Like `folds`, but take an initial state for the first segment.
--
foldsWith
:: (Elem a, Build n nt, Build b bt)
:: (Elem a, Build n, Build b)
=> (a -> b -> b) -- ^ Worker function.
-> b -- ^ Initial state when folding segments.
-> Maybe (n, Int, b) -- ^ Name, length and initial state for first segment.
Expand All @@ -422,7 +421,7 @@ foldsWith f z start lens vals

-- Filtering --------------------------------------------------------------------------------------
-- | O(len src) Keep the elements of an array that match the given predicate.
filter :: Build a at
filter :: Build a
=> (a -> Bool) -> Array a -> Array a
filter = G.filter A
{-# INLINE filter #-}
Expand Down Expand Up @@ -504,7 +503,7 @@ unzip arr@(A.AArray_T2 arr')

-- Sloshing ---------------------------------------------------------------------------------------
-- | Concatenate nested arrays.
concat :: (Elem a, Build a at)
concat :: (Elem a, Build a)
=> Array (Array a) -- ^ Arrays to concatenate.
-> Array a
concat arr
Expand All @@ -515,7 +514,7 @@ concat arr
-- | O(len result) Concatenate the elements of some nested vector,
-- inserting a copy of the provided separator array between each element.
concatWith
:: (Elem a, Build a at, F.Unpack (Array a) aat)
:: (Elem a, Build a)
=> Array a -- ^ Separator array.
-> Array (Array a) -- ^ Arrays to concatenate.
-> Array a
Expand All @@ -540,16 +539,15 @@ concats (A.AArray_Array (N.NArray starts1 lens1 (A.AArray_Array arr)))

-- | O(len result) Perform a `concatWith`, adding a newline character to
-- the end of each inner array.
unlines :: F.Unpack (Array Char) aat
=> Array (Array Char) -> Array Char
unlines :: Array (Array Char) -> Array Char
unlines = G.unlines A
{-# INLINE unlines #-}


-- | O(len result) Insert a copy of the separator array between the elements of
-- the second and concatenate the result.
intercalate
:: (Elem a, Build a at, F.Unpack (Array a) aat)
:: (Elem a, Build a)
=> Array a -- ^ Separator array.
-> Array (Array a) -- ^ Arrays to concatenate.
-> Array a
Expand Down Expand Up @@ -592,7 +590,7 @@ slice from len arr

-- Merging ----------------------------------------------------------------------------------------
-- | Merge two sorted key-value streams.
merge :: (Ord k, Elem (k, a), Elem (k, b), Build (k, c) ct)
merge :: (Ord k, Elem (k, a), Elem (k, b), Build (k, c))
=> (k -> a -> b -> c) -- ^ Combine two values with the same key.
-> (k -> a -> c) -- ^ Handle a left value without a right value.
-> (k -> b -> c) -- ^ Handle a right value without a left value.
Expand All @@ -606,7 +604,7 @@ merge = G.merge A
-- | Like `merge`, but only produce the elements where the worker functions
-- return `Just`.
mergeMaybe
:: (Ord k, Elem (k, a), Elem (k, b), Build (k, c) ct)
:: (Ord k, Elem (k, a), Elem (k, b), Build (k, c))
=> (k -> a -> b -> Maybe c) -- ^ Combine two values with the same key.
-> (k -> a -> Maybe c) -- ^ Handle a left value without a right value.
-> (k -> b -> Maybe c) -- ^ Handle a right value without a left value.
Expand All @@ -623,7 +621,7 @@ mergeMaybe = G.mergeMaybe A
-- We walk over the stream front to back, maintaining an accumulator.
-- At each point we can chose to emit an element (or not)
--
compact :: (Elem a, Build b bt)
compact :: (Elem a, Build b)
=> (s -> a -> (s, Maybe b))
-> s
-> Array a
Expand All @@ -635,7 +633,7 @@ compact = G.compact A
-- | Like `compact` but use the first value of the stream as the
-- initial state, and add the final state to the end of the output.
compactIn
:: Build a at
:: Build a
=> (a -> a -> (a, Maybe a))
-> Array a
-> Array a
Expand All @@ -644,8 +642,7 @@ compactIn = G.compactIn A


-- | Apply a generic stream process to an array.
process :: ( Build a at, Build b bt, Elem b
, F.Unpack (G.Array A.A b) t
process :: ( Build a, Build b, Elem b
, G.Target A.A b)
=> (s -> a -> (s, Array b)) -- ^ Worker function
-> s -- ^ Initial state.
Expand All @@ -657,7 +654,7 @@ process = G.process A

-- Inserting --------------------------------------------------------------------------------------
-- | Insert elements produced by the given function in to an array.
insert :: Build a at
insert :: Build a
=> (Int -> Maybe a) -> Array a -> Array a
insert = G.insert A
{-# INLINE insert #-}
Expand Down Expand Up @@ -742,7 +739,7 @@ diceSep xEndCol xEndRow arr
-- Grouping ---------------------------------------------------------------------------------------
-- | From a stream of values which has consecutive runs of idential values,
-- produce a stream of the lengths of these runs.
groups :: (Eq a, Build a at)
groups :: (Eq a, Build a)
=> Array a -- ^ Input elements.
-> (Array (a, Int), Maybe (a, Int))
-- ^ Completed and final segment lengths.
Expand All @@ -756,7 +753,7 @@ groups arr
-- consecutive elements should be in the same group.
-- Also take an initial starting group and count.
groupsWith
:: Build a at
:: Build a
=> (a -> a -> Bool) -- ^ Comparison function.
-> Maybe (a, Int) -- ^ Starting element and count.
-> Array a -- ^ Input elements.
Expand Down
4 changes: 1 addition & 3 deletions repa-array/Data/Repa/Array/Internals/Operator/Replicate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import Data.Repa.Array.Internals.Layout as A
import Data.Repa.Array.Internals.Target as A
import Data.Repa.Array.Internals.Bulk as A
import Data.Repa.Eval.Stream as A
import Data.Repa.Fusion.Unpack as A
import Data.Repa.Stream as S
#include "repa-array.h"


-- | Segmented replicate.
replicates
:: ( BulkI lSrc (Int, a), TargetI lDst a
, Unpack (Buffer lDst a) t)
:: (BulkI lSrc (Int, a), TargetI lDst a)
=> Name lDst
-> Array lSrc (Int, a)
-> Array lDst a
Expand Down
9 changes: 0 additions & 9 deletions repa-array/Data/Repa/Array/Material/Auto/InstArray.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import Data.Repa.Array.Generic.Convert as A
import Data.Repa.Array.Internals.Layout as A
import Data.Repa.Array.Internals.Bulk as A
import Data.Repa.Array.Internals.Target as A
import Data.Repa.Fusion.Unpack as A
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector as V
import qualified Data.ByteString.Internal as BS
Expand Down Expand Up @@ -109,14 +108,6 @@ instance (Bulk l a, A.Target l a, Index l ~ Int)
{-# INLINE_ARRAY bufferLayout #-}


instance Unpack (Buffer N (Array l a)) t
=> Unpack (Buffer A (Array l a)) t where
unpack (ABuffer_Array buf) = unpack buf
repack (ABuffer_Array x) buf = ABuffer_Array (repack x buf)
{-# INLINE unpack #-}
{-# INLINE repack #-}


instance (Bulk A a, Windowable l a, Index l ~ Int)
=> Windowable A (Array l a) where
window st len (AArray_Array arr)
Expand Down
8 changes: 0 additions & 8 deletions repa-array/Data/Repa/Array/Material/Auto/InstBox.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Data.Repa.Array.Meta.Window as A
import Data.Repa.Array.Internals.Bulk as A
import Data.Repa.Array.Internals.Target as A
import Data.Repa.Array.Internals.Layout as A
import Data.Repa.Fusion.Unpack as F
import Data.Repa.Scalar.Box
import Control.Monad
#include "repa-array.h"
Expand Down Expand Up @@ -71,13 +70,6 @@ instance Target A (Box a) where
{-# INLINE_ARRAY bufferLayout #-}


instance Unpack (Buffer A (Box a)) (Buffer A (Box a)) where
unpack buf = buf
repack _ buf = buf
{-# INLINE unpack #-}
{-# INLINE repack #-}


instance Eq a
=> Eq (Array A (Box a)) where
(==) (AArray_Box arr1) (AArray_Box arr2) = arr1 == arr2
Expand Down
17 changes: 0 additions & 17 deletions repa-array/Data/Repa/Array/Material/Auto/InstChar.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Data.Repa.Array.Generic.Convert as A
import Data.Repa.Array.Internals.Layout as A
import Data.Repa.Array.Internals.Bulk as A
import Data.Repa.Array.Internals.Target as A
import Data.Repa.Fusion.Unpack as A
import Control.Monad
#include "repa-array.h"

Expand Down Expand Up @@ -41,14 +40,6 @@ instance Windowable A Char where
{-# INLINE_ARRAY window #-}


instance Unpack (Array F Char) t
=> Unpack (Array A Char) t where
unpack (AArray_Char arr) = unpack arr
repack (AArray_Char x) arr = AArray_Char (repack x arr)
{-# INLINE unpack #-}
{-# INLINE repack #-}


instance Target A Char where
data Buffer A Char
= ABuffer_Char !(Buffer F Char)
Expand Down Expand Up @@ -90,14 +81,6 @@ instance Target A Char where
{-# INLINE_ARRAY bufferLayout #-}


instance (Unpack (Buffer F Char)) t
=> (Unpack (Buffer A Char)) t where
unpack (ABuffer_Char buf) = unpack buf
repack (ABuffer_Char x) buf = ABuffer_Char (repack x buf)
{-# INLINE unpack #-}
{-# INLINE repack #-}


instance Eq (Array A Char) where
(==) (AArray_Char arr1) (AArray_Char arr2) = arr1 == arr2
{-# INLINE_ARRAY (==) #-}
Expand Down
Loading

0 comments on commit b12c63f

Please sign in to comment.