Skip to content

Feature Request: Make underlying representations accessible #171

@axman6

Description

@axman6

In the binary-serialise-cbor repo, there's been some discussion about the difficulty of being able to implement Serialise for Vectors which use more efficient representations of vectors than providing an array of structures in the encoded form. In well-typed/cborg#62 I had proposed making the underlying representation of Vectors accessible via an associated type (this isn't precise when compared to the current implementation but I hope gets the idea across):

class Vector v a where
    type VRepr v a :: *
    toVRepr :: v a -> VRepr v a
    fromVRepr :: VRepr v a -> Maybe (v a)
    ... -- The rest of the current Vector class

instance Vector v Int where
  type VRepr v Int = v Int -- this is certainly not correct

instance (Vector v a, Vector v b) => Vector v (a,b) where
    type VRepr v (a,b) = (Vector v a, Vector v b)
    -- Or perhaps?
    -- type VRepr v (a,b) = (VRepr v a, VRepr v b)
    toVRepr (V_M2 _ va vb) = (va,vb) -- AKA unzip
    ...

Which allows for defining instances for the primitive types and instances for things like (Serialise a, Serialise b) => Serialise (a,b) and we get the instances for Serialise (a,b), Serialise (Complex a) etc. essentially for free.

This is something I've run into a few times, where I wanted to take advantage of the underlying array of structures representation of Vectors without needing to know exactly which constructor was being used.

This is (obviously) not a very thorough proposal, I'm making it to spark discussion, and I'll be happy if the result is "no, because ... impossible ...", or "yes, we'll do this and change the major version".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions