I've been experimenting with monoids and Distributives lately, and I think I've found something of interest (described in my answer) - are these already known structures? (I've been unable to find any reference to them online, and I don't think I've missed some reason they would be nonsensical)
If not previously known, do they seem useful, or interesting to anyone who isn't me?
The questions that lead me here are:
- What happens if you swap products for sums in comonoids?
- What happens if you swap products for sums in comonoidal functors (coapplicatives, coalternatives, codivisibles, codecidables)? (Only somewhat answered for the first two)
The following is fairly light on the laws behind these structures, as they are a recent product of experimentation.
First, the cocartesian comagma, plus identity:
These are datatypes with an inherent ability to branch - to be a proper comonoid, this branching should not change its value (due to a neat typeclass instance for functions), but that leads to a far less interesting typeclass for the purposes described here.
Here's that instance for functions, corresponding to the monoid-requiring Divisible instance for Op:
This will not be associative unless Split is lawful, unfortunately - but I think its non-associative form could still be of use?
It is also possible to define an Unfoldable typeclass, similar to Foldable for monoids, Foldable1 for semigroups, and their theoretical further family members:
Next: what I think is a "cocartesian coalternative functor":
For algebraic datatypes, Match describes functors with at most one value in each constructor (which can then be observed and pattern-matched over).
Matchable describes functors with exactly one value in each constructor (so an uninhabited value leads to an uninhabited functor).
I believe Matchable is strictly weaker than a Traversable typeclass that traverses with Functors instead of Applicatives, but have not proven this - this would correspond to all Distributives being Applicative. (All algebraic datatypes with exactly one parameter value in each constructor are both Matchable and Traversable.)
Matchables seem interesting to me, being a part of the extended applicative family that I've been unable to find any reference to, but they really come into their own with Distributive functors from the 'distributive' library (the dual of Traversables).
Normal Distributives are isomorphic to
Reader r
for some r, and are famously (I presume famously, at least? It seems well known) equivalent to Representable functors, or right adjoints in Hask. Interpreted for algebraic datatypes, they are the algebraic datatypes with exactly one constructor.These can be extended beyond the Functor-based Distribute, though!
These mirror Traversable ~
(l (), [a])
, Traversable1 ~(l (), NonEmpty a)
, and the much-rarer functor-traversable ~(l (), a)
.(Of interest: for algebraic datatypes, the each Traversable family member has as many records as the Distributive equivalent has constructors, and vice versa)
(Of interest: just as Coapplicatives are trivial in Hask, so are Comatchables - I expect this can be interpreted as Coapplicatives enabling the many records of Distributive, and Comatchables enabling the many constructors of Traversable?)
Matchables also act like Applicatives for defining generic instances, except that while it's products of Applicatives that have a unique Applicative instance, it's actually sums of Matchables that have a unique instance!
Cocartesian Coapplicatives act as the Alternative equivalent - Cocartesian Coapply can be interpreted as being able to choose which side to take in an 'unzip' operation, and Cocartesian Coapplicative describes a totally uninhabited functor like V1 in generics.
In summary:
There are 4 types of monoid family here (monoid, comonoid, cocartesian monoid, cocartesian comonoid), of which the first and last are non-trivial in Hask. Maybe there are 6 if you include These as well as (,) and Either?
There are 6 members of the applicative family here (applicative, alternative, divisible, decidable, matchable, biasable), plus the trivial coapplicative and comatchable - this could presumably be filled out further to a total of 16 members! Or 36 including These as above
Matchable functors enable weaker versions of Distributive - where Distributive's data is always present, the data in a Match-Distributive is only sometimes present, or potentially never present in a Matchable-Distributive
(These-wise applicative = Align from the 'semialign' package, but with fewer laws? corresponding to the relationship between applicatives and zips, seen in ZipList's typeclass instances?)
Edit: A more symmetric Unfoldable instance for lists
It is possible to leave the choice to bias left or right until after-the-fact, using a similar method to the Monofoldable typeclass.
However, useful instances of Split for unfolding will be value-changing, and will have already chosen a left/right bias for these value changes. For example:
As such, the asymmetry is practically inherent to the process, and all that is needed is to be consistent about whether Left or Right signal an end-point for listlike (linear, finite) unfolds.