-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSmallstep.v
701 lines (573 loc) · 15.9 KB
/
Smallstep.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
Require Export Imp.
Require Export Relations.
Inductive tm : Type :=
| tm_const : nat -> tm
| tm_plus : tm -> tm -> tm.
Tactic Notation "tm_cases" tactic(first) ident(c) :=
first;
[ Case_aux c "tm_const" | Case_aux c "tm_plus" ].
Module SimpleArith0.
Fixpoint eval (t : tm) : nat :=
match t with
| tm_const n => n
| tm_plus a1 a2 => eval a1 + eval a2
end.
End SimpleArith0.
Module SimpleArith1.
Reserved Notation " t '===>' n " (at level 50, left associativity).
Inductive eval : tm -> nat -> Prop :=
| E_Const : forall n,
tm_const n ===> n
| E_Plus : forall t1 t2 n1 n2,
t1 ===> n1 ->
t2 ===> n2 ->
tm_plus t1 t2 ===> plus n1 n2
where " t '===>' n " := (eval t n).
End SimpleArith1.
Reserved Notation " t '===>' t' " (at level 50, left associativity).
Inductive eval : tm -> tm -> Prop :=
| E_Const : forall n1,
tm_const n1 ===> tm_const n1
| E_Plus : forall t1 n1 t2 n2,
t1 ===> tm_const n1 ->
t2 ===> tm_const n2 ->
tm_plus t1 t2 ===> tm_const (plus n1 n2)
where " t '===>' t' " := (eval t t').
Tactic Notation "eval_cases" tactic(first) ident(c) :=
first;
[ Case_aux c "E_Const" | Case_aux c "E_Plus" ].
Module SimpleArith2.
Reserved Notation " t '=>' t' " (at level 40).
Inductive step : tm -> tm -> Prop :=
| ST_PlusConstConst : forall n1 n2,
tm_plus (tm_const n1) (tm_const n2) => tm_const (plus n1 n2)
| ST_Plus1 : forall t1 t1' t2,
t1 => t1' ->
tm_plus t1 t2 => tm_plus t1' t2
| ST_Plus2 : forall n1 t2 t2',
t2 => t2' ->
tm_plus (tm_const n1) t2 => tm_plus (tm_const n1) t2'
where " t '=>' t' " := (step t t').
Tactic Notation "step_cases" tactic(first) ident(c) :=
first;
[ Case_aux c "ST_PlusConstConst"
| Case_aux c "ST_Plus1" | Case_aux c "ST_Plus2" ].
Example test_step_1 :
tm_plus
(tm_plus (tm_const 0) (tm_const 3))
(tm_plus (tm_const 2) (tm_const 4))
=>
tm_plus
(tm_const (plus 0 3))
(tm_plus (tm_const 2) (tm_const 4)).
Proof.
apply ST_Plus1. apply ST_PlusConstConst. Qed.
Example test_step_2 :
tm_plus
(tm_const 0)
(tm_plus
(tm_const 2)
(tm_plus (tm_const 0) (tm_const 3)))
=>
tm_plus
(tm_const 0)
(tm_plus
(tm_const 2)
(tm_const (plus 0 3))).
Proof.
apply ST_Plus2.
simpl.
apply ST_Plus2.
apply ST_PlusConstConst.
Qed.
Theorem step_deterministic:
partial_function step.
Proof.
unfold partial_function. intros x y1 y2 Hy1 Hy2.
generalize dependent y2.
step_cases (induction Hy1) Case; intros y2 Hy2.
Case "ST_PlusConstConst". step_cases (inversion Hy2) SCase.
SCase "ST_PlusConstConst". reflexivity.
SCase "ST_Plus1". inversion H2.
SCase "ST_Plus2". inversion H2.
Case "ST_Plus1". step_cases (inversion Hy2) SCase.
SCase "ST_PlusConstConst". rewrite <- H0 in Hy1. inversion Hy1.
SCase "ST_Plus1".
rewrite <- (IHHy1 t1'0).
reflexivity. assumption.
SCase "ST_Plus2". rewrite <- H in Hy1. inversion Hy1.
Case "ST_Plus2". step_cases (inversion Hy2) SCase.
SCase "ST_PlusConstConst". rewrite <- H1 in Hy1. inversion Hy1.
SCase "ST_Plus1". inversion H2.
SCase "ST_Plus2".
rewrite <- (IHHy1 t2'0).
reflexivity. assumption. Qed.
End SimpleArith2.
Inductive value : tm -> Prop :=
v_const: forall n, value (tm_const n).
Reserved Notation " t '=>' t' " (at level 40).
Inductive step : tm -> tm -> Prop :=
| ST_PlusConstConst : forall n1 n2,
tm_plus (tm_const n1) (tm_const n2)
=> tm_const (plus n1 n2)
| ST_Plus1 : forall t1 t1' t2,
t1 => t1' ->
tm_plus t1 t2 => tm_plus t1' t2
| ST_Plus2 : forall v1 t2 t2',
value v1 ->
t2 => t2' ->
tm_plus v1 t2 => tm_plus v1 t2'
where " t '=>' t' " := (step t t').
Tactic Notation "step_cases" tactic(first) ident(c) :=
first;
[ Case_aux c "ST_PlusConstConst"
| Case_aux c "ST_Plus1" | Case_aux c "ST_Plus2" ].
Theorem step_deterministic :
partial_function step.
Proof.
unfold partial_function.
intros x y1 y2 Hy1 Hy2.
generalize dependent y2.
step_cases (induction Hy1) Case; intros y2 Hy2.
step_cases (inversion Hy2) SCase.
reflexivity.
inversion H2.
inversion Hy2.
subst.
assumption.
subst.
inversion H3.
subst.
inversion H3.
step_cases (inversion Hy2) SCase.
rewrite <- H0 in Hy1.
inversion Hy1.
rewrite <- (IHHy1 t1'0).
reflexivity.
assumption.
rewrite <- H in Hy1.
rewrite <- H in H1.
subst.
inversion H1.
subst.
inversion Hy1.
step_cases (inversion Hy2) SCase.
subst.
inversion Hy1.
subst.
inversion H.
subst.
inversion H3.
subst.
inversion H2.
subst.
rewrite <- (IHHy1 t2'0).
reflexivity.
assumption.
Qed.
Theorem strong_progress : forall t,
value t \/ (exists t', t => t').
Proof.
tm_cases (induction t) Case.
Case "tm_const". left. apply v_const.
Case "tm_plus". right. inversion IHt1.
SCase "l". inversion IHt2.
SSCase "l". inversion H. inversion H0.
exists (tm_const (plus n n0)).
apply ST_PlusConstConst.
SSCase "r". inversion H0 as [t' H1].
exists (tm_plus t1 t').
apply ST_Plus2. apply H. apply H1.
SCase "r". inversion H as [t' H0].
exists (tm_plus t' t2).
apply ST_Plus1. apply H0. Qed.
Definition normal_form {X:Type} (R: relation X) (t: X) : Prop :=
~ (exists t', R t t').
Lemma value_is_nf: forall t,
value t -> normal_form step t.
Proof.
unfold normal_form. intros t H. inversion H.
intros contra. inversion contra. inversion H1.
Qed.
Lemma nf_is_value: forall t,
normal_form step t -> value t.
Proof.
unfold normal_form. intros t H.
assert (G: value t \/ (exists t', t => t')).
SCase "Proof of assertion". apply strong_progress.
inversion G.
SCase "l". assumption.
SCase "r". apply ex_falso_quodlibet. apply H. assumption. Qed.
Corollary nf_same_as_value : forall t,
normal_form step t <-> value t.
Proof.
split. apply nf_is_value. apply value_is_nf.
Qed.
Module Temp1.
Inductive value : tm -> Prop :=
| v_const : forall n, value (tm_const n)
| v_funny : forall t1 n2, (* <---- *)
value (tm_plus t1 (tm_const n2)).
Reserved Notation " t '=>' t' " (at level 40).
Inductive step : tm -> tm -> Prop :=
| ST_PlusConstConst : forall n1 n2,
tm_plus (tm_const n1) (tm_const n2) => tm_const (plus n1 n2)
| ST_Plus1 : forall t1 t1' t2,
t1 => t1' ->
tm_plus t1 t2 => tm_plus t1' t2
| ST_Plus2 : forall v1 t2 t2',
value v1 ->
t2 => t2' ->
tm_plus v1 t2 => tm_plus v1 t2'
where " t '=>' t' " := (step t t').
Lemma value_not_same_as_normal_form:
exists t, value t /\ ~ normal_form step t.
Proof.
intros.
unfold normal_form.
exists (tm_plus (tm_plus (tm_const 1) (tm_const 2)) (tm_const 2)).
split.
apply v_funny.
unfold not.
intros.
apply H.
exists (tm_plus (tm_const (1 + 2)) (tm_const 2)).
apply ST_Plus1.
apply ST_PlusConstConst.
Qed.
End Temp1.
Module Temp2.
Inductive value : tm -> Prop :=
| v_const : forall n, value (tm_const n).
(*Reserved Notation " t '===>' t' " (at level 40).*)
Inductive step : tm -> tm -> Prop :=
| ST_Funny : forall n, (* <---- *)
tm_const n ===> tm_plus (tm_const n) (tm_const 0)
| ST_PlusConstConst : forall n1 n2,
tm_plus (tm_const n1) (tm_const n2) ===> tm_const (plus n1 n2)
| ST_Plus1 : forall t1 t1' t2,
t1 ===> t1' ->
tm_plus t1 t2 ===> tm_plus t1' t2
| ST_Plus2 : forall v1 t2 t2',
value v1 ->
t2 ===> t2' ->
tm_plus v1 t2 ===> tm_plus v1 t2'
where " t '===>' t' " := (step t t').
Lemma value_not_same_as_normal_form :
exists t, value t /\ ~ normal_form step t.
Proof.
exists (tm_const 0).
split.
apply v_const.
unfold normal_form.
unfold not.
intro H.
apply H.
exists (tm_plus (tm_const 0) (tm_const 0)).
apply ST_Funny.
Qed.
End Temp2.
Module Temp3.
Inductive value : tm -> Prop :=
| v_const : forall n, value (tm_const n).
(*Reserved Notation " t '===>' t' " (at level 40).*)
Inductive step : tm -> tm -> Prop :=
| ST_PlusConstConst : forall n1 n2,
tm_plus (tm_const n1) (tm_const n2) ===> tm_const (plus n1 n2)
| ST_Plus1 : forall t1 t1' t2,
t1 ===> t1' ->
tm_plus t1 t2 ===> tm_plus t1' t2
where " t '===>' t' " := (step t t').
Lemma value_not_same_as_normal_form:
exists t, ~ value t /\ normal_form step t.
Proof.
exists (tm_plus (tm_const 1) (tm_plus (tm_const 0) (tm_const 0))).
split.
intros H.
inversion H.
unfold normal_form.
intros H.
inversion H.
inversion H0.
inversion H4.
Qed.
End Temp3.
Module Temp4.
Inductive tm : Type :=
| tm_true : tm
| tm_false : tm
| tm_if : tm -> tm -> tm -> tm.
Inductive value : tm -> Prop :=
| v_true : value tm_true
| v_false : value tm_false.
Inductive step : tm -> tm -> Prop :=
| ST_IfTrue : forall t1 t2,
tm_if tm_true t1 t2 ===> t1
| ST_IfFalse : forall t1 t2,
tm_if tm_false t1 t2 ===> t2
| ST_If : forall t1 t1' t2 t3,
t1 ===> t1' ->
tm_if t1 t2 t3 ===> tm_if t1' t2 t3
where " t '===>' t' " := (step t t').
Example bool_step_prop3 :
tm_if
(tm_if tm_true tm_true tm_true)
(tm_if tm_true tm_true tm_true)
tm_false
===>
tm_if
tm_true
(tm_if tm_true tm_true tm_true)
tm_false.
Proof.
apply ST_If.
apply ST_IfTrue.
Qed.
Theorem strong_progress: forall t,
value t \/ (exists t', t ===> t').
Proof.
induction t.
left.
constructor.
left.
constructor.
right.
inversion IHt1.
inversion H.
exists t2.
apply ST_IfTrue.
exists t3.
apply ST_IfFalse.
inversion H.
exists (tm_if x t2 t3).
apply ST_If.
assumption.
Qed.
Theorem step_deterministic :
partial_function step.
Proof.
unfold partial_function.
intros x y1 y2 Hy1 Hy2.
generalize dependent y2.
induction Hy1.
intros.
inversion Hy2.
reflexivity.
subst.
inversion H3.
intros.
inversion Hy2.
reflexivity.
inversion H3.
intros.
inversion Hy2.
subst.
inversion Hy1.
subst.
inversion Hy1.
subst.
apply IHHy1 in H3.
subst.
reflexivity.
Qed.
Module Temp5.
Inductive step : tm -> tm -> Prop :=
| ST_IfTrue : forall t1 t2,
tm_if tm_true t1 t2 ===> t1
| ST_IfFalse : forall t1 t2,
tm_if tm_false t1 t2 ===> t2
| ST_If : forall t1 t1' t2 t3,
t1 ===> t1' ->
tm_if t1 t2 t3 ===> tm_if t1' t2 t3
| ST_ShortCut : forall v t,
value v ->
tm_if t v v ===> v
where " t '===>' t' " := (step t t').
Definition bool_step_prop4 :=
tm_if
(tm_if tm_true tm_true tm_true)
tm_false
tm_false
===>
tm_false.
Example bool_step_prop4_holds :
bool_step_prop4.
Proof.
unfold bool_step_prop4.
apply ST_ShortCut.
constructor.
Qed.
Theorem strong_progress: forall t,
value t \/ (exists t', t ===> t').
Proof.
induction t.
left.
constructor.
left.
constructor.
inversion IHt1.
right.
inversion H.
exists t2.
constructor.
exists t3.
constructor.
right.
inversion H.
exists (tm_if x t2 t3).
apply ST_If.
assumption.
Qed.
End Temp5.
End Temp4.
Definition stepmany := refl_step_closure step.
Notation " t '===>*' t' " := (stepmany t t') (at level 40).
Lemma test_stepmany_1:
tm_plus
(tm_plus (tm_const 0) (tm_const 3))
(tm_plus (tm_const 2) (tm_const 4))
===>*
tm_const (plus (plus 0 3) (plus 2 4)).
Proof.
eapply rsc_step. apply ST_Plus1. apply ST_PlusConstConst.
eapply rsc_step. apply ST_Plus2. apply v_const.
apply ST_PlusConstConst.
eapply rsc_step. apply ST_PlusConstConst.
apply rsc_refl. Qed.
Lemma test_stepmany_2:
tm_const 3 ===>* tm_const 3.
Proof.
eapply rsc_refl.
Qed.
Lemma test_stepmany_3:
tm_plus (tm_const 0) (tm_const 3)
===>*
tm_plus (tm_const 0) (tm_const 3).
Proof.
eapply rsc_refl.
Qed.
Lemma test_stepmany_4:
tm_plus
(tm_const 0)
(tm_plus
(tm_const 2)
(tm_plus (tm_const 0) (tm_const 3)))
===>*
tm_plus
(tm_const 0)
(tm_const (plus 2 (plus 0 3))).
Proof.
eapply rsc_step.
apply ST_Plus2.
apply v_const.
apply ST_Plus2.
apply v_const.
apply ST_PlusConstConst.
eapply rsc_step.
apply ST_Plus2.
apply v_const.
apply ST_PlusConstConst.
eapply rsc_refl.
Qed.
Definition step_normal_form := normal_form step.
Definition normal_form_of (t t' : tm) :=
(t ===>* t' /\ step_normal_form t').
(*
Theorem normal_forms_unique:
partial_function normal_form_of.
Proof.
unfold partial_function. unfold normal_form_of. intros x y1 y2 P1 P2.
destruct P1 as [P11 P12]. destruct P2 as [P21 P22].
generalize dependent y2.
unfold step_normal_form in P12.
unfold step_normal_form.
unfold normal_form.
unfold normal_form in P12.
induction x.
intros.
unfold stepmany.
inversion P11.
subst.
inversion P21.
subst.
reflexivity.
subst.
inversion P21.
reflexivity.
subst.
inversion H1.
inversion H.
*)
Definition normalizing {X:Type} (R:relation X) :=
forall t, exists t',
(refl_step_closure R) t t' /\ normal_form R t'.
Lemma stepmany_congr_1 : forall t1 t1' t2,
t1 ===>* t1' ->
tm_plus t1 t2 ===>* tm_plus t1' t2.
Proof.
intros t1 t1' t2 H.
rsc_cases (induction H) Case.
apply rsc_refl.
apply rsc_step with (tm_plus y t2).
apply ST_Plus1.
apply H.
apply IHrefl_step_closure.
Qed.
Lemma stepmany_congr2 : forall t1 t2 t2',
value t1 ->
t2 ===>* t2' ->
tm_plus t1 t2 ===>* tm_plus t1 t2'.
Proof.
intros t1 t2 t2'.
intros H1.
intros H2.
induction H2.
apply rsc_refl.
apply rsc_step with (tm_plus t1 y).
apply ST_Plus2.
assumption.
assumption.
assumption.
Qed.
Theorem step_normalizing :
normalizing step.
Proof.
unfold normalizing.
tm_cases (induction t) Case.
Case "tm_const".
exists (tm_const n).
split.
SCase "l". apply rsc_refl.
SCase "r".
(* We can use rewrite with "iff" statements, not
just equalities: *)
rewrite nf_same_as_value. apply v_const.
Case "tm_plus".
destruct IHt1 as [t1' H1]. destruct IHt2 as [t2' H2].
destruct H1 as [H11 H12]. destruct H2 as [H21 H22].
rewrite nf_same_as_value in H12. rewrite nf_same_as_value in H22.
inversion H12 as [n1]. inversion H22 as [n2].
rewrite <- H in H11.
rewrite <- H0 in H21.
exists (tm_const (plus n1 n2)).
split.
SCase "l".
apply rsc_trans with (tm_plus (tm_const n1) t2).
apply stepmany_congr_1. apply H11.
apply rsc_trans with
(tm_plus (tm_const n1) (tm_const n2)).
apply stepmany_congr2. apply v_const. apply H21.
apply rsc_R. apply ST_PlusConstConst.
SCase "r".
rewrite nf_same_as_value. apply v_const. Qed.
Lemma eval__value : forall t1 t2,
eval t1 t2 ->
value t2.
Proof.
intros t1 t2 HE.
eval_cases (inversion HE) Case; apply v_const. Qed.
(*
Theorem eval__stepmany: forall t v,
eval t v -> t ===>* v.
Proof.
*)