-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterm-project.v
1644 lines (1483 loc) · 58.4 KB
/
term-project.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
(* term-project.v *)
(* FPP 2020 - YSC3236 2020-2021, Sem1 *)
(* Olivier Danvy <[email protected]> *)
(* Version of 15 Nov 2020 *)
(* ********** *)
(* Three language processors for arithmetic expressions. *)
(*
name:
student ID number:
e-mail address:
*)
(* ********** *)
(*
The primary goal of this term project is to prove the following theorem:
Theorem the_commutative_diagram :
forall sp : source_program,
interpret sp = run (compile sp).
for
* a source language of arithmetic expressions:
Inductive arithmetic_expression : Type :=
| Literal : nat -> arithmetic_expression
| Plus : arithmetic_expression -> arithmetic_expression -> arithmetic_expression
| Minus : arithmetic_expression -> arithmetic_expression -> arithmetic_expression.
Inductive source_program : Type :=
| Source_program : arithmetic_expression -> source_program.
* a target language of byte-code instructions:
Inductive byte_code_instruction : Type :=
| PUSH : nat -> byte_code_instruction
| ADD : byte_code_instruction
| SUB : byte_code_instruction.
Inductive target_program : Type :=
| Target_program : list byte_code_instruction -> target_program.
* a semantics of expressible values:
Inductive expressible_value : Type :=
| Expressible_nat : nat -> expressible_value
| Expressible_msg : string -> expressible_value.
* a source interpreter
interpret : source_program -> expressible_value
* a compiler
compile : source_program -> target_program
* a target interpreter
run : target_program -> expressible_value
The source for errors is subtraction,
since subtracting two natural numbers does not always yield a natural number:
for example, 3 - 2 is defined but not 2 - 3.
You are expected, at the very least:
* to implement a source interpreter
and to verify that it satisfies its specification
* to implement a target interpreter (i.e., a virtual machine)
and to verify that it satisfies its specification
* to implement a compiler
and to verify that it satisfies its specification
* to prove that the diagram commutes, i.e., to show that
interpreting any given expression
gives the same result as
compiling this expression and then running the resulting compiled program
Beyond this absolute minimum, in decreasing importance, it would be good:
* to make a copy of the above in a separate file
and modify it mutatis mutandis
so that the three language processors operate from right to left instead of from left to right,
* to write an accumulator-based compiler and to prove that it satisfies the specification,
* to investigate byte-code verification,
* to investigate decompilation, and
* if there is any time left, to prove that each of the specifications specifies a unique function.
Also, feel free to expand the source language and the target language,
e.g., with multiplication, quotient, and modulo.
*)
(* ********** *)
Ltac fold_unfold_tactic name := intros; unfold name; fold name; reflexivity.
Require Import Arith Bool List String Ascii.
(* caution: only use natural numbers up to 5000 *)
Definition string_of_nat (q0 : nat) : string :=
let s0 := String (ascii_of_nat (48 + (q0 mod 10))) EmptyString
in if q0 <? 10
then s0
else let q1 := q0 / 10
in let s1 := String (ascii_of_nat (48 + (q1 mod 10))) s0
in if q1 <? 10
then s1
else let q2 := q1 / 10
in let s2 := String (ascii_of_nat (48 + (q2 mod 10))) s1
in if q2 <? 10
then s2
else let q3 := q2 / 10
in let s2 := String (ascii_of_nat (48 + (q3 mod 10))) s2
in if q3 <? 10
then s2
else "00000".
Notation "A =n= B" := (beq_nat A B) (at level 70, right associativity).
(* ********** *)
(* Arithmetic expressions: *)
Inductive arithmetic_expression : Type :=
| Literal : nat -> arithmetic_expression
| Plus : arithmetic_expression -> arithmetic_expression -> arithmetic_expression
| Minus : arithmetic_expression -> arithmetic_expression -> arithmetic_expression.
(* Source programs: *)
Inductive source_program : Type :=
| Source_program : arithmetic_expression -> source_program.
(* ********** *)
(* Semantics: *)
Inductive expressible_value : Type :=
| Expressible_nat : nat -> expressible_value
| Expressible_msg : string -> expressible_value.
(* ********** *)
Definition specification_of_evaluate (evaluate : arithmetic_expression -> expressible_value) :=
(forall n : nat,
evaluate (Literal n) = Expressible_nat n)
/\
((forall (ae1 ae2 : arithmetic_expression)
(s1 : string),
evaluate ae1 = Expressible_msg s1 ->
evaluate (Plus ae1 ae2) = Expressible_msg s1)
/\
(forall (ae1 ae2 : arithmetic_expression)
(n1 : nat)
(s2 : string),
evaluate ae1 = Expressible_nat n1 ->
evaluate ae2 = Expressible_msg s2 ->
evaluate (Plus ae1 ae2) = Expressible_msg s2)
/\
(forall (ae1 ae2 : arithmetic_expression)
(n1 n2 : nat),
evaluate ae1 = Expressible_nat n1 ->
evaluate ae2 = Expressible_nat n2 ->
evaluate (Plus ae1 ae2) = Expressible_nat (n1 + n2)))
/\
((forall (ae1 ae2 : arithmetic_expression)
(s1 : string),
evaluate ae1 = Expressible_msg s1 ->
evaluate (Minus ae1 ae2) = Expressible_msg s1)
/\
(forall (ae1 ae2 : arithmetic_expression)
(n1 : nat)
(s2 : string),
evaluate ae1 = Expressible_nat n1 ->
evaluate ae2 = Expressible_msg s2 ->
evaluate (Minus ae1 ae2) = Expressible_msg s2)
/\
(forall (ae1 ae2 : arithmetic_expression)
(n1 n2 : nat),
evaluate ae1 = Expressible_nat n1 ->
evaluate ae2 = Expressible_nat n2 ->
n1 <? n2 = true ->
evaluate (Minus ae1 ae2) = Expressible_msg (String.append "numerical underflow: -" (string_of_nat (n2 - n1))))
/\
(forall (ae1 ae2 : arithmetic_expression)
(n1 n2 : nat),
evaluate ae1 = Expressible_nat n1 ->
evaluate ae2 = Expressible_nat n2 ->
n1 <? n2 = false ->
evaluate (Minus ae1 ae2) = Expressible_nat (n1 - n2))).
Definition specification_of_interpret (interpret : source_program -> expressible_value) :=
forall evaluate : arithmetic_expression -> expressible_value,
specification_of_evaluate evaluate ->
forall ae : arithmetic_expression,
interpret (Source_program ae) = evaluate ae.
(* Task 1:
a. time permitting, prove that each of the definitions above specifies at most one function;
b. implement these two functions; and
c. verify that each of your functions satisfies its specification.
*)
Proposition there_is_at_most_one_evaluate :
forall evaluate1 evaluate2 : arithmetic_expression -> expressible_value,
specification_of_evaluate evaluate1 ->
specification_of_evaluate evaluate2 ->
forall ae : arithmetic_expression,
evaluate1 ae = evaluate2 ae.
Proof.
intros evaluate1 evaluate2 S_evaluate1 S_evaluate2 ae.
destruct S_evaluate1 as [H_eval1_Literal
[[H_eval1_Plus_msg1 [H_eval1_Plus_msg2 H_eval1_Plus_nat]]
[H_eval1_Minus_msg1 [H_eval1_Minus_msg2 [H_eval1_Minus_underflow H_eval1_Minus_nat]]]]].
destruct S_evaluate2 as [H_eval2_Literal
[[H_eval2_Plus_msg1 [H_eval2_Plus_msg2 H_eval2_Plus_nat]]
[H_eval2_Minus_msg1 [H_eval2_Minus_msg2 [H_eval2_Minus_underflow H_eval2_Minus_nat]]]]].
induction ae as [n | ae1 H_eval1_ae1 ae2 H_eval1_ae2 | ae1 H_eval1_ae1 ae2 H_eval1_ae2].
- rewrite -> (H_eval1_Literal n).
rewrite -> (H_eval2_Literal n).
reflexivity.
- case (evaluate2 ae1) as [n1 | s1] eqn:H_eval2_ae1.
+ case (evaluate2 ae2) as [n2 | s2] eqn:H_eval2_ae2.
* rewrite -> (H_eval1_Plus_nat ae1 ae2 n1 n2 H_eval1_ae1 H_eval1_ae2).
rewrite -> (H_eval2_Plus_nat ae1 ae2 n1 n2 H_eval2_ae1 H_eval2_ae2).
reflexivity.
* rewrite -> (H_eval1_Plus_msg2 ae1 ae2 n1 s2 H_eval1_ae1 H_eval1_ae2).
rewrite -> (H_eval2_Plus_msg2 ae1 ae2 n1 s2 H_eval2_ae1 H_eval2_ae2).
reflexivity.
+ rewrite -> (H_eval1_Plus_msg1 ae1 ae2 s1 H_eval1_ae1).
rewrite -> (H_eval2_Plus_msg1 ae1 ae2 s1 H_eval2_ae1).
reflexivity.
- case (evaluate2 ae1) as [n1 | s1] eqn:H_eval2_ae1.
+ case (evaluate2 ae2) as [n2 | s2] eqn:H_eval2_ae2.
* case (n1 <? n2) eqn:H_n1_n2.
-- rewrite -> (H_eval1_Minus_underflow ae1 ae2 n1 n2 H_eval1_ae1 H_eval1_ae2 H_n1_n2).
rewrite -> (H_eval2_Minus_underflow ae1 ae2 n1 n2 H_eval2_ae1 H_eval2_ae2 H_n1_n2).
reflexivity.
-- rewrite -> (H_eval1_Minus_nat ae1 ae2 n1 n2 H_eval1_ae1 H_eval1_ae2 H_n1_n2).
rewrite -> (H_eval2_Minus_nat ae1 ae2 n1 n2 H_eval2_ae1 H_eval2_ae2 H_n1_n2).
reflexivity.
* rewrite -> (H_eval1_Minus_msg2 ae1 ae2 n1 s2 H_eval1_ae1 H_eval1_ae2).
rewrite -> (H_eval2_Minus_msg2 ae1 ae2 n1 s2 H_eval2_ae1 H_eval2_ae2).
reflexivity.
+ rewrite -> (H_eval1_Minus_msg1 ae1 ae2 s1 H_eval1_ae1).
rewrite -> (H_eval2_Minus_msg1 ae1 ae2 s1 H_eval2_ae1).
reflexivity.
Qed.
Fixpoint evaluate (ae : arithmetic_expression) : expressible_value :=
match ae with
| Literal n => Expressible_nat n
| Plus ae1 ae2 =>
match evaluate ae1 with
| Expressible_msg s1 => Expressible_msg s1
| Expressible_nat n1 =>
match evaluate ae2 with
| Expressible_msg s2 => Expressible_msg s2
| Expressible_nat n2 => Expressible_nat (n1 + n2)
end
end
| Minus ae1 ae2 =>
match evaluate ae1 with
| Expressible_msg s1 => Expressible_msg s1
| Expressible_nat n1 =>
match evaluate ae2 with
| Expressible_msg s2 => Expressible_msg s2
| Expressible_nat n2 =>
if n1 <? n2
then Expressible_msg (String.append "numerical underflow: -" (string_of_nat (n2 - n1)))
else Expressible_nat (n1 - n2)
end
end
end.
Lemma fold_unfold_evaluate_Literal :
forall n : nat,
evaluate (Literal n) = Expressible_nat n.
Proof.
fold_unfold_tactic evaluate.
Qed.
Lemma fold_unfold_evaluate_Plus :
forall ae1 ae2 : arithmetic_expression,
evaluate (Plus ae1 ae2) =
match evaluate ae1 with
| Expressible_msg s1 => Expressible_msg s1
| Expressible_nat n1 =>
match evaluate ae2 with
| Expressible_msg s2 => Expressible_msg s2
| Expressible_nat n2 => Expressible_nat (n1 + n2)
end
end.
Proof.
fold_unfold_tactic evaluate.
Qed.
Lemma fold_unfold_evaluate_Minus :
forall ae1 ae2 : arithmetic_expression,
evaluate (Minus ae1 ae2) =
match evaluate ae1 with
| Expressible_msg s1 => Expressible_msg s1
| Expressible_nat n1 =>
match evaluate ae2 with
| Expressible_msg s2 => Expressible_msg s2
| Expressible_nat n2 =>
if n1 <? n2
then Expressible_msg (String.append "numerical underflow: -" (string_of_nat (n2 - n1)))
else Expressible_nat (n1 - n2)
end
end.
Proof.
fold_unfold_tactic evaluate.
Qed.
Theorem evaluate_satisfies_the_specification_of_evaluate :
specification_of_evaluate evaluate.
Proof.
unfold specification_of_evaluate.
split.
- exact fold_unfold_evaluate_Literal.
- split.
+ split.
* intros ae1 ae2 s1 H_eval_ae1.
rewrite -> (fold_unfold_evaluate_Plus ae1 ae2).
rewrite -> H_eval_ae1.
reflexivity.
* split.
-- intros ae1 ae2 n1 s2 H_eval_ae1 H_eval_ae2.
rewrite -> (fold_unfold_evaluate_Plus ae1 ae2).
rewrite -> H_eval_ae1.
rewrite -> H_eval_ae2.
reflexivity.
-- intros ae1 ae2 n1 n2 H_eval_ae1 H_eval_ae2.
rewrite -> (fold_unfold_evaluate_Plus ae1 ae2).
rewrite -> H_eval_ae1.
rewrite -> H_eval_ae2.
reflexivity.
+ split.
* intros ae1 ae2 s1 H_eval_ae1.
rewrite -> (fold_unfold_evaluate_Minus ae1 ae2).
rewrite -> H_eval_ae1.
reflexivity.
* split.
-- intros ae1 ae2 n1 s2 H_eval_ae1 H_eval_ae2.
rewrite -> (fold_unfold_evaluate_Minus ae1 ae2).
rewrite -> H_eval_ae1.
rewrite -> H_eval_ae2.
reflexivity.
-- split.
++ intros ae1 ae2 n1 n2 H_eval_ae1 H_eval_ae2 H_n1_n2.
rewrite -> (fold_unfold_evaluate_Minus ae1 ae2).
rewrite -> H_eval_ae1.
rewrite -> H_eval_ae2.
rewrite -> H_n1_n2.
reflexivity.
++ intros ae1 ae2 n1 n2 H_eval_ae1 H_eval_ae2 H_n1_n2.
rewrite -> (fold_unfold_evaluate_Minus ae1 ae2).
rewrite -> H_eval_ae1.
rewrite -> H_eval_ae2.
rewrite -> H_n1_n2.
reflexivity.
Qed.
Proposition there_is_at_most_one_interpret :
forall interpret1 interpret2 : source_program -> expressible_value,
specification_of_interpret interpret1 ->
specification_of_interpret interpret2 ->
forall sp : source_program,
interpret1 sp = interpret2 sp.
Proof.
intros interpret1 interpret2 S_interpret1 S_interpret2 [ae].
unfold specification_of_interpret in S_interpret1, S_interpret2.
rewrite -> (S_interpret1 evaluate evaluate_satisfies_the_specification_of_evaluate ae).
rewrite -> (S_interpret2 evaluate evaluate_satisfies_the_specification_of_evaluate ae).
reflexivity.
Qed.
Definition interpret (sp : source_program) : expressible_value :=
match sp with
| Source_program ae => evaluate ae
end.
Theorem interpret_satisfies_the_specification_of_interpret :
specification_of_interpret interpret.
Proof.
unfold specification_of_interpret, interpret.
intros evaluate' S_evaluate' ae.
Check (there_is_at_most_one_evaluate
evaluate evaluate'
evaluate_satisfies_the_specification_of_evaluate
S_evaluate'
ae).
exact (there_is_at_most_one_evaluate
evaluate evaluate'
evaluate_satisfies_the_specification_of_evaluate
S_evaluate'
ae).
Qed.
(* ********** *)
(* Byte-code instructions: *)
Inductive byte_code_instruction : Type :=
| PUSH : nat -> byte_code_instruction
| ADD : byte_code_instruction
| SUB : byte_code_instruction.
(* Target programs: *)
Inductive target_program : Type :=
| Target_program : list byte_code_instruction -> target_program.
(* Data stack: *)
Definition data_stack := list nat.
(* ********** *)
Inductive result_of_decoding_and_execution : Type :=
| OK : data_stack -> result_of_decoding_and_execution
| KO : string -> result_of_decoding_and_execution.
Definition specification_of_decode_execute (decode_execute : byte_code_instruction -> data_stack -> result_of_decoding_and_execution) :=
(forall (n : nat)
(ds : data_stack),
decode_execute (PUSH n) ds = OK (n :: ds))
/\
((decode_execute ADD nil = KO "ADD: stack underflow")
/\
(forall (n2 : nat),
decode_execute ADD (n2 :: nil) = KO "ADD: stack underflow")
/\
(forall (n1 n2 : nat)
(ds : data_stack),
decode_execute ADD (n2 :: n1 :: ds) = OK ((n1 + n2) :: ds)))
/\
((decode_execute SUB nil = KO "SUB: stack underflow")
/\
(forall (n2 : nat),
decode_execute SUB (n2 :: nil) = KO "SUB: stack underflow")
/\
(forall (n1 n2 : nat)
(ds : data_stack),
n1 <? n2 = true ->
decode_execute SUB (n2 :: n1 :: ds) = KO (String.append "numerical underflow: -" (string_of_nat (n2 - n1))))
/\
(forall (n1 n2 : nat)
(ds : data_stack),
n1 <? n2 = false ->
decode_execute SUB (n2 :: n1 :: ds) = OK ((n1 - n2) :: ds))).
(* Task 2:
a. time permitting, prove that the definition above specifies at most one function;
b. implement this function; and
c. verify that your function satisfies the specification.
*)
Proposition there_is_at_most_one_decode_execute :
forall decode_execute1 decode_execute2 : byte_code_instruction -> data_stack -> result_of_decoding_and_execution,
specification_of_decode_execute decode_execute1 ->
specification_of_decode_execute decode_execute2 ->
forall (bcis : byte_code_instruction)
(ds : data_stack),
decode_execute1 bcis ds = decode_execute2 bcis ds.
Proof.
intros decode_execute1 decode_execute2 S_de1 S_de2 bcis ds.
destruct S_de1 as [H_push1
[[H_add1_nil [H_add1_n1 H_add1_n2_n1]]
[H_sub1_nil [H_sub1_n2 [H_sub1_n1_n2_true H_sub1_n1_n2_false]]]]].
destruct S_de2 as [H_push2
[[H_add2_nil [H_add2_n1 H_add2_n2_n1]]
[H_sub2_nil [H_sub2_n2 [H_sub2_n1_n2_true H_sub2_n1_n2_false]]]]].
case bcis as [n | | ].
- rewrite -> (H_push1 n ds).
rewrite -> (H_push2 n ds).
reflexivity.
- case ds as [ | n ds'].
+ rewrite -> H_add1_nil.
rewrite -> H_add2_nil.
reflexivity.
+ case ds' as [ | n' ds''].
* rewrite -> (H_add1_n1 n).
rewrite -> (H_add2_n1 n).
reflexivity.
* rewrite -> (H_add1_n2_n1 n' n).
rewrite -> (H_add2_n2_n1 n' n).
reflexivity.
- case ds as [ | n ds'].
+ rewrite -> H_sub1_nil.
rewrite -> H_sub2_nil.
reflexivity.
+ case ds' as [ | n' ds''].
* rewrite -> (H_sub1_n2 n).
rewrite -> (H_sub2_n2 n).
reflexivity.
* case (n' <? n) eqn:H_n1_n2.
-- rewrite -> (H_sub1_n1_n2_true n' n ds'' H_n1_n2).
rewrite -> (H_sub2_n1_n2_true n' n ds'' H_n1_n2).
reflexivity.
-- rewrite -> (H_sub1_n1_n2_false n' n ds'' H_n1_n2).
rewrite -> (H_sub2_n1_n2_false n' n ds'' H_n1_n2).
reflexivity.
Qed.
Definition decode_execute (bcis : byte_code_instruction) (ds : data_stack) : result_of_decoding_and_execution :=
match bcis with
| PUSH n =>
OK (n :: ds)
| ADD =>
match ds with
| nil =>
KO "ADD: stack underflow"
| (n2 :: nil) =>
KO "ADD: stack underflow"
| (n2 :: n1 :: ds) =>
OK ((n1 + n2) :: ds)
end
| SUB =>
match ds with
| nil =>
KO "SUB: stack underflow"
| (n2 :: nil) =>
KO "SUB: stack underflow"
| (n2 :: n1 :: ds) =>
if n1 <? n2
then KO (String.append "numerical underflow: -" (string_of_nat (n2 - n1)))
else OK ((n1 - n2) :: ds)
end
end.
Theorem decode_execute_satisfies_the_specification_of_decode_execute :
specification_of_decode_execute decode_execute.
Proof.
unfold specification_of_decode_execute, decode_execute.
split.
- intros n ds.
reflexivity.
- split.
+ split.
* reflexivity.
* split.
-- intro n.
reflexivity.
-- intros n1 n2 ds.
reflexivity.
+ split.
* reflexivity.
* split.
-- intro n.
reflexivity.
-- split.
++ intros n1 n2 ds H_n1_n2.
rewrite H_n1_n2.
reflexivity.
++ intros n1 n2 ds H_n1_n2.
rewrite H_n1_n2.
reflexivity.
Qed.
(* ********** *)
(* Specification of the virtual machine: *)
Definition specification_of_fetch_decode_execute_loop (fetch_decode_execute_loop : list byte_code_instruction -> data_stack -> result_of_decoding_and_execution) :=
forall decode_execute : byte_code_instruction -> data_stack -> result_of_decoding_and_execution,
specification_of_decode_execute decode_execute ->
(forall ds : data_stack,
fetch_decode_execute_loop nil ds = OK ds)
/\
(forall (bci : byte_code_instruction)
(bcis' : list byte_code_instruction)
(ds ds' : data_stack),
decode_execute bci ds = OK ds' ->
fetch_decode_execute_loop (bci :: bcis') ds =
fetch_decode_execute_loop bcis' ds')
/\
(forall (bci : byte_code_instruction)
(bcis' : list byte_code_instruction)
(ds : data_stack)
(s : string),
decode_execute bci ds = KO s ->
fetch_decode_execute_loop (bci :: bcis') ds =
KO s).
(* Task 3:
a. time permitting, prove that the definition above specifies at most one function;
b. implement this function; and
c. verify that your function satisfies the specification.
*)
Proposition there_is_at_most_one_fetch_decode_execute_loop :
forall fetch_decode_execute_loop1 fetch_decode_execute_loop2 : list byte_code_instruction -> data_stack -> result_of_decoding_and_execution,
specification_of_fetch_decode_execute_loop fetch_decode_execute_loop1 ->
specification_of_fetch_decode_execute_loop fetch_decode_execute_loop2 ->
forall (bcis : list byte_code_instruction)
(ds : data_stack),
fetch_decode_execute_loop1 bcis ds = fetch_decode_execute_loop2 bcis ds.
Proof.
intros loop1 loop2 S_loop1 S_loop2 bcis.
unfold specification_of_fetch_decode_execute_loop in S_loop1, S_loop2.
assert (S_loop1 := S_loop1 decode_execute decode_execute_satisfies_the_specification_of_decode_execute).
assert (S_loop2 := S_loop2 decode_execute decode_execute_satisfies_the_specification_of_decode_execute).
destruct S_loop1 as [H_loop1_nil [H_loop1_cons_OK H_loop1_cons_KO]].
destruct S_loop2 as [H_loop2_nil [H_loop2_cons_OK H_loop2_cons_KO]].
induction bcis as [ | bci bcis' IHbcis']; intro ds.
- rewrite -> (H_loop1_nil ds).
rewrite -> (H_loop2_nil ds).
reflexivity.
- case (decode_execute bci ds) as [ds' | s] eqn:H_de.
+ rewrite -> (H_loop1_cons_OK bci bcis' ds ds' H_de).
rewrite -> (H_loop2_cons_OK bci bcis' ds ds' H_de).
rewrite -> (IHbcis' ds').
reflexivity.
+ rewrite -> (H_loop1_cons_KO bci bcis' ds s H_de).
rewrite -> (H_loop2_cons_KO bci bcis' ds s H_de).
reflexivity.
Qed.
Fixpoint fetch_decode_execute_loop (bcis : list byte_code_instruction) (ds : data_stack) : result_of_decoding_and_execution :=
match bcis with
| nil =>
OK ds
| bci :: bcis' =>
match decode_execute bci ds with
| OK ds' =>
fetch_decode_execute_loop bcis' ds'
| KO s =>
KO s
end
end.
Lemma fold_unfold_fetch_decode_execute_loop_nil :
forall ds : data_stack,
fetch_decode_execute_loop nil ds =
OK ds.
Proof.
fold_unfold_tactic fetch_decode_execute_loop.
Qed.
Lemma fold_unfold_fetch_decode_execute_loop_cons :
forall (bci : byte_code_instruction)
(bcis' : list byte_code_instruction)
(ds : data_stack),
fetch_decode_execute_loop (bci :: bcis') ds =
match decode_execute bci ds with
| OK ds =>
fetch_decode_execute_loop bcis' ds
| KO s =>
KO s
end.
Proof.
fold_unfold_tactic fetch_decode_execute_loop.
Qed.
Theorem fetch_decode_execute_loop_satisfies_the_specification_of_fetch_decode_execute_loop :
specification_of_fetch_decode_execute_loop fetch_decode_execute_loop.
Proof.
unfold specification_of_fetch_decode_execute_loop.
intros decode_execute' S_decode_execute'.
split.
- exact fold_unfold_fetch_decode_execute_loop_nil.
- split.
+ intros bci bcis' ds ds' H_decode_execute'_bci_ds.
rewrite -> (fold_unfold_fetch_decode_execute_loop_cons bci bcis' ds).
Check (there_is_at_most_one_decode_execute).
Check (there_is_at_most_one_decode_execute
decode_execute'
decode_execute
S_decode_execute'
decode_execute_satisfies_the_specification_of_decode_execute
bci
ds).
rewrite -> (there_is_at_most_one_decode_execute
decode_execute'
decode_execute
S_decode_execute'
decode_execute_satisfies_the_specification_of_decode_execute
bci
ds) in H_decode_execute'_bci_ds.
rewrite -> H_decode_execute'_bci_ds.
reflexivity.
+ intros bci bcis' ds s H_decode_execute'_bci_ds.
rewrite -> (fold_unfold_fetch_decode_execute_loop_cons bci bcis' ds).
rewrite -> (there_is_at_most_one_decode_execute
decode_execute'
decode_execute
S_decode_execute'
decode_execute_satisfies_the_specification_of_decode_execute
bci
ds) in H_decode_execute'_bci_ds.
rewrite -> H_decode_execute'_bci_ds.
reflexivity.
Qed.
(* ********** *)
(* Task 4:
Prove that for any lists of byte-code instructions bcis1 and bcis2,
and for any data stack ds,
executing the concatenation of bcis1 and bcis2 (i.e., bcis1 ++ bcis2) with ds
gives the same result as
(1) executing bcis1 with ds, and then
(2) executing bcis2 with the resulting data stack, if there exists one.
*)
Lemma fold_unfold_append_nil :
forall bcis2 : list byte_code_instruction,
nil ++ bcis2 = bcis2.
Proof.
fold_unfold_tactic List.app.
Qed.
Lemma fold_unfold_append_cons :
forall (bci1 : byte_code_instruction)
(bci1s bci2s : list byte_code_instruction),
(bci1 :: bci1s) ++ bci2s =
bci1 :: (bci1s ++ bci2s).
Proof.
fold_unfold_tactic List.app.
Qed.
Theorem concatenation_of_two_list_bcis_with_ds :
forall (bcis1 bcis2 : list byte_code_instruction)
(ds : data_stack),
(forall ds' : data_stack,
fetch_decode_execute_loop bcis1 ds = OK ds' ->
fetch_decode_execute_loop bcis2 ds' =
fetch_decode_execute_loop (bcis1 ++ bcis2) ds)
/\
(forall s : string,
fetch_decode_execute_loop bcis1 ds = KO s ->
fetch_decode_execute_loop (bcis1 ++ bcis2) ds = KO s).
Proof.
intros bcis1.
induction bcis1 as [ | bci1 bcis1' IHbcis1']; intros bcis2 ds; split.
- intros ds' H_loop_bcis1.
rewrite -> (fold_unfold_append_nil bcis2).
rewrite -> (fold_unfold_fetch_decode_execute_loop_nil ds) in H_loop_bcis1.
injection H_loop_bcis1 as H_ds.
rewrite -> H_ds.
reflexivity.
- intros s H_loop_bcis1.
rewrite -> (fold_unfold_fetch_decode_execute_loop_nil ds) in H_loop_bcis1.
discriminate H_loop_bcis1.
- intros ds' H_loop_bcis1.
rewrite -> (fold_unfold_append_cons bci1 bcis1' bcis2).
rewrite -> (fold_unfold_fetch_decode_execute_loop_cons bci1 (bcis1' ++ bcis2) ds).
rewrite -> (fold_unfold_fetch_decode_execute_loop_cons bci1 bcis1' ds) in H_loop_bcis1.
case (decode_execute bci1 ds) as [ds'' | s].
+ destruct (IHbcis1' bcis2 ds'') as [IHds'' _].
exact (IHds'' ds' H_loop_bcis1).
+ destruct (IHbcis1' bcis2 ds') as [IHds'' _].
discriminate H_loop_bcis1.
- intros s H_loop_bcis1.
rewrite -> (fold_unfold_append_cons bci1 bcis1' bcis2).
rewrite -> (fold_unfold_fetch_decode_execute_loop_cons bci1 (bcis1' ++ bcis2) ds).
rewrite -> (fold_unfold_fetch_decode_execute_loop_cons bci1 bcis1' ds) in H_loop_bcis1.
case (decode_execute bci1 ds) as [ds' | s'].
+ destruct (IHbcis1' bcis2 ds') as [_ IHs].
exact (IHs s H_loop_bcis1).
+ exact H_loop_bcis1.
Qed.
(* ********** *)
Definition specification_of_run (run : target_program -> expressible_value) :=
forall fetch_decode_execute_loop : list byte_code_instruction -> data_stack -> result_of_decoding_and_execution,
specification_of_fetch_decode_execute_loop fetch_decode_execute_loop ->
(forall (bcis : list byte_code_instruction),
fetch_decode_execute_loop bcis nil = OK nil ->
run (Target_program bcis) = Expressible_msg "no result on the data stack")
/\
(forall (bcis : list byte_code_instruction)
(n : nat),
fetch_decode_execute_loop bcis nil = OK (n :: nil) ->
run (Target_program bcis) = Expressible_nat n)
/\
(forall (bcis : list byte_code_instruction)
(n n' : nat)
(ds'' : data_stack),
fetch_decode_execute_loop bcis nil = OK (n :: n' :: ds'') ->
run (Target_program bcis) = Expressible_msg "too many results on the data stack")
/\
(forall (bcis : list byte_code_instruction)
(s : string),
fetch_decode_execute_loop bcis nil = KO s ->
run (Target_program bcis) = Expressible_msg s).
(* Task 5:
a. time permitting, prove that the definition above specifies at most one function;
b. implement this function; and
c. verify that your function satisfies the specification.
*)
Proposition there_is_at_most_one_run :
forall (decode_execute : byte_code_instruction -> data_stack -> result_of_decoding_and_execution),
specification_of_decode_execute decode_execute ->
forall (fetch_decode_execute_loop : list byte_code_instruction -> data_stack -> result_of_decoding_and_execution),
specification_of_fetch_decode_execute_loop fetch_decode_execute_loop ->
forall (run1 run2 : target_program -> expressible_value),
specification_of_run run1 ->
specification_of_run run2 ->
forall (tp : target_program),
run1 tp = run2 tp.
Proof.
intros decode_execute H_decode_execute fetch_decode_execute_loop H_fetch_decode_execute_loop run1 run2 S_run1 S_run2 [bcis].
unfold specification_of_run in S_run1, S_run2.
assert (S_run1 := S_run1 fetch_decode_execute_loop H_fetch_decode_execute_loop).
assert (S_run2 := S_run2 fetch_decode_execute_loop H_fetch_decode_execute_loop).
destruct S_run1 as [H_run1_nil [H_run1_n [H_run1_n_n' H_run1_s]]].
destruct S_run2 as [H_run2_nil [H_run2_n [H_run2_n_n' H_run2_s]]].
case (fetch_decode_execute_loop bcis nil) as [ds | s] eqn:H_loop.
- case ds as [ | n ds'].
+ rewrite -> (H_run1_nil bcis H_loop).
rewrite -> (H_run2_nil bcis H_loop).
reflexivity.
+ case ds' as [ | n' ds''].
* rewrite -> (H_run1_n bcis n H_loop).
rewrite -> (H_run2_n bcis n H_loop).
reflexivity.
* rewrite -> (H_run1_n_n' bcis n n' ds'' H_loop).
rewrite -> (H_run2_n_n' bcis n n' ds'' H_loop).
reflexivity.
- rewrite -> (H_run1_s bcis s H_loop).
rewrite -> (H_run2_s bcis s H_loop).
reflexivity.
Restart.
intros decode_execute H_decode_execute fetch_decode_execute_loop H_fetch_decode_execute_loop run1 run2 S_run1 S_run2 [bcis].
unfold specification_of_run in S_run1, S_run2.
assert (S_run1 := S_run1 fetch_decode_execute_loop H_fetch_decode_execute_loop).
assert (S_run2 := S_run2 fetch_decode_execute_loop H_fetch_decode_execute_loop).
destruct S_run1 as [H_run1_nil [H_run1_n [H_run1_n_n' H_run1_s]]].
destruct S_run2 as [H_run2_nil [H_run2_n [H_run2_n_n' H_run2_s]]].
case (fetch_decode_execute_loop bcis nil) as [[ | n [ | n' ds'']] | s] eqn:H_loop.
- rewrite -> (H_run1_nil bcis H_loop).
rewrite -> (H_run2_nil bcis H_loop).
reflexivity.
- rewrite -> (H_run1_n bcis n H_loop).
rewrite -> (H_run2_n bcis n H_loop).
reflexivity.
- rewrite -> (H_run1_n_n' bcis n n' ds'' H_loop).
rewrite -> (H_run2_n_n' bcis n n' ds'' H_loop).
reflexivity.
- rewrite -> (H_run1_s bcis s H_loop).
rewrite -> (H_run2_s bcis s H_loop).
reflexivity.
Qed.
Definition run (tp : target_program) : expressible_value :=
match tp with
| Target_program bcis =>
match fetch_decode_execute_loop bcis nil with
| OK nil =>
Expressible_msg "no result on the data stack"
| OK (n :: nil) =>
Expressible_nat n
| OK (n :: n' :: ds') =>
Expressible_msg "too many results on the data stack"
| KO s =>
Expressible_msg s
end
end.
Theorem run_satisfies_the_specification_of_run :
specification_of_run run.
Proof.
unfold specification_of_run.
intros fetch_decode_execute_loop' S_loop.
split.
- intros bcis H_fetch_decode_execute_loop_nil.
unfold run.
Check (there_is_at_most_one_fetch_decode_execute_loop).
Check (there_is_at_most_one_fetch_decode_execute_loop
fetch_decode_execute_loop'
fetch_decode_execute_loop
S_loop
fetch_decode_execute_loop_satisfies_the_specification_of_fetch_decode_execute_loop
bcis
nil).
rewrite -> (there_is_at_most_one_fetch_decode_execute_loop
fetch_decode_execute_loop'
fetch_decode_execute_loop
S_loop
fetch_decode_execute_loop_satisfies_the_specification_of_fetch_decode_execute_loop
bcis
nil) in H_fetch_decode_execute_loop_nil.
rewrite -> H_fetch_decode_execute_loop_nil.
reflexivity.
- split.
+ intros bcis n H_fetch_decode_execute_loop'.
unfold run.
rewrite -> (there_is_at_most_one_fetch_decode_execute_loop
fetch_decode_execute_loop'
fetch_decode_execute_loop
S_loop
fetch_decode_execute_loop_satisfies_the_specification_of_fetch_decode_execute_loop
bcis
nil) in H_fetch_decode_execute_loop'.
rewrite -> H_fetch_decode_execute_loop'.
reflexivity.
+ split.
* intros bcis n n' ds'' H_fetch_decode_execute_loop'.
unfold run.
rewrite -> (there_is_at_most_one_fetch_decode_execute_loop
fetch_decode_execute_loop'
fetch_decode_execute_loop
S_loop
fetch_decode_execute_loop_satisfies_the_specification_of_fetch_decode_execute_loop
bcis
nil) in H_fetch_decode_execute_loop'.
rewrite -> H_fetch_decode_execute_loop'.
reflexivity.
* intros bcis s H_fetch_decode_execute_loop'.
unfold run.
rewrite -> (there_is_at_most_one_fetch_decode_execute_loop
fetch_decode_execute_loop'
fetch_decode_execute_loop
S_loop
fetch_decode_execute_loop_satisfies_the_specification_of_fetch_decode_execute_loop
bcis
nil) in H_fetch_decode_execute_loop'.
rewrite -> H_fetch_decode_execute_loop'.
reflexivity.
Qed.
(* ********** *)
Definition specification_of_compile_aux (compile_aux : arithmetic_expression -> list byte_code_instruction) :=
(forall n : nat,
compile_aux (Literal n) = PUSH n :: nil)
/\
(forall ae1 ae2 : arithmetic_expression,
compile_aux (Plus ae1 ae2) = (compile_aux ae1) ++ (compile_aux ae2) ++ (ADD :: nil))
/\
(forall ae1 ae2 : arithmetic_expression,
compile_aux (Minus ae1 ae2) = (compile_aux ae1) ++ (compile_aux ae2) ++ (SUB :: nil)).
(* Task 6:
a. time permitting, prove that the definition above specifies at most one function;
b. implement this function using list concatenation, i.e., ++; and
c. verify that your function satisfies the specification.
*)
Proposition there_is_at_most_one_compile_aux :
forall compile_aux1 compile_aux2 : arithmetic_expression -> list byte_code_instruction,
specification_of_compile_aux compile_aux1 ->
specification_of_compile_aux compile_aux2 ->
forall (ae : arithmetic_expression),
compile_aux1 ae = compile_aux2 ae.
Proof.
intros compile_aux1 compile_aux2 S_compile_aux1 S_compile_aux2 ae.
destruct S_compile_aux1 as [H_compile_aux1_Literal [H_compile_aux1_Plus H_compile_aux1_Minus]].
destruct S_compile_aux2 as [H_compile_aux2_Literal [H_compile_aux2_Plus H_compile_aux2_Minus]].
induction ae as [ n | ae1 IHae1 ae2 IHae2 | ae1 IHae1 ae2 IHae2].
- rewrite -> (H_compile_aux1_Literal n).
rewrite -> (H_compile_aux2_Literal n).
reflexivity.
- rewrite -> (H_compile_aux1_Plus ae1 ae2).
rewrite -> (H_compile_aux2_Plus ae1 ae2).
rewrite -> IHae1.
rewrite -> IHae2.
reflexivity.
- rewrite -> (H_compile_aux1_Minus ae1 ae2).
rewrite -> (H_compile_aux2_Minus ae1 ae2).
rewrite -> IHae1.
rewrite -> IHae2.
reflexivity.
Qed.
Fixpoint compile_aux (ae : arithmetic_expression) : list byte_code_instruction :=
match ae with
| Literal n =>
PUSH n :: nil
| Plus ae1 ae2 =>
(compile_aux ae1) ++ (compile_aux ae2) ++ (ADD :: nil)
| Minus ae1 ae2 =>
(compile_aux ae1) ++ (compile_aux ae2) ++ (SUB :: nil)
end.
Lemma fold_unfold_compile_aux_Literal :
forall n : nat,