Home > Enterprise >  Fixed size arrays in Haskell
Fixed size arrays in Haskell

Time:04-28

I was trying to write a library for linear algebra operations in Haskell. In order to be able to define safe operations for matrices and vectors I wanted to encode their dimensions in their types. After some research I found that using DataKinds one is able to do that, similar to the way it's done here. For example:

data Vector (n :: Nat) a

dot :: Num a => Vector n a -> Vector n a -> a

In the aforementioned article, as well as in some libraries, the size of the vectors is a phantom type and the vector type itself is a wrapper around an Array. In trying to figure out if there is a array type with its size at the type-level in the standard library I started wondering about the underlying representation of arrays. From what I could gather form this commentary on GHC memory layout, arrays need to store their size on the heap so a 3-dimensional vector would need to take up 1 more word than necessary. Of course we could use the following definition:

data Vector3 a = Vector3 a a a

which might be fine if we only care about 3D geometry, but it doesn't allow for vectors of arbitrary size and also it makes indexing awkward.

So, my question is this. Wouldn't it be useful and a potential memory optimization to have an array type with statically known size in the standard library? As far, as I understand the only thing that it would need is a different info table, which would store the size, instead of it being stored for at each heap object. Also, the compiler could choose between Array and SmallArray automatically.

CodePudding user response:

Wouldn't it be useful and a potential memory optimization to have an array type with statically known size in the standard library?

Sure. I suspect if you wrote up your use case carefully and implemented this, GHC HQ would accept a patch. You might want to do the writeup first and double-check that they're into it to avoid wasting time on a patch they won't accept, though; I certainly don't speak for them.

Also, the compiler could choose between Array and SmallArray automatically.

I'm not an expert here, but I kinda doubt this. Usually supporting polymorphism means you need a uniform representation.

  • Related