/
flat_patternProofScript.sml
2566 lines (2404 loc) · 75.1 KB
/
flat_patternProofScript.sml
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
(*
Correctness proof for flat_pattern
*)
open preamble flat_patternTheory
semanticPrimitivesTheory semanticPrimitivesPropsTheory
flatLangTheory flatSemTheory flatPropsTheory backendPropsTheory
pattern_semanticsTheory
local open bagSimps in end
val _ = temp_delsimps ["lift_disj_eq", "lift_imp_disj"]
val _ = new_theory "flat_patternProof"
val _ = set_grammar_ancestry ["flat_pattern",
"misc","ffi","bag","flatProps",
"backendProps","backend_common",
"pattern_semantics"];
(* simple properties *)
Theorem op_sets_globals_gbag:
op_sets_globals op = (op_gbag op <> {||})
Proof
Cases_on `op` \\ simp [op_sets_globals_def, op_gbag_def]
QED
Theorem compile_exp_set_globals_FST_SND:
(!cfg x. FST (SND (compile_exp cfg x)) =
(set_globals x <> {||})) /\
(!cfg xs. FST (SND (compile_exps cfg xs)) =
(elist_globals xs <> {||})) /\
(!cfg ps. FST (SND (compile_match cfg ps)) =
(elist_globals (MAP SND ps) <> {||}))
Proof
ho_match_mp_tac compile_exp_ind \\ rw [compile_exp_def] \\ fs []
\\ rpt (pairarg_tac \\ fs [])
\\ rveq \\ fs []
\\ fs [flatPropsTheory.elist_globals_REVERSE, op_sets_globals_gbag]
\\ simp [DISJ_ASSOC]
\\ simp [EXISTS_MEM, elist_globals_eq_empty, PULL_EXISTS, MEM_MAP]
\\ simp [EXISTS_PROD, ELIM_UNCURRY]
\\ metis_tac []
QED
Theorem compile_exp_set_globals_tup:
(!cfg x i sg y. compile_exp cfg x = (i, sg, y) ==>
sg = (set_globals x <> {||})) /\
(!cfg xs i sg ys. compile_exps cfg xs = (i, sg, ys) ==>
sg = (elist_globals xs <> {||})) /\
(!cfg ps i sg ps2. compile_match cfg ps = (i, sg, ps2) ==>
sg = (elist_globals (MAP SND ps) <> {||}))
Proof
metis_tac [compile_exp_set_globals_FST_SND, FST, SND]
QED
(* decoding the encoded names *)
Theorem sum_string_ords_eq:
sum_string_ords i s = SUM (MAP (\c. ORD c - 35) (DROP i s))
Proof
measureInduct_on `(\i. LENGTH s - i) i`
\\ simp [Once sum_string_ords_def]
\\ rw [rich_listTheory.DROP_EL_CONS, listTheory.DROP_LENGTH_TOO_LONG]
QED
Theorem dec_enc:
!xs. dec_name_to_num (enc_num_to_name i xs) =
i + SUM (MAP (\c. ORD c - 35) xs)
Proof
measureInduct_on `I i`
\\ simp [Once enc_num_to_name_def]
\\ CASE_TAC \\ simp [dec_name_to_num_def, sum_string_ords_eq]
QED
Theorem enc_num_to_name_inj:
(enc_num_to_name i [] = enc_num_to_name j []) = (i = j)
Proof
metis_tac [dec_enc |> Q.SPEC `[]` |> SIMP_RULE list_ss []]
QED
Theorem env_is_v_fold:
<| v := env.v |> = env
Proof
simp [environment_component_equality]
QED
(* lists and lookups *)
Theorem LIST_REL_ALOOKUP_OPTREL:
!xs ys. LIST_REL R xs ys /\
(!x y. R x y /\ MEM x xs /\ MEM y ys /\ (v = FST x \/ v = FST y) ==>
FST x = FST y /\ R2 (SND x) (SND y))
==> OPTREL R2 (ALOOKUP xs v) (ALOOKUP ys v)
Proof
Induct \\ rpt (Cases ORELSE gen_tac)
\\ simp [optionTheory.OPTREL_def]
\\ qmatch_goalsub_abbrev_tac `ALOOKUP (pair :: _)`
\\ Cases_on `pair`
\\ simp []
\\ rpt strip_tac
\\ last_x_assum drule
\\ impl_tac >- metis_tac []
\\ simp []
\\ strip_tac
\\ first_x_assum drule
\\ rw []
\\ fs [optionTheory.OPTREL_def]
QED
Theorem LIST_REL_ALOOKUP:
!xs ys. LIST_REL R xs ys /\
(!x y. R x y /\ MEM x xs /\ MEM y ys /\ (v = FST x \/ v = FST y) ==> x = y)
==> ALOOKUP xs v = ALOOKUP ys v
Proof
REWRITE_TAC [GSYM optionTheory.OPTREL_eq]
\\ rpt strip_tac
\\ drule_then irule LIST_REL_ALOOKUP_OPTREL
\\ metis_tac []
QED
Theorem LIST_REL_FILTER_MONO:
!xs ys. LIST_REL R (FILTER P1 xs) (FILTER P2 ys) /\
(!x. MEM x xs /\ P3 x ==> P1 x) /\
(!y. MEM y ys /\ P4 y ==> P2 y) /\
(!x y. MEM x xs /\ MEM y ys /\ R x y ==> P3 x = P4 y)
==> LIST_REL R (FILTER P3 xs) (FILTER P4 ys)
Proof
Induct
>- (
simp [FILTER_EQ_NIL, EVERY_MEM]
\\ metis_tac []
)
\\ gen_tac
\\ simp []
\\ reverse CASE_TAC
>- (
CASE_TAC
>- metis_tac []
\\ rw []
)
\\ rpt (gen_tac ORELSE disch_tac)
\\ fs [FILTER_EQ_CONS]
\\ rename [`_ = ys_pre ++ [y] ++ ys_post`]
\\ rveq \\ fs []
\\ fs [FILTER_APPEND]
\\ first_x_assum drule
\\ simp []
\\ disch_tac
\\ `FILTER P4 ys_pre = []` by (fs [FILTER_EQ_NIL, EVERY_MEM] \\ metis_tac [])
\\ rw []
\\ metis_tac []
QED
Theorem COND_false:
~ P ==> ((if P then x else y) = y)
Proof
simp []
QED
Theorem COND_true:
P ==> ((if P then x else y) = x)
Proof
simp []
QED
Theorem FILTER_EQ_MONO = LIST_REL_FILTER_MONO
|> Q.GEN `R` |> Q.ISPEC `(=)` |> REWRITE_RULE [LIST_REL_eq]
Theorem FILTER_EQ_MONO_TRANS = FILTER_EQ_MONO
|> Q.GEN `P2` |> Q.SPEC `\x. T`
|> Q.GEN `P4` |> Q.SPEC `P3`
|> REWRITE_RULE [FILTER_T]
Theorem FILTER_EQ_ALOOKUP_EQ:
!xs ys. FILTER P xs = ys /\ (!z. P (x, z)) ==>
ALOOKUP xs x = ALOOKUP ys x
Proof
Induct \\ simp []
\\ Cases
\\ rw []
\\ fs []
QED
Theorem ALOOKUP_FILTER_EQ = FILTER_EQ_ALOOKUP_EQ |> SIMP_RULE bool_ss []
|> GSYM;
Theorem MEM_enumerate_EL:
!xs i x. MEM x (enumerate i xs) = (?j. j < LENGTH xs /\ x = (i + j, EL j xs))
Proof
Induct \\ rw [miscTheory.enumerate_def]
\\ simp [arithmeticTheory.LT_SUC]
\\ EQ_TAC \\ rw []
\\ simp [EL_CONS, ADD1]
\\ simp [GSYM ADD1]
\\ qexists_tac `0` \\ simp []
QED
Theorem ALL_DISTINCT_enumerate:
!xs i. ALL_DISTINCT (enumerate i xs)
Proof
Induct \\ rw [miscTheory.enumerate_def, MEM_enumerate_EL]
QED
Definition pure_eval_to_def:
pure_eval_to s env exp v = (evaluate env s [exp] = (s, Rval [v]))
End
Theorem pmatch_list_Match_IMP_LENGTH:
!xs ys env env' s. pmatch_list s xs ys env = Match env' ==>
LENGTH xs = LENGTH ys
Proof
Induct
>- (
Cases \\ simp [flatSemTheory.pmatch_def]
)
>- (
gen_tac \\ Cases \\ simp [flatSemTheory.pmatch_def]
\\ simp [CaseEq "match_result"]
\\ metis_tac []
)
QED
Theorem pmatch_list_append_Match_exists:
(pmatch_list s (xs ++ ys) vs pre_bindings = Match bindings) =
(?vs1 vs2 bindings1. vs = vs1 ++ vs2 /\
pmatch_list s xs vs1 pre_bindings = Match bindings1 /\
pmatch_list s ys vs2 bindings1 = Match bindings)
Proof
Cases_on `LENGTH vs <> LENGTH xs + LENGTH ys`
>- (
EQ_TAC \\ rw []
\\ imp_res_tac pmatch_list_Match_IMP_LENGTH
\\ fs []
)
\\ fs []
\\ qspecl_then [`xs`, `TAKE (LENGTH xs) vs`, `ys`, `DROP (LENGTH xs) vs`,
`s`, `pre_bindings`]
mp_tac flatPropsTheory.pmatch_list_append
\\ rw []
\\ simp [CaseEq "match_result"]
\\ EQ_TAC \\ rw []
\\ imp_res_tac pmatch_list_Match_IMP_LENGTH
\\ simp [TAKE_APPEND, DROP_APPEND, DROP_LENGTH_TOO_LONG]
\\ rpt (goal_assum (first_assum o mp_then Any mp_tac))
\\ simp []
QED
Definition ALOOKUP_rel_def:
ALOOKUP_rel P R env1 env2 =
(!x. P x ==> OPTREL R (ALOOKUP env1 x) (ALOOKUP env2 x))
End
Theorem ALOOKUP_rel_empty:
ALOOKUP_rel P R [] []
Proof
simp [ALOOKUP_rel_def, OPTREL_def]
QED
Theorem ALOOKUP_rel_cons_false:
(~ P x ==> ALOOKUP_rel P R ((x, y) :: env1) env2 = ALOOKUP_rel P R env1 env2)
/\
(~ P x ==> ALOOKUP_rel P R env1 ((x, y) :: env2) = ALOOKUP_rel P R env1 env2)
Proof
simp [ALOOKUP_rel_def]
\\ metis_tac [COND_CLAUSES]
QED
Theorem ALOOKUP_rel_APPEND_L_false:
EVERY ((~) o P o FST) xs ==>
ALOOKUP_rel P R (xs ++ ys) zs =
ALOOKUP_rel P R ys zs
Proof
rw [ALOOKUP_rel_def, ALOOKUP_APPEND]
\\ EQ_TAC \\ rw []
\\ first_x_assum drule
\\ CASE_TAC \\ simp []
\\ drule ALOOKUP_MEM
\\ fs [EVERY_MEM, FORALL_PROD]
\\ metis_tac []
QED
Theorem ALOOKUP_rel_refl:
(!x y. P x /\ MEM (x, y) xs ==> R y y) ==> ALOOKUP_rel P R xs xs
Proof
rw [ALOOKUP_rel_def]
\\ Cases_on `ALOOKUP xs x`
\\ simp [OPTREL_def]
\\ metis_tac [ALOOKUP_MEM]
QED
Theorem ALOOKUP_rel_cons:
(P x ==> R y z) /\ ALOOKUP_rel P R ys zs ==>
ALOOKUP_rel P R ((x, y) :: ys) ((x, z) :: zs)
Proof
rw [ALOOKUP_rel_def] \\ rw [] \\ simp [OPTREL_def]
QED
Theorem ALOOKUP_rel_mono:
ALOOKUP_rel P R xs ys /\
(!x y z. P' x ==> P x) /\
(!x y z. P' x /\ R y z ==> R' y z) ==>
ALOOKUP_rel P' R' xs ys
Proof
rw [ALOOKUP_rel_def]
\\ fs [OPTREL_def]
\\ metis_tac []
QED
Theorem ALOOKUP_rel_mono_rel:
(!y z. R y z ==> R' y z) ==>
ALOOKUP_rel P R xs ys ==>
ALOOKUP_rel P R' xs ys
Proof
metis_tac [ALOOKUP_rel_mono]
QED
Theorem ALOOKUP_rel_append_suff:
ALOOKUP_rel P R xs1 xs3 /\ ALOOKUP_rel P R xs2 xs4 ==>
ALOOKUP_rel P R (xs1 ++ xs2) (xs3 ++ xs4)
Proof
rw [ALOOKUP_rel_def, ALOOKUP_APPEND]
\\ res_tac
\\ EVERY_CASE_TAC
\\ fs [OPTREL_def]
QED
Theorem ALOOKUP_rel_EQ_ALOOKUP:
ALOOKUP_rel P (=) xs ys /\ P x ==>
ALOOKUP xs x = ALOOKUP ys x
Proof
simp [ALOOKUP_rel_def]
QED
Theorem ALOOKUP_rel_eq_fst:
!xs ys.
LIST_REL (\x y. FST x = FST y /\ (P (FST x) ==> R (SND x) (SND y))) xs ys ==>
ALOOKUP_rel P R xs ys
Proof
Induct \\ rpt Cases
\\ fs [ALOOKUP_rel_def, OPTREL_def]
\\ Cases_on `h`
\\ rw []
\\ rw []
QED
Theorem pat_bindings_evaluate_FOLDR_lemma1:
!xs.
(!x. MEM x xs ==> IS_SOME (f x)) /\
(!x. IMAGE (THE o f) (set xs) ⊆ new_names) /\
(!x. MEM x xs ==> (?rv. !env2.
ALOOKUP_rel (\x. x ∉ new_names) (=) env2.v env.v ==>
evaluate env2 s [g x] = (s, Rval [rv])))
==>
!env2.
ALOOKUP_rel (\x. x ∉ new_names) (=) env2.v env.v ==>
evaluate env2 s
[FOLDR (λx exp. flatLang$Let t (f x) (g x) exp) exp xs] =
evaluate (env2 with v := MAP (λx. (THE (f x),
case evaluate env s [g x] of (_, Rval rv) => HD rv)) (REVERSE xs)
++ env2.v) s [exp]
Proof
Induct \\ simp [env_is_v_fold]
\\ rw []
\\ simp [evaluate_def]
\\ fs [DISJ_IMP_THM, FORALL_AND_THM, IMP_CONJ_THM, IS_SOME_EXISTS]
\\ rfs []
\\ simp [libTheory.opt_bind_def, ALOOKUP_rel_cons_false]
\\ simp_tac bool_ss [GSYM APPEND_ASSOC, APPEND]
\\ simp [ALOOKUP_rel_refl]
QED
Theorem pat_bindings_evaluate_FOLDR_lemma:
!new_names.
(!x. MEM x xs ==> IS_SOME (f x)) /\
(!x. IMAGE (THE o f) (set xs) ⊆ new_names) /\
(!x. MEM x xs ==> (?rv. !env2.
ALOOKUP_rel (\x. x ∉ new_names) (=) env2.v env.v ==>
evaluate env2 s [g x] = (s, Rval [rv])))
==>
evaluate env s
[FOLDR (λx exp. flatLang$Let t (f x) (g x) exp) exp xs] =
evaluate (env with v := MAP (λx. (THE (f x),
case evaluate env s [g x] of (_, Rval rv) => HD rv)) (REVERSE xs)
++ env.v) s [exp]
Proof
rw []
\\ DEP_REWRITE_TAC [pat_bindings_evaluate_FOLDR_lemma1]
\\ simp [ALOOKUP_rel_refl]
QED
Definition v_cons_in_c_def1:
v_cons_in_c c (Conv stmp xs) = (
(case stmp of SOME con_stmp => (con_stmp, LENGTH xs) ∈ c
| NONE => T) /\
EVERY (v_cons_in_c c) xs) /\
v_cons_in_c c (Vectorv vs) = EVERY (v_cons_in_c c) vs /\
v_cons_in_c c (Closure env n exp) = EVERY (\x. v_cons_in_c c (SND x)) env /\
v_cons_in_c c (Recclosure env funs n) = EVERY (\x. v_cons_in_c c (SND x)) env /\
v_cons_in_c c other = T
Termination
WF_REL_TAC `measure (v_size o SND)`
\\ rw []
\\ fs [MEM_SPLIT, SUM_APPEND, v3_size, v1_size, v_size_def]
End
Theorem v_cons_in_c_def[simp] = v_cons_in_c_def1
|> CONV_RULE (DEPTH_CONV ETA_CONV)
|> SIMP_RULE bool_ss [prove (``(λx. v_cons_in_c c (SND x))
= (v_cons_in_c c o SND)``, simp [o_DEF])]
(* a note on 'naming' below. the existing (encoded) names in the program
(in the original x and exp)
are < j for starting val j. during the recursion, j increases to i, with
new names i < nm < j appearing in the new env, and in *expressions* in
n_bindings. note *names* in n_bindings/pre_bindings come from the original
program. also new/old names mix in env, thus the many filters. *)
Theorem compile_pat_bindings_simulation:
! t i n_bindings exp exp2 spt s vs pre_bindings bindings env s2 res vset.
compile_pat_bindings t i n_bindings exp = (spt, exp2) /\
pmatch_list s (MAP FST n_bindings) vs pre_bindings = Match bindings /\
evaluate env s [exp2] = (s2, res) /\
LIST_REL (\(_, k, v_exp) v. !env2. k ∈ domain spt /\
ALOOKUP_rel ((\k. k > j /\ k < i) o dec_name_to_num) (=) env2.v env.v
==>
pure_eval_to s env2 v_exp v)
n_bindings vs /\
EVERY ((\k. k < j) o dec_name_to_num o FST) pre_bindings /\
j < i /\
ALOOKUP_rel ((\k. k < j) o dec_name_to_num) (=) env.v
(pre_bindings ++ base_vs) /\
EVERY (\(p, k, _). EVERY (\nm. dec_name_to_num nm < j) (pat_bindings p []) /\
j < k /\ k < i) n_bindings /\
EVERY (v_cons_in_c s.c ∘ SND) env.v /\
EVERY (EVERY (v_cons_in_c s.c) ∘ store_v_vs) s.refs /\
EVERY (v_cons_in_c s.c) vs
==>
?env2. evaluate env2 s [exp] = (s2, res) /\
EVERY (v_cons_in_c s.c ∘ SND) env2.v /\
ALOOKUP_rel ((\k. k < j) o dec_name_to_num) (=) env2.v (bindings ++ base_vs)
Proof
ho_match_mp_tac compile_pat_bindings_ind
\\ rpt conj_tac
\\ simp_tac bool_ss [compile_pat_bindings_def, flatSemTheory.pmatch_def,
PULL_EXISTS, EVERY_DEF, PAIR_EQ, MAP, LIST_REL_NIL, LIST_REL_CONS1,
LIST_REL_CONS2, FORALL_PROD]
\\ rpt strip_tac
\\ fs [] \\ rveq \\ fs []
\\ fs [flatSemTheory.pmatch_def]
>- (
metis_tac []
)
>- (
metis_tac []
)
>- (
rpt (pairarg_tac \\ fs [])
\\ rveq \\ fs []
\\ fs [evaluate_def]
\\ qpat_x_assum `!env. _ ==> pure_eval_to _ _ x _` mp_tac
\\ disch_then (qspec_then `env` mp_tac)
\\ simp [pure_eval_to_def, ALOOKUP_rel_refl]
\\ rw []
\\ fs [pat_bindings_def]
\\ last_x_assum (drule_then (drule_then irule))
\\ simp [libTheory.opt_bind_def]
\\ simp [ALOOKUP_rel_cons]
\\ first_x_assum (fn t => mp_tac t \\ match_mp_tac LIST_REL_mono)
\\ simp [FORALL_PROD, ALOOKUP_rel_cons_false]
)
>- (
qmatch_asmsub_abbrev_tac `pmatch _ (Plit l) lv`
\\ Cases_on `lv` \\ fs [flatSemTheory.pmatch_def]
\\ qpat_x_assum `_ = Match _` mp_tac
\\ simp [CaseEq "match_result", bool_case_eq]
\\ rw []
\\ metis_tac []
)
>- (
(* Pcon *)
qmatch_asmsub_abbrev_tac `pmatch _ (Pcon stmp _) con_v`
\\ qpat_x_assum `_ = Match _` mp_tac
\\ simp [CaseEq "match_result"]
\\ rw []
\\ Cases_on `con_v` \\ fs [flatSemTheory.pmatch_def]
\\ fs [bool_case_eq]
\\ rpt (pairarg_tac \\ fs [])
\\ rveq \\ fs []
\\ fs [MAP_MAP_o |> REWRITE_RULE [o_DEF], UNCURRY, Q.ISPEC `SND` ETA_THM]
\\ fs [LENGTH_enumerate, MAP_enumerate_MAPi, MAPi_eq_MAP]
\\ qpat_x_assum `evaluate _ _ [FOLDR _ _ _] = _` mp_tac
\\ simp [ELIM_UNCURRY]
\\ DEP_REWRITE_TAC [pat_bindings_evaluate_FOLDR_lemma]
\\ simp []
\\ conj_tac >- (
qexists_tac `{x | ~ (dec_name_to_num x < i)}`
\\ simp [SUBSET_DEF, MEM_FILTER, MEM_MAPi, PULL_EXISTS]
\\ simp [dec_enc]
\\ simp [evaluate_def]
\\ rw [IS_SOME_EXISTS]
\\ rename [`pmatch_stamps_ok _ _ _ cstmp _ con_vs`]
\\ qexists_tac `EL n con_vs`
\\ rw []
\\ qpat_x_assum `!env. _ ==> pure_eval_to _ _ x _` mp_tac
\\ DEP_REWRITE_TAC [COND_false]
\\ simp [PULL_EXISTS, NULL_FILTER, MEM_MAPi]
\\ asm_exists_tac \\ simp []
\\ simp [pure_eval_to_def]
\\ disch_then (fn t => DEP_REWRITE_TAC [t])
\\ simp [do_app_def]
\\ drule_then irule ALOOKUP_rel_mono
\\ simp []
)
\\ rw []
\\ fs [Q.ISPEC `Match m` EQ_SYM_EQ]
\\ last_x_assum irule
\\ simp [PULL_EXISTS, pmatch_list_append_Match_exists]
\\ goal_assum (first_assum o mp_then (Pat `pmatch_list _ _ _ _ = _`) mp_tac)
\\ goal_assum (first_assum o mp_then (Pat `pmatch_list _ _ _ _ = _`) mp_tac)
\\ goal_assum (first_assum o mp_then (Pat `evaluate _ _ _ = _`) mp_tac)
\\ simp []
\\ rpt (conj_tac
>- (
fs [pat_bindings_def, pats_bindings_FLAT_MAP, EVERY_FLAT, EVERY_REVERSE]
\\ fs [EVERY_EL, FORALL_PROD, UNCURRY, EL_MAP]
\\ rw []
\\ res_tac
\\ simp []
))
\\ DEP_REWRITE_TAC [ALOOKUP_rel_APPEND_L_false]
\\ simp [MAP_APPEND, REVERSE_APPEND, MEM_MAP, MEM_FILTER,
EVERY_MEM, MEM_MAPi, PULL_EXISTS, dec_enc]
\\ qpat_x_assum `!env. _ ==> pure_eval_to _ _ x _` (qspec_then `env` mp_tac)
\\ rpt strip_tac
>- (
rw []
\\ qpat_x_assum `_ ==> pure_eval_to _ _ x _` mp_tac
\\ DEP_REWRITE_TAC [COND_false]
\\ simp [ALOOKUP_rel_refl, pure_eval_to_def, NULL_FILTER]
\\ simp [MEM_MAPi, PULL_EXISTS]
\\ asm_exists_tac \\ simp []
\\ simp [evaluate_def, do_app_def]
\\ metis_tac [EVERY_EL]
)
\\ irule LIST_REL_APPEND_suff
\\ conj_tac
>- (
(* new elements *)
simp [LIST_REL_EL_EQN, LENGTH_enumerate, EL_enumerate, EL_MAP]
\\ simp [pure_eval_to_def, evaluate_def, option_case_eq]
\\ rw []
\\ drule_then (fn t => DEP_REWRITE_TAC [t]) ALOOKUP_rel_EQ_ALOOKUP
\\ simp [dec_enc]
\\ qpat_x_assum `_ ==> pure_eval_to _ _ x _` mp_tac
\\ DEP_REWRITE_TAC [COND_false]
\\ simp [NULL_FILTER, MEM_MAPi, PULL_EXISTS]
\\ fs [sptreeTheory.domain_lookup]
\\ asm_exists_tac \\ simp []
\\ rw [ALOOKUP_rel_refl, pure_eval_to_def]
\\ simp [do_app_def, option_case_eq]
\\ simp [ALOOKUP_APPEND, option_case_eq]
\\ DEP_REWRITE_TAC [GSYM MEM_ALOOKUP |> Q.SPEC `MAP f zs`]
\\ simp [MEM_MAP, EXISTS_PROD, MEM_FILTER, MEM_MAPi, enc_num_to_name_inj]
\\ simp [MAP_MAP_o, o_DEF, MAP_REVERSE]
\\ irule ALL_DISTINCT_MAP_INJ
\\ simp [MEM_FILTER, FORALL_PROD, MEM_MAPi, enc_num_to_name_inj]
\\ irule FILTER_ALL_DISTINCT
\\ simp [MAPi_enumerate_MAP]
\\ irule ALL_DISTINCT_MAP_INJ
\\ simp [FORALL_PROD, ALL_DISTINCT_enumerate]
)
(* prior env *)
\\ first_x_assum (fn t => mp_tac t \\ match_mp_tac LIST_REL_mono)
\\ simp [FORALL_PROD]
\\ rpt strip_tac
\\ first_x_assum irule
\\ simp []
\\ conj_tac \\ TRY (IF_CASES_TAC \\ simp [] \\ NO_TAC)
\\ rw [ALOOKUP_rel_def]
\\ drule_then (fn t => DEP_REWRITE_TAC [t]) ALOOKUP_rel_EQ_ALOOKUP
\\ simp [ALOOKUP_APPEND, option_case_eq, ALOOKUP_NONE, MEM_MAP, FORALL_PROD,
MEM_FILTER, MEM_MAPi]
\\ CCONTR_TAC \\ fs []
\\ fs [dec_enc]
)
>- (
(* Pref *)
qpat_x_assum `_ = Match _` mp_tac
\\ qmatch_goalsub_abbrev_tac `pmatch _ (Pref _) ref_v`
\\ Cases_on `ref_v` \\ simp [flatSemTheory.pmatch_def]
\\ rw [CaseEq "match_result", option_case_eq, CaseEq "store_v"]
\\ rpt (pairarg_tac \\ fs [])
\\ rveq \\ fs []
\\ qpat_x_assum `!env. _ ==> pure_eval_to _ _ x _` mp_tac
\\ disch_then (qspec_then `env` mp_tac)
\\ simp [evaluate_def, pure_eval_to_def, ALOOKUP_rel_refl]
\\ rw []
\\ fs [evaluate_def, do_app_def]
\\ last_x_assum match_mp_tac
\\ simp [CaseEq "match_result", PULL_EXISTS]
\\ rpt (CHANGED_TAC (asm_exists_tac \\ simp []))
\\ fs [libTheory.opt_bind_def, pat_bindings_def]
\\ simp [ALOOKUP_rel_cons_false, dec_enc]
\\ rpt conj_tac
>- (
rw [pure_eval_to_def, evaluate_def, option_case_eq]
\\ drule_then (fn t => DEP_REWRITE_TAC [t]) ALOOKUP_rel_EQ_ALOOKUP
\\ simp [dec_enc]
)
>- (
first_x_assum (fn t => mp_tac t \\ match_mp_tac LIST_REL_mono)
\\ simp [FORALL_PROD]
\\ rw []
\\ first_x_assum irule
\\ simp []
\\ rw [ALOOKUP_rel_def]
\\ drule_then (fn t => DEP_REWRITE_TAC [t]) ALOOKUP_rel_EQ_ALOOKUP
\\ simp [dec_enc, FILTER_FILTER]
\\ rw []
\\ fs [dec_enc]
)
>- (
fs [EVERY_MEM, FORALL_PROD]
\\ rw [] \\ res_tac \\ simp []
)
>- (
fs [store_lookup_def, Q.ISPEC `st.refs` EVERY_EL]
\\ first_x_assum drule
\\ simp []
)
)
QED
val s = ``s:'ffi flatSem$state``;
val s1 = mk_var ("s1", type_of s);
val s2 = mk_var ("s2", type_of s);
Definition prev_cfg_rel_def:
prev_cfg_rel past_cfg cur_cfg =
(?tm. subspt tm cur_cfg.type_map /\ past_cfg = cur_cfg with <| type_map := tm |>)
End
Theorem prev_cfg_rel_refl:
prev_cfg_rel cfg cfg
Proof
simp [prev_cfg_rel_def, config_component_equality]
QED
Theorem prev_cfg_rel_trans:
prev_cfg_rel cfg cfg' /\ prev_cfg_rel cfg' cfg'' ==> prev_cfg_rel cfg cfg''
Proof
rw [prev_cfg_rel_def]
\\ fs [config_component_equality]
\\ metis_tac [subspt_trans]
QED
val _ = IndDefLib.add_mono_thm ALOOKUP_rel_mono_rel;
Inductive v_rel:
(!v v'. simple_basic_val_rel v v' /\
LIST_REL (v_rel cfg) (v_container_xs v) (v_container_xs v') ==>
v_rel cfg v v') /\
(!N vs1 n x vs2 pcfg.
ALOOKUP_rel (\x. dec_name_to_num x < N) (v_rel cfg) vs1 vs2 /\
prev_cfg_rel pcfg cfg /\
FST (compile_exp pcfg x) < N ==>
v_rel cfg (Closure vs1 n x)
(Closure vs2 n (SND (SND (compile_exp pcfg x))))) /\
(!N vs1 fs x vs2.
ALOOKUP_rel (\x. dec_name_to_num x < N) (v_rel cfg) vs1 vs2 /\
prev_cfg_rel pcfg cfg /\
EVERY (\(n,m,e). FST (compile_exp pcfg e) < N) fs ==>
v_rel cfg (Recclosure vs1 fs x) (Recclosure vs2
(MAP (\(n,m,e). (n,m, SND (SND (compile_exp pcfg e)))) fs) x))
End
Theorem v_rel_l_cases = TypeBase.nchotomy_of ``: v``
|> concl |> dest_forall |> snd |> strip_disj
|> map (rhs o snd o strip_exists)
|> map (curry mk_comb ``v_rel cfg``)
|> map (fn t => mk_comb (t, ``v2 : v``))
|> map (SIMP_CONV (srw_ss ()) [Once v_rel_cases])
|> LIST_CONJ
val add_q = augment_srw_ss [simpLib.named_rewrites "pair_rel_thm"
[quotient_pairTheory.PAIR_REL_THM]];
Definition state_rel_def:
state_rel cfg (s:'ffi flatSem$state) (t:'ffi flatSem$state) <=>
t.clock = s.clock /\
LIST_REL (sv_rel (v_rel cfg)) s.refs t.refs /\
t.ffi = s.ffi /\
LIST_REL (OPTREL (v_rel cfg)) s.globals t.globals /\
t.c = s.c /\
s.check_ctor /\
t.check_ctor
End
Theorem state_rel_initial_state:
state_rel cfg (initial_state ffi k T) (initial_state ffi k T)
Proof
fs [state_rel_def, initial_state_def]
QED
Triviality state_rel_IMP_check_ctor:
state_rel cfg s t ==> s.check_ctor /\ t.check_ctor
Proof
fs [state_rel_def]
QED
Triviality state_rel_IMP_clock:
state_rel cfg s t ==> t.clock = s.clock
Proof
fs [state_rel_def]
QED
Triviality state_rel_IMP_c:
state_rel cfg s t ==> t.c = s.c
Proof
fs [state_rel_def]
QED
Overload nv_rel[local] =
``\cfg N. ALOOKUP_rel (\x. dec_name_to_num x < N) (v_rel cfg)``
Definition env_rel_def:
env_rel cfg N env1 env2 = nv_rel cfg N env1.v env2.v
End
val match_rel_def = Define `
(match_rel cfg N (Match env1) (Match env2) <=> nv_rel cfg N env1 env2) /\
(match_rel cfg N No_match No_match <=> T) /\
(match_rel cfg N Match_type_error Match_type_error <=> T) /\
(match_rel cfg N _ _ <=> F)`
Theorem match_rel_thms[simp]:
(match_rel cfg N Match_type_error e <=> e = Match_type_error) /\
(match_rel cfg N e Match_type_error <=> e = Match_type_error) /\
(match_rel cfg N No_match e <=> e = No_match) /\
(match_rel cfg N e No_match <=> e = No_match)
Proof
Cases_on `e` \\ rw [match_rel_def]
QED
Theorem MAX_ADD_LESS:
(MAX i j + k < l) = (i + k < l /\ j + k < l)
Proof
rw [MAX_DEF]
QED
Theorem LESS_MAX_ADD:
(l < MAX i j + k) = (l < i + k \/ l < j + k)
Proof
rw [MAX_DEF]
QED
Theorem MAX_ADD_LE:
(MAX i j + k <= l) = (i + k <= l /\ j + k <= l)
Proof
rw [MAX_DEF]
QED
Theorem env_rel_mono:
env_rel cfg i env1 env2 /\ j <= i ==>
env_rel cfg j env1 env2
Proof
rw [env_rel_def]
\\ drule_then irule ALOOKUP_rel_mono
\\ simp [FORALL_PROD]
QED
Theorem env_rel_ALOOKUP:
env_rel cfg N env1 env2 /\ dec_name_to_num n < N ==>
OPTREL (v_rel cfg) (ALOOKUP env1.v n) (ALOOKUP env2.v n)
Proof
rw [env_rel_def, ALOOKUP_rel_def]
QED
Theorem ALOOKUP_MAP_3:
(!x. MEM x xs ==> FST (f x) = FST x) ==>
ALOOKUP (MAP f xs) x = OPTION_MAP (\y. SND (f (x, y))) (ALOOKUP xs x)
Proof
Induct_on `xs` \\ rw []
\\ fs [DISJ_IMP_THM, FORALL_AND_THM]
\\ Cases_on `f h`
\\ Cases_on `h`
\\ rw []
\\ fs []
QED
Theorem ALOOKUP_rel_MAP_same:
(!x. MEM x xs ==> FST (f x) = FST (g x) /\
(P (FST (g x)) ==> R (SND (f x)) (SND (g x)))) ==>
ALOOKUP_rel P R (MAP f xs) (MAP g xs)
Proof
Induct_on `xs` \\ rw [ALOOKUP_rel_empty]
\\ fs [DISJ_IMP_THM, FORALL_AND_THM]
\\ Cases_on `f h` \\ Cases_on `g h`
\\ fs [ALOOKUP_rel_cons]
QED
Theorem do_opapp_thm:
do_opapp vs1 = SOME (nvs1, exp) /\ LIST_REL (v_rel cfg) vs1 vs2
==>
?i sg exp' nvs2 prev_cfg. compile_exp prev_cfg exp = (i, sg, exp') /\
prev_cfg_rel prev_cfg cfg /\
nv_rel cfg (i + 1) nvs1 nvs2 /\ do_opapp vs2 = SOME (nvs2, exp')
Proof
simp [do_opapp_def, pair_case_eq, case_eq_thms, PULL_EXISTS]
\\ rw []
\\ fs [v_rel_l_cases]
\\ rveq \\ fs []
\\ simp [PAIR_FST_SND_EQ]
\\ goal_assum (first_assum o mp_then (Pat `prev_cfg_reg _ _`) mp_tac)
>- (
simp [LENGTH_SND_compile_exps]
\\ irule ALOOKUP_rel_cons
\\ simp []
\\ drule_then irule ALOOKUP_rel_mono
\\ simp []
)
\\ fs [PULL_EXISTS, find_recfun_ALOOKUP, ALOOKUP_MAP]
\\ simp [ALOOKUP_MAP_3, FORALL_PROD, LENGTH_SND_compile_exps]
\\ simp [MAP_MAP_o, o_DEF, UNCURRY, Q.ISPEC `FST` ETA_THM]
\\ irule ALOOKUP_rel_cons
\\ simp [build_rec_env_eq_MAP]
\\ irule ALOOKUP_rel_append_suff
\\ simp [MAP_MAP_o, o_DEF, UNCURRY]
\\ conj_tac
>- (
irule ALOOKUP_rel_MAP_same
\\ rw [UNCURRY, v_rel_l_cases]
\\ metis_tac []
)
\\ drule_then irule ALOOKUP_rel_mono
\\ simp []
\\ imp_res_tac ALOOKUP_MEM
\\ fs [EVERY_MEM]
\\ res_tac
\\ fs []
QED
Theorem do_opapp_thm_REVERSE:
do_opapp (REVERSE vs1) = SOME (nvs1, exp) /\ LIST_REL (v_rel cfg) vs1 vs2
==>
?i sg exp' nvs2 prev_cfg.
compile_exp prev_cfg exp = (i, sg, exp') /\
prev_cfg_rel prev_cfg cfg /\
nv_rel cfg (i + 1) nvs1 nvs2 /\
do_opapp (REVERSE vs2) = SOME (nvs2, exp')
Proof
rw []
\\ drule_then irule do_opapp_thm
\\ simp []
QED
Theorem pmatch_thm:
(!(s:'ffi state) p v vs r s1 v1 vs1.
pmatch s p v vs = r /\
r <> Match_type_error /\
state_rel cfg s s1 /\
v_rel cfg v v1 /\
nv_rel cfg N vs vs1
==> ?r1. pmatch s1 p v1 vs1 = r1 /\ match_rel cfg N r r1) /\
(!(s:'ffi state) ps v vs r s1 v1 vs1.
pmatch_list s ps v vs = r /\
r <> Match_type_error /\
state_rel cfg s s1 ∧
LIST_REL (v_rel cfg) v v1 /\
nv_rel cfg N vs vs1
==> ?r1. pmatch_list s1 ps v1 vs1 = r1 /\ match_rel cfg N r r1)
Proof
ho_match_mp_tac flatSemTheory.pmatch_ind
\\ simp [flatSemTheory.pmatch_def, match_rel_def, v_rel_l_cases]
\\ rw [match_rel_def]
\\ imp_res_tac state_rel_IMP_check_ctor
\\ imp_res_tac state_rel_IMP_c
\\ fs [flatSemTheory.pmatch_def, pmatch_stamps_ok_OPTREL]
\\ rfs []
\\ imp_res_tac LIST_REL_LENGTH \\ fs []
>- ( irule ALOOKUP_rel_cons \\ simp [] )
>- (
fs [store_lookup_def, bool_case_eq, option_case_eq]
\\ every_case_tac \\ rfs []
\\ rpt (first_x_assum drule)
\\ fs [state_rel_def, LIST_REL_EL_EQN]
\\ rfs []
\\ rpt (first_x_assum drule)
\\ simp []
)
>- (
every_case_tac \\ fs []
\\ rpt (first_x_assum drule \\ rw [])
\\ TRY (rpt (asm_exists_tac \\ simp []) \\ NO_TAC)
\\ fs [match_rel_def]
)
QED
Theorem simple_val_rel_step_isClosure:
simple_basic_val_rel x y ==> ~ isClosure x /\ ~ isClosure y
Proof
Cases_on `x` \\ simp [simple_basic_val_rel_def]
\\ rw [] \\ simp []
QED
Theorem simple_val_rel:
simple_val_rel (v_rel cfg)
Proof
simp [simple_val_rel_def, v_rel_cases]
\\ rw [] \\ simp []
\\ EQ_TAC \\ rw [] \\ fs []
\\ metis_tac [simple_val_rel_step_isClosure]
QED
Theorem simple_state_rel:
simple_state_rel (v_rel cfg) (state_rel cfg)
Proof
simp [simple_state_rel_def, state_rel_def]
QED
Theorem do_app_thm = MATCH_MP simple_do_app_thm
(CONJ simple_val_rel simple_state_rel)
Theorem do_app_thm_REVERSE:
do_app cc s1 op (REVERSE vs1) = SOME (t1, r1) /\
state_rel cfg s1 s2 /\ LIST_REL (v_rel cfg) vs1 vs2
==>
?t2 r2. result_rel (v_rel cfg) (v_rel cfg) r1 r2 /\
state_rel cfg t1 t2 /\ do_app cc s2 op (REVERSE vs2) = SOME (t2, r2)
Proof
rw []
\\ drule_then irule do_app_thm
\\ simp []
QED
Theorem do_if_helper:
do_if b x y = SOME e /\ v_rel cfg b b' ==>
((b' = Boolv T) = (b = Boolv T)) /\ ((b' = Boolv F) = (b = Boolv F))
Proof
simp [Once v_rel_cases]
\\ Cases_on `b`
\\ rw [Boolv_def]
\\ EQ_TAC \\ rw [] \\ fs []
QED
Theorem list_max_LESS_EVERY:
(list_max xs < N) = (0 < N /\ EVERY (\x. x < N) xs)
Proof
Induct_on `xs`
\\ simp [list_max_def |> REWRITE_RULE [GSYM MAX_DEF]]
\\ metis_tac []
QED
Theorem max_dec_name_LESS_EVERY:
(max_dec_name ns < N) = (0 < N /\ EVERY (\n. dec_name_to_num n < N) ns)
Proof
Induct_on `ns` \\ simp [max_dec_name_def]
\\ metis_tac []
QED
Definition encode_val_def:
encode_val (Litv l) = Litv l /\
encode_val (Conv stmp xs) = Term
(case stmp of NONE => NONE | SOME (i, _) => SOME i)
(MAP encode_val xs) /\
encode_val (Loc n) = RefPtr n /\
encode_val others = Other
Termination
WF_REL_TAC `measure v_size`
\\ rw []
\\ fs [MEM_SPLIT, SUM_APPEND, v3_size]
End
Theorem decode_test_simulation:
dt_test test enc_v = SOME b /\
pure_eval_to s env x v /\
enc_v = encode_val v
==>
pure_eval_to s env (decode_test tr test x) (Boolv b)
Proof
Cases_on `test` \\ Cases_on `v`
\\ simp [encode_val_def]
\\ EVERY_CASE_TAC
\\ rw []
\\ fs [dt_test_def]
\\ fs [decode_test_def, pure_eval_to_def, evaluate_def]
\\ simp [do_app_def, do_eq_def, lit_same_type_sym]
\\ rw [Boolv_def]
QED
Theorem app_pos_Term_IMP:
!xs n. app_pos refs (Pos n pos) (Term c xs) = SOME y ==>
n < LENGTH xs /\ app_pos refs pos (EL n xs) = SOME y
Proof
Induct_on `xs`
\\ simp [app_pos_def]
\\ rw []
\\ Cases_on `n`
\\ fs [app_pos_def]
QED