-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeRelationExampleInput.v
3078 lines (2802 loc) · 85.7 KB
/
TypeRelationExampleInput.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Require Import Relations.
Require Import Classical.
Require Import Omega.
(* Add LoadPath ".". *)
Section labels.
Variable outputs : Type.
Variable inputs : Type.
Inductive label : Type :=
| inp : inputs -> label
| out : outputs -> label.
End labels.
Arguments inp [_] [_] _.
Arguments out [_] [_] _.
Section LTS.
Structure LTS : Type := lts {
carrier : Type;
inputs : Type;
outputs : Type;
return_type : Type;
return_state : carrier -> return_type -> Prop;
trans : label outputs inputs -> relation carrier;
return_stops : forall l e e' v, return_state e v -> (trans l e e' -> False) }.
Definition labels (T : LTS) := label (outputs T) (inputs T).
End LTS.
Coercion carrier : LTS >-> Sortclass.
Arguments trans [_] _ _ _.
Arguments return_state [_] _.
Section LTS_definitions.
Variable T : LTS.
Definition is_input {outputs : Type} {inputs : Type} (l : label outputs inputs) := match l with
| inp _ => True
| _ => False
end.
Definition input_total := forall P Q (i : inputs T), trans (inp i) P Q -> forall (j : inputs T), exists Q', trans (inp j) P Q' .
Definition determinate := forall (P Q Q' : T) (l l' : labels T), trans l P Q -> trans l' P Q' -> ((l = l' /\ Q = Q') \/ (is_input l /\ is_input l')).
Definition determinate_inputs := forall (P Q Q' : T) (i : inputs T), trans (inp i) P Q -> trans (inp i) P Q' -> Q = Q'.
Definition stuck e := (forall (l : labels T) e', ~ trans l e e') /\ (forall v, ~ return_state e v).
End LTS_definitions.
Arguments stuck [_] _.
Section simulations.
Variable S : LTS.
Variable T : LTS.
Variable rel_inp : inputs S -> inputs T -> Prop.
Variable rel_out : outputs S -> outputs T -> Prop.
Variable rel_ret : return_type S -> return_type T -> Prop.
Variable R : S -> T -> Prop.
Definition rel_input_total := forall j, exists i, rel_inp i j.
Definition partial_rel_input_total := forall se se' i0 j, trans (inp i0) se se' -> exists se'' i, rel_inp i j /\ trans (inp i) se se''.
Definition forward_simulation : Prop :=
forall se te, R se te ->
(forall sv, return_state se sv -> exists tv, return_state te tv /\ rel_ret sv tv)
/\ (forall tv, return_state te tv -> exists sv, return_state se sv /\ rel_ret sv tv)
/\ (forall se' so, trans (out so) se se' -> exists te', exists to, R se' te' /\ trans (out to) te te' /\ rel_out so to)
/\ (forall se' si, trans (inp si) se se' -> exists te', exists ti, R se' te' /\ trans (inp ti) te te' /\ rel_inp si ti).
Definition backward_simulation : Prop :=
forall se te, R se te ->
(forall tv, return_state te tv -> exists sv, return_state se sv /\ rel_ret sv tv)
/\ (forall te' to, trans (out to) te te' -> exists se', exists so, R se' te' /\ trans (out so) se se' /\ rel_out so to)
/\ (forall te' ti, trans (inp ti) te te' -> exists se', exists si, R se' te' /\ trans (inp si) se se' /\ rel_inp si ti).
Definition locally_flippable (R : S -> T -> Prop) (se : S) (te : T) : Prop :=
forall se' te' te'' si ti ti',
trans (inp si) se se'
-> trans (inp ti) te te'
-> trans (inp ti') te te''
-> rel_inp si ti
-> rel_inp si ti'
-> R se' te''
-> exists si0 se0, rel_inp si0 ti /\ trans (inp si0) se se0 /\ R se0 te'.
Definition flippable (R : S -> T -> Prop) := forall se te, R se te -> locally_flippable R se te.
Definition stuck_respecting (R : S -> T -> Prop) := forall se te, R se te -> stuck se -> stuck te.
End simulations.
Lemma inp_total_rel_inp_total_partial : forall S T rel_inp, input_total S -> rel_input_total S T rel_inp -> partial_rel_input_total S T rel_inp.
intros.
intros se se' si0 ti H'.
destruct (H0 ti) as [si].
destruct (H se se' si0) with si as [se''].
assumption.
exists se''. exists si.
split; try assumption.
Qed.
Arguments forward_simulation [_] [_] _ _ _ _.
Arguments backward_simulation [_] [_] _ _ _ _.
Arguments flippable [_] [_] _ _.
Arguments stuck_respecting [_] [_] _.
Section flipping_theorem.
Variable S : LTS.
Variable T : LTS.
Variable rel_inp : inputs S -> inputs T -> Prop.
Variable rel_out : outputs S -> outputs T -> Prop.
Variable rel_ret : return_type S -> return_type T -> Prop.
Variable R : S -> T -> Prop.
Hypothesis target_det : determinate T.
Hypothesis source_total : partial_rel_input_total S T rel_inp.
Hypothesis fwd : forward_simulation rel_inp rel_out rel_ret R.
Hypothesis flipbl : flippable rel_inp R.
Hypothesis stck : stuck_respecting R.
Lemma stck_stck : forall se te te' l, R se te -> trans l te te' -> exists l' se', trans l' se se'.
intros.
Admitted.
Theorem flip_theorem : backward_simulation rel_inp rel_out rel_ret R.
intros se te Rst.
split; try split.
+ apply fwd; assumption.
+ intros te' to tr.
destruct (stck_stck se te te' (out to)) as [l].
assumption.
assumption.
induction l.
- destruct H as [se' tr'].
exfalso.
destruct (fwd) with se te.
assumption.
destruct H0.
destruct H1.
destruct (H2 se' i) as [te''].
assumption.
destruct H3 as [ti].
destruct target_det with te te' te'' (out to : labels T) (inp ti : labels T).
assumption. apply H3.
* destruct H4.
inversion H4.
* destruct H4. inversion H4.
- destruct fwd with se te.
assumption.
destruct H1.
destruct H2.
destruct H as [se' H].
destruct (H2 se' o) as [te''].
assumption.
destruct H4 as [to'].
destruct target_det with te te' te'' (out to : labels T) (out to' : labels T).
* assumption.
* apply H4.
* destruct H5.
inversion H5; subst.
exists se'.
exists o.
split. apply H4.
split. assumption.
apply H4.
* destruct H5.
inversion H5.
+ intros te' ti tr.
destruct (stck_stck se te te' (inp ti)) as [l].
assumption.
assumption.
induction l.
- destruct H as [se' tr'].
destruct source_total with se se' i ti as [se''].
assumption.
destruct H as [si].
destruct fwd with se te.
assumption.
destruct H1.
destruct H2.
destruct (H3 se'' si) as [te''].
apply H.
destruct H4 as [ti'].
destruct flipbl with se te se'' te' te'' si ti ti' as [si0]; try assumption.
apply H.
apply H4.
apply H.
apply H4.
apply H4.
destruct H5 as [se0].
exists se0.
exists si0.
split; try split; try apply H5.
- destruct H as [se' tr'].
exfalso.
destruct (fwd) with se te.
assumption.
destruct H0.
destruct H1.
destruct (H1 se' o) as [te''].
assumption.
destruct H3 as [to].
destruct target_det with te te' te'' (inp ti : labels T) (out to : labels T).
assumption. apply H3.
* destruct H4.
inversion H4.
* destruct H4. inversion H5.
Qed.
End flipping_theorem.
(* SOURCE EXPRESSIONS, SOURCE LTS *)
Section source.
Inductive SExp :=
SNat : nat -> SExp
| SBool : bool -> SExp
| SPlus : SExp -> SExp -> SExp
| STimes : SExp -> SExp -> SExp
| SIte : SExp -> SExp -> SExp -> SExp
| SLe : SExp -> SExp -> SExp
| SInBool : SExp
| SInNat : SExp.
Inductive SInput :=
| SIBoo : bool -> SInput
| SINat : nat -> SInput.
Definition SLabel := label unit SInput.
Definition SLInput (si : SInput) : SLabel := inp si.
Definition SLSilent : SLabel := out tt.
Inductive STrans : SLabel -> SExp -> SExp -> Prop :=
| STPlus : forall n m, STrans SLSilent (SPlus (SNat n) (SNat m)) (SNat (n+m))
| STPlus_left : forall se1 se1' se2 sl, STrans sl se1 se1' -> STrans sl (SPlus se1 se2) (SPlus se1' se2)
| STPlus_right : forall se se' sl n, STrans sl se se' -> STrans sl (SPlus (SNat n) se) (SPlus (SNat n) se')
| STTimes : forall n m, STrans SLSilent (STimes (SNat n) (SNat m)) (SNat (n*m))
| STTimes_left : forall se1 se1' se2 sl, STrans sl se1 se1' -> STrans sl (STimes se1 se2) (STimes se1' se2)
| STTimes_right : forall se se' sl n, STrans sl se se' -> STrans sl (STimes (SNat n) se) (STimes (SNat n) se')
| STLe_left : forall se1 se1' se2 sl, STrans sl se1 se1' -> STrans sl (SLe se1 se2) (SLe se1' se2)
| STLe_right : forall n se2 se2' sl, STrans sl se2 se2' -> STrans sl (SLe (SNat n) se2) (SLe (SNat n) se2')
| STLe_true : forall n m, n <= m -> STrans SLSilent (SLe (SNat n) (SNat m)) (SBool true)
| STLe_false : forall n m, n > m -> STrans SLSilent (SLe (SNat n) (SNat m)) (SBool false)
| STIte_eval : forall se1 se1' se2 se3 sl, STrans sl se1 se1' -> STrans sl (SIte se1 se2 se3) (SIte se1' se2 se3)
| STIte_true : forall se2 se3, STrans SLSilent (SIte (SBool true) se2 se3) se2
| STIte_false : forall se2 se3, STrans SLSilent (SIte (SBool false) se2 se3) se3
| STInBool : forall b, STrans (SLInput (SIBoo b)) SInBool (SBool b)
| STInNat : forall n, STrans (SLInput (SINat n)) SInNat (SNat n).
Inductive SRetState : SExp -> SInput -> Prop :=
| SRetNat : forall n, SRetState (SNat n) (SINat n)
| SRetBool : forall b, SRetState (SBool b) (SIBoo b).
Lemma SReturn_stops : forall sl se se' sv, SRetState se sv -> (STrans sl se se' -> False).
intros.
induction H0; try inversion H.
Qed.
Definition Slts := lts SExp SInput unit SInput SRetState STrans SReturn_stops.
Definition SStuck (se : Slts) := stuck se.
End source.
Section source_typing.
Inductive type :=
TyNat
| TyBool.
Inductive typing : SExp -> type -> Prop :=
| type_nat : forall n, typing (SNat n) TyNat
| type_bool : forall b, typing (SBool b) TyBool
| type_plus : forall se1 se2,
typing se1 TyNat ->
typing se2 TyNat ->
typing (SPlus se1 se2) TyNat
| type_times : forall se1 se2,
typing se1 TyNat ->
typing se2 TyNat ->
typing (STimes se1 se2) TyNat
| type_hite : forall se1 se2 se3 t,
typing se1 TyBool ->
typing se2 t ->
typing se3 t ->
typing (SIte se1 se2 se3) t
| type_hle : forall se1 se2,
typing se1 TyNat ->
typing se2 TyNat ->
typing (SLe se1 se2) TyBool
| type_hinnat :
typing SInNat TyNat
| type_hinbool :
typing SInBool TyBool.
Lemma subject_reduction : forall se se' sl ty, typing se ty -> STrans sl se se' -> typing se' ty.
Admitted.
Lemma nonstck : forall (se : Slts), ~ stuck se -> (exists sv, SRetState se sv) \/ (exists sl se', STrans sl se se').
intros se nstck.
destruct (classic (exists sv, SRetState se sv)).
+ left. apply H.
+ right.
apply NNPP.
intro F. apply nstck.
split.
- intros l e'. intro F'. apply F.
exists l. exists e'. apply F'.
- intros v F'. apply H. exists v. apply F'.
Qed.
Lemma typed_notstuck : forall se ty, typing se ty -> ~ SStuck se.
intros se ty typ stck.
induction typ.
+ destruct stck as [ntr nret].
eapply nret. econstructor.
+ destruct stck as [ntr nret].
eapply nret; econstructor.
+ destruct (nonstck se1).
- intro F. apply IHtyp1. apply F.
- destruct H as [v ret].
inversion ret; subst.
destruct (nonstck se2).
* intro F. apply IHtyp2. apply F.
* destruct H as [v' ret'].
inversion ret'; subst.
destruct stck. destruct (H (SLSilent) (SNat (n+n0))).
constructor.
inversion typ2.
* destruct H as [sl H]. destruct H as [se' strans].
destruct stck. destruct (H sl (SPlus (SNat n) se')).
econstructor. assumption.
* inversion typ1.
- destruct H as [sl H]. destruct H as [se' strans].
destruct stck. destruct (H sl (SPlus se' se2)).
constructor. apply strans.
+ destruct (nonstck se1).
- intro F. apply IHtyp1. apply F.
- destruct H as [v ret].
inversion ret; subst.
destruct (nonstck se2).
* intro F. apply IHtyp2. apply F.
* destruct H as [v' ret'].
inversion ret'; subst.
destruct stck. destruct (H (SLSilent) (SNat (n*n0))).
constructor.
inversion typ2.
* destruct H as [sl H]. destruct H as [se' strans].
destruct stck. destruct (H sl (STimes (SNat n) se')).
econstructor. assumption.
* inversion typ1.
- destruct H as [sl H]. destruct H as [se' strans].
destruct stck. destruct (H sl (STimes se' se2)).
constructor. apply strans.
+ destruct (nonstck se1).
- intro F. apply IHtyp1. apply F.
- destruct H as [v ret].
inversion ret; subst.
inversion typ1.
destruct stck.
induction b.
* destruct (H SLSilent se2). constructor.
* destruct (H SLSilent se3). constructor.
- destruct H as [sl H]. destruct H as [se' strans].
destruct stck. destruct (H sl (SIte se' se2 se3)).
constructor. assumption.
+ destruct (nonstck se1).
- intro F. apply IHtyp1. apply F.
- destruct H as [v ret].
inversion ret; subst.
destruct (nonstck se2).
* intro F. apply IHtyp2. apply F.
* destruct H as [v' ret'].
inversion ret'; subst.
destruct stck.
destruct (classic (le n n0)).
destruct (H (SLSilent) (SBool true)).
constructor. assumption.
destruct (H (SLSilent) (SBool false)).
constructor. omega.
inversion typ2.
* destruct H as [sl H]. destruct H as [se' strans].
destruct stck. destruct (H sl (SLe (SNat n) se')).
apply STLe_right. assumption.
* inversion typ1.
- destruct H as [sl H]. destruct H as [se' strans].
destruct stck. destruct (H sl (SLe se' se2)).
constructor. apply strans.
+ destruct stck. destruct (H (inp (SINat 0)) (SNat 0)).
constructor.
+ destruct stck. destruct (H (inp (SIBoo true)) (SBool true)).
constructor.
Qed.
End source_typing.
(* TARGET EXPRESSIONS, TARGET LTS *)
Section target.
Inductive TExp :=
TNat : nat -> TExp
| TPlus : TExp -> TExp -> TExp
| TTimes : TExp -> TExp -> TExp
| TIte : TExp -> TExp -> TExp -> TExp -> TExp
| TIn : TExp.
Inductive TInput :=
| TINat : nat -> TInput.
Definition TLabel := label unit TInput.
Definition TLInput (ti : TInput) : TLabel := inp ti.
Definition TLSilent : TLabel := out tt.
Inductive TTrans : TLabel -> TExp -> TExp -> Prop :=
| TTPlus : forall n m, TTrans TLSilent (TPlus (TNat n) (TNat m)) (TNat (n+m))
| TTPlus_left : forall te1 te1' te2 tl, TTrans tl te1 te1' -> TTrans tl (TPlus te1 te2) (TPlus te1' te2)
| TTPlus_right : forall te te' tl n, TTrans tl te te' -> TTrans tl (TPlus (TNat n) te) (TPlus (TNat n) te')
| TTTimes : forall n m, TTrans TLSilent (TTimes (TNat n) (TNat m)) (TNat (n*m))
| TTTimes_left : forall te1 te1' te2 tl, TTrans tl te1 te1' -> TTrans tl (TTimes te1 te2) (TTimes te1' te2)
| TTTimes_right : forall te te' tl n, TTrans tl te te' -> TTrans tl (TTimes (TNat n) te) (TTimes (TNat n) te')
| TTIte_eval_left : forall te1 te1' te2 te3 te4 tl, TTrans tl te1 te1' -> TTrans tl (TIte te1 te2 te3 te4) (TIte te1' te2 te3 te4)
| TTIte_eval_right : forall n te2 te2' te3 te4 tl, TTrans tl te2 te2' -> TTrans tl (TIte (TNat n) te2 te3 te4) (TIte (TNat n) te2' te3 te4)
| TTIte_true : forall n m te3 te4, n <= m -> TTrans TLSilent (TIte (TNat n) (TNat m) te3 te4) te3
| TTIte_false : forall n m te3 te4, n > m -> TTrans TLSilent (TIte (TNat n) (TNat m) te3 te4) te4
| TTIn : forall n, TTrans (TLInput (TINat n)) TIn (TNat n).
Inductive TRetState : TExp -> TInput -> Prop :=
| TRetNat : forall n, TRetState (TNat n) (TINat n).
Lemma TReturn_stops : forall tl te te' tv, TRetState te tv -> (TTrans tl te te' -> False).
intros. induction H0; try inversion H.
Qed.
Definition Tlts := lts TExp TInput unit TInput TRetState TTrans TReturn_stops.
Definition TStuck (te : Tlts) := stuck te.
Lemma determinate_inputs_target : determinate_inputs Tlts.
Admitted.
Lemma determinate_target : determinate Tlts.
Admitted.
End target.
(* COMPILER *)
Section compiler.
Fixpoint compile (se : SExp) : TExp :=
match se with
SNat n => TNat n
| SBool true => TNat 1
| SBool false => TNat 0
| SPlus se1 se2 => TPlus (compile se1) (compile se2)
| STimes se1 se2 => TTimes (compile se1) (compile se2)
| SLe se1 se2 => TIte (compile se1) (compile se2) (TNat 1) (TNat 0)
| SIte se1 se2 se3 => TIte (compile se1) (TNat 0) (compile se3) (compile se2)
| SInBool => TIn
| SInNat => TIn
end.
End compiler.
(* The simulation relation, the relations between inputs/outputs, proving it can be flipped properties *)
Section simulation_relation.
Inductive rel_inp : inputs Slts -> inputs Tlts -> Prop :=
| rel_nat : forall n, rel_inp (SINat n) (TINat n)
| rel_true : forall n, n > 0 -> rel_inp (SIBoo true) (TINat n)
| rel_false : rel_inp (SIBoo false) (TINat 0).
Inductive rel_out : unit -> unit -> Prop := silent_refl : rel_out tt tt.
Lemma is_input_total : partial_rel_input_total Slts Tlts rel_inp.
intros se se' si0 ti H.
induction H.
Admitted.
Inductive simulation0 : Slts -> Tlts -> Prop :=
| SimNat : forall n, simulation0 (SNat n) (TNat n)
| SimTrue : forall n, n > 0 -> simulation0 (SBool true) (TNat n)
| SimFalse : simulation0 (SBool false) (TNat 0)
| SimPlus : forall se1 se2 te1 te2, simulation0 se1 te1 -> simulation0 se2 te2 -> simulation0 (SPlus se1 se2) (TPlus te1 te2)
| SimTimes : forall se1 se2 te1 te2, simulation0 se1 te1 -> simulation0 se2 te2 -> simulation0 (STimes se1 se2) (TTimes te1 te2)
| SimLe : forall se1 se2 te1 te2, simulation0 se1 te1 -> simulation0 se2 te2 -> simulation0 (SLe se1 se2) (TIte te1 te2 (TNat 1) (TNat 0))
| SimIte : forall se1 se2 se3 te1 te2 te3, simulation0 se1 te1 -> simulation0 se2 te2 -> simulation0 se3 te3 -> simulation0 (SIte se1 se2 se3) (TIte te1 (TNat 0) te3 te2)
| SimInBool : simulation0 SInBool TIn
| SimInNat : simulation0 SInNat TIn.
Definition sim_typed : Slts -> Tlts -> Prop := fun se te => exists ty, typing se ty /\ simulation0 se te.
Lemma sim_compile : forall se, simulation0 se (compile se).
intro se.
induction se; try constructor; try assumption.
induction b; constructor. constructor.
Qed.
Ltac badtr := exfalso; try (eapply TReturn_stops; [econstructor| eassumption]); try (eapply SReturn_stops; [econstructor| eassumption]);
try (eapply SReturn_stops; [eapply SRetBool| eassumption]).
Lemma simulation_allpositive : forall (se se' : Slts) (te te' : Tlts) n, n > 0 -> simulation0 se te -> STrans (inp (SIBoo true)) se se' -> TTrans (inp(TINat n)) te te' -> simulation0 se' te'.
intros se se' te te' n pos sim. revert se' te'.
induction sim; intros se' te' strans ttrans.
+ exfalso. eapply SReturn_stops. econstructor. apply strans.
+ exfalso. eapply TReturn_stops. econstructor. apply ttrans.
+ exfalso. eapply TReturn_stops. econstructor. eassumption.
+ inversion strans; subst.
inversion ttrans; subst.
- constructor. apply IHsim1; try assumption.
assumption.
- inversion sim1; subst.
* exfalso. eapply SReturn_stops. econstructor.
eassumption.
* exfalso. eapply SReturn_stops. eapply SRetBool. apply H3.
* exfalso. eapply SReturn_stops. eapply SRetBool. apply H3.
- inversion ttrans; subst.
* inversion sim1. subst.
badtr.
* inversion sim1; subst.
constructor.
constructor.
apply IHsim2.
assumption.
assumption.
+ inversion strans; subst.
inversion ttrans; subst.
- constructor. apply IHsim1; try assumption.
assumption.
- inversion sim1; subst; badtr.
- inversion sim1; subst.
inversion ttrans; subst. badtr.
constructor.
constructor.
apply IHsim2.
assumption.
assumption.
+ inversion strans; subst.
inversion ttrans; subst.
- constructor. apply IHsim1; try assumption.
assumption.
- inversion ttrans; subst.
inversion sim1; subst; badtr.
inversion sim1; subst; badtr.
- inversion ttrans; subst.
* inversion sim1. subst.
badtr.
* inversion sim1; subst.
constructor.
constructor.
apply IHsim2.
assumption.
assumption.
+ inversion strans; subst.
inversion ttrans; subst.
- constructor. apply IHsim1; try assumption.
assumption. assumption.
- badtr.
+ inversion strans; subst.
inversion ttrans; subst.
constructor. assumption.
+ inversion strans.
Qed.
Lemma is_flippable : flippable rel_inp simulation0.
intros se te Rst se' te' te'' si ti ti' tsi tti tti' ri ri' Rst'.
induction si.
+ induction b.
- exists (SIBoo true). exists se'.
split. assumption.
split. assumption.
inversion ri. subst.
inversion ri'. subst.
eapply simulation_allpositive.
apply H.
apply Rst.
apply tsi.
apply tti.
- inversion ri. subst.
inversion ri'. subst.
exists (SIBoo false). exists se'.
split. constructor.
split. assumption.
destruct determinate_inputs_target with te te' te'' (TINat 0).
apply tti. apply tti'.
assumption.
+ inversion ri. subst. inversion ri'. subst.
exists (SINat n). exists se'.
split. assumption.
split. assumption.
destruct determinate_inputs_target with te te' te'' (TINat n).
apply tti. apply tti'.
assumption.
Qed.
Lemma is_stck_respecting : stuck_respecting sim_typed.
intros se te simst stck.
unfold sim_typed in simst.
destruct simst as [ty H]. destruct H as [typ sim0].
exfalso.
eapply typed_notstuck.
eassumption.
apply stck.
Qed.
Lemma is_flippable_typed : flippable rel_inp sim_typed.
intros se te Rst se' te' te'' si ti ti' tsi tti tti' ri ri' Rst'.
destruct Rst as [ty Rst]. destruct Rst as [typ Rst].
destruct Rst' as [ty' Rst']. destruct Rst' as [typ' Rst'].
destruct is_flippable with se te se' te' te'' si ti ti' as [si0 H]; try assumption.
destruct H as [se0 H].
exists si0. exists se0.
split. apply H.
split. apply H.
exists ty.
split.
eapply subject_reduction. apply typ. apply H.
apply H.
Qed.
Lemma is_forward_sim : forward_simulation rel_inp rel_out rel_inp simulation0.
intros se te Rst. induction Rst.
+ split; [|split; [|split]]; intros.
- exists (TINat n). split. econstructor. inversion H; subst. constructor.
- exists (SINat n). split; try constructor. inversion H; subst. constructor.
- badtr.
- badtr.
+ split; [|split; [|split]]; intros.
- exists (TINat n). split. econstructor. inversion H0; subst. constructor. assumption.
- exists (SIBoo true). split; try constructor. inversion H0; subst. constructor. assumption.
- badtr.
- badtr.
+ split; [|split; [|split]]; intros.
- exists (TINat 0). split. econstructor. inversion H; subst. constructor.
- exists (SIBoo false). split; try constructor. inversion H; subst. constructor.
- badtr.
- badtr.
+ split; [|split; [|split]]; intros.
- inversion H.
- inversion H.
- inversion H; subst.
* inversion Rst1; subst. inversion Rst2; subst.
exists (TNat (n+m)). exists tt.
split; try split.
constructor.
constructor.
constructor.
* destruct IHRst1. destruct H1. destruct H2.
destruct (H2 se1' so) as [te' HH]. assumption.
destruct HH as [to H5].
exists (TPlus te' te2). exists to.
split; try split.
constructor. apply H5. assumption.
constructor. apply H5.
apply H5.
* destruct IHRst2. destruct H1. destruct H2.
destruct (H2 se'0 so) as [te' HH]. assumption.
destruct HH as [to H5].
exists (TPlus te1 te'). exists to.
inversion Rst1. subst.
split; try split.
constructor. constructor. apply H5.
apply TTPlus_right. apply H5.
apply H5.
- inversion H; subst.
* destruct IHRst1. destruct H1. destruct H2.
destruct (H3 se1' si) as [te' HH]. assumption.
destruct HH as [ti H5].
exists (TPlus te' te2). exists ti.
split; try split.
constructor. apply H5. assumption.
constructor. apply H5.
apply H5.
* destruct IHRst2. destruct H1. destruct H2.
destruct (H3 se'0 si) as [te' HH]. assumption.
destruct HH as [ti H5].
exists (TPlus te1 te'). exists ti.
inversion Rst1. subst.
split; try split.
constructor. constructor. apply H5.
apply TTPlus_right. apply H5.
apply H5.
+ split; [|split; [|split]]; intros.
- inversion H.
- inversion H.
- inversion H; subst.
* inversion Rst1; subst. inversion Rst2; subst.
exists (TNat (n*m)). exists tt.
split; try split.
constructor.
constructor.
constructor.
* destruct IHRst1. destruct H1. destruct H2.
destruct (H2 se1' so) as [te' HH]. assumption.
destruct HH as [to H5].
exists (TTimes te' te2). exists to.
split; try split.
constructor. apply H5. assumption.
constructor. apply H5.
apply H5.
* destruct IHRst2. destruct H1. destruct H2.
destruct (H2 se'0 so) as [te' HH]. assumption.
destruct HH as [to H5].
exists (TTimes te1 te'). exists to.
inversion Rst1. subst.
split; try split.
constructor. constructor. apply H5.
constructor. apply H5.
apply H5.
- inversion H; subst.
* destruct IHRst1. destruct H1. destruct H2.
destruct (H3 se1' si) as [te' HH]. assumption.
destruct HH as [ti H5].
exists (TTimes te' te2). exists ti.
split; try split.
constructor. apply H5. assumption.
constructor. apply H5.
apply H5.
* destruct IHRst2. destruct H1. destruct H2.
destruct (H3 se'0 si) as [te' HH]. assumption.
destruct HH as [ti H5].
exists (TTimes te1 te'). exists ti.
inversion Rst1. subst.
split; try split.
constructor. constructor. apply H5.
constructor. apply H5.
apply H5.
+ split; [|split; [|split]]; intros.
- exfalso; inversion H.
- exfalso; inversion H.
- inversion H; subst.
* destruct IHRst1. destruct H1. destruct H2.
destruct (H2 se1' so) as [te' HH]. assumption.
destruct HH as [to H5].
exists (TIte te' te2 (TNat 1) (TNat 0)). exists to.
split; try split.
constructor. apply H5. assumption.
constructor. apply H5.
apply H5.
* destruct IHRst2. destruct H1. destruct H2.
destruct (H2 se2' so) as [te' HH]. assumption.
destruct HH as [to H5].
exists (TIte te1 te' (TNat 1) (TNat 0)). exists to.
inversion Rst1. subst.
split; try split.
constructor. constructor. apply H5.
constructor. apply H5.
apply H5.
* inversion Rst1; subst. inversion Rst2; subst.
exists (TNat 1). exists tt.
split; try split.
constructor.
constructor.
constructor.
omega.
constructor.
* inversion Rst1; subst. inversion Rst2; subst.
exists (TNat 0). exists tt.
split; try split.
constructor.
constructor.
omega.
constructor.
- inversion H; subst.
* destruct IHRst1. destruct H1. destruct H2.
destruct (H3 se1' si) as [te' HH]. assumption.
destruct HH as [to H5].
exists (TIte te' te2 (TNat 1) (TNat 0)). exists to.
split; try split.
constructor. apply H5. assumption.
constructor. apply H5.
apply H5.
* destruct IHRst2. destruct H1. destruct H2.
destruct (H3 se2' si) as [te' HH]. assumption.
destruct HH as [to H5].
exists (TIte te1 te' (TNat 1) (TNat 0)). exists to.
inversion Rst1. subst.
split; try split.
constructor. constructor. apply H5.
constructor. apply H5.
apply H5.
+ split; [|split; [|split]]; intros.
- exfalso; inversion H.
- exfalso; inversion H.
- inversion H; subst.
* destruct IHRst1. destruct H1. destruct H2.
destruct (H2 se1' so) as [te' HH]. assumption.
destruct HH as [to H6].
exists (TIte te' (TNat 0) te3 te2). exists to.
split; try split.
constructor. apply H6. assumption.
assumption. constructor. apply H6.
apply H6.
* inversion Rst1; subst.
exists te2. exists tt.
split; try split.
assumption.
constructor.
assumption.
constructor.
* inversion Rst1; subst.
exists te3. exists tt.
split; try split.
assumption.
constructor.
constructor.
constructor.
- inversion H; subst.
destruct IHRst1. destruct H1. destruct H2.
destruct (H3 se1' si) as [te' HH]. assumption.
destruct HH as [ti H6].
exists (TIte te' (TNat 0) te3 te2). exists ti.
split; try split.
constructor. apply H6. assumption.
assumption. constructor. apply H6.
apply H6.
+ split; [|split; [|split]]; intros.
- exfalso; inversion H.
- exfalso; inversion H.
- exfalso; inversion H.
- inversion H; subst.
induction b.
* exists (TNat 1).
exists (TINat 1).
split; try split; constructor.
omega. omega.
* exists (TNat 0).
exists (TINat 0).
split; try split; constructor.
+ split; [|split; [|split]]; intros.
- exfalso; inversion H.
- exfalso; inversion H.
- exfalso; inversion H.
- inversion H; subst.
exists (TNat n).
exists (TINat n).
split; try split; constructor.
Qed.
Lemma is_forward_sim_typed : forward_simulation rel_inp rel_out rel_inp sim_typed.
intros se te Rst.
destruct Rst as [ty H].
destruct H as [typ sim0].
destruct is_forward_sim with se te.
apply sim0.
destruct H0.
destruct H1.
split; try split; try split.
+ apply H.
+ apply H0.
+ intros.
destruct H1 with se' so as [te' ].
assumption.
destruct H4 as [to].
exists te'. exists to.
split; try split.
- exists ty.
split.
eapply subject_reduction; try eassumption.
apply H4.
- apply H4.
- apply H4.
+ intros.
destruct H2 with se' si as [te' ].
assumption.
destruct H4 as [ti].
exists te'. exists ti.
split; try split.
- exists ty.
split.
eapply subject_reduction; try eassumption.
apply H4.
- apply H4.
- apply H4.
Qed.
Lemma is_backward_sim : backward_simulation rel_inp rel_out rel_inp sim_typed.
apply flip_theorem.
apply determinate_target.
apply is_input_total.
apply is_forward_sim_typed.
apply is_flippable_typed.
apply is_stck_respecting.
Qed.
End simulation_relation.
(* Big step semantics *)
Require Import List.
Import ListNotations.