persistent-vector icon indicating copy to clipboard operation
persistent-vector copied to clipboard

Get rid of extra indirections (WIP)

Open treeowl opened this issue 5 years ago • 4 comments

Setting out to implement the ideas in #12.

  • Store a Vector_ in the root node instead of an Array of Vector_s. This simplifies almost everything and should make the code smaller. This seems especially important for folds, which we generally want to inline. But we get one extra indirection per vector until we actually unlift everything.

  • Make Vector_ a GADT. This lets us use the type system to enforce the height balance invariant. Ultimately, this is going to have to look different, but I think it's not a bad first step.

  • Add a validity test to check the rest of the data structure invariants.

treeowl avatar Oct 21 '20 20:10 treeowl

A couple thoughts:

  1. I think I actually want to work mostly with UnliftedArray# rather than UnliftedArray. So I probably want a module providing relatively user-friendly operations on UnliftedArray# (using Int rather than Int#, using ST with Box rather than explicit state-token passing, etc).

  2. To make things as backwards compatible as possible, use a data family rather than an injective type family. But we want to discard those newtype constructors with as little fuss as possible. I'm developing a pretty decent sense of how I think I want that to work.

treeowl avatar Oct 30 '20 00:10 treeowl

There is another alternative to working mostly with UnliftedArray#, I think. The idea is to generalize Array and UnliftedArray to a unified type. This will be annoying, but it might shift the burden of ugliness into a simpler space.

-- Keep the kind open to support unboxed arrays?
data L
data U
type family Blob (l :: Type) (a :: Type) :: TYPE 'UnliftedRep
type instance Blob L a = Array# a
type instance Blob U a = UnliftedArray# (Unlifted a)

data Array (l :: Type) (a :: Type) = Array (Blob l a)

What's this do? It lets us yank an Array out of an Array 'U without having to determine whether it's actually an Array# or an UnliftedArray#.

treeowl avatar Oct 30 '20 00:10 treeowl

No, that second thing isn't quite right. We need to add in an assumption that an Array U holds an Array x... hmm... Might not work out.

treeowl avatar Oct 30 '20 00:10 treeowl

Yeah, I think I'm going with the first idea. It sucks, but at least I know how to make it work.

treeowl avatar Oct 30 '20 00:10 treeowl