1
------------------------------------------------------------------------
2
-- The Agda standard library
4
-- The free monad construction on indexed containers
5
------------------------------------------------------------------------
7
module Data.Container.Indexed.FreeMonad where
10
open import Function hiding (const)
11
open import Category.Monad.Predicate
12
open import Data.Container.Indexed hiding (_∈_)
13
open import Data.Container.Indexed.Combinator hiding (id; _∘_)
14
open import Data.Empty
15
open import Data.Sum using (inj₁; inj₂)
16
open import Data.Product
17
open import Data.W.Indexed
18
open import Relation.Unary
19
open import Relation.Unary.PredicateTransformer
21
------------------------------------------------------------------------
26
_⋆C_ : ∀ {i o c r} {I : Set i} {O : Set o} →
27
Container I O c r → Pred O c → Container I O _ _
30
_⋆_ : ∀ {ℓ} {O : Set ℓ} → Container O O ℓ ℓ → Pt O ℓ
33
pattern returnP x = (inj₁ x , _)
34
pattern doP c k = (inj₂ c , k)
36
do : ∀ {ℓ} {O : Set ℓ} {C : Container O O ℓ ℓ} {X} →
38
do (c , k) = sup (doP c k)
40
rawPMonad : ∀ {ℓ} {O : Set ℓ} {C : Container O O ℓ ℓ} →
41
RawPMonad {ℓ = ℓ} (_⋆_ C)
42
rawPMonad {C = C} = record
47
return : ∀ {X} → X ⊆ C ⋆ X
48
return x = sup (inj₁ x , ⊥-elim ∘ lower)
50
_=<<_ : ∀ {X Y} → X ⊆ C ⋆ Y → C ⋆ X ⊆ C ⋆ Y
51
f =<< sup (returnP x) = f x
52
f =<< sup (doP c k) = do (c , λ r → f =<< k r)
54
leaf : ∀ {ℓ} {O : Set ℓ} {C : Container O O ℓ ℓ} {X : Pred O ℓ} →
56
leaf (c , k) = do (c , return? ∘ k)
58
open RawPMonad rawPMonad