-
Notifications
You must be signed in to change notification settings - Fork 1
/
proof_treiber_history_old.v
2549 lines (2303 loc) · 109 KB
/
proof_treiber_history_old.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From gpfsl.examples Require Import sflib.
From iris.algebra Require Import auth gset gmap agree.
From iris.algebra Require Import lib.mono_list.
From iris.proofmode Require Import tactics.
From gpfsl.base_logic Require Import meta_data.
From gpfsl.examples.algebra Require Import mono_list_list.
From gpfsl.logic Require Import logatom atomics invariants.
From gpfsl.logic Require Import repeat_loop new_delete.
From gpfsl.examples Require Import map_seq loc_helper.
From gpfsl.examples.stack Require Import spec_history_old code_treiber.
Require Import iris.prelude.options.
(** Prove that Treiber stack satisfies the logically atomic, history-based
specifications *)
#[local] Notation next := 0%nat (only parsing).
#[local] Notation data := 1%nat (only parsing).
#[local] Notation history := (history sevent).
#[local] Notation graph_event := (graph_event sevent).
Implicit Types
(stk : stack_state) (node : event_id * Z * view * logView)
(** mo of the head pointer, not including the initializing write. *)
(mo : list (option loc * view))
(** Event mo. This is used for deriving the linearization order [lin].
Write event ([Push]/[Pop]) order coincides with xo and read-only events ([EmpPop])
are associated with the write event that they read. The generation [O] refers
to [esi], the list of empty pops that reads the head pointer init msg
(non-atomic). The generation [S gen'] refers to [gen']-th element of [emo],
which is a triple of a write event that created the generation, a list of
associated empty pops, and the msg written by the write event (using CAS). *)
(emo : list ((event_id * list event_id) * (option loc * view)))
(esi : list event_id)
(M : logView)
(s n: loc) (tid: thread_id) (γh γe: gname).
Section emo.
Local Open Scope nat_scope.
(** Find the generation of [emo] started by a non-empty event [e]. *)
Definition emo_find emo e : option ((event_id * (list event_id)) * (option loc * view)) :=
snd <$> (list_find (λ '((e', _), (_, _)), e' = e) emo).
Definition omsg_of_id emo e : option (option loc * view) := snd <$> emo_find emo e.
Definition oloc_of_id emo e : option loc := mjoin (fst <$> omsg_of_id emo e).
(** Index to an event in emo. [eidx] *)
Inductive emo_idx : Set :=
| emo_e (gen i' : nat) (* [i']-th empty pop at generation [gen] *)
| emo_ne (gen' : nat) (* the non-empty event of generation [S gen'] *)
.
Definition gen_of eidx :=
match eidx with
| emo_e gen _ => gen
| emo_ne gen' => (S gen')
end.
Definition lookup_emo esi emo eidx : option event_id :=
match eidx with
| emo_e gen i' =>
match gen with
| O => esi !! i'
| S gen' => emo.*1.*2 !! gen' ≫= (!!) i'
end
| emo_ne gen' => emo.*1.*1 !! gen'
end.
(** "Lexicographic" order of emo index *)
Inductive emo_idx_le : ∀ (eidx1 eidx2 : emo_idx), Prop :=
| emo_idx_le_e_e_1 gen1 i1' gen2 i2'
(LTgen : gen1 < gen2)
: emo_idx_le (emo_e gen1 i1') (emo_e gen2 i2')
| emo_idx_le_e_e_2 gen i1' i2'
(LEi' : i1' ≤ i2')
: emo_idx_le (emo_e gen i1') (emo_e gen i2')
| emo_idx_le_e_ne_1 gen1 i1' gen2'
(LTgen : gen1 < S gen2')
: emo_idx_le (emo_e gen1 i1') (emo_ne gen2')
| emo_idx_le_e_ne_2 gen' i1'
: emo_idx_le (emo_e (S gen') i1') (emo_ne gen')
| emo_idx_le_ne_e gen1' gen2 i2'
(LTgen : S gen1' ≤ gen2)
: emo_idx_le (emo_ne gen1') (emo_e gen2 i2')
| emo_idx_le_ne_ne gen1' gen2'
(LEgen : gen1' ≤ gen2')
: emo_idx_le (emo_ne gen1') (emo_ne gen2')
.
(** hb implies emo-before. *)
Definition hb_emo (E : history) esi emo :=
∀ eidx1 eidx2 e1 e2 eV2
(EMO_eidx1 : lookup_emo esi emo eidx1 = Some e1)
(EMO_eidx2 : lookup_emo esi emo eidx2 = Some e2)
(EV2 : E !! e2 = Some eV2)
(IN_LVIEW : e1 ∈ eV2.(ge_lview)),
emo_idx_le eidx1 eidx2.
(** Linearization of [esi] and [emo]. [lin] *)
Definition lin_of_emo esi emo :=
esi ++ concat ((λ '((e, es), _), e :: es) <$> emo).
Lemma lin_of_emo_singleton e es msg :
lin_of_emo [] [(e, es, msg)] = [e] ++ es.
Proof.
unfold lin_of_emo; by list_simplifier.
Qed.
Lemma lin_of_emo_app esi emo1 emo2 :
lin_of_emo esi (emo1 ++ emo2) =
(lin_of_emo esi emo1) ++ (lin_of_emo [] emo2).
Proof.
unfold lin_of_emo; list_simplifier. by rewrite concat_app.
Qed.
(** Insert (append) empty pop to the generation [S gen'] of [emo]. *)
Definition emo_insert_e emo gen' e :=
alter (λ '((e', es'), msg'), ((e', es'++[e]), msg')) gen' emo.
(** Convert an [eidx] into an index in [lin]. *)
Definition emo_idx_to_lin_idx esi emo eidx :=
match eidx with
| emo_e gen i' =>
match gen with
| O => i'
| S gen' =>
length esi
+ length (concat ((λ '((e, es), _), e :: es) <$> take gen' emo))
+ S i'
end
| emo_ne gen' =>
length esi
+ length (concat ((λ '((e, es), _), e :: es) <$> take gen' emo))
end
.
(** [esi] is well-formed. *)
Inductive esi_good (E : history) esi : Prop :=
| esi_good_intro
(EMPPOPS : Forall (λ e', ∃ eV', E !! e' = Some eV' ∧ eV'.(ge_event) = EmpPop) esi)
(EMPPOP_XO_SUB :
esi `sublist_of` (seq 0 (length E)))
.
(** Each generation of [emo] is well-formed *)
Inductive emo_gen_good (E : history) e (es : list event_id) (on : option loc) V : Prop :=
| emo_gen_good_intro eV
(EV : E !! e = Some eV)
(NOT_EMPPOP : eV.(ge_event) ≠ EmpPop)
(VIEW : V = eV.(ge_view).(dv_wrt))
(EMPPOPS : Forall (λ e', ∃ eV',
« EV' : E !! e' = Some eV' » ∧
« EMPPOP : eV'.(ge_event) = EmpPop » ∧
(* TODO: Does this hold in the presence of promises? Is there other
relation (e.g. view) that we can use instead? *)
« RW_XO : e < e' » ) es)
(EMPPOP_EMPTY :
(* Empty pops can be inserted only when the stack is empty *)
∀ (NE : es ≠ []),
« INTERP_EMTPY : stack_interp E (filter (not_empty_pop E) (seq 0 e) ++ [e]) [] [] »)
(EMPTYING_POP :
(* The event that makes the stack empty writes null. *)
stack_interp E (filter (not_empty_pop E) (seq 0 e) ++ [e]) [] []
↔ on = None)
(EMPPOP_XO_SUB :
(* The linearization order of the same-generation empty pops is xo. *)
es `sublist_of` (seq 0 (length E)))
.
(** [esi] + [emo] is well-formed. *)
Inductive esi_emo_good (E : history) esi emo : Prop :=
| emo_good_intro
(ESI_GOOD : esi_good E esi)
(GEN_GOOD : Forall (λ '((e, es), (on, V)), emo_gen_good E e es on V) emo)
(XO_AGREE : emo.*1.*1 = write_xo E)
(HB_EMO : hb_emo E esi emo)
(LIN_PERM : lin_of_emo esi emo ≡ₚ seq 0 (length E))
.
(* No [Inj] instance because it's partial *)
Definition emo_inj esi emo :=
∀ eidx1 eidx2 e
(EMO_LOOKUP1 : lookup_emo esi emo eidx1 = Some e)
(EMO_LOOKUP2 : lookup_emo esi emo eidx2 = Some e),
eidx1 = eidx2.
Tactic Notation "destruct_eidx" ident(eidx) ident(EMO_LOOKUP) "as" ident(gen') ident(i') ident(e) ident(es) ident(msg) ident(GEN) ident(ES_i') ident(LEgen') ident(LEi') :=
destruct eidx as [[|gen'] i'|gen']; simpl in *;
[apply lookup_lt_Some in EMO_LOOKUP as LEi'
|apply bind_Some in EMO_LOOKUP as [es [GEN ES_i']];
apply list_lookup_fmap_inv in GEN as ([e ?] & [= <-] & GEN);
apply list_lookup_fmap_inv in GEN as ([[? ?] msg] & [= <- <-] & GEN);
apply lookup_lt_Some in GEN as LEgen';
apply lookup_lt_Some in ES_i' as LEi'
|apply list_lookup_fmap_inv in EMO_LOOKUP as ([? es] & [= <-] & GEN);
apply list_lookup_fmap_inv in GEN as ([[? ?] msg] & [= <- <-] & GEN);
apply lookup_lt_Some in GEN as LEgen'].
Section emo_lemmas.
#[global] Instance emo_idx_eqdec : EqDecision emo_idx.
Proof. solve_decision. Qed.
Lemma length_concat_fmap_take_plus_S (f : _ → list event_id) gen1' emo x N
(GEN1 : emo !! gen1' = Some x) :
length (concat (f <$> take (gen1' + S N) emo))
= length (concat (f <$> take gen1' emo)) + (length (f x) + length (concat (f <$> take N (drop (S gen1') emo)))).
Proof.
apply lookup_lt_Some in GEN1 as LEgen1'.
rewrite -{1}(take_drop gen1' emo).
rewrite take_add_app; last first. { rewrite take_length. lia. }
rewrite fmap_app concat_app app_length.
rewrite (drop_S _ _ _ GEN1). rewrite firstn_cons fmap_cons /=.
rewrite app_length. done.
Qed.
Lemma emo_idx_to_lin_idx_inj esi emo eidx1 eidx2 e
(EMO_LOOKUP1 : lookup_emo esi emo eidx1 = Some e)
(EMO_LOOKUP2 : lookup_emo esi emo eidx2 = Some e)
(EQlin : emo_idx_to_lin_idx esi emo eidx1 = emo_idx_to_lin_idx esi emo eidx2) :
eidx1 = eidx2.
Proof.
unfold emo_idx_to_lin_idx in *.
set (f := λ '(e0, es, _), e0 :: es) in *.
destruct_eidx eidx1 EMO_LOOKUP1 as gen1' i1' e1 es1 msg1 GEN1 ES_i1' LEgen1' LEi1';
destruct_eidx eidx2 EMO_LOOKUP2 as gen2' i2' e2 es2 msg2 GEN2 ES_i2' LEgen2' LEi2'.
- by simplify_eq.
- lia.
- lia.
- lia.
- case (decide (gen1' = gen2')) as [->|NEgen].
{ simplify_eq. by assert (i1' = i2') as -> by lia. }
exfalso.
wlog: gen1' gen2' i1' i2' e1 e2 es1 es2 msg1 msg2
GEN1 GEN2 LEgen1' LEgen2' ES_i1' ES_i2' LEi1' LEi2' EQlin NEgen
/ gen1' < gen2'.
{ case (le_gt_dec gen1' gen2') => ? X.
- eapply (X gen1' gen2' i1' i2'); (done || lia).
- by eapply (X gen2' gen1' i2' i1'). }
clear NEgen. intros LT.
assert (∃ (N : nat), gen2' = gen1' + (S N)) as [N ->].
{ exists (gen2' - gen1' - 1). lia. } clear LT.
have {}EQlin : length (concat (f <$> take gen1' emo)) + i1' = length (concat (f <$> take (gen1' + S N) emo)) + i2' by lia.
erewrite length_concat_fmap_take_plus_S in EQlin; [|done]. simpl in EQlin.
assert (i1' = S (length es1 + length (concat (f <$> take N (drop (S gen1') emo)))) + i2') as ->; lia.
- (* symmetric to 8th case *) exfalso.
have {}EQlin : length (concat (f <$> take gen1' emo)) + S i1' = length (concat (f <$> take gen2' emo)) by lia.
have [LT|[EQ|GT]] : gen1' < gen2' ∨ gen1' = gen2' ∨ gen1' > gen2' by lia.
+ assert (∃ (N : nat), gen2' = gen1' + (S N)) as [N ->].
{ exists (gen2' - gen1' - 1). lia. } clear LT.
erewrite length_concat_fmap_take_plus_S in EQlin; [|done]. simpl in EQlin.
assert (i1' = length es1 + length (concat (f <$> take N (drop (S gen1') emo)))); lia.
+ subst. lia.
+ assert (∃ (N : nat), gen1' = gen2' + (S N)) as [N ->].
{ exists (gen1' - gen2' - 1). lia. } clear GT.
erewrite length_concat_fmap_take_plus_S in EQlin; [|done]. simpl in EQlin.
assert (i1' = length es2 + length (concat (f <$> take N (drop (S gen2') emo)))); lia.
- lia.
- (* symmetric to 6th case *) exfalso.
have {}EQlin : length (concat (f <$> take gen2' emo)) + S i2' = length (concat (f <$> take gen1' emo)) by lia.
have [LT|[EQ|GT]] : gen2' < gen1' ∨ gen2' = gen1' ∨ gen2' > gen1' by lia.
+ assert (∃ (N : nat), gen1' = gen2' + (S N)) as [N ->].
{ exists (gen1' - gen2' - 1). lia. } clear LT.
erewrite length_concat_fmap_take_plus_S in EQlin; [|done]. simpl in EQlin.
assert (i2' = length es2 + length (concat (f <$> take N (drop (S gen2') emo)))); lia.
+ subst. lia.
+ assert (∃ (N : nat), gen2' = gen1' + (S N)) as [N ->].
{ exists (gen2' - gen1' - 1). lia. } clear GT.
erewrite length_concat_fmap_take_plus_S in EQlin; [|done]. simpl in EQlin.
assert (i2' = length es1 + length (concat (f <$> take N (drop (S gen1') emo)))); lia.
- case (decide (gen1' = gen2')) as [->|NEgen]; first by simplify_eq.
exfalso.
wlog: gen1' gen2' es1 es2 msg1 msg2 GEN1 GEN2 LEgen1' LEgen2' EQlin NEgen
/ gen1' < gen2'.
{ case (le_gt_dec gen1' gen2') => ? X.
- eapply (X gen1' gen2'); (done || lia).
- by eapply (X gen2' gen1'). }
clear NEgen. intros LT.
assert (∃ (N : nat), gen2' = gen1' + (S N)) as [N ->].
{ exists (gen2' - gen1' - 1). lia. } clear LT.
erewrite length_concat_fmap_take_plus_S in EQlin; [|done]. simpl in EQlin. lia.
Qed.
Lemma lin_of_emo_split_esi esi emo :
lin_of_emo esi emo = esi ++ lin_of_emo [] emo.
Proof. done. Qed.
Lemma lin_of_emo_add_emo_ne esi emo e es msg :
lin_of_emo esi (emo ++ [(e, es, msg)]) = lin_of_emo esi emo ++ e :: es.
Proof.
unfold lin_of_emo. list_simplifier. rewrite concat_app /=. list_simplifier. done.
Qed.
Lemma lookup_emo_app esi emo emo' eidx e
(EMO_LOOKUP : lookup_emo esi emo eidx = Some e) :
lookup_emo esi (emo ++ emo') eidx = Some e.
Proof.
unfold lookup_emo in *. list_simplifier. repeat case_match.
- done.
- apply bind_Some in EMO_LOOKUP as [es [GEN ES_i']].
have LT := GEN. apply lookup_lt_Some in LT.
rewrite lookup_app_l; [|done]. rewrite GEN. done.
- have LT := EMO_LOOKUP. apply lookup_lt_Some in LT.
rewrite lookup_app_l; [|done]. rewrite EMO_LOOKUP. done.
Qed.
Lemma lookup_emo_inv_e esi emo gen i' e
(EMO_LOOKUP : lookup_emo esi emo (emo_e gen i') = Some e) :
∃ es,
« GEN : (esi :: emo.*1.*2) !! gen = Some es » ∧
« ES_i' : es !! i' = Some e ».
Proof.
unfold lookup_emo in *. case Egen: gen => [|gen']; subst.
- by exists esi.
- apply bind_Some in EMO_LOOKUP as [es [??]]. by exists es.
Qed.
Lemma lookup_emo_inv_ne esi emo gen' e
(EMO_LOOKUP : lookup_emo esi emo (emo_ne gen') = Some e) :
« GEN : emo.*1.*1 !! gen' = Some e ».
Proof. done. Qed.
Lemma emo_idx_to_lin_idx_emo_app esi emo emo' eidx e
(EMO_LOOKUP : lookup_emo esi emo eidx = Some e) :
emo_idx_to_lin_idx esi emo eidx = emo_idx_to_lin_idx esi (emo ++ emo') eidx.
Proof.
unfold emo_idx_to_lin_idx. case Eeidx: eidx => [[|gen'] i'|gen'].
- done.
- subst. apply lookup_emo_inv_e in EMO_LOOKUP. des. simplify_list_eq.
have LT : gen' < length emo.
{ apply lookup_lt_Some in GEN. by rewrite !fmap_length in GEN. }
rewrite take_app_le; [done|lia].
- subst. apply lookup_emo_inv_ne in EMO_LOOKUP as GEN. des.
have LT : gen' < length emo.
{ apply lookup_lt_Some in GEN. by rewrite !fmap_length in GEN. }
rewrite take_app_le; [done|lia].
Qed.
Lemma lin_of_emo_lookup_lookup_emo esi emo i e
(NODUP : NoDup (lin_of_emo esi emo))
(LIN_I : lin_of_emo esi emo !! i = Some e) :
∃ eidx,
« IDX : i = emo_idx_to_lin_idx esi emo eidx » ∧
« EMO_LOOKUP : lookup_emo esi emo eidx = Some e ».
Proof.
revert i e NODUP LIN_I.
induction emo as [|[[e' es] msg] emo] using rev_ind; intros.
{ exists (emo_e O i). unfold lin_of_emo in *. list_simplifier. done. }
rewrite ->lin_of_emo_add_emo_ne,NoDup_app in NODUP. des.
rewrite lin_of_emo_add_emo_ne in LIN_I.
set len := length (lin_of_emo esi emo).
have Hi : i < len ∨ i = len ∨ i > len by lia. des.
- (* use [i] in IH to get a [eidx] and use it *)
rewrite lookup_app_l in LIN_I; [|done].
specialize (IHemo _ _ NODUP LIN_I). des.
exists eidx. split; red.
+ by erewrite emo_idx_to_lin_idx_emo_app in IDX.
+ by eapply lookup_emo_app.
- rewrite list_lookup_middle in LIN_I; [|done]. simplify_eq.
exists (emo_ne (length emo)). list_simplifier. split; red.
+ subst len. by rewrite /lin_of_emo app_length.
+ by rewrite -!(fmap_length fst) lookup_app_1_eq.
- rewrite ->lookup_app_r,lookup_cons in LIN_I; [|lia].
case Esub: (i - len) => [|i']; [lia|]. rewrite -/len Esub in LIN_I.
have {Esub}Hi : i = len + S i' by lia. subst.
subst len. unfold lin_of_emo in *. rewrite app_length.
exists (emo_e (S (length emo)) i'). split; red.
+ rewrite /emo_idx_to_lin_idx. list_simplifier. done.
+ unfold lookup_emo. list_simplifier.
by rewrite -(fmap_length fst) -(fmap_length snd) lookup_app_1_eq.
Qed.
Lemma lookup_emo_lin_of_emo_lookup esi emo eidx e
(EMO_LOOKUP : lookup_emo esi emo eidx = Some e) :
lin_of_emo esi emo !! (emo_idx_to_lin_idx esi emo eidx) = Some e.
Proof.
unfold lin_of_emo, emo_idx_to_lin_idx.
set (f := λ '(e0, es, _), e0 :: es) in *.
destruct eidx as [[|gen'] i'|gen']; simpl in *.
- rewrite lookup_app_l; [done|]. by eapply lookup_lt_Some.
- rewrite lookup_app_r; [|lia]. rewrite (_ : (∀ x a b, x + a + b - x = a + b)); [|lia].
apply bind_Some in EMO_LOOKUP as [es [GEN ES_i']].
apply list_lookup_fmap_inv in GEN as ([? ?] & [= <-] & GEN).
apply list_lookup_fmap_inv in GEN as ([[? ?] [? ?]] & [= <- <-] & GEN).
apply lookup_lt_Some in GEN as LEN.
rewrite -{2}(take_drop gen' emo). rewrite fmap_app concat_app.
rewrite lookup_app_r; [|lia]. rewrite (_ : ∀ x a, x + a - x = a); [|lia].
rewrite (drop_S _ _ _ GEN). rewrite fmap_cons concat_cons /=.
apply lookup_lt_Some in ES_i' as LEN'. rewrite lookup_app_l; [done|lia].
- rewrite lookup_app_r; [|lia]. rewrite (_ : (∀ x a, x + a - x = a)); [|lia].
apply list_lookup_fmap_inv in EMO_LOOKUP as ([? ?] & [= <-] & GEN).
apply list_lookup_fmap_inv in GEN as ([[? ?] [? ?]] & [= <- <-] & GEN).
apply lookup_lt_Some in GEN as LEN.
rewrite -{2}(take_drop gen' emo). rewrite fmap_app concat_app.
rewrite lookup_app_r; [|lia]. rewrite (_ : ∀ x, x - x = 0); [|lia].
rewrite (drop_S _ _ _ GEN) //=.
Qed.
Lemma event_in_esi_emo (E : history) esi emo e
(LIN_PERM : lin_of_emo esi emo ≡ₚ seq 0 (length E))
(IS : is_Some (E !! e)) :
∃ eidx, lookup_emo esi emo eidx = Some e.
Proof.
have NODUP : NoDup (lin_of_emo esi emo) by by eapply ord_nodup.
move: IS => /lookup_lt_is_Some /lookup_xo => XO_LOOKUP.
apply Permutation_sym,Permutation_inj in LIN_PERM as [_ (f & _ & LIN_PERM)].
specialize (LIN_PERM e). rewrite LIN_PERM in XO_LOOKUP.
specialize (lin_of_emo_lookup_lookup_emo _ _ _ _ NODUP XO_LOOKUP). i. des.
by exists eidx.
Qed.
Lemma longer_take i1 i2 (lss : list (list event_id)) (LE: i1 ≤ i2):
length (concat (take i1 lss))
≤ length (concat (take i2 lss)).
Proof.
apply prefix_length.
revert lss i2 LE. induction i1; intros; simpl.
{ apply prefix_nil. }
destruct i2; first lia. destruct lss; first done; simpl.
apply prefix_app, IHi1; lia.
Qed.
Lemma lin_idx_e_i'_mono esi emo gen i1' i2' (LEi' : i1' ≤ i2') :
emo_idx_to_lin_idx esi emo (emo_e gen i1') ≤ emo_idx_to_lin_idx esi emo (emo_e gen i2').
Proof.
unfold emo_idx_to_lin_idx.
destruct gen; lia.
Qed.
Lemma lin_idx_ne_gen_mono esi emo gen1' gen2' (LEgen : gen1' ≤ gen2') :
emo_idx_to_lin_idx esi emo (emo_ne gen1') ≤ emo_idx_to_lin_idx esi emo (emo_ne gen2').
Proof.
simpl. do 2 rewrite fmap_take.
by apply Nat.add_le_mono_l, longer_take.
Qed.
Lemma lin_idx_e_ne_gen_mono esi emo gen1 i1' gen2'
(EMO_LOOKUP1 : is_Some (lookup_emo esi emo (emo_e gen1 i1')))
(LTgen : gen1 < S gen2') :
emo_idx_to_lin_idx esi emo (emo_e gen1 i1') < emo_idx_to_lin_idx esi emo (emo_ne gen2').
Proof.
inversion EMO_LOOKUP1 as [e LOOKUP].
apply lookup_emo_inv_e in LOOKUP as [es [LOOKUP1 LOOKUP2]].
apply lookup_lt_Some in LOOKUP2; clear e.
eapply Nat.lt_le_trans; last first.
{ apply lin_idx_ne_gen_mono. instantiate (1 := gen1); lia. }
destruct gen1; simpl.
{ inversion LOOKUP1; lia. }
rewrite <- Nat.add_assoc. apply Nat.add_lt_mono_l.
do 2 rewrite fmap_take.
simpl in LOOKUP1.
do 2 rewrite list_lookup_fmap in LOOKUP1.
destruct (emo !! gen1) eqn:EG; last inversion LOOKUP1.
destruct p as [[ep esp] rest]. inversion LOOKUP1; subst.
remember (λ '(y, _), let '(e, es0) := y in e :: es0) as f.
assert ((f <$> emo) !! gen1 = Some (ep::es)).
- rewrite list_lookup_fmap. rewrite EG.
by list_simplifier.
- apply take_S_r in H. rewrite H.
rewrite concat_app /= app_nil_r app_length cons_length. lia.
Qed.
Lemma lin_idx_ne_e_gen_mono esi emo gen1' gen2 i2' (LEgen : S gen1' ≤ gen2) :
emo_idx_to_lin_idx esi emo (emo_ne gen1') < emo_idx_to_lin_idx esi emo (emo_e gen2 i2').
Proof.
destruct gen2; first lia; simpl.
apply le_S_n in LEgen.
rewrite <- Nat.add_assoc. apply Nat.add_lt_mono_l.
do 2 rewrite fmap_take.
eapply Nat.le_lt_trans.
- apply longer_take, LEgen.
- lia.
Qed.
Lemma lin_idx_e_gen_mono esi emo gen1 gen2 i1' i2'
(EMO_LOOKUP1 : is_Some (lookup_emo esi emo (emo_e gen1 i1')))
(LTgen : gen1 < gen2) :
emo_idx_to_lin_idx esi emo (emo_e gen1 i1') < emo_idx_to_lin_idx esi emo (emo_e gen2 i2').
Proof.
destruct gen2; first lia.
eapply Nat.lt_le_trans.
- by apply lin_idx_e_ne_gen_mono.
- simpl; lia.
Qed.
Lemma not_empty_pop_lin E emo esi
(ESI_GOOD: esi_good E esi)
(GEN_GOOD: Forall
(λ '(y, y0), let '(e, es) := y in let '(on, V) := y0 in
emo_gen_good E e es on V) emo) :
filter (not_empty_pop E) (lin_of_emo esi emo) = emo.*1.*1.
Proof.
induction emo using rev_ind; intros; simpl.
- destruct ESI_GOOD. clear -EMPPOPS.
unfold lin_of_emo; list_simplifier.
induction esi; [by rewrite filter_nil|].
rewrite filter_cons.
apply Forall_cons in EMPPOPS as [[eV [Ha EMP1]] EMP2].
apply IHesi in EMP2; rewrite EMP2.
destruct (decide (not_empty_pop E a)); auto.
edestruct n; [apply Ha|done].
- destruct x as [[e es] [on V]].
do 2 rewrite fmap_app; simpl.
rewrite lin_of_emo_add_emo_ne. rewrite filter_app.
apply Forall_app in GEN_GOOD as [GENE GENX].
apply IHemo in GENE; rewrite GENE.
list_simplifier. destruct H.
clear -EV NOT_EMPPOP EMPPOPS.
rewrite filter_cons_True; last first.
{ intros ??. rewrite EV in EV0. by injection EV0 as ->. }
clear -EMPPOPS.
assert (filter (not_empty_pop E) es = []); last by rewrite H.
induction es; [apply filter_nil|].
apply Forall_cons in EMPPOPS as [[eV [Ea [EMP1 _]]] EMP2].
apply IHes in EMP2. rewrite filter_cons.
destruct (decide (not_empty_pop E a)); last done.
edestruct n; [apply Ea|done].
Qed.
Lemma emo_ids_le_new (E : history) esi emo eidx e
(EMO_LOOKUP : lookup_emo esi emo eidx = Some e)
(ESI_EMO_GOOD : esi_emo_good E esi emo) :
(e < length E)%nat.
Proof.
destruct ESI_EMO_GOOD. destruct ESI_GOOD.
have ? : Forall (λ e, e < length E)%nat (seq 0 (length E)).
{ apply Forall_seq. lia. }
destruct eidx as [[|gen'] i'|gen']; simplify_list_eq.
- have LE : Forall (λ e, e < length E)%nat esi by apply: Forall_sublist.
move: LE. rewrite Forall_lookup => /(_ _ _ EMO_LOOKUP). lia.
- apply bind_Some in EMO_LOOKUP as (es & GEN & ES_i').
apply list_lookup_fmap_inv in GEN as ([? ?] & [= <-] & GEN).
apply list_lookup_fmap_inv in GEN as ([[? ?] [? ?]] & [= <- <-] & GEN).
rewrite ->Forall_lookup in GEN_GOOD. destruct (GEN_GOOD _ _ GEN).
have LE : Forall (λ e, e < length E)%nat es by apply: Forall_sublist.
move: LE. rewrite Forall_lookup => /(_ _ _ ES_i'). lia.
- have LE : Forall (λ e, e < length E)%nat (write_xo E) by apply: Forall_filter.
rewrite XO_AGREE in EMO_LOOKUP.
move: LE. rewrite Forall_lookup => /(_ _ _ EMO_LOOKUP). lia.
Qed.
Lemma lookup_emo_old_esi esi emo eidx e e0
(EMO_LOOKUP : lookup_emo (esi ++ [e0]) emo eidx = Some e)
(NEedix : eidx ≠ emo_e O (length esi)) :
lookup_emo esi emo eidx = Some e.
Proof.
destruct eidx as [[|gen'] i'|gen']; simplify_list_eq; [|done|done].
have LEi' : (i' < length esi)%nat.
{ apply lookup_lt_Some in EMO_LOOKUP. rewrite app_length /= in EMO_LOOKUP.
have ? : i' ≠ length esi by intros ->. lia. }
by rewrite lookup_app_l in EMO_LOOKUP.
Qed.
Lemma emo_insert_e_11 emo gen' e :
(emo_insert_e emo gen' e).*1.*1 = emo.*1.*1.
Proof.
rewrite /emo_insert_e.
rewrite (list_alter_fmap fst
(λ '(e', es', msg'), (e', es' ++ [e], msg'))
(λ '(e', es'), (e', es' ++ [e]))
emo gen'); last first.
{ induction emo; [done|]. constructor; [|done]. destruct a. destruct p. by simpl. }
rewrite (list_alter_fmap fst
(λ '(e', es'), (e', es' ++ [e]))
id
emo.*1 gen').
{ by rewrite list_alter_id. }
induction emo.*1; [done|]. constructor; [|done]. destruct a. by simpl.
Qed.
Lemma emo_insert_e_12 emo gen' e :
(emo_insert_e emo gen' e).*1.*2 = alter (λ es' : list event_id, es' ++ [e]) gen' emo.*1.*2.
Proof.
rewrite /emo_insert_e.
rewrite (list_alter_fmap fst
(λ '(e', es', msg'), (e', es' ++ [e], msg'))
(λ '(e', es'), (e', es' ++ [e]))
emo gen'); last first.
{ induction emo; [done|]. constructor; [|done]. destruct a. destruct p. by simpl. }
rewrite (list_alter_fmap snd
(λ '(e', es'), (e', es' ++ [e]))
(λ es', es' ++ [e])
emo.*1 gen'); [done|].
induction emo.*1; [done|]. constructor; [|done]. destruct a. by simpl.
Qed.
Lemma emo_insert_e_2 emo gen' e :
(emo_insert_e emo gen' e).*2 = emo.*2.
Proof.
rewrite /emo_insert_e.
rewrite (list_alter_fmap snd
(λ '(e', es', msg'), (e', es' ++ [e], msg'))
id
emo gen'); [by rewrite list_alter_id|].
induction emo; [done|]. constructor; [|done]. destruct a. destruct p. by simpl.
Qed.
Lemma lookup_emo_old_emo_e esi emo eidx gen0' es e e0
(EMO_LOOKUP : lookup_emo esi (emo_insert_e emo gen0' e0) eidx = Some e)
(GEN0 : emo.*1.*2 !! gen0' = Some es)
(NEedix : eidx ≠ emo_e (S gen0') (length es)) :
lookup_emo esi emo eidx = Some e.
Proof.
destruct eidx as [[|gen'] i'|gen']; simplify_list_eq; [done|..];
case (decide (gen' = gen0')) as [->|NE].
- rewrite emo_insert_e_12 in EMO_LOOKUP.
rewrite list_lookup_alter GEN0 /= in EMO_LOOKUP.
have ? : i' ≠ length es by intros ->.
rewrite lookup_app_1_ne in EMO_LOOKUP; [|done].
apply bind_Some. by exists es.
- rewrite emo_insert_e_12 in EMO_LOOKUP.
rewrite list_lookup_alter_ne in EMO_LOOKUP; done.
- by rewrite emo_insert_e_11 in EMO_LOOKUP.
- by rewrite emo_insert_e_11 in EMO_LOOKUP.
Qed.
Lemma lookup_emo_old_emo_ne esi emo eidx e0 e msg0
(EMO_LOOKUP : lookup_emo esi (emo ++ [(e0, [], msg0)]) eidx = Some e)
(NEedix : eidx ≠ emo_ne (length emo)) :
lookup_emo esi emo eidx = Some e.
Proof.
rewrite -(fmap_length fst) -(fmap_length snd) in NEedix.
destruct eidx as [[|gen'] i'|gen']; simplify_list_eq; [done|..];
case (decide (gen' = (length emo.*1.*2))) as [->|NE].
- rewrite lookup_app_1_eq /= in EMO_LOOKUP. done.
- rewrite lookup_app_1_ne in EMO_LOOKUP; done.
- rewrite fmap_length -(fmap_length fst) in EMO_LOOKUP. simplify_eq.
- rewrite fmap_length -(fmap_length fst) in NE.
rewrite lookup_app_1_ne in EMO_LOOKUP; done.
Qed.
Lemma lookup_emo_new_ne (E : history) esi emo eidx (e := length E) msg
(EMO_LOOKUP : lookup_emo esi (emo ++ [(e, [], msg)]) eidx = Some e)
(ESI_EMO_GOOD : esi_emo_good E esi emo) :
eidx = emo_ne (length emo).
Proof.
case (decide (eidx = emo_ne (length emo))) as [->|NE]; [done|]. exfalso.
apply lookup_emo_old_emo_ne in EMO_LOOKUP; [|done].
eapply (emo_ids_le_new E) in EMO_LOOKUP; [lia|done].
Qed.
Lemma sub_esi_emo_lookup esi1 esi2 emo1 emo2 eidx e
(Subesi : esi1 ⊑ esi2)
(Subne : emo1.*1.*1 ⊑ emo2.*1.*1)
(Subess : emo1.*1.*2 `prefixes_of` emo2.*1.*2) :
lookup_emo esi1 emo1 eidx = Some e → lookup_emo esi2 emo2 eidx = Some e.
Proof.
case eidx as [[|gen'] i'|gen'] => /= EMO_LOOKUP.
- by eapply prefix_lookup_Some.
- apply bind_Some in EMO_LOOKUP as (es1 & GEN1 & ES1_i').
have [es2 GEN2] : ∃ es2, emo2.*1.*2 !! gen' = Some es2.
{ clear -Subne GEN1.
have [e {}GEN1] : ∃ e, emo1.*1 !! gen' = Some (e, es1).
{ apply list_lookup_fmap_inv in GEN1 as ([e ?] & [= <-] & GEN). by exists e. }
apply (f_equal (fmap fst)) in GEN1. rewrite -list_lookup_fmap /= in GEN1.
have GEN2 := prefix_lookup_Some _ _ _ _ GEN1 Subne.
have [es2 {}GEN2] : ∃ es2, emo2.*1 !! gen' = Some (e, es2).
{ apply list_lookup_fmap_inv in GEN2 as ([? es] & [= <-] & GEN). by exists es. }
apply (f_equal (fmap snd)) in GEN2. rewrite -list_lookup_fmap /= in GEN2.
by exists es2. }
specialize (Subess gen'). rewrite GEN1 GEN2 /= in Subess.
have ES2_i' := prefix_lookup_Some _ _ _ _ ES1_i' Subess.
apply bind_Some. by exists es2.
- by eapply prefix_lookup_Some.
Qed.
Lemma emo_insert_e_12_prefixes emo gen' e :
emo.*1.*2 `prefixes_of` (emo_insert_e emo gen' e).*1.*2.
Proof.
rewrite emo_insert_e_12. intros i.
case (decide (i = gen')) as [EQ|NE]; case Ei: (emo.*1.*2 !! i); simplify_eq.
+ rewrite list_lookup_alter Ei /=. by apply prefix_app_r.
+ rewrite list_lookup_alter Ei /=. done.
+ rewrite list_lookup_alter_ne; [|done]. rewrite Ei //=.
+ rewrite list_lookup_alter_ne; [|done]. rewrite Ei //=.
Qed.
Lemma emo_insert_ne_12_prefixes emo e msg :
emo.*1.*2 `prefixes_of` (emo ++ [(e, [], msg)]).*1.*2.
Proof.
rewrite !fmap_app /=. intros i.
case Ei: (emo.*1.*2 !! i); simplify_eq.
+ apply lookup_lt_Some in Ei as Hi. rewrite lookup_app_l; [|done]. by rewrite Ei /=.
+ simpl. by case_match.
Qed.
Lemma lin_perm_emo_inj (E : history) esi emo
(LIN_PERM : lin_of_emo esi emo ≡ₚ seq 0 (length E)) :
emo_inj esi emo.
Proof.
intros ?????.
(* another way: Using [NoDup (lin_of_emo _ _)]... Is that easier? *)
apply lookup_emo_lin_of_emo_lookup in EMO_LOOKUP1 as LIN1, EMO_LOOKUP2 as LIN2.
apply Permutation_inj in LIN_PERM as [_ (f & INJ_f & LIN_PERM)].
rewrite ->LIN_PERM in LIN1,LIN2.
apply lookup_lt_Some in LIN1 as LT1, LIN2 as LT2.
rewrite seq_length in LT1,LT2.
rewrite ->lookup_xo in LIN1,LIN2; [|done..].
rewrite -LIN1 in LIN2. injection LIN2 as EQ%(inj f).
by eapply emo_idx_to_lin_idx_inj in EQ.
Qed.
Lemma emo_inj_esi_insert E esi emo
(INJ : emo_inj esi emo)
(ESI_EMO_GOOD : esi_emo_good E esi emo) :
emo_inj (esi ++ [length E]) emo.
Proof.
intros ???.
case (decide (eidx1 = emo_e O (length esi))) => [->|NE1]; simpl;
[rewrite lookup_app_1_eq; intros [= <-]
|intros HL1%lookup_emo_old_esi; [|done]];
(case (decide (eidx2 = emo_e O (length esi))) => [->|NE2]; simpl;
[rewrite lookup_app_1_eq; (intros [= <-] || intros _)
|intros HL2%lookup_emo_old_esi; [|done]]).
- done.
- exfalso. eapply (emo_ids_le_new E) in HL2; [lia|done].
- exfalso. eapply (emo_ids_le_new E) in HL1; [lia|done].
- by eapply INJ.
Qed.
Lemma emo_inj_emo_insert_e E esi emo gen' o es msg
(INJ : emo_inj esi emo)
(GEN : emo !! gen' = Some (o, es, msg))
(ESI_EMO_GOOD : esi_emo_good E esi emo) :
emo_inj esi (emo_insert_e emo gen' (length E)).
Proof.
intros ???.
have GEN12 : emo.*1.*2 !! gen' = Some es.
{ apply (f_equal (fmap fst)), (f_equal (fmap snd)) in GEN.
rewrite -!list_lookup_fmap // in GEN. }
case (decide (eidx1 = emo_e (S gen') (length es))) => [->|NE1]; simpl;
[rewrite emo_insert_e_12 list_lookup_alter GEN12 /= lookup_app_1_eq; intros [= <-]
|intros HL1%(lookup_emo_old_emo_e _ _ _ _ es); [|done..]];
(case (decide (eidx2 = emo_e (S gen') (length es))) => [->|NE2]; simpl;
[rewrite emo_insert_e_12 list_lookup_alter GEN12 /= lookup_app_1_eq; (intros [= <-] || intros _)
|intros HL2%(lookup_emo_old_emo_e _ _ _ _ es); [|done..]]).
- done.
- exfalso. eapply (emo_ids_le_new E) in HL2; [lia|done].
- exfalso. eapply (emo_ids_le_new E) in HL1; [lia|done].
- by eapply INJ.
Qed.
Lemma emo_inj_emo_insert_ne E esi emo msg
(INJ : emo_inj esi emo)
(ESI_EMO_GOOD : esi_emo_good E esi emo) :
emo_inj esi (emo ++ [(length E, [], msg)]).
Proof.
intros ???.
case (decide (eidx1 = emo_ne (length emo.*1.*1))) => [->|NE1]; simpl;
[rewrite !fmap_app /= lookup_app_1_eq; intros [= <-]
|rewrite !fmap_length in NE1; intros HL1%lookup_emo_old_emo_ne; [|done]];
(case (decide (eidx2 = emo_ne (length emo.*1.*1))) => [->|NE2]; simpl;
[rewrite !fmap_app /= lookup_app_1_eq; (intros [= <-] || intros _)
|rewrite !fmap_length in NE2; intros HL2%lookup_emo_old_emo_ne; [|done]]).
- done.
- exfalso. eapply (emo_ids_le_new E) in HL2; [lia|done].
- exfalso. eapply (emo_ids_le_new E) in HL1; [lia|done].
- by eapply INJ.
Qed.
Lemma max_gen esi emo eidx e
(EMO_LOOKUP : lookup_emo esi emo eidx = Some e) :
gen_of eidx ≤ length emo.
Proof.
destruct_eidx eidx EMO_LOOKUP as gen' i' e' es msg GEN ES_i' LEgen' LEi'; lia.
Qed.
Lemma esi_good_mono E1 E2 esi (ESI_GOOD : esi_good E1 esi) (SubE : E1 ⊑ E2) :
esi_good E2 esi.
Proof.
destruct ESI_GOOD. rewrite ->Forall_lookup in EMPPOPS.
constructor.
- rewrite Forall_lookup => i' e ES_i'.
specialize (EMPPOPS _ _ ES_i') as [eV [EV' ?]].
exists eV. split; [|done]. by eapply prefix_lookup_Some.
- destruct SubE as [E ->]. rewrite app_length /= seq_app.
apply sublist_inserts_r, EMPPOP_XO_SUB.
Qed.
Lemma emo_gen_good_mono E1 E2 e es on V
(GEN_GOOD : emo_gen_good E1 e es on V)
(SubE : E1 ⊑ E2) :
emo_gen_good E2 e es on V.
Proof.
destruct GEN_GOOD.
apply lookup_lt_Some in EV as LT.
rewrite (write_xo_stable _ E2) in EMPPOP_EMPTY EMPTYING_POP; [|lia|done].
econstructor.
- by eapply prefix_lookup_Some.
- done.
- done.
- rewrite ->Forall_lookup in EMPPOPS |- *. intros ? ? ES_i'.
specialize (EMPPOPS _ _ ES_i'). des.
exists eV'. unfold NW. split_and!; [by eapply prefix_lookup_Some|done..].
- intros ES. specialize (EMPPOP_EMPTY ES). unnw.
by apply (stack_interp_mono_prefix E1).
- split; last first.
{ move=> /EMPTYING_POP; by apply stack_interp_mono_prefix. }
intros INTERP. apply EMPTYING_POP.
apply (stack_interp_mono E2); [|done].
rewrite Forall_lookup => i e' In eV' EV2'.
have ? : e' ≤ e.
{ rewrite lookup_app in In. case_match eqn:Heqo; simplify_list_eq; [|lia].
apply list_filter_lookup_Some_inv in Heqo as [? [In _]].
apply lookup_seq in In. lia. }
apply (prefix_lookup_inv E1 E2); [done| |done].
apply lookup_lt_is_Some_2. lia.
- destruct SubE as [E ->]. rewrite app_length seq_app.
apply sublist_inserts_r, EMPPOP_XO_SUB.
Qed.
Lemma stack_interp_esi E esi
(EMPPOPS : Forall (λ e', ∃ eV',
E !! e' = Some eV' ∧ eV'.(ge_event) = EmpPop) esi) :
stack_interp E esi [] [].
Proof.
induction esi using rev_ind; first constructor.
apply Forall_app in EMPPOPS; des.
apply IHesi in EMPPOPS.
apply stack_interp_app; exists []; split; auto.
rewrite -> Forall_singleton in EMPPOPS0; des.
apply (stack_interp_snoc _ x eV' _ [] _ [] _); auto.
- constructor.
- apply (stack_run_EmpPop _ _ []); auto.
Qed.
Lemma stack_interp_esi_nil E esi emo stk
(EMPPOPS : Forall (λ e', ∃ eV',
E !! e' = Some eV' ∧ eV'.(ge_event) = EmpPop) esi) :
stack_interp E (lin_of_emo esi emo) [] stk
↔ stack_interp E (lin_of_emo [] emo) [] stk.
Proof.
split; intros.
- rewrite lin_of_emo_split_esi in H.
apply stack_interp_app in H; des.
eapply (stack_interp_functional _ _ _ [] _) in H;
[by subst|by apply stack_interp_esi].
- rewrite lin_of_emo_split_esi.
apply stack_interp_app.
exists []; split;
[by apply stack_interp_esi|auto].
Qed.
Lemma lin_of_emo_insert_e esi emo g e
(Hg : g < length emo) :
lin_of_emo esi (emo_insert_e emo g e) =
lin_of_emo esi (take (S g) emo) ++
[e] ++ lin_of_emo [] (drop (S g) emo).
Proof.
apply lookup_lt_is_Some_2 in Hg.
inv Hg.
unfold emo_insert_e. rewrite (alter_take_drop _ _ _ x); auto.
rewrite (take_S_r _ _ x); auto.
do 3 rewrite lin_of_emo_app. rewrite <- app_assoc.
destruct x as [[e' es'] msg'].
unfold lin_of_emo; by list_simplifier.
Qed.
Lemma lin_perm_add_esi esi emo e
(LIN_PERM: lin_of_emo esi emo ≡ₚ seq 0 e):
lin_of_emo (esi ++ [e]) emo ≡ₚ seq 0 (e + 1).
Proof.
rewrite seq_app /=. rewrite lin_of_emo_split_esi.
rewrite -(assoc app esi) (comm app [e]) (assoc app esi).
by apply Permutation_app.
Qed.
Lemma lin_perm_add_emo_ne esi emo e msg
(LIN_PERM: lin_of_emo esi emo ≡ₚ seq 0 e):
lin_of_emo esi (emo ++ [(e, [], msg)]) ≡ₚ seq 0 (e + 1).
Proof.
rewrite seq_app /=. rewrite lin_of_emo_add_emo_ne. by apply Permutation_app.
Qed.
Lemma lin_perm_add_emo_e esi emo gen' x e
(GEN : emo !! gen' = Some x)
(LIN_PERM: lin_of_emo esi emo ≡ₚ seq 0 e) :
lin_of_emo esi (emo_insert_e emo gen' e) ≡ₚ seq 0 (e + 1).
Proof.
rewrite lin_of_emo_insert_e; [|by apply lookup_lt_Some in GEN].
rewrite (comm app [e]) (assoc app _ _ [e]).
rewrite seq_app /=. apply Permutation_app; [|done].
by rewrite -lin_of_emo_app take_drop.
Qed.
Lemma hb_emo_add_ne E esi emo msg eV
(ESI_EMO_GOOD : esi_emo_good E esi emo)
(EWF : history_wf E)
(HB_EMO : hb_emo E esi emo) :
hb_emo (E ++ [eV]) esi (emo ++ [(length E, [], msg)]).
Proof.
unfold hb_emo. intros.
set gen' := length emo.*1.*1.
set eidx := emo_ne gen'.
case (decide (eidx = eidx1)) as [<-|NE1]; case (decide (eidx = eidx2)) as [<-|NE2].
-- (* this ∈ this.lview *)
by apply emo_idx_le_ne_ne.
-- (* this ∈ old.lview: old can't contain this *)
exfalso. rewrite /= !fmap_app /= in EMO_eidx1.
apply lookup_last_Some_2 in EMO_eidx1 as ->.
eapply lookup_emo_old_emo_ne in EMO_eidx2;
[|subst eidx gen'; rewrite !fmap_length in NE2; done].
exploit (emo_ids_le_new E esi emo eidx2 e2); [done..|]. intro.
rewrite lookup_app_l in EV2; [|done].
move: (hwf_logview_closed _ EWF _ _ EV2 _ IN_LVIEW) => /lookup_lt_is_Some. lia.
-- (* old ∈ this.lview *)
rewrite /= !fmap_app /= in EMO_eidx2.
apply lookup_last_Some_2 in EMO_eidx2 as ->.
apply lookup_last_Some_2 in EV2 as ->.
eapply lookup_emo_old_emo_ne in EMO_eidx1;
[|subst eidx gen'; rewrite !fmap_length in NE1; done].
have MAXgen : (gen_of eidx1 ≤ gen')%nat.
{ subst gen'. rewrite !fmap_length. by eapply max_gen. }
destruct eidx1 as [gen1 i1'|gen1']; intros; simpl in *.
++ apply emo_idx_le_e_ne_1. lia.
++ apply emo_idx_le_ne_ne. lia.
-- (* old ∈ old.lview *)
eapply lookup_emo_old_emo_ne in EMO_eidx1,EMO_eidx2;
[|subst eidx gen'; rewrite !fmap_length in NE1,NE2; done..].
exploit (emo_ids_le_new E esi emo eidx2 e2); [done..|]. intros.
rewrite lookup_app_l in EV2; [|done].
by eapply (HB_EMO eidx1 eidx2).
Qed.
Lemma xo_agree_cut E emo i x
(XO_AGREE : emo.*1.*1 = write_xo E)
(LOOKUP : emo !! i = Some x) :
take i (write_xo E) = filter (not_empty_pop E) (seq 0 x.1.1).
Proof.
assert (emo.*1.*1 !! i = Some x.1.1).
{ by rewrite !list_lookup_fmap LOOKUP. }
rewrite XO_AGREE in H. by apply take_filter_seq.
Qed.
End emo_lemmas.
Section emo_ghost.
Definition esiR : cmra := mono_listR (leibnizO event_id).
Definition emo_neR : cmra := mono_listR (leibnizO event_id).
Definition emo_eR : cmra := mono_list_listR (leibnizO event_id).
Definition emoR : cmra := prodR (prodR esiR emo_neR) emo_eR.
Definition emo_auth esi emo : emoR :=
( ●ML (esi : listO (leibnizO _)),
●ML (emo.*1.*1 : listO (leibnizO _)),
●MLL (emo.*1.*2 : listO (listO (leibnizO _)))).
Definition emo_lb esi emo : emoR :=
( ◯ML (esi : listO (leibnizO _)),
◯ML (emo.*1.*1 : listO (leibnizO _)),
◯MLL (emo.*1.*2 : listO (listO (leibnizO _)))).
Section rules.
Context `{!inG Σ emoR}.
Lemma emo_auth_lb_valid γ esi1 esi2 emo1 emo2 :
own γ (emo_auth esi1 emo1) -∗
own γ (emo_lb esi2 emo2) -∗
⌜ esi2 `prefix_of` esi1 ∧
emo2.*1.*1 `prefix_of` emo1.*1.*1 ∧
emo2.*1.*2 `prefixes_of` emo1.*1.*2 ⌝.
Proof.
iIntros "Hauth Hlb".
by iDestruct (own_valid_2 with "Hauth Hlb")
as %[[?%mono_list_both_valid_L ?%mono_list_both_valid_L] ?%mono_list_list_both_valid_L].
Qed.
Lemma emo_lb_own_get γ esi emo :
own γ (emo_auth esi emo) -∗ own γ (emo_lb esi emo).
Proof.
intros. apply own_mono. rewrite !prod_included. simpl. split_and!.
- apply mono_list_included.
- apply mono_list_included.
- apply mono_list_list_included.
Qed.
Lemma emo_own_alloc esi emo :
⊢ |==> ∃ γ, own γ (emo_auth esi emo) ∗ own γ (emo_lb esi emo).
Proof.
setoid_rewrite <- own_op. apply own_alloc. rewrite !pair_valid /=. split_and!.
- apply mono_list_both_valid. exists []. by rewrite app_nil_r.
- apply mono_list_both_valid. exists []. by rewrite app_nil_r.
- apply mono_list_list_both_valid. intros i.
case Eess: (emo.*1.*2 !! i); simpl; [|done].
exists []. by rewrite app_nil_r.
Qed.
Lemma emo_auth_own_update {γ esi emo} esi' emo' :
esi `prefix_of` esi' →
emo.*1.*1 `prefix_of` emo'.*1.*1 →
emo.*1.*2 `prefixes_of` emo'.*1.*2 →
own γ (emo_auth esi emo) ==∗ own γ (emo_auth esi' emo') ∗ own γ (emo_lb esi' emo').
Proof.
iIntros (???) "Hauth".
iAssert (own γ (emo_auth esi' emo')) with "[> Hauth]" as "Hauth".
{ iApply (own_update with "Hauth"). apply prod_update; [apply prod_update|].
- by apply mono_list_update.
- by apply mono_list_update.