IO.agda 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. {-# OPTIONS --without-K #-}
  2. module Common.IO where
  3. open import Agda.Builtin.IO public
  4. open import Common.Bool
  5. open import Common.Char
  6. open import Common.Nat
  7. open import Common.String
  8. open import Common.Unit
  9. open import Common.Float
  10. infixl 1 _>>=_
  11. postulate
  12. return : ∀ {a} {A : Set a} → A → IO A
  13. _>>=_ : ∀ {a b} {A : Set a} {B : Set b} → IO A → (A → IO B) → IO B
  14. {-# COMPILE GHC return = \_ _ -> return #-}
  15. {-# COMPILE GHC _>>=_ = \_ _ _ _ -> (>>=) #-}
  16. {-# COMPILE UHC return = \_ _ x -> UHC.Agda.Builtins.primReturn x #-}
  17. {-# COMPILE UHC _>>=_ = \_ _ _ _ x y -> UHC.Agda.Builtins.primBind x y #-}
  18. {-# COMPILE JS return =
  19. function(u0) { return function(u1) { return function(x) { return function(cb) { cb(x); }; }; }; } #-}
  20. {-# COMPILE JS _>>=_ =
  21. function(u0) { return function(u1) { return function(u2) { return function(u3) {
  22. return function(x) { return function(y) { return function(cb) {
  23. x(function (xx) { y(xx)(cb); });
  24. }; }; };
  25. }; }; }; }
  26. #-}
  27. {-# FOREIGN GHC import qualified Data.Text.IO #-}
  28. postulate
  29. putStr : String -> IO Unit
  30. {-# COMPILE GHC putStr = Data.Text.IO.putStr #-}
  31. {-# COMPILE UHC putStr = UHC.Agda.Builtins.primPutStr #-}
  32. {-# COMPILE JS putStr = function (x) { return function(cb) { process.stdout.write(x); cb(0); }; } #-}
  33. printChar : Char -> IO Unit
  34. printChar c = putStr (charToStr c)
  35. putStrLn : String -> IO Unit
  36. putStrLn s = putStr s >>= \_ -> putStr "\n"
  37. printNat : Nat -> IO Unit
  38. printNat n = putStr (natToString n)
  39. printBool : Bool -> IO Unit
  40. printBool true = putStr "true"
  41. printBool false = putStr "false"
  42. printFloat : Float -> IO Unit
  43. printFloat f = putStr (floatToString f)
  44. infixr 2 _<$>_
  45. _<$>_ : {A B : Set}(f : A -> B)(m : IO A) -> IO B
  46. f <$> x = x >>= λ y -> return (f y)
  47. infixr 0 bind
  48. bind : ∀ {a b} {A : Set a} {B : Set b} → IO A → (A → IO B) → IO B
  49. bind m f = m >>= f
  50. infixr 0 then
  51. then : ∀ {a b} {A : Set a} {B : Set b} -> IO A -> IO B -> IO B
  52. then m f = m >>= λ _ -> f
  53. syntax bind e (\ x -> f) = x <- e , f
  54. syntax then e f = e ,, f