forked from PrincetonUniversity/VST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetSem.v
2338 lines (1893 loc) · 52.7 KB
/
DetSem.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
(* Copyright 2012-2015 by Adam Petcher. *
* Use of this source code is governed by the license described *
* in the LICENSE file at the root of the source tree. *)
(* An oracle machine semantics for computations. *)
Set Implicit Arguments.
Require Export fcf.Comp.
Require Import fcf.Blist.
Require Import fcf.Fold.
Require Import Permutation.
Require Import Omega.
Local Open Scope list_scope.
Local Open Scope comp_scope.
(* A type for keeping track of the state of the evaluation. *)
Inductive comp_state(A : Set) :=
| cs_done : A -> Blist -> comp_state A
| cs_eof : comp_state A
| cs_more : Comp A -> Blist -> comp_state A.
(* a type for returning answers *)
Inductive comp_answer(A : Set) :=
| ca_done : A -> comp_answer A
| ca_eof : comp_answer A.
Lemma comp_answer_eq_dec : forall (A : Set),
eq_dec A ->
eq_dec (comp_answer A).
intuition.
unfold eq_dec in *.
intuition.
destruct a1; destruct a2.
destruct (H a a0); subst.
left.
trivial.
right.
intuition.
inversion H0; clear H0; subst.
intuition.
right; intuition; discriminate.
right; intuition; discriminate.
left.
trivial.
Qed.
(* a functional form of the small-step semantics*)
Fixpoint evalDet_step(A : Set)(c : Comp A)(s : Blist) : comp_state A :=
match c in Comp A return comp_state A with
| Ret pf a => cs_done a s
| Rnd n =>
match (shiftOut s n) with
| Some (v, s') => cs_more (Ret (@Bvector_eq_dec n) v) s'
| None => (@cs_eof (Bvector n))
end
| Bind c1 c2 =>
match (evalDet_step c1 s) with
| cs_eof _ => (@cs_eof _)
| cs_done b s' => cs_more (c2 b) s'
| cs_more c1' s' => cs_more (Bind c1' c2) s'
end
| Repeat c P =>
cs_more (Bind c (fun a => if (P a) then (Ret (comp_eq_dec c) a) else (Repeat c P))) s
end.
Inductive evalDet_steps(A : Set) : comp_state A -> comp_state A -> Prop :=
| evalDet_steps_refl : forall ans,
evalDet_steps ans ans
| evalDet_steps_step :
forall c s ans ans',
(evalDet_step c s) = ans ->
evalDet_steps ans ans' ->
evalDet_steps (cs_more c s) ans'.
Hint Constructors evalDet_steps : evalDet.
Inductive evalDet(A : Set)(c : Comp A)(s : Blist) : comp_answer A -> Prop :=
| evalDet_done : forall a s',
evalDet_steps (cs_more c s) (cs_done a s') ->
evalDet c s (ca_done a)
| evalDet_eof :
evalDet_steps (cs_more c s) (@cs_eof A) ->
evalDet c s (@ca_eof A).
Theorem evalDet_steps_trans : forall (A : Set)(x y : comp_state A),
evalDet_steps x y ->
forall z,
evalDet_steps y z ->
evalDet_steps x z.
induction 1; intuition; subst.
econstructor.
trivial.
eapply IHevalDet_steps.
trivial.
Qed.
Theorem evalDet_steps_bind_more_h : forall(A B : Set) x y,
evalDet_steps x y ->
forall (c1 : Comp B)(c2 : B -> Comp A) s c1' s',
x = (cs_more c1 s) ->
y = (cs_more c1' s') ->
evalDet_steps (cs_more (Bind c1 c2) s) (cs_more (Bind c1' c2) s').
induction 1; intuition; subst.
Ltac evalDet_tac1 :=
match goal with
| [H : cs_more _ _ = cs_more _ _ |- _ ] => inversion H; clear H; subst
| [|- evalDet_steps ?c ?c ] => econstructor
| [H : evalDet_steps (evalDet_step ?c ?s) _ |- evalDet_steps (cs_more (Bind ?c _) ?s) _ ] => inversion H; clear H; subst
| [H : (evalDet_step ?c ?s) = _ |- evalDet_steps (cs_more (Bind ?c _) ?s) _ ] => econstructor; simpl
| [|- context[match ?x with | cs_done _ _ => _ | cs_eof _ => _ | cs_more _ _ => _ end] ] => case_eq x; intuition
end.
repeat evalDet_tac1.
inversion H0; clear H0; subst.
inversion H1; clear H1; subst.
econstructor.
eauto.
simpl.
rewrite H3.
econstructor.
inversion H1; clear H1; subst.
econstructor.
eauto.
simpl.
rewrite <- H.
eapply IHevalDet_steps.
auto.
trivial.
Qed.
Theorem evalDet_steps_bind_more : forall(A B : Set)(c1 : Comp B)(c2 : B -> Comp A) s c1' s',
evalDet_steps (cs_more c1 s) (cs_more c1' s') ->
evalDet_steps (cs_more (Bind c1 c2) s) (cs_more (Bind c1' c2) s').
intuition.
eapply evalDet_steps_bind_more_h; eauto.
Qed.
Lemma evalDet_steps_done_inv_h : forall (A : Set)(x y : comp_state A),
evalDet_steps x y ->
forall (c : Comp A)(a : A) s s',
x = (cs_more c s) ->
y = (cs_done a s') ->
exists c'' s'', evalDet_steps (cs_more c s) (cs_more c'' s'') /\ evalDet_step c'' s'' = (cs_done a s').
induction 1; intuition; subst.
discriminate.
inversion H1; clear H1; subst.
inversion H0; clear H0; subst.
econstructor. econstructor.
intuition.
destruct (IHevalDet_steps c a s s'); eauto.
destruct H0; intuition.
exists x.
exists x0.
intuition.
symmetry in H.
econstructor.
eapply H.
trivial.
Qed.
Lemma evalDet_steps_done_inv : forall (A : Set)(c : Comp A)(a : A) s s',
evalDet_steps (cs_more c s) (cs_done a s') ->
exists c'' s'', evalDet_steps (cs_more c s) (cs_more c'' s'') /\ evalDet_step c'' s'' = (cs_done a s').
intuition.
eapply evalDet_steps_done_inv_h; eauto.
Qed.
Lemma evalDet_steps_eof_inv_h : forall (A : Set)(x y : comp_state A),
evalDet_steps x y ->
forall (c : Comp A) s,
x = (cs_more c s) ->
y = (@cs_eof A) ->
exists c'' s'', evalDet_steps (cs_more c s) (cs_more c'' s'') /\ evalDet_step c'' s'' = (@cs_eof A).
induction 1; intuition; subst.
discriminate.
inversion H1; clear H1; subst.
inversion H0; clear H0; subst.
econstructor. econstructor.
intuition.
destruct (IHevalDet_steps c s); eauto.
destruct H0; intuition.
exists x.
exists x0.
intuition.
econstructor.
rewrite <- H.
eauto.
rewrite <- H.
trivial.
Qed.
Lemma evalDet_steps_eof_inv : forall (A : Set)(c : Comp A) s,
evalDet_steps (cs_more c s) (@cs_eof A) ->
exists c'' s'', evalDet_steps (cs_more c s) (cs_more c'' s'') /\ evalDet_step c'' s'' = (@cs_eof A).
intuition.
eapply evalDet_steps_eof_inv_h; eauto.
Qed.
Theorem evalDet_steps_bind_done : forall(A B : Set)(c1 : Comp B)(c2 : B -> Comp A) a s s',
evalDet_steps (cs_more c1 s) (cs_done a s') ->
evalDet_steps (cs_more (Bind c1 c2) s) (cs_more (c2 a) s').
intuition.
apply evalDet_steps_done_inv in H.
destruct H.
destruct H.
intuition.
eapply evalDet_steps_trans.
eapply evalDet_steps_bind_more.
eauto.
econstructor.
simpl.
rewrite H1.
eauto.
econstructor.
Qed.
Theorem evalDet_bind_eof : forall(A B : Set)(c1 : Comp B)(c2 : B -> Comp A) s,
evalDet_steps (cs_more c1 s) (@cs_eof B) ->
evalDet_steps (cs_more (Bind c1 c2) s) (@cs_eof A).
intuition.
apply evalDet_steps_eof_inv in H.
destruct H.
destruct H.
intuition.
eapply evalDet_steps_trans.
eapply evalDet_steps_bind_more.
eauto.
econstructor.
simpl.
rewrite H1.
eauto.
econstructor.
Qed.
Inductive comp_state_eq(A : Set) : comp_state A -> comp_state A -> Prop :=
| cse_done :
forall a s,
comp_state_eq (cs_done a s) (cs_done a s)
| cse_eof :
comp_state_eq (@cs_eof A) (@cs_eof A)
| cse_more :
forall c1 c2 s,
Comp_eq c1 c2 ->
comp_state_eq (cs_more c1 s) (cs_more c2 s).
Theorem comp_state_eq_refl : forall (A : Set)(c : comp_state A),
comp_state_eq c c.
destruct c; intuition;
econstructor.
eapply Comp_eq_refl.
Qed.
Lemma evalDet_steps_done_func_h : forall (A : Set)(x : comp_state A) y1,
evalDet_steps x y1 ->
forall a a' s s',
y1 = (cs_done a s) ->
evalDet_steps x (cs_done a' s') ->
(a = a' /\ s = s').
induction 1; intros; subst.
inversion H0; clear H0; subst.
intuition.
inversion H2; clear H2; subst.
eapply IHevalDet_steps;
trivial.
Qed.
Theorem evalDet_steps_done_func : forall (A : Set) x (a a' : A) s s',
evalDet_steps x (cs_done a s) ->
evalDet_steps x (cs_done a' s') ->
(a = a' /\ s = s').
intros.
eapply evalDet_steps_done_func_h.
eapply H.
eauto.
eauto.
Qed.
Lemma evalDet_steps_done_eof_func_h : forall (A : Set)(x : comp_state A) y1,
evalDet_steps x y1 ->
forall a s,
y1 = (cs_done a s) ->
evalDet_steps x (@cs_eof A) ->
False.
induction 1; intros; subst.
inversion H0.
inversion H2; clear H2; subst.
eapply IHevalDet_steps;
trivial.
Qed.
Theorem evalDet_steps_done_eof_func : forall (A : Set) x (a : A) s,
evalDet_steps x (cs_done a s) ->
evalDet_steps x (@cs_eof A) ->
False.
intros.
eapply evalDet_steps_done_eof_func_h.
eapply H.
eauto.
eauto.
Qed.
Theorem evalDet_func : forall (A : Set)(c : Comp A)(s : Blist)(y1 y2 : comp_answer A),
evalDet c s y1 ->
evalDet c s y2 ->
y1 = y2.
intuition.
inversion H; subst; clear H.
inversion H0; subst; clear H0.
f_equal.
eapply evalDet_steps_done_func; eauto.
exfalso.
eapply evalDet_steps_done_eof_func; eauto.
inversion H0; clear H0; subst.
exfalso.
eapply evalDet_steps_done_eof_func; eauto.
trivial.
Qed.
Definition evalDet_equiv(A : Set)(c1 c2 : Comp A) :=
(forall s y, evalDet c1 s y <-> evalDet c2 s y).
Lemma evalDet_equiv_symm : forall (A : Set)(c1 c2 : Comp A),
evalDet_equiv c1 c2 ->
evalDet_equiv c2 c1.
unfold evalDet_equiv. intuition.
eapply H; trivial.
eapply H; trivial.
Qed.
Theorem evalDet_steps_bind_done_inv_h : forall (A : Set)(x y : comp_state A),
evalDet_steps x y ->
forall (B : Set)(c1 : Comp B)(c2 : B -> Comp A) s s' a,
x = (cs_more (Bind c1 c2) s) ->
y = (cs_done a s') ->
exists b s'', evalDet_steps (cs_more c1 s) (cs_done b s'') /\
evalDet_steps (cs_more (c2 b) s'') (cs_done a s').
induction 1; intuition; subst.
discriminate.
inversion H1; clear H1; subst.
inversion H0; clear H0; subst.
case_eq (evalDet_step c1 s0); intuition;
rewrite H in H2; discriminate.
case_eq (evalDet_step c1 s0); intuition;
rewrite H0 in H.
inversion H; clear H; subst.
econstructor. econstructor. intuition.
econstructor.
eapply H0.
econstructor.
econstructor; eauto.
discriminate.
inversion H; clear H; subst.
edestruct IHevalDet_steps.
simpl.
rewrite H0.
eauto.
eauto.
destruct H.
intuition.
econstructor. econstructor. intuition.
econstructor.
eapply H0.
eauto.
trivial.
Qed.
Theorem evalDet_steps_bind_done_inv : forall (A B : Set)(c1 : Comp B)(c2 : B -> Comp A) s s' a,
evalDet_steps (cs_more (Bind c1 c2) s) (cs_done a s') ->
exists b s'', evalDet_steps (cs_more c1 s) (cs_done b s'') /\
evalDet_steps (cs_more (c2 b) s'') (cs_done a s').
intuition.
eapply evalDet_steps_bind_done_inv_h; eauto.
Qed.
Theorem evalDet_steps_bind_eof_inv_h :
forall (A : Set)(x y : comp_state A),
evalDet_steps x y ->
forall (B : Set)(c1 : Comp B)(c2 : B -> Comp A) s,
x = (cs_more (Bind c1 c2) s) ->
y = (@cs_eof A) ->
evalDet_steps (cs_more c1 s) (@cs_eof B) \/
exists b s', evalDet_steps (cs_more c1 s) (cs_done b s') /\
evalDet_steps (cs_more (c2 b) s') (@cs_eof A).
induction 1; intuition; subst.
discriminate.
inversion H1; clear H1; subst.
inversion H0; clear H0; subst.
case_eq (evalDet_step c1 s0); intuition;
rewrite H in H2.
discriminate.
left.
econstructor.
rewrite H.
eauto.
econstructor.
discriminate.
case_eq (evalDet_step c1 s0); intuition;
rewrite H0 in H.
inversion H; clear H; subst.
right.
econstructor. econstructor. intuition.
econstructor.
eauto.
econstructor.
econstructor.
eauto.
trivial.
discriminate.
inversion H; clear H; subst.
edestruct (IHevalDet_steps).
simpl.
rewrite H0.
eauto.
trivial.
left.
econstructor; eauto.
destruct H. destruct H. intuition.
right.
econstructor. econstructor. intuition.
econstructor; eauto.
trivial.
Qed.
Theorem evalDet_steps_bind_eof_inv : forall (A B : Set)(c1 : Comp B)(c2 : B -> Comp A) s,
evalDet_steps (cs_more (Bind c1 c2) s) (@cs_eof A) ->
evalDet_steps (cs_more c1 s) (@cs_eof B) \/
exists b s', evalDet_steps (cs_more c1 s) (cs_done b s') /\
evalDet_steps (cs_more (c2 b) s') (@cs_eof A).
intuition.
eapply evalDet_steps_bind_eof_inv_h; eauto.
Qed.
Theorem evalDet_bind_assoc : forall (A : Set)(c1 : Comp A)(B C : Set)(c2 : A -> Comp B)(c3 : B -> Comp C),
evalDet_equiv (Bind (Bind c1 c2) c3) (Bind c1 (fun a => (Bind (c2 a) c3))).
intuition.
unfold evalDet_equiv.
intuition.
inversion H; clear H; subst.
Ltac evalDet_tac :=
match goal with
| [H1 : evalDet_steps ?x (cs_done ?a1 ?s1), H2 : evalDet_steps ?x (@cs_eof _) |- _ ] => exfalso; eauto using evalDet_steps_done_eof_func; intuition
| [H : evalDet_steps (cs_more (Bind _ _) _) (cs_done _ _) |- _ ] => apply evalDet_steps_bind_done_inv in H
| [H : evalDet_steps (cs_more (Bind _ _) _) (@cs_eof _) |- _ ] => apply evalDet_steps_bind_eof_inv in H; intuition
| [H : exists _ : _, _ |- _ ] => destruct H; intuition
| [H1 : evalDet_steps ?x (cs_done ?a1 ?s1), H2 : evalDet_steps ?x (cs_done ?a2 ?s2) |- _ ] => assert (a1 = a2 /\ s1 = s2); eauto using evalDet_steps_done_func; intuition; subst; clear H1; eauto
end.
repeat evalDet_tac.
econstructor.
eapply evalDet_steps_trans.
eapply evalDet_steps_bind_done; eauto.
eapply evalDet_steps_trans.
eapply evalDet_steps_bind_done; eauto.
eauto.
repeat evalDet_tac.
econstructor.
eapply evalDet_bind_eof; eauto.
econstructor.
eapply evalDet_steps_trans.
eapply evalDet_steps_bind_done; eauto.
eapply evalDet_bind_eof; eauto.
econstructor.
eapply evalDet_steps_trans.
eapply evalDet_steps_bind_done; eauto.
eapply evalDet_steps_trans.
eapply evalDet_steps_bind_done; eauto.
eauto.
inversion H; clear H; subst.
repeat evalDet_tac.
econstructor.
eapply evalDet_steps_trans.
eapply evalDet_steps_bind_done; eauto.
eapply evalDet_steps_trans.
eapply evalDet_steps_bind_done; eauto.
eauto.
eauto.
repeat evalDet_tac.
econstructor.
eapply evalDet_steps_trans.
eapply evalDet_bind_eof; eauto.
eapply evalDet_bind_eof; eauto.
econstructor.
econstructor.
eapply evalDet_bind_eof.
eapply evalDet_steps_trans.
eapply evalDet_steps_bind_done; eauto.
eauto.
econstructor.
eapply evalDet_steps_trans.
eapply evalDet_steps_bind_done; eauto.
eapply evalDet_steps_trans.
eapply evalDet_steps_bind_done; eauto.
eauto.
eauto.
Qed.
Theorem evalDet_done_eof_func:
forall (A : Set) (c : Comp A) (a : A) (s : Blist),
evalDet c s (ca_done a) -> evalDet c s (@ca_eof A) -> False.
intuition.
inversion H; clear H; subst.
inversion H0; clear H0; subst.
eapply evalDet_steps_done_eof_func; eauto.
Qed.
Lemma getSupport_In_evalDet_step_done : forall (A : Set)(c : Comp A) a s s',
evalDet_step c s = cs_done a s' ->
In a (getSupport c).
induction c; intuition; simpl in *.
inversion H; clear H; subst.
intuition.
destruct (evalDet_step c s); try discriminate.
eapply in_getAllBvectors.
discriminate.
Qed.
Lemma getSupport_In_evalDet_step_more : forall (A : Set)(c c' : Comp A) s s' a,
evalDet_step c s = cs_more c' s' ->
In a (getSupport c') ->
In a (getSupport c).
induction c; intuition; simpl in *.
discriminate.
case_eq (evalDet_step c s); intuition;
rewrite H2 in H0.
inversion H0; clear H0; subst.
eapply in_getUnique.
eapply in_flatten.
exists (getSupport (c0 b)).
intuition.
eapply in_map_iff.
exists b.
intuition.
eapply getSupport_In_evalDet_step_done; eauto.
discriminate.
inversion H0; clear H0; subst.
simpl in *.
apply in_getUnique_if in H1.
apply in_flatten in H1.
destruct H1; intuition.
eapply in_getUnique.
eapply in_flatten.
exists x.
intuition.
apply in_map_iff in H1.
destruct H1; intuition.
subst.
eapply in_map_iff.
exists x0.
intuition.
eapply IHc; eauto.
eapply in_getAllBvectors.
inversion H; clear H; subst.
simpl in *.
eapply in_getUnique_if in H0.
eapply in_flatten in H0.
destruct H0.
intuition.
eapply in_map_iff in H0.
destruct H0.
intuition.
subst.
case_eq (b x0); intuition;
rewrite H in H1; simpl in *.
intuition; subst.
eapply filter_In; eauto.
trivial.
Qed.
Lemma getSupport_In_evalDet_steps_h : forall (A : Set)(x y : comp_state A),
evalDet_steps x y ->
forall (c : Comp A) a s s',
x = (cs_more c s) ->
y = (cs_done a s') ->
In a (getSupport c).
induction 1; intuition; subst.
discriminate.
inversion H1; clear H1; subst.
inversion H0; clear H0; subst.
eapply getSupport_In_evalDet_step_done; eauto.
eapply getSupport_In_evalDet_step_more.
eauto.
eapply IHevalDet_steps.
eauto.
eauto.
Qed.
Lemma getSupport_In_evalDet_steps : forall (A : Set)(c : Comp A) a s s',
evalDet_steps (cs_more c s) (cs_done a s') ->
In a (getSupport c).
intuition.
eapply getSupport_In_evalDet_steps_h; eauto.
Qed.
Theorem getSupport_In_evalDet : forall (A : Set)(c : Comp A) a s,
evalDet c s (ca_done a) ->
In a (getSupport c).
intuition.
inversion H; clear H; subst.
eapply getSupport_In_evalDet_steps; eauto.
Qed.
(* The following predicate makes it easier for us to do induction on the number of loop iterations. *)
Inductive evalDet_repeat_steps (A : Set)(P : A -> bool) : comp_state A -> comp_state A -> Prop :=
| evalDet_repeat_steps_done :
forall c s a s',
evalDet_steps (cs_more c s) (cs_done a s') ->
P a = true ->
evalDet_repeat_steps P (cs_more c s) (cs_done a s')
| evalDet_repeat_steps_eof :
forall c s,
evalDet_steps (cs_more c s) (@cs_eof A) ->
evalDet_repeat_steps P (cs_more c s) (@cs_eof A)
| evalDet_repeat_steps_step :
forall c s a s' y,
evalDet_steps (cs_more c s) (cs_done a s') ->
P a = false ->
evalDet_repeat_steps P (cs_more c s') y ->
evalDet_repeat_steps P (cs_more c s) y.
Inductive evalDet_repeat(A : Set)(P : A -> bool)(c : Comp A)(s : Blist) : comp_answer A -> Prop :=
| evalDet_repeat_done : forall a s',
evalDet_repeat_steps P (cs_more c s) (cs_done a s') ->
evalDet_repeat P c s (ca_done a)
| evalDet_repeat_eof :
evalDet_repeat_steps P (cs_more c s) (@cs_eof A) ->
evalDet_repeat P c s (@ca_eof A).
Lemma list_skipn_strong_ind_h : forall (A : Type) l (P : list A -> Prop) ,
P nil ->
(forall x, (forall n, n > 0 -> P (skipn n x)) -> P x) ->
(forall n, P (skipn n l)).
induction l;
intuition; simpl in *.
destruct n; simpl in *; trivial.
destruct n; simpl in *.
eapply H0.
intuition.
destruct n; try omega; simpl in *.
eapply IHl; intuition.
eapply IHl; intuition.
Qed.
Lemma list_skipn_strong_ind : forall (A : Type) l (P : list A -> Prop) ,
P nil ->
(forall x, (forall n, n > 0 -> P (skipn n x)) -> P x) ->
P l.
intuition.
assert (l = skipn 0 l).
simpl.
trivial.
rewrite H1.
eapply list_skipn_strong_ind_h; trivial.
Qed.
Lemma evalDet_step_nil_inv : forall (A : Set)(c : Comp A)(a1 a2 : A) s2,
evalDet_step c nil = (cs_done a2 s2) ->
In a1 (getSupport c) ->
a1 = a2.
induction c; intuition; simpl in *; intuition; subst.
inversion H; clear H; subst.
trivial.
destruct (evalDet_step c nil); discriminate.
destruct n; try discriminate.
discriminate.
Qed.
Lemma evalDet_step_done_nil_inv : forall (A : Set)(c : Comp A) a ls,
evalDet_step c nil = (cs_done a ls) ->
ls = nil.
induction c; intuition; simpl in *.
inversion H; clear H; subst.
trivial.
case_eq (evalDet_step c nil); intuition;
rewrite H1 in H0;
discriminate.
destruct n;
discriminate.
discriminate.
Qed.
Lemma evalDet_step_more_nil_inv : forall (A : Set)(c c' : Comp A) ls,
evalDet_step c nil = (cs_more c' ls) ->
ls = nil.
induction c; intuition; simpl in *.
discriminate.
case_eq (evalDet_step c nil); intuition;
rewrite H1 in H0.
inversion H0; clear H0; subst.
eapply evalDet_step_done_nil_inv; eauto.
discriminate.
inversion H0; clear H0; subst.
eauto.
destruct n.
inversion H; clear H; subst.
trivial.
discriminate.
inversion H; clear H; subst.
trivial.
Qed.
Lemma evalDet_step_done_support_singleton : forall (A : Set)(c : Comp A) s a,
evalDet_step c s = cs_done a s ->
getSupport c = (a :: nil).
induction c; intuition; simpl in *.
inversion H; clear H; subst.
trivial.
destruct (evalDet_step c s); discriminate.
destruct (shiftOut s n).
destruct p.
discriminate.
discriminate.
discriminate.
Qed.
Lemma getUnique_NoDup_eq : forall (A : Set)(eqd : eq_dec A)(ls : list A),
NoDup ls ->
getUnique ls eqd = ls.
induction ls; intuition; simpl in *.
inversion H; clear H; subst.
destruct (in_dec eqd a (getUnique ls eqd)).
exfalso.
apply H2.
eapply in_getUnique_if.
eauto.
f_equal.
eauto.
Qed.
Lemma getUnique_Permutation : forall (A : Set)(eqd1 eqd2 : eq_dec A)(ls1 ls2 : list A),
Permutation ls1 ls2 ->
Permutation (getUnique ls1 eqd1) (getUnique ls2 eqd2).
induction 1; intuition; simpl in *.
destruct (in_dec eqd1 x (getUnique l eqd1)).
destruct (in_dec eqd2 x (getUnique l' eqd2)).
eauto.
exfalso.
eapply n.
eapply Permutation_in; eauto.
destruct (in_dec eqd2 x (getUnique l' eqd2)).
exfalso.
eapply n.
eapply Permutation_in.
eapply Permutation_sym.
eauto.
eauto.
eapply perm_skip.
trivial.
destruct (in_dec eqd1 x (getUnique l eqd1)).
destruct (in_dec eqd1 y (getUnique l eqd1)).
destruct (in_dec eqd2 y (getUnique l eqd2)).
destruct (in_dec eqd2 x (getUnique l eqd2)).
eapply NoDup_Permutation; eauto using getUnique_NoDup; intuition;
eauto using in_getUnique, in_getUnique_if.
exfalso.
eauto using in_getUnique, in_getUnique_if.
exfalso.
eauto using in_getUnique, in_getUnique_if.
destruct (in_dec eqd2 y (getUnique l eqd2)).
exfalso.
eauto using in_getUnique, in_getUnique_if.
destruct (in_dec eqd2 x (y :: getUnique l eqd2)).
eapply perm_skip.
eapply NoDup_Permutation; eauto using getUnique_NoDup; intuition;
eauto using in_getUnique, in_getUnique_if.
simpl in *. intuition.
exfalso.
eauto using in_getUnique, in_getUnique_if.
destruct (in_dec eqd1 y (x :: getUnique l eqd1)).
destruct (in_dec eqd2 y (getUnique l eqd2)).
destruct (in_dec eqd2 x (getUnique l eqd2)).
exfalso.
eauto using in_getUnique, in_getUnique_if.
eapply perm_skip.
eapply NoDup_Permutation; eauto using getUnique_NoDup; intuition;
eauto using in_getUnique, in_getUnique_if.
destruct (in_dec eqd2 x (y :: getUnique l eqd2)).
simpl in *; intuition.
clear H.
subst.
eapply perm_skip.
eapply NoDup_Permutation; eauto using getUnique_NoDup; intuition;
eauto using in_getUnique, in_getUnique_if.
subst.
exfalso.
eauto using in_getUnique, in_getUnique_if.
subst.
exfalso.
eauto using in_getUnique, in_getUnique_if.
exfalso.
eauto using in_getUnique, in_getUnique_if.
simpl in *; intuition; subst.
intuition.
exfalso.
eauto using in_getUnique, in_getUnique_if.
destruct (in_dec eqd2 y (getUnique l eqd2)).
destruct (in_dec eqd2 x (getUnique l eqd2)).
exfalso.
eauto using in_getUnique, in_getUnique_if.
simpl in n0; intuition.
exfalso.
eauto using in_getUnique, in_getUnique_if.
destruct (in_dec eqd2 x (y :: getUnique l eqd2)).
simpl in *; intuition; subst.
intuition.
exfalso.
eauto using in_getUnique, in_getUnique_if.
eapply perm_trans.
eapply perm_swap.
eapply perm_skip.
eapply perm_skip.
eapply NoDup_Permutation; eauto using getUnique_NoDup; intuition;
eauto using in_getUnique, in_getUnique_if.
eapply perm_trans.
eapply IHPermutation1.
eapply (@perm_trans _ _ (getUnique l' eqd1)).
eapply NoDup_Permutation; eauto using getUnique_NoDup; intuition;
eauto using in_getUnique, in_getUnique_if.
trivial.
Qed.
Lemma flatten_Permutation : forall (A : Type)(ls1 ls2 : list (list A)),
Permutation ls1 ls2 ->
Permutation (flatten ls1) (flatten ls2).
induction 1; intuition; simpl in *.
eapply Permutation_app.
eapply Permutation_refl.
trivial.
repeat rewrite app_assoc.
eapply Permutation_app.
eapply Permutation_app_comm.
eapply Permutation_refl.
eapply perm_trans.
eapply IHPermutation1.
trivial.
Qed.
Lemma to_list_nil_inv : forall (A : Type)(n : nat)(v : Vector.t A n),
Vector.to_list v = nil ->
n = O.
intuition.
destruct v; simpl in *.
trivial.
unfold Vector.to_list in *.
discriminate.
Qed.
Lemma app_second_eq :
forall (A : Type) (ls2 ls1 ls3 : list A),
ls1 = ls2 ++ ls3 -> length ls1 = length ls3 -> ls1 = ls3 /\ ls2 = nil.
induction ls2; simpl in *; intuition;
subst;
simpl in *;
rewrite app_length in H0;
omega.
Qed.
Lemma shiftOut_same_inv : forall s n v,
shiftOut s n = Some (v, s) ->
n = O.
intuition.
eapply to_list_nil_inv.
eapply app_second_eq.
eapply shiftOut_correct_inv.
eauto.
trivial.
Qed.
Lemma filter_Permutation : forall (A : Set)(ls1 ls2 : list A)(P : A -> bool),
Permutation ls1 ls2 ->
Permutation (filter P ls1) (filter P ls2).