Christopher Howard | 16 Apr 14:35 2013

Monad fold

So, I'm doing something like this

foldl (>>=) someA list :: Monad m => m a

where
  list :: Monad m => [a -> m a],
  someA :: Monad m => m a

Is there a more concise way to write this? I don't think foldM is what I
want -- or is it?

--

-- 
frigidcode.com

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Lyndon Maydwell | 16 Apr 14:47 2013
Picon

Re: Monad fold

You could do:

runKleisli . mconcat . map Kleisli :: Monoid (Kleisli m a b) => [a -> m b] -> a -> m b

Would that work for you?


On Tue, Apr 16, 2013 at 8:35 PM, Christopher Howard <christopher.howard <at> frigidcode.com> wrote:
So, I'm doing something like this

foldl (>>=) someA list :: Monad m => m a

where
  list :: Monad m => [a -> m a],
  someA :: Monad m => m a

Is there a more concise way to write this? I don't think foldM is what I
want -- or is it?

--
frigidcode.com


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Oliver Charles | 16 Apr 14:53 2013
Picon

Re: Monad fold

On 04/16/2013 01:47 PM, Lyndon Maydwell wrote:
> You could do:
>
> runKleisli . mconcat . map Kleisli :: Monoid (Kleisli m a b) => [a -> 
> m b] -> a -> m b
>
> Would that work for you?
I can't find an instance for Monoid (Kleisli m a b) in `base`, so 
presumably the author would also have to write this instance? If so - 
would that really be any different to using that fold?

- Ollie
Tom Ellis | 16 Apr 15:49 2013
Picon

Re: Monad fold

On Tue, Apr 16, 2013 at 01:53:19PM +0100, Oliver Charles wrote:
> On 04/16/2013 01:47 PM, Lyndon Maydwell wrote:
> >You could do:
> >
> >runKleisli . mconcat . map Kleisli :: Monoid (Kleisli m a b) => [a
> >-> m b] -> a -> m b
> >
> >Would that work for you?
> I can't find an instance for Monoid (Kleisli m a b) in `base`, so
> presumably the author would also have to write this instance? If so
> - would that really be any different to using that fold?

It doesn't make sense anyway.  It would have to be "Kleisli m a a" which
would presumably require a newtype.

Tom
Lyndon Maydwell | 16 Apr 15:50 2013
Picon

Re: Monad fold

Yep. I was backstabbed by ghci seemingly having no issue with my definition when I asked for the type.


On Tue, Apr 16, 2013 at 9:49 PM, Tom Ellis <tom-lists-haskell-cafe-2013 <at> jaguarpaw.co.uk> wrote:
On Tue, Apr 16, 2013 at 01:53:19PM +0100, Oliver Charles wrote:
> On 04/16/2013 01:47 PM, Lyndon Maydwell wrote:
> >You could do:
> >
> >runKleisli . mconcat . map Kleisli :: Monoid (Kleisli m a b) => [a
> >-> m b] -> a -> m b
> >
> >Would that work for you?
> I can't find an instance for Monoid (Kleisli m a b) in `base`, so
> presumably the author would also have to write this instance? If so
> - would that really be any different to using that fold?

It doesn't make sense anyway.  It would have to be "Kleisli m a a" which
would presumably require a newtype.

Tom

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Roman Cheplyaka | 16 Apr 15:56 2013

Re: Monad fold

Right. See also this discussion:
http://www.haskell.org/pipermail/libraries/2009-July/012106.html

Roman

* Tom Ellis <tom-lists-haskell-cafe-2013 <at> jaguarpaw.co.uk> [2013-04-16 14:49:48+0100]
> On Tue, Apr 16, 2013 at 01:53:19PM +0100, Oliver Charles wrote:
> > On 04/16/2013 01:47 PM, Lyndon Maydwell wrote:
> > >You could do:
> > >
> > >runKleisli . mconcat . map Kleisli :: Monoid (Kleisli m a b) => [a
> > >-> m b] -> a -> m b
> > >
> > >Would that work for you?
> > I can't find an instance for Monoid (Kleisli m a b) in `base`, so
> > presumably the author would also have to write this instance? If so
> > - would that really be any different to using that fold?
> 
> It doesn't make sense anyway.  It would have to be "Kleisli m a a" which
> would presumably require a newtype.
> 
> Tom
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
Lyndon Maydwell | 16 Apr 14:55 2013
Picon

Re: Monad fold

Wow looks like this Monoid instance isn't included in Control.Monad... My mistake.


On Tue, Apr 16, 2013 at 8:47 PM, Lyndon Maydwell <maydwell <at> gmail.com> wrote:
You could do:

runKleisli . mconcat . map Kleisli :: Monoid (Kleisli m a b) => [a -> m b] -> a -> m b

Would that work for you?


On Tue, Apr 16, 2013 at 8:35 PM, Christopher Howard <christopher.howard <at> frigidcode.com> wrote:
So, I'm doing something like this

foldl (>>=) someA list :: Monad m => m a

where
  list :: Monad m => [a -> m a],
  someA :: Monad m => m a

Is there a more concise way to write this? I don't think foldM is what I
want -- or is it?

--
frigidcode.com


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Roman Cheplyaka | 16 Apr 14:57 2013

Re: Monad fold

* Christopher Howard <christopher.howard <at> frigidcode.com> [2013-04-16 04:35:39-0800]
> So, I'm doing something like this
> 
> foldl (>>=) someA list :: Monad m => m a
> 
> where
>   list :: Monad m => [a -> m a],
>   someA :: Monad m => m a
> 
> Is there a more concise way to write this? I don't think foldM is what I
> want -- or is it?

I don't think it can get any more concise. (No, foldM isn't what you
want.)

But you most probably should prefer the right fold in this case.
(Unless you don't care about efficiency.)

Something like

  someA >>= foldr (>=>) return list

should do.

Roman
Arseniy Alekseyev | 16 Apr 16:04 2013
Picon

Re: Monad fold

Hi!

Although foldM won't make things much nicer, it can be used here as well:

someA >>= \a -> foldM (flip id) a list

Cheers!
Arseniy

On 16 April 2013 13:35, Christopher Howard
<christopher.howard <at> frigidcode.com> wrote:
> So, I'm doing something like this
>
> foldl (>>=) someA list :: Monad m => m a
>
> where
>   list :: Monad m => [a -> m a],
>   someA :: Monad m => m a
>
> Is there a more concise way to write this? I don't think foldM is what I
> want -- or is it?
>
> --
> frigidcode.com
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

Gmane