Examples.lagda 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. \subsection{Examples}
  2. \label{examples}
  3. \AgdaHide{
  4. \begin{code}
  5. {-# OPTIONS --sized-types #-}
  6. module Issue854.Examples where
  7. open import Function.Base
  8. open import Function.Inverse using (module Inverse)
  9. open import Data.Unit
  10. open import Data.Product
  11. open import Data.List
  12. open import Data.List.Membership.Propositional
  13. open import Data.List.Relation.Unary.Any
  14. open import Data.Container.FreeMonad using (rawMonad)
  15. open import Relation.Binary.PropositionalEquality
  16. open import Data.List.Relation.Binary.Pointwise hiding (refl)
  17. open import Data.List.Relation.Binary.Subset.Propositional
  18. open import Data.List.Relation.Binary.Subset.Propositional.Properties using (xs⊆xs++ys)
  19. open import Category.Monad
  20. open import Issue854.Types
  21. open import Issue854.Context
  22. open import Issue854.WellTyped
  23. open import Issue854.WellTypedSemantics
  24. \end{code}
  25. }
  26. \begin{code}
  27. ′N : Sig
  28. ′N = (𝟙 , 𝟘) ∷ (𝟙 , 𝟙) ∷ []
  29. N = μ ′N
  30. ze : ∀ {Γ} → Γ ⊢^v _ ∶ N
  31. ze = con (here refl) ⟨⟩ (𝟘-elim (var zero))
  32. pattern su n = con (there (here refl)) ⟨⟩ n
  33. #0 #1 #2 #3 #4 #5 : ∀ {Γ} → Γ ⊢^v _ ∶ N
  34. #0 = ze
  35. #1 = su #0
  36. #2 = su #1
  37. #3 = su #2
  38. #4 = su #3
  39. #5 = su #4
  40. _⊙_ : ∀ {Γ Σ U V f a} → Γ ▻ U ⊢^v f ∶ V → Γ ⊢^c a ∶ Σ ⋆ U → Γ ⊢^c _ ∶ Σ ⋆ V
  41. f ⊙ a = _to_ a (return f) id id
  42. plus : ∀ {Γ Σ} → Γ ⊢^c _ ∶ N ⇒ N ⇒ Σ ⋆ N
  43. plus = ƛ {-m-} ƛ {-n-} _⊢^c_∶_.iter φ (var {-m-} (suc zero))
  44. where
  45. φ : _ ⊢^cs _ ∶ Alg ′N (_ ⋆ N)
  46. φ = ƛ ƛ return (var {-n-} (suc (suc zero))) ∷
  47. ƛ ƛ (su′ ⊙ ih) ∷
  48. []
  49. -- XXX: Not indented properly...
  50. where
  51. su′ : _ ▻ N ⊢^v _ ∶ N
  52. su′ = con (there (here refl)) ⟨⟩ (var (suc zero))
  53. ih : _ ⊢^c _ ∶ _ ⋆ N
  54. ih = force (var zero) · ⟨⟩
  55. -- test-plus : ⟦ plus {[]}{[]} · #2 · #1 ⟧^c tt ≡ ⟦ return #3 ⟧^c tt
  56. -- test-plus = refl
  57. State : VType → Sig
  58. State S = (𝟙 , S) ∷ (S , 𝟙) ∷ []
  59. -- XXX: get : {m : True (𝟙 , S) ∈? Σ)} → Σ ⋆ S?
  60. state^suc : ∀ {Γ} → Γ ⊢^c _ ∶ State N ⋆ 𝟙
  61. state^suc {Γ} = _to_ (op get · ⟨⟩) (op put · su′) id id
  62. where
  63. get = here refl
  64. put = there (here refl)
  65. su′ : Γ ▻ N ⊢^v _ ∶ N
  66. su′ = con (there (here refl)) ⟨⟩ (var (suc zero))
  67. state^Homo : ∀ {Γ Σ S X} → Γ ⊢^cs _ ∶ PHomo (State S) X S Σ (X ⊗ S)
  68. state^Homo =
  69. ƛ ƛ ƛ (((π₂ (force (var (suc zero)) · var zero)) · var zero)) ∷
  70. ƛ ƛ ƛ ((π₂ (force (var (suc zero)) · ⟨⟩)) · var (suc (suc zero))) ∷
  71. ƛ ƛ return (var (suc zero) , var zero) ∷ []
  72. ex-state : [] ⊢^c _ ∶ [] ⋆ (𝟙 ⊗ N)
  73. ex-state = run {Σ′ = State N}{[]} state^Homo state^suc (xs⊆xs++ys _ _) id · #0
  74. test-state : ⟦ ex-state ⟧^c tt ≡ ⟦ return (⟨⟩ , #1) ⟧^c tt
  75. test-state = refl
  76. \end{code}
  77. The booleans.
  78. \begin{code}
  79. ′𝟚 : Sig
  80. ′𝟚 = (𝟙 , 𝟘) ∷ (𝟙 , 𝟘) ∷ []
  81. 𝟚 = μ ′𝟚
  82. tru fal : ∀ {Γ} → Γ ⊢^v _ ∶ 𝟚
  83. tru = con (here refl) ⟨⟩ (𝟘-elim (var zero))
  84. fal = con (there (here refl)) ⟨⟩ (𝟘-elim (var zero))
  85. cond : ∀ {Γ Σ V} → Γ ⊢^c _ ∶ 𝟚 ⇒ V ⇒ V ⇒ Σ ⋆ V
  86. cond {Γ}{Σ}{T} = ƛ {-b-} ƛ {-t-} ƛ {-f-} iter φ
  87. (var {-b-} (suc (suc zero)))
  88. where
  89. φ : Γ ▻ 𝟚 ▻ T ▻ T ⊢^cs _ ∶ Alg ′𝟚 (Σ ⋆ T)
  90. φ = ƛ ƛ return (var {-t-} (suc (suc (suc zero)))) ∷
  91. ƛ ƛ return (var {-f-} (suc (suc zero))) ∷ []
  92. if : ∀ {Γ C} → Γ ⊢^c _ ∶ 𝟚 ⇒ ⁅ C ⁆ ⇒ ⁅ C ⁆ ⇒ C
  93. if {Γ}{C} = ƛ {-b-} ƛ {-t-} ƛ {-f-} iter φ
  94. (var {-b-} (suc (suc zero)))
  95. where
  96. φ : Γ ▻ 𝟚 ▻ ⁅ C ⁆ ▻ ⁅ C ⁆ ⊢^cs _ ∶ Alg ′𝟚 C
  97. φ = ƛ ƛ force (var {-t-} (suc (suc (suc zero)))) ∷
  98. ƛ ƛ force (var {-f-} (suc (suc zero))) ∷ []
  99. \end{code}
  100. \begin{code}
  101. not : ∀ {Γ Σ} → Γ ⊢^c _ ∶ 𝟚 ⇒ Σ ⋆ 𝟚
  102. not = ƛ (cond · var zero · fal · tru)
  103. \end{code}
  104. Possibly aborting computations.
  105. \begin{code}
  106. Abort : Sig
  107. Abort = (𝟙 , 𝟘) ∷ []
  108. aborting : ∀ {Γ V} → Γ ⊢^c _ ∶ Abort ⋆ V
  109. aborting = _to_ (op (here refl) · ⟨⟩)
  110. (return (𝟘-elim (var zero))) id id
  111. \end{code}
  112. \begin{code}
  113. -- postulate
  114. -- weaken^C : ∀ {Γ V C c} → Γ ⊢^c c ∶ C → Γ ▻ V ⊢^c c ∶ C
  115. --
  116. -- _<_·_>_ : ∀ {Γ Σ Σ′ Σ″ U V f a} → Γ ⊢^c f ∶ U ⇒ Σ ⋆ V → Σ ⊆ Σ″ → Σ′ ⊆ Σ″ →
  117. -- Γ ⊢^c a ∶ Σ′ ⋆ U → Γ ⊢^c _ ∶ Σ″ ⋆ V
  118. -- f < p · q > a = _to_ a (weaken^C f · var zero) q p
  119. --
  120. -- put-abort : ∀ {Γ S} → Γ ⊢^c _ ∶ (State S ++ Abort) ⋆ 𝟙
  121. -- put-abort {S = S} = op put < xs⊆xs++ys · xs⊆ys++xs {xs = State S} > aborting
  122. -- -- (aborting to (op put · var zero)) (inr′ {xs = State S}) inl′
  123. -- where
  124. -- put : (S , 𝟙) ∈ State S
  125. -- put = there (here refl)
  126. \end{code}
  127. \begin{code}
  128. Maybe : VType → VType
  129. Maybe X = μ ((𝟙 , 𝟘) ∷ (X , 𝟘) ∷ [])
  130. jus : ∀ {Γ V v} → Γ ⊢^v v ∶ V → Γ ⊢^v _ ∶ Maybe V
  131. jus t = con (there (here refl)) t (𝟘-elim (var zero))
  132. nothin : ∀ {Γ V} → Γ ⊢^v _ ∶ Maybe V
  133. nothin = con (here refl) ⟨⟩ (𝟘-elim (var zero))
  134. \end{code}
  135. \begin{code}
  136. abort^Homo : ∀ {Γ Σ X} → Γ ⊢^cs _ ∶ PHomo Abort X 𝟙 Σ (Maybe X)
  137. abort^Homo = ƛ ƛ ƛ return nothin ∷
  138. ƛ ƛ return (jus (var (suc zero))) ∷ []
  139. \end{code}
  140. \begin{code}
  141. -- if get then put false else aborting
  142. ex : ∀ {Γ} → Γ ⊢^c _ ∶ (State 𝟚 ++ Abort) ⋆ 𝟙
  143. ex = _to_ (op get · ⟨⟩)
  144. (if · var zero · thunk (op put · fal) · thunk aborting′) id id
  145. where
  146. get : (𝟙 , 𝟚) ∈ (State 𝟚 ++ Abort)
  147. get = here refl
  148. put : (𝟚 , 𝟙) ∈ (State 𝟚 ++ Abort)
  149. put = there (here refl)
  150. aborting′ : ∀ {Γ V} → Γ ⊢^c _ ∶ (State 𝟚 ++ Abort) ⋆ V
  151. aborting′ = _to_ (op (there (there (here refl))) · ⟨⟩)
  152. (return (𝟘-elim (var zero))) id id
  153. \end{code}
  154. \begin{code}
  155. ex-state′ : ∀ {Γ} → Γ ⊢^c _ ∶ 𝟚 ⇒ Abort ⋆ (𝟙 ⊗ 𝟚)
  156. ex-state′ = run {Σ′ = State 𝟚} state^Homo ex id id
  157. ++-comm : ∀ {a}{A : Set a} xs {ys : List A} → xs ++ ys ⊆ ys ++ xs
  158. ++-comm xs m = to (++↔++ xs _) ⟨$⟩ m
  159. where
  160. open import Data.List.Relation.Unary.Any.Properties
  161. open import Function.Inverse
  162. open import Function.Equality
  163. open Inverse
  164. ex-abort : ∀ {Γ} → Γ ⊢^c _ ∶ 𝟙 ⇒ State 𝟚 ⋆ Maybe 𝟙
  165. ex-abort = run {Σ′ = Abort} abort^Homo ex (++-comm (State 𝟚)) id
  166. \end{code}
  167. \begin{code}
  168. ex-state-abort : ∀ {Γ} → Γ ⊢^c _ ∶ 𝟚 ⇒ [] ⋆ Maybe (𝟙 ⊗ 𝟚)
  169. ex-state-abort = ƛ (run {Σ′ = Abort} abort^Homo
  170. (ex-state′ · var zero) id id · ⟨⟩)
  171. ex-abort-state : ∀ {Γ} → Γ ⊢^c _ ∶ 𝟚 ⇒ [] ⋆ (Maybe 𝟙 ⊗ 𝟚)
  172. ex-abort-state = ƛ (run {Σ′ = State 𝟚} state^Homo
  173. (ex-abort · ⟨⟩) id id · var zero)
  174. test-state-abort : ⟦ ex-state-abort · tru ⟧^c tt ≡
  175. ⟦ return (jus (⟨⟩ , fal)) ⟧^c tt
  176. test-state-abort = refl
  177. test-abort-state : ⟦ ex-abort-state · tru ⟧^c tt ≡
  178. ⟦ return (jus ⟨⟩ , fal) ⟧^c tt
  179. test-abort-state = refl -- refl
  180. \end{code}