I'm looking at the references at the backpack
wiki trying to understand in which cases the use of backpack
would be considered appropriate over other Haskell features like type-classes and type-families.
In particular, in this blog-post by the author of backpack
, an example is presented that implements a simple matcher for regular expressions. However as far as I understand, the same module could have been coded using type families.
Are there any examples that concisely show the advantages of backpack over more traditional Haskell features? If the example I referred above is a good one, do you know why a solution that uses type families would be subpar?
Main benefits of Backpack:
I wrote a blog post about one particular usage of Backpack: implementing a polymorphic interface for containers data structures:
Having such an interface for containers allows to:
Map
, HashMap
and IntMap
).If terms of making code cleaner. This is the signature of the groupBy
function implemented using Backpack:
groupBy :: forall k f a . (Foldable f, Key k) => (a -> k) -> f a -> Map k (NonEmpty a)
It's clear, and it's just a plain Haskell. If you implement an interface for containers using typeclasses and type families (this is done in relude
, this signature will look like this:
groupBy
:: forall f t a . (Foldable f, DynamicMap t, Val t ~ NonEmpty a, Monoid t)
=> (a -> Key t) -> f a -> t
Much harder to read and understand.
Also it was discussed recently that Backpack can help with avoiding CPP when you need to compile Haskell code targetting different platforms (aka conditional compilation).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With