forked from CakeML/cakeml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfScript.sml
3557 lines (3331 loc) · 135 KB
/
cfScript.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
(*
Defines the characteristic formula (CF) function cf_def and proves
that it is sound w.r.t. the evaluate semantics of CakeML.
*)
open preamble
open set_sepTheory helperLib ml_translatorTheory ConseqConv
open ml_translatorTheory semanticPrimitivesTheory fpSemTheory
open cfHeapsBaseTheory cfHeapsTheory cfHeapsBaseLib cfStoreTheory
open cfNormaliseTheory cfAppTheory
open cfTacticsBaseLib evaluateTheory
val _ = temp_delsimps ["lift_disj_eq", "lift_imp_disj"]
val _ = new_theory "cf"
val _ = set_grammar_ancestry
["cfHeapsBase","cfHeaps","cfStore","cfNormalise","cfApp",
"ml_translator", "ffi"];
val _ = monadsyntax.temp_disable_monadsyntax()
(*------------------------------------------------------------------*)
(* Characteristic formula for not-implemented or impossible cases *)
val cf_bottom_def = Define `
cf_bottom = \env. local (\H Q. F)`
(*------------------------------------------------------------------*)
(* Machinery for dealing with n-ary applications/functions in cf.
Applications and ((mutually)recursive)functions are unary in
CakeML's AST.
Some machinery is therefore needed to walk down the AST and extract
multiple arguments (for an application), or multiple parameters and
the final function body (for functions).
*)
(** 1) n-ary applications *)
val dest_opapp_not_empty_arglist = Q.prove (
`!e f args. dest_opapp e = SOME (f, args) ==> args <> []`,
Cases \\ fs [dest_opapp_def] \\ rename1 `App op _` \\
Cases_on `op` \\ fs [dest_opapp_def] \\ every_case_tac \\
fs []
)
(** 2) n-ary single non-recursive functions *)
(* An auxiliary definition *)
val is_bound_Fun_def = Define `
is_bound_Fun (SOME _) (Fun _ _) = T /\
is_bound_Fun _ _ = F`
(* [Fun_body]: walk down successive lambdas and return the inner
function body *)
val Fun_body_def = Define `
Fun_body (Fun _ body) =
(case Fun_body body of
| NONE => SOME body
| SOME body' => SOME body') /\
Fun_body _ = NONE`
val Fun_body_exp_size = Q.prove (
`!e e'. Fun_body e = SOME e' ==> exp_size e' < exp_size e`,
Induct \\ fs [Fun_body_def] \\ every_case_tac \\
fs [astTheory.exp_size_def]
)
val is_bound_Fun_unfold = Q.prove (
`!opt e. is_bound_Fun opt e ==> (?n body. e = Fun n body)`,
Cases_on `e` \\ fs [is_bound_Fun_def]
)
(* [Fun_params]: walk down successive lambdas and return the list of
parameters *)
val Fun_params_def = Define `
Fun_params (Fun n body) =
n :: (Fun_params body) /\
Fun_params _ =
[]`
val Fun_params_Fun_body_NONE = Q.prove (
`!e. Fun_body e = NONE ==> Fun_params e = []`,
Cases \\ fs [Fun_body_def, Fun_params_def] \\ every_case_tac
)
(* [naryFun]: build the AST for a n-ary function *)
val naryFun_def = Define `
naryFun [] body = body /\
naryFun (n::ns) body = Fun n (naryFun ns body)`
val Fun_params_Fun_body_repack = Q.prove (
`!e e'.
Fun_body e = SOME e' ==>
naryFun (Fun_params e) e' = e`,
Induct \\ fs [Fun_body_def, Fun_params_def] \\ every_case_tac \\
rpt strip_tac \\ fs [Once naryFun_def] \\ TRY every_case_tac \\
TRY (fs [Fun_params_Fun_body_NONE, Once naryFun_def] \\ NO_TAC) \\
once_rewrite_tac [naryFun_def] \\ fs []
)
(** 3) n-ary (mutually)recursive functions *)
(** Mutually recursive definitions (and closures) use a list of tuples
(function name, parameter name, body). [letrec_pull_params] returns the
"n-ary" version of this list, that is, a list of tuples:
(function name, parameters names, inner body)
*)
val letrec_pull_params_def = Define `
letrec_pull_params [] = [] /\
letrec_pull_params ((f, n, body)::funs) =
(case Fun_body body of
| NONE => (f, [n], body)
| SOME body' => (f, n::Fun_params body, body')) ::
(letrec_pull_params funs)`
Theorem letrec_pull_params_names:
!funs P.
MAP (\ (f,_,_). P f) (letrec_pull_params funs) =
MAP (\ (f,_,_). P f) funs
Proof
Induct \\ fs [letrec_pull_params_def] \\ rpt strip_tac \\
rename1 `ftuple::funs` \\ PairCases_on `ftuple` \\
fs [letrec_pull_params_def] \\ every_case_tac \\ fs []
QED
Theorem letrec_pull_params_LENGTH:
!funs. LENGTH (letrec_pull_params funs) = LENGTH funs
Proof
Induct \\ fs [letrec_pull_params_def] \\ rpt strip_tac \\
rename1 `ftuple::funs` \\ PairCases_on `ftuple` \\
fs [letrec_pull_params_def] \\ every_case_tac \\ fs []
QED
Theorem letrec_pull_params_append:
!l l'.
letrec_pull_params (l ++ l') =
letrec_pull_params l ++ letrec_pull_params l'
Proof
Induct \\ rpt strip_tac \\ fs [letrec_pull_params_def] \\
rename1 `ftuple::_` \\ PairCases_on `ftuple` \\ rename1 `(f,n,body)` \\
fs [letrec_pull_params_def]
QED
Theorem letrec_pull_params_cancel:
!funs.
MAP (\ (f,ns,body). (f, HD ns, naryFun (TL ns) body))
(letrec_pull_params funs) =
funs
Proof
Induct \\ rpt strip_tac \\ fs [letrec_pull_params_def] \\
rename1 `ftuple::_` \\ PairCases_on `ftuple` \\ rename1 `(f,n,body)` \\
fs [letrec_pull_params_def] \\ every_case_tac \\ fs [naryFun_def] \\
fs [Fun_params_Fun_body_repack]
QED
Theorem letrec_pull_params_nonnil_params:
!funs f ns body.
MEM (f,ns,body) (letrec_pull_params funs) ==>
ns <> []
Proof
Induct \\ rpt strip_tac \\ fs [letrec_pull_params_def, MEM] \\
rename1 `ftuple::funs` \\ PairCases_on `ftuple` \\
rename1 `(f',n',body')::funs` \\
fs [letrec_pull_params_def] \\ every_case_tac \\ fs [naryFun_def] \\
metis_tac []
QED
Theorem find_recfun_letrec_pull_params:
!funs f n ns body.
find_recfun f (letrec_pull_params funs) = SOME (n::ns, body) ==>
find_recfun f funs = SOME (n, naryFun ns body)
Proof
Induct \\ fs [letrec_pull_params_def]
THEN1 (fs [Once find_recfun_def]) \\
rpt strip_tac \\ rename1 `ftuple::funs` \\ PairCases_on `ftuple` \\
rename1 `(f',n',body')` \\ fs [letrec_pull_params_def] \\
every_case_tac \\ pop_assum mp_tac \\
once_rewrite_tac [find_recfun_def] \\ fs [] \\
every_case_tac \\ rw [] \\ fs [naryFun_def, Fun_params_Fun_body_repack]
QED
(*------------------------------------------------------------------*)
(* The semantic counterpart of n-ary functions: n-ary closures
Also, dealing with environments.
*)
(* [naryClosure] *)
val naryClosure_def = Define `
naryClosure env (n::ns) body = Closure env n (naryFun ns body)`
(* Properties of [naryClosure] *)
val with_clock_clock = Q.prove(
`(s with clock := k).clock = k`,
EVAL_TAC);
val with_clock_with_clock = Q.prove(
`((s with clock := k1) with clock := k2) = s with clock := k2`,
EVAL_TAC);
val with_clock_self = Q.prove(
`(s with clock := s.clock) = s`,
fs [state_component_equality]);
val with_clock_self_eq = Q.prove(
`!ck. (s with clock := ck) = s <=> ck = s.clock`,
fs [state_component_equality]);
val evaluate_to_heap_with_clock = prove(
``evaluate_to_heap (st with clock := ck) = evaluate_to_heap st``,
fs [evaluate_to_heap_def,FUN_EQ_THM,evaluate_ck_def]);
Theorem app_one_naryClosure:
!env n ns x xs body H Q.
ns <> [] ==> xs <> [] ==>
app (p:'ffi ffi_proj) (naryClosure env (n::ns) body) (x::xs) H Q ==>
app (p:'ffi ffi_proj) (naryClosure (env with v := nsBind n x env.v) ns body) xs H Q
Proof
rpt strip_tac \\ Cases_on `ns` \\ Cases_on `xs` \\ fs [] \\
rename1 `app _ (naryClosure _ (n::n'::ns) _) (x::x'::xs) _ _` \\
Cases_on `xs` THENL [all_tac, rename1 `_::_::x''::xs`] \\
fs [app_def, naryClosure_def, naryFun_def] \\
fs [app_basic_def] \\ rpt strip_tac \\ first_x_assum progress \\
Cases_on `r` \\ fs [POSTv_def, cond_def] \\
pop_assum mp_tac \\
simp [Once evaluate_to_heap_def] \\
strip_tac \\ rveq \\ fs [] \\
fs [SEP_EXISTS, cond_def, STAR_def, SPLIT_emp2, PULL_EXISTS] \\
qpat_x_assum `do_opapp _ = _` (assume_tac o REWRITE_RULE [do_opapp_def]) \\
fs [] \\ qpat_x_assum `Fun _ _ = _` (assume_tac o GSYM) \\ fs [] \\
fs [evaluate_ck_def, evaluate_def] \\ rw [] \\
fs [do_opapp_def] \\
progress SPLIT_of_SPLIT3_2u3 \\ first_x_assum progress \\
once_rewrite_tac [CONJ_COMM] \\
asm_exists_tac \\ fs [evaluate_to_heap_with_clock] \\
asm_exists_tac \\ fs [evaluate_to_heap_with_clock] \\
rename1 `SPLIT3 heap1 (h_f', h_k UNION h_g, h_g')` \\
`SPLIT3 heap1 (h_f', h_k, h_g UNION h_g')`
by SPLIT_TAC \\
asm_exists_tac \\ fs []
QED
Theorem curried_naryClosure:
!env len ns body.
ns <> [] ==> len = LENGTH ns ==>
curried (p:'ffi ffi_proj) len (naryClosure env ns body)
Proof
Induct_on `ns` \\ fs [naryClosure_def, naryFun_def] \\ Cases_on `ns`
THEN1 (once_rewrite_tac [ONE] \\ fs [Once curried_def]) \\
rpt strip_tac \\ fs [naryClosure_def, naryFun_def] \\
rw [Once curried_def] \\ fs [app_basic_def] \\ rpt strip_tac \\
fs [emp_def, cond_def, do_opapp_def, POSTv_def,evaluate_to_heap_def] \\
rename1 `SPLIT (st2heap _ st) ({}, h_k)` \\
`SPLIT3 (st2heap (p:'ffi ffi_proj) st) ({}, h_k, {})` by SPLIT_TAC \\
instantiate \\ rename1 `nsBind n x _` \\
first_x_assum (qspecl_then [`env with v := nsBind n x env.v`, `body`]
assume_tac) \\
qmatch_assum_abbrev_tac `curried _ _ v` \\ qexists_tac `Val v` \\
qunabbrev_tac `v` \\ fs [] \\
fs [evaluate_ck_def, evaluate_def] \\
fs [LENGTH_CONS, PULL_EXISTS] \\
qexists_tac `st.clock` \\ fs [with_clock_self] \\
fs [GSYM naryFun_def, GSYM naryClosure_def] \\ rpt strip_tac \\
irule app_one_naryClosure \\ fs []
QED
(* [naryRecclosure] *)
val naryRecclosure_def = Define `
naryRecclosure env naryfuns f =
Recclosure env
(MAP (\ (f, ns, body). (f, HD ns, naryFun (TL ns) body)) naryfuns)
f`;
(* Properties of [naryRecclosure] *)
Theorem do_opapp_naryRecclosure:
!funs f n ns body x env env' exp.
find_recfun f (letrec_pull_params funs) = SOME (n::ns, body) ==>
(do_opapp [naryRecclosure env (letrec_pull_params funs) f; x] =
SOME (env', exp)
<=>
(ALL_DISTINCT (MAP (\ (f,_,_). f) funs) /\
env' = (env with v := nsBind n x (build_rec_env funs env env.v)) /\
exp = naryFun ns body))
Proof
rpt strip_tac \\ progress find_recfun_letrec_pull_params \\
fs [naryRecclosure_def, do_opapp_def, letrec_pull_params_cancel] \\
eq_tac \\ every_case_tac \\ fs []
QED
Theorem app_one_naryRecclosure:
!funs f n ns body x xs env H Q.
ns <> [] ==> xs <> [] ==>
find_recfun f (letrec_pull_params funs) = SOME (n::ns, body) ==>
(app (p:'ffi ffi_proj) (naryRecclosure env (letrec_pull_params funs) f) (x::xs) H Q ==>
app (p:'ffi ffi_proj)
(naryClosure
(env with v := nsBind n x (build_rec_env funs env env.v))
ns body) xs H Q)
Proof
rpt strip_tac \\ Cases_on `ns` \\ Cases_on `xs` \\ fs [] \\
rename1 `SOME (n::n'::ns, _)` \\ rename1 `app _ _ (x::x'::xs)` \\
Cases_on `xs` THENL [all_tac, rename1 `_::_::x''::xs`] \\
fs [app_def, naryClosure_def, naryFun_def] \\
fs [app_basic_def] \\
rpt strip_tac \\ first_x_assum progress \\
Cases_on `r` \\ fs [POSTv_def, SEP_EXISTS, cond_def, STAR_def, SPLIT_emp2] \\
pop_assum mp_tac \\
simp [Once evaluate_to_heap_def] \\ rw [] \\ rveq \\ fs [] \\
progress_then (fs o sing) do_opapp_naryRecclosure \\ rw [] \\
fs [naryFun_def, evaluate_ck_def, evaluate_def] \\ rw [] \\
fs [do_opapp_def] \\ progress SPLIT_of_SPLIT3_2u3 \\ first_x_assum progress \\
fs [evaluate_to_heap_with_clock] \\
once_rewrite_tac [CONJ_COMM] \\
asm_exists_tac \\ fs [evaluate_to_heap_with_clock] \\
asm_exists_tac \\ fs [evaluate_to_heap_with_clock] \\
rename1 `SPLIT3 heap1 (h_f', h_k UNION h_g, h_g')` \\
`SPLIT3 heap1 (h_f', h_k, h_g UNION h_g')`
by SPLIT_TAC \\
asm_exists_tac \\ fs []
QED
Theorem curried_naryRecclosure:
!env funs f len ns body.
ALL_DISTINCT (MAP (\ (f,_,_). f) funs) ==>
find_recfun f (letrec_pull_params funs) = SOME (ns, body) ==>
len = LENGTH ns ==>
curried (p:'ffi ffi_proj) len (naryRecclosure env (letrec_pull_params funs) f)
Proof
rpt strip_tac \\ Cases_on `ns` \\ fs []
THEN1 (
fs [curried_def, semanticPrimitivesPropsTheory.find_recfun_ALOOKUP] \\
progress ALOOKUP_MEM \\ progress letrec_pull_params_nonnil_params \\ fs []
) \\
rename1 `n::ns` \\ Cases_on `ns` \\ fs [Once curried_def] \\
rename1 `n::n'::ns` \\ strip_tac \\ fs [app_basic_def] \\ rpt strip_tac \\
fs [POSTv_def, emp_def, cond_def, evaluate_to_heap_def] \\
progress_then (fs o sing) do_opapp_naryRecclosure \\ rw [] \\
qexists_tac
`Val (naryClosure (env with v := nsBind n x (build_rec_env funs env env.v))
(n'::ns) body)` \\
simp [PULL_EXISTS] \\
Q.LIST_EXISTS_TAC [`{}`, `st.clock`, `st`] \\ simp [] \\ rpt strip_tac
THEN1 SPLIT_TAC
THEN1 (irule curried_naryClosure \\ fs [])
THEN1 (irule app_one_naryRecclosure \\ fs [LENGTH_CONS] \\ metis_tac []) \\
fs [naryFun_def, naryClosure_def] \\
fs [evaluate_ck_def, evaluate_def, with_clock_self]
QED
Theorem letrec_pull_params_repack:
!funs f env.
naryRecclosure env (letrec_pull_params funs) f =
Recclosure env funs f
Proof
Induct \\ rpt strip_tac \\ fs [naryRecclosure_def, letrec_pull_params_def] \\
rename1 `ftuple::_` \\ PairCases_on `ftuple` \\ rename1 `(f,n,body)` \\
fs [letrec_pull_params_def] \\ every_case_tac \\ fs [naryFun_def] \\
fs [Fun_params_Fun_body_repack]
QED
(** Extending environments *)
(* TODO: there's a version of this already defined ...*)
(* [extend_env] *)
val extend_env_v_def = Define `
extend_env_v [] [] env_v = env_v /\
extend_env_v (n::ns) (xv::xvs) env_v =
extend_env_v ns xvs (nsBind n xv env_v)`;
val extend_env_def = Define `
extend_env ns xvs (env:'v sem_env) = (env with v := extend_env_v ns xvs env.v)`;
Theorem extend_env_v_rcons:
!ns xvs n xv env_v.
LENGTH ns = LENGTH xvs ==>
extend_env_v (ns ++ [n]) (xvs ++ [xv]) env_v =
nsBind n xv (extend_env_v ns xvs env_v)
Proof
Induct \\ rpt strip_tac \\ first_assum (assume_tac o GSYM) \\
fs [LENGTH_NIL, LENGTH_CONS, extend_env_v_def]
QED
Theorem extend_env_v_zip:
!ns xvs env_v.
LENGTH ns = LENGTH xvs ==>
extend_env_v ns xvs env_v = nsAppend (alist_to_ns (ZIP (REVERSE ns, REVERSE xvs))) env_v
Proof
Induct \\ rpt strip_tac \\ first_assum (assume_tac o GSYM) \\
fs [LENGTH_NIL, LENGTH_CONS, extend_env_v_def, GSYM ZIP_APPEND] \\
FULL_SIMP_TAC std_ss [Once (GSYM namespacePropsTheory.nsAppend_alist_to_ns),Once (GSYM (namespacePropsTheory.nsAppend_assoc))] \\
Cases_on`env_v`>> EVAL_TAC
QED
(* [build_rec_env_aux] *)
val build_rec_env_aux_def = Define `
build_rec_env_aux funs (fs: (tvarN, tvarN # exp) alist) env add_to_env =
FOLDR
(\ (f,x,e) env'. nsBind f (Recclosure env funs f) env')
add_to_env
fs`;
val build_rec_env_zip_aux = Q.prove (
`!(fs: (tvarN, tvarN # exp) alist) funs env env_v.
nsAppend
(alist_to_ns
(ZIP (MAP (\ (f,_,_). f) fs,
MAP (\ (f,_,_). naryRecclosure env (letrec_pull_params funs) f) fs)))
env_v =
FOLDR (\ (f,x,e) env'. nsBind f (Recclosure env funs f) env') env_v fs`,
Induct \\ rpt strip_tac THEN1 (fs []) \\
rename1 `ftuple::fs` \\ PairCases_on `ftuple` \\ rename1 `(f,n,body)` \\
fs [letrec_pull_params_repack]
);
Theorem build_rec_env_zip:
!funs env env_v.
nsAppend
(alist_to_ns
(ZIP (MAP (\ (f,_,_). f) funs,
MAP (\ (f,_,_). naryRecclosure env (letrec_pull_params funs) f) funs)))
env_v =
build_rec_env funs env env_v
Proof
fs [build_rec_env_def, build_rec_env_zip_aux]
QED
(* [extend_env_rec] *)
val extend_env_v_rec_def = Define `
extend_env_v_rec rec_ns rec_xvs ns xvs env_v =
extend_env_v ns xvs (nsAppend (alist_to_ns (ZIP (rec_ns, rec_xvs))) env_v)`;
val extend_env_rec_def = Define `
extend_env_rec rec_ns rec_xvs ns xvs (env:'v sem_env) =
env with v := extend_env_v_rec rec_ns rec_xvs ns xvs env.v`;
Theorem extend_env_rec_build_rec_env:
!funs env env_v.
extend_env_v_rec
(MAP (\ (f,_,_). f) funs)
(MAP (\ (f,_,_). naryRecclosure env (letrec_pull_params funs) f) funs)
[] [] env_v =
build_rec_env funs env env_v
Proof
rpt strip_tac \\ fs [extend_env_v_rec_def, extend_env_v_def, build_rec_env_zip]
QED
(*------------------------------------------------------------------*)
(** Pattern matching.
Restrictions:
- the CakeML pattern-matching must "typecheck" (does not raise
Match_type_error)
- patterns that use [Pref] are not currently supported
*)
val pat_bindings_def = astTheory.pat_bindings_def
val pmatch_def = pmatch_def
val pmatch_ind = pmatch_ind
(* [pat_wildcards]: computes the number of wildcards a pattern contains. *)
val pat_wildcards_def = tDefine "pat_wildcards" `
pat_wildcards Pany = 1n /\
pat_wildcards (Pvar _) = 0n /\
pat_wildcards (Plit _) = 0n /\
pat_wildcards (Pref _) = 0n /\
pat_wildcards (Pcon _ args) = pats_wildcards args /\
pat_wildcards (Ptannot p _) = pat_wildcards p /\
pat_wildcards (Pas p _) = pat_wildcards p /\
pats_wildcards [] = 0n /\
pats_wildcards (p::ps) =
(pat_wildcards p) + (pats_wildcards ps)`
(WF_REL_TAC `measure pat_size`)
(* [v_of_pat]: from a pattern and a list of instantiations for the pattern
variables, produce a semantic value
*)
val v_of_matching_pat_aux_measure_def = Define `
v_of_matching_pat_aux_measure x =
sum_CASE x
(\ (_,p,_,_). pat_size p)
(\ (_,ps,_,_). list_size pat_size ps)`;
val v_of_pat_def = tDefine "v_of_pat" `
v_of_pat envC (Pvar x) insts wildcards =
(case insts of
xv::rest => SOME (xv, rest, wildcards)
| _ => NONE) /\
v_of_pat envC (Pas x p) insts wildcards =
NONE (* unsupported *) /\
v_of_pat envC Pany insts wildcards =
(case wildcards of
xv::rest => SOME (xv, insts, rest)
| _ => NONE) /\
v_of_pat envC (Plit l) insts wildcards =
SOME (Litv l, insts, wildcards) /\
v_of_pat envC (Pcon c args) insts wildcards =
(case v_of_pat_list envC args insts wildcards of
SOME (argsv, insts_rest, wildcards_rest) =>
(case c of
NONE => SOME (Conv NONE argsv, insts_rest, wildcards_rest)
| SOME id =>
case nsLookup envC id of
NONE => NONE
| SOME (len, t) =>
if len = LENGTH argsv then
SOME (Conv (SOME t) argsv, insts_rest, wildcards_rest)
else NONE)
| NONE => NONE) /\
v_of_pat _ (Pref pat) _ _ =
NONE (* unsupported *) /\
v_of_pat envC (Ptannot p _) insts wildcards =
v_of_pat envC p insts wildcards /\
v_of_pat _ _ _ _ = NONE /\
v_of_pat_list _ [] insts wildcards = SOME ([], insts, wildcards) /\
v_of_pat_list envC (p::argspat) insts wildcards =
(case v_of_pat envC p insts wildcards of
SOME (v, insts_rest, wildcards_rest) =>
(case v_of_pat_list envC argspat insts_rest wildcards_rest of
SOME (vs, insts_rest', wildcards_rest') =>
SOME (v::vs, insts_rest', wildcards_rest')
| NONE => NONE)
| NONE => NONE) /\
v_of_pat_list _ _ _ _ = NONE`
(WF_REL_TAC `measure v_of_matching_pat_aux_measure` \\
fs [v_of_matching_pat_aux_measure_def, list_size_def,
astTheory.pat_size_def] \\
gen_tac \\ Induct \\ fs [list_size_def, astTheory.pat_size_def]);
val v_of_pat_ind = fetch "-" "v_of_pat_ind";
Theorem v_of_pat_list_length:
!envC pats insts wildcards vs rest.
v_of_pat_list envC pats insts wildcards = SOME (vs, rest, wrest) ==>
LENGTH pats = LENGTH vs
Proof
Induct_on `pats` \\ fs [v_of_pat_def] \\ rpt strip_tac \\
every_case_tac \\ fs [] \\ rw [] \\ first_assum irule \\ instantiate
QED
Theorem v_of_pat_insts_length:
(!envC pat insts wildcards v insts_rest wildcards_rest.
v_of_pat envC pat insts wildcards = SOME (v, insts_rest, wildcards_rest) ==>
(LENGTH insts = LENGTH (pat_bindings pat []) + LENGTH insts_rest)) /\
(!envC pats insts wildcards vs insts_rest wildcards_rest.
v_of_pat_list envC pats insts wildcards = SOME (vs, insts_rest, wildcards_rest) ==>
(LENGTH insts = LENGTH (pats_bindings pats []) + LENGTH insts_rest))
Proof
HO_MATCH_MP_TAC v_of_pat_ind \\ rpt strip_tac \\
fs [v_of_pat_def, pat_bindings_def, LENGTH_NIL] \\ rw []
THEN1 (every_case_tac \\ fs [LENGTH_NIL])
THEN1 (every_case_tac \\ fs [])
THEN1 (
(* v_of_pat _ (Pcon _ _) _ _ *)
every_case_tac \\ fs [] \\ rw [] \\
once_rewrite_tac [semanticPrimitivesPropsTheory.pat_bindings_accum] \\ fs []
)
THEN1 (
(* v_of_pat_list _ (_::_) _ _ *)
every_case_tac \\ fs [] \\ rw [] \\
once_rewrite_tac [semanticPrimitivesPropsTheory.pat_bindings_accum] \\ fs []
)
QED
Theorem v_of_pat_wildcards_count:
(!envC pat insts wildcards v insts_rest wildcards_rest.
v_of_pat envC pat insts wildcards = SOME (v, insts_rest, wildcards_rest) ==>
(LENGTH wildcards = pat_wildcards pat + LENGTH wildcards_rest)) /\
(!envC pats insts wildcards vs insts_rest wildcards_rest.
v_of_pat_list envC pats insts wildcards = SOME (vs, insts_rest, wildcards_rest) ==>
(LENGTH wildcards = pats_wildcards pats + LENGTH wildcards_rest))
Proof
HO_MATCH_MP_TAC v_of_pat_ind \\ rpt strip_tac \\
fs [v_of_pat_def, pat_bindings_def, pat_wildcards_def, LENGTH_NIL] \\ rw [] \\
every_case_tac \\ fs [] \\ rw []
QED
Theorem v_of_pat_extend_insts:
(!envC pat insts wildcards v rest wildcards_rest insts'.
v_of_pat envC pat insts wildcards = SOME (v, rest, wildcards_rest) ==>
v_of_pat envC pat (insts ++ insts') wildcards = SOME (v, rest ++ insts', wildcards_rest)) /\
(!envC pats insts wildcards vs rest wildcards_rest insts'.
v_of_pat_list envC pats insts wildcards = SOME (vs, rest, wildcards_rest) ==>
v_of_pat_list envC pats (insts ++ insts') wildcards = SOME (vs, rest ++ insts', wildcards_rest))
Proof
HO_MATCH_MP_TAC v_of_pat_ind \\ rpt strip_tac \\
try_finally (fs [v_of_pat_def])
THEN1 (fs [v_of_pat_def] \\ every_case_tac \\ fs [])
THEN1 (fs [v_of_pat_def] \\ every_case_tac \\ fs [])
THEN1 (
rename1 `Pcon c _` \\ Cases_on `c`
THEN1 (fs [v_of_pat_def] \\ every_case_tac \\ fs [] \\ rw [])
THEN1 (
fs [v_of_pat_def] \\ every_case_tac \\ rw [] \\
TRY (qpat_x_assum `LENGTH _ = LENGTH _` (K all_tac)) \\ fs [] \\
rw []
)
)
THEN1 (fs [v_of_pat_def] \\ every_case_tac \\ fs [] \\ rw [] \\ fs [])
QED
Theorem v_of_pat_extend_wildcards:
(!envC pat insts wildcards v rest wildcards_rest wildcards'.
v_of_pat envC pat insts wildcards = SOME (v, rest, wildcards_rest) ==>
v_of_pat envC pat insts (wildcards ++ wildcards') = SOME (v, rest, wildcards_rest ++ wildcards')) /\
(!envC pats insts wildcards vs rest wildcards_rest wildcards'.
v_of_pat_list envC pats insts wildcards = SOME (vs, rest, wildcards_rest) ==>
v_of_pat_list envC pats insts (wildcards ++ wildcards') = SOME (vs, rest, wildcards_rest ++ wildcards'))
Proof
HO_MATCH_MP_TAC v_of_pat_ind \\ rpt strip_tac \\
try_finally (fs [v_of_pat_def])
THEN1 (fs [v_of_pat_def] \\ every_case_tac \\ fs [])
THEN1 (fs [v_of_pat_def] \\ every_case_tac \\ gvs [])
THEN1 (
rename1 `Pcon c _` \\ Cases_on `c`
THEN1 (fs [v_of_pat_def] \\ every_case_tac \\ fs [] \\ rw [])
THEN1 (
first_x_assum (assume_tac o (SIMP_RULE std_ss [v_of_pat_def])) \\
every_case_tac \\ fs [] \\ rw [] \\ fs [v_of_pat_def]
)
)
THEN1 (fs [v_of_pat_def] \\ every_case_tac \\ fs [] \\ rw [] \\ fs [])
QED
Theorem v_of_pat_NONE_extend_insts:
(!envC pat insts wildcards insts'.
v_of_pat envC pat insts wildcards = NONE ==>
LENGTH insts >= LENGTH (pat_bindings pat []) ==>
LENGTH wildcards >= pat_wildcards pat ==>
v_of_pat envC pat (insts ++ insts') wildcards = NONE) /\
(!envC pats insts wildcards insts'.
v_of_pat_list envC pats insts wildcards = NONE ==>
LENGTH insts >= LENGTH (pats_bindings pats []) ==>
LENGTH wildcards >= pats_wildcards pats ==>
v_of_pat_list envC pats (insts ++ insts') wildcards = NONE)
Proof
HO_MATCH_MP_TAC v_of_pat_ind \\ rpt strip_tac \\
try_finally (
fs [v_of_pat_def, pat_bindings_def, pat_wildcards_def] \\
every_case_tac \\ fs []
)
THEN1 (
rename1 `Pcon c _` \\ Cases_on `c`
THEN1 (
fs [v_of_pat_def, pat_bindings_def, pat_wildcards_def] \\
every_case_tac \\ fs []
)
THEN1 (
fs [v_of_pat_def, pat_bindings_def, pat_wildcards_def] \\
every_case_tac \\ fs [] \\ rw [] \\
metis_tac [v_of_pat_list_length]
)
)
THEN1 (
fs [v_of_pat_def, pat_bindings_def, pat_wildcards_def] \\
every_case_tac \\ fs [] \\
fs [Once (snd (CONJ_PAIR semanticPrimitivesPropsTheory.pat_bindings_accum))]
THEN1 (
`LENGTH insts >= LENGTH (pat_bindings pat [])` by (fs []) \\
`LENGTH wildcards >= pat_wildcards pat` by (fs []) \\
fs []
)
THEN1 (
rw [] \\
progress (fst (CONJ_PAIR v_of_pat_insts_length)) \\
progress (fst (CONJ_PAIR v_of_pat_wildcards_count)) \\
rename1 `LENGTH insts = _ + LENGTH rest2` \\
rename1 `LENGTH wildcards = _ + LENGTH w_rest2` \\
`LENGTH rest2 >= LENGTH (pats_bindings pats [])` by (fs []) \\
`LENGTH w_rest2 >= pats_wildcards pats` by (fs []) \\ fs [] \\
progress (fst (CONJ_PAIR v_of_pat_extend_insts)) \\
pop_assum (qspec_then `insts'` assume_tac) \\ fs [] \\ rw [] \\ fs []
)
)
QED
Theorem v_of_pat_remove_rest_insts:
(!pat envC insts wildcards v rest wildcards_rest.
v_of_pat envC pat insts wildcards = SOME (v, rest, wildcards_rest) ==>
?insts'.
insts = insts' ++ rest /\
LENGTH insts' = LENGTH (pat_bindings pat []) /\
v_of_pat envC pat insts' wildcards = SOME (v, [], wildcards_rest)) /\
(!pats envC insts wildcards vs rest wildcards_rest.
v_of_pat_list envC pats insts wildcards = SOME (vs, rest, wildcards_rest) ==>
?insts'.
insts = insts' ++ rest /\
LENGTH insts' = LENGTH (pats_bindings pats []) /\
v_of_pat_list envC pats insts' wildcards = SOME (vs, [], wildcards_rest))
Proof
HO_MATCH_MP_TAC astTheory.pat_induction \\ rpt strip_tac \\
try_finally (fs [v_of_pat_def, pat_bindings_def])
THEN1 (fs [v_of_pat_def, pat_bindings_def] \\ every_case_tac \\ fs [])
THEN1 (fs [v_of_pat_def, pat_bindings_def] \\ every_case_tac \\ gvs [])
THEN1 (
rename1 `Pcon c _` \\ Cases_on `c` \\
fs [v_of_pat_def, pat_bindings_def] \\ every_case_tac \\ fs [] \\ rw [] \\
first_assum progress \\ rw []
)
THEN1 (
fs [v_of_pat_def, pat_bindings_def] \\ every_case_tac \\ fs [] \\ rw [] \\
qpat_assum `v_of_pat _ _ _ _ = _` (first_assum o progress_with) \\
qpat_assum `v_of_pat_list _ _ _ _ = _` (first_assum o progress_with) \\
rw []
THEN1 (
once_rewrite_tac [snd (CONJ_PAIR semanticPrimitivesPropsTheory.pat_bindings_accum)] \\
fs []
)
THEN1 (
rename1 `LENGTH insts_pats = LENGTH (pats_bindings pats [])` \\
rename1 `LENGTH insts_pat = LENGTH (pat_bindings pat [])` \\
rename1 `v_of_pat_list _ _ (_ ++ rest) _` \\
fs [APPEND_ASSOC] \\ every_case_tac \\ fs []
THEN1 (
progress_then (qspec_then `rest` assume_tac)
(fst (CONJ_PAIR v_of_pat_NONE_extend_insts)) \\
`LENGTH (insts_pat ++ insts_pats) >= LENGTH (pat_bindings pat [])`
by (fs []) \\
`LENGTH wildcards >= pat_wildcards pat` by (
progress (fst (CONJ_PAIR v_of_pat_wildcards_count)) \\ fs []
) \\ fs []
) \\
rename1 `v_of_pat_list _ _ rest' _ = _` \\
qpat_x_assum `v_of_pat _ _ (_ ++ _) _ = _` (first_assum o progress_with) \\
`rest' = insts_pats` by (metis_tac [APPEND_11_LENGTH]) \\ fs [] \\ rw [] \\ fs []
)
)
QED
Theorem v_of_pat_insts_unique:
(!envC pat insts wildcards rest wildcards_rest v.
v_of_pat envC pat insts wildcards = SOME (v, rest, wildcards_rest) ==>
(!insts' wildcards'. v_of_pat envC pat insts' wildcards' = SOME (v, rest, wildcards_rest) <=> (insts' = insts /\ wildcards' = wildcards))) /\
(!envC pats insts wildcards rest wildcards_rest vs.
v_of_pat_list envC pats insts wildcards = SOME (vs, rest, wildcards_rest) ==>
(!insts' wildcards'. v_of_pat_list envC pats insts' wildcards' = SOME (vs, rest, wildcards_rest) <=> (insts' = insts /\ wildcards' = wildcards)))
Proof
HO_MATCH_MP_TAC v_of_pat_ind \\ rpt strip_tac \\
try_finally (fs [v_of_pat_def] \\ every_case_tac \\ fs [])
THEN1 (fs [v_of_pat_def] \\ every_case_tac \\ fs [] \\ metis_tac [])
THEN1 (fs [v_of_pat_def] \\ every_case_tac \\ fs [] \\ metis_tac [])
THEN1 (
reverse eq_tac THEN1 (rw []) \\ fs [v_of_pat_def] \\ strip_tac \\
every_case_tac \\ fs [] \\ rw [] \\
first_x_assum (qspecl_then [`insts'`, `wildcards'`] assume_tac) \\
fs []
)
THEN1 (
reverse eq_tac THEN1 (rw []) \\ strip_tac \\ fs [v_of_pat_def] \\
every_case_tac \\ fs [] \\ rw [] \\ fs []
)
QED
(* [v_of_pat_norest]: Wrapper that checks that there are no remaining
instantiations and wildcards instantiations
*)
val v_of_pat_norest_def = Define `
v_of_pat_norest envC pat insts wildcards =
case v_of_pat envC pat insts wildcards of
SOME (v, [], []) => SOME v
| _ => NONE`;
Theorem v_of_pat_norest_insts_length:
!envC pat insts wildcards v.
v_of_pat_norest envC pat insts wildcards = SOME v ==>
LENGTH insts = LENGTH (pat_bindings pat [])
Proof
rpt strip_tac \\ fs [v_of_pat_norest_def] \\ every_case_tac \\ fs [] \\
rw [] \\ progress (fst (CONJ_PAIR v_of_pat_insts_length)) \\ fs []
QED
Theorem v_of_pat_norest_wildcards_count:
!envC pat insts wildcards v.
v_of_pat_norest envC pat insts wildcards = SOME v ==>
LENGTH wildcards = pat_wildcards pat
Proof
rpt strip_tac \\ fs [v_of_pat_norest_def] \\ every_case_tac \\ fs [] \\
rw [] \\ progress (fst (CONJ_PAIR v_of_pat_wildcards_count)) \\ fs []
QED
Theorem v_of_pat_norest_insts_unique:
!envC pat insts wildcards v.
v_of_pat_norest envC pat insts wildcards = SOME v ==>
(!insts' wildcards'. v_of_pat_norest envC pat insts' wildcards' = SOME v <=> (insts' = insts /\ wildcards' = wildcards))
Proof
rpt strip_tac \\ fs [v_of_pat_norest_def] \\
every_case_tac \\ fs [] \\ rw [] \\
try_finally (
CONV_TAC quantHeuristicsTools.OR_NOT_CONV \\
strip_tac \\ rw [] \\ fs []
) \\ TRY (CCONTR_TAC \\ fs [] \\ NO_TAC) \\
progress_then
(qspecl_then [`insts'`, `wildcards'`] assume_tac)
(fst (CONJ_PAIR (v_of_pat_insts_unique))) \\
fs [] \\
eq_tac \\ rw [] \\ fs []
QED
(* Predicates that discriminate the patterns we want to deal
with. [validate_pat] packs them all up.
*)
(* might be wrong *)
val pat_typechecks_def = Define `
pat_typechecks envC s pat v =
(pmatch envC s pat v [] <> Match_type_error)`;
Definition pat_without_Pref_Pas_def:
pat_without_Pref_Pas (Pcon _ args) = EVERY pat_without_Pref_Pas args /\
pat_without_Pref_Pas (Pref _) = F /\
pat_without_Pref_Pas (Ptannot p _) = pat_without_Pref_Pas p /\
pat_without_Pref_Pas (Pas p _) = F /\
pat_without_Pref_Pas _ = T
Termination
WF_REL_TAC `measure pat_size` \\ simp[] \\
Cases \\ Induct \\ fs [basicSizeTheory.option_size_def] \\
fs [list_size_def, astTheory.pat_size_def] \\ rpt strip_tac \\ fs [] \\
first_assum progress \\ fs []
End
val validate_pat_def = Define `
validate_pat envC s pat v env =
(pat_typechecks envC s pat v /\
pat_without_Pref_Pas pat /\
ALL_DISTINCT (pat_bindings pat []))`;
(* Lemmas that relate [v_of_pat] and [pmatch], the pattern-matching function
from the semantics.
*)
Theorem same_type_EQ_same_ctor[simp]:
same_type r r <=> same_ctor r r
Proof
Cases_on `r` \\ fs [same_type_def] \\ fs [same_ctor_def]
QED
Theorem same_ctor_REFL[simp]:
same_ctor r r
Proof
fs [same_ctor_def]
QED
Theorem v_of_pat_pmatch:
(!envC s pat v env_v insts wildcards wildcards_rest.
v_of_pat envC pat insts wildcards = SOME (v, [], wildcards_rest) ==>
pmatch envC s pat v env_v = Match
(ZIP (pat_bindings pat [], REVERSE insts) ++ env_v)) /\
(!envC s pats vs env_v insts wildcards wildcards_rest.
v_of_pat_list envC pats insts wildcards = SOME (vs, [], wildcards_rest) ==>
pmatch_list envC s pats vs env_v = Match
(ZIP (pats_bindings pats [], REVERSE insts) ++ env_v))
Proof
HO_MATCH_MP_TAC pmatch_ind \\ rpt strip_tac \\ rw [] \\
try_finally (
fs [pmatch_def, v_of_pat_def, pat_bindings_def] \\
CHANGED_TAC every_case_tac \\ fs [] \\
CHANGED_TAC every_case_tac \\ fs []
)
THEN1 (
fs [pmatch_def, v_of_pat_def, pat_bindings_def] \\
every_case_tac \\ fs [] \\ rw []
THEN1 (progress v_of_pat_list_length \\ first_assum progress)
THEN1 (progress v_of_pat_list_length \\ fs [])
)
THEN1 (
fs [pmatch_def, v_of_pat_def, pat_bindings_def] \\
every_case_tac \\ fs [] \\ rw [] \\
progress v_of_pat_list_length \\ first_assum progress \\ fs []
)
THEN1 (
fs [pmatch_def, v_of_pat_def, pat_bindings_def] \\
every_case_tac \\ fs [] \\ rw [] \\ first_assum progress
)
THEN1 (
fs [pmatch_def, pat_bindings_def, v_of_pat_def] \\
every_case_tac \\ fs [] \\ rw [] \\
try_finally (
progress (fst (CONJ_PAIR v_of_pat_remove_rest_insts)) \\ rw [] \\
rename1 `insts' ++ rest` \\ first_assum (qspec_then `insts'` (fs o sing))
) \\
once_rewrite_tac [snd (CONJ_PAIR semanticPrimitivesPropsTheory.pat_bindings_accum)] \\
fs [] \\ progress (fst (CONJ_PAIR v_of_pat_remove_rest_insts)) \\ rw [] \\
fs [REVERSE_APPEND] \\ progress (snd (CONJ_PAIR v_of_pat_insts_length)) \\
fs [GSYM ZIP_APPEND] \\ once_rewrite_tac [GSYM APPEND_ASSOC] \\
rpt (first_x_assum progress) \\ rw []
)
QED
Theorem v_of_pat_norest_pmatch:
!envC s pat v env_v insts wildcards.
v_of_pat_norest envC pat insts wildcards = SOME v ==>
pmatch envC s pat v env_v = Match
(ZIP (pat_bindings pat [], REVERSE insts) ++ env_v)
Proof
rpt strip_tac \\ fs [v_of_pat_norest_def] \\
irule (fst (CONJ_PAIR v_of_pat_pmatch)) \\
every_case_tac \\ rw [] \\ instantiate
QED
Theorem pmatch_v_of_pat:
(!envC s pat v env_v env_v'.
pmatch envC s pat v env_v = Match env_v' ==>
pat_without_Pref_Pas pat ==>
?insts wildcards.
env_v' = ZIP (pat_bindings pat [], REVERSE insts) ++ env_v /\
v_of_pat envC pat insts wildcards = SOME (v, [], [])) /\
(!envC s pats vs env_v env_v'.
pmatch_list envC s pats vs env_v = Match env_v' ==>
EVERY (\pat. pat_without_Pref_Pas pat) pats ==>
?insts wildcards.
env_v' = ZIP (pats_bindings pats [], REVERSE insts) ++ env_v /\
v_of_pat_list envC pats insts wildcards = SOME (vs, [], []))
Proof
HO_MATCH_MP_TAC pmatch_ind \\ rpt strip_tac \\ rw [] \\
try_finally (fs [pmatch_def, v_of_pat_def, pat_bindings_def])
THEN1 (
qexists_tac `[]` \\ Q.REFINE_EXISTS_TAC `w::ws` \\
fs [pmatch_def, v_of_pat_def, pat_bindings_def] \\ rw []
)
THEN1 (
fs [pmatch_def, v_of_pat_def, pat_bindings_def] \\
qpat_x_assum `_ = _` (fs o sing o GSYM) \\
rename1 `[(x,xv)]` \\ qexists_tac `[xv]` \\ fs []
)
THEN1 (
fs [pmatch_def, v_of_pat_def, pat_bindings_def] \\
every_case_tac \\ fs []
)
THEN1 (
fs [pmatch_def, pat_without_Pref_Pas_def] \\ every_case_tac \\ fs [] \\
fs [pat_bindings_def] \\ Q.LIST_EXISTS_TAC [`insts`, `wildcards`] \\
rename1 `same_ctor t1 t2` \\ fs [] \\
rewrite_tac [v_of_pat_def] \\ fs [] \\ progress v_of_pat_list_length \\
fs [same_ctor_def]
)
THEN1 (
fs [pmatch_def, pat_without_Pref_Pas_def] \\ every_case_tac \\ fs [] \\
fs [pat_bindings_def] \\ Q.LIST_EXISTS_TAC [`insts`, `wildcards`] \\ fs [] \\
rewrite_tac [v_of_pat_def] \\ every_case_tac \\ fs []
)
THEN1 (fs [pat_without_Pref_Pas_def])
THEN1 (fs [pat_without_Pref_Pas_def])
THEN1 (fs [pat_without_Pref_Pas_def,pat_bindings_def,v_of_pat_def,pmatch_def]
\\ metis_tac[])
THEN1 (
fs [pmatch_def] \\ every_case_tac \\ fs [] \\ rw [] \\
first_assum progress \\ rw [] \\ fs [pat_bindings_def] \\
once_rewrite_tac [semanticPrimitivesPropsTheory.pat_bindings_accum] \\ fs [] \\
rename1 `v_of_pat _ _ insts wildcards = _` \\
rename1 `v_of_pat_list _ _ insts' wildcards' = _` \\
qexists_tac `insts ++ insts'` \\ qexists_tac `wildcards ++ wildcards'` \\
progress (fst (CONJ_PAIR v_of_pat_insts_length)) \\
progress (snd (CONJ_PAIR v_of_pat_insts_length)) \\ fs [ZIP_APPEND] \\
progress_then (qspec_then `insts'` assume_tac)
(fst (CONJ_PAIR v_of_pat_extend_insts)) \\
progress_then (qspec_then `wildcards'` assume_tac)
(fst (CONJ_PAIR v_of_pat_extend_wildcards)) \\
progress_then (qspec_then `insts'` assume_tac)
(snd (CONJ_PAIR v_of_pat_extend_insts)) \\
progress_then (qspec_then `wildcards'` assume_tac)
(snd (CONJ_PAIR v_of_pat_extend_insts)) \\
fs [v_of_pat_def]
)
QED
Theorem pmatch_v_of_pat_norest:
!envC s pat v env_v env_v'.
pmatch envC s pat v env_v = Match env_v' ==>
pat_without_Pref_Pas pat ==>
?insts wildcards.
env_v' = ZIP (pat_bindings pat [], REVERSE insts) ++ env_v /\
v_of_pat_norest envC pat insts wildcards = SOME v
Proof
rpt strip_tac \\ progress (fst (CONJ_PAIR pmatch_v_of_pat)) \\ fs [] \\
Q.LIST_EXISTS_TAC [`insts`, `wildcards`] \\ fs [v_of_pat_norest_def]
QED
(* The nested ifs corresponding to a list of patterns *)
val cf_cases_def = Define `
cf_cases v nomatch_exn [] env H Q =
local (\H Q. H ==>> Q (Exn nomatch_exn) /\ Q =~e> POST_F) H Q /\
cf_cases v nomatch_exn ((pat, row_cf)::rows) env H Q =
local (\H Q.
((if (?insts wildcards. v_of_pat_norest env.c pat insts wildcards = SOME v) then
(!insts wildcards. v_of_pat_norest env.c pat insts wildcards = SOME v ==>
can_pmatch_all env.c [] (MAP FST rows) v /\
row_cf (extend_env (REVERSE (pat_bindings pat [])) insts env) H Q)
else cf_cases v nomatch_exn rows env H Q) /\
(!s. validate_pat env.c s pat v env.v))
) H Q`;
fun NETA_CONV t = let
val (params, body) = strip_abs t