-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweightslang.v
More file actions
2594 lines (2404 loc) · 83.3 KB
/
weightslang.v
File metadata and controls
2594 lines (2404 loc) · 83.3 KB
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
Set Implicit Arguments.
Unset Strict Implicit.
Require Import QArith NArith.
Require Import ProofIrrelevance.
Require Import mathcomp.ssreflect.ssreflect.
From mathcomp Require Import all_ssreflect.
From mathcomp Require Import all_algebra.
Require Import OUVerT.dist OUVerT.numerics OUVerT.dyadic OUVerT.bigops.
Require Import MWU.weights.
(** An extremely simple probabilistic programming language,
used to implement multiplicative weights update (weights.v) *)
Inductive val : Type :=
| QVal : D -> val.
Inductive binop : Type := BPlus | BMinus | BMult.
Section com.
Variable A : Type. (* The game type *)
Inductive expr : Type :=
| EVal : val -> expr
| EOpp : expr -> expr
| EWeight : A -> expr
| ECost : A -> expr
| EEps : expr
| EBinop : binop -> expr -> expr -> expr.
Inductive com : Type :=
| CSkip : com
| CUpdate : (A -> expr) -> com
| CRecv : com
| CSend : com
| CSeq : com -> com -> com
| CIter : N.t -> com -> com.
End com.
Arguments EVal [A] _.
Arguments EEps [A].
Arguments CSkip [A].
Arguments CRecv [A].
Arguments CSend [A].
Arguments CIter [A] _ _.
Lemma val_eq_dec (v1 v2 : val) : {v1=v2}+{v1<>v2}.
Proof.
decide equality.
apply Deq_dec.
Qed.
Lemma eqType_eq_dec (A : eqType) (a s : A) : {a=s}+{a<>s}.
Proof.
case H: (a == s).
{ by move: (eqP H) => ->; left. }
{ by right => H2; subst a; rewrite eq_refl in H. }
Qed.
Lemma expr_eq_dec (A : eqType) (e1 e2 : expr A) : {e1=e2}+{e1<>e2}.
Proof.
decide equality.
apply: val_eq_dec.
apply: eqType_eq_dec.
apply: eqType_eq_dec.
decide equality.
Qed.
(** Commands aren't decidable in general, because they allow
embedded Coq functions in CUpdate. *)
Lemma com_Iter_dec A (c : com A) :
(exists c1 n, c=CIter n c1) \/ forall c1 n, c<>CIter c1 n.
Proof.
case: c; try solve[right => c0 H; congruence].
{ move => c c0; right => c3; congruence. }
by move => n c; left; exists c, n.
Qed.
Lemma com_Skip_dec A (c : com A) : {c=CSkip}+{c<>CSkip}.
Proof. case: c; try solve[left => // | right => H; congruence]. Qed.
Definition mult_weights_body (A : Type) : com A :=
CSeq
CRecv
(CSeq (CUpdate (fun a : A =>
(EBinop BMult
(EWeight a)
(EBinop BMinus
(EVal (QVal D1))
(EBinop BMult EEps (ECost a))))))
CSend).
Definition mult_weights_init (A : Type) : com A :=
CSeq
(CUpdate (fun a : A => EVal (QVal D1)))
CSend.
Definition mult_weights (A : Type) (n : N.t) : com A :=
CSeq
(** 1. Initialize weights to uniform *)
(mult_weights_init A)
(** 2. The main MWU loop *)
(CIter n (mult_weights_body A)).
Section client_oracle.
Local Open Scope ring_scope.
(* Client oracle *)
Class ClientOracle (A : finType) T oracle_chanty :=
mkOracle { oracle_recv :
T -> oracle_chanty -> {ffun A -> rat} -> T -> Prop
; oracle_send :
T -> dist A rat_realFieldType -> oracle_chanty -> T -> Prop
; oracle_recv_ok : forall st ch f t' (a : A),
@oracle_recv st ch f t' ->
`|f a| <= 1
}.
End client_oracle.
Section semantics.
Local Open Scope ring_scope.
Variable A : finType.
Variable a0 : A. (*A must be inhabited.*)
Context T oracle_chanty `{Hco : ClientOracle A T oracle_chanty}.
Record state : Type :=
mkState
{ SCosts : {ffun A -> rat} (* the current cost vector *)
; SCostsOk : forall a, `|SCosts a| <= 1
(* the history of cost vectors seen so far *)
; SPrevCosts : seq {c : {ffun A -> rat} & forall a, `|c a| <= 1}
; SWeights : {ffun A -> rat} (* current weights *)
; SWeightsOk : forall a, 0 < SWeights a
; SEpsilon : rat (* epsilon -- a parameter *)
; SEpsilonOk : 0 < SEpsilon <= 1 / 2%:R
(* the history of the generated distributions over actions *)
; SOutputs : seq (dist A rat_realFieldType)
; SChan : oracle_chanty
; SOracleSt : T }.
Definition eval_binop (b : binop) (v1 v2 : rat) :=
match b with
| BPlus => v1 + v2
| BMinus => v1 - v2
| BMult => v1 * v2
end.
Fixpoint eval (e : expr A) (s : state) : rat :=
match e with
| EVal v =>
match v with
| QVal q => Q_to_rat (D_to_Q q)
end
| EOpp e' => let: v := eval e' s in - v
| EWeight a => SWeights s a
| ECost a => SCosts s a
| EEps => SEpsilon s
| EBinop b e1 e2 =>
let: v1 := eval e1 s in
let: v2 := eval e2 s in
eval_binop b v1 v2
end.
Definition CMAX_costs_seq_cons
(c : costs A)
(pf : forall a, `|c a| <= 1)
(cs : CMAX_costs_seq A)
: CMAX_costs_seq A.
Proof.
exists (c :: projT1 cs); apply/CMAXP => c'.
rewrite in_cons; case/orP.
{ by move/eqP => ->. }
by move => Hx a; apply: (CMAXP _ (projT2 cs) c' Hx a).
Defined.
Lemma CMAX_nil :
forall c, c \in (nil : seq (costs A)) -> forall a, `|c a| <= 1.
Proof. by []. Qed.
(** The small-step semantics *)
Inductive step : com A -> state -> com A -> state -> Prop :=
| SUpdate :
forall f s pf,
let: s' :=
@mkState
(SCosts s)
(SCostsOk s)
(SPrevCosts s)
(finfun (fun a => eval (f a) s))
pf
(SEpsilon s)
(SEpsilonOk s)
(SOutputs s)
(SChan s)
(SOracleSt s)
in
step (CUpdate f) s CSkip s'
| SRecv :
forall s f t' (Hrecv : oracle_recv (SOracleSt s) (SChan s) f t') pf,
let: s' :=
@mkState
f
pf
(existT _ (SCosts s) (SCostsOk s) :: SPrevCosts s)
(SWeights s)
(SWeightsOk s)
(SEpsilon s)
(SEpsilonOk s)
(SOutputs s)
(SChan s)
t'
in
step CRecv s CSkip s'
| SSend :
forall s ch t',
let: d:=
p_aux_dist
a0
(SEpsilonOk s)
(SWeightsOk s)
CMAX_nil (*Important that [cs := nil] here!
[p_aux_dist] applied to the empty sequence
of cost functions specializes to the distribution formed
by: SWeights w / \Gamma. *) in
oracle_send (SOracleSt s) d ch t' ->
let: s' :=
@mkState
(SCosts s)
(SCostsOk s)
(SPrevCosts s)
(SWeights s)
(SWeightsOk s)
(SEpsilon s)
(SEpsilonOk s)
(d :: SOutputs s)
ch
t'
in
step CSend s CSkip s'
| SSeq1 :
forall c2 s,
step (CSeq CSkip c2) s c2 s
| SSeq2 :
forall c1 c1' c2 s1 s2,
step c1 s1 c1' s2 ->
step (CSeq c1 c2) s1 (CSeq c1' c2) s2
| SIter0 :
forall c s,
step (CIter N0 c) s CSkip s
| SIterS :
forall c s (n : N.t),
(0 < n)%N ->
step (CIter n c) s (CSeq c (CIter (N.pred n) c)) s.
Inductive step_plus : com A -> state -> com A -> state -> Prop :=
| step1 :
forall c s c' s',
step c s c' s' ->
step_plus c s c' s'
| step_trans :
forall c s c'' s'' c' s',
step c s c'' s'' ->
step_plus c'' s'' c' s' ->
step_plus c s c' s'.
(** Another version of the operational semantics, with slightly
different iteration semantics (FIXME! FOR BACKWARD COMPATIBILITY
WITH EXISTING PROOFS.) *)
Inductive step' : com A -> state -> com A -> state -> Prop :=
| SUpdate' :
forall f s pf,
let: s' :=
@mkState
(SCosts s)
(SCostsOk s)
(SPrevCosts s)
(finfun (fun a => eval (f a) s))
pf
(SEpsilon s)
(SEpsilonOk s)
(SOutputs s)
(SChan s)
(SOracleSt s)
in
step' (CUpdate f) s CSkip s'
| SRecv' :
forall s f t' (Hrecv : oracle_recv (SOracleSt s) (SChan s) f t') pf,
let: s' :=
@mkState
f
pf
(existT _ (SCosts s) (SCostsOk s) :: SPrevCosts s)
(SWeights s)
(SWeightsOk s)
(SEpsilon s)
(SEpsilonOk s)
(SOutputs s)
(SChan s)
t'
in
step' CRecv s CSkip s'
| SSend' :
forall s ch t',
let: d:=
p_aux_dist
a0
(SEpsilonOk s)
(SWeightsOk s)
CMAX_nil (*Important that [cs := nil] here!
[p_aux_dist] applied to the empty sequence
of cost functions specializes to the distribution formed
by: SWeights w / \Gamma. *) in
oracle_send (SOracleSt s) d ch t' ->
let: s' :=
@mkState
(SCosts s)
(SCostsOk s)
(SPrevCosts s)
(SWeights s)
(SWeightsOk s)
(SEpsilon s)
(SEpsilonOk s)
(d :: SOutputs s)
ch
t'
in
step' CSend s CSkip s'
| SSeq1' :
forall c2 s,
step' (CSeq CSkip c2) s c2 s
| SSeq2' :
forall c1 c1' c2 s1 s2,
step' c1 s1 c1' s2 ->
step' (CSeq c1 c2) s1 (CSeq c1' c2) s2
| SIter1' :
forall c s,
step' (CIter 1 c) s c s
| SIterS' :
forall c s (n : N.t),
(1 < n)%N ->
step' (CIter n c) s (CSeq c (CIter (N.pred n) c)) s.
Inductive step'_plus : com A -> state -> com A -> state -> Prop :=
| step'1 :
forall c s c' s',
step' c s c' s' ->
step'_plus c s c' s'
| step'_trans :
forall c s c'' s'' c' s',
step' c s c'' s'' ->
step'_plus c'' s'' c' s' ->
step'_plus c s c' s'.
Inductive final_com : com A -> Prop :=
| final_skip :
final_com CSkip.
(** Here's the corresponding big-step semantics. Note that it's
index by a nat [n], to break the nonwellfounded recursion
in the [CRepeat] case. *)
Inductive stepN : nat -> com A -> state -> state -> Prop :=
| N0 :
forall n s, stepN n CSkip s s
| NUpdate :
forall n f s pf,
let: s' :=
@mkState
(SCosts s)
(SCostsOk s)
(SPrevCosts s)
(finfun (fun a => eval (f a) s))
pf
(SEpsilon s)
(SEpsilonOk s)
(SOutputs s)
(SChan s)
(SOracleSt s)
in
stepN (S n) (CUpdate f) s s'
| NRecv :
forall n s f t' (Hrecv : oracle_recv (SOracleSt s) (SChan s) f t') pf,
let: s' :=
@mkState
f
pf
(existT _ (SCosts s) (SCostsOk s) :: SPrevCosts s)
(SWeights s)
(SWeightsOk s)
(SEpsilon s)
(SEpsilonOk s)
(SOutputs s)
(SChan s)
t'
in
stepN (S n) CRecv s s'
| NSend :
forall n s ch t',
let: d:=
p_aux_dist
a0
(SEpsilonOk s)
(SWeightsOk s)
CMAX_nil in
oracle_send (SOracleSt s) d ch t' ->
let: s' :=
@mkState
(SCosts s)
(SCostsOk s)
(SPrevCosts s)
(SWeights s)
(SWeightsOk s)
(SEpsilon s)
(SEpsilonOk s)
(d :: SOutputs s)
ch
t'
in
stepN (S n) CSend s s'
| NSeq :
forall n c1 c2 s1 s1' s2,
stepN n c1 s1 s1' ->
stepN n c2 s1' s2 ->
stepN (S n) (CSeq c1 c2) s1 s2
| NIter0 :
forall n c s,
stepN n (CIter N0 c) s s
| NIterS :
forall n (n' : N.t) c s s',
(0 < nat_of_bin n')%N ->
stepN n (CSeq c (CIter (N.pred n') c)) s s' ->
stepN (S n) (CIter n' c) s s'.
Lemma step_plus_trans c1 c2 c3 s1 s2 s3 :
step_plus c1 s1 c2 s2 ->
step_plus c2 s2 c3 s3 ->
step_plus c1 s1 c3 s3.
Proof.
move => H H1; induction H.
apply: step_trans; first by apply: H. apply: H1.
apply: step_trans. apply: H. by apply IHstep_plus.
Qed.
Lemma step'_plus_trans c1 c2 c3 s1 s2 s3 :
step'_plus c1 s1 c2 s2 ->
step'_plus c2 s2 c3 s3 ->
step'_plus c1 s1 c3 s3.
Proof.
move => H H1; induction H.
apply: step'_trans; first by apply: H. apply: H1.
apply: step'_trans. apply: H. by apply IHstep'_plus.
Qed.
Lemma step_plus_trans2 c1 c2 c3 s1 s2 s3 :
step_plus c1 s1 c2 s2 ->
step c2 s2 c3 s3 ->
step_plus c1 s1 c3 s3.
Proof.
move => H H2; induction H.
{ apply: step_trans; first by apply: H. constructor. apply: H2. }
apply: step_trans. apply: H. by apply: IHstep_plus.
Qed.
Lemma step_plus_seq c1 c1' c2 s s' :
step_plus c1 s c1' s' ->
step_plus (CSeq c1 c2) s (CSeq c1' c2) s'.
Proof.
induction 1.
constructor.
by constructor.
apply: step_trans.
constructor.
apply: H.
apply: IHstep_plus.
Qed.
Lemma step'_plus_seq c1 c1' c2 s s' :
step'_plus c1 s c1' s' ->
step'_plus (CSeq c1 c2) s (CSeq c1' c2) s'.
Proof.
induction 1.
constructor.
by constructor.
apply: step'_trans.
constructor.
apply: H.
apply: IHstep'_plus.
Qed.
Lemma step_plus_CSkip s c' s' :
step_plus CSkip s c' s' -> False.
Proof.
remember CSkip as c.
move => H; revert Heqc.
induction H.
{ move => H2; subst c; inversion H. }
move => H2; subst c. inversion H.
Qed.
Lemma nat_of_bin_to_nat b : nat_of_bin b = N.to_nat b.
Proof.
rewrite /nat_of_bin.
case: b => //.
elim => //.
{ move => p /= H; rewrite /Pos.to_nat /=; f_equal.
rewrite ZL6 NatTrec.doubleE H -muln2 mulnC -multE /=.
by rewrite -plus_n_O. }
move => p /= H; rewrite /Pos.to_nat /=; f_equal.
rewrite ZL6 NatTrec.doubleE H -muln2 mulnC -multE /=.
by rewrite -plus_n_O.
Qed.
Lemma step_plus_CIter0 c s c' s' :
step_plus (CIter 0 c) s c' s' -> s = s'.
Proof.
remember (CIter 0 c) as c0.
move => H; revert c Heqc0.
induction H.
{ move => c0 H2; subst c.
inversion H; subst; auto. }
move => c0 H2; subst c.
inversion H; subst.
{ destruct (step_plus_CSkip H0). }
inversion H; subst.
rewrite nat_of_bin_to_nat in H4.
by [].
Qed.
Lemma step_seq_inv :
forall c1 c2 s c' s',
step (CSeq c1 c2) s c' s' ->
[/\ c1=CSkip, c'=c2 & s'=s] \/
exists c1' s'',
[/\ step c1 s c1' s'', c'=CSeq c1' c2, s'=s'' & c1<>CSkip].
Proof.
move => c1 c2 s c' s'; inversion 1; subst; clear H.
{ by left; split. }
right; exists c1', s'; split => //.
inversion H5; subst; try solve[inversion 1].
Qed.
Lemma step'_seq_inv :
forall c1 c2 s c' s',
step' (CSeq c1 c2) s c' s' ->
[/\ c1=CSkip, c'=c2 & s'=s] \/
exists c1' s'',
[/\ step' c1 s c1' s'', c'=CSeq c1' c2, s'=s'' & c1<>CSkip].
Proof.
move => c1 c2 s c' s'; inversion 1; subst; clear H.
{ by left; split. }
right; exists c1', s'; split => //.
inversion H5; subst; try solve[inversion 1].
Qed.
Lemma step_plus_seq_inv1 :
forall c1 c2 s c' s',
step_plus (CSeq c1 c2) s c' s' ->
final_com c' ->
c1<>CSkip ->
exists s'',
step_plus c1 s CSkip s'' /\
step_plus (CSeq CSkip c2) s'' c' s'.
Proof.
move => c1 c2 s c' s'.
remember (CSeq c1 c2) as c => H.
revert c1 c2 Heqc.
induction H.
{ move => c1 c2 H2; subst c; inversion 1; subst. clear H0.
move => H2.
case: (step_seq_inv H).
{ case => H3 H4 H5.
subst c1.
congruence. }
case => c1' [] s0 []H3 H4 H5 H6.
exists s0.
split => //. }
move => c1 c2 H2; subst c. inversion 1; subst. clear H1.
move => H2.
case: (step_seq_inv H).
{ case => H3 H4 H5.
subst c1.
congruence. }
case => c1' [] s0 []H3 H4 H5 H6.
subst.
case: (com_Skip_dec c1').
{ move => H7. subst c1'.
exists s0.
split => //.
constructor => //. }
move => H7.
case: (IHstep_plus c1' c2 erefl) => // sx [] H8 H9.
exists sx.
split => //.
apply: step_trans.
apply: H3.
apply: H8.
Qed.
Lemma step'_plus_seq_inv1 :
forall c1 c2 s c' s',
step'_plus (CSeq c1 c2) s c' s' ->
final_com c' ->
c1<>CSkip ->
exists s'',
step'_plus c1 s CSkip s'' /\
step'_plus (CSeq CSkip c2) s'' c' s'.
Proof.
move => c1 c2 s c' s'.
remember (CSeq c1 c2) as c => H.
revert c1 c2 Heqc.
induction H.
{ move => c1 c2 H2; subst c; inversion 1; subst. clear H0.
move => H2.
case: (step'_seq_inv H).
{ case => H3 H4 H5.
subst c1.
congruence. }
case => c1' [] s0 []H3 H4 H5 H6.
exists s0.
split => //. }
move => c1 c2 H2; subst c. inversion 1; subst. clear H1.
move => H2.
case: (step'_seq_inv H).
{ case => H3 H4 H5.
subst c1.
congruence. }
case => c1' [] s0 []H3 H4 H5 H6.
subst.
case: (com_Skip_dec c1').
{ move => H7. subst c1'.
exists s0.
split => //.
constructor => //. }
move => H7.
case: (IHstep'_plus c1' c2 erefl) => // sx [] H8 H9.
exists sx.
split => //.
apply: step'_trans.
apply: H3.
apply: H8.
Qed.
Lemma step_plus_seq_skip :
forall c s c' s',
step_plus (CSeq CSkip c) s c' s' ->
s=s' \/ step_plus c s c' s'.
Proof.
move => c s c' s'; remember (CSeq _ _) as c0; induction 1.
{ subst c0; inversion H; subst; first by left.
by inversion H5; subst; clear H5; left. }
subst c0; inversion H; subst; clear H; first by right.
inversion H6; subst; clear H6.
Qed.
Lemma step_plus_seq_skip_final :
forall c s c' s',
final_com c' ->
step_plus (CSeq CSkip c) s c' s' ->
(s=s' /\ final_com c) \/ step_plus c s c' s'.
Proof.
move => c s c' s' Hx; remember (CSeq _ _) as c0; induction 1.
{ subst c0; inversion H; subst; first by left; split.
by inversion H5; subst; clear H5; left. }
subst c0; inversion H; subst; clear H; first by right.
inversion H6; subst; clear H6.
Qed.
Lemma step'_plus_seq_skip_final :
forall c s c' s',
final_com c' ->
step'_plus (CSeq CSkip c) s c' s' ->
(s=s' /\ final_com c) \/ step'_plus c s c' s'.
Proof.
move => c s c' s' Hx; remember (CSeq _ _) as c0; induction 1.
{ subst c0; inversion H; subst; first by left; split.
by inversion H5; subst; clear H5; left. }
subst c0; inversion H; subst; clear H; first by right.
inversion H6; subst; clear H6.
Qed.
Lemma step_plus_CIter_inv n c s c' s' :
final_com c' ->
(0 < n)%num ->
step_plus (CIter n c) s c' s' ->
exists s0,
[/\ step_plus (CSeq c (CIter (N.pred n) c)) s (CIter (N.pred n) c) s0
& step_plus (CIter (N.pred n) c) s0 c' s'].
Proof.
move => H H2.
remember (CIter n c) as c0.
move => H0.
revert c Heqc0 H H2.
destruct H0.
{ move => c0 H2; subst c. inversion 1; subst. move => H2.
inversion H; subst => //. }
move => c0 H2; subst c. inversion 1; subst. clear H1 => H2.
inversion H; subst => //.
destruct (com_Skip_dec c0).
{ subst c0.
inversion H0; subst. clear H0.
{ inversion H1. }
move: (step_seq_inv H1).
case; last first.
{ case => x; case => y []; inversion 1. }
case => _ H4 H5; subst.
clear H H1.
exists s''; split => //.
constructor.
constructor. }
case: (step_plus_seq_inv1 H0) => // sx [] H3 H4.
have H5:
step_plus (CSeq c0 (CIter (N.pred n) c0)) s''
(CSeq CSkip (CIter (N.pred n) c0)) sx.
{ apply: step_plus_seq => //. }
exists sx; split.
{ apply: step_plus_trans; first by apply: H5.
constructor. constructor. }
case: (step_plus_seq_skip_final _ H4) => // [][]H6 H8.
subst sx.
inversion H8.
Qed.
Lemma step'_plus_CIter_inv n c s c' s' :
final_com c' ->
(1 < n)%num ->
step'_plus (CIter n c) s c' s' ->
exists s0,
[/\ step'_plus (CSeq c (CIter (N.pred n) c)) s (CIter (N.pred n) c) s0
& step'_plus (CIter (N.pred n) c) s0 c' s'].
Proof.
move => H H2.
remember (CIter n c) as c0.
move => H0.
revert c Heqc0 H H2.
destruct H0.
{ move => c0 H2; subst c. inversion 1; subst. move => H2.
inversion H; subst => //. }
move => c0 H2; subst c. inversion 1; subst. clear H1 => H2.
inversion H; subst => //.
destruct (com_Skip_dec c0).
{ subst c0.
inversion H0; subst. clear H0.
{ inversion H1. }
move: (step'_seq_inv H1).
case; last first.
{ case => x; case => y []; inversion 1. }
case => _ H4 H5; subst.
clear H H1.
exists s''; split => //.
constructor.
constructor. }
case: (step'_plus_seq_inv1 H0) => // sx [] H3 H4.
have H5:
step'_plus (CSeq c0 (CIter (N.pred n) c0)) s''
(CSeq CSkip (CIter (N.pred n) c0)) sx.
{ apply: step'_plus_seq => //. }
exists sx; split.
{ apply: step'_plus_trans; first by apply: H5.
constructor. constructor. }
case: (step'_plus_seq_skip_final _ H4) => // [][]H6 H8.
subst sx.
inversion H8.
Qed.
Lemma step_plus_CIter_skip_split n1 n2 s c' s'' s' :
step_plus (CIter n1 CSkip) s CSkip s'' ->
step_plus (CIter n2 CSkip) s'' c' s' ->
step_plus (CIter (n1+n2) CSkip) s c' s'.
Proof.
move: n2 s c' s'' s'.
set (P (n : N.t) :=
forall (n2 : N.t) (s : state) (c' : com A) (s'' s' : state),
step_plus (CIter n CSkip) s CSkip s'' ->
step_plus (CIter n2 CSkip) s'' c' s' ->
step_plus (CIter (n + n2) CSkip) s c' s').
apply: (N.peano_ind P _ _ n1) => //.
{ rewrite /P => n2 s c' s'' s' H H2.
move: (step_plus_CIter0 H) => ->.
rewrite N.add_0_l; apply: H2. }
rewrite /P => n IH n2 s c' s'' s' H H2.
have Hx: (0 < N.succ n)%num.
{ apply: N.lt_0_succ. }
have Hy: (0 < N.succ (n + n2))%N.
{ apply/ltP; rewrite nat_of_bin_to_nat N2Nat.inj_succ; omega. }
case: (step_plus_CIter_inv _ _ H) => //.
move => x []H3 H4.
rewrite N.add_succ_l.
case: (step_plus_seq_skip H3).
{ move => ->.
apply: step_plus_trans.
{ constructor. constructor => //. }
rewrite N.pred_succ in H4.
apply: step_plus_trans; last first.
{ apply: (IH _ _ _ _ _ H4 H2). }
rewrite N.pred_succ.
constructor.
constructor. }
rewrite N.pred_succ in H3, H4|-* => H5.
apply: step_plus_trans.
{ constructor. constructor => //. }
apply: step_plus_trans; last first.
{ have H6: step_plus (CIter n CSkip) s CSkip s''.
{ apply: step_plus_trans; first by apply: H5.
apply: H4. }
apply: (IH _ _ _ _ _ H6 H2). }
rewrite N.pred_succ.
constructor.
constructor.
Qed.
Lemma bwahaha (c c1 : com A) : c = CSeq c1 c -> False.
Proof.
elim: c c1 => // c IH c' H c1; inversion 1.
by move: (H _ H3).
Qed.
Lemma step_same_false c s c' s' :
step c s c' s' ->
c = c' ->
False.
Proof.
induction 1; try solve[inversion 1].
{ move => H2; symmetry in H2; apply: (bwahaha H2). }
by inversion 1; subst; apply: IHstep.
Qed.
Lemma step_plus_seq_inv2 c1 c2 s x :
c1 <> CSkip ->
(forall s s', step_plus c2 s c2 s' -> False) ->
step_plus (CSeq c1 c2) s c2 x ->
step_plus c1 s CSkip x.
Proof.
remember (CSeq c1 c2) as c.
move => H Hx H2; revert c1 H Heqc.
induction H2.
{ move => c1 H3 H4; subst c.
case: (step_seq_inv H).
{ case => H4; subst c1; congruence. }
case => c1' []s1' []H4 H5 H6 H7.
subst s1'.
case: (com_Skip_dec c1').
{ move => H8. subst c1'.
constructor; apply: H4. }
elimtype False; apply: (bwahaha H5). }
move => c1 H3 H4; subst c.
case: (step_seq_inv H).
{ case => H4; subst c1; congruence. }
case => c1' []s1' []H4 H5 H6 H7; subst.
case: (com_Skip_dec c1').
{ move => H8; subst.
have ->: s' = s1'.
{ case: (step_plus_seq_skip H2); first by move => ->.
move => H5.
elimtype False.
apply: (Hx _ _ H5). }
apply: step1.
apply: H4. }
move => Hnskip.
apply: step_trans; first by apply: H4.
apply: IHstep_plus => //.
Qed.
(*the kinds of traces generated by CIter*)
Inductive iter_step : nat -> com A -> nat -> com A -> Prop :=
| iter_step0 :
forall c,
iter_step 0 (CIter 0 c) 0 CSkip
| iter_step_unfold :
forall c n,
(0 < n)%num ->
iter_step (N.to_nat n) (CIter n c) (N.to_nat n) (CSeq c (CIter (N.pred n) c))
| iter_step1 :
forall c c' c2 n,
iter_step (S (N.to_nat n)) (CSeq c (CIter n c2)) (S (N.to_nat n)) (CSeq c' (CIter n c2))
| iter_step_done :
forall c n,
iter_step (S (N.to_nat n)) (CSeq CSkip (CIter n c)) (N.to_nat n) (CIter n c).
(*control continuations derived from CIter*)
Inductive is_iter : nat -> com A -> Prop :=
| is_iter_CSkip : is_iter 0 CSkip
| is_iter_CIter :
forall c n, is_iter (N.to_nat n) (CIter n c)
| is_iter_CSeq_CIter :
forall c c' n, is_iter (S (N.to_nat n)) (CSeq c (CIter n c')).
Lemma step_iter_step c s c' s' n :
step c s c' s' ->
is_iter n c ->
exists n', iter_step n c n' c' /\ is_iter n' c'.
Proof.
induction 1;
try solve[inversion 1|
inversion 1; subst; eexists; split; try constructor].
inversion 1; subst.
exists (S (N.to_nat (N.pred n0))).
split; try constructor.
have Hx: (0 < n0)%num.
{ clear - H; rewrite /N.lt N2Nat.inj_compare.
rewrite nat_of_bin_to_nat in H.
by rewrite -nat_compare_lt /lt; apply/leP. }
have ->: (S (N.to_nat (N.pred n0))) = N.to_nat n0.
{ rewrite -N2Nat.inj_succ N.succ_pred_pos => //. }
constructor => //.
Qed.
Inductive iter_plus : nat -> com A -> nat -> com A -> Prop :=
| iter1 :
forall n c n' c',
iter_step n c n' c' ->
iter_plus n c n' c'
| iter_trans :
forall n c n'' c'' n' c',
iter_step n c n'' c'' ->
iter_plus n'' c'' n' c' ->
iter_plus n c n' c'.
Lemma step_plus_iter_plus n c s c' s' :
step_plus c s c' s' ->
is_iter n c ->
exists n', iter_plus n c n' c' /\ is_iter n' c'.
Proof.
move => H.
revert n.
induction H.
{ move => n H2; case: (step_iter_step H H2) => n' []H3 H4.
exists n'; split; auto.
constructor; auto. }
move => n H2.
case: (step_iter_step H H2) => n'' []H3 H4.
case: (IHstep_plus _ H4) => n' []H5 H6.
exists n'. split; auto.
apply: iter_trans; first by apply: H3.
apply: H5.
Qed.
Definition is_literal_iter (c : com A) :=
match c with
| CIter _ _ => True
| _ => False
end.
Lemma iter_step_literal_ord n c n' c' :
iter_step n c n' c' ->
(is_literal_iter c' /\ (n' < n)%coq_nat) \/
(~is_literal_iter c' /\ n' = n).
Proof.
induction 1; try solve[right; split => //].
left; split => //.
Qed.
Lemma iter_plus_literal_ord n c n' c' :
iter_plus n c n' c' ->
(is_literal_iter c' /\ (n' < n)%coq_nat) \/
(~is_literal_iter c' /\ (n' <= n)%coq_nat).
Proof.
induction 1.
{ case: (iter_step_literal_ord H) => [][]H1 H2.
left => //.
subst n'; right => //. }
case: IHiter_plus => [][]H1 H2.
{ case: (iter_step_literal_ord H) => [][]H3 H4.
{ left.
split => //.
omega. }
left.
split => //.
omega. }
case: (iter_step_literal_ord H) => [][]H3 H4.
{ right; split => //. omega. }
subst n''.
right; split => //.
Qed.
Lemma step_plus_iter_same_false n c s s' :
step_plus (CIter n c) s (CIter n c) s' -> False.