6.2.16. Do And If Then Else — Glasgow Haskell Compiler 9.14.0.20251028 User's Guide

# 6.2.16. Do And If Then Else

DoAndIfThenElse
  Since:

    7.0.1

  Status:

    Included in `Haskell2010`

  Allow semicolons in `if` expressions.

Normally, a conditional is written like this: `if cond then expr1 else expr2`. With the extension `DoAndIfThenElse`, semicolons are allowed before the `then` and also before the `else`, allowing `if cond; then expr1; else expr2`. (You can also include either semicolon on its own.)

Allowing semicolons in the middle of a conditional is useful in connection with layout-controlled blocks, like `do`-blocks. This is because GHC invisibly inserts a semicolon between each line of a layout-controlled block. Accordingly, with `DoAndIfThenElse`, we can write code like this

    f mb x y = do
      b <- mb
      if b
      then x
      else y

Without `DoAndIfThenElse`, the `then` and `else` lines would have to be indented with respect to the rest of the lines in the `do`-block.