-
Notifications
You must be signed in to change notification settings - Fork 0
/
Monad.v
2112 lines (1835 loc) · 67.3 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From Tealeaves Require Export
Classes.Monoid
Classes.Applicative
Classes.Comonad
Functors.Writer.
From Tealeaves Require
Classes.Monad
Classes.Kleisli.Decorated.Monad.
Export Decorated.Monad (preincr).
Import Monoid.Notations.
Import Product.Notations.
#[local] Generalizable Variables ϕ T W G A B C D F M.
Section operations.
Context
(W : Type)
(T : Type -> Type)
(F : Type -> Type).
Class Binddt :=
binddt :
forall (G : Type -> Type)
`{Fmap G} `{Pure G} `{Mult G}
(A B : Type),
(W * A -> G (T B)) -> F A -> G (F B).
End operations.
Definition kcompose_dtm
{A B C}
`{Fmap G1} `{Pure G1} `{Mult G1}
`{Fmap G2} `{Pure G2} `{Mult G2}
`{Binddt W T T} `{Monoid_op W} :
(W * B -> G2 (T C)) ->
(W * A -> G1 (T B)) ->
(W * A -> G1 (G2 (T C))) :=
fun g f '(w, a) => fmap G1 (binddt W T T G2 B C (preincr w g)) (f (w, a)).
#[local] Infix "⋆dtm" := kcompose_dtm (at level 60) : tealeaves_scope.
Section class.
Context
(W : Type)
(T : Type -> Type)
`{Return T}
`{Binddt W T T}
`{op: Monoid_op W} `{unit: Monoid_unit W}.
Class Monad :=
{ kdtm_monoid :> Monoid W;
kdtm_binddt0 : forall (A B : Type) `{Applicative G} (f : W * A -> G (T B)),
binddt W T T G A B f ∘ ret T = f ∘ ret (W ×);
kdtm_binddt1 : forall (A : Type),
binddt W T T (fun A => A) A A (ret T ∘ extract (prod W)) = @id (T A);
kdtm_binddt2 :
forall (A B C : Type) `{Applicative G1} `{Applicative G2}
(g : W * B -> G2 (T C)) (f : W * A -> G1 (T B)),
fmap G1 (binddt W T T G2 B C g) ∘ binddt W T T G1 A B f =
binddt W T T (G1 ∘ G2) A C (g ⋆dtm f);
kdtm_morph : forall (G1 G2 : Type -> Type) `{morph : ApplicativeMorphism G1 G2 ϕ} `(f : W * A -> G1 (T B)),
ϕ (T B) ∘ binddt W T T G1 A B f = binddt W T T G2 A B (ϕ (T B) ∘ f);
}.
End class.
#[global] Arguments binddt {W}%type_scope {T}%function_scope (F)%function_scope
{Binddt} G%function_scope {H H0 H1} {A B}%type_scope _%function_scope _.
Module Notations.
Infix "⋆dtm" := kcompose_dtm (at level 60) : tealeaves_scope.
End Notations.
Section class.
Context
(W : Type)
(T : Type -> Type)
`{Return T}
`{Binddt W T T}
`{op: Monoid_op W} `{unit: Monoid_unit W}
`{! Monoid W}.
Lemma kcompose_dtm_incr : forall
`{Applicative G1} `{Applicative G2}
`(g : W * B -> G2 (T C)) `(f : W * A -> G1 (T B)) (w : W),
(g ∘ incr w) ⋆dtm (f ∘ incr w) = (g ⋆dtm f) ∘ incr w.
Proof.
intros. unfold kcompose_dtm.
ext [w' a]. unfold preincr.
reassociate ->. rewrite incr_incr.
reflexivity.
Qed.
Lemma kcompose_dtm_preincr : forall
`{Applicative G1} `{Applicative G2}
`(g : W * B -> G2 (T C)) `(f : W * A -> G1 (T B)) (w : W),
(preincr w g) ⋆dtm (preincr w f) = preincr w (g ⋆dtm f).
Proof.
intros. unfold preincr. rewrite kcompose_dtm_incr.
reflexivity.
Qed.
Context
`{! DT.Monad.Monad W T}.
Lemma dtm_kleisli_identity1 : forall `{Applicative G} `(f : W * A -> G (T B)),
kcompose_dtm (G2 := fun A => A) (ret T ∘ extract (W ×)) f = f.
Proof.
intros. unfold kcompose_dtm.
ext [w a]. unfold preincr.
reassociate ->. rewrite (extract_incr).
rewrite (kdtm_binddt1 W T).
rewrite (fun_fmap_id G).
reflexivity.
Qed.
Lemma dtm_kleisli_identity2 : forall `{Applicative G} `(g : W * A -> G (T B)),
kcompose_dtm (G1 := fun A => A) g (ret T ∘ extract (W ×)) = g.
Proof.
intros. unfold kcompose_dtm.
ext [w a]. unfold compose. cbn.
compose near a.
change (fmap (fun A => A) ?f) with f.
rewrite (kdtm_binddt0 W T); auto.
cbv. change (g ((w ● Ƶ), a) = g (w, a)).
simpl_monoid.
reflexivity.
Qed.
Lemma dtm_kleisli_assoc :
forall `{Applicative G1} `{Applicative G2} `{Applicative G3}
`(h : W * C -> G3 (T D)) `(g : W * B -> G2 (T C)) `(f : W * A -> G1 (T B)),
kcompose_dtm (G1 := G1 ∘ G2) h (g ⋆dtm f) =
kcompose_dtm (G2 := G2 ∘ G3) (h ⋆dtm g) f.
Proof.
intros. unfold kcompose_dtm.
ext [w a]. cbn.
unfold_ops @Fmap_compose.
compose near (f (w, a)) on left.
rewrite (fun_fmap_fmap G1).
fequal.
rewrite (kdtm_binddt2 W T); auto.
fequal.
rewrite kcompose_dtm_preincr.
reflexivity.
Qed.
End class.
From Tealeaves.Classes.Kleisli Require Import
Monad
Decorated.Functor
Decorated.Monad
Traversable.Functor
Traversable.Monad
DT.Functor.
(** * Auxiliary lemmas for constant applicative functors *)
(******************************************************************************)
Module Derived.
Section with_kleisli.
Context
(T : Type -> Type)
`{Binddt W T T}
`{Return T}.
#[export] Instance Fmap_Binddt : Fmap T :=
fun (A B : Type) (f : A -> B) => binddt T (fun A => A) (ret T ∘ f ∘ extract (W ×)).
#[export] Instance Fmapdt_Binddt: Fmapdt W T
:= fun G _ _ _ A B f => binddt T G (fmap G (ret T) ∘ f).
#[export] Instance Bindd_Binddt: Bindd W T T
:= fun A B f => binddt T (fun A => A) f.
#[export] Instance Bindt_Binddt: Bindt T T
:= fun G _ _ _ A B f => binddt T G (f ∘ extract (W ×)).
#[export] Instance Bind_Binddt: Bind T T
:= fun A B f => binddt T (fun A => A) (f ∘ extract (W ×)).
#[export] Instance Fmapd_Binddt: Fmapd W T
:= fun A B f => binddt T (fun A => A) (ret T ∘ f).
#[export] Instance Traverse_Binddt: Traverse T
:= fun G _ _ _ A B f => binddt T G (fmap G (ret T) ∘ f ∘ extract (W ×)).
End with_kleisli.
Section with_monad.
Context
(T : Type -> Type)
`{DT.Monad.Monad W T}.
Lemma fmap_id : forall (A : Type),
fmap T (@id A) = @id (T A).
Proof.
intros. unfold_ops @Fmap_Binddt.
change (ret T ∘ id) with (ret T (A := A)).
now rewrite (kdtm_binddt1 W 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_Binddt.
change (binddt T (fun A0 : Type => A0) (ret T ∘ g ∘ extract (prod W)))
with (fmap (fun A => A) (binddt T (fun A0 : Type => A0) (ret T ∘ g ∘ extract (prod W)))).
rewrite (kdtm_binddt2 W T _ _ _ (G1 := fun A => A) (G2 := fun A => A)).
fequal.
- now rewrite Mult_compose_identity1.
- unfold kcompose_dtm. ext [w a].
unfold_ops @Fmap_I.
compose near (w, a) on left.
do 2 reassociate <- on left.
unfold_compose_in_compose.
rewrite (kdtm_binddt0 W T _ _ (G := fun A => A)).
unfold_ops @Return_writer @Monoid_unit_product.
unfold compose; cbn.
reflexivity.
Qed.
#[export] Instance: Classes.Functor.Functor T :=
{| fun_fmap_id := fmap_id;
fun_fmap_fmap := fmap_fmap;
|}.
Lemma fmap_binddt: forall (G1 : Type -> Type) (A B C : Type) `{Applicative G1}
(g : B -> C)
(f : W * A -> G1 (T B)),
fmap G1 (fmap T g) ∘ binddt T G1 f =
binddt T G1 (fmap G1 (fmap T g) ∘ f).
Proof.
intros. unfold_ops @Fmap_Binddt.
rewrite (kdtm_binddt2 W T A B C (G1 := G1) (G2 := fun A => A)).
fequal.
- now rewrite Mult_compose_identity1.
- ext [w a]. cbn. now rewrite Decorated.Monad.preincr_extract.
Qed.
Lemma binddt_fmap: forall (G2 : Type -> Type) (A B C : Type) `{Applicative G2}
(g : W * B -> G2 (T C))
(f : A -> B),
binddt T G2 g ∘ fmap T f =
binddt T G2 (fun '(w, a) => g (w, f a)).
Proof.
intros. unfold_ops @Fmap_Binddt.
change (binddt T G2 g) with (fmap (fun A => A) (binddt T G2 g)).
rewrite (kdtm_binddt2 W T A B C (G1 := fun A => A)).
fequal. now rewrite Mult_compose_identity2.
unfold kcompose_dtm. ext [w a].
change (fmap (fun A => A) ?f) with f.
unfold compose; cbn.
compose near (f a) on left.
rewrite (kdtm_binddt0 W T _ _ _ (G := G2)).
cbv. change (op w unit0) with (w ● Ƶ).
now simpl_monoid.
Qed.
End with_monad.
(** ** Specifications for lesser Kleisli operations *)
(******************************************************************************)
Section special_cases.
Context
(W : Type)
(T : Type -> Type)
`{Return T}
`{Binddt W T T}
`{Applicative F}.
(** *** Rewriting rules for special cases of <<binddt>> *)
(******************************************************************************)
Lemma bindt_to_binddt `(f : A -> F (T B)):
bindt T F f = binddt T F (f ∘ extract (W ×)).
Proof.
reflexivity.
Qed.
Lemma bindd_to_binddt `(f : W * A -> T B):
bindd T f = binddt T (fun A => A) f.
Proof.
reflexivity.
Qed.
Lemma fmapdt_to_binddt `(f : W * A -> F B):
fmapdt T F f = binddt T F (fmap F (ret T) ∘ f).
Proof.
reflexivity.
Qed.
Lemma bind_to_binddt `(f : A -> T B):
bind T f = binddt T (fun A => A) (f ∘ extract (W ×)).
Proof.
reflexivity.
Qed.
Lemma fmapd_to_binddt `(f : W * A -> B):
fmapd T f = binddt T (fun A => A) (ret T ∘ f).
Proof.
reflexivity.
Qed.
Lemma fmapt_to_binddt `(f : A -> F B):
traverse T F f = binddt T F (fmap F (ret T) ∘ f ∘ extract (W ×)).
Proof.
reflexivity.
Qed.
Lemma fmap_to_binddt `(f : A -> B):
fmap T f = binddt T (fun A => A) (ret T ∘ f ∘ extract (W ×)).
Proof.
reflexivity.
Qed.
(** *** Rewriting rules for special cases of <<bindt>> *)
(******************************************************************************)
Lemma bind_to_bindt `(f : A -> T B):
bind T f = bindt T (fun A => A) f.
Proof.
reflexivity.
Qed.
Lemma fmapt_to_bindt `(f : A -> F B):
traverse T F f = bindt T F (fmap F (ret T) ∘ f).
Proof.
reflexivity.
Qed.
Lemma fmap_to_bindt `(f : A -> B):
fmap T f = bindt T (fun A => A) (ret T ∘ f).
Proof.
reflexivity.
Qed.
(** *** Rewriting rules for special cases of <<bindd>> *)
(******************************************************************************)
Lemma bind_to_bindd `(f : A -> T B):
bind T f = bindd T (f ∘ extract (W ×)).
Proof.
reflexivity.
Qed.
Lemma fmapd_to_bindd `(f : W * A -> B):
fmapd T f = bindd T (ret T ∘ f).
Proof.
reflexivity.
Qed.
Lemma fmap_to_bindd `(f : A -> B):
fmap T f = bindd T (ret T ∘ f ∘ extract (W ×)).
Proof.
reflexivity.
Qed.
(** *** Rewriting rules for special cases of <<fmapdt>> *)
(******************************************************************************)
Lemma fmapd_to_fmapdt `(f : W * A -> B):
fmapd T f = fmapdt T (fun A => A) f.
Proof.
reflexivity.
Qed.
Lemma fmap_to_fmapdt `(f : A -> B):
fmap T f = fmapdt T (fun A => A) (f ∘ extract (W ×)).
Proof.
reflexivity.
Qed.
Lemma fmapt_to_fmapdt `(f : A -> F B):
traverse T F f = fmapdt T F (f ∘ extract (W ×)).
Proof.
reflexivity.
Qed.
(** *** Rewriting rules for special cases of <<fmapt>> *)
(******************************************************************************)
Lemma fmap_to_fmapt `(f : A -> B):
fmap T f = traverse T (fun A => A) f.
Proof.
reflexivity.
Qed.
(** *** Rewriting rules for special cases of <<fmapd>> *)
(******************************************************************************)
Lemma fmap_to_fmapd `(f : A -> B):
fmap T f = fmapd T (f ∘ extract (W ×)).
Proof.
reflexivity.
Qed.
(** *** Rewriting rules for special cases of <<bind>> *)
(******************************************************************************)
Lemma fmap_to_bind `(f : A -> B):
fmap T f = bind T (ret T ∘ f).
Proof.
reflexivity.
Qed.
End special_cases.
Import Kleisli.Traversable.Monad.Notations.
Import Kleisli.DT.Functor.Notations.
Import Kleisli.Decorated.Monad.Notations.
Import Kleisli.Monad.Notations.
Import Comonad.Notations.
(** ** Special cases of Kleisli composition *)
(******************************************************************************)
Section kleisli_composition.
Context
(W : Type)
(T : Type -> Type)
`{DT.Monad.Monad W T}.
(*
d/t/m:
000 0 no d or t or m
001 1 no context or effect
010 2 no context or subst
011 3 no context
100 4 no effect or subst
101 5 no effect
110 6 no subst
111 7 everything
*)
(** *** Composition when <<g>> is context-agnostic *)
(******************************************************************************)
(** Composition when <<g>> is context-agnostic reduces to Kleisli
composition for traversable monads. *)
Theorem dtm_kleisli_37 {A B C} : forall
`{Applicative G1} `{Applicative G2}
(g : B -> G2 (T C)) (f : W * A -> G1 (T B)),
(g ∘ extract (W ×)) ⋆dtm f = g ⋆tm f.
Proof.
intros. unfold kcompose_dtm.
ext [w a]. rewrite preincr_extract.
reflexivity.
Qed.
(** Composition when neither <<g>> or <<f>> is context-sensitive *)
Lemma kcompose_dtm_33 :
forall `{Applicative G1} `{Applicative G2}
`(g : B -> G2 (T C)) `(f : A -> G1 (T B)),
kcompose_dtm (G1 := G1) (G2 := G2) (g ∘ extract (W ×)) (f ∘ extract (W ×)) =
(kcompose_tm (G1 := G1) (G2 := G2) g f) ∘ extract (W ×).
Proof.
intros. unfold kcompose_dtm.
ext [w a]. rewrite preincr_extract.
reflexivity.
Qed.
(** Composition when <<g>> has no applicative effect *)
Theorem dtm_kleisli_57 {A B C} : forall
`{Applicative G1}
(g : W * B -> T C) (f : W * A -> G1 (T B)),
kcompose_dtm (G2 := fun A => A) g f = g ⋆dtm f.
Proof.
reflexivity.
Qed.
(** *** Composition when <<g>> has no substitution *)
(******************************************************************************)
(** Composition when <<g>> has no substitution *)
Theorem dtm_kleisli_67 {A B C} : forall
`{Applicative G1} `{Applicative G2}
(g : W * B -> G2 C) (f : W * A -> G1 (T B)),
(fmap G2 (ret T) ∘ g) ⋆dtm f = (fmap G2 (ret T) ∘ g) ⋆dtm f.
Proof.
reflexivity.
Qed.
(** Composition when neither <<g>> or <<f>> perform a substitution *)
Lemma kcompose_dtm_66 : forall
`{Applicative G1} `{Applicative G2}
`(g : W * B -> G2 C) `(f : W * A -> G1 B),
(fmap G2 (ret T) ∘ g) ⋆dtm (fmap G1 (ret T) ∘ f) =
fmap (G1 ∘ G2) (ret T) ∘ (kcompose_dt (G1 := G1) (G2 := G2) g f).
Proof.
intros. unfold kcompose_dtm.
ext [w a]. unfold compose at 2.
compose near (f (w, a)).
rewrite (fun_fmap_fmap G1).
rewrite (kdtm_binddt0 W T); auto.
rewrite preincr_ret.
unfold kcompose_dt. unfold_ops @Fmap_compose.
do 2 reassociate <-.
unfold_compose_in_compose.
rewrite (fun_fmap_fmap G1).
unfold strength.
unfold compose; cbn.
compose near (f (w, a)) on right.
rewrite (fun_fmap_fmap G1).
reflexivity.
Qed.
(** *** Composition when <<g>> has no applicative effect *)
(******************************************************************************)
(** Composition when neither <<g>> or <<f>> has an applicative effect *)
Lemma kcompose_dtm_55 : forall
`(g : W * B -> T C) `(f : W * A -> T B),
kcompose_dtm (G1 := fun A => A) (G2 := fun A => A) g f =
kcompose_dm g f.
Proof.
reflexivity.
Qed.
(** Composition when neither <<g>> or <<f>> has an applicative effect or substitution *)
Lemma kcompose_dtm_44 : forall
`(g : W * B -> C) `(f : W * A -> B),
kcompose_dtm (G1 := fun A => A) (G2 := fun A => A)
(ret T ∘ g) (ret T ∘ f) = ret T ∘ (g co⋆ f).
Proof.
intros. rewrite kcompose_dtm_55.
unfold kcompose_dm.
ext [w a].
intros. unfold_ops @Bindd_Binddt.
unfold compose. compose near (f (w, a)).
rewrite (kdtm_binddt0 W T _ _ (G := fun A => A)).
cbv. change (op w unit0) with (w ● Ƶ). now simpl_monoid.
Qed.
(** *** Composition when <<f>> has no applicative effect *)
(******************************************************************************)
(** Composition when <<f>> has no applicative effect *)
Theorem dtm_kleisli_75 {A B C} : forall
`{Applicative G2}
(g : W * B -> G2 (T C)) (f : W * A -> T B),
kcompose_dtm (G1 := fun A => A) g f = fun '(w, a) => binddt T G2 (preincr w g) (f (w, a)).
Proof.
reflexivity.
Qed.
(** Composition when <<f>> has no applicative effect, substitution, or context-sensitivity *)
Lemma kcompose_dtm_70 : forall
`{Applicative G}
`(g : W * B -> G (T C)) `(f : A -> B),
kcompose_dtm (G1 := fun A => A) (G2 := G)
g (ret T ∘ f ∘ extract (W ×)) = g ∘ fmap (W ×) f.
Proof.
intros. unfold kcompose_dtm.
ext [w a]. unfold compose.
cbn. compose near (f a) on left.
change (fmap (fun A => A) ?f) with f.
rewrite (kdtm_binddt0 W T _ _ (G := G)).
now rewrite preincr_ret.
Qed.
(** Composition when <<f>> is just a map *)
Theorem dtm_kleisli_70 {A B C} : forall
`{Applicative G2}
(g : W * B -> G2 (T C)) (f : A -> B),
kcompose_dtm (G1 := fun A => A) (G2 := G2) g
(ret T ∘ f ∘ extract (W ×)) = g ∘ fmap (W ×) f.
Proof.
intros. unfold kcompose_dtm.
ext [w a]. unfold compose. cbn.
compose near (f a) on left.
change (fmap (fun A => A) ?f) with f.
rewrite (kdtm_binddt0 W T); auto.
now rewrite (preincr_ret).
Qed.
(** Composition when <<f>> has no applicative effect or substitution *)
Lemma kcompose_dtm_74 : forall
`{Applicative G}
`(g : W * B -> G (T C)) `(f : W * A -> B),
kcompose_dtm (G1 := fun A => A) (G2 := G)
g (ret T ∘ f) = g co⋆ f.
Proof.
intros. unfold kcompose_dtm.
ext [w a]. unfold compose.
compose near (f (w, a)).
change (fmap (fun A => A) ?f) with f.
rewrite (kdtm_binddt0 W T _ _ (G := G)).
now rewrite preincr_ret.
Qed.
(** *** Others *)
(******************************************************************************)
(** Composition when <<f>> is context-agnostic *)
Theorem dtm_kleisli_73 {A B C} : forall
`{Applicative G1} `{Applicative G2}
(g : W * B -> G2 (T C)) (f : A -> G1 (T B)),
g ⋆dtm (f ∘ extract (W ×)) =
((fun '(w, t) => fmap G1 (binddt T G2 (preincr w g)) t) ∘ fmap (W ×) f).
Proof.
intros. unfold kcompose_dtm.
ext [w a]. unfold compose. cbn.
reflexivity.
Qed.
(** Composition when <<f>> has no substitution *)
Theorem dtm_kleisli_76 {A B C} : forall
`{Applicative G1} `{Applicative G2}
(g : W * B -> G2 (T C)) (f : W * A -> G1 B),
g ⋆dtm (fmap G1 (ret T) ∘ f) = g ⋆dt f.
Proof.
intros. unfold kcompose_dtm.
ext [w a]. unfold kcompose_dt.
unfold compose. cbn.
compose near (f (w, a)).
rewrite (fun_fmap_fmap G1).
rewrite (fun_fmap_fmap G1).
fequal.
rewrite (kdtm_binddt0 W T); auto.
now rewrite (preincr_ret).
Qed.
End kleisli_composition.
(** * Lesser Kleisli typeclass instances *)
(******************************************************************************)
Section instances.
Context
(W : Type)
(T : Type -> Type)
`{Kleisli.DT.Monad.Monad W T}.
(** ** Monad *)
(******************************************************************************)
Lemma kmon_bind0_T : forall (A B : Type) (f : A -> T B),
bind T f ∘ ret T = f.
Proof.
intros. unfold_ops @Bind_Binddt.
rewrite (kdtm_binddt0 W T _ _ _ (G := fun A => A)).
reflexivity.
Qed.
Lemma kmon_bind1_T : forall A : Type,
bind T (ret T) = @id (T A).
Proof.
intros. unfold_ops @Bind_Binddt.
now rewrite (kdtm_binddt1 W T).
Qed.
Lemma kmon_bind2_T : forall (B C : Type) (g : B -> T C) (A : Type) (f : A -> T B),
bind T g ∘ bind T f = bind T (g ⋆ f).
Proof.
intros. unfold_ops @Bind_Binddt.
change (binddt T (fun A0 : Type => A0) (g ∘ extract (prod W)))
with (fmap (fun A => A)
(binddt T (fun A0 : Type => A0) (g ∘ extract (prod W)))).
rewrite (kdtm_binddt2 W T _ _ _ (G1 := fun A => A) (G2 := fun A => A)).
fequal.
- now rewrite Mult_compose_identity1.
- change_left ((g ∘ extract (prod W)) ⋆dm (f ∘ extract (W ×))).
unfold kcompose_dm. ext [w a]. unfold compose at 2. cbn.
rewrite preincr_extract.
reflexivity.
Qed.
#[export] Instance KM_KDTM : Kleisli.Monad.Monad T :=
{| kmon_bind0 := kmon_bind0_T;
kmon_bind1 := kmon_bind1_T;
kmon_bind2 := kmon_bind2_T;
|}.
(** ** Decorated monad *)
(******************************************************************************)
Lemma kmond_bindd0_T : forall (A B : Type) (f : W * A -> T B),
bindd T f ∘ ret T = f ∘ ret (prod W).
Proof.
intros. unfold_ops @Bindd_Binddt.
now rewrite (kdtm_binddt0 W T _ _ _ (G := fun A => A)).
Qed.
Lemma kmond_bindd1_T : forall A : Type,
bindd T (ret T ∘ extract (prod W)) = @id (T A).
Proof.
intros. unfold_ops @Bindd_Binddt.
now rewrite (kdtm_binddt1 W T).
Qed.
Lemma kmond_bindd2_T : forall (B C : Type) (g : W * B -> T C) (A : Type) (f : W * A -> T B),
bindd T g ∘ bindd T f = bindd T (g ⋆dm f).
Proof.
intros. unfold_ops @Bindd_Binddt.
change (binddt T ?I g) with (fmap (fun A => A) (binddt T I g)).
rewrite (kdtm_binddt2 W T _ _ _ (G1 := fun A => A) (G2 := fun A => A)).
fequal. now rewrite Mult_compose_identity1.
Qed.
#[export] Instance KDM_KDTM: Kleisli.Decorated.Monad.Monad T :=
{| kmond_bindd0 := kmond_bindd0_T;
kmond_bindd1 := kmond_bindd1_T;
kmond_bindd2 := kmond_bindd2_T;
|}.
(** ** Traversable monad *)
(******************************************************************************)
Lemma ktm_bindt0_T : forall
(A B : Type) (G : Type -> Type) (H1 : Fmap G)
(H2 : Pure G) (H3 : Mult G),
Applicative G ->
forall f : A -> G (T B), bindt T G f ∘ ret T = f.
Proof.
intros. unfold_ops @Bindt_Binddt.
rewrite (kdtm_binddt0 W T); auto.
Qed.
Lemma ktm_bindt1_T : forall A : Type,
bindt T (fun A : Type => A) (ret T) = @id (T A).
Proof.
intros. unfold_ops @Bindt_Binddt.
now rewrite (kdtm_binddt1 W T).
Qed.
Lemma ktm_bindt2_T : forall
(A B C : Type) (G1 : Type -> Type) (H1 : Fmap G1)
(H2 : Pure G1) (H3 : Mult G1),
Applicative G1 ->
forall (G2 : Type -> Type) (H5 : Fmap G2) (H6 : Pure G2) (H7 : Mult G2),
Applicative G2 ->
forall (g : B -> G2 (T C)) (f : A -> G1 (T B)),
fmap G1 (bindt T G2 g) ∘ bindt T G1 f = bindt T (G1 ∘ G2) (g ⋆tm f).
Proof.
intros. unfold_ops @Bindt_Binddt.
rewrite (kdtm_binddt2 W T); auto.
fequal. rewrite (kcompose_dtm_33 W T).
reflexivity.
Qed.
Lemma ktm_morph_T : forall
(G1 G2 : Type -> Type) (H1 : Fmap G1) (H2 : Pure G1) (H3 : Mult G1) (H4 : Fmap G2)
(H5 : Pure G2) (H6 : Mult G2) (ϕ : forall A : Type, G1 A -> G2 A),
ApplicativeMorphism G1 G2 ϕ ->
forall (A B : Type) (f : A -> G1 (T B)),
ϕ (T B) ∘ bindt T G1 f = bindt T G2 (ϕ (T B) ∘ f).
Proof.
intros. unfold_ops @Bindt_Binddt.
now rewrite (kdtm_morph W T G1 G2).
Qed.
#[export] Instance KTM_KDTM: Traversable.Monad.Monad T :=
{| ktm_bindt0 := ktm_bindt0_T;
ktm_bindt1 := ktm_bindt1_T;
ktm_bindt2 := ktm_bindt2_T;
ktm_morph := ktm_morph_T;
|}.
(** ** Decorated-traversable functor *)
(******************************************************************************)
Lemma kdtfun_fmapdt1_T : forall A : Type,
fmapdt T (fun A0 : Type => A0) (extract (W ×)) = @id (T A).
Proof.
intros. unfold_ops @Fmapdt_Binddt.
change (fmap (fun A => A) ?f) with f.
now rewrite (kdtm_binddt1 W T).
Qed.
Lemma kdtfun_fmapdt2_T :
forall (G1 : Type -> Type) (H0 : Fmap G1) (H1 : Pure G1) (H2 : Mult G1) (H3 : Applicative G1)
(G2 : Type -> Type) (H4 : Fmap G2) (H5 : Pure G2) (H6 : Mult G2) (H7 : Applicative G2)
(B C : Type) (g : W * B -> G2 C) (A : Type) (f : W * A -> G1 B),
fmap G1 (fmapdt T G2 g) ∘ fmapdt T G1 f = fmapdt T (G1 ∘ G2) (g ⋆dt f).
Proof.
intros. unfold_ops @Fmapdt_Binddt.
rewrite (kdtm_binddt2 W T); auto.
fequal. now rewrite (kcompose_dtm_66 W T).
Qed.
Lemma kdtfun_morph_T :
forall (G1 G2 : Type -> Type) (H0 : Fmap G1) (H1 : Pure G1) (H2 : Mult G1)
(H3 : Fmap G2) (H4 : Pure G2) (H5 : Mult G2) (ϕ : forall A : Type, G1 A -> G2 A),
ApplicativeMorphism G1 G2 ϕ ->
forall (A B : Type) (f : W * A -> G1 B), fmapdt T G2 (ϕ B ∘ f) = ϕ (T B) ∘ fmapdt T G1 f.
Proof.
intros. unfold_ops @Fmapdt_Binddt.
rewrite (kdtm_morph W T _ _ (ϕ := ϕ)).
fequal. reassociate <-.
unfold compose. ext [w a]. now rewrite (appmor_natural G1 G2).
Qed.
#[export] Instance KDT_KDTM: DT.Functor.DecoratedTraversableFunctor W T :=
{| kdtfun_fmapdt1 := kdtfun_fmapdt1_T;
kdtfun_fmapdt2 := kdtfun_fmapdt2_T;
kdtfun_morph := kdtfun_morph_T;
|}.
(** ** Decorated functor *)
(******************************************************************************)
Lemma dfun_fmapd1_T : forall A : Type,
fmapd T (extract (W ×)) = @id (T A).
Proof.
intros. unfold_ops @Fmapd_Binddt.
now rewrite (kdtm_binddt1 W T).
Qed.
Lemma dfun_fmapd2_T : forall (A B C : Type) (g : W * B -> C) (f : W * A -> B),
fmapd T g ∘ fmapd T f = fmapd T (g ∘ cobind (W ×) f).
Proof.
intros. unfold_ops @Fmapd_Binddt.
change (binddt T (fun A0 : Type => A0) ?g) with
(fmap (fun A => A) (binddt T (fun A0 : Type => A0) g)) at 1.
rewrite (kdtm_binddt2 W T A B C (G1 := fun A => A) (G2 := fun A => A)).
fequal. now rewrite Mult_compose_identity2.
now rewrite (kcompose_dtm_44 W T).
Qed.
#[export] Instance KD_KDTM: Kleisli.Decorated.Functor.DecoratedFunctor W T :=
{| dfun_fmapd1 := dfun_fmapd1_T;
dfun_fmapd2 := dfun_fmapd2_T;
|}.
(** ** Traversable functor *)
(******************************************************************************)
Lemma trf_traverse_id_T : forall A : Type,
traverse T (fun A0 : Type => A0) (@id A) = id.
Proof.
unfold_ops @Traverse_Binddt @Fmap_I.
apply (kdtm_binddt1 W T).
Qed.
Lemma trf_traverse_traverse_T : forall (G1 G2 : Type -> Type) (H0 : Fmap G2) (H1 : Pure G2) (H2 : Mult G2),
Applicative G2 ->
forall (H4 : Fmap G1) (H5 : Pure G1) (H6 : Mult G1),
Applicative G1 ->
forall (B C : Type) (g : B -> G2 C) (A : Type) (f : A -> G1 B),
fmap G1 (traverse T G2 g) ∘ traverse T G1 f =
traverse T (G1 ∘ G2) (fmap G1 g ∘ f).
Proof.
intros. unfold_ops @Traverse_Binddt.
rewrite (kdtm_binddt2 W T); auto.
rewrite (kcompose_dtm_33 W T).
rewrite (kcompose_tm_ret T).
reflexivity.
Qed.
Lemma trf_traverse_morphism_T : forall (G1 G2 : Type -> Type) (H0 : Fmap G1) (H1 : Pure G1)
(H2 : Mult G1) (H3 : Fmap G2) (H4 : Pure G2)
(H5 : Mult G2) (ϕ : forall A : Type, G1 A -> G2 A),
ApplicativeMorphism G1 G2 ϕ ->
forall (A B : Type) (f : A -> G1 B),
ϕ (T B) ∘ traverse T G1 f = traverse T G2 (ϕ B ∘ f).
Proof.
intros. unfold_ops @Traverse_Binddt.
rewrite (kdtm_morph W T G1 G2).
do 2 reassociate <- on left.
fequal. unfold compose; ext x.
inversion H8.
rewrite appmor_natural.
reflexivity.
Qed.
#[export] Instance KT_KDTM: Traversable.Functor.TraversableFunctor T :=
{| trf_traverse_id := trf_traverse_id_T;
trf_traverse_traverse := trf_traverse_traverse_T;
trf_traverse_morphism := trf_traverse_morphism_T;
|}.
(** ** Functor *)
(******************************************************************************)
End instances.
Section binddt.
Context
(T : Type -> Type)
(G1 G2 : Type -> Type)
`{DT.Monad.Monad W T}
`{Applicative G1}
`{Applicative G2}
{A B C : Type}.
(** ** <<binddt>> on the right *)
(******************************************************************************)
Lemma bindd_binddt: forall
(g : W * B -> T C)
(f : W * A -> G1 (T B)),
fmap G1 (bindd T g) ∘ binddt T G1 f =
binddt T G1 (fun '(w, a) => fmap G1 (bindd T (preincr w g)) (f (w, a))).
Proof.
intros. unfold_ops @Bindd_Binddt.
rewrite (kdtm_binddt2 W T A B C (G1 := G1) (G2 := fun A => A)).
fequal. now rewrite Mult_compose_identity1.
Qed.
Lemma fmapdt_binddt: forall
(g : W * B -> G2 C)
(f : W * A -> G1 (T B)),
fmap G1 (fmapdt T G2 g) ∘ binddt T G1 f =
binddt T (G1 ∘ G2) (fun '(w, a) => fmap G1 (fmapdt T G2 (preincr w g)) (f (w, a))).
Proof.
intros. unfold_ops @Fmapdt_Binddt.
rewrite (kdtm_binddt2 W T A B C (G1 := G1) (G2 := G2)).
reflexivity.
Qed.
Lemma bindt_binddt: forall
(g : B -> G2 (T C))
(f : W * A -> G1 (T B)),
fmap G1 (bindt T G2 g) ∘ binddt T G1 f =
binddt T (G1 ∘ G2) (fmap G1 (bindt T G2 g) ∘ f).
Proof.
intros. unfold_ops @Bindt_Binddt.
rewrite (kdtm_binddt2 W T A B C (G1 := G1) (G2 := G2)).
fequal. unfold kcompose_dtm. ext [w a].
now rewrite preincr_extract.
Qed.
Lemma bind_binddt: forall
(g : B -> T C)
(f : W * A -> G1 (T B)),
fmap G1 (bind T g) ∘ binddt T G1 f =
binddt T G1 (fmap G1 (bind T g) ∘ f).
Proof.
intros. unfold_ops @Bindt_Binddt.
unfold_ops @Bind_Binddt.
rewrite (kdtm_binddt2 W T A B C (G1 := G1) (G2 := fun A => A)).
fequal.
- now rewrite Mult_compose_identity1.
- ext [w a]. cbn. now rewrite (preincr_extract).
Qed.
Lemma fmapd_binddt: forall
(g : W * B -> C)
(f : W * A -> G1 (T B)),
fmap G1 (fmapd T g) ∘ binddt T G1 f =
binddt T G1 (fun '(w, a) => fmap G1 (fmapd T (preincr w g)) (f (w, a))).
Proof.
intros. unfold_ops @Fmapd_Binddt.
rewrite (kdtm_binddt2 W T A B C (G1 := G1) (G2 := fun A => A)).
fequal. now rewrite Mult_compose_identity1.
Qed.
Lemma fmapt_binddt: forall
(g : B -> G2 C)
(f : W * A -> G1 (T B)),
fmap G1 (traverse T G2 g) ∘ binddt T G1 f =
binddt T (G1 ∘ G2) (fmap G1 (traverse T G2 g) ∘ f).
Proof.
intros.
intros. unfold_ops @Traverse_Binddt.
rewrite (kdtm_binddt2 W T A B C (G1 := G1) (G2 := G2)).
fequal. ext [w a]. cbn.
now rewrite preincr_extract.
Qed.
(** ** <<binddt>> on the left *)
(******************************************************************************)
Lemma binddt_bindd: forall
(g : W * B -> G2 (T C))
(f : W * A -> T B),
binddt T G2 g ∘ bindd T f =
binddt T G2 (fun '(w, a) => binddt T G2 (preincr w g) (f (w, a))).
Proof.
intros. unfold_ops @Bindd_Binddt.
change (binddt T G2 g) with (fmap (fun A => A) (binddt T G2 g)).
rewrite (kdtm_binddt2 W T A B C (G1 := fun A => A)).
fequal. now rewrite Mult_compose_identity2.
Qed.
Lemma binddt_fmapdt: forall
(g : W * B -> G2 (T C))
(f : W * A -> G1 B),
fmap G1 (binddt T G2 g) ∘ fmapdt T G1 f =
binddt T (G1 ∘ G2) (fun '(w, a) => fmap G1 (fun b => g (w, b)) (f (w, a))).
Proof.
intros. unfold_ops @Fmapdt_Binddt.
rewrite (kdtm_binddt2 W T A B C).
fequal. ext [w a]. unfold compose; cbn.
compose near (f (w, a)) on left.
rewrite (fun_fmap_fmap G1).
fequal. ext b. unfold compose; cbn.
compose near b on left.
rewrite (kdtm_binddt0 W T); auto.
now rewrite preincr_ret.
Qed.
Lemma binddt_bindt: forall
(g : W * B -> G2 (T C))
(f : A -> G1 (T B)),
fmap G1 (binddt T G2 g) ∘ bindt T G1 f =
binddt T (G1 ∘ G2) (fun '(w, a) => fmap G1 (binddt T G2 (preincr w g)) (f a)).
Proof.
intros. unfold_ops @Bindt_Binddt.
now rewrite (kdtm_binddt2 W T).
Qed.
Lemma binddt_bind: forall
(g : W * B -> G2 (T C))
(f : A -> T B),
binddt T G2 g ∘ bind T f =
binddt T G2 (fun '(w, a) => binddt T G2 (preincr w g) (f a)).
Proof.