Types of when and unless in Control.Monad

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Types of when and unless in Control.Monad

Andreas Abel
In Control.Monad, when has type

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

I think this type should be generalized to

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

to avoid silly "return ()" statements like in

   when cond $ do
     monadicComputationWhoseResultIWantToDiscard
     return ()

Cheers,
Andreas

P.S.:  A more systematic solution would be to change the Haskell
language by either introducing a Top type which is the supertype of
everything and use it instead of ().

Alternatively, make () the top type and interpret matching against the
empty tuple as just computing the weak head normal form and then
discarding the result.  The latter is needed to preserve the current
behavior of

   (\ () -> bla) (error "raised")

vs.

   (\ _ -> bla) (error "not raised")


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

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

[hidden email]
http://www2.tcs.ifi.lmu.de/~abel/

_______________________________________________
Haskell mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell
Reply | Threaded
Open this post in threaded view
|

Re: Types of when and unless in Control.Monad

John Meacham
Yes, this has always bothered me too.

   John

On Sat, Apr 21, 2012 at 2:51 AM, Andreas Abel <[hidden email]> wrote:

> In Control.Monad, when has type
>
>  when :: Monad m => Bool -> m () -> m ()
>
> I think this type should be generalized to
>
>  when :: Monad m => Bool -> m a -> m ()
>
> to avoid silly "return ()" statements like in
>
>  when cond $ do
>    monadicComputationWhoseResultIWantToDiscard
>    return ()
>
> Cheers,
> Andreas
>
> P.S.:  A more systematic solution would be to change the Haskell language by
> either introducing a Top type which is the supertype of everything and use
> it instead of ().
>
> Alternatively, make () the top type and interpret matching against the empty
> tuple as just computing the weak head normal form and then discarding the
> result.  The latter is needed to preserve the current behavior of
>
>  (\ () -> bla) (error "raised")
>
> vs.
>
>  (\ _ -> bla) (error "not raised")
>
>
> --
> Andreas Abel  <><      Du bist der geliebte Mensch.
>
> Theoretical Computer Science, University of Munich
> Oettingenstr. 67, D-80538 Munich, GERMANY
>
> [hidden email]
> http://www2.tcs.ifi.lmu.de/~abel/
>
> _______________________________________________
> Haskell mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell

_______________________________________________
Haskell mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell
Reply | Threaded
Open this post in threaded view
|

Re: Types of when and unless in Control.Monad

M Farkas-Dyck
In reply to this post by Andreas Abel
On 21/04/2012, Andreas Abel <[hidden email]> wrote:
> to avoid silly "return ()" statements like in
>
>    when cond $ do
>      monadicComputationWhoseResultIWantToDiscard
>      return ()

(when cond ∘ void) monadicComputationWhoseResultIWantToDiscard
or
when cond $ () <$ monadicComputationWhoseResultIWantToDiscard

_______________________________________________
Haskell mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell
Reply | Threaded
Open this post in threaded view
|

Re: Types of when and unless in Control.Monad

Julian Gilbey
On Sat, Apr 21, 2012 at 08:28:27PM -0500, Strake wrote:

> On 21/04/2012, Andreas Abel <[hidden email]> wrote:
> > to avoid silly "return ()" statements like in
> >
> >    when cond $ do
> >      monadicComputationWhoseResultIWantToDiscard
> >      return ()
>
> (when cond ∘ void) monadicComputationWhoseResultIWantToDiscard
> or
> when cond $ () <$ monadicComputationWhoseResultIWantToDiscard

How is that simpler than

  when cond monadicComputationWhoseResultIWantToDiscard

which it would be with the suggested new type?

   Julian

_______________________________________________
Haskell mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell
Reply | Threaded
Open this post in threaded view
|

Re: Types of when and unless in Control.Monad

Christian Höner zu Siederdissen-2
* Julian Gilbey <[hidden email]> [22.04.2012 09:22]:

> On Sat, Apr 21, 2012 at 08:28:27PM -0500, Strake wrote:
> > On 21/04/2012, Andreas Abel <[hidden email]> wrote:
> > > to avoid silly "return ()" statements like in
> > >
> > >    when cond $ do
> > >      monadicComputationWhoseResultIWantToDiscard
> > >      return ()
> >
> > (when cond ∘ void) monadicComputationWhoseResultIWantToDiscard
> > or
> > when cond $ () <$ monadicComputationWhoseResultIWantToDiscard
>
> How is that simpler than
>
>   when cond monadicComputationWhoseResultIWantToDiscard
>
> which it would be with the suggested new type?
>
>    Julian
Wouldn't "when_" and "unless_" or similar be better? I'd probably like
to have the compiler annoy me, since it is not clear that I want to
discard the result. If I really want to discard, I should have to make
it clear as there is probably a good reason for the inner function to
return a result in the first place?

Gruss,
Christian

_______________________________________________
Haskell mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell

attachment0 (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Types of when and unless in Control.Monad

Ivan Lazar Miljenovic
On 22 April 2012 21:39, Christian Höner zu Siederdissen
<[hidden email]> wrote:

> * Julian Gilbey <[hidden email]> [22.04.2012 09:22]:
>> On Sat, Apr 21, 2012 at 08:28:27PM -0500, Strake wrote:
>> > On 21/04/2012, Andreas Abel <[hidden email]> wrote:
>> > > to avoid silly "return ()" statements like in
>> > >
>> > >    when cond $ do
>> > >      monadicComputationWhoseResultIWantToDiscard
>> > >      return ()
>> >
>> > (when cond ∘ void) monadicComputationWhoseResultIWantToDiscard
>> > or
>> > when cond $ () <$ monadicComputationWhoseResultIWantToDiscard
>>
>> How is that simpler than
>>
>>   when cond monadicComputationWhoseResultIWantToDiscard
>>
>> which it would be with the suggested new type?
>>
>>    Julian
>
> Wouldn't "when_" and "unless_" or similar be better? I'd probably like
> to have the compiler annoy me, since it is not clear that I want to
> discard the result. If I really want to discard, I should have to make
> it clear as there is probably a good reason for the inner function to
> return a result in the first place?

Agreed; I'm not sure if I agree with having such functionality
(Henning makes some good points), but if people deem it desirable then
I think it would be better to have them with new names for the reasons
you state.

--
Ivan Lazar Miljenovic
[hidden email]
http://IvanMiljenovic.wordpress.com

_______________________________________________
Haskell mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell