-
Notifications
You must be signed in to change notification settings - Fork 11
/
hierarchy.v
1373 lines (1137 loc) · 52 KB
/
hierarchy.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 *)
Ltac typeof X := type of X.
Require Import ssrmatching Reals JMeq.
From mathcomp Require Import all_ssreflect ssralg ssrnum.
From mathcomp Require boolp.
From mathcomp Require Import mathcomp_extra Rstruct reals.
From infotheo Require Import Reals_ext.
From infotheo Require Import realType_ext.
Require Import preamble.
From HB Require Import structures.
(******************************************************************************)
(* A formalization of monadic effects over the category Set *)
(* *)
(* We consider the type Type of Coq as the category Set and define functors *)
(* and a hierarchy of monads on top of functors. These monads are used to *)
(* develop the basics of monadic equational reasoning. The file category.v *)
(* provides a more generic definition of functors and monads as well as a *)
(* bridge to this file. *)
(* *)
(* Module FunctorLaws == map laws of a functor *)
(* functor == type of functors *)
(* F # g == application of the functor F to the morphism g *)
(* F ~> G == natural transformation from functor F to functor G *)
(* f ~~> g == forall A, f A -> g A, notation used for the *)
(* components of a natural transformation *)
(* naturality F G f == the components f form a natural transformation *)
(* F ~> G *)
(* Module JoinLaws == join laws of a monad *)
(* isMonad == mixin of monad *)
(* >>= == notation for the standard bind operator *)
(* m >> f := m >>= (fun _ => f) *)
(* monad == type of monads *)
(* Ret == natural transformation idfun ~> M for a monad M *)
(* Join == natural transformation M \o M ~> M for a monad M *)
(* Module BindLaws == bind laws of a monad *)
(* *)
(* Failure and nondeterministic monads: *)
(* failMonad == failure monad *)
(* failR0Monad == fail is right zero of bind *)
(* altMonad == monad with nondeterministic choice *)
(* prePlusMonad == nondeterminism + failR0 + distributivity *)
(* plusMonad == preplusMonad + commutativity and idempotence *)
(* altCIMonad == altMonad + commutativity + idempotence *)
(* nondetMonad == failMonad + altMonad *)
(* nondetCIMonad == failMOnad + altCIMonad *)
(* exceptMonad == failMonad + catch *)
(* *)
(* Control monads (wip): *)
(* contMonad, shiftresetMonad, jumpMonad *)
(* *)
(* State monads: *)
(* stateMonad S == state monad with a state of type S *)
(* stateRunMonad == state + RunStateT *)
(* let N be a monad and S be the type of states, then *)
(* when m is a computation in the monad *)
(* stateRunMonad S N, RunStateT m s runs m in a *)
(* state s and returns a computation in the monad N *)
(* exceptStateRunMonad == stateRun + except *)
(* reifyMonad == reify interface *)
(* stateReifyMonad == monadState + reify *)
(* failStateReifyMonad == stateReify + fail *)
(* nondetStateMonad == backtrackable state *)
(* arrayMonad == array monad *)
(* plusArrayMonad == plus monad + array monad *)
(* *)
(* ML_universe == a type with decidable equality to represent an *)
(* OCaml type together with its Coq representation *)
(* in the type of a Tarski universe *)
(* typedStoreMonad == A monad for OCaml computations *)
(* typedStoreRunMonad == typedStoreMonad + crun *)
(* *)
(* Trace monads: *)
(* traceMonad == trace monad *)
(* traceReifyMonad == trace + reify *)
(* stateTraceMonad == state + trace *)
(* stateTraceReifyMonad == stateTrace + reify *)
(* *)
(* Probability monads: *)
(* probMonad == probabilistic choice and bind left-distributes over *)
(* choice *)
(* probDrMonad == probMonad + bind right-distributes over choice *)
(* altProbMonad == combined (probabilistic and nondeterministic) choice *)
(* exceptProbMonad == exceptions + probabilistic choice *)
(* *)
(* Freshness monads: *)
(* freshMonad == monad with freshness *)
(* failFreshMonad == freshMonad + failure *)
(* *)
(* references: *)
(* - R. Affeldt, J. Garrigue, T. Saikawa, Environment-friendly monadic *)
(* equational reasoning for OCaml, The Coq Workshop 2023 *)
(* - R. Affeldt, D. Nowak, Extending Equational Monadic Reasoning with Monad *)
(* Transformers, TYPES 2020, https://arxiv.org/abs/2011.03463 *)
(* - R. Affeldt, D. Nowak, T. Saikawa, A Hierarchy of Monadic Effects for *)
(* Program Verification using Equational Reasoning, MPC 2019 *)
(* - J. Gibbons, R. Hinze, Just do it: simple monadic equational reasoning, *)
(* ICFP 2011 *)
(* - J. Gibbons, Unifying Theories of Programming with Monads, UTP 2012 *)
(******************************************************************************)
Set Implicit Arguments.
Unset Strict Implicit.
Unset Printing Implicit Defensive.
Declare Scope do_notation.
Declare Scope mprog.
Declare Scope monae_scope.
Delimit Scope monae_scope with monae.
Declare Scope proba_monad_scope.
Reserved Notation "f (o) g" (at level 11).
Reserved Notation "m >> f" (at level 49).
Reserved Notation "'fmap' f" (at level 4).
Reserved Notation "x '[~]' y" (at level 50).
Reserved Notation "mx <| p |> my" (format "mx <| p |> my", at level 49).
Notation "f ~~> g" := (forall A, f A -> g A)
(at level 51, only parsing) : monae_scope.
Local Open Scope monae_scope.
Notation UU1 := Type.
Notation UU0 := Type.
(* NB: not putting M in Set -> Set because of expressions like:
M (A * (size s).-1.-tuple A)%type *)
Module FunctorLaws.
Section def.
Variable (F : UU0 -> UU0) (f : forall A B : UU0, (A -> B) -> F A -> F B).
Definition id := forall A : UU0, f id = id :> (F A -> F A).
Definition comp := forall (A B C : UU0) (g : B -> C) (h : A -> B),
f (g \o h) = f g \o f h.
End def.
End FunctorLaws.
HB.mixin Record isFunctor (F : UU0 -> UU0) := {
actm : forall A B : UU0, (A -> B) -> F A -> F B ;
functor_id : FunctorLaws.id actm ;
functor_o : FunctorLaws.comp actm }.
#[short(type=functor)]
HB.structure Definition Functor := {F of isFunctor F}.
Notation "F # g" := (@actm F _ _ g) : monae_scope.
Notation "'fmap' f" := (_ # f) : mprog.
Section functorid.
Let id_actm (A B : UU0) (f : A -> B) : idfun A -> idfun B := f.
Let id_id : FunctorLaws.id id_actm. Proof. by []. Qed.
Let id_comp : FunctorLaws.comp id_actm. Proof. by []. Qed.
HB.instance Definition _ := isFunctor.Build idfun id_id id_comp.
End functorid.
Lemma FIdE (A B : UU0) (f : A -> B) : idfun # f = f. Proof. by []. Qed.
Section functor_composition.
Variables F G : functor.
Let comp_actm (A B : UU0) (h : A -> B) : (F \o G) A -> (F \o G) B :=
F # (G # h).
Let comp_id : FunctorLaws.id comp_actm.
Proof. by rewrite /FunctorLaws.id => A; rewrite /comp_actm 2!functor_id. Qed.
Let comp_comp : FunctorLaws.comp comp_actm.
Proof.
rewrite /FunctorLaws.comp => A B C g' h; rewrite /comp_actm.
by apply boolp.funext => m; rewrite [in RHS]compE 2!functor_o.
Qed.
HB.instance Definition _ := isFunctor.Build (F \o G) comp_id comp_comp.
End functor_composition.
Lemma FCompE (F G : functor) (A B : UU0) (k : A -> B) :
[the functor of F \o G] # k = F # (G # k).
Proof. by []. Qed.
(* monadic counterpart of function composition:
composes a pure function after a monadic function *)
Section fcomp.
Variable M : functor.
Definition fcomp (A B C : UU0) (f : A -> B) (g : C -> M A) :=
locked ((M # f) \o g).
Arguments fcomp : simpl never.
Local Notation "f (o) g" := (fcomp f g).
Lemma fcomp_def (A B C : UU0) (f : A -> B) (g : C -> M A) :
f (o) g = ((M # f)) \o g.
Proof. by rewrite /fcomp; unlock. Qed.
Lemma fcompE (A B C : UU0) (f : A -> B) (g : C -> M A) c :
(f (o) g) c = (M # f) (g c).
Proof. by rewrite fcomp_def. Qed.
Lemma fcomp_comp (A B C D : UU0) (f : A -> B) (g : C -> A) (m : D -> M C) :
(f \o g) (o) m = f (o) (g (o) m).
Proof. by rewrite 3!fcomp_def functor_o compA. Qed.
End fcomp.
Notation "f (o) g" := (fcomp f g) : mprog.
Arguments fcomp : simpl never.
Lemma functor_ext (F G : functor) :
forall (H : Functor.sort F = Functor.sort G),
@actm G =
eq_rect _ (fun m : UU0 -> UU0 => forall A B : UU0, (A -> B) -> m A -> m B)
(@actm F) _ H ->
G = F.
Proof.
move: F G => [F [[HF1 HF2 HF3]]] [G [[HG1 HG2 HG3]]] /= H.
subst F => /= H.
congr (Functor.Pack (Functor.Class _)).
have ? : HG1 = HF1.
rewrite /actm /= in H.
apply funext_dep => x.
apply funext_dep => y.
apply funext_dep => z.
by move/(congr1 (fun i => i x y z)) : H.
subst HG1.
congr (isFunctor.Axioms_ _); exact/proof_irr.
Defined.
Definition naturality (F G : functor) (f : F ~~> G) :=
forall (A B : UU0) (h : A -> B), (G # h) \o f A = f B \o (F # h).
Arguments naturality : clear implicits.
HB.mixin Record isNatural (F G : functor) (f : F ~~> G) := {
natural : naturality F G f }.
#[short(type=nattrans)]
HB.structure Definition Nattrans (F G : functor) := {f of isNatural F G f}.
Arguments natural {F G} s.
Notation "f ~> g" := (nattrans f g) : monae_scope.
Section natrans_lemmas.
Variables (F G : functor) (phi : F ~> G).
Lemma nattrans_ext (f g : F ~> G) : f = g <-> forall a, (f a = g a :> (_ -> _)).
Proof.
split => [ -> // |]; move: f g => [f Hf] [g Hg] /= fg.
have ? : f = g by exact: funext_dep.
subst g.
suff : Hf = Hg by move=> ->.
case: Hf => -[Hf].
case: Hg => -[Hg].
do 2 f_equal.
exact: proof_irr.
Qed.
End natrans_lemmas.
(*Require Import Logic.Eqdep.
Lemma natural_ext (F G G' : functor) (t : F ~> G) (t' : F ~> G') :
forall (H : G = G'),
forall (K : forall X (x : F X), Natural.cpnt t' x = eq_rect _ (fun m : functor => m X) (Natural.cpnt t x) _ H),
t' = eq_rect _ (fun m : functor => F ~> m) t _ H.
Proof.
move : t t' => [t t1] [t' t'1] /= H; subst G' => H /=.
have ? : t = t'.
apply funext_dep => A; apply funext_dep => x.
by rewrite H -[in RHS]eq_rect_eq.
subst t'.
congr Natural.Pack; exact/proof_irr.
Qed.
Lemma natural_ext2 (F F' : functor) (t : F \O F ~> F) (t' : F' \O F' ~> F') :
forall (K : F = F'),
forall L : (forall X (x : (F' \O F') X),
Natural.cpnt t' x = eq_rect _ (fun m : functor => m X)
(Natural.cpnt t (eq_rect _ (fun m : functor => (m \O m) X) x _ (esym K)))
_ K),
t' = eq_rect _ (fun m => m \O m ~> m) t _ K.
Proof.
move: t t' => [t t1] [t' t'1] /= H L; subst F.
rewrite -[in RHS]eq_rect_eq /=.
have ? : t = t'.
apply funext_dep => A; apply funext_dep => x.
by rewrite L -[in RHS]eq_rect_eq.
subst t'.
congr Natural.Pack; exact/proof_irr.
Qed.*)
Module JoinLaws.
Section join_laws.
Context {F : functor}.
Variables (ret : idfun ~~> F) (join : F \o F ~~> F).
Arguments ret {_}.
Arguments join {A}.
Definition left_unit := forall A, join \o ret = id :> (F A -> F A).
Definition right_unit := forall A, join \o F # ret = id :> (F A -> F A).
Definition associativity := forall A,
join \o F # join = join \o join :> (F (F (F A)) -> F A).
End join_laws.
End JoinLaws.
HB.mixin Record isMonad (F : UU0 -> UU0) of Functor F := {
ret : idfun ~> F ;
join : F \o F ~> F ;
bind : forall (A B : UU0), F A -> (A -> F B) -> F B ;
bindE : forall (A B : UU0) (f : A -> F B) (m : F A),
bind A B m f = join B ((F # f) m) ;
joinretM : JoinLaws.left_unit ret join ;
joinMret : JoinLaws.right_unit ret join ;
joinA : JoinLaws.associativity join }.
#[short(type=monad)]
HB.structure Definition Monad := {F of isMonad F &}.
(* we introduce Ret as a way to make the second arguments of ret implicit,
o.w. Coq won't let us *)
Notation Ret := (@ret _ _).
Notation Join := (@join _ _).
Arguments bind {s A B} : simpl never.
Notation "m >>= f" := (bind m f) : monae_scope.
Lemma eq_bind (M : monad) (A B : UU0) (m : M A) (f1 f2 : A -> M B) :
f1 =1 f2 -> m >>= f1 = m >>= f2.
Proof. by move=> f12; congr bind; apply boolp.funext. Qed.
Section monad_lemmas.
Variable M : monad.
Lemma fmapE (A B : UU0) (f : A -> B) (m : M A) :
(M # f) m = m >>= (ret B \o f).
Proof.
by rewrite bindE [in RHS]functor_o -[in RHS]compE compA joinMret.
Qed.
End monad_lemmas.
Module BindLaws.
Section bindlaws.
Variable F : UU0 -> UU0.
Variable b : forall (A B : UU0), F A -> (A -> F B) -> F B.
Local Notation "m >>= f" := (b m f).
Definition associative := forall A B C (m : F A) (f : A -> F B) (g : B -> F C),
(m >>= f) >>= g = m >>= (fun x => f x >>= g).
Definition right_distributive (add : forall B, F B -> F B -> F B) :=
forall A B (m : F A) (k1 k2 : A -> F B),
m >>= (fun x => add _ (k1 x) (k2 x)) = add _ (m >>= k1) (m >>= k2).
Definition left_distributive (add : forall B, F B -> F B -> F B) :=
forall A B (m1 m2 : F A) (k : A -> F B),
(add _ m1 m2) >>= k = add _ (m1 >>= k) (m2 >>= k).
Definition left_zero (f : forall A, F A) :=
forall (A B : UU0) (g : A -> F B), f A >>= g = f B.
Definition right_zero (f : forall A, F A) :=
forall (A B : UU0) (g : F B), g >>= (fun _ => f A) = f A.
Definition left_neutral (r : forall A : UU0, A -> F A) :=
forall (A B : UU0) (a : A) (f : A -> F B), r _ a >>= f = f a.
Definition right_neutral (r : forall A : UU0, A -> F A) :=
forall A (m : F A), m >>= r _ = m.
Definition left_id (r : forall A, F A) (op : forall B, F B -> F B -> F B) :=
forall A (m : F A), op _ (r _) m = m.
Definition right_id (r : forall A, F A) (op : forall B, F B -> F B -> F B) :=
forall A (m : F A), op _ m (r _) = m.
End bindlaws.
End BindLaws.
Definition join_of_bind (F : functor)
(b : forall (A B : UU0), F A -> (A -> F B) -> F B) : F \o F ~~> F :=
fun A : UU0 => (b _ A)^~ id.
Definition bind_of_join (F : functor) (j : F \o F ~~> F)
(A B : UU0) (m : F A) (f : A -> F B) : F B :=
j B ((F # f) m).
Section from_join_laws_to_bind_laws.
Variable (F : functor) (ret : idfun ~> F) (join : F \o F ~> F).
Hypothesis joinretM : JoinLaws.left_unit ret join.
Hypothesis joinMret : JoinLaws.right_unit ret join.
Hypothesis joinA : JoinLaws.associativity join.
Lemma bindretf_derived : BindLaws.left_neutral (bind_of_join join) ret.
Proof.
move=> A B a f; rewrite /bind_of_join -(compE (@join _)) -(compE _ (@ret _)).
by rewrite -compA (natural ret) compA joinretM compidf.
Qed.
Lemma bindmret_derived : BindLaws.right_neutral (bind_of_join join) ret.
Proof. by move=> A m; rewrite /bind_of_join -(compE (@join _)) joinMret. Qed.
Lemma bindA_derived : BindLaws.associative (bind_of_join join).
Proof.
move=> A B C m f g; rewrite /bind_of_join.
rewrite [LHS](_ : _ = ((@join _ \o (F # g \o @join _) \o F # f) m)) //.
rewrite (natural join) (compA (@join C)) -joinA -(compE (@join _)).
transitivity ((@join _ \o F # (@join _ \o (F # g \o f))) m) => //.
by rewrite -2!compA functor_o FCompE -[in LHS](@functor_o F).
Qed.
End from_join_laws_to_bind_laws.
HB.factory Record isMonad_ret_join (F : UU0 -> UU0) of isFunctor F := {
ret : idfun ~> F ;
join : F \o F ~> F ;
joinretM : JoinLaws.left_unit ret join ;
joinMret : JoinLaws.right_unit ret join ;
joinA : JoinLaws.associativity join }.
HB.builders Context M of isMonad_ret_join M.
Let F := [the functor of M].
Let bind (A B : UU0) (m : F A) (f : A -> F B) : F B :=
bind_of_join join m f.
Let bindE (A B : UU0) (f : A -> M B) (m : M A) :
bind m f = join B ((F # f) m).
Proof. by []. Qed.
HB.instance Definition _ := isMonad.Build M bindE joinretM joinMret joinA.
HB.end.
HB.factory Record isMonad_ret_bind (F : UU0 -> UU0) := {
ret : forall (A : UU0), A -> F A ;
bind : forall (A B : UU0), F A -> (A -> F B) -> F B ;
bindretf : BindLaws.left_neutral bind ret ;
bindmret : BindLaws.right_neutral bind ret ;
bindA : BindLaws.associative bind }.
HB.builders Context M of isMonad_ret_bind M.
Let actm (a b : UU0) (f : a -> b) m := bind m (@ret _ \o f).
Let actm_id : FunctorLaws.id actm.
Proof.
move=> a.
rewrite /actm; apply: boolp.funext => m /=.
by rewrite bindmret.
Qed.
Let actm_comp : FunctorLaws.comp actm.
Proof.
move=> a b c g h.
rewrite /actm; apply: boolp.funext => m /=.
rewrite bindA.
congr bind.
apply: boolp.funext => u /=.
by rewrite bindretf.
Qed.
HB.instance Definition _ := isFunctor.Build M actm_id actm_comp.
Let F := [the functor of M].
Local Notation FF := [the functor of F \o F].
Let ret_naturality : naturality idfun F ret.
Proof.
move=> a b h.
rewrite FIdE /hierarchy.actm /= /actm; apply: boolp.funext => m /=.
by rewrite bindretf.
Qed.
HB.instance Definition _ :=
isNatural.Build idfun F (ret : idfun ~~> F) ret_naturality.
Let join' : FF ~~> F := fun _ m => bind m idfun.
Let actm_bind (a b c : UU0) (f : a -> b) m (g : c -> F a) :
(actm f) (bind m g) = bind m (actm f \o g).
Proof. by rewrite /actm bindA. Qed.
Let join'_naturality : naturality FF F join'.
Proof.
move=> a b h.
rewrite /join' /=; apply: boolp.funext => mm /=.
rewrite /hierarchy.actm /= /isFunctor.actm /=.
rewrite actm_bind bindA /=.
congr bind.
apply: boolp.funext => m /=.
by rewrite bindretf /=.
Qed.
HB.instance Definition _ := isNatural.Build _ _ _ join'_naturality.
Let join := [the FF ~> F of join'].
Let bind_map (A B C : UU0) (f : A -> B) (m : M A) (g : B -> M C) :
bind ((F # f) m) g = bind m (g \o f).
Proof.
rewrite bindA; congr bind.
by apply: boolp.funext => ?; rewrite bindretf.
Qed.
Let bindE (a b : UU0) (f : a -> M b) (m : M a) :
bind m f = join b ((F # f) m).
Proof.
rewrite /join /= /hierarchy.actm /= /join' /=.
by rewrite bind_map.
Qed.
Let joinretM : JoinLaws.left_unit ret join.
Proof.
move=> a; apply: boolp.funext => m.
by rewrite /join /= /join' /= bindretf.
Qed.
Let joinMret : JoinLaws.right_unit ret join.
Proof.
move=> a; apply: boolp.funext => m.
rewrite /join /= /join'.
rewrite /hierarchy.actm /= /actm /=.
rewrite bindA /=.
rewrite [X in bind m X](_ : _ = fun x => ret x) ?bindmret //=; apply: boolp.funext => ?.
by rewrite bindretf.
Qed.
Let joinA : JoinLaws.associativity join.
Proof.
move => a; apply: boolp.funext => m.
rewrite /join /= /join'.
rewrite /hierarchy.actm /= /actm.
rewrite !bindA.
congr bind.
apply: boolp.funext => u /=.
by rewrite bindretf.
Qed.
HB.instance Definition _ := isMonad.Build M bindE joinretM joinMret joinA.
HB.end.
Section monad_lemmas.
Variable M : monad.
Lemma bindretf : BindLaws.left_neutral (@bind M) ret.
Proof.
move: (@bindretf_derived M ret join joinretM).
rewrite (_ : bind_of_join _ = @bind M) //.
apply funext_dep => A; apply funext_dep => B.
apply funext_dep => m; apply funext_dep => f.
by rewrite bindE.
Qed.
Lemma bindmret : BindLaws.right_neutral (@bind M) ret.
Proof.
move: (@bindmret_derived M ret join joinMret).
rewrite (_ : bind_of_join _ = @bind M) //.
apply funext_dep => A; apply funext_dep => B.
apply funext_dep => m; apply funext_dep => f.
by rewrite bindE.
Qed.
Lemma bindA : BindLaws.associative (@bind M).
Proof.
move: (@bindA_derived M join joinA).
rewrite (_ : bind_of_join _ = @bind M) //.
apply funext_dep => A; apply funext_dep => B.
apply funext_dep => m; apply funext_dep => f.
by rewrite bindE.
Qed.
End monad_lemmas.
Notation "'do' x <- m ; e" := (bind m (fun x => e)) (only parsing) : do_notation.
Notation "'do' x : T <- m ; e" :=
(bind m (fun x : T => e)) (only parsing) : do_notation.
Delimit Scope do_notation with Do.
Notation "m >> f" := (bind m (fun _ => f)) : monae_scope.
Fixpoint sequence (M : monad) A (s : seq (M A)) : M (seq A) :=
(if s isn't h :: t then Ret [::] else
do v <- h; do vs <- sequence t; Ret (v :: vs))%Do.
Lemma sequence_nil (M : monad) (A : UU0) :
sequence [::] = Ret [::] :> M (seq A).
Proof. by []. Qed.
Lemma sequence_cons (M : monad) A h (t : seq (M A)) :
(sequence (h :: t) = do x <- h ; do vs <- sequence t ; Ret (x :: vs))%Do.
Proof. by []. Qed.
Definition skip M := @ret M _ tt.
Arguments skip {M} : simpl never.
Ltac bind_ext :=
let congr_ext m := ltac:(congr (bind m); apply boolp.funext) in
match goal with
| |- @bind _ _ _ ?m ?f1 = @bind _ _ _ ?m ?f2 =>
congr_ext m
| |- @bind _ _ _ ?m1 ?f1 = @bind _ _ _ ?m2 ?f2 =>
first[ simpl m1; congr_ext m1 | simpl m2; congr_ext m2 ]
end.
Section bindskip.
Lemma bindmskip (M : monad) (m : M unit) : m >> skip = m.
Proof. rewrite -[RHS]bindmret; bind_ext; by case. Qed.
Lemma bindskipf (M : monad) A (m : M A) : skip >> m = m.
Proof. exact: bindretf. Qed.
End bindskip.
(* experimental *)
Tactic Notation "With" tactic(tac) "Open" ssrpatternarg(pat) :=
ssrpattern pat;
let f := fresh "f" in
intro f;
let g := fresh "g" in
let typ := typeof f in
let x := fresh "x" in
evar (g : typ);
rewrite (_ : f = g);
[rewrite {}/f {}/g|
apply boolp.funext => x; rewrite {}/g {}/f; tac]; last first.
Tactic Notation "Open" ssrpatternarg(pat) :=
With (idtac) Open pat.
Section fmap_and_join.
Variable M : monad.
Local Open Scope mprog.
Lemma bind_fmap (A B C : UU0) (f : A -> B) (m : M A) (g : B -> M C) :
fmap f m >>= g = m >>= (g \o f).
Proof. by rewrite fmapE bindA; under eq_bind do rewrite bindretf. Qed.
Lemma fmap_if (A B : UU0) (f : A -> B) b (m : M A) a :
fmap f (if b then m else Ret a) = if b then fmap f m else Ret (f a).
Proof. case: ifPn => Hb //; by rewrite fmapE bindretf. Qed.
Lemma fmap_bind (A B C : UU0) (f : A -> B) m (g : C -> M A) :
fmap f (m >>= g) = m >>= (f (o) g).
Proof.
rewrite fcomp_def fmapE bindA; bind_ext => c.
by rewrite compE -/(fmap _ _) fmapE.
Qed.
Lemma skip_fmap (A B : UU0) (f : A -> B) (mb : M B) ma :
mb >> (fmap f ma) = fmap f (mb >> ma).
Proof. by rewrite fmap_bind fcomp_def. Qed.
(*Lemma rev_map A B (f : A -> B) : rev \o map f = map f \o rev.
Proof.
apply functional_extensionality.
by elim=> // h t /= IH; rewrite !rev_cons IH map_rcons.
Qed.*)
Lemma joinE A (pp : M (M A)) : Join pp = pp >>= id.
Proof. rewrite bindE; congr Join; by rewrite functor_id. Qed.
Lemma join_fmap (A B : UU0) (f : A -> M B) (m : M A) : Join (fmap f m) = m >>= f.
Proof. by rewrite bindE. Qed.
End fmap_and_join.
Section kleisli.
Variable M : monad.
Implicit Types A B C D : UU0.
Definition kleisli A B C (m : B -> M C) (n : A -> M B) : A -> M C :=
Join \o (M # m) \o n.
Local Notation "m <=< n" := (kleisli m n).
Local Notation "m >=> n" := (kleisli n m).
Lemma kleisli_def A B C (g : B -> M C) (f : A -> M B) :
(f >=> g) = Join \o (M # g) \o f.
Proof. by []. Qed.
Lemma kleisliE A B C (g : B -> M C) (f : A -> M B) (a : A) :
(f >=> g) a = (f a) >>= g.
Proof. by rewrite /kleisli /= join_fmap. Qed.
Lemma bind_kleisli A B C m (f : A -> M B) (g : B -> M C) :
m >>= (f >=> g) = (m >>= f) >>= g.
Proof. by rewrite bindA; bind_ext => a; rewrite /kleisli !compE join_fmap. Qed.
Lemma ret_kleisli A B (k : A -> M B) : Ret >=> k = k.
Proof. by rewrite /kleisli -compA (natural ret) FIdE compA joinretM. Qed.
Local Open Scope mprog.
Lemma fcomp_kleisli A B C D (f : A -> B) (g : C -> M A) (h : D -> M C) :
f (o) (g <=< h) = (f (o) g) <=< h.
Proof.
rewrite /kleisli 2!fcomp_def 2!(compA (fmap f)) natural [in RHS]functor_o.
by rewrite -compA.
Qed.
Lemma kleisli_fcomp A B C (f : A -> M B) (g : B -> A) (h : C -> M B) :
((f \o g) <=< h) = f <=< (g (o) h).
Proof. by rewrite /kleisli fcomp_def functor_o 2!compA. Qed.
Local Close Scope mprog.
Lemma kleisliA A B C D (f : A -> M B) (g : B -> M C) (h : C -> M D) :
f >=> g >=> h = f >=> (g >=> h).
Proof.
apply: funext_dep => a; rewrite !kleisliE bindA.
by under [in RHS]eq_bind do rewrite kleisliE.
Qed.
End kleisli.
Notation "m <=< n" := (kleisli m n) : monae_scope.
Notation "m >=> n" := (kleisli n m) : monae_scope.
HB.mixin Record isMonadFail (M : UU0 -> UU0) of Monad M := {
fail : forall A : UU0, M A ;
(* exceptions are left-zeros of sequential composition *)
bindfailf : BindLaws.left_zero (@bind M) fail
(* fail A >>= f = fail B *) }.
#[short(type=failMonad)]
HB.structure Definition MonadFail := {M of isMonadFail M & }.
Arguments bindfailf [_].
Arguments fail {_} {_}.
Section guard_assert.
Variable M : failMonad.
Definition guard (b : bool) : M unit := if b then skip else fail.
Lemma guardPn (b : bool) : if_spec b skip fail (~~ b) b (guard b).
Proof. by rewrite /guard; case: ifPn => ?; constructor. Qed.
Lemma guardT : guard true = skip. Proof. by []. Qed.
Lemma guardF : guard false = fail. Proof. by []. Qed.
(* guard distributes over conjunction *)
Lemma guard_and a b : guard (a && b) = guard a >> guard b.
Proof.
by move: a b => -[|] b /=; [rewrite bindskipf | rewrite bindfailf].
Qed.
Definition assert {A : UU0} (p : pred A) (a : A) : M A := guard (p a) >> Ret a.
Lemma assertE {A : UU0} (p : pred A) (a : A) :
assert p a = guard (p a) >> Ret a.
Proof. by []. Qed.
Lemma assertT {A : UU0} (a : A) : assert xpredT a = Ret a.
Proof. by rewrite assertE guardT bindskipf. Qed.
Lemma assertF {A : UU0} (a : A) : assert xpred0 a = fail.
Proof. by rewrite assertE guardF bindfailf. Qed.
Lemma assertPn {A : UU0} (p : pred A) (a : A) :
if_spec (p a) (Ret a) fail (~~ (p a)) (p a) (assert p a).
Proof.
rewrite assertE; case: guardPn => pa;
by [rewrite bindskipf; constructor | rewrite bindfailf; constructor].
Qed.
Definition bassert {A : UU0} (p : pred A) (m : M A) : M A := m >>= assert p.
(* follows from guards commuting with anything *)
Lemma commutativity_of_assertions A q :
Join \o (M # (bassert q)) = bassert q \o Join :> (_ -> M A).
Proof.
by apply boolp.funext => x; rewrite !compE join_fmap /bassert joinE bindA.
Qed.
(* guards commute with anything *)
Lemma guardsC (HM : BindLaws.right_zero (@bind M) (@fail _)) b B (m : M B) :
guard b >> m = m >>= assert (fun=> b).
Proof.
case: guardPn => Hb.
- rewrite bindskipf.
under eq_bind do rewrite assertT.
by rewrite bindmret.
- under [RHS]eq_bind do rewrite assertF.
by rewrite bindfailf HM.
Qed.
End guard_assert.
Arguments assert {M} {A} : simpl never.
Arguments guard {M} : simpl never.
HB.mixin Record isMonadAlt (M : UU0 -> UU0) of Monad M := {
alt : forall T : UU0, M T -> M T -> M T ;
altA : forall T : UU0, associative (@alt T) ;
(* composition distributes leftwards over choice *)
alt_bindDl : BindLaws.left_distributive (@bind M) alt
(* in general, composition does not distribute rightwards over choice *)
(* NB: no bindDr to accommodate both angelic and demonic interpretations of
nondeterminism *) }.
#[short(type=altMonad)]
HB.structure Definition MonadAlt := {M of isMonadAlt M & }.
Notation "a [~] b" := (@alt _ _ a b). (* infix notation *)
HB.mixin Record isMonadAltCI (M : UU0 -> UU0) of MonadAlt M := {
altmm : forall A : UU0, idempotent (@alt M A) ;
altC : forall A : UU0, commutative (@alt M A) }.
#[short(type=altCIMonad)]
HB.structure Definition MonadAltCI := {M of isMonadAltCI M & }.
Arguments altC {_} {_}.
Arguments altmm {_} {_}.
Section altci_lemmas.
Variable (M : altCIMonad).
Lemma altCA A : @left_commutative (M A) (M A) (fun x y => x [~] y).
Proof. by move=> x y z; rewrite altA altC altA altC (altC x). Qed.
Lemma altAC A : @right_commutative (M A) (M A) (fun x y => x [~] y).
Proof. move=> x y z; by rewrite altC altA (altC x). Qed.
Lemma altACA A : @interchange (M A) (fun x y => x [~] y) (fun x y => x [~] y).
Proof. move=> x y z t; rewrite !altA; congr (_ [~] _); by rewrite altAC. Qed.
End altci_lemmas.
HB.mixin Record isMonadNondet (M : UU0 -> UU0) of MonadFail M & MonadAlt M := {
altfailm : @BindLaws.left_id M (@fail M) (@alt M) ;
altmfail : @BindLaws.right_id M (@fail M) (@alt M) }.
#[short(type=nondetMonad)]
HB.structure Definition MonadNondet := {M of isMonadNondet M & }.
#[short(type=nondetCIMonad)]
HB.structure Definition MonadCINondet := {M of MonadAltCI M & MonadNondet M }.
Section nondet_big.
Variables (M : nondetMonad) (A : UU0).
HB.instance Definition _ :=
Monoid.isLaw.Build _ _ _ (@altA M A) (@altfailm _ _) (@altmfail _ _).
Lemma test_bigop n : \big[(@alt _ _)/fail]_(i < n) (fail : M A) = fail.
Proof.
elim: n => [|n IH]; first by rewrite big_ord0.
by rewrite big_ord_recr /= IH altmfail.
Qed.
End nondet_big.
HB.mixin Record isMonadFailR0 (M : UU0 -> UU0) of MonadFail M :=
{ bindmfail : BindLaws.right_zero (@bind [the monad of M]) (@fail _) }.
#[short(type=failR0Monad)]
HB.structure Definition MonadFailR0 := {M of isMonadFailR0 M & }.
HB.mixin Record isMonadPrePlus (M : UU0 -> UU0)
of MonadNondet M & MonadFailR0 M :=
{ alt_bindDr : BindLaws.right_distributive (@bind M) alt }.
#[short(type=prePlusMonad)]
HB.structure Definition MonadPrePlus := {M of isMonadPrePlus M & }.
#[short(type=plusMonad)]
HB.structure Definition MonadPlus := {M of MonadCINondet M & MonadPrePlus M}.
HB.mixin Record isMonadExcept (M : UU0 -> UU0) of MonadFail M := {
catch : forall A, M A -> M A -> M A ;
(* monoid *)
catchmfail : forall A, right_id fail (@catch A) ;
catchfailm : forall A, left_id fail (@catch A) ;
catchA : forall A, associative (@catch A) ;
(* unexceptional bodies need no handler *)
catchret : forall A x, @left_zero (M A) (M A) (Ret x) (@catch A)
(* NB: left-zero of sequential composition inherited from failMonad *) }.
#[short(type=exceptMonad)]
HB.structure Definition MonadExcept := {M of isMonadExcept M & }.
Arguments catch {_} {_}.
HB.mixin Record isMonadContinuation (M : UU0 -> UU0) of Monad M := {
(* NB: interface is wip *)
callcc : forall A B : UU0, ((A -> M B) -> M A) -> M A;
callcc0 : forall (A B : UU0) (g : (A -> M B) -> M A) (k : B -> M B),
@callcc _ _ (fun f => g (fun x => f x >>= k)) = @callcc _ _ g
(* see Sect. 7.2 of [Schrijvers, 19] *);
callcc1 : forall (A B : UU0) (m : M B),
@callcc _ _ (fun _ : B -> M A => m) = m
(* see Sect. 3.3 of [Wadler, 94] *);
callcc2 : forall (A B C : UU0) (m : M A) x (k : A -> B -> M C),
@callcc _ _ (fun f : _ -> M _ => m >>= (fun a => f x >>= (fun b => k a b)))
= @callcc _ _ (fun f : _ -> M _ => m >> f x) ;
callcc3 : forall (A B : UU0) (m : M A) b,
@callcc _ _ (fun f : B -> M B => m >> f b) =
@callcc _ _ (fun _ : B -> M B => m >> Ret b) }.
#[short(type=contMonad)]
HB.structure Definition MonadContinuation := {M of isMonadContinuation M & }.
Arguments callcc {_} {_} {_}.
HB.mixin Record isMonadShiftReset (U : UU0) (M : UU0 -> UU0)
of MonadContinuation M := {
shift : forall A : UU0, ((A -> M U) -> M U) -> M A ;
reset : M U -> M U ;
shiftreset0 : forall (A : UU0) (m : M A), @shift _ (fun k => m >>= k) = m ;
(* see Sect. 3.3 of [Wadler, 94] *)
shiftreset1 : forall (A B : UU0) (h : (A -> M B) -> M A), callcc h =
@shift _ (fun k' => h (fun x => @shift _ (fun k'' => k' x)) >>= k') ;
(* see Sect. 3.3 of [Wadler, 94] *)
shiftreset2 : forall (A : UU0) (c : A) (c': U) (k : A -> U -> _),
(reset (do x <- Ret c; do y <- @shift _ (fun _ => Ret c'); k x y) =
Ret c >> Ret c')%Do ;
shiftreset3 : forall (c c' : U) (k : U -> U -> _),
(reset (do x <- Ret c; do y <- @shift _ (fun f => do v <- f c'; f v);
Ret (k x y)) =
reset (do x <- Ret c; do y <- @shift _ (fun f => f c');
Ret (k x (k x y))))%Do ;
shiftreset4 : forall (c : U) k,
(reset (do y <- @shift _ (@^~ c); Ret (k y)) = Ret (k c))%Do }.
#[short(type=shiftresetMonad)]
HB.structure Definition MonadShiftReset U := {M of isMonadShiftReset U M & }.
Arguments shift {_} {_} {_}.
(* NB: wip, no model *)
(* Sect. 7.2 of [Tom Schrijvers & al., Monad Transformers and Modular
Algebraic Effects: What Binds Them Together, Haskell 2019] *)
HB.mixin Record isMonadJump (ref : UU0 -> UU0) (M : UU0 -> UU0) of Monad M := {
jump : forall A B : UU0, ref A -> A -> M B;
sub : forall A B : UU0, (ref A -> M B) -> (A -> M B) -> M B;
jump0 : forall (A B : UU0) k x, @sub _ _ (fun r => @jump A B r x) k = k x ;
jump1 : forall (A B : UU0) p k, @sub A B (fun _ => p) k = p;
jump2 : forall (A B : UU0) p r', @sub _ _ p (@jump A B r') = p r';
jump3 : forall (A B : UU0) (p : ref A -> ref B -> M B) (k1 : A -> M B) k2,
@sub _ _ (fun r1 : ref A => @sub _ _ (fun r2 => p r1 r2) (k2 r1)) k1 =
@sub _ _ (fun r2 : ref B => @sub _ _ (fun r1 => p r1 r2) k1)
(fun x => @sub _ _ (k2^~ x) k1)
(*NB: differs from [Schrijvers et al. 19]*);
jump4 : forall (A B : UU0) r x k, (@jump A B r x) >>= k = @jump A B r x;
jump5 : forall (A B : UU0) p q k,
@sub A B p q >>= k = @sub A B (p >=> k) (q >=> k) }.
#[short(type=jumpMonad)]
HB.structure Definition MonadJump ref := {M of isMonadJump ref M & }.
Definition Jump ref (M : jumpMonad ref) := @jump ref M.
Arguments Jump {_} {_} {_} {_}.
Definition Sub ref (M : jumpMonad ref) := @sub ref M.
Arguments sub {_} {_} {_} {_}.
HB.mixin Record isMonadState (S : UU0) (M : UU0 -> UU0) of Monad M := {
get : M S ;
put : S -> M unit ;
putput : forall s s', put s >> put s' = put s' ;
putget : forall s, put s >> get = put s >> Ret s ;
getput : get >>= put = skip ;
getget : forall (A : UU0) (k : S -> S -> M A),
get >>= (fun s => get >>= k s) = get >>= fun s => k s s }.
#[short(type=stateMonad)]
HB.structure Definition MonadState (S : UU0) := { M of isMonadState S M & }.
(*NB: explicit join newly added*)
#[short(type=failStateMonad)]
HB.structure Definition MonadFailState (S : UU0) :=
{ M of isMonadFail M & isMonadState S M & isMonad M & isFunctor M }.
(*NB: explicit join newly added*)
#[short(type=failR0StateMonad)]
HB.structure Definition MonadFailR0State (S : UU0) :=
{ M of isMonadFailR0 M & isMonadState S M & isMonadFail M & isMonad M &
isFunctor M }.
#[short(type=nondetStateMonad)]
HB.structure Definition MonadNondetState (S : UU0) :=
{ M of MonadPrePlus M & MonadState S M }.
HB.mixin Record isMonadStateRun (S : UU0) (N : monad)
(M : UU0 -> UU0) of MonadState S M := {
runStateT : forall A : UU0, M A -> S -> N (A * S)%type ;
runStateTret : forall (A : UU0) (a : A) (s : S),
@runStateT _ (Ret a) s = Ret (a, s) ;
runStateTbind : forall (A B : UU0) (m : M A) (f : A -> M B) (s : S),
@runStateT _ (m >>= f) s =
@runStateT _ m s >>= fun x => @runStateT _ (f x.1) x.2 ;
runStateTget : forall s : S, @runStateT _ get s = Ret (s, s) ;
runStateTput : forall s' s : S, @runStateT _ (put s') s = Ret (tt, s') }.
#[short(type=stateRunMonad)]
HB.structure Definition MonadStateRun (S : UU0) (N : monad) :=
{M of isMonadStateRun S N M & }.
Arguments runStateT {_} {_} {_} {_}.
HB.mixin Record isMonadExceptStateRun
(S : UU0) (N : exceptMonad) (M : UU0 -> UU0)
of MonadStateRun S N M & MonadExcept M := Mixin {
runStateTfail : forall (A : UU0) (s : S),
runStateT (@fail [the exceptMonad of M] A) s = @fail N _ ;
runStateTcatch : forall (A : UU0) (s : S) (m1 m2 : M A),
runStateT (@catch [the exceptMonad of M] _ m1 m2) s =
@catch N _ (runStateT m1 s) (runStateT m2 s) }.
#[short(type=exceptStateRunMonad)]
HB.structure Definition MonadExceptStateRun (S : UU0) (N : exceptMonad) :=
{M of isMonadExceptStateRun S N M & }.