Possible Changes to Bundle.Chunk?
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Feature
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- haskell
- Domain
- data, performance
Research direction
Start with Data.Vector.Fusion.Bundle.Monadic and compare the three proposed Chunk designs, including their compatibility implications. The issue does not identify a target API, implementation file beyond the module, or tests. First obtain a decision on which ranked option to pursue; done means the chosen Chunk change and any proposed utility or instances are implemented with an agreed import location.
Written by the indexing model from the issue text.
Description
As it is, the Chunk type in Data.Vector.Fusion.Bundle.Monadic is much less useful than it could be.Here are some ways, ranked by desirability and compatibility-breaking, on how it could be better.
- Add a Monoid instance and utility functions for Chunk.
This is the simplest change, and one that breaks the fewest programs. The most general utility functions would probably be toChunk :: (Vector v a) => v a -> Chunk v a and fromChunk :: (Vector v a) => Chunk v a -> v a, and possibly singleton :: (Vector v a) => a -> Chunk v a. This would let Chunks be used like ByteString Builders, as a low-overhead, O(1) concatenation option.
The definition for Monoid would be:
import qualified Data.Vector.Generic.Mutable as M
instance Monoid (Chunk v a) where
mempty = Chunk 0 (const $ return ())
mappend (Chunk na wa) (Chunk nb wb) = Chunk (na + nb) (\v -> wa (M.take na v) >> wb (M.drop na v))
- Change the type of Chunk.
This would involve a Yoneda transform on Chunk's type:
data Chunk v a = Chunk Int (forall m r. (PrimMonad m, Vector v r) => Mutable v (PrimState m) r -> (a -> r) -> m ())
This would turn Chunk into a Functor, meaning the Functor instance for Bundle could be a lot simpler and involve a lot less copying. Also, this prevents badly-behaved Chunks from reading and mutating the MVector they are given.
- Change the kind of Chunk.
If you generalize, then you can make Chunks that can construct any type of Vector:
data Chunk a = Chunk Int (forall m v r. (PrimMonad m, Vector v r) => Mutable v (PrimState m) r -> (a -> r) -> m ())
In addition to making Chunks able to produce any kind of Vector, this also admits an Applicative instance for Chunk!
import qualified Data.Vector.Generic.Mutable as M
import qualified Data.Vector.Mutable as MV
instance Applicative Chunk where
pure a = Chunk 1 (\v c -> M.write v 0 (c a))
(Chunk nf wf) <*> (Chunk na wa) = Chunk (nf * na) $ \v c -> do
vf <- MV.new nf
wf vf id
let loop n = if n >= nf then return () else do
f <- MV.read vf n
wa (M.slice (n * na) na v) (c . f)
loop (n + 1)
loop 0
(Chunk na _) *> (Chunk nb wb) = Chunk (na * nb) $ \v c -> do
let loop n = if n >= na then return () else wb (M.slice (n * nb) nb) c >> loop (n + 1)
loop 0
This also makes it an Alternative, with empty and <|> defined as mempty and mappend, respectively.
If these changes are made, then it would probably be a good idea to move Chunk from its current location and give it a dedicated import, like Data.Vector.Chunk, for use in building Vectors cheaply.
So is it possible to make these changes and make Chunk more than just a utility type for Bundle?
- Dominant language
- Haskell
- Stars
- 401
- Forks
- 145
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from haskell/vector
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
Similar issues
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
enhancement tricorder
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
zip-archive-0.5 Open
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
commercialhaskell/stackage#8124 · 1 comment ·
-
chore
Difficulty 1/5 Under an hour Newbie friendliness 91/100
alunduil/alunduil-chezmoi#792 ·