Mon 5 May 2008
Twan van Laarhoven pointed out that fzip from the other day is a close cousin of applicative chosen to be an inverse of the universal construction 'unfzip'.
During that post I also put off talking about the dual of zipping, so I figured I'd bring up the notion of choosing a notion of 'cozipping' by defining it as an inverse to a universally definable notion of 'counzipping'.
[Edit: Twan pointed out I had flipped which was the dual of zip, revised]
Abusing the new category-extras to avoid making an enormous post and recycling the constructions from the previous post, we can define:
{-# LANGUAGE FlexibleInstances #-} module Control.Functor.Cozip where import Control.Arrow ((|||),(+++)) import Control.Monad.Identity import Control.Bifunctor.Biff import Control.Bifunctor.Fix
The same inverse question leads to some observations about the dual of fzip as opposed to its inverse.
We could call them cozip and uncozip for lack of a better name, but as we will see cozip is more accurately about deciding classifying the contents of the functor, so maybe deserves a better, more evocative name like 'decide' or 'cleave'.
counzip and its bifunctorial equivalent always exist.
counzip :: Functor f => Either (f a) (f b) -> f (Either a b) counzip = fmap Left ||| fmap Right counbizip :: Bifunctor f => Either (f a c) (f b d) -> f (Either a b) (Either c d) counbizip = bimap Left Left ||| bimap Right Right
But there are some cases where its inverse doesn't exist, such as the Reader/State monads, or where uncozip only has a right inverse like with Maybe/Either.
Counzipping basically demonstrates the fact that if I have either a container of a's or a container of b's, I can treat that as a container of 'as or bs', giving up the knowledge that the container contains entirely one or the other.
Its inverse describes the cases where we can recover this information.
class Functor f => Cozip f where cozip :: f (Either a b) -> Either (f a) (f b) instance Cozip Identity where cozip = bimap Identity Identity . runIdentity
Now, in general a functor that has more than one 'hole' will not be recoverable because one hole could contain an a, and the other could contain a b, so you would be unable to perform the split. Futhermore unless there is a way to decide the value contained you'll never be able to tell which branch of the Either to return, so functors like the reader/state monads are out.
However, this does not close the door to a few other functors:
instance Cozip ((,)c) where cozip (c,ab) = bimap ((,)c) ((,)c) ab -- ambiguous choice instance Cozip Maybe where cozip = maybe (Left Nothing) (bimap Just Just) -- cozip = maybe (Right Nothing) (bimap Just Just) -- ambiguous choice instance Cozip (Either c) where cozip = (Left . Left) ||| bimap Right Right -- cozip = (Right . Left) ||| bimap Right Right
Note that the definitions for Maybe and (Either c) had to 'choose' where to put the "Nothing/Left" term. Consequently they are only right-inverses of counzip.
You can also go and generate one that says that the functor coproduct of a pair of cozippable functors is cozippable, just like the functor product of a pair of zippable functors is zippable (given by the construction given for BiffB the other day).
Finally, the only surprising instance is for the free monad of a cozippable functor.
-- instance Cozip f => Cozip (Free f) where instance Cozip f => Cozip (FixB (BiffB Either Identity f)) where cozip (InB (BiffB (Left (Identity (Left a))))) = Left (InB (BiffB (Left (Identity a)))) cozip (InB (BiffB (Left (Identity (Right a))))) = Right (InB (BiffB (Left (Identity a)))) cozip (InB (BiffB (Right as))) = ((InB . BiffB . Right) +++ (InB . BiffB . Right)) (cozip (fmap cozip as))
This says that even though Either is not 'bicozippable' - which appears to be an ill-defined concept - we can build up a general cozip for the free monad of an cozippable functor. The reason is that if your functor only has one place to put a value, then putting the free monad in that place just means that you have to search longer.
So, we've found the fact that free monads of cozippable functors are cozippable, in contrast to the conclusion of the other day that cofree comonads of zippable functors are zippable.
May 5th, 2008 at 7:23 pm
An interesting post, I was thinking along a slightly different (and wrong) route myself.
First of all, shouldn’t cozip be called counzip and vice-versa? fzip has type “(f a * f b) -> f (a*b)” doesn’t taking the dual give “(f a + f b) Either (f a) (f b)”?
I was thinking about the type
> almostcozip :: f (Either a b) -> (f a, f b)
This function can be implemented unambiguously for [] and Maybe, and is in quite useful in practice. It also has a right inverse,
> acounzip :: (f a, f b) -> f (Either a b)
If (f x) is a Monoid then
> acounzip (fa, fb) = fmap Left fa `mappend` fmap Right fb
Thinking about it some more, there is also the dual type
> azip :: Either (f a) (f b) -> f (a,b)
> aunzip :: f (a,b) -> Either (f a) (f b)
But I can’t think of any nice function with this type.
May 5th, 2008 at 8:00 pm
You’re right, I flipped the names when I was slapping together the code over lunch. I had left my notes at home. I’ll fix it up.
Hrmm, of course now you need an extra ‘fair’ acounzip as well.
August 4th, 2010 at 3:03 pm
Counzipping makes a pretty good toy model of the runtime type erasure that happens in Java and Scala generics — it pushes a tag from the root of a container down into the individual ‘holes’. Cases where the cozip doesn’t exist become unsafe ‘unchecked casts’. I wonder if this sort of analysis could be used as a basis for better compile-time analysis of where unsafety occurs.
September 23rd, 2022 at 3:49 am
order stromectol over the counter https://500px.com/p/skulogovid/?view=groups...
Factor clearly applied!!…
September 23rd, 2022 at 7:55 am
Viagra bula https://500px.com/p/bersavahi/?view=groups...
Appreciate it! An abundance of advice!
…
September 24th, 2022 at 12:58 am
Viagra canada https://reallygoodemails.com/canadianpharmaceuticalsonlineusa...
Lovely postings. Many thanks….
September 24th, 2022 at 4:37 am
northwestpharmacy https://www.provenexpert.com/canadian-pharmaceuticals-online-usa/...
Wow loads of awesome data….
September 26th, 2022 at 8:39 am
Viagra vs viagra https://melaninterest.com/user/canadian-pharmaceuticals-online/?view=likes...
Seriously many of excellent info!…
September 26th, 2022 at 12:42 pm
Viagra purchasing https://haikudeck.com/canadian-pharmaceuticals-online-personal-presentation-827506e003...
Whoa a good deal of wonderful facts!…
September 26th, 2022 at 4:51 pm
Viagra alternative https://buyersguide.americanbar.org/profile/420642/0...
Amazing plenty of very good info!…
September 27th, 2022 at 6:31 am
Generic for viagra https://slides.com/canadianpharmaceuticalsonline...
Reliable knowledge. Thank you!…
September 27th, 2022 at 10:08 am
ivermectina https://challonge.com/esapenti...
Regards. A lot of content.
…
September 27th, 2022 at 2:37 pm
Viagra 20mg https://challonge.com/gotsembpertvil...
Appreciate it. Plenty of facts.
…
September 28th, 2022 at 7:57 am
Low cost viagra 20mg https://order-stromectol-over-the-counter.estranky.cz/clanky/order-stromectol-over-the-counter.html...
Good postings. Appreciate it!…
September 28th, 2022 at 2:24 pm
stromectol pill https://soncheebarxu.estranky.cz/clanky/stromectol-for-head-lice.html...
Seriously tons of beneficial info….
September 29th, 2022 at 3:56 am
Viagra reviews https://lehyriwor.estranky.sk/clanky/stromectol-cream.html...
Reliable stuff. Cheers….
September 29th, 2022 at 7:42 am
Cheap viagra https://dsdgbvda.zombeek.cz/...
You actually expressed that well….
September 29th, 2022 at 2:09 pm
Viagra prices https://inflavnena.zombeek.cz/...
Great postings. Thanks….
September 30th, 2022 at 8:26 am
Low cost viagra 20mg https://www.myscrsdirectory.com/profile/421708/0...
Many thanks! I value this….
September 30th, 2022 at 3:36 pm
Tadalafil tablets https://supplier.ihrsa.org/profile/421717/0...
You said it adequately.!…
October 1st, 2022 at 5:57 am
northwest pharmacies https://wefbuyersguide.wef.org/profile/421914/0...
Truly lots of awesome data….
October 1st, 2022 at 9:58 am
Viagra reviews https://legalmarketplace.alanet.org/profile/421920/0...
Good stuff, Many thanks….
October 2nd, 2022 at 3:35 am
Discount viagra https://moaamein.nacda.com/profile/422018/0...
Kudos, A good amount of knowledge.
…
October 2nd, 2022 at 8:02 am
Generic viagra https://www.audiologysolutionsnetwork.org/profile/422019/0...
Really loads of fantastic info!…
October 2nd, 2022 at 11:19 am
Viagra generico https://network.myscrs.org/profile/422020/0...
Kudos! I appreciate this!…
October 3rd, 2022 at 5:05 am
Viagra for sale https://sanangelolive.com/members/canadianpharmaceuticalsonlineusa...
Very good write ups. Cheers!…
October 3rd, 2022 at 8:29 am
How does viagra work https://sanangelolive.com/members/girsagerea...
You actually said that well!…
October 4th, 2022 at 7:11 am
safe canadian online pharmacies https://www.ecosia.org/search?q=“My Canadian Pharmacy – Extensive Assortment of Medications – 2022″…
Wonderful postings. Thanks….
October 4th, 2022 at 10:55 am
canada pharmacies online prescriptions https://www.mojomarketplace.com/user/Canadianpharmaceuticalsonline-EkugcJDMYH...
Fine info. With thanks….
October 4th, 2022 at 3:20 pm
online pharmacies canada https://seedandspark.com/user/canadian-pharmaceuticals-online...
Thanks a lot. Useful stuff….
October 5th, 2022 at 3:13 am
Viagra rezeptfrei https://www.giantbomb.com/profile/canadapharmacy/blog/canadian-pharmaceuticals-online/265652/...
You actually suggested that fantastically!…
October 5th, 2022 at 12:13 pm
canadadrugs https://feeds.feedburner.com/bing/Canadian-pharmaceuticals-online...
You’ve made your stand very well.!…
October 5th, 2022 at 5:02 pm
Viagra bula https://search.gmx.com/web/result?q=“My Canadian Pharmacy – Extensive Assortment of Medications – 2022″…
Superb stuff. Thank you….
October 6th, 2022 at 2:08 am
Viagra generico online https://search.seznam.cz/?q=“My Canadian Pharmacy – Extensive Assortment of Medications – 2022″…
Amazing loads of superb knowledge….
October 6th, 2022 at 5:50 am
Tadalafil 5mg https://sanangelolive.com/members/unsafiri...
Awesome advice. Many thanks!…
October 6th, 2022 at 10:07 am
Viagra reviews …
Wow a lot of great knowledge….
October 6th, 2022 at 4:03 pm
canadian pharcharmy online https://swisscows.com/en/web?query=“My Canadian Pharmacy – Extensive Assortment of Medications – 2022″…
You actually expressed this very well!…
October 7th, 2022 at 3:20 am
Generic for viagra https://www.dogpile.com/serp?q=“My Canadian Pharmacy – Extensive Assortment of Medications – 2022″…
Regards! I enjoy this!…
October 7th, 2022 at 10:29 am
5 mg viagra coupon printable …
With thanks, Loads of information!
…
October 8th, 2022 at 6:28 am
canadian prescriptions online https://search.givewater.com/serp?q=“My Canadian Pharmacy – Extensive Assortment of Medications – 2022″…
Wonderful forum posts. With thanks….
October 8th, 2022 at 11:57 am
canadian pharmacies online prescriptions https://www.bakespace.com/members/profile/Сanadian pharmaceuticals for usa sales/1541108/…
Lovely posts. Thanks a lot….
October 9th, 2022 at 2:42 pm
Viagra or viagra https://www.infospace.com/serp?q=“My Canadian Pharmacy – Extensive Assortment of Medications – 2022″…
Regards! A lot of tips.
…
October 11th, 2022 at 4:54 am
Viagra 5 mg funziona https://results.excite.com/serp?q=“My Canadian Pharmacy – Extensive Assortment of Medications – 2022″…
You said it adequately.!…
October 11th, 2022 at 10:17 am
Viagra purchasing https://canadianpharmaceuticalsonline.as.me/schedule.php...
Amazing content. Thank you!…
October 13th, 2022 at 12:13 pm
Viagra reviews https://feeds.feedburner.com/bing/stromectolnoprescription...
Whoa lots of very good material!…