Theory and Design of PL (CS 538)
March 2, 2020
Don’t memorize: we will provide references for everything you will need
term = atom "+" term | atom "-" term | atom ;
atom = num | "(" term ")" ;
m a
a is the type of stuff that is “returned”m augments a with “side information”m a is a type of computation returning stuff of type asaState s a into a normal valuemain always have this type in Haskell?IO is a monad!() is the “return” typeIO Int. How to get the Int out?IO Int -> Int?There is no way to do this!
IO Int gives Int and may do real-world stuff
main :: IO ()
main = do
myRef <- newIORef 0 -- New counter, init 0
count <- readIORef myRef -- Read count
putStrLn $ "Count: " ++ (show count)
writeIORef (count + 1) myRef -- Update count
count' <- readIORef myRef -- Read again
putStrLn $ "Count: " ++ (show count')IO a can literally change the worlddata State s a = MkState (s -> (a, s ))
data IO a = MkIO (WorldState -> (a, WorldState))
-- Think: IO a === State WorldState aWorldState is the state of the whole world
IO a -> IO b
IO a is “run”
main)Cake, it’s a cakeIO Cake, it’s a cake recipeA cake recipe is different from a cake!
But no matter what, we will always have just a cake recipe, and not a cake!
main, type IO ()aaaaa to computation returning ba’sb’sb and a to computation returning bba’sb