haskell - How do `do` and `where` mix? -
from book have following code snippets
mutableupdateio :: int -> io (mv.mvector realworld int) mutableupdateio n = mvec <- gm.new (n + 1) go n mvec 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 $ mvec <- gm.new (n + 1) go n mvec go 0 v = v.freeze v go n v = (mv.write v n 0) >> go (n - 1) v like hindent indents them. want introduce braces , semicolons, whitespace isn't relevant more. because curious.
the second example suggests, where belongs whole runst $ ... expression, first example suggests, somehow part of go n mvec statement. reading in haskell report chapter 2.7 tried introduce braces , semicolons in first example like
mutableupdateio :: int -> io (mv.mvector realworld int) mutableupdateio n = { mvec <- gm.new (n + 1); go n mvec; { go 0 v = return v; go n v = (mv.write v n 0) >> go (n - 1) v; } ; } but parsing error. why that?
why layout hindent produces first example mutableupdateio valid haskell? shouldn't braces , semicolons introduced in above try?
the where blocks belong neither runst $ ... expression nor go n mvec statement; belong mutableupdateio n = ... declaration , mutableupdatest n = ... declaration. braces , semicolons should go this:
mutableupdateio :: int -> io (mv.mvector realworld int) mutableupdateio n = { mvec <- gm.new (n + 1); go n mvec; } { go 0 v = return v; go n v = (mv.write v n 0) >> go (n - 1) v; } the relevant sentence informal description in report in chapter 2.7 this:
a close brace inserted whenever syntactic category containing layout list ends; is, if illegal lexeme encountered @ point close brace legal, close brace inserted.
since where illegal lexeme inside expression, ends do block , close brace inserted there. explains why layout hindent produced legal.
Comments
Post a Comment