IO.agda 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. module Lib.IO where
  2. open import Lib.List
  3. open import Lib.Prelude
  4. {-# FOREIGN GHC import qualified System.Environment #-}
  5. FilePath = String
  6. postulate
  7. IO : Set -> Set
  8. getLine : IO String
  9. putStrLn : String -> IO Unit
  10. putStr : String -> IO Unit
  11. bindIO : {A B : Set} -> IO A -> (A -> IO B) -> IO B
  12. returnIO : {A : Set} -> A -> IO A
  13. getArgs : IO (List String)
  14. readFile : FilePath -> IO String
  15. writeFile : FilePath -> String -> IO Unit
  16. {-# BUILTIN IO IO #-}
  17. {-# COMPILE GHC IO = type IO #-}
  18. {-# COMPILE GHC putStr = putStr #-}
  19. {-# COMPILE GHC putStrLn = putStrLn #-}
  20. {-# COMPILE GHC bindIO = \_ _ -> (>>=) :: IO a -> (a -> IO b) -> IO b #-}
  21. {-# COMPILE GHC returnIO = \_ -> return :: a -> IO a #-}
  22. -- we need to throw away the type argument to return and bind
  23. -- and resolve the overloading explicitly (since Alonzo
  24. -- output is sprinkled with unsafeCoerce#).
  25. {-# COMPILE GHC getLine = getLine #-}
  26. {-# COMPILE GHC getArgs = System.Environment.getArgs #-}
  27. {-# COMPILE GHC readFile = readFile #-}
  28. {-# COMPILE GHC writeFile = writeFile #-}
  29. mapM : {A B : Set} -> (A -> IO B) -> List A -> IO (List B)
  30. mapM f [] = returnIO []
  31. mapM f (x :: xs) = bindIO (f x) \y -> bindIO (mapM f xs) \ys -> returnIO (y :: ys)
  32. mapM₋ : {A : Set} -> (A -> IO Unit) -> List A -> IO Unit
  33. mapM₋ f xs = bindIO (mapM f xs) \_ -> returnIO unit