6 ms·
monadic IO isn't pure in the same sense that even a pure Haskell function isn't pure (cuz it's usually executing on a register machine), but from a language sem
by rtpg 5y ago
monadic IO isn't pure in the same sense that even a pure Haskell function isn't pure (cuz it's usually executing on a register machine), but from a language semantics thing, it _is_ a pure thing!
The whole premise is that you end up with some main function which is a description of side effects, but you still have referential transparency and the like at a language level (so I/O doesn't need special treatment).
(there's a whole thing about "monad = IO" in laypeople discussion of Haskell which I think muddies this. There are loads of monads not about doing side effects, like list. But instead people think there's a link between monads and side effects, because we say "IO monad" instead of just "in IO" or something)
EDIT: it's v true that like.... side effects are side effects are side effects, so you can take that shortcut when talking about stuff. But if you want to get into the details about whether some expression is pure or not, you're brushing up against important semantics!
- goto11 5y agoSo in another subthread I compared two code samples. One in Haskell: main = do putStrLn "Hello, what's your name?" name <- getLine putStrLn $ "Hello " ++ name And one in JavaScript: function main() { let name = prompt('Hello, what is your name?'); alert('Hello, ' + name); } My claim is that either both have side effects or neither. In both cases the definition of a function does not in itself have side effects, and the function can be passed around and manipulated without being executed. But when the function is executed, it causes side effects. Haskell have the feature that the type system can statically verify if functions are pure or not. This is very cool, but it does not change the semantics or purity of a function, it just verifies it statically.
- tome 5y agoI wrote down the properties that we acutally care about when we talk about purity at https://news.ycombinator.com/item?id=27261487 https://news.ycombinator.com/item?id=27261487
- przemo_li 5y agoThat's because every single line of code is about side effect. That means that you have already eliminated all the unnecessary side effects (in both cases), and there is no extra work that could be made pure (also in both cases). Haskell gives you good tools to limit number of side effects to minimum necessary and verify that it is so. JavaScript? It's free for all and people talk all the time how developer discipline leads to more maintainance codebases. Examples give for that discipline? (Almost) all are about more purity.