Thu 4 Dec 2008
The Pointed-Set Comonad
Posted by Edward Kmett under Comonads , Haskell , Uncategorized[50] Comments
Last night, Chung-Chieh Shan posted an example of a pointed-set monad on his blog, which happens to be isomorphic to a non-empty stream monad with a different emphasis.
But, I thought I should point out that the pointed set that he posted also has a comonadic structure, which may be exploited since it is just a variation on the "zipper comonad," a structure that is perhaps more correctly called a "pointing comonad."
But first, a little background:
With combinatorial species you point a data structure by marking a single element in it as special. We can represent that with the product of an element and the derivative of the original type.
F*[A] = A * F'[A]
So, then looking at Shan's pointed set, we can ask what combinatorial species has a list as its derivative?
The answer is a cycle, not a set.
This fact doesn't matter to the monad, since the only way a monadic action interacts with that extra structure is safely through bind, but does for the comonad where every comonadic action has access to that structure, but no control over the shape of the result.
However, we don't really have a way to represent an unordered set in Haskell, so if you are treating a list as a set, the derivative of a set is another set then we can also view the a * [a] as a pointed set, so long as we don't depend on the order of the elements in the list in any way in obtaining the result of our comonadic actions.
I've changed the name of his data type to PointedSet
to avoid conflicting with the definitions of Pointed
and Copointed
functors in category extras.
module PointedSet where import Control.Comonad -- from my category-extras library import Data.List (inits,tails) -- used much later below data PointedSet a = PointedSet a [a] deriving (Eq, Ord, Show, Read) instance Functor PointedSet where fmap f (PointedSet x xs) = PointedSet (f x) $ fmap f xs
The definition for extract is obvious, since you have already selected a point, just return it.
instance Copointed PointedSet where extract (PointedSet x _) = x
On the other hand, for duplicate we have a couple of options. An obvious and correct, but boring implementation transforms a value as follows:
boring_duplicate :: PointedSet a -> PointedSet (PointedSet a) boring_duplicate xxs@(PointedSet x xs) = PointedSet xxs $ fmap (flip PointedSet []) xs
*PointedSet> boring_duplicate $ PointedSet 0 [1..3] PointedSet (PointedSet 0 [1..3]) [ PointedSet 1 [], PointedSet 2 [], PointedSet 3 [] ]
but that just abuses the fact that we can always return an empty list.
Another fairly boring interpretation is to just use the guts of the definition of the Stream comonad, but that doesn't model a set with a single memory singled out.
A more interesting version refocuses on each element of the list in turn, which makes the connection to the zipper comonad much more obvious. Since we want a pointed set and not a pointed cycle, we can focus on an element just by swapping out the element in the list in that position for the focus.
Again, since we can't specify general species in Haskell, this is as close as we can come to the correct comonadic structure for a pointed set. Due to the limitations of our type system, the comonadic action can still see the order of elements in the set, but it shouldn't use that information.
Since we don't care to preserve the order of the miscellaneous set elements, the refocus
helper function below can just accumulate preceding elements in an accumulating parameter in reverse order.
instance Comonad PointedSet where duplicate xxs@(PointedSet x xs) = PointedSet xxs $ refocus [] x xs where refocus :: [a] -> a -> [a] -> [PointedSet a] refocus acc x (y:ys) = PointedSet y (acc ++ (x:ys)) : refocus (y:acc) x ys refocus acc x [] = []
Now,
*PointedSet> duplicate $ PointedSet 0 [1..3] = PointedSet (PointedSet 0 [1,2,3]) [ PointedSet 1 [0,2,3], PointedSet 2 [1,0,3], PointedSet 3 [2,1,0] ]
With that in hand we can define comonadic actions that can look at an entire PointedSet
and return a value, then extend them comonadically to generate new pointed sets.
For instance, if we had a numerical pointed set and wanted to blur our focus somewhat we could weight an average between the focused and unfocused elements:
smooth :: Fractional a => a -> PointedSet a -> a smooth w (PointedSet a as) = w * a + (1 - w) * sum as / fromIntegral (length as)
Smoothing is a safe pointed-set comonadic operation because it doesn't care about the order of the elements in the list.
And so now we can blur the distinction between the focused element and the rest of the set:
*PointedSet> extend (smooth 0.5) $ PointedSet 10 [1..5] PointedSet 6.5 [2.9,3.3,3.7,4.1,4.5]
A quick pass over the comonad laws shows that they all check out.
As noted above, if your comonadic action uses the order of the elements in the list beyond the selection of the focus, then it isn't really a valid pointed set comonadic operation. This is because we are abusing a list to approximate a (multi)set.
The Pointed-Cycle Comonad
A slight variation on this theme keeps the order of the elements the same in exchange for a more expensive refocusing operation and just rotates them through the focus.
data PointedCycle a = PointedCycle a [a] deriving (Eq, Ord, Show,Read) instance Functor PointedCycle where fmap f (PointedCycle x xs) = PointedCycle (f x) $ fmap f xs instance Copointed PointedCycle where extract (PointedCycle x _) = x instance Comonad PointedCycle where duplicate xxs@(PointedCycle x xs) = PointedCycle xxs . fmap listToCycle . tail $ rotations (x:xs) where rotations :: [a] -> [[a]] rotations xs = init $ zipWith (++) (tails xs) (inits xs) listToCycle (x:xs) = PointedCycle x xs
With that you acknowledge that you really have a pointed cycle and the writer of the comonadic action can safely use the ordering information intrinsic to the list as a natural consequence of having taken the derivative of a cycle.
*PointedSet> duplicate $ PointedCycle 0 [1..3] PointedCycle (PointedCycle 0 [1,2,3]) [ PointedCycle 1 [2,3,0], PointedCycle 2 [3,0,1], PointedCycle 3 [0,1,2] ]
January 12th, 2009 at 4:40 am
Sorry for asking this so late after your writing, but I have been looking at a lot of info that I should have learned in my mid-twenties – plus, I’m slow ;)
Anyway, I was wondering if you know of a class of objects categories that work like monads, but have no “zero” (a la MonadZero in Haskell), and seem to work just fine as comonads – in fact they are comonads?
January 12th, 2009 at 10:04 am
Do you mean that have no return, just bind? Monads without mzero are er.. just monads. ;)
February 22nd, 2012 at 3:02 pm
Anything with mzero (or empty) cannot be a comonad, as far as I can tell; extract and mzero are mutually exclusive.
December 1st, 2015 at 7:05 am
…Recent Blogroll Additions…
[...]The whole look of your web site is fantastic, let well as the content material![...]…
September 23rd, 2022 at 8:25 am
Viagra tablets australia https://500px.com/p/bersavahi/?view=groups...
Kudos! Numerous data!
…
September 24th, 2022 at 1:29 am
cialis canadian pharmacy https://reallygoodemails.com/canadianpharmaceuticalsonlineusa...
Nicely put. Many thanks….
September 24th, 2022 at 5:07 am
Viagra generico https://www.provenexpert.com/canadian-pharmaceuticals-online-usa/...
Helpful forum posts. Cheers!…
September 24th, 2022 at 10:13 am
Viagra for sale https://sanangelolive.com/members/pharmaceuticals...
Nicely put, Regards….
September 26th, 2022 at 9:10 am
Buy generic viagra https://melaninterest.com/user/canadian-pharmaceuticals-online/?view=likes...
Truly quite a lot of useful information….
September 26th, 2022 at 1:12 pm
canadian pharmacys https://haikudeck.com/canadian-pharmaceuticals-online-personal-presentation-827506e003...
You suggested this superbly!…
September 26th, 2022 at 5:21 pm
Viagra coupon https://buyersguide.americanbar.org/profile/420642/0...
Thanks, A good amount of tips.
…
September 27th, 2022 at 1:03 am
Interactions for viagra https://experiment.com/users/canadianpharmacy...
Incredible quite a lot of helpful material!…
September 27th, 2022 at 7:00 am
Viagra pills https://slides.com/canadianpharmaceuticalsonline...
You actually expressed this really well!…
September 27th, 2022 at 10:39 am
Generic for viagra https://challonge.com/esapenti...
You said it nicely.!…
September 27th, 2022 at 3:07 pm
Interactions for viagra https://challonge.com/gotsembpertvil...
Many thanks. I enjoy this!…
September 28th, 2022 at 4:52 am
Viagra tablets https://challonge.com/citlitigolf...
Seriously loads of terrific information….
September 28th, 2022 at 8:25 am
Buy viagra online https://order-stromectol-over-the-counter.estranky.cz/clanky/order-stromectol-over-the-counter.html...
Nicely put. Thank you….
September 28th, 2022 at 2:52 pm
Viagra tablets australia https://soncheebarxu.estranky.cz/clanky/stromectol-for-head-lice.html...
Thanks! Ample stuff.
…
September 29th, 2022 at 4:23 am
Viagra tablets https://lehyriwor.estranky.sk/clanky/stromectol-cream.html...
Really a good deal of wonderful data….
September 29th, 2022 at 8:11 am
Tadalafil https://dsdgbvda.zombeek.cz/...
Thanks a lot. A good amount of info.
…
September 29th, 2022 at 2:37 pm
How does viagra work https://inflavnena.zombeek.cz/...
Whoa tons of superb material….
September 30th, 2022 at 8:53 am
Cheap viagra https://www.myscrsdirectory.com/profile/421708/0...
Nicely put. With thanks!…
September 30th, 2022 at 4:05 pm
Viagra 5 mg funziona https://supplier.ihrsa.org/profile/421717/0...
Terrific stuff. Many thanks!…
October 1st, 2022 at 6:26 am
Buy viagra online https://wefbuyersguide.wef.org/profile/421914/0...
You actually explained this fantastically….
October 1st, 2022 at 10:27 am
northwest pharmacies https://legalmarketplace.alanet.org/profile/421920/0...
Whoa loads of wonderful info….
October 2nd, 2022 at 4:03 am
Viagra canada https://moaamein.nacda.com/profile/422018/0...
Wonderful write ups. Many thanks!…
October 2nd, 2022 at 8:30 am
Viagra cost https://www.audiologysolutionsnetwork.org/profile/422019/0...
With thanks, I appreciate it!…
October 2nd, 2022 at 11:47 am
Viagra for sale https://network.myscrs.org/profile/422020/0...
Many thanks, Numerous write ups!
…
October 3rd, 2022 at 5:33 am
Viagra from canada https://sanangelolive.com/members/canadianpharmaceuticalsonlineusa...
With thanks. I enjoy this!…
October 3rd, 2022 at 8:58 am
Viagra vs viagra https://sanangelolive.com/members/girsagerea...
You definitely made your point!…
October 4th, 2022 at 7:43 am
top rated online canadian pharmacies https://www.ecosia.org/search?q=“My Canadian Pharmacy – Extensive Assortment of Medications – 2022″…
Beneficial material. Many thanks….
October 4th, 2022 at 11:32 am
Viagra 5 mg funziona https://www.mojomarketplace.com/user/Canadianpharmaceuticalsonline-EkugcJDMYH...
Appreciate it, Quite a lot of info.
…
October 4th, 2022 at 3:51 pm
canada drugs https://seedandspark.com/user/canadian-pharmaceuticals-online...
You suggested it really well….
October 5th, 2022 at 8:54 am
Viagra rezeptfrei https://www.giantbomb.com/profile/canadapharmacy/blog/canadian-pharmaceuticals-online/265652/...
Regards! Ample data.
…
October 5th, 2022 at 5:32 pm
Viagra tablets australia https://search.gmx.com/web/result?q=“My Canadian Pharmacy – Extensive Assortment of Medications – 2022″…
Nicely put. Thanks!…
October 6th, 2022 at 2:38 am
Viagra or viagra https://search.seznam.cz/?q=“My Canadian Pharmacy – Extensive Assortment of Medications – 2022″…
With thanks, Numerous material!
…
October 6th, 2022 at 6:19 am
stromectol cream https://sanangelolive.com/members/unsafiri...
Terrific postings. Regards!…
October 6th, 2022 at 10:36 am
canadian drug …
Incredible many of fantastic data….
October 6th, 2022 at 4:33 pm
discount canadian pharmacies https://swisscows.com/en/web?query=“My Canadian Pharmacy – Extensive Assortment of Medications – 2022″…
Truly plenty of great material!…
October 7th, 2022 at 3:52 am
Viagra 20mg https://www.dogpile.com/serp?q=“My Canadian Pharmacy – Extensive Assortment of Medications – 2022″…
Wow loads of amazing data!…
October 7th, 2022 at 11:02 am
Buy viagra online …
Helpful tips. Thank you….
October 8th, 2022 at 7:01 am
canadian drug https://search.givewater.com/serp?q=“My Canadian Pharmacy – Extensive Assortment of Medications – 2022″…
Thank you! Numerous stuff!
…
October 8th, 2022 at 12:30 pm
Viagra canada https://www.bakespace.com/members/profile/Сanadian pharmaceuticals for usa sales/1541108/…
Many thanks! I appreciate this!…
October 9th, 2022 at 10:45 am
Viagra vs viagra vs levitra https://results.excite.com/serp?q=“My Canadian Pharmacy – Extensive Assortment of Medications – 2022″…
You said it nicely…..
October 9th, 2022 at 3:14 pm
canadian medications https://www.infospace.com/serp?q=“My Canadian Pharmacy – Extensive Assortment of Medications – 2022″…
Appreciate it. A lot of data.
…
October 10th, 2022 at 7:31 am
canada drugs online https://headwayapp.co/canadianppharmacy-changelog...
Nicely put, Thanks a lot….
October 11th, 2022 at 5:29 am
Tadalafil tablets https://results.excite.com/serp?q=“My Canadian Pharmacy – Extensive Assortment of Medications – 2022″…
You actually explained it perfectly….
October 11th, 2022 at 10:52 am
canadian rx https://canadianpharmaceuticalsonline.as.me/schedule.php...
Thanks. Loads of data!
…
October 13th, 2022 at 12:48 pm
Viagra online https://feeds.feedburner.com/bing/stromectolnoprescription...
Nicely put. Appreciate it….
October 14th, 2022 at 5:14 am
Viagra pills https://reallygoodemails.com/orderstromectoloverthecounterusa...
You said it perfectly…..