-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImp.v
More file actions
1376 lines (1206 loc) · 39.5 KB
/
Copy pathImp.v
File metadata and controls
1376 lines (1206 loc) · 39.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(** * Imp: Simple Imperative Programs *)
Set Warnings "-notation-overridden,-notation-incompatible-prefix".
From Stdlib Require Import Bool.
From Stdlib Require Import Init.Nat.
From Stdlib Require Import Arith.
From Stdlib Require Import EqNat. Import Nat.
From Stdlib Require Import Lia.
From Stdlib Require Import List. Import ListNotations.
From Stdlib Require Import Strings.String.
From LF Require Import Maps.
Set Default Goal Selector "!".
Module AExp.
Inductive aexp : Type :=
| ANum (n : nat)
| APlus (a1 a2 : aexp)
| AMinus (a1 a2 : aexp)
| AMult (a1 a2 : aexp).
Inductive bexp : Type :=
| BTrue
| BFalse
| BEq (a1 a2 : aexp)
| BNeq (a1 a2 : aexp)
| BLe (a1 a2 : aexp)
| BGt (a1 a2 : aexp)
| BNot (b : bexp)
| BAnd (b1 b2 : bexp).
Fixpoint aeval (a : aexp) : nat :=
match a with
| ANum n => n
| APlus a1 a2 => (aeval a1) + (aeval a2)
| AMinus a1 a2 => (aeval a1) - (aeval a2)
| AMult a1 a2 => (aeval a1) * (aeval a2)
end.
Example test_aeval1 :
aeval (APlus (ANum 2) (ANum 2)) = 4.
Proof. reflexivity. Qed.
Fixpoint beval (b : bexp) : bool :=
match b with
| BTrue => true
| BFalse => false
| BEq a1 a2 => (aeval a1) =? (aeval a2)
| BNeq a1 a2 => negb ((aeval a1) =? (aeval a2))
| BLe a1 a2 => (aeval a1) <=? (aeval a2)
| BGt a1 a2 => negb ((aeval a1) <=? (aeval a2))
| BNot b1 => negb (beval b1)
| BAnd b1 b2 => andb (beval b1)(beval b2)
end.
Fixpoint optimize_0plus (a : aexp) : aexp :=
match a with
| ANum n => ANum n
| APlus (ANum 0) e2 => optimize_0plus e2
| APlus e1 e2 => APlus (optimize_0plus e1)(optimize_0plus e2)
| AMinus e1 e2 => AMinus (optimize_0plus e1)(optimize_0plus e2)
| AMult e1 e2 => AMult (optimize_0plus e1)(optimize_0plus e2)
end.
Example test_optimize_0plus :
optimize_0plus (APlus (ANum 2)
(APlus (ANum 0)
(APlus (ANum 0)(ANum 1))))
= APlus (ANum 2) (ANum 1).
Proof. reflexivity. Qed.
Theorem optimize_0plus_sound : forall a,
aeval (optimize_0plus a) = aeval a.
Proof.
intros. induction a.
- reflexivity.
- destruct a1 eqn:Ea1.
+ destruct n eqn:En.
* simpl. apply IHa2.
* simpl. rewrite IHa2. reflexivity.
+ simpl. simpl in IHa1. rewrite IHa1. rewrite IHa2. reflexivity.
+ simpl. simpl in IHa1. rewrite IHa1. rewrite IHa2. reflexivity.
+ simpl. simpl in IHa1. rewrite IHa1. rewrite IHa2. reflexivity.
- simpl. rewrite IHa1. rewrite IHa2. reflexivity.
- simpl. rewrite IHa1. rewrite IHa2. reflexivity.
Qed.
(* ROCQ AUTOMATION *)
Theorem silly1 :forall (P : Prop), P -> P.
Proof.
intros P HP.
try reflexivity.
apply HP.
Qed.
Theorem silly2 : forall ae, aeval ae = aeval ae.
Proof.
try reflexivity.
Qed.
Lemma foo : forall n, 0 <=? n = true.
Proof.
intros.
destruct n.
- simpl. reflexivity.
- simpl. reflexivity.
Qed.
Lemma foo' : forall n, 0 <=? n = true.
Proof.
intros.
destruct n;
simpl;
reflexivity.
Qed.
Theorem optimize_0plus_sound' : forall a,
aeval (optimize_0plus a) = aeval a.
Proof.
intros a.
induction a;
try (simpl; rewrite IHa1; rewrite IHa2; reflexivity).
- reflexivity.
- destruct a1 eqn:Ea1;
try (simpl; simpl in IHa1; rewrite IHa1; rewrite IHa2; reflexivity).
+ destruct n eqn:En; simpl; rewrite IHa2; reflexivity.
Qed.
Theorem optimize_0plus_sound'': forall a,
aeval (optimize_0plus a) = aeval a.
Proof.
intros a.
induction a;
(* Most cases follow directly by the IH *)
try (simpl; rewrite IHa1; rewrite IHa2; reflexivity);
(* ... or are immediate by definition *)
try reflexivity.
(* The interesting case is when a = APlus a1 a2. *)
- (* APlus *)
destruct a1; try (simpl; simpl in IHa1; rewrite IHa1;
rewrite IHa2; reflexivity).
+ (* a1 = ANum n *) destruct n;
simpl; rewrite IHa2; reflexivity.
Qed.
Theorem In10 : In 10 [1;2;3;4;5;6;7;8;9;10].
Proof.
repeat (try (left; reflexivity); right).
Qed.
Theorem In10' : In 10 [1;2;3;4;5;6;7;8;9;10].
Proof.
repeat simpl.
repeat (left; reflexivity).
repeat (right; try (left; reflexivity)).
Qed.
(** The tactic [repeat T] does not have any upper bound on the
number of times it applies [T]. If [T] is a tactic that _always_
succeeds (and makes progress), then repeat [T] will loop
forever. *)
Theorem repeat_loop : forall (m n : nat),
m + n = n + m.
Proof.
intros m n.
(* Uncomment the next line to see the infinite loop occur. You will
then need to interrupt Coq to make it listen to you again. (In
Proof General, [C-c C-c] does this.) *)
(* repeat rewrite Nat.add_comm. *)
Admitted.
Fixpoint optimize_0plus_b (b : bexp) : bexp :=
match b with
| BTrue => BTrue
| BFalse => BFalse
| BEq a1 a2 => BEq(optimize_0plus a1)(optimize_0plus a2)
| BNeq a1 a2 => BNeq (optimize_0plus a1)(optimize_0plus a2)
| BLe a1 a2 => BLe (optimize_0plus a1)(optimize_0plus a2)
| BGt a1 a2 => BGt (optimize_0plus a1)(optimize_0plus a2)
| BNot b => BNot (optimize_0plus_b b)
| BAnd b1 b2 => BAnd (optimize_0plus_b b1) (optimize_0plus_b b2)
end.
Example optimize_0plus_b_test1:
optimize_0plus_b (BNot (BGt (APlus (ANum 0) (ANum 4)) (ANum 8))) =
(BNot (BGt (ANum 4) (ANum 8))).
Proof. reflexivity. Qed.
Example optimize_0plus_b_test2:
optimize_0plus_b (BAnd (BLe (APlus (ANum 0) (ANum 4)) (ANum 5)) BTrue) =
(BAnd (BLe (ANum 4) (ANum 5)) BTrue).
Proof. reflexivity. Qed.
Theorem optimize_0plus_b_sound : forall b,
beval (optimize_0plus_b b) = beval b.
Proof.
intros.
induction b;
try ( simpl; repeat (rewrite optimize_0plus_sound); reflexivity).
- simpl. rewrite IHb. reflexivity.
- simpl. rewrite IHb1. rewrite IHb2. reflexivity.
Qed.
(** [] *)
(* (** **** Exercise: 4 stars, standard, optional (optimize) *) *)
Fixpoint optimize ( a : aexp) : aexp :=
match a with
| ANum n => ANum n
| APlus e1 e2 =>
match optimize e1 with
| ANum 0 => optimize e2
| e1 =>
match optimize e2 with
| ANum 0 => e1
| e2 => APlus e1 e2
end
end
| AMinus e1 e2 =>
match optimize e1 with
| ANum 0 => ANum 0
| e1 =>
match optimize e2 with
| ANum 0 => e1
| e2 => AMinus e1 e2
end
end
| AMult e1 e2 =>
match optimize e1 with
| ANum 0 => ANum 0
| ANum 1 => optimize e2
| e1 =>
match optimize e2 with
| ANum 0 => ANum 0
| ANum 1 => e1
| e2 => AMult e1 e2
end
end
end.
Theorem optimize_sound : forall a,
aeval (optimize a) = aeval a.
Proof.
intros a.
induction a.
- reflexivity.
- simpl. rewrite <- IHa1. rewrite <- IHa2. destruct (optimize a1);
try (destruct (optimize a2); [destruct n; [rewrite add_0_r; reflexivity | reflexivity] | reflexivity | reflexivity | reflexivity]).
+ destruct n.
* reflexivity.
* destruct (optimize a2).
** destruct n0.
*** rewrite add_0_r. reflexivity.
*** reflexivity.
** reflexivity.
** reflexivity.
** reflexivity.
- simpl. rewrite <- IHa1. rewrite <- IHa2. destruct (optimize a1);
try (destruct (optimize a2); [destruct n; [rewrite sub_0_r; reflexivity | reflexivity] | reflexivity | reflexivity | reflexivity]).
+ destruct n.
* reflexivity.
* destruct (optimize a2).
** destruct n0.
*** reflexivity.
*** reflexivity.
** reflexivity.
** reflexivity.
** reflexivity.
- simpl. rewrite <- IHa1. rewrite <- IHa2. destruct (optimize a1);
try (destruct (optimize a2); [destruct n; [rewrite mul_0_r; reflexivity | destruct n; [rewrite mul_1_r; reflexivity | reflexivity]] | reflexivity | reflexivity | reflexivity]).
+ destruct n.
* reflexivity.
* destruct n.
** simpl. rewrite add_0_r. reflexivity.
** destruct (optimize a2).
*** destruct n0.
**** rewrite mul_0_r. reflexivity.
**** destruct n0.
***** rewrite mul_1_r. reflexivity.
***** reflexivity.
*** reflexivity.
*** reflexivity.
*** reflexivity.
Qed.
Ltac invert H :=
inversion H; subst; clear H.
Lemma invert_example1: forall {a b c: nat}, [a ;b] = [a;c] -> b = c.
intros.
invert H.
reflexivity.
Qed.
Example silly_presburger_example : forall m n o p,
m + n <= n + o /\ o + 3 = p + 3 ->
m <= p.
Proof.
intros. lia.
Qed.
Example add_comm__lia : forall m n,
m + n = n + m.
Proof.
intros. lia.
Qed.
Example add_assoc__lia : forall m n p,
m + (n + p) = m + n + p.
Proof.
intros. lia.
Qed.
Module aevalR_first_try.
Inductive aevalR : aexp -> nat -> Prop :=
| E_ANum (n : nat) :
aevalR (ANum n) n
| E_APlus (e1 e2 : aexp) (n1 n2 : nat ) :
aevalR e1 n1 ->
aevalR e2 n2 ->
aevalR (AMinus e1 e2) (n1 + n2)
| E_AMinus (e1 e2 : aexp) (n1 n2 : nat ) :
aevalR e1 n1 ->
aevalR e2 n2 ->
aevalR (AMinus e1 e2) (n1 - n2)
| E_AMult (e1 e2 : aexp) (n1 n2 : nat ) :
aevalR e1 n1 ->
aevalR e2 n2 ->
aevalR (AMinus e1 e2) (n1 * n2).
Module HypothesisNames.
Inductive aevalR : aexp -> nat -> Prop :=
| E_ANum (n : nat) :
aevalR (ANum n) n
| E_APlus (e1 e2 : aexp) (n1 n2 : nat)
(H1 : aevalR e1 n1)
(H2 : aevalR e2 n2) :
aevalR (APlus e1 e2) (n1 + n2)
| E_AMinus (e1 e2 : aexp) (n1 n2 : nat)
(H1 : aevalR e1 n1)
(H2 : aevalR e2 n2) :
aevalR (AMinus e1 e2) (n1 - n2)
| E_AMult (e1 e2 : aexp) (n1 n2 : nat)
(H1 : aevalR e1 n1)
(H2 : aevalR e2 n2) :
aevalR (AMult e1 e2) (n1 * n2).
End HypothesisNames.
Notation "e '==>' n"
:= (aevalR e n)
(at level 90, left associativity)
: type_scope.
End aevalR_first_try.
Reserved Notation "e '==>' n" (at level 90, left associativity).
Inductive aevalR : aexp -> nat -> Prop :=
| E_ANum (n :nat) :
(ANum n) ==> n
| E_APlus (e1 e2 : aexp) (n1 n2 :nat ) :
(e1 ==> n1) ->
(e2 ==> n2) ->
(APlus e1 e2) ==> (n1+n2)
| E_AMinus (e1 e2 : aexp) (n1 n2 : nat) :
(e1 ==> n1) ->
(e2 ==> n2) ->
(AMinus e1 e2) ==> (n1 - n2)
| E_AMult (e1 e2 : aexp) (n1 n2 : nat) :
(e1 ==> n1) ->
(e2 ==> n2) ->
(AMult e1 e2) ==> (n1*n2)
where "e '==>' n" := (aevalR e n) : type_scope.
Theorem aevalR_iff_aeval : forall a n,
(a ==> n) <-> aeval a = n.
Proof.
split.
- (* -> *)
intros H.
induction H; simpl.
+ (* E_ANum *)
reflexivity.
+ (* E_APlus *)
rewrite IHaevalR1. rewrite IHaevalR2. reflexivity.
+ (* E_AMinus *)
rewrite IHaevalR1. rewrite IHaevalR2. reflexivity.
+ (* E_AMult *)
rewrite IHaevalR1. rewrite IHaevalR2. reflexivity.
- (* <- *)
generalize dependent n.
induction a;
simpl; intros; subst.
+ (* ANum *)
apply E_ANum.
+ (* APlus *)
apply E_APlus.
* apply IHa1. reflexivity.
* apply IHa2. reflexivity.
+ (* AMinus *)
apply E_AMinus.
* apply IHa1. reflexivity.
* apply IHa2. reflexivity.
+ (* AMult *)
apply E_AMult.
* apply IHa1. reflexivity.
* apply IHa2. reflexivity.
Qed.
(** Again, we can make the proof quite a bit shorter using some
tacticals. *)
Theorem aevalR_iff_aeval' : forall a n,
(a ==> n) <-> aeval a = n.
Proof.
(* WORKED IN CLASS *)
split.
- (* -> *)
intros H; induction H; subst; reflexivity.
- (* <- *)
generalize dependent n.
induction a; simpl; intros; subst; constructor;
try apply IHa1; try apply IHa2; reflexivity.
Qed.
(** **** Exercise: 3 stars, standard (bevalR)
Write a relation [bevalR] in the same style as
[aevalR], and prove that it is equivalent to [beval]. *)
Reserved Notation "e '==>b' b" (at level 90, left associativity).
Inductive bevalR: bexp -> bool -> Prop :=
| E_BTrue : BTrue ==>b true
| E_BFalse : BFalse ==>b false
| E_BEq (e1 e2 : aexp) (n1 n2 : nat) (H1 : aevalR e1 n1) (H2 : aevalR e2 n2) : BEq e1 e2 ==>b n1 =? n2
| E_BNeq (e1 e2 : aexp)(n1 n2 : nat)(H1: aevalR e1 n1) (H2 : aevalR e2 n2) : BNeq e1 e2 ==>b negb(n1 =? n2)
| E_BLe (e1 e2 : aexp) (n1 n2 : nat) (H1 : aevalR e1 n1) (H2 : aevalR e2 n2) : BLe e1 e2 ==>b n1 <=? n2
| E_BGt (e1 e2 : aexp) (n1 n2 : nat) (H1 : aevalR e1 n1) (H2 : aevalR e2 n2) : BGt e1 e2 ==>b negb (n1 <=? n2)
| E_BNot (e : bexp) (b : bool) (H : bevalR e b) : BNot e ==>b negb b
| E_BAnd (e1 e2 : bexp) (b1 b2 : bool) (H1 : bevalR e1 b1) (H2 : bevalR e2 b2) : BAnd e1 e2 ==>b andb b1 b2
where "e '==>b' b" := (bevalR e b) : type_scope
.
Lemma bevalR_iff_beval : forall b bv,
b ==>b bv <-> beval b = bv.
Proof.
split.
- intros. induction H; simpl;
try (apply aevalR_iff_aeval in H1; apply aevalR_iff_aeval in H2); subst; reflexivity.
- intros.
destruct b;
subst; constructor;
try (apply aevalR_iff_aeval; reflexivity);
[ | rename b1 into b | rename b2 into b ];
induction b;
constructor;
try (apply aevalR_iff_aeval; reflexivity);
try (apply IHb);
try (apply IHb1);
try (apply IHb2).
Qed.
End AExp.
Module aevalR_division.
Inductive aexp : Type :=
| ANum (n : nat)
| APlus (a1 a2 : aexp)
| AMinus (a1 a2 : aexp)
| AMult (a1 a2 : aexp)
| ADiv (a1 a2 : aexp). (* <--- NEW *)
Reserved Notation "e '==>' n"
(at level 90, left associativity).
Inductive aevalR : aexp -> nat -> Prop :=
| E_ANum (n : nat) :
(ANum n) ==> n
| E_APlus (a1 a2 : aexp) (n1 n2 : nat) :
(a1 ==> n1) -> (a2 ==> n2) -> (APlus a1 a2) ==> (n1 + n2)
| E_AMinus (a1 a2 : aexp) (n1 n2 : nat) :
(a1 ==> n1) -> (a2 ==> n2) -> (AMinus a1 a2) ==> (n1 - n2)
| E_AMult (a1 a2 : aexp) (n1 n2 : nat) :
(a1 ==> n1) -> (a2 ==> n2) -> (AMult a1 a2) ==> (n1 * n2)
| E_ADiv (a1 a2 : aexp) (n1 n2 n3 : nat) : (* <----- NEW *)
(a1 ==> n1) -> (a2 ==> n2) -> (n2 > 0) ->
(mult n2 n3 = n1) -> (ADiv a1 a2) ==> n3
where "a '==>' n" := (aevalR a n) : type_scope.
End aevalR_division.
Module aevalR_extended.
Reserved Notation "e '==>' n" (at level 90, left associativity).
Inductive aexp : Type :=
| AAny (* <--- NEW *)
| ANum (n : nat)
| APlus (a1 a2 : aexp)
| AMinus (a1 a2 : aexp)
| AMult (a1 a2 : aexp).
Inductive aevalR : aexp -> nat -> Prop :=
| E_Any (n : nat) :
AAny ==> n (* <--- NEW *)
| E_ANum (n : nat) :
(ANum n) ==> n
| E_APlus (a1 a2 : aexp) (n1 n2 : nat) :
(a1 ==> n1) -> (a2 ==> n2) -> (APlus a1 a2) ==> (n1 + n2)
| E_AMinus (a1 a2 : aexp) (n1 n2 : nat) :
(a1 ==> n1) -> (a2 ==> n2) -> (AMinus a1 a2) ==> (n1 - n2)
| E_AMult (a1 a2 : aexp) (n1 n2 : nat) :
(a1 ==> n1) -> (a2 ==> n2) -> (AMult a1 a2) ==> (n1 * n2)
where "a '==>' n" := (aevalR a n) : type_scope.
End aevalR_extended.
Definition state := total_map nat.
Inductive aexp : Type :=
| ANum (n : nat)
| AId (x : string) (* <--- NEW *)
| APlus (a1 a2 : aexp)
| AMinus (a1 a2 : aexp)
| AMult (a1 a2 : aexp).
Definition W : string := "W".
Definition X : string := "X".
Definition Y : string := "Y".
Definition Z : string := "Z".
Inductive bexp : Type :=
| BTrue
| BFalse
| BEq (a1 a2 : aexp)
| BNeq (a1 a2 : aexp)
| BLe (a1 a2 : aexp)
| BGt (a1 a2 : aexp)
| BNot (b : bexp)
| BAnd (b1 b2 : bexp).
Coercion AId : string >-> aexp.
Coercion ANum : nat >-> aexp.
Declare Custom Entry com.
Declare Scope com_scope.
Declare Custom Entry com_aux.
Notation "<{ e }>" := e
(e custom com, format "'[hv' <{ '/ ' '[v' e ']' '/' }> ']'") : com_scope.
Notation "( x )" := x (in custom com, x at level 99).
Notation "x" := x (in custom com at level 0, x constr at level 0).
Notation "f x .. y" := (.. (f x) .. y)
(in custom com at level 0, only parsing,
f constr at level 0, x constr at level 1,
y constr at level 1).
Notation "x + y" := (APlus x y) (in custom com at level 50, left associativity).
Notation "x - y" := (AMinus x y) (in custom com at level 50, left associativity).
Notation "x * y" := (AMult x y) (in custom com at level 40, left associativity).
Notation "'true'" := true (at level 1).
Notation "'true'" := BTrue (in custom com at level 0).
Notation "'false'" := false (at level 1).
Notation "'false'" := BFalse (in custom com at level 0).
Notation "x <= y" := (BLe x y) (in custom com at level 70, no associativity).
Notation "x > y" := (BGt x y) (in custom com at level 70, no associativity).
Notation "x = y" := (BEq x y) (in custom com at level 70, no associativity).
Notation "x <> y" := (BNeq x y) (in custom com at level 70, no associativity).
Notation "x && y" := (BAnd x y) (in custom com at level 80, left associativity).
Notation "'~' b" := (BNot b) (in custom com at level 75, right associativity).
Open Scope com_scope.
Definition example_aexp : aexp := <{ 3 + (X+2)}>.
Definition example_bexp : bexp := <{ true && ~(X <= 4)}>.
Fixpoint aeval (st : state)
(a : aexp) : nat :=
match a with
| ANum n => n
| AId x => st x
| <{a1 + a2}> => (aeval st a1) + (aeval st a2)
| <{a1 - a2}> => (aeval st a1) - (aeval st a2)
| <{a1 * a2}> => (aeval st a1) * (aeval st a2)
end.
Fixpoint beval (st : state)
(b : bexp) : bool :=
match b with
| <{true}> => true
| <{false}> => false
| <{a1 = a2}> => (aeval st a1) =? (aeval st a2)
| <{a1 <> a2}> => negb ((aeval st a1) =? (aeval st a2))
| <{a1 <= a2}> => (aeval st a1) <=? (aeval st a2)
| <{a1 > a2}> => negb ((aeval st a1) <=? (aeval st a2))
| <{~b1}> => negb (beval st b1)
| <{b1 && b2}> => andb (beval st b1)(beval st b2)
end.
Definition empty_st := (_ !-> 0).
Notation "x '!->' v" := (x !-> v ; empty_st)(at level 100, right associativity).
Example aexp1 :
aeval (X !-> 5) <{ 3 + (X*2)}> = 13.
Proof. reflexivity. Qed.
Example aexp2 :
aeval (X !-> 5 ; Y !-> 4) <{ Z + (X * Y) }>
= 20.
Proof. reflexivity. Qed.
Example bexp1 :
beval (X !-> 5) <{ true && ~(X <= 4)}> = true.
Proof. reflexivity. Qed.
Inductive com : Type :=
| CSkip
| CAsgn (x : string) (a : aexp)
| CSeq (c1 c2 : com)
| CIf (b : bexp)(c1 c2 : com)
| CWhile (b:bexp)(c:com).
Notation "'skip'" := CSkip
(in custom com at level 0) : com_scope.
Notation "x := y" := (CAsgn x y)
(in custom com at level 0, x constr at level 0, y at level 85, no associativity,
format "x := y") : com_scope.
Notation "x ; y" := (CSeq x y)
(in custom com at level 90,
right associativity,
format "'[v' x ; '/' y ']'") : com_scope.
Notation "'if' x 'then' y 'else' z 'end'" := (CIf x y z)
(in custom com at level 89, x at level 99, y at level 99, z at level 99,
format "'[v' 'if' x 'then' '/ ' y '/' 'else' '/ ' z '/' 'end' ']'") : com_scope.
Notation "'while' x 'do' y 'end'" := (CWhile x y)
(in custom com at level 89, x at level 99, y at level 99,
format "'[v' 'while' x 'do' '/ ' y '/' 'end' ']'") : com_scope.
Definition fact_in_coq : com :=
<{ Z := X;
Y := 1;
while Z <> 0 do
Y := Y*Z;
Z := Z -1
end }>.
Print fact_in_coq.
Unset Printing Notations.
Print fact_in_coq.
Set Printing Notations.
Print example_bexp.
(* ===> example_bexp = <{(true && ~ (X <= 4))}> *)
Set Printing Coercions.
Print example_bexp.
(* ===> example_bexp = <{(true && ~ (AId X <= ANum 4))}> *)
Print fact_in_coq.
Unset Printing Coercions.
Locate aexp.
Locate "&&".
Locate ";".
Locate "while".
(* ================================================================= *)
(** ** More Examples *)
(* ----------------------------------------------------------------- *)
(** *** Assignment: *)
Definition plus2 : com :=
<{ X := X + 2 }>.
Definition XtimesYinZ : com :=
<{ Z := X * Y }>.
(* ----------------------------------------------------------------- *)
(** *** Loops *)
Definition subtract_slowly_body : com :=
<{ Z := Z - 1 ;
X := X - 1 }>.
Definition subtract_slowly : com :=
<{ while X <> 0 do
subtract_slowly_body
end }>.
Definition subtract_3_from_5_slowly : com :=
<{ X := 3 ;
Z := 5 ;
subtract_slowly }>.
(* ----------------------------------------------------------------- *)
(** *** An infinite loop: *)
Definition loop : com :=
<{ while true do
skip
end }>.
(* ################################################################# *)
(** * Evaluating Commands *)
(** Next we need to define what it means to evaluate an Imp command.
The fact that [while] loops don't necessarily terminate makes
defining an evaluation function tricky... *)
(* ================================================================= *)
(** ** Evaluation as a Function (Failed Attempt) *)
(** Here's an attempt at defining an evaluation function for commands
(with a bogus [while] case). *)
Fixpoint ceval_fun_no_while (st : state) (c : com) : state :=
match c with
| <{ skip }> => st
| <{ x := a}> => (x !-> aeval st a ; st)
| <{ c1 ; c2}> =>
let st' := ceval_fun_no_while st c1 in
ceval_fun_no_while st' c2
| <{ if b then c1 else c2 end}> =>
if (beval st b)
then ceval_fun_no_while st c1
else ceval_fun_no_while st c2
| <{ while b do c end }> =>
st
end.
Reserved Notation
"st '=[' c ']=>' st'"
(at level 40, c custom com at level 99,
st constr, st' constr at next level).
Inductive ceval : com -> state -> state -> Prop :=
| E_Skip : forall st,
st =[ skip ]=> st
| E_Asgn : forall st a n x,
aeval st a = n ->
st =[ x := a ]=> (x !-> n ; st)
| E_Seq : forall c1 c2 st st' st'',
st =[ c1 ]=> st' ->
st' =[ c2 ]=> st'' ->
st =[ c1 ; c2 ]=> st''
| E_IfTrue : forall st st' b c1 c2,
beval st b = true ->
st =[ c1 ]=> st' ->
st =[ if b then c1 else c2 end]=> st'
| E_IfFalse : forall st st' b c1 c2,
beval st b = false ->
st =[ c2 ]=> st' ->
st =[ if b then c1 else c2 end]=> st'
| E_WhileFalse : forall b st c,
beval st b = false ->
st =[ while b do c end ]=> st
| E_WhileTrue : forall st st' st'' b c,
beval st b = true ->
st =[ c ]=> st' ->
st' =[ while b do c end ]=> st'' ->
st =[ while b do c end ]=> st''
where "st =[ c ]=> st'" := (ceval c st st').
Example ceval_example1:
empty_st =[
X := 2;
if (X <= 1)
then Y := 3
else Z := 4
end
]=> (Z !-> 4 ; X !-> 2).
Proof.
(* We must supply the intermediate state *)
apply E_Seq with (X !-> 2).
- (* assignment command *)
apply E_Asgn. reflexivity.
- (* if command *)
apply E_IfFalse.
+ reflexivity.
+ apply E_Asgn. reflexivity.
Qed.
(** **** Exercise: 2 stars, standard (ceval_example2) *)
Example ceval_example2:
empty_st =[
X := 0;
Y := 1;
Z := 2
]=> (Z !-> 2 ; Y !-> 1 ; X !-> 0).
Proof.
apply E_Seq with (X !-> 0).
- apply E_Asgn. reflexivity.
- apply E_Seq with ( Y !-> 1 ; X !-> 0).
+ apply E_Asgn. reflexivity.
+ apply E_Asgn. reflexivity.
Qed.
Set Printing Implicit.
Check @ceval_example2.
Definition pup_to_n : com :=
<{ Y := 0;
while X > 0 do
Y := Y + X;
X := X -1
end }>.
Theorem pup_to_2_ceval :
(X !-> 2) =[
pup_to_n
]=> (X !-> 0 ; Y !-> 3 ; X !-> 1 ; Y !-> 2 ; Y !-> 0 ; X !-> 2).
Proof.
apply E_Seq with ( Y !-> 0 ; X !-> 2).
- apply E_Asgn. reflexivity.
- apply E_WhileTrue with (X !-> 1; Y !-> 2 ; Y !-> 0 ; X !-> 2).
+ reflexivity.
+ apply E_Seq with (Y !-> 2 ; Y !-> 0 ; X !-> 2).
* apply E_Asgn. reflexivity.
* apply E_Asgn. reflexivity.
+ apply E_WhileTrue with (X !-> 0 ; Y !-> 3 ; X !-> 1 ; Y !-> 2 ; Y !-> 0 ; X !-> 2).
* reflexivity.
* apply E_Seq with (Y !-> 3 ; X !-> 1 ; Y !-> 2 ; Y !-> 0 ; X !-> 2).
** apply E_Asgn. reflexivity.
** apply E_Asgn. reflexivity.
* apply E_WhileFalse. reflexivity.
Qed.
Theorem ceval_deterministic: forall c st st1 st2,
st =[ c ]=> st1 ->
st =[ c ]=> st2 ->
st1 = st2.
Proof.
intros c st st1 st2 E1 E2.
generalize dependent st2.
induction E1; intros st2 E2; inversion E2; subst.
- (* E_Skip *) reflexivity.
- (* E_Asgn *) reflexivity.
- (* E_Seq *)
rewrite (IHE1_1 st'0 H1) in *.
apply IHE1_2. assumption.
- (* E_IfTrue, b evaluates to true *)
apply IHE1. assumption.
- (* E_IfTrue, b evaluates to false (contradiction) *)
rewrite H in H5. discriminate.
- (* E_IfFalse, b evaluates to true (contradiction) *)
rewrite H in H5. discriminate.
- (* E_IfFalse, b evaluates to false *)
apply IHE1. assumption.
- (* E_WhileFalse, b evaluates to false *)
reflexivity.
- (* E_WhileFalse, b evaluates to true (contradiction) *)
rewrite H in H2. discriminate.
- (* E_WhileTrue, b evaluates to false (contradiction) *)
rewrite H in H4. discriminate.
- (* E_WhileTrue, b evaluates to true *)
rewrite (IHE1_1 st'0 H3) in *.
apply IHE1_2. assumption. Qed.
(* REASONING ABOUT IMP PROGRAMS *)
Theorem plus2_spec : forall st n st',
st X = n ->
st =[ plus2 ]=> st' ->
st' X = n + 2.
Proof.
intros st n st' HX Heval.
(** Inverting [Heval] essentially forces Coq to expand one step of
the [ceval] computation -- in this case revealing that [st']
must be [st] extended with the new value of [X], since [plus2]
is an assignment. *)
inversion Heval. subst. clear Heval. simpl.
apply t_update_eq. Qed.
(** **** Exercise: 3 stars, standard, optional (XtimesYinZ_spec)
State and prove a specification of [XtimesYinZ]. *)
(* FILL IN HERE *)
Theorem XtimesYinZ_spec : forall st m n st',
st X = m ->
st Y = n ->
st =[ XtimesYinZ ]=> st' ->
st' Z = m * n.
Proof.
intros st m n st' HX HY Heval.
inversion Heval. subst. clear Heval. simpl. apply t_update_eq.
Qed.
Theorem loop_never_stops : forall st st',
~(st =[ loop ]=> st').
Proof.
intros st st' contra. unfold loop in contra.
remember <{ while true do skip end }> as loopdef
eqn:Heqloopdef.
induction contra; inversion Heqloopdef; subst; try discriminate.
apply IHcontra2. reflexivity.
Qed.
Fixpoint no_whiles (c : com) : bool :=
match c with
| <{ skip }> =>
true
| <{ _ := _ }> =>
true
| <{ c1 ; c2}> =>
andb (no_whiles c1)(no_whiles c2)
| <{ if _ then ct else cf end}> =>
andb (no_whiles ct)(no_whiles cf)
| <{ while _ do _ end}> =>
false
end.
Inductive no_whilesR : com -> Prop :=
| no_whiles_Skip : no_whilesR <{ skip }>
| no_whiles_Asgn : forall x a, no_whilesR <{ x := a }>
| no_whiles_Seq : forall c1 c2 (H1 : no_whilesR c1)(H2 : no_whilesR c2), no_whilesR <{ c1 ; c2}>
| no_whiles_If : forall b c1 c2 (H1 : no_whilesR c1)(H2 : no_whilesR c2), no_whilesR<{if b then c1 else c2 end}>
.
Theorem no_whiles_eqv :
forall c, no_whiles c = true <-> no_whilesR c.
Proof.
intros.
split.
- induction c;
try constructor;
try (inversion H as [H1]; apply andb_true_iff in H1);
try (destruct H1 as [H1 _]; apply IHc1; apply H1);
try (destruct H1 as [_ H2]; apply IHc2; apply H2);
discriminate.
- intros H.
induction H;
try reflexivity;
apply andb_true_iff;
split;
assumption.
Qed.
Theorem no_whiles_terminating :
forall c, no_whilesR c -> forall st, exists st', st =[ c ]=> st'.
Proof.
intros c H.
induction c.
- intros. exists st. constructor.
- intros. exists (x !-> (aeval st a) ; st). constructor. reflexivity.
- intros. inversion H as [| | c1' c2' H1 H2|]. subst. clear H.
assert (H: exists st1, st =[ c1 ]=> st1). { apply IHc1. assumption. }
clear H1. destruct H as [st1 H1].
assert (H: exists st2, st1 =[ c2 ]=> st2). { apply IHc2. assumption. }
clear H2. destruct H as [st2 H2].
exists st2. apply E_Seq with (st':=st1); assumption.
- intros. inversion H as [| | | b' c1' c2' H1 H2]. subst. clear H.
assert (H: exists st1, st =[ c1 ]=> st1). { apply IHc1. assumption. }
clear H1. destruct H as [st1 H1].
assert (H: exists st2, st =[ c2 ]=> st2). { apply IHc2. assumption. }
clear H2. destruct H as [st2 H2].
destruct (beval st b) eqn:Hbeval.
+ exists st1. apply E_IfTrue; assumption.
+ exists st2. apply E_IfFalse; assumption.
- inversion H.
Qed.
(* ADDITIONAL EXERCISES *)
Inductive sinstr : Type :=
| SPush (n : nat)
| SLoad (x : string)
| SPlus
| SMinus
| SMult.
Fixpoint s_execute(st : state)(stack : list nat) (prog : list sinstr) : list nat :=
match prog with
| SPush n :: prog => s_execute st (n :: stack) prog
| SLoad x :: prog => s_execute st (st x :: stack) prog
| SPlus :: prog => match stack with
| n2 :: n1 :: stack => s_execute st (n1 + n2 :: stack) prog
| _ => s_execute st stack prog