I searched Hackage and couldn't find anything like the following but it seems to be fairly simple and useful. Is there a library that contains sort of data type?
data HList c where
(:-) :: c a => a -> HList c
Nil :: HList c
All the HLists I found could have any type, and weren't constrained.
If there isn't I'll upload my own.
The
generics-sop
package offers this out of the box.A heterogeneous list can be defined in
generics-sop
by usingand instantiating it to the identity type constructor
I
(fromgenerics-sop
) orIdentity
(fromData.Functor.Identity
).The library then offers the constraint
All
such that e.g.is the type of a heterogeneous list where all contained types are in the
Show
class. Conceptually,All
is a type family that computes the constraint for every element in a type-level list:(Only that in the actual definition,
All
is additionally wrapped in a type class so that it can be partially applied.)The library furthermore offers all sorts of functions that traverse and transform
NP
s given a common constraint.