RunCompat.lagda 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. \subsection{Compatibility layer between containers and signatures}
  2. \label{run-compat}
  3. In section \ref{run} we developed a library for working with effects in
  4. free monads of containers. Since signatures (list of pairs of types) are
  5. used in our langauge rather than containers directly, we will now give a
  6. compatibility layer between containers and signatures.
  7. \AgdaHide{
  8. \begin{code}
  9. {-# OPTIONS --sized-types #-}
  10. module Issue854.RunCompat where
  11. open import Level
  12. open import Function
  13. open import Function.Equivalence using (_⇔_)
  14. open import Data.Empty
  15. open import Data.Sum
  16. open import Data.Product as Prod
  17. open import Data.List using (List; []; _∷_; _++_)
  18. open import Data.List.Relation.Unary.Any
  19. open import Data.Container as Cont hiding (refl; _∈_)
  20. renaming (⟦_⟧ to ⟦_⟧^C; μ to μ^C; _⇒_ to _⇒^C_)
  21. open import Data.Container.Combinator using () renaming (_⊎_ to _⊎^C_)
  22. open import Data.Container.FreeMonad
  23. renaming (_⋆_ to _⋆^C_; _⋆C_ to _⋆^CC_)
  24. open import Data.W
  25. open import Relation.Binary
  26. open import Relation.Binary.PropositionalEquality as PE hiding ([_])
  27. open import Data.List.Relation.Binary.Subset.Propositional
  28. open import Issue854.TypesSemantics using (Sh; Pos; ⌊_⌋^Sig; sh; ar)
  29. open import Issue854.Run using (_⋆^S_; embed)
  30. \end{code}
  31. }
  32. \begin{code}
  33. ⊆→⇒ : ∀ {Σ Σ′} → Σ ⊆ Σ′ → ⌊ Σ ⌋^Sig ⇒^C ⌊ Σ′ ⌋^Sig
  34. ⊆→⇒ σ = record
  35. { shape = shape′ σ
  36. ; position = λ {s} → pos σ s
  37. }
  38. where
  39. shape′ : ∀ {Σ Σ′} → Σ ⊆ Σ′ → Sh Σ → Sh Σ′
  40. shape′ {[]} _ ()
  41. shape′ {_ ∷ _} σ (inj₁ p) = sh (σ (here refl)) p
  42. shape′ {_ ∷ _} σ (inj₂ s) = shape′ (σ ∘ there) s
  43. pos : ∀ {Σ Σ′}(σ : Σ ⊆ Σ′) s → Pos Σ′ (shape′ σ s) → Pos Σ s
  44. pos {[]} σ () _
  45. pos {_ ∷ _} σ (inj₁ par) p = ar (σ (here refl)) par p
  46. pos {_ ∷ _} σ (inj₂ s) p = pos (σ ∘ there) s p
  47. \end{code}
  48. \begin{code}
  49. ⇒++→⇒⊎ : ∀ {Σ Σ′ Σ″} → ⌊ Σ ⌋^Sig ⇒^C ⌊ Σ′ ++ Σ″ ⌋^Sig →
  50. ⌊ Σ ⌋^Sig ⇒^C (⌊ Σ′ ⌋^Sig ⊎^C ⌊ Σ″ ⌋^Sig)
  51. ⇒++→⇒⊎ m = m′ Morphism.∘ m
  52. where
  53. m′ : ∀ {Σ Σ′} → ⌊ Σ ++ Σ′ ⌋^Sig ⇒^C (⌊ Σ ⌋^Sig ⊎^C ⌊ Σ′ ⌋^Sig)
  54. m′ {Σ} = record
  55. { shape = shape′
  56. ; position = λ {s} → pos {Σ} s
  57. }
  58. where
  59. shape′ : ∀ {Σ Σ′} → Sh (Σ ++ Σ′) → Sh Σ ⊎ Sh Σ′
  60. shape′ {[]} s′ = inj₂ s′
  61. shape′ {_ ∷ _} (inj₁ p) = inj₁ (inj₁ p)
  62. shape′ {_ ∷ Σ} (inj₂ ss′) = [ inj₁ ∘ inj₂ , inj₂ ]′ (shape′ {Σ} ss′)
  63. pos : ∀ {Σ Σ′} s → Position (⌊ Σ ⌋^Sig ⊎^C ⌊ Σ′ ⌋^Sig) (shape′ s) →
  64. Position ⌊ Σ ++ Σ′ ⌋^Sig s
  65. pos {[]} _ p′ = p′
  66. pos {_ ∷ _} (inj₁ _) a = a
  67. pos {_ ∷ Σ} (inj₂ ss′) p = pos {Σ} ss′ (lemma {Σ} ss′ p)
  68. where
  69. lemma : ∀ {Σ Σ′ ω} ss′ →
  70. [ Pos (ω ∷ Σ) , Pos Σ′ ]′ ([ inj₁ ∘ inj₂ , inj₂ ]′ (shape′ ss′)) →
  71. [ Pos Σ , Pos Σ′ ]′ (shape′ ss′)
  72. lemma {[]} _ p′ = p′
  73. lemma {_ ∷ Σ} (inj₁ p) a = a
  74. lemma {_ ∷ Σ} (inj₂ ss′) _ with shape′ {Σ} ss′
  75. lemma {_ ∷ Σ} (inj₂ _) p | inj₁ _ = p
  76. lemma {_ ∷ Σ} (inj₂ _) p′ | inj₂ _ = p′
  77. \end{code}
  78. \begin{code}
  79. ⊎⋆→++⋆ : ∀ {Σ Σ′ X} → (⌊ Σ ⌋^Sig ⊎^C ⌊ Σ′ ⌋^Sig) ⋆^C X → (Σ ++ Σ′) ⋆^S X
  80. ⊎⋆→++⋆ = embed m
  81. where
  82. m : ∀ {Σ Σ′} → (⌊ Σ ⌋^Sig ⊎^C ⌊ Σ′ ⌋^Sig) ⇒^C ⌊ Σ ++ Σ′ ⌋^Sig
  83. m {Σ} = record
  84. { shape = [ sh-inl , sh-inr {Σ} ]′
  85. ; position = λ { {inj₁ s} pp′ → pos-inl s pp′
  86. ; {inj₂ s′} pp′ → pos-inr {Σ} s′ pp′
  87. }
  88. }
  89. where
  90. sh-inl : ∀ {Σ Σ′} → Sh Σ → Sh (Σ ++ Σ′)
  91. sh-inl {[]} ()
  92. sh-inl {_ ∷ _} (inj₁ p) = inj₁ p
  93. sh-inl {_ ∷ _} (inj₂ s) = inj₂ (sh-inl s)
  94. sh-inr : ∀ {Σ Σ′} → Sh Σ′ → Sh (Σ ++ Σ′)
  95. sh-inr {[]} s′ = s′
  96. sh-inr {_ ∷ Σ} s′ = inj₂ (sh-inr {Σ} s′)
  97. pos-inl : ∀ {Σ Σ′} s → Pos (Σ ++ Σ′) (sh-inl s) → Pos Σ s
  98. pos-inl {[]} () _
  99. pos-inl {_ ∷ _} (inj₁ _) a = a
  100. pos-inl {_ ∷ _} (inj₂ s) p = pos-inl s p
  101. pos-inr : ∀ {Σ Σ′} s′ → Pos (Σ ++ Σ′) (sh-inr {Σ} s′) → Pos Σ′ s′
  102. pos-inr {[]} _ p′ = p′
  103. pos-inr {_ ∷ Σ} s′ p = pos-inr {Σ} s′ p
  104. \end{code}