4 ms·
You're right, but I think there's a distinction here that is easy to miss. An IO action describes what to do, but something still has to do that action. A Hask
by Twisol 5y ago
You're right, but I think there's a distinction here that is easy to miss.
An IO action describes what to do, but something still has to do that action. A Haskell program on its own doesn't do anything; that's exactly why it's pure. The Haskell runtime interprets an IO value, unraveling it into the string of commands that comprise it. The runtime executes the commands produced by the Haskell program.
Haskell uses monads exactly because it lets you describe/express complex sequences of actions (for which later actions depend on earlier actions) without requiring that the machinery for actually performing those actions be part of the program too.
- anderskaseorg 5y agoThat would be equally true of a programming model where every program is a pure function from a list of input lines to a list of output lines. Everyone would agree that such a model is pure (if limiting), even though the function itself is just a function, and the language runtime takes care of the ugly low-level details of actually making the system calls that read lines from STDIN and write lines to STDOUT. Purity doesn’t mean that effects don’t happen. It means that effects don’t happen as a side effect of mere expression evaluation.
- Twisol 5y ago> Purity doesn’t mean that effects don’t happen. It means that effects don’t happen as a side effect of mere expression evaluation. I don't disagree? I was adding color to your explanation for other readers, not attempting to correct you. The evaluation of a program merely reduces or simplifies it, transforming its structure without adding or removing information. When we can't reduce further without interacting with the environment, something else must mediate that interaction. > That would be equally true of a programming model where every program is a pure function from a list of input lines to a list of output lines. Indeed, Haskell used this model (`[Request] -> [Response]`) before switching to use an IO monad. It made for a poor architectural basis for complex programs.
- goto11 5y agoBut isn't it the case in any programming language that impure operations only have side effects when they are executed? You might as well argue that a JavaScript program on its own is pure - it is only when it is executed by the runtime it has side effects. You can also pass a function around without executing it in JavaScript.
- pwm 5y agoA good mental model is that Haskell has expressions that are evaluated and JavaScript has statements that are executed. const io_actions = ["hello", "world"].map(s => console.log(s)); io_actions[1]; vs. io_actions = map putStrLn ["hello", "world"] main = io_actions !! 1 JS will print out "hello" and "world" and the value of io_actions[0] is undefined. In Haskell, the value of io_actions !! 0 is IO "world" which is just a value you can pass around in your program. Only when the Haskell runtime evaluates your code is when it will do the effect, which is to print "world". "hello" is never printed. Naturally you can also pattern match on IO (): f :: IO () f = case putStrLn "hello" of _ -> putStrLn "world" also only prints "world". The IO type in Haskell is akin to IO (State World -> (State World, a)), ie. the state monad where the state threaded through the computation is the "real world". This can be demonstrated with the following: f :: IO () f = IO $ \world -> case putStrLn "foo" of IO _ -> case putStrLn "bar" of IO g -> g world prints "bar".
- goto11 5y agoThe JavaScript equivalent would be: const io_actions = ["hello", "world"].map(s => () => console.log(s)); io_actions[1](); which also prints "world" when the JavaScript runtime evaluates the code.
- pwm 5y agoIf your point is that one can implement a Haskell like language in any turing complete programming language then sure, you are correct. GP's point however was that Haskell is purposefully designed to be written and thought about this way from the ground up. Ie. putStrLn "hello" is not a command that is executed verbatim when the runtime gets to its source position. It's an IO action and an IO action is just a value that can be passed around or, by virtue of IO being a Functor, mapped over, etc... If you say that purity and referential transparency are not only reserved for Haskell then again technically sure, but the point of Haskell is that it makes these concepts the centre of its design. Instead of having to jump through hoops to write code where you can do the above you have to jump through hoops to do the opposite.