Skip to content

Commit

Permalink
Remove pull array index (unsafe), add uncons and empty. (#475)
Browse files Browse the repository at this point in the history
* Remove pull array index (unsafe), add uncons.

* Add pull array `empty`, add tests
  • Loading branch information
sjoerdvisscher committed Apr 9, 2024
1 parent 4398151 commit 16795c7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
14 changes: 6 additions & 8 deletions src/Data/Array/Polarized.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,12 @@
-- vecfilter vec f = Push.alloc (transfer (loop (Pull.fromVector vec) f))
-- where
-- loop :: Pull.Array a -> (a -> Bool) -> Pull.Array a
-- loop arr f = case Pull.findLength arr of
-- (0,_) -> Pull.fromFunction (error "empty") 0
-- (n,_) -> case Pull.split 1 arr of
-- (head, tail) -> case Pull.index head 0 of
-- (a,_) ->
-- if f a
-- then Pull.append (Pull.singleton a) (loop tail f)
-- else loop tail f
-- loop arr f = case Pull.uncons arr of
-- Nothing -> Pull.empty
-- Just (a, as) ->
-- if f a
-- then Pull.append (Pull.singleton a) (loop as f)
-- else loop as f
-- @
--
--
Expand Down
5 changes: 3 additions & 2 deletions src/Data/Array/Polarized/Pull.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Data.Array.Polarized.Pull
fromVector,
make,
singleton,
empty,

-- * Consumption
toVector,
Expand All @@ -27,7 +28,7 @@ module Data.Array.Polarized.Pull
findLength,
split,
reverse,
index,
uncons,
)
where

Expand All @@ -45,7 +46,7 @@ import Data.Array.Polarized.Pull.Internal
import qualified Data.Functor.Linear as Data
import Data.Vector (Vector)
import qualified Data.Vector as Vector
import Prelude.Linear hiding (foldMap, foldr, reverse, zip, zipWith)
import Prelude.Linear hiding (foldMap, foldr, reverse, uncons, zip, zipWith)
import qualified Unsafe.Linear as Unsafe

-- | Convert a pull array into a list.
Expand Down
11 changes: 8 additions & 3 deletions src/Data/Array/Polarized/Pull/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ instance Data.Functor Array where
-- is interesting in and of itself: I think this is like an n-ary With), and
-- changing the other arrows makes no difference)

-- | Create an empty pull array
empty :: Array a
empty = fromFunction (\_ -> error "Data.Array.Polarized.Pull.Internal.empty: this should never be called") 0

-- | Produce a pull array of lenght 1 consisting of solely the given element.
singleton :: a %1 -> Array a
singleton = Unsafe.toLinear (\x -> fromFunction (\_ -> x) 1)
Expand Down Expand Up @@ -110,6 +114,7 @@ split k (Array f n) = (fromFunction f (min k n), fromFunction (\x -> f (x + k))
reverse :: Array a %1 -> Array a
reverse (Array f n) = Array (\x -> f (n + 1 - x)) n

-- | Index a pull array (without checking bounds)
index :: Array a %1 -> Int -> (a, Array a)
index (Array f n) ix = (f ix, Array f n)
-- | Decompose an array into its head and tail, returns @Nothing@ if the array is empty.
uncons :: Array a %1 -> Maybe (a, Array a)
uncons (Array _ 0) = Nothing
uncons (Array f n) = Just (f 0, fromFunction (\x -> f (x + 1)) (n - 1))
14 changes: 13 additions & 1 deletion test/Test/Data/Polarized.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Test.Data.Polarized (polarizedArrayTests) where
import qualified Data.Array.Polarized as Polar
import qualified Data.Array.Polarized.Pull as Pull
import qualified Data.Array.Polarized.Push as Push
import Data.Functor.Linear (fmap)
import qualified Data.Vector as Vector
import Hedgehog
import qualified Hedgehog.Gen as Gen
Expand Down Expand Up @@ -34,10 +35,12 @@ polarizedArrayTests =
testPropertyNamed "Push.make ~ Vec.replicate" "pushMake" pushMake,
testPropertyNamed "Pull.append ~ Vec.append" "pullAppend" pullAppend,
testPropertyNamed "Pull.asList . Pull.fromVector ~ id" "pullAsList" pullAsList,
testPropertyNamed "Pull.empty = []" "pullEmpty" pullEmpty,
testPropertyNamed "Pull.singleton x = [x]" "pullSingleton" pullSingleton,
testPropertyNamed "Pull.splitAt ~ splitAt" "pullSplitAt" pullSplitAt,
testPropertyNamed "Pull.make ~ Vec.replicate" "pullMake" pullMake,
testPropertyNamed "Pull.zip ~ zip" "pullZip" pullZip
testPropertyNamed "Pull.zip ~ zip" "pullZip" pullZip,
testPropertyNamed "Pull.uncons ~ uncons" "pullUncons" pullUncons
]

list :: Gen [Int]
Expand Down Expand Up @@ -88,6 +91,10 @@ pullAsList = property Prelude.$ do
xs <- forAll list
xs === Pull.asList (Pull.fromVector (Vector.fromList xs))

pullEmpty :: Property
pullEmpty = property Prelude.$ do
([] :: [Int]) === Pull.asList Pull.empty

pullSingleton :: Property
pullSingleton = property Prelude.$ do
x <- forAll randInt
Expand Down Expand Up @@ -115,3 +122,8 @@ pullZip = property Prelude.$ do
let xs' = Pull.fromVector (Vector.fromList xs)
let ys' = Pull.fromVector (Vector.fromList ys)
zip xs ys === Pull.asList (Pull.zip xs' ys')

pullUncons :: Property
pullUncons = property Prelude.$ do
xs <- forAll list
uncons xs === fmap (fmap Pull.asList) (Pull.uncons (Pull.fromVector (Vector.fromList xs)))

0 comments on commit 16795c7

Please sign in to comment.