Skip to content

Feature Request: Make underlying representations accessible #171

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".

Activity

  1. cartazio commented on Jun 2, 2017

    @cartazio
    Contributor
  2. axman6 commented on Jun 22, 2017

    @axman6
    Author

    So my intent was to add the unwrapping features to the Vector class, or the type family which defines the structure of the representations. I'm just not sure what the best way to implement this would be.

    The cbor package (now officially to be known as cborg) should be released by the first of July (hopefully).

  3. cartazio commented on Jun 22, 2017

    @cartazio
    Contributor
  4. dolio commented on Jul 4, 2017

    @dolio
    Contributor

    Hmm...

    It's been on my todo list for far too long to expose the constructors of various vector types. However, I hadn't thought of making an abstraction for it.

    I guess my question is: is this actually any better than just having the constructors available? It doesn't seem like there is much hope for writing code that is abstract over the representation, because that can change with vector and element type, and all you have is the ability to convert between the vector and its underlying representation. Is there something in cborg that would make this more useful than it seems to me?

    As far as exposing the internals goes in general, I'm for it.

  5. utdemir commented on May 13, 2021

    @utdemir

    As far as exposing the internals goes in general, I'm for it.

    I just wanted to mention that I would also appreciate being able to access the Vector constructor. We have an issue on linear-base, where we have a Array#, and going from that to a Vector is a bit annoying without having access to the Vector constructor.

  6. lehins commented on May 13, 2021

    @lehins
    Contributor

    @utdemir Going from Array# shouldn't be a problem with this new fromArray conversion function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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