Dissect.agda 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. module Dissect where
  2. import Functor
  3. import Sets
  4. import Isomorphism
  5. open Sets
  6. open Functor
  7. open Functor.Semantics
  8. open Functor.Recursive
  9. infixr 40 _+₂_
  10. infixr 60 _×₂_
  11. ∇ : U -> U₂
  12. ∇ (K A) = K₂ [0]
  13. ∇ Id = K₂ [1]
  14. ∇ (F + G) = ∇ F +₂ ∇ G
  15. ∇ (F × G) = ∇ F ×₂ ↗ G +₂ ↖ F ×₂ ∇ G
  16. diagonal : U₂ -> U
  17. diagonal (K₂ A) = K A
  18. diagonal (↖ F) = F
  19. diagonal (↗ F) = F
  20. diagonal (F +₂ G) = diagonal F + diagonal G
  21. diagonal (F ×₂ G) = diagonal F × diagonal G
  22. module Derivative where
  23. import Derivative as D
  24. ∂ : U -> U
  25. ∂ F = diagonal (∇ F)
  26. open Isomorphism
  27. same : (F : U)(X : Set) -> ⟦ ∂ F ⟧ X ≅ ⟦ D.∂ F ⟧ X
  28. same (K A) X = refl-≅ [0]
  29. same Id X = refl-≅ [1]
  30. same (F + G) X = iso[+] (same F X) (same G X)
  31. same (F × G) X = iso[+] (iso[×] (same F X) (refl-≅ _))
  32. (iso[×] (refl-≅ _) (same G X))
  33. Stack : (F : U) -> Set -> Set -> Set
  34. Stack F C J = List (⟦ ∇ F ⟧₂ C J)
  35. NextJoker : U -> Set -> Set -> Set
  36. NextJoker F C J = J [×] ⟦ ∇ F ⟧₂ C J [+] ⟦ F ⟧ C
  37. mutual
  38. into : (F : U){C J : Set} -> ⟦ F ⟧ J -> NextJoker F C J
  39. into (K A) a = inr a
  40. into Id x = inl < x , <> >
  41. into (F + G) (inl f) = (id <×> inl <+> inl) (into F f)
  42. into (F + G) (inr g) = (id <×> inr <+> inr) (into G g)
  43. into (F × G) < fj , gj > = tryL F G (into F fj) gj
  44. next : (F : U){C J : Set} -> ⟦ ∇ F ⟧₂ C J -> C -> NextJoker F C J
  45. next (K A) () _
  46. next Id <> c = inr c
  47. next (F + G) (inl f') c = (id <×> inl <+> inl) (next F f' c)
  48. next (F + G) (inr g') c = (id <×> inr <+> inr) (next G g' c)
  49. next (F × G) (inl < f' , gj >) c = tryL F G (next F f' c) gj
  50. next (F × G) (inr < fc , g' >) c = tryR F G fc (next G g' c)
  51. tryL : (F G : U){C J : Set} ->
  52. NextJoker F C J -> ⟦ G ⟧ J -> NextJoker (F × G) C J
  53. tryL F G (inl < j , f' >) gj = inl < j , inl < f' , gj > >
  54. tryL F G (inr fc) gj = tryR F G fc (into G gj)
  55. tryR : (F G : U){C J : Set} ->
  56. ⟦ F ⟧ C -> NextJoker G C J -> NextJoker (F × G) C J
  57. tryR F G fc (inl < j , g' >) = inl < j , inr < fc , g' > >
  58. tryR F G fc (inr gc) = inr < fc , gc >
  59. map : (F : U){C J : Set} -> (J -> C) -> ⟦ F ⟧ J -> ⟦ F ⟧ C
  60. map F φ f = iter (into F f) where
  61. iter : NextJoker F _ _ -> ⟦ F ⟧ _
  62. iter (inl < j , d >) = iter (next F d (φ j))
  63. iter (inr f) = f
  64. fold : (F : U){T : Set} -> (⟦ F ⟧ T -> T) -> μ F -> T
  65. fold F {T} φ r = inward r [] where
  66. mutual
  67. inward : μ F -> Stack F T (μ F) -> T
  68. inward (inn f) γ = onward (into F f) γ
  69. outward : T -> Stack F T (μ F) -> T
  70. outward t [] = t
  71. outward t (f' :: γ) = onward (next F f' t) γ
  72. onward : NextJoker F T (μ F) -> Stack F T (μ F) -> T
  73. onward (inl < r , f' >) γ = inward r (f' :: γ)
  74. onward (inr t) γ = outward (φ t) γ
  75. -- can we make a non-tail recursive fold?
  76. -- of course, nothing could be simpler: (not structurally recursive though)
  77. fold' : (F : U){T : Set} -> (⟦ F ⟧ T -> T) -> μ F -> T
  78. fold' F φ = φ ∘ map F (fold' F φ) ∘ out
  79. -- Fold operators
  80. Φ : (F : U) -> Set -> Set
  81. Φ (K A) T = A -> T
  82. Φ Id T = T -> T
  83. Φ (F + G) T = Φ F T [×] Φ G T
  84. Φ (F × G) T = (T -> T -> T) [×] (Φ F T [×] Φ G T)
  85. mkφ : (F : U){T : Set} -> Φ F T -> ⟦ F ⟧ T -> T
  86. mkφ (K A) f a = f a
  87. mkφ Id f t = f t
  88. mkφ (F + G) < φf , φg > (inl f) = mkφ F φf f
  89. mkφ (F + G) < φf , φg > (inr g) = mkφ G φg g
  90. mkφ (F × G) < _○_ , < φf , φg > > < f , g > = mkφ F φf f ○ mkφ G φg g