Guest User

Untitled

a guest
Jan 16th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. Require Import Vectors.Vector.
  2.  
  3. Import VectorNotations.
  4. Section CT.
  5. Context {F : Type -> Type}.
  6. Record Functor__Dict := {
  7. fmap__ {a b} : (a -> b) -> F a -> F b;
  8. fmap_id__ {t} : forall ft, fmap__ (fun a : t => a) ft = ft;
  9. fmap_fusion__ {a b c} : forall (f : a -> b) (g : b -> c) fa, fmap__ g (fmap__ f fa) = fmap__ (fun e => g (f e)) fa
  10. }.
  11.  
  12. Definition Functor := forall r, (Functor__Dict -> r) -> r.
  13.  
  14. Existing Class Functor.
  15.  
  16. Definition fmap `{f : Functor} {a b} : (a -> b) -> F a -> F b :=
  17. f _ (fmap__) a b.
  18.  
  19. (* Class Functor := { fmap {a b : Set} : (a -> b) -> F a -> F b; *)
  20. (* fmap_id {t : Set} : forall ft, fmap (fun a : t => a) ft = ft; *)
  21. (* fmap_fusion {a b c : Set} : forall (f : a -> b) (g : b -> c) fa, fmap g (fmap f fa) = fmap (fun e => g (f e)) fa *)
  22. (* }. *)
  23.  
  24. Record Pure__Dict := { pure__ {t} : t -> F t }.
  25.  
  26. Definition Pure := forall r , (Pure__Dict -> r) -> r.
  27.  
  28. Existing Class Pure.
  29.  
  30. Definition pure `{p : Pure} {t} : t -> F t :=
  31. p _ (pure__) t.
  32.  
  33. Record Applicative__Dict := { _f__ :> Functor;
  34. _p__ :> Pure;
  35. zip__ {a b} : F a -> F b -> F (a * b)%type;
  36. ap__ {a b} : F (a -> b) -> F a -> F b := fun ff fa => fmap (fun t => match t with (f, e) => f e end) (zip__ ff fa);
  37. zipWith__ {a b c} : (a -> b -> c) -> F a -> F b -> F c := fun f fa fb => ap__ (fmap f fa) fb
  38. }.
  39.  
  40. Definition Applicative :=
  41. forall r, (Applicative__Dict -> r) -> r.
  42.  
  43. Existing Class Applicative.
  44.  
  45. Definition zip `{A : Applicative} {a b} : F a -> F b -> F (a * b)%type :=
  46. A _ (zip__) a b.
  47.  
  48. Definition ap `{A : Applicative} {a b} : F (a -> b) -> F a -> F b :=
  49. A _ (ap__) a b.
  50.  
  51. Definition zipWith `{A : Applicative} {a b c} : (a -> b -> c) -> F a -> F b -> F c :=
  52. A _ (zipWith__) a b c.
  53.  
  54. Definition _f `{A : Applicative} : Functor :=
  55. A _ (_f__).
  56.  
  57. Definition _p `{A : Applicative} : Pure :=
  58. A _ (_p__).
  59.  
  60. Record Pointed__Dict := { point__ {t} : F t }.
  61. Definition Pointed := forall r, (Pointed__Dict -> r) -> r.
  62. Existing Class Pointed.
  63.  
  64. Definition point `{P : Pointed} {t} : F t :=
  65. P _ (point__) t.
  66.  
  67. (* Class Monad `{Pure} := { join {t} : F (F t) -> F t }. *)
  68.  
  69. (* Class MonadFilter `{Pointed} `{Monad} := { filter {t} (p : t -> bool) : F t -> F {e : t | p e = true }; *)
  70. (* }. *)
  71. End CT.
  72.  
  73. Section ExtCT.
  74. Context {F : Type -> Type} {G : Type -> Type}.
  75. Record Traversable__Dict := { t_f__ :> @Functor F;
  76. sequence__ `{@Applicative G} {t} : F (G t) -> G (F t);
  77. traverse__ `{@Applicative G} {t u} : (t -> G u) -> F t -> G (F u) :=
  78. fun f ft => sequence__ (fmap f ft)
  79. }.
  80. Definition Traversable := forall r, (Traversable__Dict -> r) -> r.
  81. Existing Class Traversable.
  82.  
  83. Definition sequence `{T : Traversable} `{@Applicative G} {t} : F (G t) -> G (F t) :=
  84. T _ (sequence__) _ _.
  85.  
  86. Definition traverse `{T: Traversable} `{@Applicative G} {t u} : (t -> G u) -> F t -> G (F u) :=
  87. T _ (traverse__) _ _ _.
  88. End ExtCT.
Add Comment
Please, Sign In to add comment