Questions tagged [distributive]

The categorical dual of Traversable (Data.Distributive in Haskell). This is a structure that builds on Functor and is an expression of distributive laws (an arithmetic example is a(b + c) = ab + ac).

Due to the lack of non-trivial comonoids in Haskell, we can restrict ourselves to requiring a Functor rather than some Coapplicative class. Categorically every Distributive functor is actually a right adjoint, and so it must be Representable endofunctor and preserve all limits. This is a fancy way of saying it isomorphic to (->) x for some x.

To be distributable a container will need to have a way to consistently zip a potentially infinite number of copies of itself. This effectively means that the holes in all values of that type, must have the same cardinality, fixed sized vectors, infinite streams, functions, etc. and no extra information to try to merge together.

See Data.Traversable by Edward Kmett.

9 questions
11
votes
2 answers

Why are traversals defined over Applicatives, fundamentally?

I've been on a bit of a "distilling everything to its fundamentals" kick lately, and I've been unable to find clear theoretical reasons for how the Traversable typeclass is defined, only practical ones of "it's useful to be able to traverse over…
janet
  • 221
  • 1
  • 9
8
votes
2 answers

Is there any typeclass that defines the function from `a -> m b` to `m (a -> b)`?

The function from a -> m b to m (a -> b) rarely appears in programming, but can be made in the Reader monad. The following code is a tentative implementation. Does such a library exist? class Monad m => MonadShift m where shift :: (a -> m b) -> m…
Hexirp
  • 410
  • 2
  • 11
7
votes
2 answers

Is there such thing as a bidistributive? What function do I need here?

I have code (in C# actually, but this question has nothing to do with C# specifically, so I will speak of all my types in Haskell-speak) where I am working inside of an Either a b. I then bind a function with a signature that in Haskell-speak is b…
Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
5
votes
1 answer

Can ZipList be Distributive?

Base provides ZipList, which is just a wrapper for [] where <*> is based on zip instead of cartesian-product. This isn't the default because it's not consistent with the Monad [] instance, but some people find it more intuitive, and both behaviors…
ShapeOfMatter
  • 991
  • 6
  • 25
4
votes
2 answers

What is a cocartesian comonoid, and what is a cocartesian comonoidal functor?

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…
janet
  • 221
  • 1
  • 9
3
votes
1 answer

How to write a Representable instance using only Distributive properties?

Say I've got a Distributive instance written for some complex custom type, Foo. Is it possible to write Foo's Representable instance using only the properties available from its Distributive instance? And, if not, then why is Distributive a…
dbanas
  • 1,707
  • 14
  • 24
2
votes
2 answers

How can I get from `a -> Parser b` to `Parser (a -> b)`?

I'm trying to update a parsec parser that uses buildExpressionParser from Text.Parsec.Expr. I'm trying (and possibly this is ill-advised, but it looks like it's supposed to be practical) to build part of the DSL verification into the parser. But I'm…
ShapeOfMatter
  • 991
  • 6
  • 25
2
votes
1 answer

Is there a name for this higher-level "bi" version of distribute in Haskell?

I have a Bitraversable called t that supports this operation: someName :: Monad m => (t (m a) (m b) -> c) -> m (t a b) -> c In other words, it's possible to take a function that accepts two monads packaged into the bitraversable and turn it into a…
jacobsa
  • 5,719
  • 1
  • 28
  • 60
2
votes
2 answers

Issue when using distributive conditional types combined with generic method

I've been trying to make a generic function which receives and object T and receives a string property name of that object T. I used https://www.typescriptlang.org/docs/handbook/advanced-types.html as an example (section: Distributive conditional…