Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. Require Import Hask.Ltac.
  2. Require Import Hask.Control.Applicative.
  3. Require Import Hask.Control.Monad.
  4. Require Import Hask.Control.Monad.Cont.
  5. Require Import Data.Functor.Identity.
  6. Require Export Coq.Classes.Morphisms.
  7. Require Export Coq.Relations.Relation_Definitions.
  8.  
  9. Generalizable All Variables.
  10.  
  11. (** Isomorphisms *)
  12.  
  13. Definition isomorphic (X Y : Type) : Prop :=
  14. exists (f : X -> Y), exists (g : Y -> X),
  15. f \o g = id /\ g \o f = id.
  16.  
  17. Notation "X ≅ Y" := (isomorphic X Y) (at level 100).
  18.  
  19. Program Instance Iso_Equivalence : Equivalence isomorphic.
  20. Obligation 1.
  21. intro x.
  22. exists id.
  23. exists id.
  24. unfold id; firstorder.
  25. Qed.
  26. Obligation 2.
  27. firstorder.
  28. Qed.
  29. Obligation 3.
  30. intros x y z H1 H2.
  31. firstorder.
  32. exists (x1 \o x0).
  33. exists (x2 \o x3).
  34. rewrite comp_assoc.
  35. assert ((x1 \o x0) \o x2 = x1 \o (x0 \o x2)).
  36. rewrite comp_assoc.
  37. reflexivity.
  38. assert ((x2 \o x3) \o x1 = x2 \o (x3 \o x1)).
  39. rewrite comp_assoc.
  40. reflexivity.
  41. split.
  42. rewrite H3, H, <- H0.
  43. reflexivity.
  44. rewrite comp_assoc, H4, H2, <- H1.
  45. reflexivity.
  46. Qed.
  47.  
  48. Add Parametric Relation {A B : Type} : Type isomorphic
  49. reflexivity proved by (@Equivalence_Reflexive _ _ Iso_Equivalence)
  50. symmetry proved by (@Equivalence_Symmetric _ _ Iso_Equivalence)
  51. transitivity proved by (@Equivalence_Transitive _ _ Iso_Equivalence)
  52. as isomorphic_relation.
  53.  
  54. (** Adjoint Functors *)
  55.  
  56. Definition adjoint (f g : Type -> Type) : Prop :=
  57. forall a b, f a -> b ≅ a -> g b.
  58.  
  59. Notation "F ⊣ G" := (adjoint F G) (at level 100).
  60.  
  61. (** Representable Functors *)
  62.  
  63. Definition representable (f : Type -> Type) : Prop :=
  64. forall a, exists R, R -> a ≅ f a.
  65.  
  66. (** Identity *)
  67.  
  68. Require Import FunctionalExtensionality.
  69.  
  70. Lemma identity_iso : forall (A : Type), Identity A ≅ A.
  71. Proof. reflexivity. Qed.
  72.  
  73. Lemma Identity_representable : representable Identity.
  74. Proof.
  75. intro a.
  76. exists unit.
  77. exists (fun k => k tt).
  78. exists (fun i u => i).
  79. unfold comp.
  80. split;
  81. extensionality x.
  82. reflexivity.
  83. extensionality u.
  84. destruct u.
  85. reflexivity.
  86. Qed.
  87.  
  88. (** Codensity *)
  89.  
  90. Definition Codensity m a := forall r : Type, (a -> m r) -> m r.
  91.  
  92. Axiom Codensity_parametricity :
  93. forall `{Monad m} A B (k : Codensity m A) (f : A -> m B),
  94. k _ \o (fun x => x >=> f) = bind f \o k _.
  95.  
  96. Import MonadLaws.
  97.  
  98. Lemma codensity_iso : forall `{MonadLaws m} A, Codensity m A ≅ m A.
  99. Proof.
  100. intros.
  101. exists (fun k => k _ pure).
  102. exists (fun x _ k => x >>= k).
  103. split.
  104. apply join_fmap_pure.
  105. extensionality k.
  106. extensionality r.
  107. extensionality p.
  108. pose proof (Codensity_parametricity A r k p).
  109. unfold comp, bind in *.
  110. apply equal_f with (x:=pure) in H1.
  111. rewrite <- H1.
  112. unfold kleisli_compose, bind, comp, id.
  113. f_equal.
  114. extensionality x.
  115. rewrite fmap_pure_x, join_pure_x.
  116. reflexivity.
  117. Qed.
  118.  
  119. Axiom Cont_parametricity : forall A B (f : A -> B) (k : forall r, Cont r A),
  120. f (k _ id) = k _ f.
  121.  
  122. Lemma cont_iso : forall A, (forall r, Cont r A) ≅ A.
  123. Proof.
  124. intros.
  125. unfold isomorphic.
  126. exists (fun k => k _ id).
  127. exists (fun x _ k => k x).
  128. split.
  129. extensionality x.
  130. reflexivity.
  131. extensionality k.
  132. extensionality r.
  133. extensionality p.
  134. unfold comp.
  135. apply Cont_parametricity.
  136. Qed.
  137.  
  138. Axiom Yoneda_parametricity :
  139. forall `{Functor f} `(k : forall r, (a -> r) -> f r) `(p : a -> b),
  140. fmap p (k a id) = k b p.
  141.  
  142. Lemma yoneda_iso : forall `{FunctorLaws f} a, (forall r, (a -> r) -> f r) ≅ f a.
  143. Proof.
  144. intros.
  145. exists (fun k => k _ id).
  146. exists (fun x _ f => fmap f x).
  147. unfold comp.
  148. split.
  149. extensionality x.
  150. rewrite fmap_id.
  151. reflexivity.
  152. extensionality k.
  153. extensionality r.
  154. extensionality p.
  155. apply Yoneda_parametricity.
  156. Qed.
  157.  
  158. Definition Reader (e : Type) (a : Type) := e -> a.
  159. Definition State (s : Type) (a : Type) := s -> (a * s).
  160.  
  161. Instance Impl_Functor {A} : Functor (fun B => A -> B) := {
  162. fmap := fun A B f run => fun xs => f (run xs)
  163. }.
  164.  
  165. Instance Impl_Applicative {A} : Applicative (fun B => A -> B) := {
  166. pure := fun _ x => fun xs => x;
  167. ap := fun A B runf runx => fun xs => runf xs (runx xs)
  168. }.
  169.  
  170. Instance Impl_Monad {A} : Monad (fun B => A -> B) := {
  171. join := fun A run => fun xs => run xs xs
  172. }.
  173.  
  174. Lemma codensity_reader_state : forall e a, Codensity (Reader e) a ≅ State e a.
  175. Proof.
  176. intros.
  177. unfold Codensity, Reader, State, comp.
  178. unfold isomorphic.
  179. exists (fun (k : forall r : Type, (a -> e -> r) -> e -> r) s =>
  180. (k _ (fun x y => x) s, k _ (fun x y => y) s)).
  181. exists (fun k _ f e => match k e with (x,y) => f x y end).
  182. unfold comp.
  183. split.
  184. extensionality x.
  185. extensionality s.
  186. unfold id.
  187. destruct (x s).
  188. reflexivity.
  189. extensionality k.
  190. extensionality r.
  191. extensionality f.
  192. extensionality s.
  193. unfold id.
  194. pose proof (@Codensity_parametricity (Reader e) Impl_Monad a r k f).
  195. unfold comp in H.
  196. apply equal_f with (x:=pure) in H.
  197. unfold kleisli_compose, bind, comp, id in H.
  198. simpl in H.
  199. replace (fun (x : a) (xs : e) => f x xs) with f in H by trivial.
  200. rewrite H.
  201. f_equal.
  202. pose proof (@Codensity_parametricity (Reader e) Impl_Monad a e k (fun _ => id)).
  203. unfold comp in H0.
  204. unfold kleisli_compose, bind, comp, id in H0.
  205. simpl in H0.
  206. apply equal_f with (x:=pure) in H0.
  207. rewrite H0.
  208. reflexivity.
  209. Qed.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement