-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweightsextract.v
More file actions
1453 lines (1355 loc) · 46.1 KB
/
weightsextract.v
File metadata and controls
1453 lines (1355 loc) · 46.1 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 String.
Require Import ProofIrrelevance.
Require Import Coq.Logic.FunctionalExtensionality.
(*The computable state representation is an FMap over
player indices, represented as positive.*)
Require Import Coq.FSets.FMapAVL Coq.FSets.FMapFacts.
Require Import Structures.Orders NArith.
Require Import mathcomp.ssreflect.ssreflect.
From mathcomp Require Import all_ssreflect.
From mathcomp Require Import all_algebra.
Import GRing.Theory Num.Def Num.Theory.
Require Reals.
Require Import OUVerT.strings OUVerT.compile OUVerT.dist
OUVerT.numerics OUVerT.dyadic OUVerT.orderedtypes.
Require Import MWU.weights MWU.weightslang.
(** Move me to a numerics file **)
Lemma reduceReduce q : Qred (D_to_Q (Dred q)) = Qred (D_to_Q q).
Proof.
rewrite (Qred_complete (D_to_Q q) (D_to_Q (Dred q))) => //.
by rewrite Dred_correct.
Qed.
(** Here's a description of the compilation algorithm:
Source Language
Binary Arithmetic Operations
b ::= + | - | *
Expressions
e ::= q (* rationals *)
| -e (* arithmetic negation *)
| weight a (* weight of action [a] *)
| cost a (* cost of action [a] *)
| eps (* the epsilon parameter *)
| e b e (* binary operations *)
Commands
c ::= skip
| update f (* update weights by (f : A -> e) *)
| recv (* receive a cost vector from the environment *)
| send (* send an action ~ w / (\sum_(a : A) w a) *)
| c1; c2
| iter n c (* iterate c n-times *)
Target Language (over game type A):
The same as the source language, but with new semantics operating
over compilable states.
s := { (* Compilable: *)
cur_costs : M.t A Q (* the current cost vector, mapping actions to costs *)
; prev_costs : seq (M.t A Q)
; weights : M.t A Q (* the weights table, mapping actions to weights *)
; eps : Q (* the parameter \epsilon *)
(* Logical: *)
; outputs : seq (dist A rat_realFieldType) }.
We make a few assumptions about the game type A, in order to use
actions as keys in the maps [cur_costs] and [weights]. In particular,
that it has an order that satisfies: OrderedType.OrderedType.
*)
Definition zero : Q := 0.
Definition one : Q := 1.
Definition two : Q := Qmake 2 1.
(* The package provided by the client network oracle to MWU *)
Class ClientOracle {A} :=
mkOracle { T : Type (* oracle private state *)
; oracle_init_state : T
; oracle_chanty : Type
; oracle_bogus_chan : oracle_chanty
; oracle_prerecv : T -> oracle_chanty -> bool * T
; oracle_recv : T -> oracle_chanty -> (list (A*D) * T)
; oracle_presend : T -> list (A*D) -> bool
; oracle_send : T -> list (A*D) -> (oracle_chanty * T)
; oracle_recv_ok : forall st ch st',
oracle_prerecv st ch = (true, st') ->
forall a,
exists d,
[/\ In (a,d) (oracle_recv st' ch).1
, Dle (-D1) d & Dle d D1]
; oracle_recv_nodup : forall st ch st',
oracle_prerecv st ch = (true, st') ->
NoDupA (fun p q => p.1 = q.1) (oracle_recv st' ch).1
}.
(** * Program *)
Module MWUPre (A : MyOrderedType).
Module A' := OrderedType_of_MyOrderedType A.
Module M := Make A'.
Module MFacts := Facts M.
Module MProps := Properties M.
Definition cGamma (weights : M.t D) : Q :=
D_to_Q (M.fold (fun a q acc => Dadd q acc) weights (DD (Dmake 0 1))).
Section mwu.
Variable num_players : nat.
Context `{Showable A.t}.
Context `{Enumerable A.t}.
(** Assume an oracle over the provided GameType *)
Context `{oracle : ClientOracle A.t}.
Record cstate : Type :=
mkCState
{ SCosts : M.t D (* the current cost vector *)
; SPrevCosts : list (M.t D)
; SWeights : M.t D
; SEpsilon : D (* epsilon -- a parameter *)
(* the history of the generated distributions over actions *)
; SOutputs : list (A.t -> Q)
; SChan : oracle_chanty
; SOracleSt : T }.
(** Send weights to the network. *)
Definition mwu_send (m : M.t D) (oracle_st : T) : (oracle_chanty * T) :=
oracle_send oracle_st (M.elements m).
(* Receive a cost vector (a map) from the network. *)
Definition mwu_recv : oracle_chanty -> T -> (M.t D * T) :=
fun ch => fun st =>
let (l, st') := oracle_recv st ch in
let l':= print_Dvector l l in
(MProps.of_list l', st').
Definition eval_binopc (b : binop) (v1 v2 : D) : D :=
match b with
| BPlus => Dadd v1 v2
| BMinus => Dsub v1 v2
| BMult => Dmult v1 v2
end.
Fixpoint evalc (e : expr A.t) (s : cstate) : option D :=
match e with
| EVal v =>
match v with
| QVal q => Some q
end
| EOpp e' =>
match evalc e' s with
| Some v' => Some (Dopp v')
| None => None
end
| EWeight a => M.find a (SWeights s)
| ECost a => M.find a (SCosts s)
| EEps => Some (SEpsilon s)
| EBinop b e1 e2 =>
let: v1 := evalc e1 s in
let: v2 := evalc e2 s in
match v1, v2 with
| Some v1', Some v2' => Some (eval_binopc b v1' v2')
| _, _ => None
end
end.
(*NOTE: This code is much complicated by the fact that [evalc] can
fail -- otherwise, we could just use [M.mapi].*)
Definition update_weights
(f : A.t -> expr A.t) (s : cstate)
: option (M.t D) :=
M.fold
(fun a _ acc =>
match acc with
| None => None
| Some acc' =>
match evalc (f a) s with
| None => None
| Some q =>
if Dlt_bool D0 q then Some (M.add a (Dred q) acc')
else None
end
end)
(SWeights s)
(Some (M.empty D)).
Definition weights_distr (weights : M.t D) : A.t -> Q :=
fun a : A.t =>
match M.find a weights with
| None => 0
| Some d => D_to_Q d / cGamma weights
end.
Fixpoint interp (c : com A.t) (s : cstate) : option cstate :=
match c with
| CSkip => Some s
| CUpdate f =>
let w := update_weights f s
in match w with
| None => None
| Some w' =>
Some (mkCState
(SCosts s)
(SPrevCosts s)
w'
(SEpsilon s)
(SOutputs s)
(SChan s)
(SOracleSt s))
end
| CRecv =>
let (b, st') := oracle_prerecv (SOracleSt s) (SChan s) in
if b then
let (c, st'') := mwu_recv (SChan s) st'
in Some (mkCState
c
(SCosts s :: SPrevCosts s)
(SWeights s)
(SEpsilon s)
(SOutputs s)
(SChan s)
st'')
else None
| CSend =>
if oracle_presend (SOracleSt s) (M.elements (SWeights s))
then
let (ch, st') := mwu_send (SWeights s) (SOracleSt s) in
let d := weights_distr (SWeights s)
in Some (mkCState
(SCosts s)
(SPrevCosts s)
(SWeights s)
(SEpsilon s)
(d :: SOutputs s)
ch
st')
else None
| CSeq c1 c2 =>
match interp c1 s with
| None => None
| Some s' => interp c2 s'
end
| CIter n c =>
(*NOTE: We could further short-circuit this iteration -- in practice,
it shouldn't matter for performance since [interp] should never
fail on MWU, starting in appropriate initial state.*)
N.iter
n
(fun s =>
match s with
| None => None
| Some s' => interp c s'
end)
(Some s)
end.
Section init.
Context `{Enumerable A.t}.
Definition init_map : M.t D :=
MProps.of_list (List.map (fun a => (a, D1)) (enumerate A.t)).
Definition init_cstate (epsQ : D) :=
@mkCState
init_map (** The initial cost function is never used --
we only include it because the type [state] forces
an [SCost] projection. *)
[::]
init_map
epsQ
[::]
oracle_bogus_chan
oracle_init_state.
End init.
(* Context `{Enumerable A.t}. *)
Definition mwu (eps : D) (nx : N.t) : option cstate :=
interp (mult_weights A.t nx) (init_cstate eps).
End mwu.
End MWUPre.
(* Note [MWU_Type]:
~~~~~~~~~~~~~~~~
The following module type is used in:
- wenetwork.v
- we2wl.v
to unify the module instantiations of MWU built in the MWUProof
functor (below) and the WE_NodePkg functor (defined in we2wl.v). *)
Module Type MWU_Type.
Declare Module A : MyOrderedType.
Module MWUPre := MWUPre A.
Include MWUPre.
End MWU_Type.
Module MWU (A : MyOrderedType) <: MWU_Type.
Module A := A.
Module MWUPre := MWUPre A. Include MWUPre.
End MWU.
Module MWUProof (T : MyOrderedType) (MWU : MWU_Type with Module A := T).
Module A := T.
Module M := MWU.M.
Module MFacts := Facts M.
Module MProps := Properties M.
Import MWU.
Section OrderedFinType_Section.
Variables
(eq_mixin : Equality.mixin_of A.t)
(choice_mixin : choiceMixin (EqType A.t eq_mixin))
(fin_mixin : Finite.mixin_of (ChoiceType (EqType A.t eq_mixin) choice_mixin)).
Definition t : finType :=
FinType (ChoiceType (EqType A.t eq_mixin) choice_mixin) fin_mixin.
Lemma InA_ext A (P Q : A -> A -> Prop) l x :
(forall a b, P a b <-> Q a b) ->
InA P x l <-> InA Q x l.
Proof.
move => H; elim: l.
{ split; inversion 1. }
move => a l IH; split; inversion 1; subst.
{ by constructor; rewrite -H. }
{ by apply: InA_cons_tl; rewrite -IH. }
{ by constructor; rewrite H. }
by apply: InA_cons_tl; rewrite IH.
Qed.
Lemma NoDupA_ext A (P Q : A -> A -> Prop) l :
(forall a b, P a b <-> Q a b) ->
NoDupA P l <-> NoDupA Q l.
Proof.
elim: l => // a l IH H; split; inversion 1; subst.
{ constructor.
move => H5; apply: H3; rewrite InA_ext => //.
apply: H5.
by rewrite -IH. }
constructor => //.
move => H5; apply: H3; rewrite InA_ext => //.
apply: H5.
by move => ax b; rewrite -H.
rewrite IH => //.
Qed.
Section mwuProof.
Variable num_players : nat.
Context `{showable_instance : Showable A.t}.
Context `{enumerable_instance : Enumerable A.t}.
(** Assume an oracle over the provided GameType *)
Context `{coracle : ClientOracle A.t}.
(** An alias used below: *)
Definition oracle_cT : Type := (T (ClientOracle := coracle)).
Lemma recv_ok :
forall st ch st', oracle_prerecv st ch = (true, st') ->
forall a,
exists d,
[/\ M.find a (mwu_recv ch st').1 = Some d
, Dle_bool (-D1) d & Dle_bool d D1].
Proof.
rewrite /mwu_recv.
move => st ch st' Hpre a'.
have H: NoDupA (M.eq_key (elt:=D)) (oracle_recv st' ch).1.
{ generalize (oracle_recv_nodup (ClientOracle:=coracle) Hpre) => H.
rewrite NoDupA_ext; first by apply: H.
rewrite /M.eq_key /M.Raw.Proofs.PX.eqk => a b.
by rewrite -A.eqP. }
move: a' => a.
case: (oracle_recv_ok Hpre a) => q []H2 H3.
exists q; split => //.
2: { by rewrite <-Dle_bool_iff in H3; rewrite H3. }
2: { by rewrite <-Dle_bool_iff in p; rewrite p. }
destruct (oracle_recv st' ch).
rewrite MProps.of_list_1b => //.
move: H H2 {H3 p}; rewrite print_Dvector_id;
generalize (oracle_recv (ClientOracle:=coracle) st ch) a q.
move=> _ /=. move: l.
elim => // [][]a' q' l' IH a0 q0; inversion 1; subst; case.
{ case => -> -> /=.
have ->: MProps.F.eqb a0 a0 = true.
{ rewrite /MProps.F.eqb.
case: (MProps.F.eq_dec a0 a0) => //.
by rewrite -A.eqP. }
by []. }
simpl.
case H4: (MProps.F.eqb a0 a').
{ move => H5.
rewrite /MProps.F.eqb in H4.
move: H4.
case: (MProps.F.eq_dec a0 a') => //.
rewrite -A.eqP => Hx; subst a'.
clear - H2 H5.
elim: l' H2 H5 => // a1 l IH H6 H7.
destruct H7.
{ inversion H; subst. clear H0.
elimtype False.
apply: H6.
by left; rewrite /M.eq_key /M.Raw.Proofs.PX.eqk /= -A.eqP. }
move => _; apply: IH => //.
by move => H7; apply: H6; right. }
by move => H5; apply: (IH _ _ H3 H5).
by rewrite print_Dvector_id.
Qed.
Definition match_maps
(s : {ffun t -> rat})
(m : M.t D) : Prop :=
forall a,
exists q, M.find a m = Some q /\ Qred (D_to_Q q) = rat_to_Q (s a).
Definition match_costs
(s : {c : {ffun t -> rat} & forall a : t, (`|c a| <= 1)%R})
(m : M.t D) : Prop :=
match_maps (projT1 s) m.
Inductive match_costs_seq :
seq {c : {ffun t -> rat} & forall a : t, (`|c a| <= 1)%R} ->
list (M.t D) ->
Prop :=
| match_costs_nil :
match_costs_seq nil nil
| match_costs_cons :
forall s ss m mm,
match_costs s m ->
match_costs_seq ss mm ->
match_costs_seq [:: s & ss] [:: m & mm].
Inductive match_distrs :
seq (dist t rat_realFieldType) ->
seq (t -> Q) ->
Prop :=
| match_distrs_nil :
match_distrs nil nil
| match_distrs_cons :
forall d f l l',
pmf d = finfun (fun a => Q_to_rat (f a)) ->
match_distrs l l' ->
match_distrs [:: d & l] [:: f & l'].
(** The high-level oracle *)
Context oracle_T `{oracle: weightslang.ClientOracle t oracle_T oracle_chanty}.
Notation "'state' t" := (@state t oracle_T oracle_chanty) (at level 50).
(** and its match relation *)
Variable match_oracle_states : oracle_T -> oracle_cT -> Prop.
(** The oracular compilation interface *)
Class match_oracles : Prop :=
mkMatchOracles {
match_oracle_recv : forall (ct ct' : oracle_cT) (t : oracle_T) s ch,
match_oracle_states t ct ->
oracle_prerecv ct ch = (true, ct') ->
let: (m, ct'') := mwu_recv ch ct' in
match_maps s m ->
exists t',
[/\ weightslang.oracle_recv t ch s t'
& match_oracle_states t' ct'']
; match_oracle_send :
forall (ct : oracle_cT) (tx : oracle_T) m
(s : {ffun t -> rat}) (s_ok : forall a : t, (0 < s a)%R)
a0 eps (eps_ok : (0 < eps <= 1 / 2%:R)%R),
match_oracle_states tx ct ->
match_maps s m ->
oracle_presend ct (MProps.to_list m) = true ->
let: (ch, ct') := mwu_send m ct in
let: d := p_aux_dist a0 eps_ok s_ok (cs:=[::]) (CMAX_nil (A:=t)) in
exists t',
[/\ weightslang.oracle_send tx d ch t'
& match_oracle_states t' ct' ]
}.
Context (Hmatch_ora : match_oracles).
Inductive match_states : state t -> cstate -> Prop :=
| mkMatchStates :
forall s m s_ok ss mm w w_ok wc eps eps_ok epsc outs outs' ch
oracle_st coracle_st,
match_maps s m ->
match_costs_seq ss mm ->
match_maps w wc ->
rat_to_Q eps = Qred (D_to_Q epsc) ->
match_distrs outs outs' ->
match_oracle_states oracle_st coracle_st ->
match_states
(@mkState _ _ _ s s_ok ss w w_ok eps eps_ok outs ch oracle_st)
(@mkCState _ m mm wc epsc outs' ch coracle_st).
Definition eval_binopc (b : binop) (v1 v2 : D) :=
match b with
| BPlus => Dadd v1 v2
| BMinus => Dsub v1 v2
| BMult => Dmult v1 v2
end.
Fixpoint evalc (e : expr t) (s : cstate) : option D :=
match e with
| EVal v =>
match v with
| QVal q => Some q
end
| EOpp e' =>
match evalc e' s with
| Some v' => Some (Dopp v')
| None => None
end
| EWeight a => M.find a (SWeights s)
| ECost a => M.find a (SCosts s)
| EEps => Some (SEpsilon
(oracle:=coracle)
s)
| EBinop b e1 e2 =>
let: v1 := evalc e1 s in
let: v2 := evalc e2 s in
match v1, v2 with
| Some v1', Some v2' => Some (eval_binopc b v1' v2')
| _, _ => None
end
end.
Fixpoint cGamma'_aux (l : list (M.key * D)) :=
match l with
| nil => D0
| a :: l' => Dadd a.2 (cGamma'_aux l')
end.
Lemma cGamma_cGamma'_aux l :
fold_right
(fun y : M.key * D => [eta Dadd y.2])
D0
l =
cGamma'_aux l.
Proof. elim: l => // a l IH /=; rewrite IH. Qed.
Definition cGamma' m :=
D_to_Q (cGamma'_aux (List.rev (M.elements m))).
Lemma cGamma_cGamma' m :
cGamma m = cGamma' m.
Proof. by rewrite /cGamma /cGamma' M.fold_1 -fold_left_rev_right. Qed.
Definition gamma' (l : seq t) (s : {ffun t -> rat}) : rat :=
\sum_(a <- l) (s a)%R.
Lemma gamma'_cons x l s :
(gamma' (x :: l) s = s x + gamma' l s)%R.
Proof. by rewrite /gamma' big_cons. Qed.
Lemma gamma_gamma' w : gamma' (index_enum t) w = gamma w.
Proof. by []. Qed.
Lemma match_maps_gamma_cGamma'_aux
(s : {ffun t -> rat})
(l : list (M.key * D)) :
(forall a q, In (a, q) l -> rat_to_Q (s a) = Qred (D_to_Q q)) ->
gamma' (List.map (fun x => x.1) l) s = Q_to_rat (D_to_Q (cGamma'_aux l)).
Proof.
elim: l s => //.
{ move => s /= IH; rewrite /gamma' /= big_nil.
rewrite DO_to_Q0' /Q_to_rat /= fracqE /=.
by rewrite GRing.mul0r. }
case => a q l IH s /= H.
symmetry.
apply: rat_to_QK2.
rewrite gamma'_cons Dadd_ok IH.
{ have ->: s a = Q_to_rat (D_to_Q q).
{ move: (H _ _ (or_introl erefl)) => H2.
rewrite (rat_to_QK2 (r:=s a)) => //.
by rewrite -(Qred_correct (D_to_Q q)) H2. }
rewrite rat_to_Q_plus 2!rat_to_QK1.
by rewrite 2!Qred_correct. }
move => ax qx H2.
by apply: (H _ _ (or_intror H2)).
Qed.
Lemma InA_notin a (l : seq t) : ~InA A.eq a l -> a \notin l.
Proof.
elim: l a => // a l IH ax H; rewrite /in_mem /=; apply/negP; case/orP.
{ by move/eqP => H2; subst ax; apply: H; left; rewrite -A.eqP. }
move => H2; apply: H; right.
case: (InA_dec A.eq_dec ax l) => // H3; move: (IH _ H3).
by rewrite /in_mem /= H2.
Qed.
Lemma notin_InA a (l : seq t) : a \notin l -> ~InA A.eq a l.
Proof.
elim: l a => //.
{ move => a _; inversion 1. }
move => a l IH ax H; rewrite /in_mem /=; inversion 1; subst.
{ move: H2; rewrite -A.eqP => H3; subst ax; clear H0.
by rewrite in_cons in H; move: (negP H); apply; apply/orP; left. }
apply: IH; last by apply: H2.
by move: (negP H) => H3; apply/negP => H4; apply: H3; apply/orP; right.
Qed.
Lemma InA_not_InA_eq x y (l : seq t) : InA A.eq x l -> ~InA A.eq y l -> x<>y.
Proof.
elim: l; first by inversion 1.
move => a l IH.
inversion 1; subst; clear H.
{ move => H2 H3; subst y.
by apply: H2; left. }
move => H2 H3; subst x.
by apply: H2; right.
Qed.
Lemma InA_map A B (Aeq : A -> A -> Prop) (Beq : B -> B -> Prop) x l (f : A -> B) :
(forall x y, Aeq x y -> Beq (f x) (f y)) ->
InA Aeq x l ->
InA Beq (f x) (List.map f l).
Proof.
move => H; elim: l; first by inversion 1.
move => a l IH.
inversion 1; subst; clear H0.
{ by left; apply: H. }
by simpl; right; apply: (IH H2).
Qed.
Lemma InA_map' A B (Aeq : A -> A -> Prop) (Beq : B -> B -> Prop) x l (f : A -> B) :
(forall x y, Beq (f x) (f y) -> Aeq x y) ->
InA Beq (f x) (List.map f l) ->
InA Aeq x l.
Proof.
move => H; elim: l; first by inversion 1.
move => a l IH.
inversion 1; subst; clear H0.
{ by left; apply: H. }
by simpl; right; apply: (IH H2).
Qed.
Lemma NoDupA_map A B (Aeq : A -> A -> Prop) (Beq : B -> B -> Prop) l (f : A -> B) :
(forall x y, Beq (f x) (f y) -> Aeq x y) ->
NoDupA Aeq l ->
NoDupA Beq (List.map f l).
Proof.
move => H; induction 1; first by constructor.
simpl; constructor => // H2; apply: H0.
by apply: (InA_map' H).
Qed.
Lemma match_maps_find1 T (x : t) m q :
M.find (elt:=T) x m = Some q ->
(count_mem x) (List.map [eta fst] (List.rev (M.elements (elt:=T) m))) = 1%N.
Proof.
move: (M.elements_3w m); move/NoDupA_rev => H H2.
have H3: InA A.eq x (List.map [eta fst] (List.rev (M.elements (elt:=T) m))).
{ have H3: M.find (elt:=T) x m <> None.
{ move => H3; rewrite H3 in H2; congruence. }
clear H2; move: H3; rewrite -MProps.F.in_find_iff MProps.F.elements_in_iff.
case => e; move: (M.elements _) => l; clear H => H.
have ->: x = fst (x, e) by [].
have H2: InA (M.eq_key_elt (elt:=T)) (x, e) (List.rev l).
{ by rewrite InA_rev. }
have H3: forall x y : M.key * T, M.eq_key_elt x y -> A.eq x.1 y.1.
{ by case => x1 x2; case => y1 y2; case. }
by apply (InA_map H3 H2). }
have H4: NoDupA A.eq (List.map [eta fst] (List.rev (M.elements (elt:=T) m))).
{ move: (H2); rewrite -MProps.F.find_mapsto_iff MProps.F.elements_mapsto_iff => H4.
clear - H; apply: (NoDupA_map _ H); case => x1 x2; case => y1 y2 //. }
clear H.
move: H3 H4; move: (List.map _ _) => l.
clear H2 m q.
elim: l => //.
{ inversion 1. }
move => a l' IH; inversion 1; subst.
{ clear H3.
inversion 1; subst.
simpl.
move: H0; rewrite -A.eqP => ->.
have [a' Hx]: exists a' : t, a = a'.
{ by exists a. }
have [l'' Hl]: exists l'' : seq t, l' = l''.
{ by exists l'. }
subst a l'.
have ->: (count_mem a') l'' = 0%N.
{ have H5: a' \notin l'' by apply: InA_notin.
apply: (count_memPn H5). }
by rewrite addn0 eq_refl. }
inversion 1; subst => /=.
have [a' Hx]: exists a' : t, a = a'. { by exists a. }
have [l'' Hl]: exists l'' : seq t, l' = l''. { by exists l'. } subst a l'.
have ->: a' == x = false.
{ move: (InA_not_InA_eq H0 H2) => H6.
case H7: (a' == x) => //.
move: (eqP H7) => H8; subst x; contradiction. }
rewrite IH => //.
Qed.
Lemma match_maps_enum_count_mem s m :
match_maps s m ->
forall x : t,
(count_mem x) (index_enum t) =
(count_mem x) (List.map [eta fst] (List.rev (M.elements (elt:=D) m))).
Proof.
move => H x; case: (H x) => q []H1 H2; move {H}; rewrite (@enumP t x).
by rewrite (match_maps_find1 H1).
Qed.
Lemma match_maps_enum_perm_eq s m :
match_maps s m ->
perm_eq (index_enum t)
(List.map [eta fst] (List.rev (M.elements m))).
Proof.
move => H; rewrite /perm_eq; apply/allP => x.
by rewrite mem_cat; case/orP => /= H2;
apply/eqP; apply: match_maps_enum_count_mem.
Qed.
Lemma match_maps_gamma'_elements s m :
match_maps s m ->
gamma' (index_enum t) s =
gamma' (List.map [eta fst] (List.rev (M.elements m))) s.
Proof.
rewrite /gamma' /match_maps => H; apply: eq_big_perm.
by apply: (match_maps_enum_perm_eq H).
Qed.
Lemma match_maps_gamma_cGamma s m :
match_maps s m ->
gamma s = Q_to_rat (cGamma m).
Proof.
rewrite cGamma_cGamma' -(gamma_gamma' s).
move => H; rewrite -(match_maps_gamma_cGamma'_aux (s:=s)).
{ apply: match_maps_gamma'_elements => //. }
move => a q H2.
case: (H a) => q' []H3 H4.
have H5: In (a,q) (M.elements m).
{ by rewrite in_rev. }
clear H2; have ->: q = q'.
{ move: H3; rewrite -MProps.F.find_mapsto_iff => H3.
have H6: InA (M.eq_key_elt (elt:=D)) (a, q) (M.elements m).
{ apply: In_InA => //. }
move: H6; rewrite -MProps.F.elements_mapsto_iff => H6.
apply: MProps.F.MapsTo_fun; first by apply: H6.
apply: H3. }
by rewrite H4.
Qed.
Fixpoint update_weights'_aux
(f : t -> expr t) (s : cstate) w l
: option (M.t D) :=
match l with
| nil => Some w
| a :: l' =>
match evalc (f a) s with
| None => None
| Some q =>
if Dlt_bool D0 q then
match (update_weights'_aux f s w l') with
| None => None
| Some m => Some (M.add a (Dred q) m)
end
else None
end
end.
Lemma update_weights'_aux_app f s w l1 l2 :
update_weights'_aux f s w (l1 ++ l2) =
match update_weights'_aux f s w l2 with
| None => None
| Some w' => update_weights'_aux f s w' l1
end.
Proof.
elim: l1 l2 w => //=.
{ move => l2 w; case: (update_weights'_aux _ _ _ _) => //. }
move => a l IH l2 w; move: IH.
case H2: (update_weights'_aux _ _ _ l2) => [w'|].
{ move => IH.
case: (evalc (f a) s) => // x.
case: (Dlt_bool D0 x) => //.
by rewrite IH H2. }
move => ->; rewrite H2.
case: (evalc (f a) s) => //.
move => a0; case: (Dlt_bool D0 a0) => //.
Qed.
Definition update_weights'
(f : t -> expr t) (s : cstate)
: option (M.t D) :=
update_weights'_aux f s (M.empty D)
(List.map (fun x => x.1) (List.rev (M.elements (SWeights s)))).
Lemma update_weights'_aux_inv f s m l m' :
update_weights'_aux f s m l = Some m' ->
forall a,
In a l ->
exists q,
[/\ M.find a m' = Some (Dred q)
, evalc (f a) s = Some q
& Dlt D0 q].
Proof.
elim: l m m' => // a l IH m m' /=.
case H: (evalc _ _) => // [q].
case H2: (Dlt_bool D0 q) => //.
case H3: (update_weights'_aux _ _ _ _) => // [m''] H4 a' H5.
case: H5.
{ move => H6; subst a'; inversion H4; subst.
rewrite MProps.F.add_eq_o.
{ exists q.
split => //.
by rewrite -Dlt_bool_iff. }
rewrite /A.eq.
by rewrite -A.eqP.
}
move => H5.
case: (IH _ _ H3 _ H5) => q' []H6 H7 H8.
case: (A.eq_dec a a').
{ rewrite -A.eqP => H9. subst a'.
exists q; split => //.
inversion H4; subst.
rewrite MProps.F.add_eq_o; last by rewrite /A.eq -A.eqP.
by [].
by rewrite -Dlt_bool_iff.
}
move => H9.
exists q'.
split => //.
inversion H4; subst.
rewrite MProps.F.add_neq_o => //.
Qed.
Lemma update_weights'_inv1 f s m :
(forall a, exists q, M.find a (SWeights s) = Some q) ->
update_weights' f s = Some m ->
forall a,
exists q,
[/\ M.find a m = Some (Dred q)
, evalc (f a) s = Some q
& Dlt D0 q].
Proof.
rewrite /update_weights' => H H2 a.
have H3: In a (List.map [eta fst] (List.rev (M.elements (SWeights s)))).
{ clear - H.
case: (H a) => q H2.
have H3: M.find a (SWeights s) <> None.
{ move => H4; rewrite H4 in H2; congruence. }
move: H3; rewrite -MProps.F.in_find_iff MProps.F.elements_in_iff.
case => q'; move: (M.elements _) => l.
elim: l => //=.
{ inversion 1. }
case => []a' b l IH /=.
inversion 1; subst.
{ destruct H1; simpl in *; subst.
move: H0; rewrite /A.eq -A.eqP => H3; subst a'.
have ->: a = (a, b).1 by [].
apply: in_map.
apply: in_or_app.
right.
left => //. }
move: (IH H1).
rewrite in_map_iff; case; case => a'' b' /=; case => -> H3.
have ->: a = (a, b').1 by [].
apply: in_map.
by apply: in_or_app; left. }
apply: (update_weights'_aux_inv H2 H3).
Qed.
Lemma update_weights_weights'_aux f s l w :
fold_right
(fun (y : M.key * D) (x : option (M.t D)) =>
match x with
| Some acc' =>
match evalc (f y.1) s with
| Some q =>
if Dlt_bool D0 q then Some (M.add y.1 (Dred q) acc')
else None
| None => None
end
| None => None
end) (Some w) l =
update_weights'_aux f s w (List.map [eta fst] l).
Proof.
move: w; elim: l => // [][]a b l IH.
move => w /=; rewrite IH.
case: (update_weights'_aux _ _ _ _) => //.
case: (evalc _ _) => // q''.
case: (Dlt_bool D0 q'') => //.
Qed.
Lemma update_weights_weights' f s :
update_weights f s = update_weights' f s.
Proof.
rewrite /update_weights /update_weights' M.fold_1 -fold_left_rev_right.
apply: update_weights_weights'_aux.
Qed.
Lemma update_weights_inv1 f s m :
(forall a, exists q, M.find a (SWeights s) = Some q) ->
update_weights f s = Some m ->
forall a,
exists q,
[/\ M.find a m = Some (Dred q)
, evalc (f a) s = Some q
& Dlt D0 q].
Proof.
rewrite update_weights_weights'.
apply: update_weights'_inv1.
Qed.
Lemma match_eval f (r : state t) (s : cstate) q :
match_maps (weightslang.SWeights r) (SWeights s) ->
match_maps (weightslang.SCosts r) (SCosts s) ->
rat_to_Q (weightslang.SEpsilon r) = Qred (D_to_Q (SEpsilon s)) ->
forall a : t,
evalc (f a) s = Some q ->
Qred (D_to_Q q) = rat_to_Q (eval (f a) r).
Proof.
move => H Hx Hy a; move: (f a) => e.
elim: e q.
{ move => v q /=.
case: v => // q'.
inversion 1; subst.
by rewrite rat_to_QK1. }
{ move => e IH q.
unfold evalc; fold evalc.
case H2: (evalc e s) => // [q']; inversion 1; subst.
rewrite Dopp_ok Qred_opp IH => //.
by rewrite rat_to_Qopp. }
{ move => a' q H2.
simpl in H2.
case: (H a') => q' []H3 H4.
rewrite H3 in H2; inversion H2; subst. clear H2.
by rewrite H4. }
{ move => a' q H2.
simpl in H2.
case: (Hx a') => q' []H3 H4.
rewrite H3 in H2; inversion H2; subst. clear H2.
by rewrite H4. }
{ move => q /=; inversion 1; subst.
by rewrite Hy. }
move => b e1 IH1 e2 IH2 q.
rewrite /evalc -/evalc.
case H1: (evalc e1 s) => // [v1].
case H2: (evalc e2 s) => // [v2].
inversion 1; subst. clear H0.
(*case analysis on the binary operations*)
case: b; rewrite /eval_binopc /eval_binop.
{ rewrite rat_to_Q_red; apply: Qred_complete; rewrite rat_to_Q_plus.
rewrite -(IH1 _ H1).
rewrite -(IH2 _ H2).
by rewrite 2!Qred_correct Dadd_ok. }
{ rewrite rat_to_Q_red; apply: Qred_complete; rewrite rat_to_Q_plus.
rewrite -(IH1 _ H1).
rewrite rat_to_Qopp.
rewrite -(IH2 _ H2).
by rewrite 2!Qred_correct Dadd_ok Dopp_ok. }
rewrite rat_to_Q_red; apply: Qred_complete; rewrite rat_to_Q_mul.
rewrite -(IH1 _ H1).
rewrite -(IH2 _ H2).
by rewrite 2!Qred_correct Dmult_ok.
Qed.
Lemma update_weights_inv2 (f : t -> expr t) r s m :
match_states r s ->
update_weights f s = Some m ->
forall a : t,
exists q,
[/\ M.find a m = Some (Dred q)
, evalc (f a) s = Some q
, Qred (D_to_Q q) = rat_to_Q (eval (f a) r)
& Qlt 0 (D_to_Q q)].
Proof.
move => H H2 a.
move: (@update_weights_inv1 f s m) => Hinv1.
move: (@match_eval f r s) => Hmatch.
case: s H H2 Hinv1 Hmatch; intros.
inversion H; subst.
have H3: forall a, exists q, M.find a SWeights0 = Some q.
{ move => a'; case: (H11 a') => q []H3 H4; exists q => //. }
case: (Hinv1 H3 H2 a) => q []H1 H4 H5.
exists q; split => //.
apply: Hmatch => //.
rewrite /Dlt in H5.
clear - H5. move: H5.
by rewrite D_to_Q0.
Qed.
Lemma match_maps_update_weights (f : t -> expr t) r s m :
match_states r s ->
update_weights f s = Some m ->
match_maps [ffun a => eval (f a) r] m /\
(forall a, 0 < eval (f a) r)%R.
Proof.
move => H H2; split => a; case: (update_weights_inv2 H H2 a) => q.
{ case => H3 H4 H5 H6.
exists (Dred q); split => //.
rewrite reduceReduce.
by rewrite H5 ffunE. }
case => H3 H4 H5 H6.
have H7: 0 < Qred (D_to_Q q) by rewrite Qred_correct.
rewrite H5 in H7; clear - H7.
have H6: 0 = inject_Z 0 by [].
rewrite H6 -rat_to_Q0 in H7.
by apply: rat_to_Q_lt'.
Qed.
Variable a0 : t.
Lemma interp_step_plus :
forall (s : state t) (tx tx' : cstate) (c : com t),