Guest User

Untitled

a guest
Jan 17th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 KB | None | 0 0
  1. Require Import Vectors.Vector.
  2.  
  3.  
  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. }.
  79. Definition Traversable := forall r, (Traversable__Dict -> r) -> r.
  80. Existing Class Traversable.
  81.  
  82. Definition sequence `{T : Traversable} `{@Applicative G} {t} : F (G t) -> G (F t) :=
  83. T _ (sequence__) _ _.
  84.  
  85. Definition traverse' `{T : Traversable} `{@Applicative G} {t u} : (t -> G u) -> F t -> G (F u) :=
  86. T _ (traverse__) _ _ _.
  87.  
  88. Definition traverse `{T : Traversable} `{@Applicative G} {t u} : (t -> G u) -> F t -> G (F u) :=
  89. let f := T _ (t_f__) in fun f ft => sequence (fmap f ft).
  90. End ExtCT.
  91. Section VectorU.
  92. Import VectorNotations.
  93. Program Definition vFunctor {n} : @Functor__Dict (fun t => Vector.t t n) := {| fmap__ _ _ f := map f;
  94. |}.
  95. Next Obligation. induction ft; simpl; f_equal; auto. Defined.
  96. Next Obligation. induction fa; simpl; f_equal; auto. Defined.
  97. Global Instance vector_Functor {n} : @Functor (fun t => Vector.t t n) := fun r f => f vFunctor.
  98.  
  99. Definition vPointed : @Pointed__Dict (fun t => Vector.t t 0) := {| point__ := nil |}.
  100. Global Instance vector_Pointed : @Pointed (fun t => Vector.t t 0) := fun r f => f vPointed.
  101.  
  102. Definition vPure : @Pure__Dict (fun t => Vector.t t 1) := {| pure__ _ x := [x] |}.
  103. Global Instance vector_Pure : @Pure (fun t => Vector.t t 1) := fun r f => f vPure.
  104.  
  105. Definition vSequence {G} {n} `{Applicative G} : forall T, Vector.t (G T) n -> G (Vector.t T n).
  106. intros. induction X; pose proof _f as f; pose proof _p as p. apply pure; trivial. apply (@point (fun f => Vector.t f 0) _ _). eapply ap. apply (fmap (fun a b => a :: b) h). exact IHX.
  107. Defined.
  108. Definition vTraversable {G} {n} : @Traversable__Dict (fun t => Vector.t t n) G := {| sequence__ _ := vSequence;
  109. traverse__ _ _ _ := fun f ft => vSequence _ (fmap f ft)
  110. |}.
  111. Global Instance vector_Traversable {n} {G} : @Traversable (fun t => Vector.t t n) G := fun r f => f vTraversable.
  112. End VectorU.
  113. Section ListU.
  114. Require Import Lists.List.
  115. Import ListNotations.
  116. Definition lFunctor : @Functor__Dict list := {| fmap__ := map;
  117. fmap_id__ := map_id;
  118. fmap_fusion__ := map_map
  119. |}.
  120. Global Instance list_Functor : @Functor list := fun r f => f lFunctor.
  121.  
  122. Definition lPointed : @Pointed__Dict list := {| point__ := @nil |}.
  123. Global Instance list_Pointed : @Pointed list := fun r f => f lPointed.
  124.  
  125. Definition lPure : @Pure__Dict list := {| pure__ _ a := [a] |}.
  126. Global Instance list_Pure : @Pure list := fun r f => f lPure.
  127.  
  128. Definition lApp : @Applicative__Dict list := {| zip__ := @combine |}.
  129. Global Instance list_Applicative : @Applicative list := fun r f => f lApp.
  130. End ListU.
  131. Section Demo.
  132. Import ListNotations.
  133. Inductive t1 : Set :=
  134. | a1 : t1
  135. | a2 {n} : Vector.t t1 n -> t1
  136. | a3 {n} : Vector.t t2 n -> t1
  137. with t2 : Set :=
  138. | b1 : t2
  139. | b2 {n} : Vector.t t1 n -> t2
  140. | b3 {n} : Vector.t t2 n -> t2.
  141.  
  142. Inductive t3 : Set :=
  143. | c1 : t3 -> t3 -> t3
  144. | c2 {n} : Vector.t t1 n -> t3
  145. | c3 {n} : Vector.t t2 n -> t3.
  146.  
  147. Inductive t4 : Set :=
  148. | d1 : t4
  149. | d2 : t4 -> t4 -> t4
  150. | d3 {n} : Vector.t t4 n -> t4
  151. | d4 {n} : Vector.t t4 n -> t4.
  152.  
  153. (** works *)
  154. Fixpoint f1 f : list t1 :=
  155. match f with
  156. | d1 => [a1]
  157. | d2 x x0 => f1 x ++ f1 x0
  158. | d3 x => map a2 (sequence (Vector.map f1 x))
  159. | d4 x => map a3 (sequence (Vector.map f2 x))
  160. end
  161. with f2 f : list t2 :=
  162. match f with
  163. | d1 => [b1]
  164. | d2 x x0 => f2 x ++ f2 x0
  165. | d3 x => map b2 (sequence (Vector.map f1 x))
  166. | d4 x => map b3 (sequence (Vector.map f2 x))
  167. end.
  168.  
  169. (** should ideally work *)
  170. Fail Fixpoint f1' f : list t1 :=
  171. match f with
  172. | d1 => [a1]
  173. | d2 x x0 => f1' x ++ f1' x0
  174. | d3 x => map a2 (traverse f1' x)
  175. | d4 x => map a3 (traverse f2' x)
  176. end
  177. with f2' f : list t2 :=
  178. match f with
  179. | d1 => [b1]
  180. | d2 x x0 => f2' x ++ f2' x0
  181. | d3 x => map b2 (traverse f1' x)
  182. | d4 x => map b3 (traverse f2' x)
  183. end.
  184.  
  185. (** should alternatively work *)
  186. Fail Fixpoint f1'' f : list t1 :=
  187. match f with
  188. | d1 => [a1]
  189. | d2 x x0 => f1'' x ++ f1'' x0
  190. | d3 x => map a2 (traverse' f1'' x)
  191. | d4 x => map a3 (traverse' f2'' x)
  192. end
  193. with f2'' f : list t2 :=
  194. match f with
  195. | d1 => [b1]
  196. | d2 x x0 => f2'' x ++ f2'' x0
  197. | d3 x => map b2 (traverse' f1'' x)
  198. | d4 x => map b3 (traverse' f2'' x)
  199. end.
  200.  
  201. End Demo.
Add Comment
Please, Sign In to add comment