Debian Codegraph @

src/Data/Container/Indexed/FreeMonad.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- The free monad construction on indexed containers
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Container.Indexed.FreeMonad where
10
11 open import Level using (Level; _⊔_; lower)
12 open import Function.Base hiding (const)
13 open import Effect.Monad.Predicate using (RawPMonad)
14 open import Data.Container.Indexed using (Container; Command; Response; next; ⟦_⟧; μ)
15 open import Data.Container.Indexed.Combinator hiding (id; _∘_)
16 open import Data.Empty using (⊥; ⊥-elim)
17 open import Data.Sum.Base using (inj₁; inj₂)
18 open import Data.Product.Base using (_,_)
19 open import Data.W.Indexed using (sup)
20 open import Relation.Unary using (Pred; _⊆_; ⋃; _∈_; {_})
21 open import Relation.Unary.PredicateTransformer using (Pt)
22 open import Relation.Binary.PropositionalEquality.Core using (refl)
23
24 ------------------------------------------------------------------------
25
26 infixl 9 _⋆C_
27 infix 9 _⋆_
28
29 _⋆C_ : ∀ {i o c r} {I : Set i} {O : Set o} →
30 Container I O c r → Pred O c → Container I O _ _
31 C ⋆C X = const X ⊎′ C
32
33 _⋆_ : ∀ {ℓ} {O : Set ℓ} → Container O O ℓ ℓ → Pt O ℓ
34 C ⋆ X = μ (C ⋆C X)
35
36 pattern returnP x = (inj₁ x , _)
37 pattern doP c k = (inj₂ c , k)
38
39 inn : ∀ {ℓ} {O : Set ℓ} {C : Container O O ℓ ℓ} {X} →
40 ⟦ C ⟧ (C ⋆ X) ⊆ C ⋆ X
41 inn (c , k) = sup (doP c k)
42
43 rawPMonad : ∀ {ℓ} {O : Set ℓ} {C : Container O O ℓ ℓ} →
44 RawPMonad {ℓ = ℓ} (_⋆_ C)
45 rawPMonad {C = C} = record
46 { return? = return
47 ; _=<?_ = _=<<_
48 }
49 where
50 return : ∀ {X} → X ⊆ C ⋆ X
51 return x = sup (inj₁ x , ⊥-elim ∘ lower)
52
53 _=<<_ : ∀ {X Y} → X ⊆ C ⋆ Y → C ⋆ X ⊆ C ⋆ Y
54 f =<< sup (returnP x) = f x
55 f =<< sup (doP c k) = inn (c , λ r → f =<< k r)
56
57 leaf : ∀ {ℓ} {O : Set ℓ} {C : Container O O ℓ ℓ} {X : Pred O ℓ} →
58 ⟦ C ⟧ X ⊆ C ⋆ X
59 leaf (c , k) = inn (c , return? ∘ k)
60 where
61 open RawPMonad rawPMonad
62
63 generic : ∀ {ℓ} {O : Set ℓ} {C : Container O O ℓ ℓ} {o}
64 (c : Command C o) →
65 o ∈ C ⋆ (⋃[ r ∶ Response C c ] { next C c r })
66 generic c = inn (c , λ r → return? (r , refl))
67 where
68 open RawPMonad rawPMonad