| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- The IO monad |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --guardedness #-} |
| 8 |
|
| 9 |
module Effect.Monad.IO where |
| 10 |
|
| 11 |
open import Data.Product.Base using (_,_) |
| 12 |
open import Effect.Functor using (RawFunctor) |
| 13 |
open import Function.Base using (id) |
| 14 |
open import IO.Base using (IO) |
| 15 |
open import Level using (Level; suc) |
| 16 |
|
| 17 |
private |
| 18 |
variable |
| 19 |
f g : Level |
| 20 |
A : Set f |
| 21 |
M : Set f → Set g |
| 22 |
|
| 23 |
------------------------------------------------------------------------ |
| 24 |
-- IO monad operations |
| 25 |
|
| 26 |
record RawMonadIO |
| 27 |
(M : Set f → Set (suc f)) |
| 28 |
: Set (suc f) where |
| 29 |
field |
| 30 |
liftIO : IO A → M A |
| 31 |
|
| 32 |
------------------------------------------------------------------------ |
| 33 |
-- IO monad specifics |
| 34 |
|
| 35 |
monadIO : RawMonadIO {f} IO |
| 36 |
monadIO = record { liftIO = id } |
| 37 |
|
| 38 |
open import Effect.Monad.State.Transformer.Base using (StateT; mkStateT) |
| 39 |
|
| 40 |
liftStateT : ∀ {S} → RawFunctor M → RawMonadIO M → RawMonadIO (StateT S M) |
| 41 |
liftStateT M MIO = record |
| 42 |
{ liftIO = λ io → mkStateT (λ s → (s ,_) <$> liftIO io) |
| 43 |
} where open RawFunctor M; open RawMonadIO MIO |
| 44 |
|
| 45 |
open import Effect.Monad.Reader.Transformer.Base using (ReaderT; mkReaderT) |
| 46 |
|
| 47 |
liftReaderT : ∀ {R} → RawMonadIO M → RawMonadIO (ReaderT R M) |
| 48 |
liftReaderT MIO = record |
| 49 |
{ liftIO = λ io → mkReaderT (λ r → liftIO io) |
| 50 |
} where open RawMonadIO MIO |
| 51 |
|
| 52 |
open import Effect.Monad.Writer.Transformer.Base using (WriterT; mkWriterT) |
| 53 |
|
| 54 |
liftWriterT : ∀ {f 𝕎} → RawFunctor M → RawMonadIO M → RawMonadIO (WriterT {f = f} 𝕎 M) |
| 55 |
liftWriterT M MIO = record |
| 56 |
{ liftIO = λ io → mkWriterT (λ w → (w ,_) <$> liftIO io) |
| 57 |
} where open RawFunctor M; open RawMonadIO MIO |