Brittany doesn't generate blank lines for do {stmts;;;stmts} #270

Closed
opened 2019-12-03 02:11:42 +01:00 by bredelings · 8 comments
bredelings commented 2019-12-03 02:11:42 +01:00 (Migrated from github.com)

Hi, I'm generating Haskell code with braces and semi-colons and then using brittany to remove braces and semicolons and use braces instead.

I'm trying to figure out how to generate code so that after brittany processes it there will be spaces between things. I've tried ; ;, ; ;, \n; \n; . In all cases, brittany consumes the extra ; (and the newline) and generates lines with no space between them.

Any idea how I can generate do { stmts; <something>; stmts} and get

  do
    stmts

    stmts

out?

Hi, I'm generating Haskell code with braces and semi-colons and then using brittany to remove braces and semicolons and use braces instead. I'm trying to figure out how to generate code so that after brittany processes it there will be spaces between things. I've tried `; ;`, `; ;`, `\n; \n; `. In all cases, brittany consumes the extra `;` (and the newline) and generates lines with no space between them. Any idea how I can generate `do { stmts; <something>; stmts}` and get ``` do stmts stmts ``` out?
eborden commented 2019-12-03 21:02:16 +01:00 (Migrated from github.com)

I'm very curious why this is your workflow. It seems very non-standard and rather a niche issue.

I'm very curious why this is your workflow. It seems very non-standard and rather a niche issue.
bredelings commented 2019-12-03 21:20:51 +01:00 (Migrated from github.com)

I have a C++ program that translates a simple input language into Haskell and then runs it in a special interpreter that tracks execution traces, in order to implement probabilistic programming.

BTW, my recollection (I think from the Haskell 2010 report) was that the reason Haskell handles layout by translating whitespace to {. ;. and } is precisely so that Haskell can be machine-generated. I'm not sure generation of Haskell code in general is "very non-standard".

I have two questions. First, does the implementation ignore all blank lines inside a { } block? Second, what kind of work would be required to handle blank lines like this?

I have a C++ program that translates a simple input language into Haskell and then runs it in a special interpreter that tracks execution traces, in order to implement probabilistic programming. BTW, my recollection (I think from the Haskell 2010 report) was that the reason Haskell handles layout by translating whitespace to `{`. `;`. and `}` is precisely so that Haskell can be machine-generated. I'm not sure generation of Haskell code in general is "very non-standard". I have two questions. First, does the implementation ignore all blank lines inside a `{` `}` block? Second, what kind of work would be required to handle blank lines like this?
bredelings commented 2019-12-03 21:27:34 +01:00 (Migrated from github.com)

Hmm, it seems that brittany will preserve whitespace before comments. So both the whitespace and the comment are preserved below.

sample_imodel_1 = do {arg_mean_length <- shifted_exponential 10.0 1.0
;arg_log_rate <- Probability.laplace (-4.0) 0.707

-- This is another comment
;return (IModel.rs07 arg_log_rate arg_mean_length, ["rs07:log_rate" %=% arg_log_rate, "rs07:mean_length" %=% arg_mean_length])}
Hmm, it seems that brittany will preserve whitespace before comments. So both the whitespace and the comment are preserved below. ``` sample_imodel_1 = do {arg_mean_length <- shifted_exponential 10.0 1.0 ;arg_log_rate <- Probability.laplace (-4.0) 0.707 -- This is another comment ;return (IModel.rs07 arg_log_rate arg_mean_length, ["rs07:log_rate" %=% arg_log_rate, "rs07:mean_length" %=% arg_mean_length])} ```
eborden commented 2019-12-03 21:40:19 +01:00 (Migrated from github.com)

I'm not sure generation of Haskell code in general is "very non-standard".

Aha! You are right. I had not thought of that use case until you mentioned it.

> I'm not sure generation of Haskell code in general is "very non-standard". Aha! You are right. I had not thought of that use case until you mentioned it.
lspitzner commented 2019-12-04 13:49:17 +01:00 (Migrated from github.com)
do { a; ; ; b ; c }

in the AST looks like

HsDo
  NoExt
  DoExpr
  A Nothing
    [ A Just (Ann (DP (0,1)) [] [] [(AnnSemiSep,DP (0,0)),(AnnSemiSep,DP (0,1)),(AnnSemiSep,DP (0,1))] Nothing Nothing)
        BodyStmt{..}    -- boils down to binding "a"
    , A Just (Ann (DP (0,1)) [] [] [(AnnSemiSep,DP (0,1))] Nothing Nothing)
        BodyStmt{..}    -- boils down to binding "b"
    , A Just (Ann (DP (0,1)) [] [] [] Nothing Nothing)
        BodyStmt{..}    -- boils down to binding "c"
    ]

So this involves somehow turning AnnSemiSeps into newlines. Either by pre-processing on the annotations or by treating it like an "after-element-comment" that consists only of newlines.

~~~~.hs do { a; ; ; b ; c } ~~~~ in the AST looks like ~~~~ HsDo NoExt DoExpr A Nothing [ A Just (Ann (DP (0,1)) [] [] [(AnnSemiSep,DP (0,0)),(AnnSemiSep,DP (0,1)),(AnnSemiSep,DP (0,1))] Nothing Nothing) BodyStmt{..} -- boils down to binding "a" , A Just (Ann (DP (0,1)) [] [] [(AnnSemiSep,DP (0,1))] Nothing Nothing) BodyStmt{..} -- boils down to binding "b" , A Just (Ann (DP (0,1)) [] [] [] Nothing Nothing) BodyStmt{..} -- boils down to binding "c" ] ~~~~ So this involves somehow turning `AnnSemiSep`s into newlines. Either by pre-processing on the annotations or by treating it like an "after-element-comment" that consists only of newlines.
lspitzner commented 2019-12-04 13:58:50 +01:00 (Migrated from github.com)

@bredelings please test if the semicolon-newlines branch works for you (you will need to enable the new config flag in addition to running that branch).

@bredelings please test if the [semicolon-newlines](https://github.com/lspitzner/brittany/tree/semicolon-newlines) branch works for you (you will need to enable the new config flag in addition to running that branch).
bredelings commented 2019-12-04 17:52:15 +01:00 (Migrated from github.com)

Indeed, it works! I added the line lconfig_experimentalSemicolonNewlines: true to the config.

Indeed, it works! I added the line ` lconfig_experimentalSemicolonNewlines: true` to the config.
lspitzner commented 2019-12-09 23:00:05 +01:00 (Migrated from github.com)

implemented in 0.12.1.1

implemented in 0.12.1.1
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: hexagoxel/brittany#270
There is no content yet.