Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let/if indentation in do blocks #1120

Closed
1 task done
digama0 opened this issue Apr 24, 2022 · 3 comments
Closed
1 task done

let/if indentation in do blocks #1120

digama0 opened this issue Apr 24, 2022 · 3 comments
Labels
enhancement New feature or request low priority

Comments

@digama0
Copy link
Collaborator

digama0 commented Apr 24, 2022

Prerequisites

  • Put an X between the brackets on this line if you have done all of the following:
    • Checked that your issue isn't already filed.
    • Reduced the issue to a self-contained, reproducible test case.

Description

The parser does not accept:

example : Id Nat := do
  let x ← if true then
    pure 1
  else
    pure 2
  pure x

It requires an extra level of indentation:

example : Id Nat := do
  let x ←
    if true then
      pure 1
    else
      pure 2
  pure x

Can we do something to the withPosition stuff for if to support this case? This is tricky because one-sided if is legal in that position (which is part of a do notation, even though the possible productions are limited). My suggested interpretation would be for the if to expect the else to be aligned with the let, if both are on the same line. (Banning one-sided if there is also reasonable since the let x <- part is guaranteed superfluous in that case seeing as it would bind an element of Unit.)

Note that there is no issue with this indentation pattern with let x := if ... else because in this case the if ... else is parsed as an expression and not a do seq item so the else is mandatory and does not need to be aligned in any particular way.

Versions

Lean (version 4.0.0-nightly-2022-04-16, commit 726b735c6df7, Release)

@leodemoura
Copy link
Member

Yeah, this issue is quite annoying. I agree it is natural to try to write

  let x ← if true then
    pure 1
  else
    pure 2

I frequently hit this problem too. This issue is a low priority, but we will try to figure out something.

@Kha
Copy link
Member

Kha commented May 18, 2022

I would suggest a new parser combinator withPositionAfterLinebreak that applies withPosition only if checkLinebreakBefore succeeded. That would allow if to fall back to the let's position if they are on the same line. I can implement it if that sounds right.

@leodemoura
Copy link
Member

@Kha Thanks for the suggestion. I think it should work.

@leodemoura leodemoura added enhancement New feature or request low priority labels Jun 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request low priority
Projects
None yet
Development

No branches or pull requests

3 participants