Add instance Monad ZipList

Gershom B gershomb at gmail.com
Thu Jun 4 18:10:43 UTC 2020


I think that the counterexample I linked should be enough to rule out any instance for monad, in fact, in conjunction with the fact that to make liftA2 (,) correspond to zip, we need to have the join operation result in something which is the minimum of the length of the enclosing ziplist and all “enclosed” ziplists.

I think but haven’t proven that it is the case that not only is infinite ziplist (i.e. “stream”) a monad, and every fixed-size ziplist a monad, but also ziplist is “categorically” a monad if we consider only functions a -> m b which are natural with regards to list length. I.e. ZipVect N is a graded monad under the min monoid.

At one point I think I worked out, but haven’t found the code, that a cps-transformed ziplist can be made a monad (or at least overcome the counterexample), through fixing associativity by forcing reassociation of all binds (a la Voigtlander’s codensity trick).  If that works, it’s a nice curiosity, but I don’t think people are really crying out with a use case for a “proper” monad for ziplist anyway.

-g
On Jun 4, 2020, 12:43 PM -0400, Gershom B <gershomb at gmail.com>, wrote:
> I know of counterexamples that show why the “obvious” instances don’t work. One nice one is here:
>
> https://www.reddit.com/r/haskell/comments/nfyvy/instance_monad_ziplist_where/c38x9q9?utm_source=share&utm_medium=web2x
>
> But I don’t know of a simple full proof of why no such instance is possible. My guess would be that we should consider the action of the operations on lengths of lists, and show that the requirements for monad and ziplist conflict.
>
> -g
> On Jun 4, 2020, 11:57 AM -0400, David Feuer <david.feuer at gmail.com>, wrote:
> > To add documentation, we need an explanation of *why* it's impossible.
> >
> > > On Thu, Jun 4, 2020, 11:35 AM chessai . <chessai1996 at gmail.com> wrote:
> > > > Agreed, that would be a great addition.
> > > >
> > > > > On Thu, Jun 4, 2020, 8:31 AM Simon Jakobi <simon.jakobi at googlemail.com> wrote:
> > > > > > This sounds worth documenting though.
> > > > > >
> > > > > > Dannyu, would you be interested in updating the ZipList docs to say
> > > > > > why it doesn't have a Monad instance?
> > > > > >
> > > > > > Cheers,
> > > > > > Simon
> > > > > >
> > > > > > Am Do., 4. Juni 2020 um 17:21 Uhr schrieb chessai . <chessai1996 at gmail.com>:
> > > > > > >
> > > > > > > David is right. This can't happen, unfortunately
> > > > > > >
> > > > > > > On Thu, Jun 4, 2020, 12:48 AM David Feuer <david.feuer at gmail.com> wrote:
> > > > > > >>
> > > > > > >> I don't remember why right now, but it's moderately well-known that there is no possible Monad instance compatible with the Applicative instance for ZipList. See the answers to https://stackoverflow.com/questions/6463058/help-on-writing-the-colist-monad-exercise-from-an-idioms-intro-paper by pigworker (Conor McBride) and C. A. McCann.
> > > > > > >>
> > > > > > >> On Thu, Jun 4, 2020, 2:53 AM Dannyu NDos <ndospark320 at gmail.com> wrote:
> > > > > > >>>
> > > > > > >>> instance Monad ZipList where
> > > > > > >>>     ZipList [] >>= _ = ZipList []
> > > > > > >>>     ZipList (x:xs) >>= f = ZipList $ do
> > > > > > >>>         let ZipList y' = f x
> > > > > > >>>         guard (not (null y'))
> > > > > > >>>         let ZipList ys = ZipList xs >>= ZipList . join . maybeToList . fmap snd . uncons . getZipList . f
> > > > > > >>>         head y' : ys
> > > > > > >>>
> > > > > > >>> instance MonadFail ZipList where
> > > > > > >>>     fail _ = empty
> > > > > > >>>
> > > > > > >>> instance MonadPlus ZipList
> > > > > > >>> _______________________________________________
> > > > > > >>> Libraries mailing list
> > > > > > >>> Libraries at haskell.org
> > > > > > >>> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
> > > > > > >>
> > > > > > >> _______________________________________________
> > > > > > >> Libraries mailing list
> > > > > > >> Libraries at haskell.org
> > > > > > >> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
> > > > > > >
> > > > > > > _______________________________________________
> > > > > > > Libraries mailing list
> > > > > > > Libraries at haskell.org
> > > > > > > http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
> > _______________________________________________
> > Libraries mailing list
> > Libraries at haskell.org
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20200604/e8fd3385/attachment.html>


More information about the Libraries mailing list