3 ms·
I'm not entirely sure if you are implying that wrong indentation in Haskell will result in programs that compile, but give differing output? I have honestly nev
by Tehnix 9y ago
I'm not entirely sure if you are implying that wrong indentation in Haskell will result in programs that compile, but give differing output? I have honestly never seen that happen, and couldn't imagine a piece of code that would compile in either cases.
EDIT:
1) as masklinn mentions, the indentation in Python and Haskell works differently. Furthermore, Python can have expressions as top-level, which Haskell cannot, and you can also have if statements with no else in Python, which again, in Haskell you cannot.
2) I know of no formatters for Haskell which will change the semantics, since they all rely on rendering an AST of the code (IIRC).
3) Ocaml is also white-space sensitive, so I don't see the point in the comparison there?
4) You would need to have some pretty contrived code for it to both satisfy the parser and type-check and be wrong logic caused by moving an indentation.
Like, you can't do it in a function that's a if-then-else plainly, nor a case-statement. Maybe a where statement, but then you just promoted the function to top-level, which will not be a problem unless it's shadowing something else, for which you'll get a compiler warning.
Show me some code, and then I'll consider the point valid, I just can't come up with any examples.
- joeyh 9y agoThis example is very contrived, but challenge accepted.. main = do let print = putStr . (++ " ") in do print "happy" print "holidays" -- indentation sensitive putStrLn "!" De-indenting the marked line by 2 spaces exposes some scroogey irony. (Note that compiler warnings give a hint about the bad idea in this code, but nested do's can have this problem without warnings also.)
- avmich 9y agoI'm not sure that's what GP meant. I understood what he said as "it's hard to write code which correct in logic, types, and which passes correct beautifier which changes logic", and I'd think this is impossible. The example you gave requires to change logic - because indentation is a part of the logic here.
- gmfawcett 9y agoOcaml isn't whitespace sensitive (other than in the obvious sense, i.e., to separate tokens). You can write a whole module (or series of modules) on a single line if you want to.
- marcosdumay 9y agoYou can write a whole module in a single line of Haskell too. That's not the correct thing to test.
- gmfawcett 9y agoReally? How do you put multiple "import" statements on a single line in Haskell? Or a function signature followed by its definition? Ocaml uses whitespace (including newlines) for exactly one reason: token separation. Indentation and line breaks are arbitrary, and can be replaced with spaces in any sequence of Ocaml statements. The same cannot be said for Haskell.
- dragonwriter 9y ago> Really? How do you put multiple "import" statements on a single line in Haskell? With semicolons and curly braces; Haskell's newline/indent syntax is merely an aesthetic substitute for those.