-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDPSS_A_rel.v
3083 lines (2773 loc) · 103 KB
/
DPSS_A_rel.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
(**
This file provides a relational description of an
environment executing DPSS -- Algorithm A.
Author: Sam Merten
**)
Require Import QArith Qminmax Qabs QOrderedType.
Require Import Omega Nsatz.
Require Import Setoid List SetoidList Permutation SetoidPermutation.
Require Import Coq.Classes.RelationClasses.
Require Import ProofIrrelevance.
Require Import ExtendedEqualities.
Require Import DPSS_Primitives DPSS.DPSS_Numerics.
Existing Instance Qeq_opt_rel.
(** Definition of agents acting in the environment **)
Record simAgent : Type :=
mkRealAgent {
sHeading : Direction; (* The direction of the agents movement *)
sLocation : Q; (* The agent's current location *)
sDestination : option Q; (* Where the agent ends its escort *)
sAgentID : nat;
}.
(** Begin:
setoid equality for simAgents and morphisms for record fields **)
Inductive simAgentEQ : simAgent -> simAgent -> Prop :=
mkSA_EQ :
forall a1 a2, sHeading a1 = sHeading a2 ->
sLocation a1 == sLocation a2 ->
sAgentID a1 = sAgentID a2 ->
Qeq_opt (sDestination a1) (sDestination a2) ->
simAgentEQ a1 a2.
Lemma simAgentEQ_refl : reflexive _ simAgentEQ.
Proof. constructor; firstorder. Qed.
Lemma simAgentEQ_symm : symmetric _ simAgentEQ.
Proof. constructor; inversion H; firstorder. Qed.
Lemma simAgentEQ_trans :transitive _ simAgentEQ.
Proof.
constructor; inversion H; inversion H0;
etransitivity; eauto.
Qed.
Lemma simAgentEQ_equiv : Equivalence simAgentEQ.
Proof.
constructor.
- apply simAgentEQ_refl.
- apply simAgentEQ_symm.
- apply simAgentEQ_trans.
Qed.
Add Parametric Relation : (simAgent) (simAgentEQ)
reflexivity proved by simAgentEQ_refl
symmetry proved by simAgentEQ_symm
transitivity proved by simAgentEQ_trans
as simAgentEQ_rel.
Add Parametric Morphism : sHeading with
signature simAgentEQ ==> eq as sHeading_mor.
Proof.
intros. inversion H. auto.
Qed.
Add Parametric Morphism : sLocation with
signature simAgentEQ ==> Qeq as sLocation_mor.
Proof.
intros. inversion H. auto.
Qed.
Add Parametric Morphism : sDestination with
signature simAgentEQ ==> Qeq_opt as sDestination_mor.
Proof.
intros. inversion H. firstorder.
Qed.
Add Parametric Morphism : sAgentID with
signature simAgentEQ ==> eq as sAgentID_mor.
Proof.
intros. inversion H. auto.
Qed.
(** End:
setoid equality for simAgents and morphisms for record fields **)
(** A definition of the environment in the simulation **)
Inductive simEnv :=
mkRealEnv {
sNumAgents : nat;
sAgents : forall (n : nat), (n < sNumAgents)%nat -> simAgent;
sPerimLength : Q;
sSpeed : Q;
sTime : Q
}.
(** Begin:
setoid equality for simEnvs and morphisms for record fields **)
Inductive simEnvEQ : simEnv -> simEnv -> Prop :=
mkSE_EQ :
forall e1 e2, sNumAgents e1 = sNumAgents e2 ->
(forall n pf1 pf2,
simAgentEQ (sAgents e1 n pf1) (sAgents e2 n pf2)) ->
sPerimLength e1 == sPerimLength e2 ->
sSpeed e1 == sSpeed e2 ->
sTime e1 == sTime e2 ->
simEnvEQ e1 e2.
Lemma simEnvEQ_refl : reflexive _ simEnvEQ.
Proof.
constructor; intros; try (replace pf1 with pf2); firstorder.
apply proof_irrelevance.
Qed.
Lemma simEnvEQ_symm : symmetric _ simEnvEQ.
Proof.
constructor; inversion H; intros; firstorder.
symmetry. apply H1.
Qed.
Lemma simEnvEQ_trans : transitive _ simEnvEQ.
Proof.
constructor; inversion H; inversion H0; intros; firstorder;
etransitivity; eauto. Unshelve. firstorder.
Qed.
Lemma simEnvEQ_equiv : Equivalence simEnvEQ.
Proof.
constructor.
- apply simEnvEQ_refl.
- apply simEnvEQ_symm.
- apply simEnvEQ_trans.
Qed.
Add Parametric Relation : (simEnv) (simEnvEQ)
reflexivity proved by simEnvEQ_refl
symmetry proved by simEnvEQ_symm
transitivity proved by simEnvEQ_trans
as simEnvEQ_rel.
Add Parametric Morphism : sNumAgents with
signature simEnvEQ ==> eq as sNumAgents_mor.
Proof. intros. inversion H. firstorder. Qed.
Add Parametric Morphism : sPerimLength with
signature simEnvEQ ==> Qeq as sPerimLenght_mor.
Proof. intros. inversion H. firstorder. Qed.
Add Parametric Morphism : sSpeed with
signature simEnvEQ ==> Qeq as sSpeed_mor.
Proof. intros. inversion H. firstorder. Qed.
Add Parametric Morphism : sTime with
signature simEnvEQ ==> Qeq as sTime_mor.
Proof. intros. inversion H. firstorder. Qed.
(*
Because of the dependecy of the proof term in sAgents,
building a morphism for this term is obnoxious. Instead,
we'll just do the rewriting manually.
*)
Lemma sAgents_mor :
forall e1 e2 n1 n2 pf1 pf2,
simEnvEQ e1 e2 ->
n1 = n2 ->
simAgentEQ (sAgents e1 n1 pf1) (sAgents e2 n2 pf2).
Proof. intros. subst. inversion H; subst. apply H1. Qed.
(** End:
setoid equality for simEnvs and morphisms for record fields **)
(* The perimeter boundries for agents *)
Definition agentUpperPatrolRange
(n : nat) (E : simEnv) (pf : (n < sNumAgents E)%nat) : Q :=
(1 + (Q_of_nat n)) * (sPerimLength E) / (Q_of_nat (sNumAgents E)).
Definition agentLowerPatrolRange
(n : nat) (E : simEnv) (pf : (n < sNumAgents E)%nat) : Q :=
((Q_of_nat n)) * (sPerimLength E) / (Q_of_nat (sNumAgents E)).
Lemma agentPatrolRange_order :
forall n E pf1 pf2,
0 < sPerimLength E ->
0 < Q_of_nat (sNumAgents E) ->
agentLowerPatrolRange n E pf1 < agentUpperPatrolRange n E pf2.
Proof.
intros. unfold agentLowerPatrolRange, agentUpperPatrolRange.
apply Qlt_shift_div_l. auto. field_simplify.
rewrite Qlt_minus_iff. field_simplify in H. field_simplify. auto.
- intros H1. rewrite H1 in H0. inversion H0.
Qed.
Program Definition agentPatrolRange
n E pf
(pf_perimlength : 0 < sPerimLength E)
(pf_agentnum : 0 < Q_of_nat (sNumAgents E))
: QInterval :=
mkInterval (agentUpperPatrolRange n E pf)
(agentLowerPatrolRange n E pf)
_.
Next Obligation. apply Qlt_le_weak. apply agentPatrolRange_order; auto. Qed.
(* Again, the propositions make this obnoxious to do automatically *)
Lemma agentUpperPatrolRange_mor :
forall e1 e2 n1 n2 pf1 pf2,
simEnvEQ e1 e2 ->
(n1 = n2) ->
Qeq (agentUpperPatrolRange n1 e1 pf1) (agentUpperPatrolRange n2 e2 pf2).
Proof.
intros. unfold agentUpperPatrolRange. rewrite H, H0. firstorder.
Qed.
Lemma agentLowerPatrolRange_mor :
forall e1 e2 n1 n2 pf1 pf2,
simEnvEQ e1 e2 ->
(n1 = n2) ->
Qeq (agentLowerPatrolRange n1 e1 pf1) (agentLowerPatrolRange n2 e2 pf2).
Proof.
intros. unfold agentLowerPatrolRange. rewrite H, H0. firstorder.
Qed.
(** Properties of a valid environment **)
Inductive sNumAgents_Pos_inv : simEnv -> Prop :=
| mkNumAgents_Pos_inv :
forall E, (O < sNumAgents E)%nat -> sNumAgents_Pos_inv E.
Inductive sPerimLength_Pos_inv : simEnv -> Prop :=
| mkPerimLength_Pos_inv :
forall E, (0 < sPerimLength E) -> sPerimLength_Pos_inv E.
Inductive sSpeed_Positive_inv : simEnv -> Prop :=
| mkSpeed_Positive_inv :
forall E, 0 < sSpeed E -> sSpeed_Positive_inv E.
Inductive sAgents_InPerim_inv : simEnv -> Prop :=
| mkAgents_InPerim_inv :
forall E,
(forall n (pf : (n < sNumAgents E)%nat),
0 <= (sLocation (sAgents E n pf)) <= sPerimLength E) ->
sAgents_InPerim_inv E.
Inductive sAgents_Ordered_inv : simEnv -> Prop :=
| mkAgents_Ordered_inv :
forall E,
( forall n1 n2
(pf1 : (n1 < sNumAgents E)%nat)
(pf2 : (n2 < sNumAgents E)%nat)
(pf3 : (n1 < n2)%nat),
(sLocation (sAgents E n1 pf1)) <= (sLocation (sAgents E n2 pf2))) ->
sAgents_Ordered_inv E.
Inductive sAgents_heading_towards_dest_inv : simEnv -> Prop :=
| mkAgents_heading_towards_dest_inv :
forall E,
( forall n
(pf : (n < sNumAgents E)%nat) d,
sDestination (sAgents E n pf) = Some d ->
directionFromSourceToTarget
(sLocation (sAgents E n pf))
d
(sHeading ((sAgents E n pf)))) ->
sAgents_heading_towards_dest_inv E.
Inductive sAgents_dest_in_perim_inv : simEnv -> Prop :=
| mkAgents_dest_in_perim_inv :
forall E,
( forall n
(pf : (n < sNumAgents E)%nat) d,
Qeq_opt (sDestination (sAgents E n pf)) (Some d) ->
0 <= d <= sPerimLength E) ->
sAgents_dest_in_perim_inv E.
Inductive sAgents_ID_match_inv : simEnv -> Prop :=
| mkAgents_ID_match_inv :
forall E,
( forall n
(pf : (n < sNumAgents E)%nat),
sAgentID (sAgents E n pf) = n) ->
sAgents_ID_match_inv E.
Inductive sAgents_lower_border_inv : simEnv -> Prop :=
| mkAgents_lower_border_inv :
forall E,
(forall n pf,
sLocation (sAgents E n pf) == 0 ->
sHeading (sAgents E n pf) = dRight) ->
sAgents_lower_border_inv E.
Inductive sAgents_upper_border_inv : simEnv -> Prop :=
| mkAgents_upper_border_inv :
forall E,
(forall n pf,
sLocation (sAgents E n pf) == sPerimLength E ->
sHeading (sAgents E n pf) = dLeft) ->
sAgents_upper_border_inv E.
Inductive sAgents_order_dir_inv : simEnv -> Prop :=
| mkAgents_sAgents_order_inv :
forall E,
(forall n1 n2 pf1 pf2 (pf3 : (n1 < n2)%nat),
(sLocation (sAgents E n1 pf1) ==
(sLocation (sAgents E n2 pf2))) ->
((sHeading (sAgents E n1 pf1) = (sHeading (sAgents E n2 pf2))) \/
((sHeading (sAgents E n1 pf1) = dLeft) /\
(sHeading (sAgents E n2 pf2) = dRight)))) ->
sAgents_order_dir_inv E.
(** An environment satisfying all of these properties is valid *)
Inductive validSimEnv : simEnv -> Prop :=
| mkValidSimEnv :
forall E,
sNumAgents_Pos_inv E ->
sPerimLength_Pos_inv E ->
sSpeed_Positive_inv E ->
sAgents_InPerim_inv E ->
sAgents_Ordered_inv E ->
sAgents_heading_towards_dest_inv E ->
sAgents_dest_in_perim_inv E ->
sAgents_ID_match_inv E ->
sAgents_lower_border_inv E ->
sAgents_upper_border_inv E ->
sAgents_order_dir_inv E ->
validSimEnv E.
(* Is there some relational name for preservation of a predicate
under a relation? *)
Lemma simEnvEQ_Preserves_validSimEnv :
forall E1 E2,
simEnvEQ E1 E2 ->
validSimEnv E1 ->
validSimEnv E2.
Proof.
intros. inversion H; inversion H0; do 2 constructor; subst.
- rewrite <- H. inversion H8. auto.
- rewrite <- H. inversion H9. auto.
- rewrite <- H4. inversion H10. auto.
- intros. rewrite <- H3.
erewrite <- H2. inversion H11. auto.
- intros. do 2 erewrite <- H2.
inversion H12; auto.
- intros. inversion H13.
assert (n < sNumAgents E1)%nat by firstorder.
assert (Qeq_opt (sDestination (sAgents E1 n H20)) (Some d)).
{ erewrite H2. rewrite H6. firstorder. } inversion H21.
subst. symmetry in H22. apply H7 in H22.
inversion H22; rewrite H2 in H19; erewrite <- H19;
[eapply ltDir | eapply gtDir]; rewrite <- H2;
subst; rewrite <- H24; eauto.
- intros. rewrite <- H. inversion H14. eapply H7.
rewrite H2. eauto.
- intros. rewrite <- H2. inversion H15. eauto.
- intros. erewrite sHeading_mor. 2: rewrite <- H2; reflexivity.
inversion H16; subst. apply H7. rewrite <- H6.
erewrite sLocation_mor. reflexivity. apply H2.
- intros. erewrite sHeading_mor. 2: rewrite <- H2; reflexivity.
inversion H17; subst. apply H7.
rewrite H3. rewrite <- H6.
apply sLocation_mor. apply H2.
- intros. inversion H18; subst.
assert (n1 < sNumAgents E1)%nat as pf1'.
{ rewrite H1; auto. }
assert (n2 < sNumAgents E1)%nat as pf2'.
{ rewrite H1; auto. }
assert (sLocation (sAgents E1 n1 pf1') ==
sLocation (sAgents E1 n2 pf2')).
{ etransitivity. etransitivity.
2: apply H6.
apply sLocation_mor. apply H2.
apply sLocation_mor. symmetry. apply H2.
}
specialize (H7 n1 n2 pf1' pf2' pf3 H19).
inversion H7; [left | right].
etransitivity.
transitivity (sHeading (sAgents E1 n1 pf1')).
apply sHeading_mor.
symmetry; apply H2. apply H20.
apply sHeading_mor. apply H2.
split; destruct H20;
[rewrite <- H20 | rewrite <- H21];
apply sHeading_mor; symmetry; apply H2.
Unshelve. all: rewrite H; auto.
Qed.
(** Definitions for the type of various events that require a response from agents **)
Record simLowerBorderEvent {E : simEnv} : Type :=
mkLBEvent {
LB_agent : simAgent;
LB_ID : nat;
LB_time : Q;
LB_ID_pf : (LB_ID < sNumAgents E)%nat;
LB_ID_inv : (LB_agent = sAgents E LB_ID LB_ID_pf);
LB_path_inv : locationAfterTime
(sLocation LB_agent) (sHeading LB_agent)
(sSpeed E) LB_time
== 0;
LB_time_pos : 0 < LB_time;
}.
Record simUpperBorderEvent {E : simEnv} : Type :=
mkUBEvent {
UB_agent : simAgent;
UB_ID : nat;
UB_time : Q;
UB_ID_pf : (UB_ID < sNumAgents E)%nat;
UB_ID_inv : (UB_agent = sAgents E UB_ID UB_ID_pf);
UB_path_inv : locationAfterTime
(sLocation UB_agent) (sHeading UB_agent)
(sSpeed E) UB_time
== sPerimLength E;
UB_time_pos : 0 < UB_time;
}.
Record simEndEscortEvent {E : simEnv} : Type :=
mkEEEvent {
EE_agent : simAgent;
EE_ID : nat;
EE_time : Q;
EE_destination : Q;
EE_ID_pf : (EE_ID < sNumAgents E)%nat;
EE_ID_inv : (EE_agent = sAgents E EE_ID EE_ID_pf);
EE_destination_inv : sDestination EE_agent = Some EE_destination;
EE_heading : directionFromSourceToTarget
(sLocation EE_agent) (EE_destination) (sHeading EE_agent);
EE_path_inv : locationAfterTime
(sLocation EE_agent) (sHeading EE_agent)
(sSpeed E) EE_time == EE_destination;
EE_time_nonneg : 0 <= EE_time;
}.
Record simAgentEncounterEvent {E : simEnv} : Type :=
mkAEEvent {
AE_agent1 : simAgent;
AE_ID1 : nat;
AE_agent2 : simAgent;
AE_ID2 : nat;
AE_time : Q;
AE_ID_pf1 : (AE_ID1 < sNumAgents E)%nat;
AE_ID_inv1 : (AE_agent1 = sAgents E AE_ID1 AE_ID_pf1);
AE_ID_pf2 : (AE_ID2 < sNumAgents E)%nat;
AE_ID_inv2 : (AE_agent2 = sAgents E AE_ID2 AE_ID_pf2);
AE_distance_pos : 0 < qDistance (sLocation AE_agent1) (sLocation AE_agent2);
AE_path_inv : locationAfterTime
(sLocation AE_agent1) (sHeading AE_agent1)
(sSpeed E) AE_time
==
locationAfterTime
(sLocation AE_agent2) (sHeading AE_agent2)
(sSpeed E) AE_time;
AE_time_pos : 0 < AE_time;
}.
(** Eqaulities over the various events types **)
Inductive simLowerBorderEventEQ {E : simEnv} :
(@simLowerBorderEvent E) -> (@simLowerBorderEvent E) -> Prop :=
mkLBE_EQ :
forall e1 e2, simAgentEQ (LB_agent e1) (LB_agent e2) ->
LB_ID e1 = LB_ID e2 ->
LB_time e1 == LB_time e2 ->
simLowerBorderEventEQ e1 e2.
Lemma simLowerBorderEventEQ_refl {E : simEnv} :
reflexive _ (@simLowerBorderEventEQ E).
Proof. constructor; intros; firstorder. Qed.
Lemma simLowerBorderEventEQ_symm {E : simEnv} :
symmetric _ (@simLowerBorderEventEQ E).
Proof. constructor; inversion H; firstorder. Qed.
Lemma simLowerBorderEventEQ_trans {E : simEnv} :
transitive _ (@simLowerBorderEventEQ E).
Proof.
constructor; inversion H; inversion H0; intros; firstorder;
etransitivity; eauto.
Qed.
Lemma simLowerBorderEventEQ_equiv {E : simEnv} :
Equivalence (@simLowerBorderEventEQ E).
Proof.
constructor.
- apply simLowerBorderEventEQ_refl.
- apply simLowerBorderEventEQ_symm.
- apply simLowerBorderEventEQ_trans.
Qed.
Add Parametric Relation (E : simEnv) : (@simLowerBorderEvent E) (simLowerBorderEventEQ)
reflexivity proved by simLowerBorderEventEQ_refl
symmetry proved by simLowerBorderEventEQ_symm
transitivity proved by simLowerBorderEventEQ_trans
as simLowerBorderEventEQ_rel.
Add Parametric Morphism (E : simEnv) : (@LB_agent E) with
signature simLowerBorderEventEQ ==> simAgentEQ as LB_agent_mor.
Proof. intros. inversion H. auto. Qed.
Add Parametric Morphism (E : simEnv) : (@LB_ID E) with
signature simLowerBorderEventEQ ==> eq as LB_ID_mor.
Proof. intros. inversion H. auto. Qed.
Add Parametric Morphism (E : simEnv) : (@LB_time E) with
signature simLowerBorderEventEQ ==> Qeq as LB_time_mor.
Proof. intros. inversion H. auto. Qed.
Definition locationAfterEvent_LB {E : simEnv} (e : @simLowerBorderEvent E) : Q :=
locationAfterTime (sLocation (LB_agent e)) (sHeading (LB_agent e))
(sSpeed E) (LB_time e).
Add Parametric Morphism (E : simEnv) : (@locationAfterEvent_LB E) with
signature simLowerBorderEventEQ ==> Qeq as locationAfterEvent_LB_mor.
Proof.
intros. unfold locationAfterEvent_LB. inversion H. subst.
rewrite H0, H2. reflexivity.
Qed.
Inductive simUpperBorderEventEQ {E : simEnv} :
(@simUpperBorderEvent E) -> (@simUpperBorderEvent E) -> Prop :=
mkUBE_EQ :
forall e1 e2, simAgentEQ (UB_agent e1) (UB_agent e2) ->
UB_ID e1 = UB_ID e2 ->
UB_time e1 == UB_time e2 ->
simUpperBorderEventEQ e1 e2.
Lemma simUpperBorderEventEQ_refl {E : simEnv} :
reflexive _ (@simUpperBorderEventEQ E).
Proof. constructor; intros; firstorder. Qed.
Lemma simUpperBorderEventEQ_symm {E : simEnv} :
symmetric _ (@simUpperBorderEventEQ E).
Proof. constructor; inversion H; firstorder. Qed.
Lemma simUpperBorderEventEQ_trans {E : simEnv} :
transitive _ (@simUpperBorderEventEQ E).
Proof.
constructor; inversion H; inversion H0; intros; firstorder;
etransitivity; eauto.
Qed.
Lemma simUpperBorderEventEQ_equiv {E : simEnv} :
Equivalence (@simUpperBorderEventEQ E).
Proof.
constructor.
- apply simUpperBorderEventEQ_refl.
- apply simUpperBorderEventEQ_symm.
- apply simUpperBorderEventEQ_trans.
Qed.
Add Parametric Relation (E : simEnv) : (@simUpperBorderEvent E) (simUpperBorderEventEQ)
reflexivity proved by simUpperBorderEventEQ_refl
symmetry proved by simUpperBorderEventEQ_symm
transitivity proved by simUpperBorderEventEQ_trans
as simUpperBorderEventEQ_rel.
Add Parametric Morphism (E : simEnv) : (@UB_agent E) with
signature simUpperBorderEventEQ ==> simAgentEQ as UB_agent_mor.
Proof. intros. inversion H. auto. Qed.
Add Parametric Morphism (E : simEnv) : (@UB_ID E) with
signature simUpperBorderEventEQ ==> eq as UB_ID_mor.
Proof. intros. inversion H. auto. Qed.
Add Parametric Morphism (E : simEnv) : (@UB_time E) with
signature simUpperBorderEventEQ ==> Qeq as UB_time_mor.
Proof. intros. inversion H. auto. Qed.
Inductive simAgentEncounterEventEQ {E : simEnv} :
(@simAgentEncounterEvent E) -> (@simAgentEncounterEvent E) -> Prop :=
mkAEE_EQ :
forall e1 e2, simAgentEQ (AE_agent1 e1) (AE_agent1 e2) ->
simAgentEQ (AE_agent2 e1) (AE_agent2 e2) ->
AE_ID1 e1 = AE_ID1 e2 ->
AE_ID2 e1 = AE_ID2 e2 ->
AE_time e1 == AE_time e2 ->
simAgentEncounterEventEQ e1 e2.
Lemma simAgentEncounterEventEQ_refl {E : simEnv} :
reflexive _ (@simAgentEncounterEventEQ E).
Proof. constructor; intros; firstorder. Qed.
Lemma simAgentEncounterEventEQ_symm {E : simEnv} :
symmetric _ (@simAgentEncounterEventEQ E).
Proof. constructor; inversion H; firstorder. Qed.
Lemma simAgentEncounterEventEQ_trans {E : simEnv} :
transitive _ (@simAgentEncounterEventEQ E).
Proof.
constructor; inversion H; inversion H0; intros; subst;
etransitivity; eauto.
Qed.
Lemma simAgentEncounterEventEQ_equiv {E : simEnv} :
Equivalence (@simAgentEncounterEventEQ E).
Proof.
constructor.
- apply simAgentEncounterEventEQ_refl.
- apply simAgentEncounterEventEQ_symm.
- apply simAgentEncounterEventEQ_trans.
Qed.
Add Parametric Relation (E : simEnv) :
(@simAgentEncounterEvent E) (simAgentEncounterEventEQ)
reflexivity proved by simAgentEncounterEventEQ_refl
symmetry proved by simAgentEncounterEventEQ_symm
transitivity proved by simAgentEncounterEventEQ_trans
as simAgentEncounterEventEQ_rel.
Add Parametric Morphism (E : simEnv) : (@AE_agent1 E) with
signature simAgentEncounterEventEQ ==> simAgentEQ as AE_agent1_mor.
Proof. intros. inversion H. auto. Qed.
Add Parametric Morphism (E : simEnv) : (@AE_agent2 E) with
signature simAgentEncounterEventEQ ==> simAgentEQ as AE_agent2_mor.
Proof. intros. inversion H. auto. Qed.
Add Parametric Morphism (E : simEnv) : (@AE_ID1 E) with
signature simAgentEncounterEventEQ ==> eq as AE_ID1_mor.
Proof. intros. inversion H. auto. Qed.
Add Parametric Morphism (E : simEnv) : (@AE_ID2 E) with
signature simAgentEncounterEventEQ ==> eq as AE_ID2_mor.
Proof. intros. inversion H. auto. Qed.
Add Parametric Morphism (E : simEnv) : (@AE_time E) with
signature simAgentEncounterEventEQ ==> Qeq as AE_time_mor.
Proof. intros. inversion H. auto. Qed.
Inductive simEndEscortEventEQ {E : simEnv} :
(@simEndEscortEvent E) -> (@simEndEscortEvent E) -> Prop :=
mkEEE_EQ :
forall e1 e2, simAgentEQ (EE_agent e1) (EE_agent e2) ->
EE_ID e1 = EE_ID e2 ->
EE_time e1 == EE_time e2 ->
EE_destination e1 == EE_destination e2 ->
simEndEscortEventEQ e1 e2.
Lemma simEndEscortEventEQ_refl {E : simEnv} :
reflexive _ (@simEndEscortEventEQ E).
Proof. constructor; intros; firstorder. Qed.
Lemma simEndEscortEventEQ_symm {E : simEnv} :
symmetric _ (@simEndEscortEventEQ E).
Proof. constructor; inversion H; firstorder. Qed.
Lemma simEndEscortEventEQ_trans {E : simEnv} :
transitive _ (@simEndEscortEventEQ E).
Proof.
constructor; inversion H; inversion H0; intros; firstorder;
etransitivity; eauto.
Qed.
Lemma simEndEscortEventEQ_equiv {E : simEnv} :
Equivalence (@simEndEscortEventEQ E).
Proof.
constructor.
- apply simEndEscortEventEQ_refl.
- apply simEndEscortEventEQ_symm.
- apply simEndEscortEventEQ_trans.
Qed.
Add Parametric Relation (E : simEnv) :
(@simEndEscortEvent E) (simEndEscortEventEQ)
reflexivity proved by simEndEscortEventEQ_refl
symmetry proved by simEndEscortEventEQ_symm
transitivity proved by simEndEscortEventEQ_trans
as simEndEscortEventEQ_rel.
Add Parametric Morphism (E : simEnv) : (@EE_agent E) with
signature simEndEscortEventEQ ==> simAgentEQ as EE_agent_mor.
Proof. intros. inversion H. auto. Qed.
Add Parametric Morphism (E : simEnv) : (@EE_ID E) with
signature simEndEscortEventEQ ==> eq as EE_ID_mor.
Proof. intros. inversion H. auto. Qed.
Add Parametric Morphism (E : simEnv) : (@EE_time E) with
signature simEndEscortEventEQ ==> Qeq as EE_time_mor.
Proof. intros. inversion H. auto. Qed.
Add Parametric Morphism (E : simEnv) : (@EE_destination E) with
signature simEndEscortEventEQ ==> Qeq as EE_destination_mor.
Proof. intros. inversion H. auto. Qed.
(* An event is simply built using one of these event types *)
Inductive simEvent {E : simEnv} : Type :=
| simEvent_LB : @simLowerBorderEvent E -> simEvent
| simEvent_UB : @simUpperBorderEvent E -> simEvent
| simEvent_AE : @simAgentEncounterEvent E -> simEvent
| simEvent_EE : @simEndEscortEvent E -> simEvent.
Definition timeOfEvent {E : simEnv} (e : @simEvent E): Q :=
match e with
| simEvent_LB e' => LB_time e'
| simEvent_UB e' => UB_time e'
| simEvent_AE e' => AE_time e'
| simEvent_EE e' => EE_time e'
end.
Definition locationOfEvent {E : simEnv} (e : @simEvent E): Q :=
match e with
| simEvent_LB e' => locationAfterTime
(sLocation (LB_agent e')) (sHeading (LB_agent e'))
(sSpeed E) (timeOfEvent e)
| simEvent_UB e' => locationAfterTime
(sLocation (UB_agent e')) (sHeading (UB_agent e'))
(sSpeed E) (timeOfEvent e)
| simEvent_AE e' => locationAfterTime
(sLocation (AE_agent1 e')) (sHeading (AE_agent1 e'))
(sSpeed E) (timeOfEvent e)
| simEvent_EE e' => locationAfterTime
(sLocation (EE_agent e')) (sHeading (EE_agent e'))
(sSpeed E) (timeOfEvent e)
end.
Definition agentIDOfEvent {E : simEnv} (e : @simEvent E): nat :=
match e with
| simEvent_LB e' => LB_ID e'
| simEvent_UB e' => UB_ID e'
| simEvent_AE e' => AE_ID1 e'
| simEvent_EE e' => EE_ID e'
end.
Definition agentIDPfOfEvent {E : simEnv} (e : @simEvent E) :
(agentIDOfEvent e < sNumAgents E)%nat :=
match e with
| simEvent_LB e' => LB_ID_pf e'
| simEvent_UB e' => UB_ID_pf e'
| simEvent_AE e' => AE_ID_pf1 e'
| simEvent_EE e' => EE_ID_pf e'
end.
Definition agentOfEvent {E : simEnv} (e : @simEvent E) : simAgent :=
sAgents E (agentIDOfEvent e) (agentIDPfOfEvent e).
Definition directionAfterEndEscortEvent
{E : simEnv} (e : @simEndEscortEvent E) : Direction :=
let e' := simEvent_EE e in
if Qeq_bool
(locationOfEvent e')
(agentUpperPatrolRange (agentIDOfEvent e') E (agentIDPfOfEvent e'))
then dLeft
else dRight.
Definition destinationAfterEncounterAgentEvent
{E : simEnv} (e : @simAgentEncounterEvent E) : option Q :=
let e' := simEvent_AE e in
let agent1_UB := agentUpperPatrolRange (AE_ID1 e) E (AE_ID_pf1 e) in
let agent1_LB := agentLowerPatrolRange (AE_ID1 e) E (AE_ID_pf1 e) in
let agent2_UB := agentUpperPatrolRange (AE_ID2 e) E (AE_ID_pf2 e) in
let agent2_LB := agentLowerPatrolRange (AE_ID2 e) E (AE_ID_pf2 e) in
if Qeq_bool agent1_UB agent2_LB then
Some agent1_UB
else if Qeq_bool agent1_LB agent2_UB then
Some agent1_LB
else None.
Definition directionAfterEncounterAgentEvent
{E : simEnv} (e : @simAgentEncounterEvent E) : Direction :=
let e' := simEvent_AE e in
let d := destinationAfterEncounterAgentEvent e in
match d with
| None => sHeading (agentOfEvent e')
| Some d' => match (locationOfEvent e') ?= (d') with
| Eq => sHeading (agentOfEvent e')
| Lt => dRight
| Gt => dLeft
end
end.
Add Parametric Morphism E : destinationAfterEncounterAgentEvent with
signature (@simAgentEncounterEventEQ E) ==> Qeq_opt as destinationAfterEncounterAgentEvent_mor.
Proof.
intros. unfold destinationAfterEncounterAgentEvent.
rewrite agentUpperPatrolRange_mor with (pf2 := AE_ID_pf1 y);
[| reflexivity |rewrite H; reflexivity].
rewrite agentLowerPatrolRange_mor with (pf2 := AE_ID_pf2 y);
[| reflexivity |rewrite H; reflexivity].
case Qeq_bool.
- constructor. apply agentUpperPatrolRange_mor; try rewrite H; reflexivity.
- rewrite agentLowerPatrolRange_mor with (pf2 := AE_ID_pf1 y);
[| reflexivity |rewrite H; reflexivity].
rewrite agentUpperPatrolRange_mor with (pf2 := AE_ID_pf2 y);
[| reflexivity |rewrite H; reflexivity].
case Qeq_bool; try reflexivity. constructor.
apply agentLowerPatrolRange_mor. reflexivity.
rewrite H. reflexivity.
Qed.
Inductive eventEQ {E : simEnv} : (@simEvent E) -> (@simEvent E) -> Prop :=
| eventEQ_LBE : forall e1 e2 e1' e2',
e1 = simEvent_LB e1' ->
e2 = simEvent_LB e2' ->
simLowerBorderEventEQ e1' e2' ->
eventEQ e1 e2
| eventEQ_UBE : forall e1 e2 e1' e2',
e1 = simEvent_UB e1' ->
e2 = simEvent_UB e2' ->
simUpperBorderEventEQ e1' e2' ->
eventEQ e1 e2
| eventEQ_AEE : forall e1 e2 e1' e2',
e1 = simEvent_AE e1' ->
e2 = simEvent_AE e2' ->
simAgentEncounterEventEQ e1' e2' ->
eventEQ e1 e2
| eventEQ_EEE : forall e1 e2 e1' e2',
e1 = simEvent_EE e1' ->
e2 = simEvent_EE e2' ->
simEndEscortEventEQ e1' e2' ->
eventEQ e1 e2.
Lemma eventEQ_refl {E : simEnv} : reflexive _ (@eventEQ E).
Proof.
unfold reflexive. intros. destruct x.
- eapply eventEQ_LBE; reflexivity.
- eapply eventEQ_UBE; reflexivity.
- eapply eventEQ_AEE; reflexivity.
- eapply eventEQ_EEE; try reflexivity.
Qed.
Lemma eventEQ_symm {E : simEnv} : symmetric _ (@eventEQ E).
Proof.
unfold symmetric. intros. inversion H; subst.
- eapply eventEQ_LBE; eauto. firstorder.
- eapply eventEQ_UBE; eauto. firstorder.
- eapply eventEQ_AEE; eauto. firstorder.
- eapply eventEQ_EEE; eauto. firstorder.
Qed.
Lemma eventEQ_trans {E : simEnv} : transitive _ (@eventEQ E).
Proof.
unfold transitive. intros. inversion H; inversion H0; subst; try congruence.
- eapply eventEQ_LBE; eauto. etransitivity. apply H3. inversion H6. auto.
- eapply eventEQ_UBE; eauto. etransitivity. apply H3. inversion H6. auto.
- eapply eventEQ_AEE; eauto. etransitivity. apply H3. inversion H6. auto.
- eapply eventEQ_EEE; eauto. etransitivity. apply H3. inversion H6. auto.
Qed.
Lemma eventEQ_equiv {E : simEnv} : Equivalence (@eventEQ E).
Proof.
constructor.
- apply eventEQ_refl.
- apply eventEQ_symm.
- apply eventEQ_trans.
Qed.
Add Parametric Relation (E : simEnv) : (@simEvent E) (@eventEQ E)
reflexivity proved by eventEQ_refl
symmetry proved by eventEQ_symm
transitivity proved by eventEQ_trans
as eventEQ_rel.
Add Parametric Morphism (E : simEnv) : (@timeOfEvent E) with
signature eventEQ ==> Qeq as timeOfEvent_mor.
Proof.
intros. inversion H; subst; unfold timeOfEvent; rewrite H2; firstorder.
Qed.
Add Parametric Morphism (E : simEnv) : (@locationOfEvent E) with
signature eventEQ ==> Qeq as locationOfEvent_mor.
Proof.
intros. inversion H; subst; unfold locationOfEvent.
inversion H2; subst.
- rewrite H0, H3. reflexivity.
- rewrite H, H2. reflexivity.
- rewrite H, H2. reflexivity.
- rewrite H, H2. reflexivity.
Qed.
Add Parametric Morphism (E : simEnv) : (@agentIDOfEvent E) with
signature eventEQ ==> eq as agentIDOfEvent_mor.
Proof.
intros. inversion H; subst; unfold agentIDOfEvent; try rewrite H2; firstorder.
Qed.
Add Parametric Morphism (E : simEnv) : (@agentOfEvent E) with
signature eventEQ ==> simAgentEQ as agentOfEvent_mor.
Proof.
intros. inversion H; subst; unfold agentOfEvent;
[unfold LB_agent | unfold UB_agent | unfold AE_agent1 | unfold EE_agent];
destruct e1', e2'; simpl in *; subst; apply sAgents_mor; auto.
all: try reflexivity.
all: inversion H2; auto.
Qed.
Add Parametric Morphism E : directionAfterEncounterAgentEvent with
signature (@simAgentEncounterEventEQ E) ==> eq as directionAfterEncounterAgentEvent_mor.
Proof.
intros. unfold directionAfterEncounterAgentEvent.
generalize (destinationAfterEncounterAgentEvent_mor E x y H). intros.
inversion H0. inversion H; subst. unfold agentOfEvent; simpl.
rewrite sAgents_mor; [| reflexivity |apply H5].
reflexivity. rewrite H3.
assert (locationOfEvent (simEvent_AE x) == locationOfEvent (simEvent_AE y)).
apply locationOfEvent_mor. eapply eventEQ_AEE; try reflexivity. auto.
rewrite H4. case Qcompare.
apply sHeading_mor. unfold agentOfEvent. simpl.
rewrite sAgents_mor; [| reflexivity |]. reflexivity. inversion H; auto.
all: auto.
Qed.
Add Parametric Morphism E : directionAfterEndEscortEvent with
signature (@simEndEscortEventEQ E) ==> eq as directionAfterEndEscortEvent_mor.
Proof.
intros. unfold directionAfterEndEscortEvent.
assert ( locationOfEvent (simEvent_EE x) == locationOfEvent (simEvent_EE y)).
{
unfold locationOfEvent. apply locationAfterTime_mor; try rewrite H; try reflexivity.
apply timeOfEvent_mor. eapply eventEQ_EEE; try reflexivity. auto.
}
rewrite H0. rewrite agentUpperPatrolRange_mor. 1,2 : reflexivity.
apply agentIDOfEvent_mor. eapply eventEQ_EEE; try reflexivity. auto.
Qed.
Inductive nextEvents (E : simEnv) : (list (@simEvent E)) -> Prop :=
| mkNextEvents :
forall l,
(* all events in the list occur at the soonest possible time step *)
(forall (e e' : (@simEvent E)), In e l -> (timeOfEvent e) <= timeOfEvent e') ->
(*
all events that occur at the soonest possible time step are in
the list -- up to equivalence with eventEQ.
*)
(forall e,
(forall e' : (@simEvent E),timeOfEvent e <= timeOfEvent e') ->
InA eventEQ e l) ->
(* the list contains no duplicates -- up to equivalence with eventEQ*)
NoDupA eventEQ l ->
nextEvents E l.
(*
For any environment, the list of nextEvents is unique
-- up to permutation and eventEQ
*)
Lemma nextEvents_uniq :
forall E l l', nextEvents E l -> nextEvents E l' -> PermutationA eventEQ l l'.
Proof.
intros.
apply NoDupA_equivlistA_PermutationA.
- apply eventEQ_equiv.
- inversion H; auto.
- inversion H0; auto.
- constructor; intros; inversion H; subst; inversion H0; subst.
* apply H6. intros. apply InA_alt in H1. destruct H1 as [y [H1 H1']].
rewrite H1. apply H2. auto.
* apply H3. intros. apply InA_alt in H1. destruct H1 as [y [H1 H1']].
rewrite H1. apply H5. auto.
Qed.
Lemma nextEvents_time_eq :
forall E l e1 e2,
nextEvents E l ->
InA eventEQ e1 l ->
InA eventEQ e2 l ->
timeOfEvent e1 == timeOfEvent e2.
Proof.
intros.
apply InA_alt in H0.
apply InA_alt in H1.
destruct H0 as [x [Hx H0]].
destruct H1 as [y [Hy H1]].
rewrite Hx, Hy.
clear e1 e2 Hx Hy.
inversion H; subst.
apply (H2 _ x) in H1.
apply (H2 _ y) in H0.
apply Qle_antisym; auto.
Qed.
Lemma nextEvents_agent_location_inv :
forall E l a (e1 e2 : @simEvent E),
nextEvents E l ->
InA eventEQ e1 l ->
InA eventEQ e2 l ->
agentIDOfEvent e1 = a ->
agentIDOfEvent e2 = a -> locationOfEvent e1 == locationOfEvent e2.
Proof.
intros.
apply InA_alt in H0.
apply InA_alt in H1.
destruct H0 as [x [Hx H0]].
destruct H1 as [y [Hy H1]].
rewrite Hx, Hy.
rewrite Hx in H2. rewrite Hy in H3.
clear e1 e2 Hx Hy.