-
Notifications
You must be signed in to change notification settings - Fork 12
/
monad_transformer.v
1156 lines (935 loc) · 34.2 KB
/
monad_transformer.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
(* monae: Monadic equational reasoning in Coq *)
(* Copyright (C) 2020 monae authors, license: LGPL-2.1-or-later *)
From mathcomp Require Import all_ssreflect.
From mathcomp Require boolp.
Require Import preamble.
From HB Require Import structures.
Require Import hierarchy monad_lib fail_lib state_lib.
(******************************************************************************)
(* Formalization of monad transformers *)
(* *)
(* This file corresponds to the formalization of [Mauro Jaskelioff, Modular *)
(* Monad Transformers, ESOP 2009] (roughly, up to Sect. 5, Example 22). *)
(* *)
(* Module MonadMLaws == laws of monad morphisms *)
(* monadM == type of monad morphisms *)
(* monadT == type of monad transformers, i.e., functions t of *)
(* type monad -> monad equipped with an operator Lift *)
(* such that for any monad M Lift t M is a monad *)
(* morphism from M to t M *)
(* instances: *)
(* - stateT: the state monad transformer *)
(* - exceptT: the exception monad transformer *)
(* - envT: environment *)
(* - outputT *)
(* - contT: continuation *)
(* mapStateT2 == standard utility function *)
(* lifting == lifting of a sigma-operation along a monad morphism *)
(* alifting == algebraic operation defined using an algebraic *)
(* operation op and a monad morphism e *)
(* uniform_algebraic_lifting == Theorem: alifting is a lifting *)
(* *)
(* functorial == type of functors where the action on objects as type *)
(* monad -> monad *)
(* fmt == type of functorial monad transformers *)
(* instances: *)
(* - exceptFMT *)
(* - stateFMT *)
(* - envFMT *)
(* - outputFMT *)
(* *)
(******************************************************************************)
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Local Open Scope monae_scope.
Module MonadMLaws.
Section monadmlaws.
Variables (M N : monad).
Definition ret (e : M ~~> N) := forall A : UU0, e A \o Ret = Ret.
Definition bind (e : M ~~> N) := forall (A B : UU0) (m : M A) (f : A -> M B),
e B (m >>= f) = e A m >>= (e B \o f).
End monadmlaws.
End MonadMLaws.
HB.mixin Record isMonadM (M N : monad) (e : M ~~> N) := {
monadMret : MonadMLaws.ret e ;
monadMbind : MonadMLaws.bind e }.
#[short(type=monadM)]
HB.structure Definition MonadM (M N : monad) :=
{e of isMonadM M N e & isNatural M N e}.
HB.factory Record isMonadM_ret_bind (M N : monad) (e : M ~~> N) := {
monadMret : MonadMLaws.ret e ;
monadMbind : MonadMLaws.bind e }.
HB.builders Context (M N : monad) (e : M ~~> N) of isMonadM_ret_bind M N e.
Lemma naturality_monadM : naturality M N e.
Proof.
move=> A B h; apply boolp.funext => m /=.
by rewrite !fmapE monadMbind (compA (e B)) monadMret.
Qed.
HB.instance Definition _ := isNatural.Build M N e naturality_monadM.
HB.instance Definition _ := isMonadM.Build M N e monadMret monadMbind.
HB.end.
HB.mixin Record isMonadT (T : monad -> monad) := {
Lift : forall M, monadM M (T M) }.
#[short(type=monadT)]
HB.structure Definition MonadT := {T of isMonadT T}.
Arguments Lift : clear implicits.
Section state_monad_transformer.
Variables (S : UU0) (M : monad).
Definition MS := fun A : UU0 => S -> M (A * S)%type.
Definition retS : idfun ~~> MS := fun A : UU0 => curry Ret.
Definition bindS (A B : UU0) (m : MS A) f : MS B := fun s => m s >>= uncurry f.
Let bindSretf : BindLaws.left_neutral bindS retS.
Proof.
by move=> A B a f; rewrite /bindS; apply boolp.funext => s; rewrite bindretf.
Qed.
Let bindSmret : BindLaws.right_neutral bindS retS.
Proof.
move=> A m; rewrite /bindS; apply boolp.funext => s.
by rewrite -[in RHS](bindmret (m s)); bind_ext; case.
Qed.
Let bindSA : BindLaws.associative bindS.
Proof.
move=> A B C m f g; rewrite /bindS; apply boolp.funext => s.
by rewrite bindA; bind_ext; case.
Qed.
HB.instance Definition _ :=
isMonad_ret_bind.Build MS bindSretf bindSmret bindSA.
Lemma MS_mapE (A B : UU0) (f : A -> B) (m : MS A) :
([the functor of MS] # f) m = (M # (fun x => (f x.1, x.2))) \o m.
Proof.
apply boolp.funext=> s.
rewrite {1}/actm /= /bindS /= fmapE.
congr bind.
by apply: boolp.funext; case.
Qed.
Definition liftS (A : UU0) (m : M A) : MS A :=
fun s => m >>= (fun x => Ret (x, s)).
Let retliftS : MonadMLaws.ret liftS.
Proof.
move=> A; rewrite /liftS; apply: boolp.funext => a /=; apply: boolp.funext => s /=.
by rewrite bindretf.
Qed.
Let bindliftS : MonadMLaws.bind liftS.
Proof.
move=> A B m f; rewrite {1}/liftS; apply: boolp.funext => s.
rewrite [in LHS]bindA.
transitivity (liftS m s >>= uncurry (@liftS B \o f)) => //.
rewrite [in RHS]bindA.
by under [RHS]eq_bind do rewrite bindretf.
Qed.
HB.instance Definition _ := isMonadM_ret_bind.Build
M MS liftS retliftS bindliftS.
End state_monad_transformer.
Lemma MS_bindE [S : UU0] [M : monad] [A B : UU0] (m : MS S M A) (f : A -> MS S M B) s :
(m >>= f) s = m s >>= uncurry f.
Proof. by []. Qed.
Definition stateT : UU0 -> monad -> monad := MS.
HB.instance Definition _ (S : UU0) := isMonadT.Build
(stateT S) (@liftS S).
Definition mapStateT2 (A S : UU0) (N1 N2 N3 : monad)
(f : N1 (A * S)%type -> N2 (A * S)%type -> N3 (A * S)%type)
(m1 : stateT S N1 A) (m2 : stateT S N2 A) : stateT S N3 A :=
fun s => f (m1 s) (m2 s).
Section stateMonad_of_stateT.
Variables (S : UU0) (M : monad).
Let Put : S -> MS S M unit := fun s _ => Ret (tt, s).
Let Get : MS S M S := fun s => Ret (s, s).
Let bindputput s s' : Put s >> Put s' = Put s'.
Proof.
rewrite /Ret.
rewrite bindE /= /bindS; apply: boolp.funext => s0.
by rewrite MS_mapE bind_fmap bindretf.
Qed.
Let bindputget s : Put s >> Get = Put s >> Ret s.
Proof.
rewrite !bindE !MS_mapE /= /bindS.
apply: boolp.funext => s'.
by rewrite !bind_fmap !bindretf.
Qed.
Let bindgetput : Get >>= Put = skip.
Proof.
rewrite bindE MS_mapE /= /bindS.
by apply: boolp.funext => s; rewrite bind_fmap bindretf.
Qed.
Let bindgetget (A : UU0) (k : S -> S -> stateT S M A) :
Get >>= (fun s => Get >>= k s) = Get >>= (fun s => k s s).
Proof.
apply: boolp.funext => s.
rewrite /Get/=.
rewrite !bindE !MS_mapE /= /bindS /= /retS/=.
rewrite !bind_fmap !bindretf/=.
rewrite !bindE !MS_mapE /= /bindS /= /retS/=.
by rewrite bind_fmap bindretf.
Qed.
HB.instance Definition _ := @isMonadState.Build
S (MS S M) Get Put bindputput bindputget bindgetput bindgetget.
End stateMonad_of_stateT.
Section exception_monad_transformer.
Local Obligation Tactic := idtac.
Variables (Z : UU0) (* the type of exceptions *) (M : monad).
(* action on objects of the transformed monad *)
Definition MX := fun X : UU0 => M (Z + X)%type.
(* unit and bind operator of the transformed monad *)
Definition retX : idfun ~~> MX := fun X x => Ret (inr x).
Definition bindX X Y (t : MX X) (f : X -> MX Y) : MX Y :=
t >>= fun c => match c with inl z => Ret (inl z) | inr x => f x end.
Let bindXretf : BindLaws.left_neutral bindX retX.
Proof. by move=> A B a f; rewrite /bindX bindretf. Qed.
Let bindXmret : BindLaws.right_neutral bindX retX.
Proof.
by move=> A m; rewrite /bindX -[in RHS](bindmret m); bind_ext; case.
Qed.
Let bindXA : BindLaws.associative bindX.
Proof.
move=> A B C m f g; rewrite /bindX bindA; bind_ext; case => //.
by move=> z; rewrite bindretf.
Qed.
HB.instance Definition _ :=
isMonad_ret_bind.Build MX bindXretf bindXmret bindXA.
Lemma MX_mapE (A B : UU0) (f : A -> B) :
[the functor of MX] # f = M # (fun x => match x with inl y => inl y | inr y => inr (f y) end).
Proof.
apply: boolp.funext => m.
rewrite {1}/actm /= /bindX /= [in RHS]fmapE.
congr (_ _).
by apply: boolp.funext; case.
Qed.
Definition liftX X (m : M X) : MX X := m >>= (@ret [the monad of MX] _).
Let retliftX : MonadMLaws.ret liftX.
Proof.
by move=> A; apply: boolp.funext => a; rewrite /liftX /= bindretf.
Qed.
Let bindliftX : MonadMLaws.bind liftX.
Proof.
move=> A B m f; rewrite {1}/liftX.
rewrite !bindE !fmapE.
rewrite /= /join_of_bind /bindX /= !bindA.
rewrite 2!joinE !bindA.
bind_ext => a.
rewrite 3!bindretf.
rewrite /liftX /=.
bind_ext => b.
by rewrite bindretf.
Qed.
HB.instance Definition _ := isMonadM_ret_bind.Build
M [the monad of MX] liftX retliftX bindliftX.
End exception_monad_transformer.
Definition exceptT (Z : UU0) := fun M => [the monad of MX Z M].
HB.instance Definition _ (Z : UU0) := isMonadT.Build
(exceptT Z) (fun M => [the monadM M [the monad of MX Z M] of @liftX Z M]).
Section failMonad_of_exceptT.
Variable M : monad.
Let N (B : UU0) := M (unit + B)%type.
Let Fail : forall B, N B := fun B => Ret (inl tt).
Let bindfail : BindLaws.left_zero (@bind (exceptT unit M)) Fail.
Proof.
move=> A B g; rewrite /Fail.
by rewrite bindE /= /join_of_bind /bindX /= fmapE /= bindA 2!bindretf.
Qed.
HB.instance Definition _ := @isMonadFail.Build (MX unit M) Fail bindfail.
End failMonad_of_exceptT.
Section exceptMonad_of_exceptT.
Variable (M : monad).
Let N (B : UU0) := M (unit + B)%type.
Let Catch (B : UU0) (x : N B) (y : N B) : N B :=
x >>= (fun u => (fun k k' => match u with | inl a => k a | inr b => k' b end)
(fun=> y) (fun b => Ret (inr b))).
Let Catchmfail (A : UU0) :
right_id (@fail [the failMonad of MX unit M] A) (@Catch A).
Proof.
move=> x; rewrite /Catch /= /fail /= -[in RHS](bindmret x); bind_ext.
by case=> /= [[]|].
Qed.
Let Catchfailm (A : UU0) :
left_id (@fail [the failMonad of MX unit M] A) (@Catch A).
Proof.
move=> x; rewrite /N in x *.
by rewrite /Catch /= /fail /= bindretf.
Qed.
Let CatchA (A : UU0) : associative (@Catch A).
Proof.
move=> x y z; rewrite /Catch bindA; bind_ext.
by case=> [[]//|b]; rewrite bindretf.
Qed.
Let Catchret (A : UU0) (ua : A%type) :
@left_zero (N A) (N A) (Ret (inr ua)) (@Catch A).
Proof. by move=> n ; rewrite /Catch /= bindretf. Qed.
HB.instance Definition _ := @isMonadExcept.Build
(MX unit M) Catch Catchmfail Catchfailm CatchA Catchret.
End exceptMonad_of_exceptT.
Section environment_monad_transformer.
Local Obligation Tactic := idtac.
Variables (R : UU0) (M : monad).
Definition MEnv := fun A : UU0 => R -> M A.
Definition retEnv : idfun ~~> MEnv := fun (A : UU0) a r => Ret a.
Definition bindEnv A B (m : MEnv A) f : MEnv B :=
fun r => m r >>= (fun a => f a r).
Let bindEnvretf : BindLaws.left_neutral bindEnv retEnv.
Proof.
by move=> A B a f; rewrite /bindEnv; apply: boolp.funext => r; rewrite bindretf.
Qed.
Let bindEnvmret : BindLaws.right_neutral bindEnv retEnv.
Proof.
move=> A m; rewrite /bindEnv; apply: boolp.funext => r.
rewrite -[in RHS](bindmret (m r)); by bind_ext; case.
Qed.
Let bindEnvA : BindLaws.associative bindEnv.
Proof.
move=> A B C m f g; rewrite /bindEnv; apply: boolp.funext => r.
by rewrite bindA; bind_ext; case.
Qed.
HB.instance Definition _ :=
isMonad_ret_bind.Build MEnv bindEnvretf bindEnvmret bindEnvA.
Lemma MEnv_mapE (A B : UU0) (f : A -> B) (m : MEnv A) :
(MEnv # f) m = (M # f) \o m.
Proof. by apply: boolp.funext => r; rewrite /= fmapE. Qed.
Definition liftEnv A (m : M A) : MEnv A := fun r => m.
Let retliftEnv : MonadMLaws.ret liftEnv. Proof. by []. Qed.
Let bindliftEnv : MonadMLaws.bind liftEnv. Proof. by []. Qed.
HB.instance Definition envTmonadM := isMonadM_ret_bind.Build
M [the monad of MEnv] liftEnv retliftEnv bindliftEnv.
End environment_monad_transformer.
Definition envT (E : UU0) := fun M => [the monad of MEnv E M].
HB.instance Definition _ (E : UU0) := isMonadT.Build (envT E)
(fun M => [the monadM M [the monad of MEnv E M] of @liftEnv E M]).
(* traces monad transformer? *)
Section output_monad_transformer.
Local Obligation Tactic := idtac.
Variables (R : UU0) (M : monad).
Definition MO (X : UU0) := M (X * seq R)%type.
Definition retO : idfun ~~> MO := fun (A : UU0) a => Ret (a, [::]).
Definition bindO A B (m : MO A) (f : A -> MO B) : MO B :=
m >>= (fun o => let: (x, w) := o in f x >>=
(fun o' => let (x', w') := o' in Ret (x', w ++ w'))).
Let bindOretf : BindLaws.left_neutral bindO retO.
Proof.
move=> A B a f; rewrite /bindO /= bindretf /=.
rewrite (_ : (fun o' : B * seq R => _) = (fun o => Ret o)) ?bindmret //.
by apply: boolp.funext => -[].
Qed.
Let bindOmret : BindLaws.right_neutral bindO retO.
Proof.
move=> A m; rewrite /bindO /= /retO /= -[RHS]bindmret.
by bind_ext => -[a w]; rewrite bindretf cats0.
Qed.
Let bindOA : BindLaws.associative bindO.
Proof.
move=> A B C m f g; rewrite /bindO /=.
rewrite bindA; bind_ext; case=> x w.
rewrite !bindA; bind_ext; case=> x' w'.
rewrite !bindA bindretf; bind_ext; case=> x'' w''.
by rewrite bindretf catA.
Qed.
HB.instance Definition _ :=
isMonad_ret_bind.Build MO bindOretf bindOmret bindOA.
Lemma MO_mapE (A B : UU0) (f : A -> B) (m : MO A) :
([the functor of MO] # f) m = (M # (fun x => (f x.1, x.2))) m.
Proof.
rewrite [in LHS]/actm /= /bindO /= [in RHS]fmapE.
congr (_ _).
apply: boolp.funext => -[] ? ?.
by rewrite bindretf cats0.
Qed.
Definition liftO A (m : M A) : MO A := m >>= (fun x => Ret (x, [::])).
Let retliftO : MonadMLaws.ret liftO.
Proof.
move=> a; rewrite /liftO /= /retO; apply: boolp.funext => o /=.
by rewrite bindretf.
Qed.
Let bindliftO : MonadMLaws.bind liftO.
Proof.
move=> A B m f; rewrite {1}/liftO.
rewrite !bindE !fmapE /= /join_of_bind /bindO /= 2!joinE !bindA.
bind_ext => a.
rewrite /= !bindretf /liftO bindA.
bind_ext => b.
by rewrite bindretf /= bindretf.
Qed.
HB.instance Definition outputTmonadM := isMonadM_ret_bind.Build
M [the monad of MO] liftO retliftO bindliftO.
End output_monad_transformer.
Definition outputT (R : UU0) := fun M => [the monad of MO R M].
HB.instance Definition _ (R : UU0) := isMonadT.Build (outputT R)
(fun M => [the monadM M [the monad of MO R M] of @liftO R M]).
Section continuation_monad_tranformer.
Local Obligation Tactic := idtac.
Variables (r : UU0) (M : monad).
Definition MC : UU0 -> UU0 := fun A => (A -> M r) -> M r %type.
Definition retC : idfun ~~> MC := fun (A : UU0) (a : A) k => k a.
Definition bindC A B (m : MC A) f : MC B := fun k => m (f^~ k).
Let bindCretf : BindLaws.left_neutral bindC retC.
Proof. by []. Qed.
Let bindCmret : BindLaws.right_neutral bindC retC.
Proof. by []. Qed.
Let bindCA : BindLaws.associative bindC.
Proof. by []. Qed.
HB.instance Definition _ :=
isMonad_ret_bind.Build MC bindCretf bindCmret bindCA.
Lemma MC_mapE (A B : UU0) (f : A -> B) (m : MC A) :
([the functor of MC] # f) m = fun k : B -> M r => m (k \o f).
Proof. by []. Qed.
Definition liftC A (x : M A) : MC A := fun k => x >>= k.
Let retliftC : MonadMLaws.ret liftC.
Proof.
move => A; rewrite /liftC; apply: boolp.funext => a /=.
by apply: boolp.funext => s; rewrite bindretf.
Qed.
Let bindliftC : MonadMLaws.bind liftC.
Proof.
move => A B m f; rewrite /liftC; apply: boolp.funext => cont.
by rewrite 3!bindA.
Qed.
HB.instance Definition contTmonadM := isMonadM_ret_bind.Build
M [the monad of MC] liftC retliftC bindliftC.
End continuation_monad_tranformer.
Definition contT (r : UU0) := fun M => [the monad of MC r M].
HB.instance Definition _ (r : UU0) := isMonadT.Build (contT r)
(fun M => [the monadM M [the monad of MC r M] of @liftC r M]).
Definition abortT r X (M : monad) A : contT r M A := fun _ : A -> M r => Ret X.
Arguments abortT {r} _ {M} {A}.
Section contMonad_of_contT.
Variables (r : UU0) (M : monad).
Let N := MC r M.
Let Callcc (A B : UU0) : ((A -> N B) -> N A) -> N A :=
fun k f => k (fun a => fun=> f a) f.
Let Callcc0 (A B : UU0) (g : (A -> N B) -> N A) (k : B -> N B) :
@Callcc _ _ (fun f => g (fun x => f x >>= k)) = @Callcc _ _ g.
Proof. by []. Qed.
Let Callcc1 (A B : UU0) (m : N B) : @Callcc _ _ (fun _ : B -> N A => m) = m.
Proof. by []. Qed.
Let Callcc2 (A B C : UU0) (m : N A) x (k : A -> B -> N C) :
@Callcc _ _ (fun f : _ -> N _ => m >>= (fun a => f x >>= (fun b => k a b))) =
@Callcc _ _ (fun f : _ -> N _ => m >> f x).
Proof. by []. Qed.
Let Callcc3 (A B : UU0) (m : N A) b :
@Callcc _ _ (fun f : B -> N B => m >> f b) = @Callcc _ _ (fun _ : B -> N B => m >> Ret b).
Proof. by []. Qed.
HB.instance Definition _ := @isMonadContinuation.Build
(MC r M) Callcc Callcc0 Callcc1 Callcc2 Callcc3.
End contMonad_of_contT.
Section continuation_monad_transformer_examples.
Fixpoint for_loop (M : monad) (it min : nat) (body : nat -> contT unit M unit) : M unit :=
if it <= min then Ret tt
else if it is it'.+1 then
(body it') (fun _ => for_loop it' min body)
else Ret tt.
Section for_loop_lemmas.
Variable M : monad.
Implicit Types body : nat -> contT unit M unit.
Lemma loop0 i body : for_loop i i body = Ret tt.
Proof.
by case i => //= n; rewrite ltnS leqnn.
Qed.
Lemma loop1 i j body : for_loop (i.+1 + j) i body =
(body (i + j)) (fun _ => for_loop (i + j) i body).
Proof.
rewrite /=.
by case : ifPn ; rewrite ltnNge leq_addr.
Qed.
Lemma loop2 i j body :
body (i + j) = abortT tt -> for_loop (i + j).+1 i body = Ret tt.
Proof.
move=> Hbody /=.
case : ifPn => Hcond.
- reflexivity.
- by rewrite Hbody /= /abortT.
Qed.
End for_loop_lemmas.
(* TODO : instantiate with RunStateMonad *)
Definition foreach (M : monad) (items : list nat) (body : nat -> contT unit M unit) : M unit :=
foldr
(fun x next => (body x) (fun _ => next))
(Ret tt)
items.
(* Lemma loop3 : forall i j body, *)
(* foreach (i + j).+1 i body = Ret tt -> body (i + j) = @abort_tt m unit. *)
(* Proof. *)
(* move => i j body /=. *)
(* case : ifPn => //; rewrite ltnNge; rewrite leq_addr. *)
(* by []. *)
(* move => _ /= Hfor. *)
(* have Hcont2 : forall cont, body (i + j) = @abort_tt m unit -> body (i + j) cont = Ret tt. *)
(* (* split. *) *)
(* rewrite /= /abort_tt /abort. *)
(* by rewrite funeqE. *)
(* have Hcont1 : (forall cont, body (i + j) cont = Ret tt) -> body (i + j) = @abort_tt m unit. *)
(* move => Hcont. *)
(* rewrite /= /abort_tt /abort. *)
(* rewrite funeqE => k. *)
(* exact: Hcont. *)
(* apply Hcont1. *)
(* move => cont. *)
(* rewrite Hcont2 //. *)
(* set bl := (fun _ : unit => foreach (i + j) i body). *)
(* (* Check (fun _ : unit => foreach (i + j) i body). *) *)
(* generalize (Hcont1 bl). *)
(* move => bl. *)
(* Qed *)
Section sum.
Variables M : stateMonad nat.
Let sum n : M unit := for_loop n O
(fun i : nat => liftC (get >>= (fun z => put (z + i)))).
Lemma sum_test n :
sum n = get >>= (fun m => put (m + sumn (iota 0 n))).
Proof.
elim: n => [|n ih].
rewrite /sum.
rewrite loop0.
rewrite (_ : sumn (iota 0 0) = 0) //.
rewrite -[LHS]bindskipf.
rewrite -getput.
rewrite bindA.
bind_ext => a.
rewrite addn0.
rewrite -[RHS]bindmret.
bind_ext.
by case.
rewrite /sum -add1n loop1 /liftC bindA; bind_ext => m.
rewrite -/(sum n) {}ih -bindA.
rewrite putget bindA bindretf putput.
congr put.
by rewrite add0n (addnC 1) iotaD /= sumn_cat /= add0n addn0 /= addnAC addnA.
Qed.
Example sum_from_0_to_10 : M unit :=
foreach (iota 100 0) (fun i => if i > 90 then
abortT tt
else
liftC (get >>= (fun z => put (z + i)))).
End sum.
End continuation_monad_transformer_examples.
(* laws about lifted fail *)
Lemma bindLfailf (M : failMonad) S :
BindLaws.left_zero (@bind (stateT S M)) (Lift _ _ ^~ fail).
Proof.
move=> A B g /=; rewrite /liftS; apply: boolp.funext => s; rewrite bindfailf.
set x := (X in X >>= _ = _).
by rewrite (_ : x = fail) ?bindfailf// /x bindfailf.
Qed.
Lemma bindmLfail (M : failR0Monad) S :
BindLaws.right_zero (@bind (stateT S M)) (Lift _ _ ^~ fail).
Proof.
move=> A B m /=; rewrite /liftS/=; apply/boolp.funext => s; rewrite bindfailf.
set x := (X in _ >>= X = _).
rewrite (_ : x = fun=> fail) ?bindmfail//.
by apply/boolp.funext=> -[b s']; rewrite /x/= bindfailf.
Qed.
Section lifting.
Variables (E : functor) (M : monad) (op : E.-operation M)
(N : monad) (e : monadM M N).
Definition lifting (f : E \o N ~~> N) :=
forall X : UU0, e X \o op X = f X \o (E # e X).
End lifting.
Section liftingt.
Variables (E : functor) (M : monad) (op : E.-operation M)
(t : monadT).
Definition lifting_monadT := lifting op (Lift t M).
End liftingt.
Section proposition17.
Section psi.
Variables (E : functor) (M : monad).
Definition psi' (n : E ~~> M) : E \o M ~~> M := fun X => Join \o n (M X).
Lemma natural_psi' (n : E ~> M) : naturality [the functor of E \o M] M (psi' n).
Proof.
move=> A B h; rewrite /psi'.
rewrite -/(Join \o n (M A)) [LHS]compA natural.
by rewrite -compA (natural n).
Qed.
HB.instance Definition _ (n : E ~> M) := isNatural.Build
[the functor of E \o M] M (psi' n) (natural_psi' n).
Lemma algebraic_psi' (n : E ~> M) : algebraicity (psi' n).
Proof.
move=> A B g t.
rewrite bindE.
rewrite -(compE (M # g)).
rewrite {1}/psi'.
rewrite [in LHS](compA (M # g) Join).
rewrite /=.
rewrite -[in X in _ = Join X]compE.
rewrite -[in RHS](natural n).
transitivity (Join ((M # (Join \o (M # g))) (n (M A) t))); last first.
rewrite -(compE _ (n (M A)) t).
congr (Join ((M # _ \o _ ) t)).
apply: boolp.funext => ma.
by rewrite bindE.
rewrite -[in X in Join X = _]compE.
rewrite (natural join).
rewrite functor_o.
rewrite -[in RHS]FCompE.
rewrite -[RHS]compE.
rewrite [in RHS]compA.
by rewrite joinA.
Qed.
HB.instance Definition _ (n : E ~> M) := isAOperation.Build
E M (psi' n) (algebraic_psi' n).
Definition psi (n : E ~> M) := [the E.-aoperation M of psi' n].
Lemma psiE (n : E ~> M) A : psi n A = Join \o n (M A).
Proof. by []. Qed.
End psi.
Section phi.
Variables (E : functor) (M : monad).
Definition phi' (op : E.-operation M) : E ~~> M := fun X => op X \o (E # Ret).
Lemma natural_phi' (op : E.-operation M) : naturality E M (phi' op).
Proof.
move=> A B h; rewrite /phi'.
rewrite compA.
rewrite (natural op).
rewrite -compA.
rewrite -[in RHS]compA.
congr (_ \o _).
rewrite /=.
rewrite -2!(@functor_o E).
by rewrite (natural ret) FIdE.
Qed.
HB.instance Definition _ (op : E.-operation M) := isNatural.Build
E M (phi' op) (natural_phi' op).
Definition phi (op : E.-operation M) := [the _ ~> _ of phi' op].
Lemma phiE (op : E.-operation M) (X : UU0) : phi op X = op X \o (E # Ret).
Proof. by []. Qed.
End phi.
Section bijection.
Variables (E : functor) (M : monad).
Lemma psiK (op : E ~> M) : phi (psi op) = op.
Proof.
apply/nattrans_ext => A.
rewrite phiE /= psiE; apply: boolp.funext => m /=.
rewrite -(compE (op _)) -(natural op) -(compE Join).
by rewrite compA joinMret.
Qed.
Lemma phiK (op : E.-aoperation M) : psi (phi op) = op.
Proof.
apply/aoperation_ext => A.
rewrite /=.
rewrite psiE phiE; apply boolp.funext => m /=.
rewrite -(compE (op _)) joinE.
rewrite (algebraic op).
rewrite -(compE (E # _)) -functor_o.
rewrite -(compE (op _)).
set x := _^~ id.
rewrite (_ : x = Join); last first.
apply: boolp.funext => mma.
by rewrite /x bindE functor_id (*TODO: lemma*).
by rewrite joinretM functor_id compfid.
Qed.
End bijection.
End proposition17.
Section uniform_algebraic_lifting.
Variables (E : functor) (M : monad) (op : E.-aoperation M).
Variables (N : monad) (e : monadM M N).
Definition alifting := psi (e \v phi op).
Lemma aliftingE : alifting =
(fun X => Join \o e (N X) \o phi op (N X)) :> (_ ~~> _).
Proof. by rewrite /alifting/= /vcomp; unlock. Qed.
Theorem uniform_algebraic_lifting : lifting op e alifting.
Proof.
move=> X.
apply: boolp.funext => Y.
rewrite /alifting.
rewrite [in RHS]/=.
rewrite psiE.
rewrite vcompE.
rewrite phiE.
rewrite !compE.
rewrite (_ : (E # Ret) ((E # e X) Y) =
(E # (M # e X)) ((E # Ret) Y)); last first.
rewrite -[in LHS]compE -functor_o.
rewrite -[in RHS]compE -functor_o.
by rewrite (natural ret) FIdE.
set x := (Z in Join (e (N X) Z)).
rewrite (_ : x =
(M # e X) (op (M X) ((E # Ret) Y))); last first.
rewrite -(compE (M # e X)).
by rewrite (natural op).
transitivity (e X (Join (op (M X) ((E # Ret) Y)))); last first.
rewrite joinE.
rewrite monadMbind.
rewrite bindE -(compE _ (M # e X)).
by rewrite -natural.
by rewrite -[in LHS](phiK op).
Qed.
End uniform_algebraic_lifting.
HB.mixin Record isFunctorial (t : monad -> monad) := {
hmap : forall {M N : monad}, (M ~> N) -> t M ~> t N ;
functorial_id : forall M : monad, hmap (NId M) = NId (t M) ;
functorial_o : forall (M N P : monad) (t : M ~> N) (s : N ~> P),
hmap (s \v t) = hmap s \v hmap t }.
#[short(type=functorial)]
HB.structure Definition Functorial := {t of isFunctorial t}.
Arguments hmap _ {M N} _.
HB.mixin Record isFMT (t : monad -> monad) of MonadT t & Functorial t := {
fmt_ret : forall M N (e : monadM M N),
MonadMLaws.ret (hmap [the functorial of t] e) ;
fmt_bind : forall M N (e : monadM M N),
MonadMLaws.bind (hmap [the functorial of t] e) ;
natural_hmap : forall (M N : monad) (n : M ~> N),
hmap [the functorial of t] n \v Lift [the monadT of t] M =
Lift [the monadT of t] N \v n }.
#[short(type=fmt)]
HB.structure Definition FMT := {t of isFMT t & isFunctorial t & isMonadT t}.
Section fmt_lemmas.
Lemma natural_hmapE (t : fmt) (M N : monad) (n : M ~> N) (X : UU0) :
hmap t n X \o Lift t M X = Lift t N X \o n X.
Proof.
have /nattrans_ext/(_ X)/= := @natural_hmap t M N n.
by rewrite !vcompE.
Qed.
End fmt_lemmas.
Section exceptFMT.
Variable X : UU0.
Let T := [the monadT of exceptT X].
Definition hmapX' (F G : monad) (tau : F ~> G) : T F ~~> T G :=
fun (A : UU0) t => tau _ t.
Let naturality_hmapX' (F G : monad) (tau : F ~> G) :
naturality (T F) (T G) (hmapX' tau).
Proof.
move=> A B h.
by rewrite /hmapX' 2!MX_mapE (natural tau).
Qed.
HB.instance Definition hmapX (F G : monad) (tau : F ~> G) :=
isNatural.Build (T F) (T G) (hmapX' tau) (naturality_hmapX' tau).
Let hmapX_NId (M : monad) : [the _ ~> _ of hmapX' (NId M)] = NId (T M).
Proof. by apply/nattrans_ext => A. Qed.
Let hmapX_v (M N P : monad) (t : M ~> N) (s : N ~> P) :
[the _ ~> _ of hmapX' (s \v t)] =
[the _ ~> _ of hmapX' s] \v [the _ ~> _ of hmapX' t].
Proof.
by apply/nattrans_ext => /= A; rewrite vcompE /= /hmapX'/= vcompE.
Qed.
HB.instance Definition _ := isFunctorial.Build (exceptT X) hmapX_NId hmapX_v.
Let monadMret_hmapX (F G : monad) (e : monadM F G) :
MonadMLaws.ret (hmapX' e).
Proof.
move=> A; apply: boolp.funext => /= a.
by rewrite /hmapX' /retX /= -(compE (e _)) monadMret.
Qed.
Let monadMbind_hmapX (F G : monad) (e : monadM F G) :
MonadMLaws.bind (hmapX' e).
Proof.
move=> A B m f; rewrite /hmapX' /=.
rewrite !bindE /= !MX_mapE /bindX /= !bind_fmap.
rewrite !monadMbind /=.
congr (bind (e (X + A)%type m) _).
apply boolp.funext => -[x/=|//].
by rewrite -(compE (e _)) monadMret.
Qed.
Let hmapX_lift :
let h := fun F G FG => [the _ ~> _ of hmapX' FG] in
forall (M N : monad) (n : M ~> N),
h M N n \v Lift T M = Lift T N \v n.
Proof.
move=> h M N t; apply/nattrans_ext => A/=; rewrite !vcompE/=.
rewrite /hmapX' /=.
rewrite /liftX /=.
rewrite /retX /=.
apply: boolp.funext => ma /=.
rewrite (_ : (fun x : A => Ret (inr x)) = Ret \o inr) //.
rewrite -bind_fmap.
rewrite bindmret.
rewrite (_ : (fun x : A => Ret (inr x)) = Ret \o inr) //.
rewrite -bind_fmap.
rewrite bindmret.
rewrite -(compE (N # inr)).
by rewrite (natural t).
Qed.
HB.instance Definition _ := isFMT.Build (exceptT X)
monadMret_hmapX monadMbind_hmapX hmapX_lift.
End exceptFMT.
Section Fmt_stateT.
Variable S : UU0.
Let T := [the monadT of stateT S].
Definition hmapS (F G : monad) (tau : F ~> G) : T F ~~> T G :=
fun (A : UU0) t s => tau _ (t s).
Let naturality_hmapS (F G : monad) (tau : F ~> G) :
naturality (T F) (T G) (hmapS tau).
Proof.
move=> A B h.
rewrite /hmapS /=.
have H : forall G, [the monad of stateT S G] # h = [the functor of MS S G] # h by [].
rewrite !H {H}.
apply: boolp.funext => m /=.
rewrite !MS_mapE.
apply: boolp.funext => s /=.
rewrite -(compE _ (tau (A * S)%type)).
by rewrite natural.
Qed.
HB.instance Definition _ (F G : monad) (tau : F ~> G) := isNatural.Build
(T F) (T G) (hmapS tau) (naturality_hmapS tau).
Let hmapS_NId (M : monad) : [the _ ~> _ of hmapS (NId M)] = NId (T M).
Proof. exact/nattrans_ext. Qed.
Let hmapS_v (M N P : monad) (t : M ~> N) (s : N ~> P) :
[the _ ~> _ of hmapS (s \v t)] =
[the _ ~> _ of hmapS s] \v [the _ ~> _ of hmapS t].
Proof.
by apply/nattrans_ext => A /=; rewrite vcompE/= /hmapS/= vcompE.
Qed.
HB.instance Definition _ := isFunctorial.Build (stateT S) hmapS_NId hmapS_v.
Let monadMret_hmapS (F G : monad) (e : monadM F G) :
MonadMLaws.ret (hmapS e).
Proof.
move=> A; apply boolp.funext => /= a.
rewrite /hmapS /= /retS /=; apply: boolp.funext => s.
by rewrite [in LHS]curryE monadMret.
Qed.
Let monadMbind_hmapS (F G : monad) (e : monadM F G) :
MonadMLaws.bind (hmapS e).
Proof.
move=> A B m f.
rewrite /hmapS /=; apply: boolp.funext => s.
rewrite /= 2!bindE /= !MS_mapE /bindS /= 2!bind_fmap.
by rewrite monadMbind.
Qed.
Let hmapS_lift :
let h := fun F G FG => [the _ ~> _ of hmapS FG] in
forall (M N : monad) (n : M ~> N),
h M N n \v Lift T M = Lift T N \v n.
Proof.
move=> h M N t; apply/nattrans_ext => A /=; rewrite !vcompE/=.
rewrite /hmapS /=.
rewrite /liftS /=.
apply: boolp.funext => ma /=.
apply: boolp.funext => s /=.
rewrite ![in LHS]bindE ![in LHS]fmapE ![in LHS]bindE.
rewrite 2!(_ : (fun x : A => Ret (x, s)) = Ret \o (fun x => (x, s))) //.
rewrite functor_o.
rewrite -(compE Join (_ \o _)).
rewrite (compA Join (M # _)).
rewrite joinMret.
rewrite compidf.
rewrite functor_o.
rewrite compA.
rewrite joinMret.
rewrite compidf.
rewrite -(compE _ (t A)).
rewrite -(compE (t _)).