9
9
module IO where
10
10
11
11
open import Codata.Musical.Notation
12
- open import Codata.Musical.Colist
13
12
open import Codata.Musical.Costring
14
13
open import Data.Unit.Polymorphic
15
14
open import Data.String
16
15
open import Function
17
16
import IO.Primitive as Prim
18
17
open import Level
19
18
19
+ private
20
+ variable
21
+ a b : Level
22
+
20
23
------------------------------------------------------------------------
21
24
-- The IO monad
22
25
@@ -27,50 +30,96 @@ open import Level
27
30
-- introduced to avoid this problem. Possible non-termination is
28
31
-- isolated to the run function below.
29
32
30
- infixl 1 _>>=_ _>>_
31
-
32
- data IO {a} (A : Set a) : Set (suc a) where
33
+ data IO (A : Set a) : Set (suc a) where
33
34
lift : (m : Prim.IO A) → IO A
34
35
return : (x : A) → IO A
35
- _>>=_ : {B : Set a} (m : ∞ (IO B)) (f : (x : B) → ∞ (IO A)) → IO A
36
- _>>_ : {B : Set a} (m₁ : ∞ (IO B)) (m₂ : ∞ (IO A)) → IO A
36
+ bind : {B : Set a} (m : ∞ (IO B)) (f : (x : B) → ∞ (IO A)) → IO A
37
+ seq : {B : Set a} (m₁ : ∞ (IO B)) (m₂ : ∞ (IO A)) → IO A
38
+
39
+ pure : {A : Set a} → A → IO A
40
+ pure = return
41
+
42
+ module _ {A B : Set a} where
43
+
44
+ infixl 1 _<$>_ _<*>_ _>>=_ _>>_
45
+
46
+ _<*>_ : IO (A → B) → IO A → IO B
47
+ mf <*> mx = bind (♯ mf) λ f → ♯ (bind (♯ mx) λ x → ♯ pure (f x))
48
+
49
+ _<$>_ : (A → B) → IO A → IO B
50
+ f <$> m = pure f <*> m
51
+
52
+ _>>=_ : IO A → (A → IO B) → IO B
53
+ m >>= f = bind (♯ m) λ x → ♯ f x
54
+
55
+ _>>_ : IO A → IO B → IO B
56
+ m₁ >> m₂ = seq (♯ m₁) (♯ m₂)
37
57
38
58
{-# NON_TERMINATING #-}
39
59
40
- run : ∀ {a} {A : Set a} → IO A → Prim.IO A
41
- run (lift m) = m
42
- run (return x) = Prim.return x
43
- run (m >>= f) = Prim._>>=_ (run (♭ m )) λ x → run (♭ (f x))
44
- run (m₁ >> m₂) = Prim._>>=_ (run (♭ m₁)) λ _ → run (♭ m₂)
60
+ run : {A : Set a} → IO A → Prim.IO A
61
+ run (lift m) = m
62
+ run (return x) = Prim.return x
63
+ run (bind m f) = Prim._>>=_ (run (♭ m )) λ x → run (♭ (f x))
64
+ run (seq m₁ m₂) = Prim._>>=_ (run (♭ m₁)) λ _ → run (♭ m₂)
45
65
46
66
------------------------------------------------------------------------
47
67
-- Utilities
48
68
49
- sequence : ∀ {a} {A : Set a} → Colist (IO A) → IO (Colist A)
50
- sequence [] = return []
51
- sequence (c ∷ cs) = ♯ c >>= λ x →
52
- ♯ (♯ sequence (♭ cs) >>= λ xs →
53
- ♯ return (x ∷ ♯ xs))
69
+ module Colist where
70
+
71
+ open import Codata.Musical.Colist
72
+
73
+ module _ {A : Set a} where
74
+
75
+ sequence : Colist (IO A) → IO (Colist A)
76
+ sequence [] = return []
77
+ sequence (c ∷ cs) = bind (♯ c) λ x → ♯
78
+ bind (♯ sequence (♭ cs)) λ xs → ♯
79
+ return (x ∷ ♯ xs)
80
+
81
+ -- The reason for not defining sequence′ in terms of sequence is
82
+ -- efficiency (the unused results could cause unnecessary memory use).
83
+
84
+ sequence′ : Colist (IO A) → IO ⊤
85
+ sequence′ [] = return _
86
+ sequence′ (c ∷ cs) = seq (♯ c) (♯ sequence′ (♭ cs))
87
+
88
+ module _ {A : Set a} {B : Set b} where
89
+
90
+ mapM : (A → IO B) → Colist A → IO (Colist B)
91
+ mapM f = sequence ∘ map f
92
+
93
+ mapM′ : (A → IO B) → Colist A → IO ⊤
94
+ mapM′ f = sequence′ ∘ map f
95
+
96
+ module List where
97
+
98
+ open import Data.List.Base
99
+
100
+ module _ {A : Set a} where
101
+
102
+ sequence : List (IO A) → IO (List A)
103
+ sequence [] = ⦇ [] ⦈
104
+ sequence (c ∷ cs) = ⦇ c ∷ sequence cs ⦈
54
105
55
- -- The reason for not defining sequence′ in terms of sequence is
56
- -- efficiency (the unused results could cause unnecessary memory use).
106
+ -- The reason for not defining sequence′ in terms of sequence is
107
+ -- efficiency (the unused results could cause unnecessary memory use).
57
108
58
- sequence′ : ∀ {a} {A : Set a} → Colist (IO A) → IO ⊤
59
- sequence′ [] = return _
60
- sequence′ (c ∷ cs) = ♯ c >>
61
- ♯ (♯ sequence′ (♭ cs) >>
62
- ♯ return _)
109
+ sequence′ : List (IO A) → IO ⊤
110
+ sequence′ [] = return _
111
+ sequence′ (c ∷ cs) = c >> sequence′ cs
63
112
64
- module _ {a b} {A : Set a} {B : Set b} where
113
+ module _ {A : Set a} {B : Set b} where
65
114
66
- mapM : (A → IO B) → Colist A → IO (Colist B)
67
- mapM f = sequence ∘ map f
115
+ mapM : (A → IO B) → List A → IO (List B)
116
+ mapM f = sequence ∘ map f
68
117
69
- mapM′ : (A → IO B) → Colist A → IO ⊤
70
- mapM′ f = sequence′ ∘ map f
118
+ mapM′ : (A → IO B) → List A → IO ⊤
119
+ mapM′ f = sequence′ ∘ map f
71
120
72
121
ignore : ∀ {a} {A : Set a} → IO A → IO ⊤
73
- ignore io = ♯ io >> ♯ return _
122
+ ignore io = io >> return _
74
123
75
124
------------------------------------------------------------------------
76
125
-- Simple lazy IO
0 commit comments