![Page 1: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/1.jpg)
Zheng Guo
Monad and IODiscussion for assignment 5
![Page 2: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/2.jpg)
Monad
class Monad m where -- bind (>>=) :: m a -> (a -> m b) -> m b -- return return :: a -> m a
![Page 3: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/3.jpg)
Either Monad
class Monad m where -- bind (>>=) :: m a -> (a -> m b) -> m b -- return return :: a -> m a
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
![Page 4: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/4.jpg)
Either Monad
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
>>> return 1 :: Either Int Int
![Page 5: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/5.jpg)
Either Monad
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
>>> return 1 :: Either Int Int Right 1
![Page 6: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/6.jpg)
Either Monad
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
>>> Left 1 >>= \v -> Right (v + 1)
![Page 7: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/7.jpg)
Either Monad
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
>>> Left 1 >>= \v -> Right (v + 1) Left 1
![Page 8: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/8.jpg)
Either Monad
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
>>> Right 1 >>= \v -> Right (v + 1)
![Page 9: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/9.jpg)
Either Monad
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
>>> Right 1 >>= \v -> Right (v + 1) Right 2
![Page 10: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/10.jpg)
Either Monad
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
>>> Right 1 >>= \v -> Left (v + 1)
![Page 11: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/11.jpg)
Either Monad
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
>>> Right 1 >>= \v -> Left (v + 1) Left 2
![Page 12: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/12.jpg)
Either Monad
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
>>> Right 1 >>= Left >>> Right 1 >>= \v -> Left v
![Page 13: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/13.jpg)
Either Monad
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
>>> Right 1 >>= Left Left 1
![Page 14: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/14.jpg)
Either Monad for EThr
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
evalE env (EInt i) = ?? evalE env (EThr e) = ?? evalE env (EBin Plus e1 e2) = ??
![Page 15: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/15.jpg)
Either Monad for EThr
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
evalE env (EInt i) = ?? -- this should return a normal value
![Page 16: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/16.jpg)
Either Monad for EThr
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
evalE env (EInt i) = return (VInt i)
![Page 17: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/17.jpg)
Either Monad for EThr
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
evalE env (EInt i) = return (VInt i) evalE env (EInt i) = Right (VInt i)
![Page 18: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/18.jpg)
Either Monad for EThr
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
evalE env (EThr e) = ??
![Page 19: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/19.jpg)
Either Monad for EThr
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
evalE env (EThr e) = ?? 1) evaluate e to value eitherVal (use evalE), remember eitherVal has the type Either Value Value
![Page 20: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/20.jpg)
Either Monad for EThr
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
evalE env (EThr e) = ?? 1) evaluate e to value eitherVal (use evalE), remember eitherVal has the type Either Value Value 2) if eitherVal is an exception (eitherVal is a Left), then return the exception directly, otherwise (eitherVal is a Right), take the value in eitherVal, wrap it with Left (turn into an exception) and return
![Page 21: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/21.jpg)
Either Monad for EThr
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
evalE env (EThr e) = ?? 1) evaluate e to value eitherVal (use evalE), remember eitherVal has the type Either Value Value 2) if eitherVal is an exception (eitherVal is a Left), then return the exception directly, otherwise (eitherVal is a Right), take the value in eitherVal, wrap it with Left (turn into an exception) and return
v >>= Left
![Page 22: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/22.jpg)
Either Monad for EThr
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
evalE env (EBin Plus e1 e2) = ?? 1) evaluate e1, e2 to value v1, v2 (use evalE), remember v1,v2 has the type Either Value Value 2) if v1 or v2 is an exception (v is a Left), then return the exception directly, otherwise (v1, v2 are both Right), take the values, compute addition, wrap the addition result with Right and return
![Page 23: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/23.jpg)
Either Monad for EThr
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
evalE env (EBin Plus e1 e2) = ?? First option:
evalE env e1 >>= \v1 -> evalE env e2 >>= \v2 -> Right (v1 + v2)
![Page 24: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/24.jpg)
Either Monad for EThr
instance Monad (Either e) where Left l >>= _ = Left l Right r >>= k = k r
return = Right
evalE env (EBin Plus e1 e2) = ?? Second option:
do v1 <- evalE env e1 v2 <- evalE env e2 return (v1 + v2)
![Page 25: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/25.jpg)
IO MonadUseful IO functions
return :: a -> IO a (>>=) :: IO a -> (a -> IO b) -> IO b (=<<) :: (a -> IO b) -> IO a -> IO b (>>) :: IO a -> IO b -> IO b
print :: Show a => a -> IO () putStrLn :: String -> IO () putStr :: String -> IO ()
readFile :: FilePath -> IO String writeFile :: FilePath -> String -> IO () doesFileExist :: FilePath -> IO Bool
![Page 26: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/26.jpg)
IO MonadUseful IO functions
return :: a -> IO a (>>=) :: IO a -> (a -> IO b) -> IO b (=<<) :: (a -> IO b) -> IO a -> IO b (>>) :: IO a -> IO b -> IO b
>>> return 1 1
>>> return 3 >>= \v -> return (v * v) 9
>>> (\v -> return (v * v)) =<< return 3 9
![Page 27: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/27.jpg)
IO MonadUseful IO functions
print :: Show a => a -> IO () putStrLn :: String -> IO () putStr :: String -> IO ()
>>> return (reverse [1,2,3]) >>= print [3,2,1]
>>> putStrLn “first” >> putStrLn “second” first second
![Page 28: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/28.jpg)
IO MonadUseful IO functions
print :: Show a => a -> IO () putStrLn :: String -> IO () putStr :: String -> IO ()
>>> putStrLn “first” >> putStrLn “second” first second
>>> putStr “first” >> putStr “second” firstsecond
![Page 29: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/29.jpg)
IO MonadUseful IO functions
print :: Show a => a -> IO () putStrLn :: String -> IO () putStr :: String -> IO ()
>>> print “first” “first”
>>> putStrLn “first” first
with quotes!
![Page 30: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/30.jpg)
IO MonadUseful IO functions
(import System.IO) readFile :: FilePath -> IO String writeFile :: FilePath -> String -> IO ()
(import System.Directory) doesFileExist :: FilePath -> IO Bool
>>> readFile “test.txt” >>= print … >>> writeFile “test.txt” “second”
>>> doesFileExist “test.txt” False
![Page 31: Monad and IO · IO Monad for the assignment • commands always start with ‘:’ • use `L.isPrefixOf` and `pfx*` to match the command name • use `chomp` to get contents following](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8c356e9e2284314f5f0989/html5/thumbnails/31.jpg)
IO Monadfor the assignment
• commands always start with ‘:’
• use `L.isPrefixOf` and `pfx*` to match the command name
• use `chomp` to get contents following the command name, which is the filename to run or load
• use `putStrLn` to print things to the console• use `return` to put things into the IO monad