-
Notifications
You must be signed in to change notification settings - Fork 0
/
Monad.v
387 lines (318 loc) · 11.7 KB
/
Monad.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
From Tealeaves Require Export
Functors.Writer.
Import Product.Notations.
Import Monoid.Notations.
#[local] Generalizable Variables W T A B C.
(** * The <<prepromote>> operation *)
(** A decorated monad is a decorated functor whose monad operations
are compatible with the decorated structure. *)
(******************************************************************************)
Definition preincr `{Monoid_op W} (w : W) `(f : W * A -> B) :=
f ∘ incr w.
Lemma preincr_zero `{Monoid W} : forall `(f : W * A -> B),
preincr Ƶ f = f.
Proof.
intros. unfold preincr.
now rewrite incr_zero.
Qed.
Lemma preincr_incr1 `{Monoid W} : forall `(f : W * A -> B) (w1 : W) (w2 : W),
preincr w2 (f ∘ incr w1) = f ∘ (incr (w1 ● w2)).
Proof.
intros. unfold preincr.
reassociate ->.
now rewrite (incr_incr).
Qed.
Lemma preincr_preincr1 `{Monoid W} : forall `(f : W * A -> B) (w1 : W) (w2 : W),
preincr w2 (preincr w1 f) = preincr (w1 ● w2) f.
Proof.
intros. unfold preincr.
reassociate ->.
now rewrite (incr_incr).
Qed.
Lemma preincr_ret `{Monoid W} : forall `(f : W * A -> B) (w : W),
preincr w f ∘ ret (W ×) = f ∘ pair w.
Proof.
intros. ext a. cbv.
change (op w unit0) with (w ● Ƶ).
now simpl_monoid.
Qed.
Lemma preincr_extract `{Monoid W} : forall `(f : A -> B) (w : W),
preincr w (f ∘ extract (W ×)) = f ∘ extract (W ×).
Proof.
intros. now ext [w' a].
Qed.
Lemma preincr_extract2 `{Monoid W} : forall (A : Type) (w : W),
preincr w (extract (W ×)) = extract (W ×) (A := A).
Proof.
intros. now ext [w' a].
Qed.
(** * The <<Bindd>> operation *)
(** A decorated monad is a decorated functor whose monad operations
are compatible with the decorated structure. *)
(******************************************************************************)
Section operations.
Context
(W : Type)
(T : Type -> Type)
(F : Type -> Type).
Class Bindd :=
bindd : forall (A B : Type), (W * A -> T B) -> F A -> F B.
End operations.
(** ** Kleisli composition *)
(** This definition is such that more recently seen binders (those
deeper in the AST, closer to the variable occurrence) are seen on
the _left_. So @f@ gets called on @([β0, β1, ... βn], v)@
where @βn@ is the outermost binder. *)
(******************************************************************************)
Definition kcompose_dm {A B C} `{Bindd W T T} `{Monoid_op W} :
(W * B -> T C) ->
(W * A -> T B) ->
(W * A -> T C) :=
fun g f '(w, a) => bindd W T T B C (preincr w g) (f (w, a)).
#[local] Notation "g ⋆dm f" := (kcompose_dm g f) (at level 40) : tealeaves_scope.
(** ** Decorated Monad *)
(******************************************************************************)
Section class.
Context
{W : Type}
(T : Type -> Type)
`{Return T}
`{Bindd W T T}
`{Monoid_op W} `{Monoid_unit W}.
Class Monad :=
{ kmond_monoid :> Monoid W;
kmond_bindd0 : forall `(f : W * A -> T B),
bindd W T T A B f ∘ ret T (A := A) = f ∘ ret ((W ×));
kmond_bindd1 : forall (A : Type),
bindd W T T A A (ret T ∘ extract (W ×)) = @id (T A);
kmond_bindd2 : forall `(g : W * B -> T C) `(f : W * A -> T B),
bindd W T T B C g ∘ bindd W T T A B f = bindd W T T A C (g ⋆dm f);
}.
End class.
Arguments bindd {W}%type_scope {T}%function_scope (F)%function_scope
{Bindd} {A B}%type_scope _%function_scope _.
(** * Notations *)
(******************************************************************************)
Module Notations.
Notation "g ⋆dm f" := (kcompose_dm g f) (at level 40) : tealeaves_scope.
End Notations.
(** * Kleisli composition *)
(******************************************************************************)
Section kleisli_composition.
Context
`{Decorated.Monad.Monad W T}.
Lemma kcompose_incr : forall `(g : W * B -> T C) `(f : W * A -> T B) (w : W),
(g ∘ incr w) ⋆dm (f ∘ incr w) = (g ⋆dm f) ∘ incr w.
Proof.
intros. unfold kcompose_dm.
ext [w' a]. rewrite preincr_incr1.
reflexivity.
Qed.
Lemma preincr_kcompose : forall `(g : W * B -> T C) `(f : W * A -> T B) (w : W),
preincr w (g ⋆dm f) = (preincr w g) ⋆dm (preincr w f).
Proof.
intros. unfold preincr. now rewrite kcompose_incr.
Qed.
Theorem dm_kleisli_id_r {B C} : forall (g : W * B -> T C),
g ⋆dm (ret T ∘ extract (W ×)) = g.
Proof.
intros. unfold kcompose_dm.
ext [w a]. unfold compose. cbn.
compose near a on left.
rewrite (kmond_bindd0 T _).
now rewrite preincr_ret.
Qed.
Theorem dm_kleisli_id_l {A B} : forall (f : W * A -> T B),
(ret T ∘ extract (W ×)) ⋆dm f = f.
Proof.
intros. unfold kcompose_dm.
ext [w a]. rewrite preincr_extract.
now rewrite (kmond_bindd1 T _).
Qed.
Theorem dm_kleisli_assoc {A B C D} : forall (h : W * C -> T D) (g : W * B -> T C) (f : W * A -> T B),
h ⋆dm (g ⋆dm f) = (h ⋆dm g) ⋆dm f.
Proof.
intros. unfold kcompose_dm at 3.
ext [w a]. unfold preincr.
rewrite <- kcompose_incr.
rewrite <- (kmond_bindd2 T).
reflexivity.
Qed.
End kleisli_composition.
From Tealeaves Require Import
Classes.Kleisli.Monad
Classes.Kleisli.Decorated.Functor.
(** * Derived instances *)
(******************************************************************************)
Module Derived.
Import
Kleisli.Monad.Notations
Comonad.Notations.
Section operations.
Context
(T : Type -> Type)
`{Return T}
`{Bindd W T T}.
#[export] Instance Fmap_Bindd: Fmap T := fun A B f => bindd T (ret T ∘ f ∘ extract (W ×)).
#[export] Instance Bind_Bindd: Bind T T := fun A B f => bindd T (f ∘ extract (W ×)).
#[export] Instance Fmapd_Bindd: Fmapd W T := fun A B f => bindd T (ret T ∘ f).
End operations.
(** ** Lesser Kleisli composition laws *)
(******************************************************************************)
Section Kleisli_composition.
Context
(T : Type -> Type)
`{Decorated.Monad.Monad W T}.
(** *** Lifting context-agnostic substitutions *)
Lemma kcompose_extract : forall `(g : B -> T C) `(f : A -> T B),
(g ∘ extract (W ×)) ⋆dm (f ∘ extract (W ×)) = (g ⋆ f) ∘ extract (W ×).
Proof.
intros. unfold kcompose_dm.
ext [w a]. rewrite preincr_extract.
reflexivity.
Qed.
(** *** Lifting context-sensitive maps *)
Lemma kcompose_ret : forall `(g : W * B -> C) `(f : W * A -> B),
(ret T ∘ g) ⋆dm (ret T ∘ f) = ret T ∘ (g co⋆ f).
Proof.
intros. unfold kcompose_dm.
ext [w' a]. unfold compose at 2.
compose near (f (w', a)).
rewrite (kmond_bindd0 T).
rewrite preincr_ret.
reflexivity.
Qed.
(** Composition when <<f>> has no substitution *)
Theorem dm_kleisli_star1 {A B C} : forall (g : W * B -> T C) (f : W * A -> B),
g ⋆dm (ret T ∘ f) = g co⋆ f.
Proof.
intros. unfold kcompose_dm, cokcompose.
ext [w a]. unfold compose. cbn.
unfold compose, id; cbn.
compose near (f (w, a)) on left.
rewrite (kmond_bindd0 T _).
now rewrite (preincr_ret).
Qed.
(** Composition when <<g>> is context-agnostic *)
Theorem dm_kleisli_star2 {A B C} : forall (g : B -> T C) (f : W * A -> T B),
(g ∘ extract (W ×)) ⋆dm f = g ⋆ f.
Proof.
intros. unfold kcompose_dm.
ext [w a]. rewrite preincr_extract.
reflexivity.
Qed.
(** Composition when <<f>> is context-agnostic *)
Theorem dm_kleisli_star3 {A B C} : forall (g : W * B -> T C) (f : A -> T B),
g ⋆dm (f ∘ extract (W ×)) =
((fun '(w, t) => bindd T (preincr w g) t) ∘ fmap (W ×) f).
Proof.
intros. unfold kcompose_dm.
ext [w a]. reflexivity.
Qed.
(** Composition when <<g>> has no substitution *)
Theorem dm_kleisli_star4 {A B C} : forall (g : W * B -> C) (f : W * A -> T B),
(ret T ∘ g) ⋆dm f = fun '(w, t) => fmapd T (preincr w g) (f (w, t)).
Proof.
reflexivity.
Qed.
(** Other laws *)
(* Alternatively, this one could be proved using rewrite dm_kleisli_star1 *)
Theorem dm_kleisli_star5 {A B C} : forall (g : B -> T C) (f : W * A -> B),
(g ∘ extract (W ×)) ⋆dm (ret T ∘ f) = g ∘ f.
Proof.
intros. rewrite dm_kleisli_star2.
rewrite ToFunctor.kcompose_asc2.
unfold kcompose, bind, Bind_Bindd.
rewrite (kmond_bindd0 T).
reflexivity.
Qed.
End Kleisli_composition.
Section with_monad.
Context
(T : Type -> Type)
`{Decorated.Monad.Monad W T}.
#[export] Instance: Kleisli.Monad.Monad T.
Proof.
constructor; unfold_ops @Bind_Bindd.
- intros. now rewrite (kmond_bindd0 T).
- intros. now rewrite (kmond_bindd1 T).
- intros. rewrite (kmond_bindd2 T).
rewrite (kcompose_extract T).
reflexivity.
Qed.
#[export] Instance: Kleisli.Decorated.Functor.DecoratedFunctor W T.
Proof.
constructor; unfold_ops @Fmapd_Bindd.
- intros. now rewrite (kmond_bindd1 T).
- intros. rewrite (kmond_bindd2 T).
rewrite (kcompose_ret T).
reflexivity.
Qed.
End with_monad.
Section laws.
Context
(T : Type -> Type)
`{Decorated.Monad.Monad W T}.
(** *** Composition with <<bind>> *)
(******************************************************************************)
Corollary bind_bindd {A B C} : forall (g : B -> T C) (f : W * A -> T B),
bind T g ∘ bindd T f = bindd T (g ⋆ f).
Proof.
intros. unfold_ops @Bind_Bindd.
rewrite (kmond_bindd2 T).
rewrite (dm_kleisli_star2 T).
reflexivity.
Qed.
Corollary bindd_bind {A B C} : forall (g : W * B -> T C) (f : A -> T B),
bindd T g ∘ bind T f = bindd T ((fun '(w, t) => bindd T (preincr w g) t) ∘ fmap (W ×) f).
Proof.
introv. unfold_ops @Bind_Bindd.
rewrite (kmond_bindd2 T).
now rewrite (dm_kleisli_star3 T).
Qed.
(** *** Composition with <<fmapd>> *)
(******************************************************************************)
Lemma bindd_fmapd {A B C} : forall (g : W * B -> T C) (f : W * A -> B),
bindd T g ∘ fmapd T f = bindd T (g co⋆ f).
Proof.
introv. unfold_ops @Fmapd_Bindd.
rewrite (kmond_bindd2 T).
rewrite (dm_kleisli_star1 T).
reflexivity.
Qed.
Corollary fmapd_bindd {A B C} : forall (g : W * B -> C) (f : W * A -> T B),
fmapd T g ∘ bindd T f = bindd T (fun '(w, t) => fmapd T (preincr w g) (f (w, t))).
Proof.
intros. unfold_ops @Fmapd_Bindd.
rewrite (kmond_bindd2 T).
rewrite (dm_kleisli_star4 T).
reflexivity.
Qed.
(** *** Composition with <<fmap>> *)
(******************************************************************************)
Lemma bindd_fmap {A B C} : forall (g : W * B -> T C) (f : A -> B),
bindd T g ∘ fmap T f = bindd T (g ∘ fmap (prod W) f).
Proof.
intros. unfold_ops @Fmap_Bindd.
rewrite (kmond_bindd2 T).
reassociate ->.
rewrite (dm_kleisli_star1 T).
rewrite (fmap_to_cobind (W ×)).
reflexivity.
Qed.
Corollary fmap_bindd {A B C} : forall (g : B -> C) (f : W * A -> T B),
fmap T g ∘ bindd T f = bindd T (fmap T g ∘ f).
Proof.
intros. unfold_ops @Fmap_Bindd.
rewrite (kmond_bindd2 T).
rewrite (dm_kleisli_star2 T).
reflexivity.
Qed.
(** *** Composition between <<fmapd>> and <<bind>> *)
(******************************************************************************)
(** *** Composition between <<fmapd>> and <<fmap>> *)
(******************************************************************************)
(** *** Composition between <<bind>> and <<fmap>> *)
(******************************************************************************)
End laws.
End Derived.