Run.lagda 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. \subsection{Meta-theoretic constructions related to the running of
  2. effects}
  3. \label{run}
  4. In this section we develop the machinery needed to run (parts of)
  5. effects. This was done in my first year report for containers.
  6. \AgdaHide{
  7. \begin{code}
  8. {-# OPTIONS --sized-types #-}
  9. module Issue854.Run where
  10. open import Level using (lower)
  11. open import Function
  12. open import Data.Empty
  13. open import Data.Unit
  14. open import Data.Sum
  15. open import Data.Product as Prod
  16. open import Data.List.Relation.Unary.Any
  17. open import Data.Container as Cont hiding (_∈_)
  18. renaming (⟦_⟧ to ⟦_⟧^C; μ to μ^C; _⇒_ to _⇒^C_)
  19. open import Data.Container.Combinator using () renaming (_⊎_ to _⊎^C_)
  20. open import Data.Container.FreeMonad
  21. renaming (_⋆_ to _⋆^C_; _⋆C_ to _⋆^CC_)
  22. open import Data.W
  23. open import Category.Monad
  24. open import Issue854.Types using (Sig)
  25. open import Issue854.TypesSemantics using (⌊_⌋^Sig)
  26. open import Data.List.Membership.Propositional
  27. \end{code}
  28. }
  29. \begin{code}
  30. rec : ∀ {a b c} {A : Set a} {B : A → Set b} {C : Set c} →
  31. (Σ[ x ∈ A ] (B x → W (A ▷ B) × C) → C) → W (A ▷ B) → C
  32. rec f (sup (s , k)) = f (s , (λ p → (k p , rec f (k p))))
  33. _⋆^S_ : Sig → Set → Set
  34. Σ ⋆^S X = μ^C (⌊ Σ ⌋^Sig ⋆^CC X)
  35. ALG : Container _ _ → Set → Set
  36. ALG Σ X = ⟦ Σ ⟧^C X → X
  37. PALG : Container _ _ → Set → Set
  38. PALG Σ X = ⟦ Σ ⟧^C (μ^C Σ × X) → X
  39. HOMO : Container _ _ → Set → Set → Container _ _ → Set → Set
  40. HOMO Σ X I Σ′ Y = PALG (Σ ⋆^CC X) (I → Σ′ ⋆^C Y)
  41. HOMO′ : Container _ _ → Container _ _ → Set → Set → Container _ _ → Set → Set
  42. HOMO′ Σ Σ′ X I Σ″ Y = ⟦ Σ ⋆^CC X ⟧^C
  43. ((Σ′ ⋆^C X) × (I → Σ″ ⋆^C Y)) → (I → Σ″ ⋆^C Y)
  44. PHOMO : Container _ _ → Set → Set → Container _ _ → Set → Set
  45. PHOMO Σ X I Σ′ Y = ⟦ Σ ⋆^CC X ⟧^C
  46. (((Σ ⊎^C Σ′) ⋆^C X) × (I → Σ′ ⋆^C Y)) → I → Σ′ ⋆^C Y
  47. PHOMO′ : Container _ _ → Container _ _ → Set → Set → Container _ _ → Set → Set
  48. PHOMO′ Σ Σ′ X I Σ″ Y = ⟦ Σ ⋆^CC X ⟧^C
  49. (((Σ′ ⊎^C Σ″) ⋆^C X) × (I → Σ″ ⋆^C Y)) → I → Σ″ ⋆^C Y
  50. ⟪_⟫^⊆ : ∀ {Σ Σ′ Σ″ X} → Σ ⇒^C Σ″ → HOMO′ Σ Σ′ X ⊤ Σ″ X
  51. ⟪_⟫^⊆ m (inj₁ x , k) _ = M.return x
  52. where
  53. module M = RawMonad rawMonad
  54. ⟪ m ⟫^⊆ (inj₂ s , k) _ = let (s′ , k′) = ⟪ m ⟫ (s , k)
  55. in inn (s′ , λ p′ → proj₂ (k′ p′) tt)
  56. embed : ∀ {Σ Σ′ X} → Σ ⇒^C Σ′ → Σ ⋆^C X → Σ′ ⋆^C X
  57. embed {X = X} f m = rec ⟪ f ⟫^⊆ m tt
  58. inlMorph : ∀ {ℓ}{C C′ : Container ℓ ℓ} → C ⇒^C (C ⊎^C C′)
  59. inlMorph = record
  60. { shape = inj₁
  61. ; position = λ p → p
  62. }
  63. inl : ∀ {Σ Σ′ X} → Σ ⋆^C X → (Σ ⊎^C Σ′) ⋆^C X
  64. inl = embed inlMorph
  65. squeeze : ∀ {Σ Σ′ X} → ((Σ ⊎^C Σ′) ⊎^C Σ′) ⋆^C X → (Σ ⊎^C Σ′) ⋆^C X
  66. squeeze = embed m
  67. where
  68. m = record
  69. { shape = [ id , inj₂ ]′
  70. ; position = λ { {inj₁ x} → id ; {inj₂ x} → id }
  71. }
  72. lift : ∀ {Σ Σ′ X Y I} → PHOMO Σ X I Σ′ Y → PHOMO (Σ ⊎^C Σ′) X I Σ′ Y
  73. lift φ (inj₁ x , _) i = φ (inj₁ x , ⊥-elim ∘ lower) i
  74. lift φ (inj₂ (inj₁ s) , k) i = φ (inj₂ s , λ p →
  75. let (w , ih) = k p in squeeze w , ih) i
  76. lift φ (inj₂ (inj₂ s′) , k′) i = inn (s′ , λ p′ → proj₂ (k′ p′) i)
  77. weaken : ∀ {Σ Σ′ Σ″ Σ‴ X Y I} → HOMO Σ′ X I Σ″ Y → Σ ⇒^C Σ′ → Σ″ ⇒^C Σ‴ →
  78. HOMO Σ X I Σ‴ Y
  79. weaken {Σ}{Σ′}{Σ″}{Σ‴}{X}{Y} φ f g (s , k) i = w‴
  80. where
  81. w : Σ ⋆^C X
  82. w = sup (s , proj₁ ∘ k)
  83. w′ : Σ′ ⋆^C X
  84. w′ = embed f w
  85. w″ : Σ″ ⋆^C Y
  86. w″ = rec φ w′ i
  87. w‴ : Σ‴ ⋆^C Y
  88. w‴ = embed g w″
  89. ⌈_⌉^HOMO : ∀ {Σ Σ′ X Y I} → PHOMO Σ X I Σ′ Y → HOMO Σ X I Σ′ Y
  90. ⌈ φ ⌉^HOMO (inj₁ x , _) = φ (inj₁ x , ⊥-elim ∘ Level.lower)
  91. ⌈ φ ⌉^HOMO (inj₂ s , k) = φ (inj₂ s , Prod.map inl id ∘ k)
  92. RUN : ∀ {Σ Σ′ Σ″ Σ‴ X Y I} → PHOMO Σ′ X I Σ″ Y → Σ ⇒^C (Σ′ ⊎^C Σ″) → Σ″ ⇒^C Σ‴ →
  93. Σ ⋆^C X → I → Σ‴ ⋆^C Y
  94. RUN φ p q = rec (weaken (⌈ lift φ ⌉^HOMO) p q)
  95. \end{code}