-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrelude.v
1275 lines (1047 loc) · 42.4 KB
/
Prelude.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
Require Export Fol.Prelude.SfLib.
Require Export Fol.Prelude.Notations.
Require Export Coq.Arith.Compare_dec.
Require Export Coq.Arith.PeanoNat.
Require Export Coq.Bool.Bool.
Require Export Coq.Classes.RelationClasses.
Require Export Coq.Lists.List.
Require Export Coq.micromega.Lia.
Require Export Coq.Program.Basics.
Require Export Coq.Program.Utils.
Require Export Coq.Relations.Relation_Definitions.
Require Export Coq.Relations.Relation_Operators.
Require Export Coq.Setoids.Setoid.
Create HintDb simplication_hints.
#[global] Hint Rewrite forallb_app orb_true_iff orb_false_iff andb_true_iff andb_false_iff negb_true_iff negb_false_iff Nat.eqb_eq Nat.eqb_neq not_true_iff_false not_false_iff_true : simplication_hints.
Ltac obs_eqb n m :=
let H_OBS := fresh "H_OBS" in destruct (Nat.eqb n m) as [ | ] eqn: H_OBS; [rewrite Nat.eqb_eq in H_OBS | rewrite Nat.eqb_neq in H_OBS].
#[local] Obligation Tactic := idtac.
Ltac property X :=
eapply (proj2_sig X).
Tactic Notation "rewrite!" :=
autorewrite with simplication_hints in *.
#[universes(polymorphic=yes)]
Definition reify@{u v} {A : Type@{u}} {B : Type@{v}} {P : A -> B -> Prop} (f : forall x : A, { y : B | P x y }) : { f : A -> B | forall x, P x (f x) } :=
@exist (A -> B) (fun f => forall x, P x (f x)) (fun x => proj1_sig (f x)) (fun x => proj2_sig (f x)).
#[universes(polymorphic=yes)]
Definition reify_lemma@{u v} {A : Type@{u}} {B : Type@{u}} {P : A -> B -> Prop} (f : forall x : A, { y : B | P x y }) : forall x, P x (proj1_sig (@reify@{u v} A B P f) x) :=
proj2_sig (@reify@{u v} A B P f).
(** Section SETOID. *)
Class isSetoid (A : Type) : Type :=
{ eqProp (lhs : A) (rhs : A) : Prop
; eqProp_Equivalence :: Equivalence eqProp
}.
Infix "==" := eqProp : type_scope.
#[global, program]
Instance Prop_isSetoid : isSetoid Prop :=
{ eqProp := iff
; eqProp_Equivalence := iff_equivalence
}.
#[global, program]
Instance pi_isSetoid {A : Type} {B : A -> Type} `(SETOID : forall x : A, isSetoid (B x)) : isSetoid (forall x : A, B x) :=
{ eqProp f g := forall x, f x == g x }.
Next Obligation.
split.
- intros f1 x. reflexivity.
- intros f1 f2 EQ x. symmetry; exact (EQ x).
- intros f1 f2 f3 EQ EQ' x. transitivity (f2 x); [exact (EQ x) | exact (EQ' x)].
Qed.
#[program]
Definition mkSetoidFromPreOrder {A : Type} {leProp : A -> A -> Prop} `(leProp_PreOrder : @PreOrder A leProp) : isSetoid A :=
{| eqProp (x : A) (y : A) := leProp x y /\ leProp y x |}.
Next Obligation.
split; ii.
- exact (conj (@PreOrder_Reflexive A leProp leProp_PreOrder x) (@PreOrder_Reflexive A leProp leProp_PreOrder x)).
- exact (conj (proj2 H) (proj1 H)).
- exact (conj (@PreOrder_Transitive A leProp leProp_PreOrder x y z (proj1 H) (proj1 H0)) (@PreOrder_Transitive A leProp leProp_PreOrder z y x (proj2 H0) (proj2 H))).
Defined.
Lemma mkSetoidFromPreOrder_good {A : Type} {leProp : A -> A -> Prop} `(leProp_PreOrder : @PreOrder A leProp)
(SETOID := mkSetoidFromPreOrder leProp_PreOrder)
: PartialOrder SETOID.(eqProp) leProp.
Proof.
cbv. intros x y. split; exact (fun H => H).
Defined.
#[program]
Definition directProduct_of_two_Setoids {A : Type} {B : Type} (A_isSetoid : isSetoid A) (B_isSetoid : isSetoid B) : isSetoid (A * B) :=
{| eqProp lhs rhs := fst lhs == fst rhs /\ snd lhs == snd rhs |}.
Next Obligation.
ii. split.
- intros p1. split; reflexivity.
- intros p1 p2 EQ. split; symmetry. exact (proj1 EQ). exact (proj2 EQ).
- intros p1 p2 p3 EQ EQ'. split; etransitivity. exact (proj1 EQ). exact (proj1 EQ'). exact (proj2 EQ). exact (proj2 EQ').
Defined.
Lemma eqProp_refl {A : Type} `{A_isSetoid : isSetoid A}
: forall x : A, x == x.
Proof.
eapply Equivalence_Reflexive.
Defined.
Lemma eqProp_sym {A : Type} `{A_isSetoid : isSetoid A}
: forall x : A, forall y : A, x == y -> y == x.
Proof.
eapply Equivalence_Symmetric.
Defined.
Lemma eqProp_trans {A : Type} `{A_isSetoid : isSetoid A}
: forall x : A, forall y : A, forall z : A, x == y -> y == z -> x == z.
Proof.
eapply Equivalence_Transitive.
Defined.
Class eqPropCompatible1 {A : Type} {B : Type} `{A_isSetoid : isSetoid A} `{B_isSetoid : isSetoid B} (f : A -> B) : Prop :=
compatibleWith_eqProp_1 x1 x2 (x_EQ : x1 == x2) : f x1 == f x2.
Class eqPropCompatible2 {A : Type} {B : Type} {C : Type} `{A_isSetoid : isSetoid A} `{B_isSetoid : isSetoid B} `{C_isSetoid : isSetoid C} (f : A -> B -> C) : Prop :=
compatibleWith_eqProp_2 x1 x2 y1 y2 (x_EQ : x1 == x2) (y_EQ : y1 == y2) : f x1 y1 == f x2 y2.
#[global]
Add Parametric Morphism {A : Type} {B : Type} `{A_isSetoid : isSetoid A} `{B_isSetoid : isSetoid B} (f : A -> B)
`(COMPAT : @eqPropCompatible1 A B A_isSetoid B_isSetoid f)
: f with signature (eqProp ==> eqProp)
as compatibleWith_eqProp_1'.
Proof.
intros x1 x2 x_EQ. exact (compatibleWith_eqProp_1 x1 x2 x_EQ).
Defined.
#[global]
Add Parametric Morphism {A : Type} {B : Type} {C : Type} `{A_isSetoid : isSetoid A} `{B_isSetoid : isSetoid B} `{C_isSetoid : isSetoid C} (f : A -> B -> C)
`(COMPAT : @eqPropCompatible2 A B C A_isSetoid B_isSetoid C_isSetoid f)
: f with signature (eqProp ==> eqProp ==> eqProp)
as compatibleWith_eqProp_2'.
Proof.
intros x1 x2 x_EQ y1 y2 y_EQ. exact (compatibleWith_eqProp_2 x1 x2 y1 y2 x_EQ y_EQ).
Defined.
(** End SETOID. *)
(** Section PROSET. *)
Class isProset (A : Type) : Type :=
{ leProp (lhs : A) (rhs : A) : Prop
; Proset_isSetoid :: isSetoid A
; leProp_PreOrder :: PreOrder leProp
; leProp_PartialOrder :: PartialOrder eqProp leProp
}.
Infix "=<" := leProp : type_scope.
Definition Prop_isProset : isProset Prop :=
let impl_PreOrder : PreOrder impl := {| PreOrder_Reflexive (A : Prop) := @id A; PreOrder_Transitive (A : Prop) (B : Prop) (C : Prop) := @flip (B -> C) (A -> B) (A -> C) (@compose A B C); |} in
{|
leProp P Q := P -> Q;
Proset_isSetoid := mkSetoidFromPreOrder impl_PreOrder;
leProp_PreOrder := impl_PreOrder;
leProp_PartialOrder := mkSetoidFromPreOrder_good impl_PreOrder;
|}.
#[program]
Definition pi_isProset {A : Type} {B : A -> Type} `(PROSET : forall x : A, isProset (B x)) : isProset (forall x : A, B x) :=
{| leProp f g := forall x, f x =< g x; Proset_isSetoid := pi_isSetoid (fun x : A => (PROSET x).(Proset_isSetoid)) |}.
Next Obligation.
split.
- intros f1 x. exact (PreOrder_Reflexive (f1 x)).
- intros f1 f2 f3 LE1 LE2 x. exact (PreOrder_Transitive (f1 x) (f2 x) (f3 x) (LE1 x) (LE2 x)).
Defined.
Next Obligation.
i. intros f g. split; [intros f_eq_g | intros [f_le_g g_le_f] x].
- assert (claim : forall x : A, f x =< g x /\ g x =< f x).
{ intros x. eapply leProp_PartialOrder. exact (f_eq_g x). }
exact (conj (fun x => proj1 (claim x)) (fun x => proj2 (claim x))).
- eapply leProp_PartialOrder. exact (conj (f_le_g x) (g_le_f x)).
Defined.
Lemma leProp_refl {A : Type} `{A_isProset : isProset A}
: forall x : A, x =< x.
Proof.
eapply PreOrder_Reflexive.
Defined.
Lemma leProp_trans {A : Type} `{A_isProset : isProset A}
: forall x : A, forall y : A, forall z : A, x =< y -> y =< z -> x =< z.
Proof.
eapply PreOrder_Transitive.
Defined.
Lemma leProp_antisymmetry {A : Type} `{A_isProset : isProset A}
: forall x : A, forall y : A, x =< y -> y =< x -> x == y.
Proof.
intros x y x_le_y y_le_x. exact (proj2 (leProp_PartialOrder x y) (conj x_le_y y_le_x)).
Defined.
Lemma eqProp_implies_leProp {A : Type} `{A_isProset : isProset A}
: forall x : A, forall y : A, x == y -> x =< y.
Proof.
intros x y x_eq_y. exact (proj1 (proj1 (leProp_PartialOrder x y) x_eq_y)).
Defined.
Class isMonotonic1 {A : Type} {B : Type} `{A_isProset : isProset A} `{B_isProset : isProset B} (f : A -> B) : Prop :=
compatibleWith_leProp_1 x1 x2 (x_LE : x1 =< x2) : f x1 =< f x2.
Class isMonotonic2 {A : Type} {B : Type} {C : Type} `{A_isProset : isProset A} `{B_isProset : isProset B} `{C_isProset : isProset C} (f : A -> B -> C) : Prop :=
compatibleWith_leProp_2 x1 x2 y1 y2 (x_LE : x1 =< x2) (y_LE : y1 =< y2) : f x1 y1 =< f x2 y2.
#[global]
Add Parametric Morphism {A : Type} {B : Type} `{A_isProset : isProset A} `{B_isProset : isProset B} (f : A -> B)
`(MONOTONIC : @isMonotonic1 A B A_isProset B_isProset f)
: f with signature (eqProp ==> eqProp)
as isMonotonic1_compatWith_eqProp.
Proof.
ii. eapply leProp_antisymmetry.
- eapply MONOTONIC. eapply eqProp_implies_leProp. exact H.
- eapply MONOTONIC. eapply eqProp_implies_leProp. symmetry. exact H.
Defined.
#[global]
Add Parametric Morphism {A : Type} {B : Type} `{A_isProset : isProset A} `{B_isProset : isProset B} (f : A -> B)
`(MONOTONIC : @isMonotonic1 A B A_isProset B_isProset f)
: f with signature (leProp ==> leProp)
as compatibleWith_leProp_1'.
Proof.
intros x1 x2 x_LE. exact (compatibleWith_leProp_1 x1 x2 x_LE).
Defined.
#[global]
Add Parametric Morphism {A : Type} {B : Type} {C : Type} `{A_isProset : isProset A} `{B_isProset : isProset B} `{C_isProset : isProset C} (f : A -> B -> C)
`(MONOTONIC : @isMonotonic2 A B C A_isProset B_isProset C_isProset f)
: f with signature (eqProp ==> eqProp ==> eqProp)
as isMonotonic2_compatWith_eqProp.
Proof.
ii. eapply leProp_antisymmetry.
- eapply MONOTONIC; eapply eqProp_implies_leProp; assumption.
- eapply MONOTONIC; eapply eqProp_implies_leProp; symmetry; assumption.
Defined.
#[global]
Add Parametric Morphism {A : Type} {B : Type} {C : Type} `{A_isProset : isProset A} `{B_isProset : isProset B} `{C_isProset : isProset C} (f : A -> B -> C)
`(MONOTONIC : @isMonotonic2 A B C A_isProset B_isProset C_isProset f)
: f with signature (leProp ==> leProp ==> leProp)
as compatibleWith_leProp_2'.
Proof.
intros x1 x2 x_LE y1 y2 y_LE. exact (compatibleWith_leProp_2 x1 x2 y1 y2 x_LE y_LE).
Defined.
#[global]
Add Parametric Morphism {A : Type} {B : Type} {C : Type} `{A_isProset : isProset A} `{B_isProset : isProset B} `{C_isProset : isProset C} (f : A -> B -> C)
`(MONOTONIC : @isMonotonic2 A B C A_isProset B_isProset C_isProset f)
: f with signature (eqProp ==> leProp ==> leProp)
as compatibleWith_leProp_2_eqProp_l.
Proof.
intros x1 x2 x_EQ y1 y2 y_LE. exact (compatibleWith_leProp_2 x1 x2 y1 y2 (eqProp_implies_leProp x1 x2 x_EQ) y_LE).
Defined.
#[global]
Add Parametric Morphism {A : Type} {B : Type} {C : Type} `{A_isProset : isProset A} `{B_isProset : isProset B} `{C_isProset : isProset C} (f : A -> B -> C)
`(MONOTONIC : @isMonotonic2 A B C A_isProset B_isProset C_isProset f)
: f with signature (leProp ==> eqProp ==> leProp)
as compatibleWith_leProp_2_eqProp_r.
Proof.
intros x1 x2 x_LE y1 y2 y_EQ. exact (compatibleWith_leProp_2 x1 x2 y1 y2 x_LE (eqProp_implies_leProp y1 y2 y_EQ)).
Defined.
#[global]
Add Parametric Morphism {A : Type} {B : Type} {C : Type} `{A_isProset : isProset A} `{B_isProset : isProset B} `{C_isProset : isProset C} (f : A -> B -> C)
`(MONOTONIC : @isMonotonic2 A B C A_isProset B_isProset C_isProset f)
: f with signature (eqProp ==> eqProp ==> leProp)
as compatibleWith_leProp_2_eqProp_lr.
Proof.
intros x1 x2 x_EQ y1 y2 y_EQ. exact (compatibleWith_leProp_2 x1 x2 y1 y2 (eqProp_implies_leProp x1 x2 x_EQ) (eqProp_implies_leProp y1 y2 y_EQ)).
Defined.
(** End PROSET. *)
Lemma PreOrder_iff {A : Type} (R : A -> A -> Prop)
: PreOrder R <-> (forall x, forall y, R x y <-> (forall z, R z x -> R z y)).
Proof.
firstorder.
Qed.
Class isSetoid1 (F : Type -> Type) : Type :=
liftSetoid1 (X : Type) `(SETOID : isSetoid X) : isSetoid (F X).
Definition mkSetoid_from_eq {A : Type} : isSetoid A :=
{| eqProp := @eq A; eqProp_Equivalence := eq_equivalence |}.
#[global]
Instance fromSetoid1 {F : Type -> Type} {A : Type} `(SETOID1 : isSetoid1 F) : isSetoid (F A) :=
liftSetoid1 A mkSetoid_from_eq.
#[universes(polymorphic=yes)]
Definition binary_relation_on_image@{u_A u_B} {A : Type@{u_A}} {B : Type@{u_B}} (R : B -> B -> Prop) (f : A -> B) lhs rhs : Prop :=
R (f lhs) (f rhs).
#[local]
Instance relation_on_image_liftsEquivalence {A : Type} {B : Type} {eqProp : B -> B -> Prop}
(isEquivalence : Equivalence eqProp)
: forall f : A -> B, Equivalence (binary_relation_on_image eqProp f).
Proof.
intros f. constructor.
- intros x1. exact (Equivalence_Reflexive (f x1)).
- intros x1 x2 H_1EQ2. exact (Equivalence_Symmetric (f x1) (f x2) H_1EQ2).
- intros x1 x2 x3 H_1EQ2 H_2EQ3. exact (Equivalence_Transitive (f x1) (f x2) (f x3) H_1EQ2 H_2EQ3).
Defined.
#[local]
Instance relation_on_image_liftsPreOrder {A : Type} {B : Type} {leProp : B -> B -> Prop}
(isPreOrder : PreOrder leProp)
: forall f : A -> B, PreOrder (binary_relation_on_image leProp f).
Proof.
intros f. constructor.
- intros x1. exact (PreOrder_Reflexive (f x1)).
- intros x1 x2 x3 H_1LE2 H_2LE3. exact (PreOrder_Transitive (f x1) (f x2) (f x3) H_1LE2 H_2LE3).
Defined.
#[local]
Instance relation_on_image_liftsPartialOrder {A : Type} {B : Type} {eqProp : B -> B -> Prop} {leProp : B -> B -> Prop}
`{isEquivalence : @Equivalence B eqProp}
`{isPreOrder : @PreOrder B leProp}
(isPartialOrder : PartialOrder eqProp leProp)
: forall f : A -> B, PartialOrder (binary_relation_on_image eqProp f) (binary_relation_on_image leProp f).
Proof.
intros f x1 x2. constructor.
- intros H_EQ. constructor.
+ exact (proj1 (proj1 (partial_order_equivalence (f x1) (f x2)) H_EQ)).
+ exact (proj2 (proj1 (partial_order_equivalence (f x1) (f x2)) H_EQ)).
- intros H_EQ. apply (proj2 (partial_order_equivalence (f x1) (f x2))). constructor.
+ exact (proj1 H_EQ).
+ exact (proj2 H_EQ).
Defined.
Lemma relation_on_image_liftsWellFounded {A : Type} {B : Type} (R : B -> B -> Prop) (f : A -> B)
(WF : well_founded R)
: well_founded (binary_relation_on_image R f).
Proof.
intros x. remember (f x) as y eqn: y_eq_f_x.
revert x y_eq_f_x. induction (WF y) as [y' _ IH].
intros x' hyp_eq. econstructor. intros x f_x_R_f_x'.
subst y'. eapply IH; [exact f_x_R_f_x' | reflexivity].
Defined.
#[global]
Instance subSetoid {A : Type} {SETOID : isSetoid A} (P : A -> Prop) : isSetoid (@sig A P) :=
{ eqProp (lhs : @sig A P) (rhs : @sig A P) := proj1_sig lhs == proj1_sig rhs
; eqProp_Equivalence := relation_on_image_liftsEquivalence SETOID.(eqProp_Equivalence) (@proj1_sig A P)
}.
#[global]
Instance subProset {A : Type} {PROSET : isProset A} (P : A -> Prop) : isProset (@sig A P) :=
{ leProp (lhs : @sig A P) (rhs : @sig A P) := proj1_sig lhs =< proj1_sig rhs
; Proset_isSetoid := subSetoid P
; leProp_PreOrder := relation_on_image_liftsPreOrder _ (@proj1_sig A P)
; leProp_PartialOrder := relation_on_image_liftsPartialOrder _ (@proj1_sig A P)
}.
(** Section FUNCTOR. *)
Class isFunctor (F : Type -> Type) : Type :=
fmap (A : Type) (B : Type) (f : A -> B) : F A -> F B.
#[global] Arguments fmap {F} {isFunctor} {A} {B} f.
Class FunctorLaws (F : Type -> Type) `{SETOID1 : isSetoid1 F} `{FUNCTOR : isFunctor F} : Prop :=
{ fmap_eqPropCompatible1 {A : Type} {B : Type} (f : A -> B) :: eqPropCompatible1 (fmap f)
; fmap_compose {A : Type} {B : Type} {C : Type} (f : A -> B) (g : B -> C)
: fmap (@compose A B C g f) == compose (fmap g) (fmap f)
; fmap_id {A : Type}
: fmap (@id A) == id
; fmap_lifts_ext_eq {A : Type} {B : Type} (f1 : A -> B) (f2 : A -> B)
(f_EQ : forall x, f1 x = f2 x)
: fmap f1 == fmap f2
}.
#[global]
Add Parametric Morphism {F : Type -> Type} `{SETOID1 : isSetoid1 F} `{FUNCTOR : isFunctor F} (FUNCTOR_LAWS : FunctorLaws F) (A : Type) (B : Type)
: (@fmap F FUNCTOR A B) with signature (eqProp (isSetoid := pi_isSetoid (fun _ : A => @mkSetoid_from_eq B)) ==> eqProp ==> eqProp)
as fmap_compatWith_eqProp.
Proof.
intros f1 f2 f_EQ x1 x2 x_EQ. rewrite x_EQ. eapply fmap_lifts_ext_eq. exact f_EQ.
Qed.
(** End FUNCTOR. *)
(** Section MONAD. *)
Class isMonad (M : Type -> Type) : Type :=
{ bind {A : Type} {B : Type} (m : M A) (k : A -> M B) : M B
; pure {A : Type} : A -> M A
}.
Class MonadLaws (M : Type -> Type) `{SETOID1 : isSetoid1 M} `{MONAD : isMonad M} : Prop :=
{ bind_compatWith_eqProp_l {A : Type} {B : Type} (m1 : M A) (m2 : M A) (k : A -> M B)
(m_EQ : m1 == m2)
: bind m1 k == bind m2 k
; bind_compatWith_eqProp_r {A : Type} {B : Type} (m : M A) (k1 : A -> M B) (k2 : A -> M B)
(k_EQ : k1 == k2)
: bind m k1 == bind m k2
; bind_assoc {A : Type} {B : Type} {C : Type} (m : M A) (k : A -> M B) (k' : B -> M C)
: bind m (fun x => bind (k x) k') == bind (bind m k) k'
; bind_pure_l {A : Type} {B : Type} (k : A -> M B) (x : A)
: bind (pure x) k == k x
; bind_pure_r {A : Type} (m : M A)
: bind m pure == m
}.
#[global]
Add Parametric Morphism {M : Type -> Type} `{SETOID1 : isSetoid1 M} `{MONAD : isMonad M} {A: Type} {B: Type}
`(MONAD_LAWS : @MonadLaws M SETOID1 MONAD)
: (@bind M MONAD A B) with signature (eqProp ==> eqProp ==> eqProp)
as bind_compatWith_eqProp.
Proof.
intros m1 m2 m_EQ k1 k2 k_EQ.
rewrite bind_compatWith_eqProp_l with (m1 := m1) (m2 := m2) (m_EQ := m_EQ).
rewrite bind_compatWith_eqProp_r with (k1 := k1) (k2 := k2) (k_EQ := k_EQ).
reflexivity.
Qed.
#[global]
Instance mkFunctorFromMonad {M : Type -> Type} `(MONAD : isMonad M) : isFunctor M :=
fun A : Type => fun B : Type => fun f : A -> B => fun m : M A => bind m (fun x : A => pure (f x)).
Lemma mkFunctorFromMonad_good {M : Type -> Type} `{SETOID1 : isSetoid1 M} `{MONAD : isMonad M}
`(MONAD_LAWS : @MonadLaws M SETOID1 MONAD)
: @FunctorLaws M SETOID1 (mkFunctorFromMonad MONAD).
Proof.
split; ii.
- unfold fmap. unfold mkFunctorFromMonad. rewrite x_EQ. reflexivity.
- unfold compose. unfold fmap. unfold mkFunctorFromMonad. symmetry.
rewrite <- bind_assoc. eapply bind_compatWith_eqProp_r. intros i.
rewrite bind_pure_l. reflexivity.
- unfold id. unfold fmap. unfold mkFunctorFromMonad.
rewrite bind_pure_r. reflexivity.
- unfold fmap. unfold mkFunctorFromMonad. eapply bind_compatWith_eqProp_r.
intros i. rewrite f_EQ. reflexivity.
Qed.
Definition liftM2 {M : Type -> Type} {A : Type} {B : Type} {C : Type} `{MONAD : isMonad M} (f : A -> B -> C) : M A -> M B -> M C :=
fun mx => fun my => bind mx (fun x => bind my (fun y => pure (f x y))).
(** End MONAD. *)
Module E.
#[universes(polymorphic=yes)]
Definition t@{u} (A : Type@{u}) : Type@{u} := A -> Prop.
#[universes(polymorphic=yes)]
Definition In@{u} {A : Type@{u}} (x : A) (X : t@{u} A) : Prop := X x.
#[universes(polymorphic=yes)]
Definition isSubsetOf@{u} {A : Type@{u}} (X1 : t@{u} A) (X2 : t@{u} A) : Prop :=
forall x, In@{u} x X1 -> In@{u} x X2.
#[local] Infix "\in" := In : type_scope.
#[local] Infix "\subseteq" := isSubsetOf : type_scope.
#[global]
Instance ensemble_isProset {A : Type} : isProset (E.t A) :=
let PROSET : isProset (E.t A) := pi_isProset (fun _ : A => Prop_isProset) in
{|
leProp := isSubsetOf;
Proset_isSetoid := {| eqProp lhs rhs := forall x, x \in lhs <-> x \in rhs; eqProp_Equivalence := PROSET.(Proset_isSetoid).(eqProp_Equivalence) |};
leProp_PreOrder := PROSET.(leProp_PreOrder);
leProp_PartialOrder := PROSET.(leProp_PartialOrder);
|}.
#[global]
Add Parametric Morphism {A : Type}
: (@In A) with signature (eq ==> eqProp ==> iff)
as In_compatWith_eqProp.
Proof.
intros z X1 X2 X_EQ. exact (X_EQ z).
Defined.
#[global]
Instance t_isSetoid1 : isSetoid1 E.t :=
fun X : Type => fun _ : isSetoid X => (@ensemble_isProset X).(Proset_isSetoid).
#[global]
Instance t_isMonad : isMonad E.t :=
{ bind {A : Type} {B : Type} (m : E.t A) (k : A -> E.t B) := fun z => exists x, x \in m /\ z \in k x
; pure {A : Type} (x : A) := fun z => x = z
}.
#[global]
Instance t_satisfiesMonadLaws
: @MonadLaws E.t t_isSetoid1 t_isMonad.
Proof.
split; cbv; ii; firstorder congruence.
Qed.
Lemma liftM2_spec {A : Type} {B : Type} {C : Type} (f : A -> B -> C) (X : E.t A) (Y : E.t B)
: forall z, z \in liftM2 f X Y <-> exists x, x \in X /\ exists y, y \in Y /\ z = f x y.
Proof.
cbv; firstorder.
Qed.
(** Section SET_CONSTRUCTIONS. *)
Inductive unions {A : Type} (Xs : E.t (E.t A)) (x : A) : Prop :=
| In_unions X
(H_in : x \in X)
(H_IN : X \in Xs)
: x \in unions Xs.
#[local] Hint Constructors unions : core.
Lemma in_unions_iff (A : Type) Xs
: forall z, z \in @unions A Xs <-> (exists X, z \in X /\ X \in Xs).
Proof.
intros z; split; [intros [? ? ?] | intros [? [? ?]]]; eauto.
Qed.
#[global] Hint Rewrite in_unions_iff : simplication_hints.
#[global]
Instance unions_eqPropCompatible1 (A : Type)
: eqPropCompatible1 (@unions A).
Proof.
ii. do 2 rewrite in_unions_iff. now firstorder.
Qed.
Inductive union {A : Type} (X1 : E.t A) (X2 : E.t A) (x : A) : Prop :=
| In_union_l
(H_inl : x \in X1)
: x \in union X1 X2
| In_union_r
(H_inr : x \in X2)
: x \in union X1 X2.
#[local] Hint Constructors union : core.
Lemma in_union_iff (A : Type) X1 X2
: forall z, z \in @union A X1 X2 <-> (z \in X1 \/ z \in X2).
Proof.
intros z; split; [intros [? | ?] | intros [? | ?]]; eauto.
Qed.
#[global] Hint Rewrite in_union_iff : simplication_hints.
#[global]
Instance union_eqPropCompatible2 (A : Type)
: eqPropCompatible2 (@union A).
Proof.
ii. do 2 rewrite in_union_iff. now firstorder.
Qed.
Inductive empty {A : Type} : E.t A :=.
#[local] Hint Constructors empty : core.
Lemma in_empty_iff (A : Type)
: forall z, z \in @empty A <-> False.
Proof.
intros z; split; [intros [] | intros []]; eauto.
Qed.
#[global] Hint Rewrite in_empty_iff : simplication_hints.
Inductive intersection {A : Type} (X1 : E.t A) (X2 : E.t A) (x : A) : Prop :=
| In_intersection
(H_IN1 : x \in X1)
(H_IN2 : x \in X2)
: x \in intersection X1 X2.
#[local] Hint Constructors intersection : core.
Lemma in_intersection_iff (A : Type) X1 X2
: forall z, z \in @intersection A X1 X2 <-> (z \in X1 /\ z \in X2).
Proof.
intros z; split; [intros [? ?] | intros [? ?]]; eauto.
Qed.
#[global] Hint Rewrite in_intersection_iff : simplication_hints.
#[global]
Instance intersection_eqPropCompatible2 (A : Type)
: eqPropCompatible2 (@intersection A).
Proof.
ii. do 2 rewrite in_intersection_iff. now firstorder.
Qed.
Inductive singleton {A : Type} (x : A) : E.t A :=
| In_singleton
: x \in singleton x.
#[local] Hint Constructors singleton : core.
Lemma in_singleton_iff (A : Type) x
: forall z, z \in @singleton A x <-> (z = x).
Proof.
intros z; split; [intros [] | intros ->]; eauto.
Qed.
#[global] Hint Rewrite in_singleton_iff : simplication_hints.
Inductive image {A : Type} {B : Type} (f : A -> B) (X : E.t A) (y : B) : Prop :=
| In_image x
(IMAGE : y = f x)
(H_IN : x \in X)
: y \in image f X.
#[local] Hint Constructors image : core.
Lemma in_image_iff (A : Type) (B : Type) f X
: forall z, z \in @image A B f X <-> (exists x, z = f x /\ x \in X).
Proof.
intros z; split; [intros [? ? ?] | intros [? [-> ?]]]; eauto.
Qed.
#[global] Hint Rewrite in_image_iff : simplication_hints.
#[global]
Add Parametric Morphism {A : Type} {B : Type}
: (@image A B) with signature (eqProp (isSetoid := pi_isSetoid (fun _ => mkSetoid_from_eq)) ==> eqProp ==> eqProp)
as image_compatWith_eqProp.
Proof.
intros f1 f2 f_EQ X1 X2 X_EQ. intros z. do 4 red in f_EQ. do 6 red in X_EQ.
do 2 rewrite in_image_iff in *. now split; i; des; exists x; rewrite f_EQ, X_EQ in *.
Qed.
Inductive preimage {A : Type} {B : Type} (f : A -> B) (Y : E.t B) (x : A) : Prop :=
| In_preimage y
(IMAGE : y = f x)
(H_IN : y \in Y)
: x \in preimage f Y.
#[local] Hint Constructors preimage : core.
Lemma in_preimage_iff (A : Type) (B : Type) f Y
: forall z, z \in @preimage A B f Y <-> (exists y, y = f z /\ y \in Y).
Proof.
intros z; split; [intros [? ? ?] | intros [? [-> ?]]]; eauto.
Qed.
#[global] Hint Rewrite in_preimage_iff : simplication_hints.
#[global]
Add Parametric Morphism {A : Type} {B : Type}
: (@preimage A B) with signature (eqProp (isSetoid := pi_isSetoid (fun _ => mkSetoid_from_eq)) ==> eqProp ==> eqProp)
as preimage_compatWith_eqProp.
Proof.
intros f1 f2 f_EQ Y1 Y2 Y_EQ. intros z. do 4 red in f_EQ. do 6 red in Y_EQ.
do 2 rewrite in_preimage_iff in *. now split; i; des; exists y; rewrite f_EQ, Y_EQ in *.
Qed.
#[universes(polymorphic=yes)]
Definition fromList@{u} {A : Type@{u}} (xs : list A) : E.t@{u} A :=
fun x => List.In x xs.
#[global] Hint Unfold fromList : simplication_hints.
#[universes(polymorphic=yes)]
Definition full@{u} {A : Type@{u}} : E.t@{u} A :=
fun x => True.
#[global] Hint Unfold full : simplication_hints.
#[universes(polymorphic=yes)]
Definition insert@{u} {A : Type@{u}} (x1 : A) (X2 : E.t@{u} A) : E.t@{u} A :=
fun x => x = x1 \/ x \in X2.
#[global] Hint Unfold insert : simplication_hints.
Lemma in_insert_iff (A : Type) x1 X2
: forall z, z \in @insert A x1 X2 <-> (z = x1 \/ z \in X2).
Proof.
reflexivity.
Qed.
#[global] Hint Rewrite in_insert_iff : simplication_hints.
#[global]
Add Parametric Morphism {A : Type}
: (@insert A) with signature (eq ==> eqProp ==> eqProp)
as insert_compatWith_eqProp.
Proof.
firstorder.
Qed.
#[universes(polymorphic=yes)]
Definition complement@{u} {A : Type@{u}} (X : E.t@{u} A) : E.t@{u} A :=
fun x => ~ x \in X.
#[global] Hint Unfold complement : simplication_hints.
Lemma in_complement_iff (A : Type) X
: forall z, z \in @complement A X <-> (~ z \in X).
Proof.
reflexivity.
Qed.
#[global] Hint Rewrite in_complement_iff : simplication_hints.
#[global]
Add Parametric Morphism {A : Type}
: (@complement A) with signature (eqProp ==> eqProp)
as complement_compatWith_eqProp.
Proof.
firstorder.
Qed.
#[universes(polymorphic=yes)]
Definition delete@{u} {A : Type@{u}} (x1 : A) (X2 : E.t@{u} A) : E.t@{u} A :=
fun x => x <> x1 /\ x \in X2.
#[global] Hint Unfold delete : simplication_hints.
Lemma in_delete_iff (A : Type) x1 X2
: forall z, z \in @delete A x1 X2 <-> (z <> x1 /\ z \in X2).
Proof.
reflexivity.
Qed.
#[global] Hint Rewrite in_delete_iff : simplication_hints.
#[global]
Add Parametric Morphism {A : Type}
: (@delete A) with signature (eq ==> eqProp ==> eqProp)
as delete_compatWith_eqProp.
Proof.
firstorder.
Qed.
#[universes(polymorphic=yes)]
Definition intersections@{u} {A : Type@{u}} (Xs : E.t@{u} (E.t@{u} A)) : E.t@{u} A :=
fun x => forall X, X \in Xs -> x \in X.
#[global] Hint Unfold intersections : simplication_hints.
Lemma in_intersections_iff (A : Type) Xs
: forall z, z \in @intersections A Xs <-> (forall X, X \in Xs -> z \in X).
Proof.
reflexivity.
Qed.
#[global] Hint Rewrite in_intersections_iff : simplication_hints.
#[global]
Add Parametric Morphism {A : Type}
: (@intersections A) with signature (eqProp ==> eqProp)
as intersections_compatWith_eqProp.
Proof.
firstorder.
Qed.
(** End SET_CONSTRUCTIONS. *)
Ltac unfold_E := repeat
match goal with
| [ H : context[ E.In _ (fun _ => _) ] |- _ ] => unfold E.In in H
| [ |- context[ E.In _ (fun _ => _) ] ] => unfold E.In
| [ H : context[ E.isSubsetOf _ (fun _ => _) ] |- _ ] => unfold E.isSubsetOf in H
| [ |- context[ E.isSubsetOf _ (fun _ => _) ] ] => unfold E.isSubsetOf
end.
#[global]
Instance subseteq_PreOrder {A : Type}
: PreOrder (@E.isSubsetOf A).
Proof.
exact (leProp_PreOrder).
Defined.
Lemma insert_monotonic {A : Type} (x1 : A) (X2 : E.t A) (X2' : E.t A)
(SUBSET : X2 \subseteq X2')
: E.insert x1 X2 \subseteq E.insert x1 X2'.
Proof.
intros z [-> | H_IN]; [left; reflexivity | right; exact (SUBSET z H_IN)].
Qed.
End E.
Notation ensemble := E.t.
#[local] Infix "\in" := E.In : type_scope.
#[local] Infix "\subseteq" := E.isSubsetOf : type_scope.
Lemma unfold_ensemble_eqProp (A : Type) (X1 : ensemble A) (X2 : ensemble A)
: X1 == X2 <-> (X1 \subseteq X2 /\ X2 \subseteq X1).
Proof.
transitivity (forall z, z \in X1 <-> z \in X2).
- reflexivity.
- firstorder.
Qed.
#[global] Hint Rewrite unfold_ensemble_eqProp : simplication_hints.
Tactic Notation "s!" :=
repeat (
autorewrite with simplication_hints in *;
E.unfold_E;
simpl in *;
autounfold with simplication_hints in *
).
Tactic Notation "ss!" :=
s!; subst; eauto with *; firstorder (try first [lia | congruence | f_equal]).
Tactic Notation "done!" :=
now ii; first [congruence | lia | repeat ss!; done].
Section OPERATION_PROPS.
Context {A : Type} `{SETOID : isSetoid A}.
Class isAssociative (f : A -> A -> A) : Prop :=
assoc x y z : f x (f y z) == f (f x y) z.
Class isCommutative (f : A -> A -> A) : Prop :=
comm x y : f x y == f y x.
Class isIdempotent (f : A -> A -> A) : Prop :=
idem x : f x x == x.
Class distributesOver (MUL : A -> A -> A) (ADD : A -> A -> A) : Prop :=
{ left_distr x y z
: MUL x (ADD y z) == ADD (MUL x y) (MUL x z)
; right_distr x y z
: MUL (ADD y z) x == ADD (MUL y x) (MUL z x)
}.
Class isIdentityElementOf (e : A) (f : A -> A -> A) : Prop :=
{ left_id x
: f e x == x
; right_id x
: f x e == x
}.
Class isInverseOperatorOf (INV : A -> A) (OP : A -> A -> A) {e : A} {IDENTITY : isIdentityElementOf e OP} : Prop :=
{ left_inv x
: OP (INV x) x == e
; right_inv x
: OP x (INV x) == e
}.
Class AbsorptionLaw (OP1 : A -> A -> A) (OP2 : A -> A -> A) : Prop :=
{ absorption_law1 x y
: OP1 x (OP2 x y) == x
; absorption_law2 x y
: OP2 x (OP1 x y) == x
}.
Class isAnnihilatorFor (a : A) (OP : A -> A -> A) : Prop :=
{ left_ann x
: OP a x == a
; right_ann x
: OP x a == a
}.
#[local]
Instance inverse_compatWith_eqProp (OP : A -> A -> A) (e : A) (INV : A -> A)
(COMPAT : eqPropCompatible2 OP)
(ASSOCIATIVE : isAssociative OP)
(IDENTITY : isIdentityElementOf e OP)
(INVERSE : isInverseOperatorOf INV OP)
: eqPropCompatible1 INV.
Proof.
ii. rewrite <- right_id.
rewrite <- right_inv with (x := x2).
assert (claim : OP (INV x1) x2 == e).
{ rewrite <- x_EQ. eapply left_inv. }
rewrite assoc. rewrite claim. eapply left_id.
Qed.
End OPERATION_PROPS.
Class isClosureOperator {A : Type} `{PROSET : isProset A} (cl : A -> A) : Prop :=
{ cl_op_extensive : forall x, x =< cl x
; cl_op_idempotent : forall x, cl (cl x) == cl x
; cl_op_monotonic :: isMonotonic1 cl
}.
Lemma isClosureOperator_iff {A : Type} `{PROSET : isProset A} (cl : A -> A)
: isClosureOperator cl <-> (forall x, forall y, x =< cl y <-> cl x =< cl y).
Proof.
split.
- intros [EXTENSIVE IDEMPOTENT MONOTONIC] x y. split; intros LE.
+ rewrite <- IDEMPOTENT with (x := y). eapply MONOTONIC. exact LE.
+ transitivity (cl x). eapply EXTENSIVE. exact LE.
- intros IFF. split; ii.
+ rewrite -> IFF. reflexivity.
+ eapply leProp_antisymmetry.
* rewrite <- IFF. reflexivity.
* rewrite -> IFF. reflexivity.
+ rewrite <- IFF. transitivity x2. exact x_LE. rewrite -> IFF. reflexivity.
Qed.
Definition eqProp_cl {A : Type} `{SETOID : isSetoid A} (X : ensemble A) : ensemble A :=
fun z => exists x, z == x /\ x \in X.
#[global]
Instance eqProp_cl_isClosureOperator {A : Type} `{SETOID : isSetoid A}
: isClosureOperator eqProp_cl.
Proof.
rewrite isClosureOperator_iff. intros x y. split; intros LE.
- intros a [b [EQ IN]]. pose proof (LE b IN) as [c [EQ' IN']].
exists c. split; trivial. etransitivity; eauto.
- intros a IN. eapply LE. exists a. split; trivial. reflexivity.
Qed.
Class isCompatibleWith_eqProp {A : Type} `{SETOID : isSetoid A} (P : A -> Prop) : Prop :=
compatWith_eqProp : forall x, P x -> forall y, x == y -> P y.
#[global]
Add Parametric Morphism {A : Type} {SETOID : isSetoid A} (P : A -> Prop) (COMPAT : isCompatibleWith_eqProp (A := A) (SETOID := SETOID) P)
: P with signature (eqProp ==> iff)
as isCompatibleWith_eqProp_sig_eqProp_iff.
Proof.
intros x y EQ; red in COMPAT. now split; intros H_P; eapply COMPAT; eauto.
Qed.
#[global]
Instance eqProp_cl_isCompatibleWith_eqProp {A : Type} `{SETOID : isSetoid A} (X : ensemble A)
: isCompatibleWith_eqProp (eqProp_cl X).
Proof.
intros x [a [EQ IN]] y EQ'. exists a. split. rewrite <- EQ'. exact EQ. exact IN.
Qed.
#[global]
Instance isCompatibleWith_eqProp_forall {A : Type} {B : A -> Type} `{SETOID : forall i : A, isSetoid (B i)} (P : forall i : A, B i -> Prop)
(COMPAT : forall i : A, isCompatibleWith_eqProp (P i))
: isCompatibleWith_eqProp (fun x => forall i : A, P i (x i)).
Proof.
intros x P_x y EQ i. exact (COMPAT i (x i) (P_x i) (y i) (EQ i)).
Defined.
#[global]
Instance isCompatibleWith_eqProp_exists {A : Type} {B : A -> Type} `{SETOID : forall i : A, isSetoid (B i)} (P : forall i : A, B i -> Prop)
(COMPAT : forall i : A, isCompatibleWith_eqProp (P i))
: isCompatibleWith_eqProp (fun x => exists i : A, P i (x i)).
Proof.
intros x [i P_x] y EQ. exists i. exact (COMPAT i (x i) P_x (y i) (EQ i)).
Defined.
Module B.
#[local] Open Scope program_scope.
#[universes(polymorphic=yes)]
Definition dollar@{u v} {A : Type@{u}} {B : Type@{v}} (f : A -> B) (x : A) : B := f x.
#[local] Infix "$" := dollar.
#[local] Infix ">>=" := bind.
Definition kcompose {M} {MONAD : isMonad M} {A} {B} {C} (k1 : A -> M B) (k2 : B -> M C) : A -> M C :=
fun x => k1 x >>= k2.
#[local] Infix ">=>" := kcompose : program_scope.
Record stateT (S : Type) (M : Type -> Type) (X : Type) : Type :=
StateT { runStateT : S -> M (X * S)%type }.
#[global] Arguments StateT {S} {M} {X}.
#[global] Arguments runStateT {S} {M} {X}.
#[global]
Instance stateT_isMonad {S} {M} `(M_isMonad : isMonad M) : isMonad (B.stateT S M) :=
{ pure {A} (x : A) := B.StateT $ curry pure x
; bind {A} {B} (m : B.stateT S M A) (k : A -> B.stateT S M B) := B.StateT $ B.runStateT m >=> uncurry (B.runStateT ∘ k)
}.
Definition stateT_isSetoid {S} {M} `{SETOID1 : isSetoid1 M} X : isSetoid (B.stateT S M X) :=
{|
eqProp lhs rhs := forall s, B.runStateT lhs s == B.runStateT rhs s;
eqProp_Equivalence := relation_on_image_liftsEquivalence (pi_isSetoid (fun _ => fromSetoid1 SETOID1)).(eqProp_Equivalence) B.runStateT;
|}.
#[local]
Instance stateT_isSetoid1 {S} {M} `{SETOID1 : isSetoid1 M} : isSetoid1 (B.stateT S M) :=
fun X : Type => fun _ : isSetoid X => @stateT_isSetoid S M SETOID1 X.
#[local]
Instance stateT_satisfiesMonadLaws {S} {M} `{SETOID1 : isSetoid1 M} `{MONAD : isMonad M}
`(MONAD_LAWS : @MonadLaws M SETOID1 MONAD)
: MonadLaws (B.stateT S M).
Proof.
split; i.
- destruct m1 as [m1], m2 as [m2]; simpl in *. intros s. eapply bind_compatWith_eqProp_l. exact (m_EQ s).
- destruct m as [m]; simpl in *. intros s. eapply bind_compatWith_eqProp_r. intros [x s']. exact (k_EQ x s').
- destruct m as [m]; simpl in *. intros s. unfold kcompose. rewrite <- bind_assoc.
eapply bind_compatWith_eqProp_r. intros [x s']. reflexivity.
- destruct (k x) as [m] eqn: H_OBS. simpl in *. intros s. unfold kcompose. unfold curry. rewrite bind_pure_l. simpl.
unfold compose. rewrite H_OBS. reflexivity.
- destruct m as [m]; simpl in *. intros s. unfold kcompose. rewrite <- bind_pure_r with (m := m s) at 2.
eapply bind_compatWith_eqProp_r. intros [x s']. reflexivity.
Qed.
Lemma Some_ne_None {A : Type} (x : A)
: Some x <> None.
Proof.
assert (TRUE : option_rect (fun _ : option A => Prop) (fun _ : A => True) (False) (Some x)) by exact I.
intros EQ. rewrite EQ in TRUE. exact TRUE.
Defined.
Definition maybe {A : Type} {B : Type} (d : B) (f : A -> B) (m : option A) : B :=
match m with
| None => d
| Some x => f x
end.
Definition either {A : Type} {B : Type} {C : Type} (f : A -> C) (g : B -> C) (z : A + B) : C :=
match z with
| inl x => f x
| inr y => g y
end.