-
Notifications
You must be signed in to change notification settings - Fork 11
/
Properties.v
6752 lines (6258 loc) · 249 KB
/
Properties.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
Require Import Kami.Syntax Kami.Lib.Fold.
Import Word.Notations.
Import ListNotations.
Require Import Coq.Sorting.Permutation.
Require Import Coq.Sorting.PermutEq.
Require Import RelationClasses Setoid Morphisms.
Require Import ZArith.
Definition filterRegs f m (o: RegsT) :=
filter (fun x => f (getBool (in_dec string_dec (fst x) (map fst (getAllRegisters m))))) o.
Definition filterExecs f m (l: list FullLabel) :=
filter (fun x => f match fst (snd x) with
| Rle y =>
getBool (in_dec string_dec y (map fst (getAllRules m)))
| Meth (y, _) =>
getBool (in_dec string_dec y (map fst (getAllMethods m)))
end) l.
Inductive WeakInclusions : list (list FullLabel) -> list (list (FullLabel)) -> Prop :=
| WI_Nil : WeakInclusions nil nil
| WI_Cons : forall (ls ls' : list (list FullLabel)) (l l' : list FullLabel), WeakInclusions ls ls' -> WeakInclusion l l' -> WeakInclusions (l::ls)(l'::ls').
Definition WeakEqualities ls ls' := WeakInclusions ls ls' /\ WeakInclusions ls' ls.
Notation "l '[=]' r" :=
((@Permutation _ (l) (r)))
(at level 70, no associativity).
Section Semantics.
Variable o: RegsT.
Inductive PSemAction:
forall k, ActionT type k -> RegsT -> RegsT -> MethsT -> type k -> Prop :=
| PSemMCall
meth s (marg: Expr type (SyntaxKind (fst s)))
(mret: type (snd s))
retK (fret: type retK)
(cont: type (snd s) -> ActionT type retK)
readRegs newRegs (calls: MethsT) acalls
(HAcalls: acalls [=] (meth, (existT _ _ (evalExpr marg, mret))) :: calls)
(HPSemAction: PSemAction (cont mret) readRegs newRegs calls fret):
PSemAction (MCall meth s marg cont) readRegs newRegs acalls fret
| PSemLetExpr
k (e: Expr type k) retK (fret: type retK)
(cont: fullType type k -> ActionT type retK) readRegs newRegs calls
(HPSemAction: PSemAction (cont (evalExpr e)) readRegs newRegs calls fret):
PSemAction (LetExpr e cont) readRegs newRegs calls fret
| PSemLetAction
k (a: ActionT type k) (v: type k) retK (fret: type retK)
(cont: type k -> ActionT type retK)
readRegs newRegs readRegsCont newRegsCont calls callsCont
(HDisjRegs: DisjKey newRegs newRegsCont)
(HPSemAction: PSemAction a readRegs newRegs calls v)
ureadRegs unewRegs ucalls
(HUReadRegs: ureadRegs [=] readRegs ++ readRegsCont)
(HUNewRegs: unewRegs [=] newRegs ++ newRegsCont)
(HUCalls: ucalls [=] calls ++ callsCont)
(HPSemActionCont: PSemAction (cont v) readRegsCont newRegsCont callsCont fret):
PSemAction (LetAction a cont) (ureadRegs) (unewRegs)
(ucalls) fret
| PSemReadNondet
valueT (valueV: fullType type valueT)
retK (fret: type retK) (cont: fullType type valueT -> ActionT type retK)
readRegs newRegs calls
(HPSemAction: PSemAction (cont valueV) readRegs newRegs calls fret):
PSemAction (ReadNondet _ cont) readRegs newRegs calls fret
| PSemReadReg
(r: string) regT (regV: fullType type regT)
retK (fret: type retK) (cont: fullType type regT -> ActionT type retK)
readRegs newRegs calls areadRegs
(HRegVal: In (r, existT _ regT regV) o)
(HPSemAction: PSemAction (cont regV) readRegs newRegs calls fret)
(HNewReads: areadRegs [=] (r, existT _ regT regV) :: readRegs):
PSemAction (ReadReg r _ cont) areadRegs newRegs calls fret
| PSemWriteReg
(r: string) k
(e: Expr type k)
retK (fret: type retK)
(cont: ActionT type retK) readRegs newRegs calls anewRegs
(HRegVal: In (r, k) (getKindAttr o))
(HDisjRegs: key_not_In r newRegs)
(HANewRegs: anewRegs [=] (r, (existT _ _ (evalExpr e))) :: newRegs)
(HPSemAction: PSemAction cont readRegs newRegs calls fret):
PSemAction (WriteReg r e cont) readRegs anewRegs calls fret
| PSemIfElseTrue
(p: Expr type (SyntaxKind Bool)) k1
(a: ActionT type k1)
(a': ActionT type k1)
(r1: type k1)
k2 (cont: type k1 -> ActionT type k2)
readRegs1 readRegs2 newRegs1 newRegs2 calls1 calls2 (r2: type k2)
(HDisjRegs: DisjKey newRegs1 newRegs2)
(HTrue: evalExpr p = true)
(HAction: PSemAction a readRegs1 newRegs1 calls1 r1)
(HPSemAction: PSemAction (cont r1) readRegs2 newRegs2 calls2 r2)
ureadRegs unewRegs ucalls
(HUReadRegs: ureadRegs [=] readRegs1 ++ readRegs2)
(HUNewRegs: unewRegs [=] newRegs1 ++ newRegs2)
(HUCalls: ucalls [=] calls1 ++ calls2) :
PSemAction (IfElse p a a' cont) ureadRegs unewRegs ucalls r2
| PSemIfElseFalse
(p: Expr type (SyntaxKind Bool)) k1
(a: ActionT type k1)
(a': ActionT type k1)
(r1: type k1)
k2 (cont: type k1 -> ActionT type k2)
readRegs1 readRegs2 newRegs1 newRegs2 calls1 calls2 (r2: type k2)
(HDisjRegs: DisjKey newRegs1 newRegs2)
(HFalse: evalExpr p = false)
(HAction: PSemAction a' readRegs1 newRegs1 calls1 r1)
(HPSemAction: PSemAction (cont r1) readRegs2 newRegs2 calls2 r2)
ureadRegs unewRegs ucalls
(HUReadRegs: ureadRegs [=] readRegs1 ++ readRegs2)
(HUNewRegs: unewRegs [=] newRegs1 ++ newRegs2)
(HUCalls: ucalls [=] calls1 ++ calls2):
PSemAction (IfElse p a a' cont) ureadRegs unewRegs ucalls r2
| PSemDisplay
(ls: list (SysT type)) k (cont: ActionT type k)
r readRegs newRegs calls
(HPSemAction: PSemAction cont readRegs newRegs calls r):
PSemAction (Sys ls cont) readRegs newRegs calls r
| PSemReturn
k (e: Expr type (SyntaxKind k)) evale
(HEvalE: evale = evalExpr e)
readRegs newRegs calls
(HReadRegs: readRegs = nil)
(HNewRegs: newRegs = nil)
(HCalls: calls = nil) :
PSemAction (Return e) readRegs newRegs calls evale.
End Semantics.
Section BaseModule.
Variable m: BaseModule.
Variable o: RegsT.
Inductive PSubsteps: list FullLabel -> Prop :=
| NilPSubstep (HRegs: getKindAttr o [=] getKindAttr (getRegisters m)) : PSubsteps nil
| PAddRule (HRegs: getKindAttr o [=] getKindAttr (getRegisters m))
rn rb
(HInRules: In (rn, rb) (getRules m))
reads u cs
(HPAction: PSemAction o (rb type) reads u cs WO)
(HReadsGood: SubList (getKindAttr reads)
(getKindAttr (getRegisters m)))
(HUpdGood: SubList (getKindAttr u)
(getKindAttr (getRegisters m)))
l ls (HLabel: l [=] (u, (Rle rn, cs)) :: ls)
(HDisjRegs: forall x, In x ls -> DisjKey (fst x) u)
(HNoRle: forall x, In x ls -> match fst (snd x) with
| Rle _ => False
| _ => True
end)
(HPSubstep: PSubsteps ls):
PSubsteps l
| PAddMeth (HRegs: getKindAttr o [=] getKindAttr (getRegisters m))
fn fb
(HInMeths: In (fn, fb) (getMethods m))
reads u cs argV retV
(HPAction: PSemAction o ((projT2 fb) type argV) reads u cs retV)
(HReadsGood: SubList (getKindAttr reads)
(getKindAttr (getRegisters m)))
(HUpdGood: SubList (getKindAttr u)
(getKindAttr (getRegisters m)))
l ls (HLabel: l [=] (u, (Meth (fn, existT _ _ (argV, retV)), cs)) :: ls )
(HDisjRegs: forall x, In x ls -> DisjKey (fst x) u)
(HPSubsteps: PSubsteps ls):
PSubsteps l.
Inductive PPlusSubsteps: RegsT -> list RuleOrMeth -> MethsT -> Prop :=
| NilPPlusSubstep (HRegs: getKindAttr o [=] getKindAttr (getRegisters m)) : PPlusSubsteps nil nil nil
| PPlusAddRule (HRegs: getKindAttr o [=] getKindAttr (getRegisters m))
rn rb
(HInRules: In (rn, rb) (getRules m))
reads u cs
(HPAction: PSemAction o (rb type) reads u cs WO)
(HReadsGood: SubList (getKindAttr reads)
(getKindAttr (getRegisters m)))
(HUpdGood: SubList (getKindAttr u)
(getKindAttr (getRegisters m)))
upds execs calls oldUpds oldExecs oldCalls
(HUpds: upds [=] u ++ oldUpds)
(HExecs: execs [=] Rle rn :: oldExecs)
(HCalls: calls [=] cs ++ oldCalls)
(HDisjRegs: DisjKey oldUpds u)
(HNoRle: forall x, In x oldExecs -> match x with
| Rle _ => False
| _ => True
end)
(HPSubstep: PPlusSubsteps oldUpds oldExecs oldCalls):
PPlusSubsteps upds execs calls
| PPlusAddMeth (HRegs: getKindAttr o [=] getKindAttr (getRegisters m))
fn fb
(HInMeths: In (fn, fb) (getMethods m))
reads u cs argV retV
(HPAction: PSemAction o ((projT2 fb) type argV) reads u cs retV)
(HReadsGood: SubList (getKindAttr reads)
(getKindAttr (getRegisters m)))
(HUpdGood: SubList (getKindAttr u)
(getKindAttr (getRegisters m)))
upds execs calls oldUpds oldExecs oldCalls
(HUpds: upds [=] u ++ oldUpds)
(HExecs: execs [=] Meth (fn, existT _ _ (argV, retV)) :: oldExecs)
(HCalls: calls [=] cs ++ oldCalls)
(HDisjRegs: DisjKey oldUpds u)
(HPSubstep: PPlusSubsteps oldUpds oldExecs oldCalls):
PPlusSubsteps upds execs calls.
End BaseModule.
Inductive PStep: Mod -> RegsT -> list FullLabel -> Prop :=
| PBaseStep m o l (HPSubsteps: PSubsteps m o l) (HMatching: MatchingExecCalls_Base l m):
PStep (Base m) o l
| PHideMethStep m s o l (HPStep: PStep m o l)
(HHidden : forall v, In (s, projT1 v) (getKindAttr (getAllMethods m)) -> getListFullLabel_diff (s, v) l = 0%Z):
PStep (HideMeth m s) o l
| PConcatModStep m1 m2 o1 o2 l1 l2
(HPStep1: PStep m1 o1 l1)
(HPStep2: PStep m2 o2 l2)
(HMatching1: MatchingExecCalls_Concat l1 l2 m2)
(HMatching2: MatchingExecCalls_Concat l2 l1 m1)
(HNoRle: forall x y, In x l1 -> In y l2 -> match fst (snd x), fst (snd y) with
| Rle _, Rle _ => False
| _, _ => True
end)
o l
(HRegs: o [=] o1 ++ o2)
(HLabels: l [=] l1 ++ l2):
PStep (ConcatMod m1 m2) o l.
Section PPlusStep.
Variable m: BaseModule.
Variable o: RegsT.
Definition MatchingExecCalls_flat (calls : MethsT) (execs : list RuleOrMeth) (m : BaseModule) :=
forall (f : MethT),
In (fst f, projT1 (snd f)) (getKindAttr (getMethods m)) ->
(getNumFromCalls f calls <= getNumFromExecs f execs)%Z.
Inductive PPlusStep : RegsT -> list RuleOrMeth -> MethsT -> Prop :=
| BasePPlusStep upds execs calls:
PPlusSubsteps m o upds execs calls ->
MatchingExecCalls_flat calls execs m -> PPlusStep upds execs calls.
End PPlusStep.
Section Trace.
Variable m: Mod.
Definition PUpdRegs (u: list RegsT) (o o': RegsT)
:= getKindAttr o [=] getKindAttr o' /\
(forall s v, In (s, v) o' -> ((exists x, In x u /\ In (s, v) x) \/
((~ exists x, In x u /\ In s (map fst x)) /\ In (s, v) o))).
Inductive PTrace: RegsT -> list (list FullLabel) -> Prop :=
| PInitTrace (o' o'' : RegsT) ls'
(HPerm : o' [=] o'')
(HUpdRegs : Forall2 regInit o'' (getAllRegisters m))
(HTrace: ls' = nil):
PTrace o' ls'
| PContinueTrace o ls l o' ls'
(PHOldTrace: PTrace o ls)
(HPStep: PStep m o l)
(HPUpdRegs: PUpdRegs (map fst l) o o')
(HTrace: ls' = l :: ls):
PTrace o' ls'.
End Trace.
Definition PPlusUpdRegs (u o o' : RegsT) :=
getKindAttr o [=] getKindAttr o' /\
(forall s v, In (s, v) o' -> In (s, v) u \/ (~ In s (map fst u) /\ In (s, v) o)).
Section PPlusTrace.
Variable m: BaseModule.
Inductive PPlusTrace : RegsT -> list (RegsT * ((list RuleOrMeth) * MethsT)) -> Prop :=
| PPlusInitTrace (o' o'' : RegsT) ls'
(HPerm : o' [=] o'')
(HUpdRegs : Forall2 regInit o'' (getRegisters m))
(HTrace : ls' = nil):
PPlusTrace o' ls'
| PPlusContinueTrace (o o' : RegsT)
(upds : RegsT)
(execs : list RuleOrMeth)
(calls : MethsT)
(ls ls' : list (RegsT * ((list RuleOrMeth) * MethsT)))
(PPlusOldTrace : PPlusTrace o ls)
(HPPlusStep : PPlusStep m o upds execs calls)
(HUpdRegs : PPlusUpdRegs upds o o')
(HPPlusTrace : ls' = ((upds, (execs, calls))::ls)):
PPlusTrace o' ls'.
End PPlusTrace.
Definition PTraceList (m : Mod) (ls : list (list FullLabel)) :=
(exists (o : RegsT), PTrace m o ls).
Definition PTraceInclusion (m m' : Mod) :=
forall (o : RegsT) (ls : list (list FullLabel)),
PTrace m o ls -> exists (ls' : list (list FullLabel)), PTraceList m' ls' /\ WeakInclusions ls ls'.
Definition PStepSubstitute m o l :=
PSubsteps (BaseMod (getAllRegisters m) (getAllRules m) (getAllMethods m)) o l /\
MatchingExecCalls_Base l (getFlat m) /\
(forall s v, In (s, projT1 v) (getKindAttr (getAllMethods m)) ->
In s (getHidden m) ->
(getListFullLabel_diff (s, v) l = 0%Z)).
Definition StepSubstitute m o l :=
Substeps (BaseMod (getAllRegisters m) (getAllRules m) (getAllMethods m)) o l /\
MatchingExecCalls_Base l (getFlat m) /\
(forall s v, In (s, projT1 v) (getKindAttr (getAllMethods m)) ->
In s (getHidden m) ->
(getListFullLabel_diff (s, v) l = 0%Z)).
Definition InExec f (l: list (RegsT * (RuleOrMeth * MethsT))) :=
In (Meth f) (map getRleOrMeth l).
Definition InCall f (l: list (RegsT * (RuleOrMeth * MethsT))) :=
exists x, In x l /\ In f (snd (snd x)).
Lemma Kind_eq: forall k, Kind_dec k k = left eq_refl.
Proof.
intros; destruct (Kind_dec k k).
- f_equal.
apply Eqdep_dec.UIP_dec.
apply Kind_dec.
- apply (match n eq_refl with end).
Qed.
(*
Lemma Signature_eq: forall sig, Signature_dec sig sig = left eq_refl.
Proof.
intros; destruct (Signature_dec sig sig).
- f_equal.
apply Eqdep_dec.UIP_dec.
apply Signature_dec.
- apply (match n eq_refl with end).
Qed.
*)
Section InverseSemAction.
Variable o: RegsT.
Lemma inversionSemAction
k a reads news calls retC
(evalA: @SemAction o k a reads news calls retC):
match a with
| MCall m s e c =>
exists mret pcalls,
SemAction o (c mret) reads news pcalls retC /\
calls = (m, (existT _ _ (evalExpr e, mret))) :: pcalls
| LetExpr _ e cont =>
SemAction o (cont (evalExpr e)) reads news calls retC
| LetAction _ a cont =>
exists reads1 news1 calls1 reads2 news2 calls2 r1,
DisjKey news1 news2 /\
SemAction o a reads1 news1 calls1 r1 /\
SemAction o (cont r1) reads2 news2 calls2 retC /\
reads = reads1 ++ reads2 /\
news = news1 ++ news2 /\
calls = calls1 ++ calls2
| ReadNondet k c =>
exists rv,
SemAction o (c rv) reads news calls retC
| ReadReg r k c =>
exists rv reads2,
In (r, existT _ k rv) o /\
SemAction o (c rv) reads2 news calls retC /\
reads = (r, existT _ k rv) :: reads2
| WriteReg r k e a =>
exists pnews,
In (r, k) (getKindAttr o) /\
key_not_In r pnews /\
SemAction o a reads pnews calls retC /\
news = (r, (existT _ _ (evalExpr e))) :: pnews
| IfElse p _ aT aF c =>
exists reads1 news1 calls1 reads2 news2 calls2 r1,
DisjKey news1 news2 /\
match evalExpr p with
| true =>
SemAction o aT reads1 news1 calls1 r1 /\
SemAction o (c r1) reads2 news2 calls2 retC /\
reads = reads1 ++ reads2 /\
news = news1 ++ news2 /\
calls = calls1 ++ calls2
| false =>
SemAction o aF reads1 news1 calls1 r1 /\
SemAction o (c r1) reads2 news2 calls2 retC /\
reads = reads1 ++ reads2 /\
news = news1 ++ news2 /\
calls = calls1 ++ calls2
end
| Sys _ c =>
SemAction o c reads news calls retC
| Return e =>
retC = evalExpr e /\
news = nil /\
calls = nil /\
reads = nil
end.
Proof.
destruct evalA; eauto; repeat eexists; try destruct (evalExpr p); eauto; try discriminate.
Qed.
Lemma SemActionReadsSub k a reads upds calls ret:
@SemAction o k a reads upds calls ret ->
SubList reads o.
Proof.
induction 1; auto; subst;
unfold SubList in *; intros;
rewrite ?in_app_iff in *.
- subst; firstorder.
- repeat (subst; firstorder).
- subst.
rewrite ?in_app_iff in H1.
destruct H1; intuition.
- subst.
rewrite ?in_app_iff in H1.
destruct H1; intuition.
- subst; simpl in *; intuition.
Qed.
End InverseSemAction.
Section evalExpr.
Lemma castBits_same ty ni no (pf: ni = no) (e: Expr ty (SyntaxKind (Bit ni))): castBits pf e = match pf in _ = Y return Expr ty (SyntaxKind (Bit Y)) with
| eq_refl => e
end.
Proof.
unfold castBits.
destruct pf.
rewrite nat_cast_same.
auto.
Qed.
Lemma evalExpr_castBits: forall ni no (pf: ni = no) (e: Expr type (SyntaxKind (Bit ni))), evalExpr (castBits pf e) =
nat_cast (fun n => word n) pf (evalExpr e).
Proof.
intros.
unfold castBits.
destruct pf.
rewrite ?nat_cast_same.
auto.
Qed.
Lemma evalExpr_BinBit: forall kl kr k (op: BinBitOp kl kr k)
(l1 l2: Expr type (SyntaxKind (Bit kl)))
(r1 r2: Expr type (SyntaxKind (Bit kr))),
evalExpr l1 = evalExpr l2 ->
evalExpr r1 = evalExpr r2 ->
evalExpr (BinBit op l1 r1) = evalExpr (BinBit op l2 r2).
Proof.
intros.
induction op; simpl; try congruence.
Qed.
Lemma evalExpr_ZeroExtend: forall lsb msb (e1 e2: Expr type (SyntaxKind (Bit lsb))), evalExpr e1 = evalExpr e2 ->
evalExpr (ZeroExtend msb e1) = evalExpr (ZeroExtend msb e2).
Proof.
intros.
unfold ZeroExtend.
erewrite evalExpr_BinBit; eauto.
Qed.
Lemma evalExpr_pack_Bool: forall (e1 e2: Expr type (SyntaxKind Bool)),
evalExpr e1 = evalExpr e2 ->
evalExpr (pack e1) = evalExpr (pack e2).
Proof.
intros.
simpl.
rewrite H.
reflexivity.
Qed.
Lemma evalExpr_Void (e: Expr type (SyntaxKind (Bit 0))):
evalExpr e = WO.
Proof.
destruct (evalExpr e).
arithmetizeWord; simpl in *.
rewrite Z.mod_1_r; lia.
Qed.
Lemma evalExpr_countLeadingZeros ni: forall no (e: Expr type (SyntaxKind (Bit ni))),
evalExpr (countLeadingZeros no e) = countLeadingZerosWord _ no (evalExpr e).
Proof.
induction ni; simpl; intros; auto.
rewrite evalExpr_castBits.
simpl.
unfold wzero at 2.
rewrite wzero_wplus.
match goal with
| |- (if getBool ?P then _ else _) = (if ?P then _ else _) => destruct P; auto
end.
repeat f_equal.
rewrite IHni.
simpl.
rewrite evalExpr_castBits.
repeat f_equal.
Qed.
Lemma fin_to_nat_bound : forall n (x: Fin.t n), proj1_sig (Fin.to_nat x) < n.
Proof.
induction x; cbn; try lia.
destruct (Fin.to_nat x); cbn in *; lia.
Qed.
Lemma fin_to_word_id : forall n (i : Fin.t n),
wordToNat (natToWord (Nat.log2_up n) (proj1_sig (Fin.to_nat i))) = proj1_sig (Fin.to_nat i).
Proof.
intros.
pose proof (log2_up_pow2 n); pose proof (fin_to_nat_bound i).
rewrite wordToNat_natToWord; lia.
Qed.
Lemma eval_ReadArray_in_bounds : forall A n (arr : Expr type (SyntaxKind (Array n A))) i m,
n <= 2 ^ m ->
evalExpr
(ReadArray arr
(Var type (SyntaxKind (Bit m))
(natToWord m (proj1_sig (Fin.to_nat i))))) =
evalExpr arr i.
Proof.
intros.
simpl.
pose proof (fin_to_nat_bound i).
rewrite Z.mod_small.
rewrite Nat2Z.id.
destruct (lt_dec (proj1_sig (to_nat i)) n); try lia.
unfold evalExpr at 1.
erewrite Fin.of_nat_ext, Fin.of_nat_to_nat_inv; eauto.
split; try lia. rewrite pow2_of_nat.
apply Nat2Z.inj_lt. lia.
Qed.
Corollary eval_ReadArray_in_bounds_log : forall A n (arr : Expr type (SyntaxKind (Array n A))) i,
evalExpr
(ReadArray arr
(Var type (SyntaxKind (Bit (Nat.log2_up n)))
(natToWord (Nat.log2_up n) (proj1_sig (Fin.to_nat i))))) =
evalExpr arr i.
Proof. intros; apply eval_ReadArray_in_bounds, log2_up_pow2. Qed.
Corollary eval_ReadArray_in_bounds_pow : forall A n (arr : Expr type (SyntaxKind (Array (2 ^ n) A))) i,
evalExpr
(ReadArray arr
(Var type (SyntaxKind (Bit n))
(natToWord n (proj1_sig (Fin.to_nat i))))) =
evalExpr arr i.
Proof. intros; apply eval_ReadArray_in_bounds; auto. Qed.
End evalExpr.
Lemma seq_nil n m :
seq n m = nil ->
m = 0.
Proof.
induction m; auto; intro; exfalso.
rewrite seq_eq in H.
apply app_eq_nil in H; dest.
inv H0.
Qed.
Lemma Reduce_seq :
forall m n k,
k <= n ->
(map (fun x => x - k) (seq n m)) = (seq (n - k) m).
Proof.
induction m; intros; simpl; auto.
apply f_equal2; auto.
rewrite IHm, Nat.sub_succ_l; auto.
Qed.
Lemma getKindAttr_fst {A B : Type} {P : B -> Type} {Q : B -> Type} (l1 : list (A * {x : B & P x})):
forall (l2 : list (A * {x : B & Q x})),
getKindAttr l1 = getKindAttr l2 ->
(map fst l1) = (map fst l2).
Proof.
induction l1, l2; intros; auto; simpl in *; inv H.
erewrite IHl1; eauto.
Qed.
Lemma NoDup_app_split {A : Type} (l l' : list A) :
NoDup (l++l') ->
forall a,
In a l ->
~ In a l'.
Proof.
induction l'; repeat intro;[inv H1|].
specialize (NoDup_remove _ _ _ H) as P0; dest.
inv H1; apply H3; rewrite in_app_iff; auto.
exfalso; eapply IHl'; eauto.
Qed.
Lemma KeyMatch (l1 : RegsT) :
NoDup (map fst l1) ->
forall l2,
map fst l1 = map fst l2 ->
(forall s v, In (s, v) l1 -> In (s, v) l2) ->
l1 = l2.
Proof.
induction l1; intros.
- destruct l2; inv H0; auto.
- destruct a; simpl in *.
destruct l2; inv H0.
destruct p; simpl in *.
inv H.
specialize (H1 _ _ (or_introl (eq_refl))) as TMP; destruct TMP.
+ rewrite H in *.
assert (forall s v, In (s, v) l1 -> In (s, v) l2).
{ intros.
destruct (H1 _ _ (or_intror H0)); auto.
exfalso.
inv H2.
apply H3.
rewrite in_map_iff.
exists (s2, v); auto.
}
rewrite (IHl1 H5 _ H4 H0).
reflexivity.
+ exfalso.
apply H3.
rewrite H4, in_map_iff.
exists (s, s0); auto.
Qed.
Lemma seq_app' s e :
forall m (Hm_lte_e : m <= e),
seq s e = seq s m ++ seq (s + m) (e - m).
Proof.
induction e; intros.
- rewrite Nat.le_0_r in *; subst; simpl; reflexivity.
- destruct (le_lt_or_eq _ _ Hm_lte_e).
+ rewrite Nat.sub_succ_l; [|lia].
repeat rewrite seq_eq.
assert (s + m + (e - m) = s + e) as P0.
{ lia. }
rewrite (IHe m), app_assoc, P0; auto.
lia.
+ rewrite <- H.
rewrite Nat.sub_diag, app_nil_r; reflexivity.
Qed.
Lemma fst_getKindAttr {A B : Type} {P : B -> Type} (l : list (A * {x : B & P x})) :
map fst (getKindAttr l) = map fst l.
Proof.
induction l; simpl; auto.
rewrite IHl; reflexivity.
Qed.
Lemma key_not_In_app {A B : Type} (key : A) (ls1 ls2 : list (A * B)):
key_not_In key (ls1 ++ ls2) ->
key_not_In key ls1 /\ key_not_In key ls2.
Proof.
induction ls1; simpl; intros; split;
repeat intro; auto; eapply H; eauto; simpl; rewrite in_app_iff; eauto.
inv H0; eauto.
Qed.
Lemma key_not_In_app_iff {A B : Type} (key : A) (ls1 ls2 : list (A * B)):
key_not_In key (ls1 ++ ls2) <-> key_not_In key ls1 /\ key_not_In key ls2.
Proof.
split; eauto using key_not_In_app.
repeat intro; dest.
rewrite in_app_iff in H0.
destruct H0.
- eapply H; eauto.
- eapply H1; eauto.
Qed.
Lemma existsb_nexists_str str l :
existsb (String.eqb str) l = false <->
~ In str l.
Proof.
split; repeat intro.
- assert (exists x, In x l /\ (String.eqb str) x = true) as P0.
{ exists str; split; auto. apply String.eqb_refl. }
rewrite <- existsb_exists in P0; rewrite P0 in *; discriminate.
- remember (existsb _ _) as exb; symmetry in Heqexb; destruct exb; auto.
exfalso; rewrite existsb_exists in Heqexb; dest.
rewrite String.eqb_eq in *; subst; auto.
Qed.
Lemma nth_error_map_None_iff :
forall {A B : Type} (f : A -> B) (l : list A) (n : nat),
nth_error l n = None <-> nth_error (map f l) n = None.
Proof.
intros; split; intros; rewrite nth_error_None, map_length in *; assumption.
Qed.
Lemma nth_error_map_Some1 :
forall {A B : Type} (f : A -> B) (l : list A) (b : B) (n : nat),
nth_error (map f l) n = Some b -> exists a, nth_error l n = Some a /\ (f a = b).
Proof.
intros.
specialize (nth_error_map f (fun b => nth_error (map f l) n = Some b) n l) as P0.
rewrite H in P0.
remember (nth_error l _) as err0; symmetry in Heqerr0; destruct err0.
- exists a; split; auto.
destruct P0 as [P0 P1].
specialize (P0 eq_refl); inv P0; reflexivity.
- exfalso.
rewrite nth_error_None in Heqerr0.
enough (Some b <> None).
{ eapply H0; rewrite <- H.
rewrite nth_error_None, map_length; assumption. }
intro; discriminate.
Qed.
Lemma nth_error_map_Some2 :
forall {A B : Type} (f : A -> B) (l : list A) (b : B) (n : nat),
(exists a, nth_error l n = Some a /\ (f a = b)) -> nth_error (map f l) n = Some b.
Proof.
intros; dest.
rewrite <- H0; eapply map_nth_error; eauto.
Qed.
Lemma nth_error_map_iff :
forall {A B : Type} (f : A -> B) (l : list A) (b : B) (n : nat),
nth_error (map f l) n = Some b <-> (exists a, nth_error l n = Some a /\ (f a = b)).
Proof.
repeat red; intros; dest; eauto using nth_error_map_Some1, nth_error_map_Some2.
Qed.
Lemma nth_error_nil_None :
forall {A : Type} (n : nat),
nth_error (nil : list A) n = None.
Proof.
intros; rewrite nth_error_None; simpl; lia.
Qed.
Lemma SubList_map_iff {A B : Type} (f : A -> B) (l' : list B) :
forall (l : list A),
SubList l' (map f l) <->
exists l'',
SubList l'' l /\
(map f l'' = l').
Proof.
intros; split.
- induction l'; simpl; intros.
+ exists nil; simpl; split; repeat intro; auto.
destruct l; auto.
exfalso; inv H0.
+ unfold SubList in *; simpl in *.
specialize (IHl' (ltac : (eauto))); dest.
specialize (H _ (or_introl eq_refl)); rewrite in_map_iff in H; dest.
exists (x0 :: x); split; intros; [inv H3; auto|].
simpl; apply f_equal2; assumption.
- repeat intro; dest.
rewrite <- H1 in H0.
rewrite in_map_iff in *; dest.
specialize (H _ H2).
exists x1; split; assumption.
Qed.
Lemma KeyPair_Equiv {A B : Type} (l : list (A * B)) :
NoDup (map fst l) ->
forall l',
SubList l l' ->
map fst l = map fst l' ->
l = l'.
Proof.
induction l; simpl; intros.
- rewrite (map_eq_nil _ _ (eq_sym H1)); reflexivity.
- destruct l'; [discriminate|].
apply f_equal2; simpl in *.
+ assert (In a (p :: l')).
{ apply H0; left; reflexivity. }
inv H2; eauto.
exfalso.
apply (in_map fst) in H3; rewrite H1 in H; inv H1.
rewrite <- H4 in H; inv H; contradiction.
+ enough (SubList l l').
{ inv H; inv H1; eapply IHl; eauto. }
repeat intro.
specialize (H0 _ (in_cons _ _ _ H2)).
inv H0; eauto.
exfalso.
apply (in_map fst) in H2.
inv H1; rewrite H3 in H; inv H; contradiction.
Qed.
Lemma getNumCalls_nil f :
getNumCalls f nil = 0%Z.
Proof.
reflexivity.
Qed.
Lemma getNumExecs_nil f :
getNumExecs f nil = 0%Z.
Proof.
reflexivity.
Qed.
Lemma getNumFromCalls_eq_cons f g l :
f = g ->
getNumFromCalls f (g::l) = (1 + (getNumFromCalls f l))%Z.
Proof.
intro;unfold getNumFromCalls; destruct MethT_dec; auto; contradiction.
Qed.
Lemma getNumFromCalls_neq_cons f g l :
f <> g ->
getNumFromCalls f (g::l) = getNumFromCalls f l.
Proof.
intro; unfold getNumFromCalls; destruct MethT_dec; auto; contradiction.
Qed.
Opaque getNumFromCalls.
Lemma getNumFromCalls_app f l1:
forall l2,
getNumFromCalls f (l1++l2) = (getNumFromCalls f l1 + getNumFromCalls f l2)%Z.
Proof.
induction l1.
- simpl; reflexivity.
- intros.
destruct (MethT_dec f a).
+ simpl; repeat rewrite getNumFromCalls_eq_cons; auto.
rewrite IHl1; ring.
+ simpl; repeat rewrite getNumFromCalls_neq_cons; auto.
Qed.
Transparent getNumFromCalls.
Corollary getNumCalls_app f l1 :
forall l2,
getNumCalls f (l1 ++ l2) = (getNumCalls f l1 + getNumCalls f l2)%Z.
Proof.
unfold getNumCalls.
intro.
rewrite map_app, concat_app, getNumFromCalls_app.
reflexivity.
Qed.
Lemma getNumCalls_cons f a l :
getNumCalls f (a::l) = ((getNumFromCalls f (snd (snd a))) + getNumCalls f l)%Z.
Proof.
unfold getNumCalls.
simpl; rewrite getNumFromCalls_app; reflexivity.
Qed.
Transparent getNumFromCalls.
Lemma getNumFromCalls_nonneg f l :
(0 <= getNumFromCalls f l)%Z.
Proof.
induction l.
- unfold getNumFromCalls; reflexivity.
- destruct (MethT_dec f a);[rewrite getNumFromCalls_eq_cons; auto| rewrite getNumFromCalls_neq_cons; auto].
Omega.omega.
Qed.
Lemma getNumCalls_nonneg f l:
(0 <= (getNumCalls f l))%Z.
Proof.
induction l.
- rewrite getNumCalls_nil;reflexivity.
- rewrite getNumCalls_cons.
specialize (getNumFromCalls_nonneg f (snd (snd a))) as B1.
Omega.omega.
Qed.
Lemma getNumFromExecs_eq_cons f g l :
f = g ->
getNumFromExecs f ((Meth g)::l) = (1 + (getNumFromExecs f l))%Z.
Proof.
intros; simpl; destruct (MethT_dec f g); auto; contradiction.
Qed.
Lemma getNumFromExecs_neq_cons f g l :
f <> g ->
getNumFromExecs f ((Meth g)::l) = (getNumFromExecs f l).
Proof.
intros; simpl; destruct (MethT_dec f g); auto; contradiction.
Qed.
Lemma getNumFromExecs_Rle_cons f rn l:
getNumFromExecs f ((Rle rn)::l) = (getNumFromExecs f l).
Proof.
intros; simpl; reflexivity.
Qed.
Opaque getNumFromExecs.
Lemma getNumFromExecs_app f l1:
forall l2,
getNumFromExecs f (l1++l2) = (getNumFromExecs f l1 + getNumFromExecs f l2)%Z.
Proof.
induction l1.
- simpl; reflexivity.
- intros; destruct a;[|destruct (MethT_dec f f0)];simpl.
+ repeat rewrite getNumFromExecs_Rle_cons; apply IHl1.
+ repeat rewrite getNumFromExecs_eq_cons; auto.
rewrite IHl1; ring.
+ repeat rewrite getNumFromExecs_neq_cons; auto.
Qed.
Transparent getNumFromExecs.
Corollary getNumExecs_app f l1 :
forall l2,
getNumExecs f (l1++l2) = (getNumExecs f l1 + getNumExecs f l2)%Z.
Proof.
unfold getNumExecs.
intros;rewrite map_app, getNumFromExecs_app; reflexivity.
Qed.
Lemma getNumFromExecs_nonneg f l:
(0 <= (getNumFromExecs f l))%Z.
Proof.
induction l.
- simpl; reflexivity.
- destruct a;[rewrite getNumFromExecs_Rle_cons
|destruct (MethT_dec f f0);[rewrite getNumFromExecs_eq_cons
|rewrite getNumFromExecs_neq_cons]]; auto; Omega.omega.
Qed.
Corollary getNumExecs_nonneg f l :
(0 <= (getNumExecs f l))%Z.
Proof.
unfold getNumExecs;apply getNumFromExecs_nonneg.
Qed.
Lemma getNumFromCalls_perm f l l':
l [=] l' ->
getNumFromCalls f l = getNumFromCalls f l'.
Proof.
induction 1; auto.
- destruct (MethT_dec f x);[repeat rewrite getNumFromCalls_eq_cons| repeat rewrite getNumFromCalls_neq_cons];auto.
rewrite IHPermutation; reflexivity.
- destruct (MethT_dec f x), (MethT_dec f y).
+ repeat rewrite getNumFromCalls_eq_cons; auto.
+ rewrite getNumFromCalls_neq_cons, getNumFromCalls_eq_cons, getNumFromCalls_eq_cons, getNumFromCalls_neq_cons ; auto.
+ rewrite getNumFromCalls_eq_cons, getNumFromCalls_neq_cons, getNumFromCalls_neq_cons, getNumFromCalls_eq_cons ; auto.
+ repeat rewrite getNumFromCalls_neq_cons; auto.
- rewrite IHPermutation1, IHPermutation2; reflexivity.
Qed.
Global Instance getNumFromCalls_perm_rewrite' :
Proper (eq ==> @Permutation (MethT) ==> eq) (@getNumFromCalls) | 10.
Proof.
repeat red; intros; subst; eauto using getNumFromCalls_perm.
Qed.
Lemma concat_perm_rewrite (A : Type) (l l' : list (list A)):
l [=] l' ->
concat l [=] concat l'.
Proof.
induction 1.
- reflexivity.
- simpl; rewrite IHPermutation; reflexivity.
- simpl; repeat rewrite app_assoc.
apply Permutation_app_tail, Permutation_app_comm.
- eauto using Permutation_trans.
Qed.
Global Instance concat_perm_rewrite' {A : Type}:
Proper (@Permutation (list A) ==> @Permutation A) (@concat A) | 10.
Proof.
repeat red; eauto using concat_perm_rewrite.
Qed.
Corollary getNumCalls_perm_rewrite f l l':
l [=] l' ->
getNumCalls f l = getNumCalls f l'.
Proof.
unfold getNumCalls.
intros; rewrite H; reflexivity.
Qed.
Global Instance getNumCalls_perm_rewrite' :
Proper (eq ==> @Permutation FullLabel ==> eq) (@getNumCalls) | 10.
Proof.
repeat red; intros; subst; eauto using getNumCalls_perm_rewrite.
Qed.
Lemma getNumFromExecs_perm f l l':
l [=] l' ->
getNumFromExecs f l = getNumFromExecs f l'.
Proof.
induction 1; auto.
- destruct x;[repeat rewrite getNumFromExecs_Rle_cons;rewrite IHPermutation; reflexivity|].
destruct (MethT_dec f f0);[repeat rewrite getNumFromExecs_eq_cons| repeat rewrite getNumFromExecs_neq_cons];auto.
rewrite IHPermutation; reflexivity.
- destruct x, y.
+ repeat rewrite getNumFromExecs_Rle_cons; reflexivity.
+ destruct (MethT_dec f f0).
* rewrite getNumFromExecs_eq_cons, getNumFromExecs_Rle_cons, getNumFromExecs_Rle_cons, getNumFromExecs_eq_cons; auto.
* rewrite getNumFromExecs_neq_cons, getNumFromExecs_Rle_cons, getNumFromExecs_Rle_cons, getNumFromExecs_neq_cons; auto.
+ destruct (MethT_dec f f0).
* rewrite getNumFromExecs_eq_cons, getNumFromExecs_Rle_cons, getNumFromExecs_Rle_cons, getNumFromExecs_eq_cons; auto.
* rewrite getNumFromExecs_neq_cons, getNumFromExecs_Rle_cons, getNumFromExecs_Rle_cons, getNumFromExecs_neq_cons; auto.
+ destruct (MethT_dec f f0), (MethT_dec f f1).
* repeat rewrite getNumFromExecs_eq_cons; auto.
* rewrite getNumFromExecs_neq_cons, getNumFromExecs_eq_cons, getNumFromExecs_eq_cons, getNumFromExecs_neq_cons; auto.
* rewrite getNumFromExecs_eq_cons, getNumFromExecs_neq_cons, getNumFromExecs_neq_cons, getNumFromExecs_eq_cons; auto.
* repeat rewrite getNumFromExecs_neq_cons; auto.
- eauto using eq_trans.
Qed.
Global Instance getNumFromExecs_perm_rewrite' :
Proper (eq ==> @Permutation RuleOrMeth ==> eq) (@getNumFromExecs) | 10.
Proof.
repeat red; intros; subst; eauto using getNumFromExecs_perm.
Qed.