-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functor.v
109 lines (84 loc) · 2.65 KB
/
Functor.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
From Tealeaves Require Export
Classes.Comonad
Functors.Environment.
Import Product.Notations.
(** * Operation <<fmapdt>> *)
(******************************************************************************)
Section operations.
Context
(E : Type)
(F : Type -> Type).
Class Fmapd :=
fmapd : forall {A B : Type} (f : E * A -> B), F A -> F B.
End operations.
Arguments fmapd {E}%type_scope F%function_scope {Fmapd} {A B}%type_scope f%function_scope _.
(** ** Typeclass *)
(******************************************************************************)
Section class.
Context
(E : Type)
(T : Type -> Type)
`{Fmapd E T}.
Class DecoratedFunctor :=
{ dfun_fmapd1 : forall (A : Type),
fmapd T (extract (E ×)) = @id (T A);
dfun_fmapd2 : forall (A B C : Type) (g : E * B -> C) (f : E * A -> B),
fmapd T g ∘ fmapd T f = fmapd T (g ∘ cobind (E ×) f);
}.
End class.
#[local] Generalizable Variables E T.
(** * To functor *)
(******************************************************************************)
Module ToFunctor.
Section operation.
Context
(T : Type -> Type)
`{Fmapd E T}.
#[export] Instance Fmap_Fmapd : Fmap T :=
fun (A B : Type) (f : A -> B) => fmapd T (f ∘ extract (E ×)).
End operation.
Section with_kleisli.
Context
(T : Type -> Type)
`{DecoratedFunctor E T}.
Lemma fmap_id : forall (A : Type),
fmap T (@id A) = @id (T A).
Proof.
intros. unfold_ops @Fmap_Fmapd.
now rewrite (dfun_fmapd1 E T).
Qed.
Lemma fmap_fmap : forall (A B C : Type) (f : A -> B) (g : B -> C),
fmap T g ∘ fmap T f = fmap T (g ∘ f).
Proof.
intros. unfold_ops @Fmap_Fmapd.
rewrite (dfun_fmapd2 E T).
fequal. reassociate ->.
now rewrite (extract_cobind (E ×)).
Qed.
#[export] Instance: Classes.Functor.Functor T :=
{| fun_fmap_id := fmap_id;
fun_fmap_fmap := fmap_fmap;
|}.
Lemma fmap_fmapd : forall (A B C : Type)
(g : B -> C)
(f : E * A -> B),
fmap T g ∘ fmapd T f =
fmapd T (g ∘ f).
Proof.
intros. unfold_ops @Fmap_Fmapd.
rewrite (dfun_fmapd2 E T).
reassociate ->.
now rewrite (extract_cobind (E ×)).
Qed.
Lemma fmapd_fmap: forall (A B C : Type)
(g : E * B -> C)
(f : A -> B),
fmapd T g ∘ fmap T f = fmapd T (g ∘ fmap (E ×) f).
Proof.
intros. unfold_ops @Fmap_Fmapd.
rewrite (dfun_fmapd2 E T).
rewrite <- (fmap_to_cobind (E ×)).
reflexivity.
Qed.
End with_kleisli.
End ToFunctor.