1. 5
  1.  

  2. 3

    Haskell already has this all over the place, yes? Maybe not as specific/arbitrary as making an entire typeclass for Either, but there’s Alternative, MonadFail, etc.

    And then for deconstruction there’s Foldable, Traversable, etc. Again, not quite specific enough to do every single thing you can do with Either, but how many data structures have exactly that interface?

    And if you really want to be fully generic, there’s Generic, which lets you write functions on the structure of the data without knowing the names of its components.

    1. 3

      the functions we develop in our code don’t usually require bits. Instead, they require capabilities. Some to construct, some to deconstruct. If we expressed these capabilities precisely, we could free our code from having to know too much about the structures it operates on, while at the same time making our code much more reusable for different purposes.

      That sounds like making the code much more operational and less declarative, which tends to make code harder rather than easier to reason about. I don’t see data structures as a way to talk about bits (I don’t care about bits), I see them as a way to talk about values. If we want to be able to reason compositionally about f(g(x)), we need to be able to think about g(x) as a value, which is much easier if there’s a way to write down what it looks like.

      1. 1

        This sort of thing is a powerful argument for a macro system…

        1. 1

          Ultimately, I’d argue that today’s programming languages are unnaturally obsessed with data.

          Maybe its something to do with the fact, in it’s day, this was a popular text book at universities….

          https://en.wikipedia.org/wiki/Algorithms_%2B_Data_Structures_%3D_Programs

          That said, D’s ranges and design by introspection provide a very pragmatic answer where many of the library algorithms now are completely unbound from the data structures being fed to them…..

          …except when significant performance advantage can be obtained by special casing to the particular abilities of the particular data structure being used “on the day”.