Quantcast

'let' keyword optional in do notation?

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

'let' keyword optional in do notation?

Martijn Schrage-2
Hi cafe,

For a while now, I've been wondering why the 'let' keyword in a do block isn't optional. So instead of

do ...
   let x = exp1
       y = exp2
   z <- exp3
   ...
    
you could simply write
 
do ...
   x = exp1
   y = exp2
   z <- exp3    
   ...

Where each sequence of let-less bindings is put in a separate binding group. I'm no parsing wizard, but I couldn't come up with any situations in which this would cause ambiguity. To me, the let-less version is easier on the eyes, more consistent with <- bindings, and also makes it less of a hassle to move stuff around.

The above probably also holds for list/monad comprehensions, but the explicit let has never really bothered me there.

Cheers,
Martijn Schrage -- Oblomov Systems (http://www.oblomov.com)
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: 'let' keyword optional in do notation?

Vo Minh Thu
2012/8/8 Martijn Schrage <[hidden email]>:

> Hi cafe,
>
> For a while now, I've been wondering why the 'let' keyword in a do block
> isn't optional. So instead of
>
> do ...
>    let x = exp1
>        y = exp2
>    z <- exp3
>    ...
>
> you could simply write
>
> do ...
>    x = exp1
>    y = exp2
>    z <- exp3
>    ...
>
> Where each sequence of let-less bindings is put in a separate binding group.
> I'm no parsing wizard, but I couldn't come up with any situations in which
> this would cause ambiguity. To me, the let-less version is easier on the
> eyes, more consistent with <- bindings, and also makes it less of a hassle
> to move stuff around.
>
> The above probably also holds for list/monad comprehensions, but the
> explicit let has never really bothered me there.

Hi,

This is not a parsing problem, but a scoping one: try to run this program:

main = do
  let x = y
      y = 5
  let a = b
  let b = 6
  print (x, y, a, b)

Cheers,
Thu

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: 'let' keyword optional in do notation?

Ertugrul Söylemez
Vo Minh Thu <[hidden email]> wrote:

> This is not a parsing problem, but a scoping one: try to run this
> program:
>
> main = do
>   let x = y
>       y = 5
>   let a = b
>   let b = 6
>   print (x, y, a, b)
>
> Cheers,
> Thu
Martijn has actually covered this question:

> > Where each sequence of let-less bindings is put in a separate
> > binding group. I'm no parsing wizard, but I couldn't come up with
> > any situations in which this would cause ambiguity. To me, the
> > let-less version is easier on the eyes, more consistent with <-
> > bindings, and also makes it less of a hassle to move stuff around.

The suggestion seems sound to me, and the additional 'let' can really be
annoying in cases where you have a lot of 'let' bindings among very few
monadic actions.  My current way to deal with this is to move the stuff
to separate computations, but it's certainly not a nice solution:

    myComp = c >>= f
        where
        f x = ...
            where
            a = ...
            b = ...


Greets
Ertugrul

--
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.

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

signature.asc (853 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: 'let' keyword optional in do notation?

Martijn Schrage-2
On 08-08-12 17:27, Ertugrul Söylemez wrote:
Vo Minh Thu [hidden email] wrote:

This is not a parsing problem, but a scoping one: try to run this
program:

main = do
  let x = y
      y = 5
  let a = b
  let b = 6
  print (x, y, a, b)

Cheers,
Thu
Martijn has actually covered this question:

Where each sequence of let-less bindings is put in a separate
binding group. I'm no parsing wizard, but I couldn't come up with
any situations in which this would cause ambiguity. To me, the
let-less version is easier on the eyes, more consistent with <-
bindings, and also makes it less of a hassle to move stuff around.

To make it more clear, this is the transformation I propose:

do ...        -- not a let-less binding
   x1 = exp1  -- \
   ..         --  only let-less bindings  
   xn = expn  -- /
   ...        -- not a let-less binding
  
becomes

do ...
   let x1 = exp1
       ..
       xn = expn
   ...
  
So

main = do
  x = y
  y = 5
  a = b
  b = 6
  print (x, y, a, b)


would put everything in the same binding group and compile successfully. To get Thu's example, you would write

main = do
  x = y
  y = 5
  let a = b
  let b = 6
  print (x, y, a, b)

The last let could even be left out.

Cheers, Martijn

The suggestion seems sound to me, and the additional 'let' can really be
annoying in cases where you have a lot of 'let' bindings among very few
monadic actions.  My current way to deal with this is to move the stuff
to separate computations, but it's certainly not a nice solution:

    myComp = c >>= f
        where
        f x = ...
            where
            a = ...
            b = ...


Greets
Ertugrul



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


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Fwd: 'let' keyword optional in do notation?

David Feuer
---------- Forwarded message ----------
From: David Feuer <[hidden email]>
Date: Wed, Aug 8, 2012 at 12:22 PM
Subject: Re: [Haskell-cafe] 'let' keyword optional in do notation?
To: Martijn Schrage <[hidden email]>


Changing scoping rules based on whether things are right next to each
other? No thanks.

On Wed, Aug 8, 2012 at 11:44 AM, Martijn Schrage <[hidden email]> wrote:

> On 08-08-12 17:27, Ertugrul Söylemez wrote:
>
> Vo Minh Thu <[hidden email]> wrote:
>
> This is not a parsing problem, but a scoping one: try to run this
> program:
>
> main = do
>   let x = y
>       y = 5
>   let a = b
>   let b = 6
>   print (x, y, a, b)
>
> Cheers,
> Thu
>
> Martijn has actually covered this question:
>
> Where each sequence of let-less bindings is put in a separate
> binding group. I'm no parsing wizard, but I couldn't come up with
> any situations in which this would cause ambiguity. To me, the
> let-less version is easier on the eyes, more consistent with <-
> bindings, and also makes it less of a hassle to move stuff around.
>
>
> To make it more clear, this is the transformation I propose:
>
> do ...        -- not a let-less binding
>    x1 = exp1  -- \
>    ..         --  only let-less bindings
>    xn = expn  -- /
>    ...        -- not a let-less binding
>
> becomes
>
> do ...
>    let x1 = exp1
>        ..
>        xn = expn
>    ...
>
> So
>
> main = do
>
>   x = y
>   y = 5
>   a = b
>
>   b = 6
>   print (x, y, a, b)
>
>
> would put everything in the same binding group and compile successfully. To
> get Thu's example, you would write
>
> main = do
>
>   x = y
>   y = 5
>   let a = b
>   let b = 6
>   print (x, y, a, b)
>
> The last let could even be left out.
>
> Cheers, Martijn
>
> The suggestion seems sound to me, and the additional 'let' can really be
> annoying in cases where you have a lot of 'let' bindings among very few
> monadic actions.  My current way to deal with this is to move the stuff
> to separate computations, but it's certainly not a nice solution:
>
>     myComp = c >>= f
>         where
>         f x = ...
>             where
>             a = ...
>             b = ...
>
>
> Greets
> Ertugrul
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Fwd: 'let' keyword optional in do notation?

Simon Hengel
On Wed, Aug 08, 2012 at 12:22:39PM -0400, David Feuer wrote:
> Changing scoping rules based on whether things are right next to each
> other? No thanks.

Would expanding each let-less binding to a separate let "feel" more
sound to you?

Cheers,
Simon

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Fwd: 'let' keyword optional in do notation?

Martijn Schrage-2
On 08-08-12 19:01, Simon Hengel wrote:
On Wed, Aug 08, 2012 at 12:22:39PM -0400, David Feuer wrote:
Changing scoping rules based on whether things are right next to each
other? No thanks.
Would expanding each let-less binding to a separate let "feel" more
sound to you?

That was actually my first idea, but then two declarations at the same level will not be in the same binding group, so

do x = y
   y = 1

would not compile. This would create a difference with all the other places where bindings may appear.

However, having scope depend on things being next to each other (or rather, not having anything in between) is not new. Template Haskell declaration splices already cause separate binding groups for top-level declarations. Moreover, the new scope rule only holds for let-less bindings. If you use explicit lets nothing changes.

-- Martijn

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Fwd: 'let' keyword optional in do notation?

David Feuer

Is it really so bad to use an explicit let when you need mutually recursive bindings?

On Aug 8, 2012 1:51 PM, "Martijn Schrage" <[hidden email]> wrote:
On 08-08-12 19:01, Simon Hengel wrote:
On Wed, Aug 08, 2012 at 12:22:39PM -0400, David Feuer wrote:
Changing scoping rules based on whether things are right next to each
other? No thanks.
Would expanding each let-less binding to a separate let "feel" more
sound to you?

That was actually my first idea, but then two declarations at the same level will not be in the same binding group, so

do x = y
   y = 1

would not compile. This would create a difference with all the other places where bindings may appear.

However, having scope depend on things being next to each other (or rather, not having anything in between) is not new. Template Haskell declaration splices already cause separate binding groups for top-level declarations. Moreover, the new scope rule only holds for let-less bindings. If you use explicit lets nothing changes.

-- Martijn

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


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Fwd: 'let' keyword optional in do notation?

Tillmann Rendel-5
In reply to this post by Martijn Schrage-2
Hi,

Martijn Schrage wrote:

>> Would expanding each let-less binding to a separate let "feel" more
>> sound to you?
>>
> That was actually my first idea, but then two declarations at the same
> level will not be in the same binding group, so
>
> do x = y
>    y = 1
>
> would not compile. This would create a difference with all the other
> places where bindings may appear.

But it would be in line with <- bindings in the do notation, so maybe it
wouldn't feel so wrong.

   Tillmann

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Fwd: 'let' keyword optional in do notation?

Ryan Ingram
> But it would be in line with <- bindings in the do notation, so maybe it wouldn't feel so wrong.

I was about to post this exact example.

do
  x <- return 1
  x <- return x
  return x

seems to work just fine (the answer is 1).  I'd even be ok with =-in-do being non-recursive like <-

  -- ryan

On Thu, Aug 9, 2012 at 1:35 AM, Tillmann Rendel <[hidden email]> wrote:
Hi,


Martijn Schrage wrote:
Would expanding each let-less binding to a separate let "feel" more
sound to you?

That was actually my first idea, but then two declarations at the same
level will not be in the same binding group, so

do x = y
   y = 1

would not compile. This would create a difference with all the other
places where bindings may appear.

But it would be in line with <- bindings in the do notation, so maybe it wouldn't feel so wrong.

  Tillmann


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


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Fwd: 'let' keyword optional in do notation?

Martijn Schrage-2
In reply to this post by Tillmann Rendel-5
On 09-08-12 10:35, Tillmann Rendel wrote:

> Hi,
>
> Martijn Schrage wrote:
>>> Would expanding each let-less binding to a separate let "feel" more
>>> sound to you?
>>>
>> That was actually my first idea, but then two declarations at the same
>> level will not be in the same binding group, so
>>
>> do x = y
>>    y = 1
>>
>> would not compile. This would create a difference with all the other
>> places where bindings may appear.
>
> But it would be in line with <- bindings in the do notation, so maybe
> it wouldn't feel so wrong.
It would absolutely be the easiest solution to implement, since as far
as I can see, it requires only a small change to the parser. However, I
still think it will be too confusing to have bindings that look the same
as everywhere else but have different binding group rules. Especially
since there is no reason for it from a semantic point of view (unlike
when you mix in a monadic <- binding, after which it makes sense to have
a new binding group.)

Anyhow, I'll submit it as a GHC feature request and see what happens.

Cheers,
Martijn Schrage -- Oblomov Systems (http://www.oblomov.com)
>   Tillmann
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe


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