Oren Ben-Kiki | 24 Aug 11:12 2012

Signature of Control.Monad when and unless

I understand why the normal signature is:

    when :: Monad m => Bool -> m () -> m ()
    unless :: Monad m => Bool -> m () -> m ()

But why isn't there also:

    when_ :: Monad m => Bool -> m a -> m ()
    unless_ :: Monad m => Bool -> m a -> m ()

That is, I agree that results shouldn't be discarded "by default", but it should be easy to get rid of them if they are unneeded.

Thanks,

    Oren Ben-Kiki

_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
Stephen Tetley | 24 Aug 14:24 2012
Picon

Re: Signature of Control.Monad when and unless

Hasn't `void` recently been added to Base so that you can discard the
result of any monadic action?

> when True (void actionWithAnswer)

On 24 August 2012 10:12, Oren Ben-Kiki <haskell-oren <at> ben-kiki.org> wrote:
[CUT]

>
> That is, I agree that results shouldn't be discarded "by default", but it
> should be easy to get rid of them if they are unneeded.
Ivan Lazar Miljenovic | 24 Aug 14:48 2012
Picon

Re: Signature of Control.Monad when and unless

On 24 August 2012 22:24, Stephen Tetley <stephen.tetley <at> gmail.com> wrote:
> Hasn't `void` recently been added to Base so that you can discard the
> result of any monadic action?
>
>> when True (void actionWithAnswer)

I also recall a similar discussion within the past six months on this
topic, though I'm having difficulty finding it either online or in my
emails :/

I believe the consensus of the previous discussion was that having
functions such as your stated when_ and unless_ are dangerous in the
general case due to the discarded results being implicitly ignored
(same as how GHC nowadays throws warnings when statements in a
do-block aren't of type m () ).

>
>
> On 24 August 2012 10:12, Oren Ben-Kiki <haskell-oren <at> ben-kiki.org> wrote:
> [CUT]
>
>>
>> That is, I agree that results shouldn't be discarded "by default", but it
>> should be easy to get rid of them if they are unneeded.
>
> _______________________________________________
> Libraries mailing list
> Libraries <at> haskell.org
> http://www.haskell.org/mailman/listinfo/libraries

--

-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic <at> gmail.com
http://IvanMiljenovic.wordpress.com
Sean Leather | 24 Aug 14:54 2012
Picon

Re: Signature of Control.Monad when and unless

On Fri, Aug 24, 2012 at 2:48 PM, Ivan Lazar Miljenovic wrote:

On 24 August 2012 22:24, Stephen Tetley wrote:
> Hasn't `void` recently been added to Base so that you can discard the
> result of any monadic action?
>
>> when True (void actionWithAnswer)

I also recall a similar discussion within the past six months on this
topic, though I'm having difficulty finding it either online or in my
emails :/

It started in April. The thread is split between haskell <at> and libraries <at> :


Regards,
Sean
_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
Edward Kmett | 24 Aug 15:22 2012
Picon

Re: Signature of Control.Monad when and unless

Personally, I would really like to see these added. 


Alas, I missed the discussion the first time around, and I'm afraid if I tried to raise the issue again the safety police will go through and actively remove the 'a's from the remaining useful combinators I still have. ;)

I do find it particularly obnoxious that I have to round trip through the monad transformer stack just to rip off a type argument when couldn't use if it wanted to in the first place.

Moreover, the 'void' type that actually wound up implemented doesn't help at all if you only know you have a Monad and don't have the additional Functor, (e.g. are writing a monad transformer), so even though void exists you wind up having to use  'do x <- ...; return ()' in most library code anyways.

-Edward

On Fri, Aug 24, 2012 at 5:12 AM, Oren Ben-Kiki <haskell-oren <at> ben-kiki.org> wrote:
I understand why the normal signature is:

    when :: Monad m => Bool -> m () -> m ()
    unless :: Monad m => Bool -> m () -> m ()

But why isn't there also:

    when_ :: Monad m => Bool -> m a -> m ()
    unless_ :: Monad m => Bool -> m a -> m ()

That is, I agree that results shouldn't be discarded "by default", but it should be easy to get rid of them if they are unneeded.

Thanks,

    Oren Ben-Kiki


_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries


_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
Andreas Abel | 26 Aug 11:50 2012
Picon

Re: Signature of Control.Monad when and unless

On 24.08.12 3:22 PM, Edward Kmett wrote:
> Personally, I would really like to see these added.

+1

> Alas, I missed the discussion the first time around, and I'm afraid if I
> tried to raise the issue again the safety police will go through and
> actively remove the 'a's from the remaining useful combinators I still
> have. ;)

In the mentioned discussion, I proposed to generalize the types of when 
and unless to

          when   :: Monad m => Bool -> m a -> m ()
          unless :: Monad m => Bool -> m a -> m ()

which amounts to implement e.g. when by

   when True  m = m >> return ()
   when False m = return ()

My proposal triggered an outcry by the `safety community' and I dropped it.

It seems that adding when_ and unless_ seems to be a compromise everyone 
can live with.

Cheers,
Andreas

> I do find it particularly obnoxious that I have to round trip through
> the monad transformer stack just to rip off a type argument when
> couldn't use if it wanted to in the first place.
>
> Moreover, the 'void' type that actually wound up implemented doesn't
> help at all if you only know you have a Monad and don't have the
> additional Functor, (e.g. are writing a monad transformer), so even
> though void exists you wind up having to use  'do x <- ...; return
> ()' in most library code anyways.
>
> -Edward
>
> On Fri, Aug 24, 2012 at 5:12 AM, Oren Ben-Kiki
> <haskell-oren <at> ben-kiki.org <mailto:haskell-oren <at> ben-kiki.org>> wrote:
>
>     I understand why the normal signature is:
>
>          when :: Monad m => Bool -> m () -> m ()
>          unless :: Monad m => Bool -> m () -> m ()
>
>     But why isn't there also:
>
>          when_ :: Monad m => Bool -> m a -> m ()
>          unless_ :: Monad m => Bool -> m a -> m ()
>
>     That is, I agree that results shouldn't be discarded "by default",
>     but it should be easy to get rid of them if they are unneeded.
>
>     Thanks,
>
>          Oren Ben-Kiki

--

-- 
Andreas Abel  <><      Du bist der geliebte Mensch.

Theoretical Computer Science, University of Munich
Oettingenstr. 67, D-80538 Munich, GERMANY

andreas.abel <at> ifi.lmu.de
http://www2.tcs.ifi.lmu.de/~abel/

Gmane