Changes between Version 3 and Version 4 of DoAndIfThenElse


Ignore:
Timestamp:
Dec 17, 2005 11:28:08 AM (10 years ago)
Author:
ross@…
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DoAndIfThenElse

    v3 v4  
    1 = The `do`-notation and `if`-`then`-`else` =
     1= Conditionals and `do`-notation =
    22[[PageOutline]]
    3 
    4 This page concerns the possibility of tweaking the layout rules to avoid a common pitfall when `if`-`then`-`else` is used with the `do`-notation.
    5 It only documents a problem: no specific solution is proposed. The issue is somewhat related to NondecreasingIndentation.
    6 Strictly speaking, the issue is not tied to the `do`-notation, but that is where it (almost?) always shows up.
    73
    84== Brief Explanation ==
     
    2218put beginners off.
    2319
    24 Might it be possible to handle `then` and `else` specially to avoid this problem? At least in the context of `do`?
    25 Or might it be possible to treat `if` as something that opens a kind of explicit layout context (which would
    26 prevent the insertion of a closing `}`) that is closed by a matching `else`?
     20Strictly speaking, the issue is not tied to the `do`-notation, but that is where it (almost?) always shows up.
    2721
    28 == Implementation ==
     22== Proposal ==
    2923
    30 This has been implemented in jhc by changing a single line in the parser, an equivalant change in the report should have the same effect.
     24Change the syntax for conditionals to
    3125
    32 {{{
    33 old:
    34   | 'if' exp 'then' exp 'else' exp { HsIf $2 $4 $6 }
    35 new:
    36   | 'if' exp optsemi 'then' exp optsemi 'else' exp { HsIf $2 $5 $8 }
    37 }}}
     26   exp   ->      `if` exp,,1,, [`;`] `then` exp,,2,, [`;`] `else` exp,,3,,
    3827
     28i.e., add optional semicolons before `then` and `else`, making the above example legal.
     29This has been recently added to jhc and GHC, and
    3930so far, it has not caused any problems.
    4031
    4132== References ==
     33 * [http://www.haskell.org/onlinereport/exps.html#conditionals Conditionals] in the Haskell 98 Report
    4234 * Somewhat related to NondecreasingIndentation
    4335
    4436== Pros ==
    45  * Would address a layout issue that manifestly trips up a lot of people and which at least one
    46    experienced Haskell programmer (me!) finds very annoying.
     37 * Would address a layout issue that manifestly trips up a lot of people, and many experienced Haskell programmers find very annoying.
     38 * Trivial to implement
     39 * Independent of the layout rule.
    4740
    4841== Cons ==
    49  * No concrete proposal yet
    50  * Not implemented in any system to my knowledge