Debian Codegraph @

src/IO/Base.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- IO: basic types and functions
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --guardedness #-}
8
9 module IO.Base where
10
11 open import Agda.Builtin.Maybe using (Maybe; nothing; just)
12 open import Codata.Musical.Notation using (♭; ♯_; ∞)
13 open import Data.Bool.Base using (Bool; true; false; not)
14 open import Data.Sum.Base using (_⊎_; inj₁; inj₂)
15 open import Data.Unit.Polymorphic.Base using (⊤)
16 open import Function.Base using (_∘′_; const; flip)
17 open import Level using (Level; Lift; lower; 0ℓ; suc)
18
19 import Agda.Builtin.Unit as Unit0 using (⊤)
20 import IO.Primitive.Core as Prim using (IO; pure; _>>=_; _>>_)
21
22 private
23 variable
24 a b e : Level
25 A : Set a
26 B : Set b
27 E : Set e
28
29 ------------------------------------------------------------------------
30 -- The IO monad
31
32 -- One cannot write "infinitely large" computations with the
33 -- postulated IO monad in IO.Primitive without turning off the
34 -- termination checker (or going via the FFI, or perhaps abusing
35 -- something else). The following coinductive deep embedding is
36 -- introduced to avoid this problem. Possible non-termination is
37 -- isolated to the run function below.
38
39 data IO (A : Set a) : Set (suc a) where
40 lift : (m : Prim.IO A) → IO A
41 pure : (x : A) → IO A
42 bind : {B : Set a} (m : ∞ (IO B)) (f : (x : B) → ∞ (IO A)) → IO A
43 seq : {B : Set a} (m₁ : ∞ (IO B)) (m₂ : ∞ (IO A)) → IO A
44
45 lift! : IO A → IO (Lift b A)
46 lift! (lift io) = lift (io Prim.>>= λ a → Prim.pure (Level.lift a))
47 lift! (pure a) = pure (Level.lift a)
48 lift! {b = b} (bind m f) = bind (♯ lift! {b = b} (♭ m))
49 (λ x → ♯ lift! (♭ (f (lower x))))
50 lift! {b = b} (seq m₁ m₂) = seq (♯ lift! {b = b} (♭ m₁))
51 (♯ lift! (♭ m₂))
52
53 module _ {A B : Set a} where
54
55 infixl 1 _<$>_ _<*>_ _>>=_ _>>_
56 infixr 1 _=<<_
57
58 _<*>_ : IO (A → B) → IO A → IO B
59 mf <*> mx = bind (♯ mf) λ f → ♯ (bind (♯ mx) λ x → ♯ pure (f x))
60
61 _<$>_ : (A → B) → IO A → IO B
62 f <$> m = pure f <*> m
63
64 _<$_ : B → IO A → IO B
65 b <$ m = (const b) <$> m
66
67 _>>=_ : IO A → (A → IO B) → IO B
68 m >>= f = bind (♯ m) λ x → ♯ f x
69
70 _=<<_ : (A → IO B) → IO A → IO B
71 _=<<_ = flip _>>=_
72
73 _>>_ : IO A → IO B → IO B
74 m₁ >> m₂ = seq (♯ m₁) (♯ m₂)
75
76 _<<_ : IO B → IO A → IO B
77 _<<_ = flip _>>_
78
79 ------------------------------------------------------------------------
80 -- Running programs
81
82 -- A value of type `IO A` is a description of a computation that may
83 -- eventually produce an `A`. The `run` function converts this
84 -- description of a computation into calls to primitive functions that
85 -- will actually perform it.
86
87 {-# NON_TERMINATING #-}
88 run : IO A → Prim.IO A
89 run (lift m) = m
90 run (pure x) = Prim.pure x
91 run (bind m f) = run (♭ m ) Prim.>>= λ x → run (♭ (f x))
92 run (seq m₁ m₂) = run (♭ m₁) Prim.>>= λ _ → run (♭ m₂)
93
94 -- The entrypoint of an Agda program will be assigned type `Main` and
95 -- implemented using `run` on some `IO ⊤` program.
96
97 Main : Set
98 Main = Prim.IO {0ℓ} ⊤
99
100 ------------------------------------------------------------------------
101 -- Utilities
102
103 -- Make a unit-returning primitive level polymorphic
104 lift′ : Prim.IO Unit0.⊤ → IO {a} ⊤
105 lift′ io = lift (io Prim.>>= λ _ → Prim.pure _)
106
107 -- Throw away the result
108 ignore : IO A → IO ⊤
109 ignore io = io >> pure _
110
111
112 ------------------------------------------------------------------------
113 -- Conditional executions
114
115 -- Only run the action if the boolean is true
116 when : Bool → IO {a} ⊤ → IO ⊤
117 when true m = m
118 when false _ = pure _
119
120 -- Only run the action if the boolean is false
121 unless : Bool → IO {a} ⊤ → IO ⊤
122 unless = when ∘′ not
123
124 -- Run the action if the `Maybe` computation was successful
125 whenJust : Maybe A → (A → IO {a} ⊤) → IO ⊤
126 whenJust (just a) k = k a
127 whenJust nothing _ = pure _
128
129 -- Run the action if the `E ⊎_` computation was successful
130 whenInj₂ : E ⊎ A → (A → IO {a} ⊤) → IO ⊤
131 whenInj₂ (inj₂ a) k = k a
132 whenInj₂ (inj₁ _) _ = pure _
133
134
135 ------------------------------------------------------------------------
136 -- Loops
137
138 -- Keep running the action forever
139 forever : IO {a} ⊤ → IO {a} ⊤
140 forever act = seq (♯ act) (♯ forever act)
141
142 -- Keep running an IO action until we get a value. Convenient when user
143 -- input is involved and it may be malformed.
144 untilJust : IO (Maybe A) → IO A
145 -- Note that here we are forced to use `bind` & the musical notation
146 -- explicitly to guarantee that the corecursive call is guarded
147 untilJust m = bind (♯ m) λ where
148 nothing → ♯ untilJust m
149 (just a) → ♯ pure a
150
151 untilInj₂ : {A B : Set a} → (A → IO (A ⊎ B)) → A → IO B
152 untilInj₂ f x = bind (♯ f x) λ where
153 (inj₁ x′) → ♯ untilInj₂ f x′
154 (inj₂ y) → ♯ pure y
155
156
157
158
159
160
161
162 ------------------------------------------------------------------------
163 -- DEPRECATIONS
164
165 untilRight = untilInj₂
166 {-# WARNING_ON_USAGE untilRight
167 "Warning: untilRight was deprecated in v2.1.
168 Please use untilInj₂ instead."
169 #-}