by serve_yay · 11y ago · 0 comments
edit: I was somewhat inaccurate here - as the name implies, `Pure.println` is pure, but it returns a thunk which is not. That's what I'm talking about, I have trouble understanding how this isn't just a little dance you do to pretend your code isn't impure.
This nets us tests that are 100% reliable. The compiler rejects all code that can introduce external inputs.
As the author of the article I'm probably not the best person to give a different perspective on the article. Hope that helps in any case.
Based on the answers here and prior thought, I believe that Monads are Noble Lies [1]. Impurity is a hard concept. Purity is the goal. So rather than accepting into a well designed model the idea of impurity one should say something is pure even if it actually thunks at the end. This keeps the overall model sound. For people that really don't want or need or can't get into the details of real life mutability, you can keep the model as is with the lie of glossing over the thunk.
This is what we're doing here. We very explicitly separate our program into two times -- giving the instructions, and shouting "go!" (calling run). Before we shout "go!", substitution holds. Once we shout "go!" we don't give instructions any more.
In Haskell you don't even call `run` -- the runtime does this for you.
There is no Noble Lie. There is a time (a world if you like) where substitution holds. We assemble our program in this time / world. There is a time / world where the program is run. We don't construct the program in this world. (This may sound restrictive, but it isn't.)
Play with the code. The example is straightforward and easy to extend.
An important note to consider is that Haskell has a defined order of computation just like most other languages.
The thunk will be evaluated based on certain rules, combined with how Monads are chained (using `>>=` or bind) this causes the effects to naturally occur in the correct order when the thunks are evaluated.
Put another way the thunks being impure is safe because the order of evaluation of the thunks is well defined.
Note that evaluation order of pure operations is well defined but hard to pin down, as "outside in" is hard to grasp. However since those methods are pure the order isn't important.
-- set of commands
data Teletype = Write String x | Read (String -> x) deriving Functor
-- monadic data structure
data TeletypeM a = Pure a | Effect (Teletype (TeletypeM a))
-- monad instance
instance Monad TeletypeM where
return = Pure
m >>= k = case m of
Pure a -> k a
Effect tt -> Effect (fmap (>>= k) tt)
-- API
write :: String -> TeletypeM ()
write s = Effect (Write s ())
read :: TeletypeM String
read = Effect (Read (\s -> s))
Now using this we can write "effectful" teletype programs. For instance, the "echo doubler" echoTwice :: TeletypeM ()
echoTwice = forever $ do
line <- read
write line
write line
Finally, we can "interpret this into IO", which is to say give it a side-effecting semantics runTeletypeMIO :: TeletypeM a -> IO a
runTeletypeMIO m = case m of
Pure a -> return a
Effect e -> case e of
Write s next -> do
putStrLn s
runTeletypeMIO next
Read cont -> do
line <- readLine
let next = cont line
runTeletypeMIO next
This is nice and feels a lot like "pushing the toothpaste around", but we can take advantage of its pure structure for other purposes. For instance, here's a delimited interpreter which only allows it to take a certain number of "steps" runTTDelimited :: Int -> TeletypeM () -> IO ()
runTTDelimited n m
| n <= 0 = return ()
| otherwise = case m of
Pure a -> return a
Effect e -> case e of
Write s next -> do
putStrLn s
runTTDelimited (n-1) next
Read cont -> do
line <- readLine
let next = cont line
runTTDelimited (n-1) next
And here's a completely pure interpreter version of the above which also takes its input from a pre-arranged "script" runTTPure :: Int -> TeletypeM () -> ([String] -> [String])
runTTPure n m input = reverse (go n m input []) where
go n m input acc
| n <= 0 = acc
| otherwise = case m of
Pure a -> acc
Effect e -> case e of
Write s next -> go (n-1) next input (s : acc)
Read cont -> case input of
[] -> error "ran out of input"
(s : ss) -> go (n-1) (cont s) ss acc
All of these can work off of the same TeletypeM values.