Debian Codegraph @

doc/README/IO.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Simple examples of programs using IO
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --guardedness #-}
8
9 module README.IO where
10
11 open import Level
12 open import Data.Nat.Base
13 open import Data.Nat.Show using (show)
14 open import Data.String.Base using (String; _++_; lines)
15 open import Data.Unit.Polymorphic
16 open import Function.Base using (_$_)
17 open import IO
18
19 ------------------------------------------------------------------------
20 -- Basic programs
21 ------------------------------------------------------------------------
22
23 ------------------------------------------------------------------------
24 -- Hello World!
25
26 -- Minimal example of an IO program.
27 -- * The entrypoint of the executable is given type `Main`
28 -- * It is implemented using `run`, a function that converts a description
29 -- of an IO-computation into a computation that actually invokes the magic
30 -- primitives that will perform the side effects.
31
32 helloWorld : Main
33 helloWorld = run (putStrLn "Hello World!")
34
35 ------------------------------------------------------------------------
36 -- Hello {{name}}!
37
38 -- We can of course write little auxiliary functions that may be used in
39 -- larger IO programs. Here we are going to first write a function displaying
40 -- "Hello {{name}}!" when {{name}} is passed as an argument.
41
42 -- `IO` primitives whose sole purpose is generating side effects (e.g.
43 -- printing a string on the screen) are typically given a level polymorphic
44 -- type which means we may need to add explicit level annotations.
45 -- Here we state that the `IO` computation will be at level zero (`0ℓ`).
46
47 sayHello : String → IO {0ℓ} ⊤
48 sayHello name = putStrLn ("Hello " ++ name ++ "!")
49
50 -- Functions can be sequenced using monadic combinators or `do` notations.
51 -- The two following definitions are equivalent. They start by asking the
52 -- user what their name is, listen for an answer and respond by saying hello
53 -- using the `sayHello` auxiliary function we just defined.
54
55 helloName : Main
56 helloName = run (putStrLn "What is your name?" >> getLine >>= sayHello)
57
58 doHelloName : Main
59 doHelloName = run do
60 putStrLn "What is your name?"
61 name ← getLine
62 sayHello name
63
64 ------------------------------------------------------------------------
65 -- (Co)Recursive programs
66 ------------------------------------------------------------------------
67
68 ------------------------------------------------------------------------
69 -- NO GUARDEDNESS
70
71 -- If you do not need to rely on guardedness for the function to be seen as
72 -- terminating (for instance because it is structural in an inductive argument)
73 -- then you can use `do` notations to write fairly readable programs.
74
75 -- Countdown to explosion
76 countdown : ℕ → IO {0ℓ} _
77 countdown zero = putStrLn "BOOM!"
78 countdown m@(suc n) = do
79 let str = show m
80 putStrLn str
81 countdown n
82
83 -- cat the content of a finite file
84 cat : String → IO _
85 cat fp = do
86 content ← readFiniteFile fp
87 let ls = lines content
88 List.mapM′ putStrLn ls
89
90 ------------------------------------------------------------------------
91 -- TOP-LEVEL LOOP
92
93 -- If you simply want to repeat the same action over and over again, you
94 -- can use `forever` e.g. the following defines a REPL that echos whatever
95 -- the user types
96
97 echo : IO ⊤
98 echo = do
99 hSetBuffering stdout noBuffering
100 forever $ do
101 putStr "echo< "
102 str ← getLine
103 putStrLn ("echo> " ++ str)
104
105
106 ------------------------------------------------------------------------
107 -- GUARDEDNESS
108
109 -- If you are performing coinduction on a potentially infinite piece of codata
110 -- then you need to rely on guardedness. That is to say that the coinductive
111 -- call needs to be obviously under a coinductive constructor and guarded by a
112 -- sharp (♯_).
113 -- In this case you cannot use the convenient combinators that make `do`-notations
114 -- and have to revert back to the underlying coinductive constructors.
115
116 open import Codata.Musical.Notation
117 open import Codata.Musical.Colist using (Colist; []; _∷_)
118 open import Data.Bool
119 open import Data.Unit.Polymorphic.Base
120
121 -- Whether a colist is finite is semi decidable: just let the user wait until
122 -- you reach the end!
123 isFinite : ∀ {a} {A : Set a} → Colist A → IO Bool
124 isFinite [] = pure true
125 isFinite (x ∷ xs) = seq (♯ pure tt) (♯ isFinite (♭ xs))