Proposal: Add First and Last wrappers around Maybe to Data.Monoid

I've created http://hackage.haskell.org/trac/ghc/ticket/1189 to suggest adding two Monoid instances around Maybe that, instead of accumulating things like most monoids, just pick the first or last piece of data they see. Would a week (March 12) be enough time to discuss this? Thanks, Jeffrey Yasskin

Jeffrey Yasskin writes:
I've created http://hackage.haskell.org/trac/ghc/ticket/1189 to suggest adding two Monoid instances around Maybe that, instead of accumulating things like most monoids, just pick the first or last piece of data they see.
Rather than have two wrappers, I say make the a Monoid instance for
Maybe using a left-biased choice (like the MonadPlus instance).
Then we would get right-biased choice for free with the Dual wrapper.
--
David Menendez

Jeffrey, would it be difficult to add an example or two for the intended usage? Thanks, Andriy ____________________________________________________________________________________ 8:00? 8:25? 8:40? Find a flick in no time with the Yahoo! Search movie showtime shortcut. http://tools.search.yahoo.com/shortcuts/#news

On 3/5/07, Andriy Palamarchuk
Jeffrey, would it be difficult to add an example or two for the intended usage?
Not at all. Two reasonable examples are:
findLast :: Foldable t => (a -> Bool) -> t a -> Maybe a findLast pred = getLast . foldMap (\x -> if pred x then Last (Just x) else Last Nothing)
And, assuming Data.Map were extended with alterA :: (Applicative f, Ord k) => (Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)
instance Monoid a => Applicative ((,) a) -- from Control.Applicative
insertLookupWithKey :: Ord k => (k -> v -> v -> v) -> k -> v -> Map k v -> (Maybe v, Map k v) insertLookupWithKey combine key value = Arrow.first getFirst . alterA doChange key where doChange Nothing = (First Nothing, Just value) doChange (Just oldValue) = (First (Just oldValue), Just (combine key value oldValue))
I haven't run these yet, so they may need some tweaking. Since the rest of Data.Monoid doesn't have example uses, where do you want me to put these? Jeffrey

--- Jeffrey Yasskin
Since the rest of Data.Monoid doesn't have example uses, where do you want me to put these?
I include shorter examples right in the function descriptions (see http://darcs.haskell.org/libraries/base/GHC/List.lhs), or create a separate section for longer examples (see http://darcs.haskell.org/libraries/mtl/Control/Monad/Error.hs) Andriy ____________________________________________________________________________________ 8:00? 8:25? 8:40? Find a flick in no time with the Yahoo! Search movie showtime shortcut. http://tools.search.yahoo.com/shortcuts/#news

Would it be possible to move these wrappers and other data types out of Data.Monoid and into something like Data.Monoid.(Wrappers|Util|Combinators) or something like that? A huge amount of useful namespace is eaten by them and Data.Monoid is an extremely common thing to need to import. I mean Dual,Endo,All,Any,Sum,Product are definitely useful, but way to common and generically useful names to export from such a often imported package. John -- John Meacham - ⑆repetae.net⑆john⑈

John Meacham wrote:
A huge amount of useful namespace is eaten by them and Data.Monoid is an extremely common thing to need to import.
(as I say every half year or so) the real problem is that Haskell "import" is (speaking Ada) "with" and "use" at the same time, and there is no local "use" in Haskell. Use whatever names are appropriate in the library, and don't care too much for complaints from people that have acquired the ugly habit of importing everything unqualified :-) -- -- Johannes Waldmann -- Tel/Fax (0341) 3076 6479/80 -- ---- http://www.imn.htwk-leipzig.de/~waldmann/ -------

I've attached a second version of the patch to the ticket
(http://hackage.haskell.org/trac/ghc/ticket/1189) along with the HTML
Haddock generates from it. I added the two examples and the "imposed
identity" instance directly on Maybe. Would one of its fans like to
provide an example of using the Maybe monoid?
On 3/7/07, John Meacham
Would it be possible to move these wrappers and other data types out of Data.Monoid and into something like Data.Monoid.(Wrappers|Util|Combinators) or something like that?
A huge amount of useful namespace is eaten by them and Data.Monoid is an extremely common thing to need to import.
I mean Dual,Endo,All,Any,Sum,Product are definitely useful, but way to common and generically useful names to export from such a often imported package.
Arguably, the monoid wrappers around a particular type should go with that type instead of in Data.Monoid. That would mean my change would be to Data.Maybe instead of here, and All, Any, Sum, and Product would go in the Data.Bool and the Prelude (or Data.Num?). I don't care a whole lot, but unless there's a consensus to move things around, I think my change should just follow the status quo. Thanks, Jeffrey

Today is the scheduled end for discussion on this proposal. Are there
any final objections, suggestions, or comments before I update the
ticket and ask someone to commit it?
On 3/8/07, Jeffrey Yasskin
I've attached a second version of the patch to the ticket (http://hackage.haskell.org/trac/ghc/ticket/1189) along with the HTML Haddock generates from it. I added the two examples and the "imposed identity" instance directly on Maybe. Would one of its fans like to provide an example of using the Maybe monoid?

I'd like to note that the behavior for this Maybe instance is quite
different from what you get with MonadPlus. This seems inconsistent
with respect to the instances of MonadPlus and Monoid for lists.
I realize I'm objecting in the 11th hour, but I think this requires
some consideration.
Cheers,
Spencer Janssen
On Mon, 12 Mar 2007 08:10:24 -0800
"Jeffrey Yasskin"
Today is the scheduled end for discussion on this proposal. Are there any final objections, suggestions, or comments before I update the ticket and ask someone to commit it?
On 3/8/07, Jeffrey Yasskin
wrote: I've attached a second version of the patch to the ticket (http://hackage.haskell.org/trac/ghc/ticket/1189) along with the HTML Haddock generates from it. I added the two examples and the "imposed identity" instance directly on Maybe. Would one of its fans like to provide an example of using the Maybe monoid?
Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries

A MonadPlus instance and a Monoid instance will be used in different
circumstances, so I don't see a compelling argument that they should
always be the same if both exist for a particular type. Maybe you have
one? Note that [] and the nearly identical Seq are the only types so
far to have both instances, so there's no particular reason to think
we can generalize from them.
MonadPlus Maybe and Monoid First are the same though.
If people want, and can provide a name for the type, I'm willing to
wrap the "inject identity" instance in the same way First and Last are
wrapped. But if nobody besides you speaks up in the next day or two,
I'll assume it's not a widespread complaint and go with what I have.
Thanks for the comment though,
Jeffrey
On 3/12/07, Spencer Janssen
I'd like to note that the behavior for this Maybe instance is quite different from what you get with MonadPlus. This seems inconsistent with respect to the instances of MonadPlus and Monoid for lists.
I realize I'm objecting in the 11th hour, but I think this requires some consideration.
Cheers, Spencer Janssen
On Mon, 12 Mar 2007 08:10:24 -0800 "Jeffrey Yasskin"
wrote: Today is the scheduled end for discussion on this proposal. Are there any final objections, suggestions, or comments before I update the ticket and ask someone to commit it?
On 3/8/07, Jeffrey Yasskin
wrote: I've attached a second version of the patch to the ticket (http://hackage.haskell.org/trac/ghc/ticket/1189) along with the HTML Haddock generates from it. I added the two examples and the "imposed identity" instance directly on Maybe. Would one of its fans like to provide an example of using the Maybe monoid?

I've updated the ticket (http://hackage.haskell.org/trac/ghc/ticket/1189) according to the instructions for library submissions. I think the general opinion was neutral to favorable, so I'd like to find someone to commit the patch. Ross, do you have commit privileges? Thanks, Jeffrey

On Thu, Mar 15, 2007 at 10:01:06AM -0700, Jeffrey Yasskin wrote:
I've updated the ticket (http://hackage.haskell.org/trac/ghc/ticket/1189) according to the instructions for library submissions. I think the general opinion was neutral to favorable, so I'd like to find someone to commit the patch.
I've committed it. Thanks! Ian
participants (7)
-
Andriy Palamarchuk
-
David Menendez
-
Ian Lynagh
-
Jeffrey Yasskin
-
Johannes Waldmann
-
John Meacham
-
Spencer Janssen