std:IO

lib/IO.omg

lib/IO.omg106 lines
1{-  Input and output.
2
3    An `IO a` is a description of an interaction with the outside world that
4    produces an `a`.  Nothing happens when such a value is computed; it is
5    carried out when the program is run (`omegyu run`).  Actions are built
6    from the builtins `IO/pure`, `IO/bind`, `IO/putStr`, `IO/getLine`,
7    `IO/readFile`, `IO/writeFile`, `IO/args`, `IO/getEnv`, `IO/exit` and
8    `IO/loop`, most conveniently in a `do` block.
9
10    An action can produce a value of any universe, a type included:
11    `pure Natural` is an `IO Type`.  The actions that produce nothing of
12    interest, such as `putStrLn`, produce `{=}`, of type `unit`, as the
13    builtins they are made of do; `forEach` and `times` repeat actions that
14    produce the empty record of any universe, and produce the one of the
15    universe that is expected of them.
16-}
17
18let pure : forall {l : Level} {a : Type l} -> a -> IO a = IO/pure
19
20let bind
21    : forall {la lb : Level} {a : Type la} {b : Type lb} -> IO a -> (a -> IO b) -> IO b
22    = IO/bind
23
24let map
25    : forall {la lb : Level} {a : Type la} {b : Type lb} -> (a -> b) -> IO a -> IO b
26    = \{la} {lb} {a} {b} f action -> IO/bind action (\(x : a) -> IO/pure (f x))
27
28let putStr : Text -> IO unit = IO/putStr
29
30let putStrLn : Text -> IO unit = \line -> IO/putStr (line ++ "\n")
31
32-- Print a number on a line of its own.
33let print : Natural -> IO unit = \n -> putStrLn (Natural/show n)
34
35-- Read a line from standard input; `None Text` at the end of the input.
36let getLine : IO (Optional Text) = IO/getLine
37
38let readFile : Text -> IO Text = IO/readFile
39
40let writeFile : Text -> Text -> IO unit = IO/writeFile
41
42-- The command-line arguments of the program.
43let args : IO (List Text) = IO/args
44
45let getEnv : Text -> IO (Optional Text) = IO/getEnv
46
47-- Stop the program with the given exit code.
48let exit : forall {l : Level} {a : Type l} -> Natural -> IO a = IO/exit
49
50-- Run an action for each element of a list, in order.  The elements can be
51-- of any universe, and the actions produce the empty record of any universe
52-- (`unit` when they print, say); so does the whole, `unit @lr`, which can
53-- be another one: printing is a step of a `do` block of `std:Monad.io @1`.
54let forEach
55    : forall {l lr : Level} {la : Level} {a : Type la}
56      -> List a
57      -> (a -> IO (unit @l))
58      -> IO (unit @lr)
59    = \{l} {lr} {la} {a} xs f ->
60        IO/bind
61          (List/elim
62             (\(_ : List a) -> IO (unit @l))
63             (IO/pure {=})
64             (\(x : a) (_ : List a) (rest : IO (unit @l)) -> IO/bind (f x) (\(_ : unit @l) -> rest))
65             xs)
66          (\(_ : unit @l) -> IO/pure {=})
67
68-- Run an action n times, giving it the index of the iteration.  It is a
69-- loop over the index, which runs in constant memory however many times it
70-- runs (a chain of n actions would have to be built first).  As for
71-- `forEach`, the whole produces the empty record of any universe.
72let times
73    : forall {l lr : Level} -> Natural -> (Natural -> IO (unit @l)) -> IO (unit @lr)
74    = \{l} {lr} n f ->
75        IO/bind
76          (IO/loop
77             0
78             (\(k : Natural) ->
79                if Natural/isZero (Natural/subtract k n)
80                then IO/pure (None Natural)
81                else IO/bind (f k) (\(_ : unit @l) -> IO/pure (Some (k + 1)))))
82          (\(_ : Natural) -> IO/pure {=})
83
84-- Iterate a step from an initial state for as long as it returns a new
85-- state; the result is the last state.  This is the one way to write a
86-- program that runs for an unbounded time.  The state can hold types.
87let loop
88    : forall {l : Level} {s : Type l} -> s -> (s -> IO (Optional s)) -> IO s
89    = IO/loop
90
91in  { pure
92    , bind
93    , map
94    , putStr
95    , putStrLn
96    , print
97    , getLine
98    , readFile
99    , writeFile
100    , args
101    , getEnv
102    , exit
103    , forEach
104    , times
105    , loop
106    }
Your result
Complete file: lib/IO.omg · with imports