
I stumbled across this monadic combinator: mcombine :: Monad m => (a -> a -> a) -> m a -> m a -> m a mcombine f mx my = do x <- mx y <- my return (f x y) I used it to chain the outputs of two Parsec String parsers together using this operator: (<++>) :: Monad m => m String -> m String -> m String (<++>) = mcombine (++) mcombine seems like such a basic operation that it should be a library function, but I couldn't find one matching it on hoogle. Is there one? - Mike

On Sun, May 30, 2010 at 11:35 AM, Michael Vanier
I stumbled across this monadic combinator:
mcombine :: Monad m => (a -> a -> a) -> m a -> m a -> m a mcombine f mx my = do x <- mx y <- my return (f x y)
I used it to chain the outputs of two Parsec String parsers together using this operator:
(<++>) :: Monad m => m String -> m String -> m String (<++>) = mcombine (++)
mcombine seems like such a basic operation that it should be a library function, but I couldn't find one matching it on hoogle. Is there one?
- Mike
liftM2: http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/Control-Mo...
file:///usr/share/doc/ghc6-doc/html/libraries/base-4.2.0.0/Control-Monad.htm...Strangely, Hayoo didn't turn this one up... anyone know why? Michael

On 5/30/10 1:40 AM, Michael Snoyman wrote:
On Sun, May 30, 2010 at 11:35 AM, Michael Vanier
mailto:mvanier42@gmail.com> wrote: I stumbled across this monadic combinator:
mcombine :: Monad m => (a -> a -> a) -> m a -> m a -> m a mcombine f mx my = do x <- mx y <- my return (f x y)
I used it to chain the outputs of two Parsec String parsers together using this operator:
(<++>) :: Monad m => m String -> m String -> m String (<++>) = mcombine (++)
mcombine seems like such a basic operation that it should be a library function, but I couldn't find one matching it on hoogle. Is there one?
- Mike
liftM2: http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/Control-Mo...
Strangely, Hayoo didn't turn this one up... anyone know why?
Michael
Ah, cool. Thanks! - Mike

begin Michael Snoyman quotation:
http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/Control-Mo...
file:///usr/share/doc/ghc6-doc/html/libraries/base-4.2.0.0/Control-Monad.htm...Strangely, Hayoo didn't turn this one up... anyone know why?
Hoogle finds it. I didn't think Hayoo was expected to do this sort of abstract type signature search: http://haskell.org/hoogle/?hoogle=Monad+m+%3D%3E+%28a+-%3E+a+-%3E+a%29+-%3E+... It comes up as the second hit on that search or the first hit on this one: http://haskell.org/hoogle/?hoogle=Monad+m+%3D%3E+%28a+-%3E+b+-%3E+c%29+-%3E+... That second search also shows zipWith in there; I never really thought about zipWith being like liftM2 for the list Monad. I don't believe that's actually true for the normal list Monad, but it should be true of an alternate list Monad along the lines of the Functor and Applicative instances for the ZipList newtype in Control.Applicative. -md

On Sun, May 30, 2010 at 8:15 PM, Mike Dillon
That second search also shows zipWith in there; I never really thought about zipWith being like liftM2 for the list Monad. I don't believe that's actually true for the normal list Monad, but it should be true of an alternate list Monad along the lines of the Functor and Applicative instances for the ZipList newtype in Control.Applicative.
ZipList does not form a monad. --Max

On Sun, May 30, 2010 at 11:15:40AM -0700, Mike Dillon wrote:
begin Michael Snoyman quotation:
http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/Control-Mo...
file:///usr/share/doc/ghc6-doc/html/libraries/base-4.2.0.0/Control-Monad.htm...Strangely, Hayoo didn't turn this one up... anyone know why?
Hoogle finds it. I didn't think Hayoo was expected to do this sort of abstract type signature search:
http://haskell.org/hoogle/?hoogle=Monad+m+%3D%3E+%28a+-%3E+a+-%3E+a%29+-%3E+...
It comes up as the second hit on that search or the first hit on this one:
http://haskell.org/hoogle/?hoogle=Monad+m+%3D%3E+%28a+-%3E+b+-%3E+c%29+-%3E+...
That second search also shows zipWith in there; I never really thought about zipWith being like liftM2 for the list Monad. I don't believe that's actually true for the normal list Monad, but it should be true of an alternate list Monad along the lines of the Functor and Applicative instances for the ZipList newtype in Control.Applicative.
As Max noted, ZipList is not a monad. However, you have the right idea: zipWith is exactly liftA2 (the equivalent of liftM2 for Applicatives) for ZipList. -Brent

begin Brent Yorgey quotation:
On Sun, May 30, 2010 at 11:15:40AM -0700, Mike Dillon wrote:
begin Michael Snoyman quotation:
http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/Control-Mo...
file:///usr/share/doc/ghc6-doc/html/libraries/base-4.2.0.0/Control-Monad.htm...Strangely, Hayoo didn't turn this one up... anyone know why?
Hoogle finds it. I didn't think Hayoo was expected to do this sort of abstract type signature search:
http://haskell.org/hoogle/?hoogle=Monad+m+%3D%3E+%28a+-%3E+a+-%3E+a%29+-%3E+...
It comes up as the second hit on that search or the first hit on this one:
http://haskell.org/hoogle/?hoogle=Monad+m+%3D%3E+%28a+-%3E+b+-%3E+c%29+-%3E+...
That second search also shows zipWith in there; I never really thought about zipWith being like liftM2 for the list Monad. I don't believe that's actually true for the normal list Monad, but it should be true of an alternate list Monad along the lines of the Functor and Applicative instances for the ZipList newtype in Control.Applicative.
As Max noted, ZipList is not a monad. However, you have the right idea: zipWith is exactly liftA2 (the equivalent of liftM2 for Applicatives) for ZipList.
Thanks to both you and Max. After you guys responded, I went back and found an interesting haskell-cafe discussion about how ZipList cannot be made into a Monad from August 2009. I should have realized that there was a reason there is no Monad instance defined along with ZipList. -md

On Sun, May 30, 2010 at 1:35 AM, Michael Vanier
I stumbled across this monadic combinator:
mcombine :: Monad m => (a -> a -> a) -> m a -> m a -> m a mcombine f mx my = do x <- mx y <- my return (f x y)
I used it to chain the outputs of two Parsec String parsers together using this operator:
(<++>) :: Monad m => m String -> m String -> m String (<++>) = mcombine (++)
mcombine seems like such a basic operation that it should be a library function, but I couldn't find one matching it on hoogle. Is there one?
It's a lift of sorts. The tip off is the type signature. Your type is equivalent to this type: Monad m => (a -> a -> a) -> (m a -> m a -> m a) Hopefully the parens added for the grouping helps make the intuition clear. Your function takes a function and makes a new one that expects monadic parameters. Look at liftM2 in Control.Monad. More generally there is a formula here: liftM<N> f x1 .. x<N> = f` `liftM` x1 `ap` ... `ap` x<N> If you have an applicative this could also be: f <$> m1 <*> m2 HTH, Jason

It might be useful to relax the type of the function to
Monad m => (a -> b -> c) -> m a -> m b -> m c
/Jonas
On 30 May 2010 10:35, Michael Vanier
I stumbled across this monadic combinator:
mcombine :: Monad m => (a -> a -> a) -> m a -> m a -> m a mcombine f mx my = do x <- mx y <- my return (f x y)
I used it to chain the outputs of two Parsec String parsers together using this operator:
(<++>) :: Monad m => m String -> m String -> m String (<++>) = mcombine (++)
mcombine seems like such a basic operation that it should be a library function, but I couldn't find one matching it on hoogle. Is there one?
- Mike
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (7)
-
Brent Yorgey
-
Jason Dagit
-
Jonas Almström Duregård
-
Max Rabkin
-
Michael Snoyman
-
Michael Vanier
-
Mike Dillon