Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. def ran (g h : Type → Type) (α : Type) := Π β, (α → g β) → h β
  2. def lan (g h : Type → Type) (α : Type) := Σ β, (g β → α) × h β
  3.  
  4. def mapr {g h} {α β} (s : α → β) (x : ran g h α) : ran g h β :=
  5. λ b t, x b (t ∘ s)
  6.  
  7. def mapl {g h} {α β} (s : α → β) (x : lan g h α) : lan g h β :=
  8. ⟨x.1, ⟨s ∘ x.2.1, x.2.2⟩⟩
  9.  
  10. attribute [reducible] id
  11. attribute [simp] function.comp
  12.  
  13. -- @[simp] lemma prod.mk.eta {α β} : Π {p : α × β}, (p.1, p.2) = p
  14. -- | (a, b) := rfl
  15.  
  16. @[simp] lemma sigma.mk.eta {α} {β : α → Type} : Π {p : Σ α, β α}, sigma.mk p.1 p.2 = p
  17. | ⟨a, b⟩ := rfl
  18.  
  19. instance {g h} : functor (ran g h) :=
  20. { map := @mapr g h }
  21.  
  22. instance {g h} : is_lawful_functor (ran g h) :=
  23. { id_map := by intros; simp [functor.map, mapr],
  24. comp_map := by intros; simp [functor.map, mapr] }
  25.  
  26. instance {g h} : functor (lan g h) :=
  27. { map := @mapl g h }
  28.  
  29. instance {g h} : is_lawful_functor (lan g h) :=
  30. { id_map := by intros; simp [functor.map, mapl],
  31. comp_map := by intros; simp [functor.map, mapl] }
  32.  
  33. def natural {f g} [functor f] [functor g] (t : Π {α}, f α → g α) :=
  34. Π {α β} {x : f α} (s : α → β), s <$> (t x) = t (s <$> x)
  35.  
  36. axiom free_theorem {f g} [functor f] [functor g] (t : Π α, f α → g α) : natural t
  37.  
  38. section yoneda
  39. variables {f : Type → Type} [functor f] [is_lawful_functor f] {α : Type}
  40.  
  41. @[reducible] def yoneda := ran id
  42.  
  43. def check (x : f α) : yoneda f α :=
  44. λ b s, s <$> x
  45.  
  46. def uncheck (x : yoneda f α) : f α :=
  47. x α id
  48.  
  49. def uncheck_check (x : f α) : uncheck (check x) = x :=
  50. begin
  51. simp [check, uncheck, is_lawful_functor.id_map]
  52. end
  53.  
  54. def check_uncheck (x : yoneda f α) : check (uncheck x) = x :=
  55. begin
  56. simp [check, uncheck],
  57. funext,
  58. apply free_theorem (@uncheck f _ _)
  59. end
  60.  
  61. @[reducible] def coyoneda := lan id
  62.  
  63. def cocheck (x : f α) : coyoneda f α :=
  64. ⟨α, ⟨id, x⟩⟩
  65.  
  66. def councheck (x : coyoneda f α) : f α :=
  67. x.2.1 <$> x.2.2
  68.  
  69. def councheck_cocheck (x : f α) : councheck (cocheck x) = x :=
  70. begin
  71. simp [cocheck, councheck, is_lawful_functor.id_map]
  72. end
  73.  
  74. def cocheck_councheck (x : coyoneda f α) : cocheck (councheck x) = x :=
  75. begin
  76. simp [councheck],
  77. rw ←free_theorem (@cocheck f _ _),
  78. simp [cocheck, functor.map, mapl]
  79. end
  80. end yoneda
  81.  
  82. structure {u v} iso (α : Type u) (β : Type v) :=
  83. (f : α → β) (g : β → α) (gf : Π x, g (f x) = x) (fg : Π x, f (g x) = x)
  84.  
  85. namespace iso
  86. def inv {α β} (i : iso α β) : iso β α :=
  87. ⟨i.g, i.f, i.fg, i.gf⟩
  88.  
  89. def comp {α β γ} (i : iso α β) (j : iso β γ) : iso α γ :=
  90. ⟨j.f ∘ i.f, i.g ∘ j.g, by simp [j.gf, i.gf], by simp [i.fg, j.fg]⟩
  91.  
  92. universes u v w
  93. variables {f : Type u → Type v} {g : Type u → Type w} (i : Π {α}, iso (f α) (g α))
  94.  
  95. def imap [functor f] {α β : Type u} (s : α → β) (x : g α) : g β :=
  96. i.f $ s <$> i.g x
  97.  
  98. def ipure [applicative f] {α : Type u} (x : α) : g α :=
  99. i.f $ pure x
  100.  
  101. def iseq [applicative f] {α β : Type u} (s : g (α → β)) (x : g α) : g β :=
  102. i.f $ i.g s <*> i.g x
  103.  
  104. def ibind [monad f] {α β : Type u} (x : g α) (s : α → g β) : g β :=
  105. i.f $ i.g x >>= i.g ∘ s
  106.  
  107. @[priority std.priority.default-1]
  108. instance functor [functor f] : functor g :=
  109. { map := @imap f g @i _ }
  110.  
  111. -- @[priority std.priority.default-1]
  112. -- instance is_lawful_functor [functor f] : is_lawful_functor g :=
  113. -- { id_map :=
  114. -- begin
  115. -- intros,
  116. -- simp [imap, is_lawful_functor.id_map, i.fg]
  117. -- end,
  118. -- comp_map :=
  119. -- begin
  120. -- intros,
  121. -- simp [imap, i.gf],
  122. -- rw is_lawful_functor.comp_map
  123. -- end }
  124.  
  125. @[priority std.priority.default-1]
  126. instance applicative [applicative f] : applicative g :=
  127. { pure := @ipure f g @i _,
  128. seq := @iseq f g @i _ }
  129.  
  130. -- @[priority std.priority.default-1]
  131. -- instance is_lawful_applicative [is_lawful_applicative f] : is_lawful_applicative g :=
  132. -- { pure_seq_eq_map := by intros; simp,
  133. -- id_map :=
  134. -- begin
  135. -- intros,
  136. -- simp [ipure, iseq, i.gf],
  137. -- simp [is_lawful_applicative.pure_seq_eq_map, is_lawful_functor.id_map, i.fg]
  138. -- end,
  139. -- map_pure :=
  140. -- begin
  141. -- intros,
  142. -- simp [ipure, iseq, i.gf],
  143. -- simp [is_lawful_applicative.pure_seq_eq_map, is_lawful_applicative.map_pure]
  144. -- end,
  145. -- seq_pure :=
  146. -- begin
  147. -- intros,
  148. -- simp [ipure, iseq, i.gf],
  149. -- simp [is_lawful_applicative.pure_seq_eq_map, is_lawful_applicative.seq_pure]
  150. -- end,
  151. -- seq_assoc :=
  152. -- begin
  153. -- intros,
  154. -- simp [ipure, iseq, i.gf],
  155. -- simp [is_lawful_applicative.pure_seq_eq_map, is_lawful_applicative.seq_assoc]
  156. -- end }
  157.  
  158. @[priority std.priority.default-1]
  159. instance monad [monad f] : monad g :=
  160. { pure := @ipure f g @i _,
  161. bind := @ibind f g @i _ }
  162.  
  163. -- @[priority std.priority.default-1]
  164. -- instance is_lawful_monad [is_lawful_monad f] : is_lawful_monad g :=
  165. -- { id_map :=
  166. -- begin
  167. -- intros,
  168. -- simp [ipure, ibind, i.gf],
  169. -- rw monad.bind_pure_comp_eq_map,
  170. -- simp [is_lawful_functor.id_map, i.fg]
  171. -- end,
  172. -- pure_bind :=
  173. -- begin
  174. -- intros,
  175. -- simp [ipure, ibind, i.gf],
  176. -- simp [is_lawful_monad.pure_bind, i.fg]
  177. -- end,
  178. -- bind_assoc :=
  179. -- begin
  180. -- intros,
  181. -- simp [ibind, i.gf],
  182. -- simp [is_lawful_monad.bind_assoc]
  183. -- end }
  184. end iso
  185.  
  186. section yoneda_iso
  187. variables (f : Type → Type) [functor f] [is_lawful_functor f] ⦃α : Type⦄
  188.  
  189. def yoneda_iso : iso (f α) (yoneda f α) :=
  190. ⟨check, uncheck, uncheck_check, check_uncheck⟩
  191.  
  192. def coyoneda_iso : iso (f α) (coyoneda f α) :=
  193. ⟨cocheck, councheck, councheck_cocheck, cocheck_councheck⟩
  194.  
  195. def yoco_iso : iso (yoneda f α) (coyoneda f α) :=
  196. (@yoneda_iso f _ _ α).inv.comp (@coyoneda_iso f _ _ α)
  197. end yoneda_iso
  198.  
  199. -- instance {f} [applicative f] : applicative (yoneda f) := iso.applicative (yoneda_iso f)
  200. -- instance {f} [applicative f] : applicative (coyoneda f) := iso.applicative (coyoneda_iso f)
  201. -- instance {f} [monad f] : monad (yoneda f) := iso.monad (yoneda_iso f)
  202. -- instance {f} [monad f] : monad (coyoneda f) := iso.monad (coyoneda_iso f)
  203.  
  204. section naturality_proofs
  205. variables {f : Type → Type} [functor f] [is_lawful_functor f]
  206.  
  207. def natural_check : natural (@check f _ _) :=
  208. begin
  209. unfold natural, intros,
  210. dsimp [check, functor.map, mapr],
  211. funext,
  212. rw is_lawful_functor.comp_map
  213. end
  214.  
  215. def natural_uncheck : natural (@uncheck f _ _) :=
  216. begin
  217. unfold natural, intros,
  218. dsimp [uncheck, functor.map, mapr],
  219. admit -- ← stuck here
  220. end
  221.  
  222. def natural_cocheck : natural (@cocheck f _ _) :=
  223. begin
  224. unfold natural, intros,
  225. dsimp [cocheck, functor.map, mapl],
  226. admit -- ← stuck here
  227. end
  228.  
  229. def natural_councheck : natural (@councheck f _ _) :=
  230. begin
  231. unfold natural, intros,
  232. dsimp [councheck, functor.map, mapl],
  233. rw ←is_lawful_functor.comp_map
  234. end
  235. end naturality_proofs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement