forked from achlipala/frap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompilerCorrectness.v
1672 lines (1443 loc) · 48.7 KB
/
CompilerCorrectness.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
(** Formal Reasoning About Programs <http://adam.chlipala.net/frap/>
* Chapter 10: Compiler Correctness
* Author: Adam Chlipala
* License: https://creativecommons.org/licenses/by-nc-nd/4.0/ *)
Require Import Frap.
Set Implicit Arguments.
(* Let's look at another example of what we can model with operational
* semantics: correctness of compiler transformations. Our inspiration here is
* the seminal project CompCert, which uses Coq to verify a realistic C
* compiler. We will adopt the same *simulation*-based techniques as CompCert,
* albeit on a simpler language and with simpler compiler phases. We'll stick
* to transformations from the source language to itself, since that's enough to
* illustrate the big ideas. Here's the object language that we'll use, which
* is _almost_ the same as from Chapter 8. *)
Inductive arith : Set :=
| Const (n : nat)
| Var (x : var)
| Plus (e1 e2 : arith)
| Minus (e1 e2 : arith)
| Times (e1 e2 : arith).
Inductive cmd :=
| Skip
| Assign (x : var) (e : arith)
| Sequence (c1 c2 : cmd)
| If (e : arith) (then_ else_ : cmd)
| While (e : arith) (body : cmd)
| Output (e : arith).
(* The last constructor above is the new one, for generating an _output_ value,
* say to display in a terminal. By including this operation, we create
* interesting differences between the behaviors of different nonterminating
* programs. A correct compiler should preserve these differences. *)
(* The next span of notations and definitions is the same as from Chapter 8. *)
Coercion Const : nat >-> arith.
Coercion Var : var >-> arith.
(*Declare Scope arith_scope.*)
Infix "+" := Plus : arith_scope.
Infix "-" := Minus : arith_scope.
Infix "*" := Times : arith_scope.
Delimit Scope arith_scope with arith.
Notation "x <- e" := (Assign x e%arith) (at level 75).
Infix ";;" := Sequence (at level 76). (* This one changed slightly, to avoid parsing clashes. *)
Notation "'when' e 'then' then_ 'else' else_ 'done'" := (If e%arith then_ else_) (at level 75, e at level 0).
Notation "'while' e 'loop' body 'done'" := (While e%arith body) (at level 75).
Definition valuation := fmap var nat.
Fixpoint interp (e : arith) (v : valuation) : nat :=
match e with
| Const n => n
| Var x =>
match v $? x with
| None => 0
| Some n => n
end
| Plus e1 e2 => interp e1 v + interp e2 v
| Minus e1 e2 => interp e1 v - interp e2 v
| Times e1 e2 => interp e1 v * interp e2 v
end.
Inductive context :=
| Hole
| CSeq (C : context) (c : cmd).
Inductive plug : context -> cmd -> cmd -> Prop :=
| PlugHole : forall c, plug Hole c c
| PlugSeq : forall c C c' c2,
plug C c c'
-> plug (CSeq C c2) c (Sequence c' c2).
(* Here's our first difference. We add a new parameter to [step0], giving a
* _label_ that records which _externally visible effect_ the step has. For
* this language, output is the only externally visible effect, so a label
* records an optional output value. Including this element makes our semantics
* a _labelled transition system_. *)
Inductive step0 : valuation * cmd -> option nat -> valuation * cmd -> Prop :=
| Step0Assign : forall v x e,
step0 (v, Assign x e) None (v $+ (x, interp e v), Skip)
| Step0Seq : forall v c2,
step0 (v, Sequence Skip c2) None (v, c2)
| Step0IfTrue : forall v e then_ else_,
interp e v <> 0
-> step0 (v, If e then_ else_) None (v, then_)
| Step0IfFalse : forall v e then_ else_,
interp e v = 0
-> step0 (v, If e then_ else_) None (v, else_)
| Step0WhileTrue : forall v e body,
interp e v <> 0
-> step0 (v, While e body) None (v, Sequence body (While e body))
| Step0WhileFalse : forall v e body,
interp e v = 0
-> step0 (v, While e body) None (v, Skip)
| Step0Output : forall v e,
step0 (v, Output e) (Some (interp e v)) (v, Skip).
(* It's easy to push labels through steps with context. *)
Inductive cstep : valuation * cmd -> option nat -> valuation * cmd -> Prop :=
| CStep : forall C v c l v' c' c1 c2,
plug C c c1
-> step0 (v, c) l (v', c')
-> plug C c' c2
-> cstep (v, c1) l (v', c2).
(* To characterize correct compilation, it is helpful to define a relation to
* capture which output _traces_ a command might generate. Note that, for us, a
* trace is a list of output values and/or termination markers. We drop silent
* labels as we run, and we use [Some n] for outputting of [n] and [None] for
* termination. *)
Inductive generate : valuation * cmd -> list (option nat) -> Prop :=
| GenDone : forall vc,
generate vc []
| GenSkip : forall v,
generate (v, Skip) [None]
| GenSilent : forall vc vc' ns,
cstep vc None vc'
-> generate vc' ns
-> generate vc ns
| GenOutput : forall vc n vc' ns,
cstep vc (Some n) vc'
-> generate vc' ns
-> generate vc (Some n :: ns).
Local Hint Constructors plug step0 cstep generate : core.
(* Notice that [generate] is defined so that, for any two of a starting state's
* traces, one is a prefix of the other. The same wouldn't necessarily hold if
* our language were nondeterministic. *)
(* We define trace inclusion to capture the notion of
* _preserving all behaviors_. *)
Definition traceInclusion (vc1 vc2 : valuation * cmd) :=
forall ns, generate vc1 ns -> generate vc2 ns.
Infix "<|" := traceInclusion (at level 70).
(* And trace equivalence captures _having the same behaviors_. *)
Definition traceEquivalence (vc1 vc2 : valuation * cmd) :=
vc1 <| vc2 /\ vc2 <| vc1.
Infix "=|" := traceEquivalence (at level 70).
(* Trace equivalence is an appropriate notion of correctness, to relate the
* "before" and "after" programs of a compiler transformation. Note that here
* we break from our usual modus operandi, as we will not be using invariants to
* characterize correctness! This kind of trace equivalence is one of the other
* big concepts that competes with invariants to unify different correctness
* notions. *)
(* Here's a simple example program, which outputs how many days have elapsed at
* the end of each one-month period (with a simplified notion of "month"!). *)
Definition daysPerWeek := 7.
Definition weeksPerMonth := 4.
Definition daysPerMonth := (daysPerWeek * weeksPerMonth)%arith.
(* We are purposely building an expression with arithmetic that can be resolved
* at compile time, to give our optimizations something to do. *)
Example month_boundaries_in_days :=
"acc" <- 0;;
while 1 loop
when daysPerMonth then
"acc" <- "acc" + daysPerMonth;;
Output "acc"
else
(* Oh no! We must have calculated it wrong, since we got zero! *)
(* And, yes, we know this spot can never be reached. Some of our
* optimizations will prove it for us! *)
Skip
done
done.
(* Moderately laboriously, we can prove a particular example trace for this
* program, including its first two outputs. Traces of all lengths exist,
* because the program does not terminate, generating new output infinitely
* often. *)
Local Hint Extern 1 (interp _ _ = _) => simplify; equality : core.
Local Hint Extern 1 (interp _ _ <> _) => simplify; equality : core.
Theorem first_few_values :
generate ($0, month_boundaries_in_days) [Some 28; Some 56].
Proof.
unfold month_boundaries_in_days.
eapply GenSilent.
eapply CStep with (C := CSeq Hole _); eauto.
eapply GenSilent.
eapply CStep with (C := Hole); eauto.
eapply GenSilent.
eapply CStep with (C := Hole); eauto.
eapply GenSilent.
eapply CStep with (C := CSeq Hole _); eauto.
eapply GenSilent.
eapply CStep with (C := CSeq (CSeq Hole _) _); eauto.
eapply GenSilent.
eapply CStep with (C := CSeq Hole _); eauto.
eapply GenOutput.
eapply CStep with (C := CSeq Hole _); eauto.
replace 28 with (interp "acc"
($0 $+ ("acc", interp 0 $0)
$+ ("acc", interp ("acc" + daysPerMonth)%arith ($0 $+ ("acc", interp 0 $0))))); eauto.
eapply GenSilent.
eapply CStep with (C := Hole); eauto.
eapply GenSilent.
eapply CStep with (C := Hole); eauto.
eapply GenSilent.
eapply CStep with (C := CSeq Hole _); eauto.
eapply GenSilent.
eapply CStep with (C := CSeq (CSeq Hole _) _); eauto.
eapply GenSilent.
eapply CStep with (C := CSeq Hole _); eauto.
eapply GenOutput.
eapply CStep with (C := CSeq Hole _); eauto.
replace 56 with (interp "acc"
($0 $+ ("acc", interp 0 $0) $+ ("acc",
interp ("acc" + daysPerMonth)%arith ($0 $+ ("acc", interp 0 $0))) $+ ("acc",
interp ("acc" + daysPerMonth)%arith
($0 $+ ("acc", interp 0 $0) $+ ("acc",
interp ("acc" + daysPerMonth)%arith ($0 $+ ("acc", interp 0 $0))))))); eauto.
constructor.
Qed.
(** * Basic Simulation Arguments and Optimizing Expressions *)
(* Let's define an optimization that just simplifies expressions. *)
Fixpoint cfoldArith (e : arith) : arith :=
match e with
| Const _ => e
| Var _ => e
| Plus e1 e2 =>
let e1' := cfoldArith e1 in
let e2' := cfoldArith e2 in
match e1', e2' with
| Const n1, Const n2 => Const (n1 + n2)
| _, _ => Plus e1' e2'
end
| Minus e1 e2 =>
let e1' := cfoldArith e1 in
let e2' := cfoldArith e2 in
match e1', e2' with
| Const n1, Const n2 => Const (n1 - n2)
| _, _ => Minus e1' e2'
end
| Times e1 e2 =>
let e1' := cfoldArith e1 in
let e2' := cfoldArith e2 in
match e1', e2' with
| Const n1, Const n2 => Const (n1 * n2)
| _, _ => Times e1' e2'
end
end.
Theorem cfoldArith_ok : forall v e,
interp (cfoldArith e) v = interp e v.
Proof.
induct e; simplify; try equality;
repeat (match goal with
| [ |- context[match ?E with _ => _ end] ] => cases E
| [ H : _ = interp _ _ |- _ ] => rewrite <- H
end; simplify); subst; ring.
Qed.
Fixpoint cfoldExprs (c : cmd) : cmd :=
match c with
| Skip => c
| Assign x e => Assign x (cfoldArith e)
| Sequence c1 c2 => Sequence (cfoldExprs c1) (cfoldExprs c2)
| If e then_ else_ => If (cfoldArith e) (cfoldExprs then_) (cfoldExprs else_)
| While e body => While (cfoldArith e) (cfoldExprs body)
| Output e => Output (cfoldArith e)
end.
(* Here's what our optimization does to the example program. *)
Compute cfoldExprs month_boundaries_in_days.
(* It's actually not obvious how to prove trace equivalence for this kind of
* optimization, and we should be on the lookout for general principles that
* help us avoid rehashing the same argument structure for each optimization.
* To let us prove such principles, we first establish a few key properties of
* the object language. *)
(* First, any program that isn't a [Skip] can make progress. *)
Theorem skip_or_step : forall v c,
c = Skip
\/ exists v' l c', cstep (v, c) l (v', c').
Proof.
induct c; simplify; first_order; subst;
try match goal with
| [ H : cstep _ _ _ |- _ ] => invert H
end;
try match goal with
| [ |- context[cstep (?v, If ?e _ _)] ] => cases (interp e v ==n 0)
| [ |- context[cstep (?v, While ?e _)] ] => cases (interp e v ==n 0)
end; eauto 10.
Qed.
(* Now, a set of (boring) lemmas relevant to contexts: *)
Theorem plug_function : forall C c1 c2, plug C c1 c2
-> forall c2', plug C c1 c2'
-> c2 = c2'.
Proof.
induct 1; invert 1; eauto.
apply IHplug in H5.
equality.
Qed.
Lemma peel_cseq : forall C1 C2 c (c1 c2 : cmd),
C1 = C2 /\ c1 = c2
-> CSeq C1 c = CSeq C2 c /\ c1 = c2.
Proof.
equality.
Qed.
Local Hint Resolve peel_cseq : core.
Lemma plug_deterministic : forall v C c1 c2, plug C c1 c2
-> forall l vc1, step0 (v, c1) l vc1
-> forall C' c1', plug C' c1' c2
-> forall l' vc1', step0 (v, c1') l' vc1'
-> C' = C /\ c1' = c1.
Proof.
induct 1; invert 1; invert 1; invert 1; auto;
try match goal with
| [ H : plug _ _ _ |- _ ] => invert1 H
end; eauto.
Qed.
(* Finally, the big theorem we are after: the [cstep] relation is
* deterministic. *)
Lemma deterministic0 : forall vc l vc',
step0 vc l vc'
-> forall l' vc'', step0 vc l' vc''
-> l = l' /\ vc'' = vc'.
Proof.
invert 1; invert 1; simplify; propositional.
Qed.
Theorem deterministic : forall vc l vc',
cstep vc l vc'
-> forall l' vc'', cstep vc l' vc''
-> l = l' /\ vc' = vc''.
Proof.
invert 1; invert 1; simplify.
eapply plug_deterministic in H0; eauto.
invert H0.
match goal with
| [ H : step0 _ _ _, H' : step0 _ _ _ |- _ ] => eapply deterministic0 in H; [ | apply H' ]
end.
propositional; subst; auto.
invert H0.
auto.
eapply plug_function in H2; eauto.
equality.
Qed.
(* OK, we are ready for the first variant of today's big proof technique,
* _simulation_. The method is much like with invariants. Recall that, in our
* old workhorse technique, we pick a predicate over states, and we show that
* all steps preserve it. Simulation is very similar, but now we have a
* two-argument predicate or _relation_ between states of two systems. The
* relation is a simulation when it is able to track execution in one system by
* playing appropriate steps in the other. For deterministic systems like ours,
* the existence of a simulation implies trace equivalence. *)
Section simulation.
(* Here's the kind of relation we assume. *)
Variable R : valuation * cmd -> valuation * cmd -> Prop.
(* Starting from two related states, when the lefthand one makes a step, the
* righthand one can make a matching step, such that the new states are also
* related. *)
Hypothesis one_step : forall vc1 vc2, R vc1 vc2
-> forall vc1' l, cstep vc1 l vc1'
-> exists vc2', cstep vc2 l vc2' /\ R vc1' vc2'.
(* When a righthand command is related to [Skip], it must be [Skip], too. *)
Hypothesis agree_on_termination : forall v1 v2 c2, R (v1, Skip) (v2, c2)
-> c2 = Skip.
(* First (easy) step: [R] implies left-to-right trace inclusion. *)
Lemma simulation_fwd' : forall vc1 ns, generate vc1 ns
-> forall vc2, R vc1 vc2
-> generate vc2 ns.
Proof.
induct 1; simplify; eauto.
cases vc2.
apply agree_on_termination in H; subst.
auto.
eapply one_step in H; eauto.
first_order.
eauto.
eapply one_step in H1; eauto.
first_order.
eauto.
Qed.
Theorem simulation_fwd : forall vc1 vc2, R vc1 vc2
-> vc1 <| vc2.
Proof.
unfold traceInclusion; eauto using simulation_fwd'.
Qed.
(* Second (slightly harder) step: [R] implies right-to-left trace
* inclusion. *)
Lemma simulation_bwd' : forall vc2 ns, generate vc2 ns
-> forall vc1, R vc1 vc2
-> generate vc1 ns.
Proof.
induct 1; simplify; eauto.
cases vc1.
assert (c = Skip \/ exists v' l c', cstep (v0, c) l (v', c')) by apply skip_or_step.
first_order; subst.
auto.
eapply one_step in H; eauto.
first_order.
invert H.
invert H4.
invert H5.
cases vc1; cases vc.
assert (c = Skip \/ exists v' l c', cstep (v, c) l (v', c')) by apply skip_or_step.
first_order; subst.
apply agree_on_termination in H1; subst.
invert H.
invert H3.
invert H4.
specialize (one_step H1 H2).
first_order.
eapply deterministic in H; eauto.
propositional; subst.
eauto.
cases vc1; cases vc.
assert (c = Skip \/ exists v' l c', cstep (v, c) l (v', c')) by apply skip_or_step.
first_order; subst.
apply agree_on_termination in H1; subst.
invert H.
invert H3.
invert H4.
specialize (one_step H1 H2).
first_order.
eapply deterministic in H; eauto.
propositional; subst.
eauto.
Qed.
Theorem simulation_bwd : forall vc1 vc2, R vc1 vc2
-> vc2 <| vc1.
Proof.
unfold traceInclusion; eauto using simulation_bwd'.
Qed.
(* Put them together, and we have trace equivalence. *)
Theorem simulation : forall vc1 vc2, R vc1 vc2
-> vc1 =| vc2.
Proof.
simplify; split; auto using simulation_fwd, simulation_bwd.
Qed.
End simulation.
(* Now to prove our particular optimization. First, original steps can be
* lifted into optimized steps. *)
Lemma cfoldExprs_ok' : forall v1 c1 l v2 c2,
step0 (v1, c1) l (v2, c2)
-> step0 (v1, cfoldExprs c1) l (v2, cfoldExprs c2).
Proof.
invert 1; simplify;
try match goal with
| [ _ : context[interp ?e ?v] |- _ ] => rewrite <- (cfoldArith_ok v e) in *
| [ |- context[interp ?e ?v] ] => rewrite <- (cfoldArith_ok v e)
end; eauto.
Qed.
(* It helps to add optimization on contexts, as a proof device. *)
Fixpoint cfoldExprsContext (C : context) : context :=
match C with
| Hole => Hole
| CSeq C c => CSeq (cfoldExprsContext C) (cfoldExprs c)
end.
(* The optimization can be applied over [plug]. *)
Lemma plug_cfoldExprs1 : forall C c1 c2, plug C c1 c2
-> plug (cfoldExprsContext C) (cfoldExprs c1) (cfoldExprs c2).
Proof.
induct 1; simplify; eauto.
Qed.
Local Hint Resolve plug_cfoldExprs1 : core.
(* The main correctness property! *)
Theorem cfoldExprs_ok : forall v c,
(v, c) =| (v, cfoldExprs c).
Proof.
simplify.
(* Notice our clever choice of a simulation relation here, much as we often
* choose strengthened invariants. We basically just recast the theorem
* statement as a two-state predicate using equality. *)
apply simulation with (R := fun vc1 vc2 => fst vc1 = fst vc2
/\ snd vc2 = cfoldExprs (snd vc1));
simplify; propositional.
invert H0; simplify; subst.
apply cfoldExprs_ok' in H3.
cases vc2; simplify; subst.
eauto 7.
Qed.
(** * Simulations That Allow Skipping Steps *)
(* Here's a reasonable variant of the last optimization: when an [If] test
* expression reduces to a constant, replace the [If] with whichever branch is
* guaranteed to run. *)
Fixpoint cfold (c : cmd) : cmd :=
match c with
| Skip => c
| Assign x e => Assign x (cfoldArith e)
| Sequence c1 c2 => Sequence (cfold c1) (cfold c2)
| If e then_ else_ =>
let e' := cfoldArith e in
match e' with
| Const n => if n ==n 0 then cfold else_ else cfold then_
| _ => If e' (cfold then_) (cfold else_)
end
| While e body => While (cfoldArith e) (cfold body)
| Output e => Output (cfoldArith e)
end.
(* Here's how our running example optimizes further. *)
Compute cfold month_boundaries_in_days.
(* It will be helpful to have a shorthand for steps that don't generate output.
* [Notation] is a useful way to introduce a shorthand so that it looks exactly
* the same as its expansion, to all Coq tactics. *)
Notation silent_cstep := (fun a b => cstep a None b).
(* Silent steps have a few interesting properties, proved here. *)
Lemma silent_generate_fwd : forall ns vc vc',
silent_cstep^* vc vc'
-> generate vc ns
-> generate vc' ns.
Proof.
induct 1; simplify; eauto.
invert H1; auto.
invert H.
invert H3.
invert H4.
eapply deterministic in H; eauto.
propositional; subst.
auto.
eapply deterministic in H; eauto.
equality.
Qed.
Lemma silent_generate_bwd : forall ns vc vc',
silent_cstep^* vc vc'
-> generate vc' ns
-> generate vc ns.
Proof.
induct 1; eauto.
Qed.
Lemma generate_Skip : forall v a ns,
generate (v, Skip) (Some a :: ns)
-> False.
Proof.
induct 1; simplify.
invert H.
invert H3.
invert H4.
invert H.
invert H3.
invert H4.
Qed.
Local Hint Resolve silent_generate_fwd silent_generate_bwd generate_Skip : core.
(* You might have noticed that our old notion of simulation doesn't apply to the
* new optimization. The reason is that, because the optimized program skips
* some steps, some steps in the source program may not have matching steps in
* the optimized program. Let's extend simulation to allow skipped steps. *)
Section simulation_skipping.
(* Now the relation takes a number as an argument. The idea is that, for
* [R n vc1 vc2], at most [n] steps of [vc1] may go unmatched by [vc2], before
* we finally find one that matches. It is an interesting exercise to work
* out why, without tracking such quantities, this notion of simulation
* wouldn't imply trace equivalence! *)
Variable R : nat -> valuation * cmd -> valuation * cmd -> Prop.
(* Now this key hypothesis has two cases. *)
Hypothesis one_step : forall n vc1 vc2, R n vc1 vc2
-> forall vc1' l, cstep vc1 l vc1'
(* Case 1: Skipping a (silent!) step, decreasing [n] *)
-> (exists n', n = S n' /\ l = None /\ R n' vc1' vc2)
(* Case 2: Matching the step like before; note how [n]
* resets to an arbitrary new limit! *)
\/ exists n' vc2', cstep vc2 l vc2' /\ R n' vc1' vc2'.
Hypothesis agree_on_termination : forall n v1 v2 c2, R n (v1, Skip) (v2, c2)
-> c2 = Skip.
(* The forward direction is just as easy to prove. *)
Lemma simulation_skipping_fwd' : forall vc1 ns, generate vc1 ns
-> forall n vc2, R n vc1 vc2
-> generate vc2 ns.
Proof.
induct 1; simplify; eauto.
cases vc2.
apply agree_on_termination in H.
subst.
auto.
eapply one_step in H; eauto.
first_order.
eauto.
eapply one_step in H1; eauto.
first_order.
equality.
eauto.
Qed.
Theorem simulation_skipping_fwd : forall n vc1 vc2, R n vc1 vc2
-> vc1 <| vc2.
Proof.
unfold traceInclusion; eauto using simulation_skipping_fwd'.
Qed.
(* This one isn't so obvious: a step on the right can now be matched by
* _one or more_ steps on the left, preserving [R]. *)
Lemma match_step : forall n vc2 l vc2' vc1,
cstep vc2 l vc2'
-> R n vc1 vc2
-> exists vc1' vc1'' n', silent_cstep^* vc1 vc1'
/\ cstep vc1' l vc1''
/\ R n' vc1'' vc2'.
Proof.
induct n; simplify.
cases vc1; cases vc2.
assert (c = Skip \/ exists v' l' c', cstep (v, c) l' (v', c')) by apply skip_or_step.
first_order; subst.
apply agree_on_termination in H0; subst.
invert H.
invert H2.
invert H3.
eapply one_step in H0; eauto.
first_order; subst.
equality.
eapply deterministic in H; eauto.
first_order; subst.
eauto 6.
cases vc1; cases vc2.
assert (c = Skip \/ exists v' l' c', cstep (v, c) l' (v', c')) by apply skip_or_step.
first_order; subst.
apply agree_on_termination in H0; subst.
invert H.
invert H2.
invert H3.
eapply one_step in H0; eauto.
first_order; subst.
invert H0.
eapply IHn in H3; eauto.
first_order.
eauto 8.
eapply deterministic in H; eauto.
first_order; subst.
eauto 6.
Qed.
Lemma step_to_termination : forall vc v,
silent_cstep^* vc (v, Skip)
-> generate vc [None].
Proof.
clear; induct 1; eauto.
Qed.
Hint Resolve step_to_termination : core.
Lemma R_Skip : forall n vc1 v,
R n vc1 (v, Skip)
-> exists v', silent_cstep^* vc1 (v', Skip).
Proof.
induct n; simplify.
cases vc1.
assert (c = Skip \/ exists v' l c', cstep (v0, c) l (v', c')) by apply skip_or_step.
first_order; subst.
eauto.
eapply one_step in H; eauto.
first_order.
equality.
invert H.
invert H4.
invert H5.
cases vc1.
assert (c = Skip \/ exists v' l c', cstep (v0, c) l (v', c')) by apply skip_or_step.
first_order; subst.
eauto.
eapply one_step in H; eauto.
first_order; subst.
invert H.
apply IHn in H2.
first_order.
eauto.
invert H.
invert H4.
invert H5.
Qed.
Lemma simulation_skipping_bwd' : forall ns vc2, generate vc2 ns
-> forall n vc1, R n vc1 vc2
-> generate vc1 ns.
Proof.
induct 1; simplify; eauto.
cases vc1.
apply R_Skip in H; first_order.
eauto.
eapply match_step in H1; eauto.
first_order.
eauto.
eapply match_step in H1; eauto.
first_order.
eauto.
Qed.
Theorem simulation_skipping_bwd : forall n vc1 vc2, R n vc1 vc2
-> vc2 <| vc1.
Proof.
unfold traceInclusion; eauto using simulation_skipping_bwd'.
Qed.
Theorem simulation_skipping : forall n vc1 vc2, R n vc1 vc2
-> vc1 =| vc2.
Proof.
simplify; split; eauto using simulation_skipping_fwd, simulation_skipping_bwd.
Qed.
End simulation_skipping.
Local Hint Extern 1 (_ < _) => linear_arithmetic : core.
Local Hint Extern 1 (_ >= _) => linear_arithmetic : core.
Local Hint Extern 1 (_ <> _) => linear_arithmetic : core.
(* We will need to do some bookkeeping of [n] values. This function is the
* trick, as we only need to skip steps based on removing [If]s from the code.
* That means the number of [If]s in a program is an upper bound on skipped
* steps. (It's not a tight bound, because some [If]s may be in branches that
* are themselves removed by the optimization!) *)
Fixpoint countIfs (c : cmd) : nat :=
match c with
| Skip => 0
| Assign _ _ => 0
| Sequence c1 c2 => countIfs c1 + countIfs c2
| If _ c1 c2 => 1 + countIfs c1 + countIfs c2
| While _ c1 => countIfs c1
| Output _ => 0
end.
(* Our notion of [step0] porting must now allow some skipped steps, also showing
* that they decrease [If] count. *)
Lemma cfold_ok' : forall v1 c1 l v2 c2,
step0 (v1, c1) l (v2, c2)
-> step0 (v1, cfold c1) l (v2, cfold c2)
\/ (l = None /\ v1 = v2 /\ cfold c1 = cfold c2 /\ countIfs c2 < countIfs c1).
Proof.
invert 1; simplify;
try match goal with
| [ _ : context[interp ?e ?v] |- _ ] => rewrite <- (cfoldArith_ok v e) in *
| [ |- context[interp ?e ?v] ] => rewrite <- (cfoldArith_ok v e)
end;
repeat match goal with
| [ |- context[match ?E with _ => _ end] ] => cases E; subst; simplify
end; propositional; eauto.
Qed.
(* Now some fiddling with contexts: *)
Fixpoint cfoldContext (C : context) : context :=
match C with
| Hole => Hole
| CSeq C c => CSeq (cfoldContext C) (cfold c)
end.
Lemma plug_cfold1 : forall C c1 c2, plug C c1 c2
-> plug (cfoldContext C) (cfold c1) (cfold c2).
Proof.
induct 1; simplify; eauto.
Qed.
Local Hint Resolve plug_cfold1 : core.
Lemma plug_samefold : forall C c1 c1',
plug C c1 c1'
-> forall c2 c2', plug C c2 c2'
-> cfold c1 = cfold c2
-> cfold c1' = cfold c2'.
Proof.
induct 1; invert 1; simplify; propositional.
f_equal; eauto.
Qed.
Local Hint Resolve plug_samefold : core.
Lemma plug_countIfs : forall C c1 c1',
plug C c1 c1'
-> forall c2 c2', plug C c2 c2'
-> countIfs c1 < countIfs c2
-> countIfs c1' < countIfs c2'.
Proof.
induct 1; invert 1; simplify; propositional.
apply IHplug in H5; linear_arithmetic.
Qed.
Local Hint Resolve plug_countIfs : core.
Local Hint Extern 1 (interp ?e _ = _) =>
match goal with
| [ H : cfoldArith e = _ |- _ ] => rewrite <- cfoldArith_ok; rewrite H
end : core.
Local Hint Extern 1 (interp ?e _ <> _) =>
match goal with
| [ H : cfoldArith e = _ |- _ ] => rewrite <- cfoldArith_ok; rewrite H
end : core.
(* The final proof is fairly straightforward now. *)
Lemma cfold_ok : forall v c,
(v, c) =| (v, cfold c).
Proof.
simplify.
(* Note the use of [countIfs] in the simulation relation. *)
apply simulation_skipping with (R := fun n vc1 vc2 => fst vc1 = fst vc2
/\ snd vc2 = cfold (snd vc1)
/\ countIfs (snd vc1) < n)
(n := S (countIfs c));
simplify; propositional; auto.
invert H0; simplify; subst.
apply cfold_ok' in H4.
propositional; subst.
cases vc2; simplify; subst.
eauto 11.
cases vc2; simplify; subst.
cases n; try linear_arithmetic.
assert (countIfs c2 < n).
eapply plug_countIfs in H2; eauto.
eauto.
eauto 10.
Qed.
(** * Simulations That Allow Taking Multiple Matching Steps *)
(* Some optimizations actually transform code toward lower-level languages.
* Let's take the example of breaking compound expressions into step-by-step
* computations using new temporary variables. *)
(* We'll use this function to give us an infinite supply of disjoint
* temporaries. *)
Fixpoint tempVar (n : nat) : string :=
match n with
| O => "_tmp"
| S n' => tempVar n' ++ "'"
end%string.
Compute tempVar 0.
Compute tempVar 1.
Compute tempVar 2.
(* With that kind of temporary, we need to watch our for name clashes with
* variables that already exist in a program. These Boolean functions check for
* lack of clashes. We also prove some properties that will come in handy
* later. *)
Definition noUnderscoreVar (x : var) : bool :=
match x with
| String "_" _ => false
| _ => true
end.
Lemma append_assoc : forall a b c,
(a ++ (b ++ c) = (a ++ b) ++ c)%string.
Proof.
induct a; simplify; equality.
Qed.
Lemma append_assoc_String : forall a b,
(String a b = String a "" ++ b)%string.
Proof.
induct b; simplify; equality.
Qed.
Lemma noUnderscoreVar_tempVar' : forall n,
exists s, tempVar n = ("_tmp" ++ s)%string.
Proof.
induct n; simplify; first_order.
exists ""; auto.
rewrite H.
exists (x ++ "'")%string.
repeat match goal with
| [ |- context[String ?c ?x] ] =>
match x with
| "" => fail 1
| _ => rewrite (append_assoc_String c x)
end
end.
repeat rewrite append_assoc.
reflexivity.
Qed.
Theorem noUnderscoreVar_tempVar : forall x,
noUnderscoreVar x = true
-> forall n, x <> tempVar n.
Proof.
unfold not; simplify.
subst.
pose proof (noUnderscoreVar_tempVar' n).
first_order.
rewrite H0 in H.
simplify.
equality.
Qed.
Lemma tempVar_inj' : forall s1 s2,
(s1 ++ "'" = s2 ++ "'")%string
-> s1 = s2.
Proof.
induct s1; simplify.
cases s2; simplify; try equality.
invert H.
cases s2; simplify; equality.
cases s2; simplify.
invert H.
cases s1; simplify; equality.
invert H.
f_equal; auto.
Qed.
Theorem tempVar_inj : forall n1 n2,
tempVar n1 = tempVar n2
-> n1 = n2.
Proof.
induct n1; simplify; cases n2; simplify; try equality.
repeat match goal with
| [ _ : context[(?s ++ "'")%string] |- _ ] => cases s; simplify; try equality
end.
repeat match goal with
| [ _ : context[(?s ++ "'")%string] |- _ ] => cases s; simplify; try equality
end.
auto using tempVar_inj'.
Qed.
Fixpoint noUnderscoreArith (e : arith) : bool :=
match e with
| Const _ => true
| Var x => noUnderscoreVar x
| Plus e1 e2
| Minus e1 e2
| Times e1 e2 => noUnderscoreArith e1 && noUnderscoreArith e2
end.
Fixpoint noUnderscore (c : cmd) : bool :=
match c with
| Skip => true