| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- IO |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --guardedness #-} |
| 8 |
|
| 9 |
module IO where |
| 10 |
|
| 11 |
open import Codata.Musical.Notation |
| 12 |
open import Codata.Musical.Costring |
| 13 |
open import Data.Unit.Polymorphic.Base |
| 14 |
open import Data.String.Base using (String) |
| 15 |
import Data.Unit.Base as Unit0 |
| 16 |
open import Function.Base using (_∘_; flip) |
| 17 |
import IO.Primitive.Core as Prim |
| 18 |
open import Level |
| 19 |
|
| 20 |
private |
| 21 |
variable |
| 22 |
a b : Level |
| 23 |
A : Set a |
| 24 |
B : Set b |
| 25 |
|
| 26 |
------------------------------------------------------------------------ |
| 27 |
-- Re-exporting the basic type and functions |
| 28 |
|
| 29 |
open import IO.Base public |
| 30 |
open import IO.Handle public |
| 31 |
|
| 32 |
------------------------------------------------------------------------ |
| 33 |
-- Utilities |
| 34 |
|
| 35 |
module Colist where |
| 36 |
|
| 37 |
open import Codata.Musical.Colist.Base |
| 38 |
|
| 39 |
sequence : Colist (IO A) → IO (Colist A) |
| 40 |
sequence [] = pure [] |
| 41 |
sequence (c ∷ cs) = bind (♯ c) λ x → ♯ |
| 42 |
bind (♯ sequence (♭ cs)) λ xs → ♯ |
| 43 |
pure (x ∷ ♯ xs) |
| 44 |
|
| 45 |
-- The reason for not defining sequence′ in terms of sequence is |
| 46 |
-- efficiency (the unused results could cause unnecessary memory use). |
| 47 |
|
| 48 |
sequence′ : Colist (IO A) → IO ⊤ |
| 49 |
sequence′ [] = pure _ |
| 50 |
sequence′ (c ∷ cs) = seq (♯ c) (♯ sequence′ (♭ cs)) |
| 51 |
|
| 52 |
mapM : (A → IO B) → Colist A → IO (Colist B) |
| 53 |
mapM f = sequence ∘ map f |
| 54 |
|
| 55 |
mapM′ : (A → IO B) → Colist A → IO ⊤ |
| 56 |
mapM′ f = sequence′ ∘ map f |
| 57 |
|
| 58 |
forM : Colist A → (A → IO B) → IO (Colist B) |
| 59 |
forM = flip mapM |
| 60 |
|
| 61 |
forM′ : Colist A → (A → IO B) → IO ⊤ |
| 62 |
forM′ = flip mapM′ |
| 63 |
|
| 64 |
module List where |
| 65 |
|
| 66 |
open import Data.List.Base |
| 67 |
|
| 68 |
sequence : List (IO A) → IO (List A) |
| 69 |
sequence [] = ⦇ [] ⦈ |
| 70 |
sequence (c ∷ cs) = ⦇ c ∷ sequence cs ⦈ |
| 71 |
|
| 72 |
-- The reason for not defining sequence′ in terms of sequence is |
| 73 |
-- efficiency (the unused results could cause unnecessary memory use). |
| 74 |
|
| 75 |
sequence′ : List (IO A) → IO ⊤ |
| 76 |
sequence′ [] = pure _ |
| 77 |
sequence′ (c ∷ cs) = c >> sequence′ cs |
| 78 |
|
| 79 |
mapM : (A → IO B) → List A → IO (List B) |
| 80 |
mapM f = sequence ∘ map f |
| 81 |
|
| 82 |
mapM′ : (A → IO B) → List A → IO ⊤ |
| 83 |
mapM′ f = sequence′ ∘ map f |
| 84 |
|
| 85 |
forM : List A → (A → IO B) → IO (List B) |
| 86 |
forM = flip mapM |
| 87 |
|
| 88 |
forM′ : List A → (A → IO B) → IO ⊤ |
| 89 |
forM′ = flip mapM′ |
| 90 |
|
| 91 |
------------------------------------------------------------------------ |
| 92 |
-- Simple lazy IO |
| 93 |
|
| 94 |
-- Note that the functions below produce commands which, when |
| 95 |
-- executed, may raise exceptions. |
| 96 |
|
| 97 |
-- Note also that the semantics of these functions depends on the |
| 98 |
-- version of the Haskell base library. If the version is 4.2.0.0 (or |
| 99 |
-- later?), then the functions use the character encoding specified by |
| 100 |
-- the locale. For older versions of the library (going back to at |
| 101 |
-- least version 3) the functions use ISO-8859-1. |
| 102 |
|
| 103 |
open import IO.Finite public |
| 104 |
renaming (readFile to readFiniteFile) |
| 105 |
|
| 106 |
open import IO.Infinite public |
| 107 |
renaming ( writeFile to writeFile∞ |
| 108 |
; appendFile to appendFile∞ |
| 109 |
; putStr to putStr∞ |
| 110 |
; putStrLn to putStrLn∞ |
| 111 |
) |