Arrays where the index type is a function of the shape type

Version on this page:
LTS Haskell 22.29:0.5.5
Stackage Nightly 2024-07-20:0.5.5
Latest on Hackage:0.5.5

See all snapshots comfort-array appears in

Arrays from the basic array package are already very powerful compared with arrays in other languages. They may have any number of dimensions, are type safe and defined in a uniform way using the Ix class with free choice of the lower bounds (0, 1, or whatever you like).

This package goes one step further: The shape and the index type are different, but the index type is a type function of the shape type. This offers much more flexibility and type safety.

Some examples are:

  • Range: Allow dynamic choice of lower and upper array bounds such as in the Arrays from the array package. You can combine it with other shapes in other dimensions. It allows you to describe the bounds of each dimension individually.

  • Shifted: Describe array bounds by start index and length. It is sometimes more natural to use these parameters. E.g. a non-negative index type like Word cannot represent -1 and thus cannot encode an empty range starting with index 0.

  • ZeroBased, OneBased: Arrays with fixed lower bound, either 0 or 1, respectively.

  • Cyclic: Indices with wrap-around semantics.

  • Zero, (): Arrays with fixed size 0 or 1, respectively.

  • Enumeration: Arrays with indices like LT, EQ, GT and a shape of fixed size.

  • NestedTuple: Arrays with shapes that are compatible to nested tuples like (a,(a,a)) and indices like fst and fst.snd.

  • (::+): The Append type constructor allows to respresent block arrays, e.g. block matrices. It also allows to represent non-empty arrays via ()::+sh.

  • Set: Use an arbitrary ordered set as index set.

  • Map: Concatenate a set of shapes.

  • Triangular: A 2D array with the shape of a lower or upper triangular matrix.

  • Simplex: Simplices of any dimension, where the dimension is encoded in the type. An index is a tuple of monotonic ordered sub-indices.

  • Square: A 2D array where both dimensions always have equal size.

  • Cube: A 3D array where all three dimensions always have equal size.

  • Tagged: Statically distinguish shapes and indices that are isomorphic.

With our Array type you can perform

  • Fast Linear Algebra using the packages comfort-blas and lapack. The lapack package defines even more fancy shapes like tall rectangular matrices, triangular matrices and banded matrices.

  • Fast Fourier Transforms using the package comfort-fftw

  • Fast Linear Programming using the packages comfort-glpk and coinor-clp

  • Efficient Array Processing via LLVM Just-In-Time code generation using the package knead.

See also comfort-graph for a Graph data structure, with non-Int node identifiers and flexible edge types.


Change log for the comfort-array package


  •, zipWith, toList: make lazy

  • add unified methods to Shape classes: unifiedSize, unifiedOffset, unifiedSizeOffset, uncheckedIndexFromOffset. They simplify to share code between checked and unchecked variants. Actually, many implementations of these methods recursively call themselves on part shapes. However, the default methods have changed.

  • Shape.:+: -> Shape.::+. This resolves the name clash with the :+: operator from tfp. It also highlights the right associativity and non-commutativity.

  • Shape.Simplex


  • use doctest-extract for tests

  • instance Shape Map

  • Shape.Cyclic

  • Shape.Square, Shape.Cube


  • fromAssocations: Make default value the first parameter. It is most oftenly zero and thus less variable than the array size.

  • Shape.DeferredIndex: Now uses the shape as the type parameter, not the index type.


  • Storable.Mutable.Array: Replace ForeignPtr by Array.Guarded.MutablePtr. In the last release we altered the arrays after initialization which corrupted the debugging by the guarded-allocation package. This should be fixed now.

  • Shape.sizeOffset: It does not return a single offset anymore but an offset computation function. This allows to cache a size computation across many offset computations.


  • Add a monad parameter to the mutable Storable array type and generalize functions to PrimMonads. This way the mutating functions can also be used in the ST monad.


  • Add immutable Boxed array type and mutable Storable array type.


  • Split Shape.C into Shape.C and Shape.Indexed.


  • Initial version featuring the Shape.C class with type function Index and the immutable Storable array type.