How do `do` and `where` mix?

Viewed 137

From some book I have the following code snippets

mutableUpdateIO :: Int -> IO (MV.MVector RealWorld Int)
mutableUpdateIO n = do
  mvec <- GM.new (n + 1)
  go n mvec
  where
    go 0 v = return v
    go n v = (MV.write v n 0) >> go (n - 1) v

mutableUpdateST :: Int -> V.Vector Int
mutableUpdateST n =
  runST $ do
    mvec <- GM.new (n + 1)
    go n mvec
  where
    go 0 v = V.freeze v
    go n v = (MV.write v n 0) >> go (n - 1) v

like hindent indents them. Now I want to introduce all braces and semicolons, so the whitespace isn't relevant any more. Just because I am curious.

The second example suggests, that the where belongs to the whole runST $ do ... expression, but the first example suggests, that the where is somehow a part of the go n mvec statement. Reading in Haskell Report Chapter 2.7 I tried to introduce braces and semicolons in the first example like

mutableUpdateIO :: Int -> IO (MV.MVector RealWorld Int)
mutableUpdateIO n = do {
  mvec <- GM.new (n + 1);
  go n mvec;
  where {
    go 0 v = return v;
    go n v = (MV.write v n 0) >> go (n - 1) v;
  } ; }

But I get a parsing error. Why is that?

Why is the layout hindent produces for the first example mutableUpdateIO valid Haskell? Shouldn't the braces and semicolons be introduced like in my above try?

1 Answers
Related