ChainRule.agda 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. module ChainRule where
  2. import Sets
  3. import Functor
  4. import Logic.ChainReasoning.Poly as CR
  5. import Isomorphism
  6. import Derivative
  7. open Derivative
  8. open Sets
  9. open Functor
  10. open Semantics
  11. open Isomorphism
  12. module Chain = CR _==_ (\x -> refl{x = x}) (\x y z -> trans{x = x}{y}{z})
  13. open Chain
  14. chain-rule : (F G : U)(X : Set) -> ⟦ ∂ (F [ G ]) ⟧ X ≅ ⟦ ∂ F [ G ] × ∂ G ⟧ X
  15. chain-rule F G X = iso (i F) (j F) (ji F) (ij F)
  16. where
  17. i : (F : U) -> ⟦ ∂ (F [ G ]) ⟧ X -> ⟦ ∂ F [ G ] × ∂ G ⟧ X
  18. i (K A) ()
  19. i Id x = < <> , x >
  20. i (F₁ + F₂) (inl c) = (inl <×> id) (i F₁ c)
  21. i (F₁ + F₂) (inr c) = (inr <×> id) (i F₂ c)
  22. i (F₁ × F₂) (inl < c , f₂ >) = (inl ∘ <∙, f₂ > <×> id) (i F₁ c)
  23. i (F₁ × F₂) (inr < f₁ , c >) = (inr ∘ < f₁ ,∙> <×> id) (i F₂ c)
  24. j : (F : U) -> ⟦ ∂ F [ G ] × ∂ G ⟧ X -> ⟦ ∂ (F [ G ]) ⟧ X
  25. j (K A) < () , _ >
  26. j Id < <> , x > = x
  27. j (F₁ + F₂) < inl x , y > = inl (j F₁ < x , y >)
  28. j (F₁ + F₂) < inr x , y > = inr (j F₂ < x , y >)
  29. j (F₁ × F₂) < inl < x , y > , z > = inl < j F₁ < x , z > , y >
  30. j (F₁ × F₂) < inr < x , y > , z > = inr < x , j F₂ < y , z > >
  31. ij : (F : U)(x : _) -> i F (j F x) == x
  32. ij (K A) < () , _ >
  33. ij Id < <> , x > = refl
  34. ij (F₁ + F₂) < lx@(inl x) , y > =
  35. subst (\ ∙ -> (inl <×> id) ∙ == < lx , y >)
  36. (ij F₁ < x , y >) refl
  37. ij (F₁ + F₂) < rx@(inr x) , y > =
  38. subst (\ ∙ -> (inr <×> id) ∙ == < rx , y >)
  39. (ij F₂ < x , y >) refl
  40. ij (F₁ × F₂) < xy@(inl < x , y >) , z > =
  41. subst (\ ∙ -> (inl ∘ <∙, y > <×> id) ∙ == < xy , z >)
  42. (ij F₁ < x , z >) refl
  43. ij (F₁ × F₂) < xy@(inr < x , y >) , z > =
  44. subst (\ ∙ -> (inr ∘ < x ,∙> <×> id) ∙ == < xy , z >)
  45. (ij F₂ < y , z >) refl
  46. ji : (F : U)(y : _) -> j F (i F y) == y
  47. ji (K A) ()
  48. ji Id x = refl
  49. ji (F₁ + F₂) (inl c) =
  50. chain> j (F₁ + F₂) ((inl <×> id) (i F₁ c))
  51. === inl (j F₁ _) by cong (j (F₁ + F₂) ∘ (inl <×> id)) (η-[×] (i F₁ c))
  52. === inl (j F₁ (i F₁ c)) by cong (inl ∘ j F₁) (sym $ η-[×] (i F₁ c))
  53. === inl c by cong inl (ji F₁ c)
  54. ji (F₁ + F₂) rc @ (inr c) =
  55. subst (\ ∙ -> j (F₁ + F₂) ((inr <×> id) ∙) == rc) (η-[×] (i F₂ c))
  56. $ subst (\ ∙ -> inr (j F₂ ∙) == rc) (sym $ η-[×] (i F₂ c))
  57. $ subst (\ ∙ -> inr ∙ == rc) (ji F₂ c) refl
  58. ji (F₁ × F₂) l @ (inl < c , f₂ >) =
  59. subst (\ ∙ -> j (F₁ × F₂) ((inl ∘ <∙, f₂ > <×> id) ∙) == l) (η-[×] (i F₁ c))
  60. $ subst (\ ∙ -> inl < j F₁ ∙ , f₂ > == l) (sym $ η-[×] (i F₁ c))
  61. $ subst (\ ∙ -> inl < ∙ , f₂ > == l) (ji F₁ c) refl
  62. ji (F₁ × F₂) r @ (inr < f₁ , c >) =
  63. subst (\ ∙ -> j (F₁ × F₂) ((inr ∘ < f₁ ,∙> <×> id) ∙) == r) (η-[×] (i F₂ c))
  64. $ subst (\ ∙ -> inr < f₁ , j F₂ ∙ > == r) (sym $ η-[×] (i F₂ c))
  65. $ subst (\ ∙ -> inr < f₁ , ∙ > == r) (ji F₂ c) refl