-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathml_monad_translatorScript.sml
3580 lines (3387 loc) · 125 KB
/
ml_monad_translatorScript.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 EvalM and other judgements that are central to the monadic
translator.
*)
open ml_translatorTheory ml_translatorLib ml_pmatchTheory patternMatchesTheory
open astTheory semanticPrimitivesTheory evaluateTheory evaluatePropsTheory
open evaluateTheory ml_progLib ml_progTheory
open set_sepTheory Satisfy
open cfHeapsBaseTheory AC_Sort
open ml_monadBaseTheory ml_monad_translatorBaseTheory
open cfStoreTheory cfTheory cfTacticsLib packLib;
open preamble;
val _ = new_theory "ml_monad_translator";
Overload monad_bind[local] = ``st_ex_bind``;
Overload monad_unitbind[local] = ``st_ex_ignore_bind``;
Overload monad_ignore_bind[local] = ``st_ex_ignore_bind``;
Overload ex_bind[local] = ``st_ex_bind``;
Overload ex_return[local] = ``st_ex_return``;
Overload CONTAINER[local] = ``ml_translator$CONTAINER``;
val _ = hide "state";
fun imp1_fvs th =
let
val (bvs, c) = th |> concl |> strip_forall
val fvs = c |> strip_imp |> #1 |> hd |> strip_conj |> hd |> free_vars
in
op_set_diff aconv fvs bvs
end
fun old_drule th =
let val fvs = imp1_fvs th
in
FREEZE_THEN drule (GENL fvs th)
end
(* TODO: move *)
Theorem s_with_same_clock[simp]:
!s. (s with clock := s.clock) = s
Proof
fs [state_component_equality]
QED
(* -- *)
Triviality GC_ABSORB_L:
!A B s. (A * B * GC) s ==> (A * GC) s
Proof
rw[]
\\ fs[GSYM STAR_ASSOC]
\\ fs[Once STAR_def]
\\ qexists_tac `u`
\\ qexists_tac `v`
\\ fs[SAT_GC]
QED
Triviality GC_ABSORB_R:
!A B s. (A * GC * B) s ==> (A * GC) s
Proof
rw[]
\\ `A * GC * B = A * B * GC` by metis_tac[STAR_COMM, STAR_ASSOC]
\\ pop_assum(fn x => fs[x])
\\ imp_res_tac GC_ABSORB_L
QED
val HCOND_EXTRACT = cfLetAutoTheory.HCOND_EXTRACT;
Triviality REF_EXISTS_LOC:
(rv ~~> v * H) s ==> ?l. rv = Loc T l
Proof
rw[REF_def, SEP_CLAUSES, SEP_EXISTS_THM, GSYM STAR_ASSOC, HCOND_EXTRACT]
QED
Triviality ARRAY_EXISTS_LOC:
(ARRAY rv v * H) s ==> ?l. rv = Loc T l
Proof
rw[STAR_def, SEP_EXISTS_THM, SEP_CLAUSES, REF_def, ARRAY_def, cond_def]
QED
Triviality UNIQUE_CELLS:
!p s. !l xv xv' H H'. (l ~~>> xv * H) (st2heap p s) /\ (l ~~>> xv' * H') (st2heap p s) ==> xv' = xv
Proof
rw[] >>
imp_res_tac st2heap_CELL_MEM >>
imp_res_tac store2heap_IN_unique_key
QED
(* Should be moved *)
fun list_dest f tm =
let val (x,y) = f tm in list_dest f x @ list_dest f y end
handle HOL_ERR _ => [tm];
(* *)
(*********** Comes from cfLetAutoLib.sml ***********************************************)
(* [dest_pure_fact]
Deconstruct a pure fact (a heap predicate of the form &P) *)
val set_sep_cond_tm = ``set_sep$cond : bool -> hprop``;
fun dest_pure_fact p =
case (dest_term p) of
COMB dp =>
(if same_const set_sep_cond_tm (#1 dp) then (#2 dp)
else raise (ERR "dest_pure_fact" "Not a pure fact"))
| _ => raise (ERR "dest_pure_fact" "Not a pure fact");
(***************************************************************************************)
fun PURE_FACTS_FIRST_CONV H =
let
val preds = list_dest dest_star H
val (pfl, hpl) = List.partition (can dest_pure_fact) preds
val ordered_preds = pfl @ hpl
in
if List.null ordered_preds then REFL H
else
let val H' = List.foldl (fn (x, y) => mk_star(y, x)) (List.hd ordered_preds)
(List.tl ordered_preds)
(* For some strange reason, AC_CONV doesn't work *)
val H_to_norm = STAR_AC_CONV H
val norm_to_H' = (SYM(STAR_AC_CONV H') handle UNCHANGED => REFL H')
in TRANS H_to_norm norm_to_H'
end
end;
val EXTRACT_PURE_FACTS_CONV =
(RATOR_CONV PURE_FACTS_FIRST_CONV)
THENC (SIMP_CONV pure_ss [GSYM STAR_ASSOC])
THENC (SIMP_CONV pure_ss [HCOND_EXTRACT])
THENC (SIMP_CONV pure_ss [STAR_ASSOC]);
(* TODO: use EXTRACT_PURE_FACT_CONV to rewrite EXTRACT_PURE_FACTS_TAC *)
fun EXTRACT_PURE_FACTS_TAC (g as (asl, w)) =
let
fun is_hprop a = ((dest_comb a |> fst |> type_of) = ``:hprop`` handle HOL_ERR _ => false)
val hpreds = List.filter is_hprop asl
val hpreds' = List.map (fst o dest_comb) hpreds
val hpreds_eqs = mapfilter (PURE_FACTS_FIRST_CONV) hpreds'
in
((fs hpreds_eqs) >> fs[GSYM STAR_ASSOC] >> fs[HCOND_EXTRACT] >> fs[STAR_ASSOC]) g
end;
(***********************************************************************************************)
fun first_assum_rewrite_once th =
pop_assum(fn x => ASSUME_TAC(PURE_ONCE_REWRITE_RULE[th] x))
Type state = ``:'ffi semanticPrimitives$state``
(***)
(*
* Definition of EvalM
* `ro`: references only
*)
Definition EvalM_def:
EvalM ro env st exp P H <=>
!(s:'ffi state).
REFS_PRED H st s ==>
?s2 res st2 ck.
evaluate (s with clock := ck) env [exp] = (s2,res) /\
P st (st2, res) /\ REFS_PRED_FRAME ro H (st, s) (st2, s2) /\
s.fp_state = s2.fp_state
End
(* refinement invariant for ``:('a, 'b, 'c) M`` *)
Type M = ``:'a -> ('b, 'c) exc # 'a``
Definition MONAD_def:
MONAD (a:'a->v->bool) (b: 'b->v->bool) (x:('refs, 'a, 'b) M)
(state1:'refs)
(state2:'refs,res: (v list,v) result) =
case (x state1, res) of
((M_success y, st), Rval [v]) => (st = state2) /\ a y v
| ((M_failure e, st), Rerr (Rraise v)) => (st = state2) /\
b e v
| _ => F
End
val H = mk_var("H",``:('a -> hprop) # 'ffi ffi_proj``);
(* return *)
Theorem EvalM_return:
!H b. Eval env exp (a x) ==>
EvalM ro env st exp (MONAD a b (ex_return x)) ^H
Proof
rw[Eval_def,EvalM_def,st_ex_return_def,MONAD_def]
\\ first_x_assum(qspec_then`s.refs`strip_assume_tac)
\\ imp_res_tac (evaluate_empty_state_IMP)
\\ fs [eval_rel_def,PULL_EXISTS]
\\ drule evaluate_set_clock \\ simp []
\\ disch_then (qspec_then `s.clock` mp_tac)
\\ strip_tac \\ fs []
\\ asm_exists_tac \\ simp []
\\ `(s with <|clock := s.clock; refs := s.refs ⧺ refs'|>) =
(s with <|refs := s.refs ⧺ refs'|>)` by fs [state_component_equality]
\\ fs [REFS_PRED_FRAME_append]
QED
(* bind *)
Theorem EvalM_bind:
(a1 ==> EvalM ro env st e1 (MONAD b c (x:('refs, 'b, 'c) M))
(H:('refs -> hprop) # 'ffi ffi_proj)) /\
(!z v. b z v ==> a2 z ==>
EvalM ro (write name v env) (SND (x st)) e2
(MONAD a c ((f z):('refs, 'a, 'c) M)) H) ==>
(a1 /\ !z. (CONTAINER(FST(x st) = M_success z) ==> a2 z)) ==>
EvalM ro env st (Let (SOME name) e1 e2) (MONAD a c (ex_bind x f)) H
Proof
rw[EvalM_def,MONAD_def,st_ex_return_def,PULL_EXISTS, CONTAINER_def] \\ fs[]
\\ last_x_assum drule \\ rw[]
\\ imp_res_tac REFS_PRED_FRAME_imp
\\ Cases_on `x st` \\ fs []
\\ rename1 `x st = (succ,new_state)`
\\ simp [evaluate_def,pair_case_eq,PULL_EXISTS]
\\ reverse (Cases_on `succ`) \\ fs []
THEN1
(Cases_on `res` \\ fs[] \\ rw [] \\ Cases_on `e`
\\ fs [st_ex_bind_def] \\ rveq \\ asm_exists_tac \\ fs [])
\\ fs[st_ex_bind_def]
\\ Cases_on `res` \\ fs []
\\ drule evaluate_sing \\ strip_tac \\ rveq \\ fs []
\\ last_x_assum drule \\ rw[]
\\ first_x_assum drule \\ rw[]
\\ Cases_on `f a' new_state` \\ fs []
\\ drule evaluate_set_clock
\\ qpat_x_assum `evaluate _ _ _ = _` kall_tac
\\ disch_then (qspec_then `s2'.clock` mp_tac)
\\ impl_tac THEN1 (CCONTR_TAC \\ fs [] \\ EVERY_CASE_TAC \\ fs [])
\\ strip_tac \\ fs [] \\ pop_assum mp_tac
\\ drule evaluate_set_clock \\ fs []
\\ disch_then (qspec_then `ck1` mp_tac)
\\ rpt strip_tac \\ fs []
\\ asm_exists_tac \\ fs [write_def,namespaceTheory.nsOptBind_def]
\\ Cases_on `q` \\ fs []
\\ Cases_on `res'` \\ fs []
\\ TRY (Cases_on `e`) \\ fs []
\\ imp_res_tac evaluate_sing \\ fs [] \\ rveq \\ fs []
\\ imp_res_tac REFS_PRED_FRAME_trans
QED
(* bind ignore *)
Theorem EvalM_bind_ignore:
(EvalM ro env st e1 (MONAD b c (x:('refs, 'b, 'c) M))
(H:('refs -> hprop) # 'ffi ffi_proj)) /\
(EvalM ro env (SND (x st)) e2 (MONAD a c (f:('refs, 'a, 'c) M)) H) ==>
EvalM ro env st (Let NONE e1 e2) (MONAD a c (st_ex_ignore_bind x f)) H
Proof
rw[EvalM_def,MONAD_def,st_ex_return_def,PULL_EXISTS, CONTAINER_def] \\ fs[]
\\ last_x_assum drule \\ rw[]
\\ imp_res_tac REFS_PRED_FRAME_imp
\\ Cases_on `x st` \\ fs []
\\ rename1 `x st = (succ,new_state)`
\\ simp [evaluate_def,pair_case_eq,PULL_EXISTS]
\\ reverse (Cases_on `succ`) \\ fs []
THEN1
(Cases_on `res` \\ fs[] \\ rw [] \\ Cases_on `e`
\\ fs [st_ex_ignore_bind_def] \\ rveq \\ asm_exists_tac \\ fs [])
\\ fs[st_ex_ignore_bind_def]
\\ Cases_on `res` \\ fs []
\\ drule evaluate_sing \\ strip_tac \\ rveq \\ fs []
\\ last_x_assum drule \\ rw[]
\\ Cases_on `f new_state` \\ fs []
\\ drule evaluate_set_clock
\\ qpat_x_assum `evaluate _ _ _ = _` kall_tac
\\ disch_then (qspec_then `s2'.clock` mp_tac)
\\ impl_tac THEN1 (CCONTR_TAC \\ fs [] \\ EVERY_CASE_TAC \\ fs [])
\\ strip_tac \\ fs [] \\ pop_assum mp_tac
\\ drule evaluate_set_clock \\ fs []
\\ disch_then (qspec_then `ck1` mp_tac)
\\ rpt strip_tac \\ fs []
\\ asm_exists_tac \\ fs [write_def,namespaceTheory.nsOptBind_def]
\\ Cases_on `q` \\ fs []
\\ Cases_on `res'` \\ fs []
\\ TRY (Cases_on `e`) \\ fs []
\\ imp_res_tac evaluate_sing \\ fs [] \\ rveq \\ fs []
\\ imp_res_tac REFS_PRED_FRAME_trans
QED
Theorem EvalM_pure_seq:
Eval env e1 (c y) ∧
EvalM ro env st e2 (MONAD a b x) ^H ⇒
EvalM ro env st (Let NONE e1 e2) (MONAD a b (pure_seq y x)) ^H
Proof
rw []
\\ ‘pure_seq y x = monad_ignore_bind (ex_return y) x’ by
fs [pure_seq_def,st_ex_ignore_bind_def,st_ex_return_def, FUN_EQ_THM]
\\ fs [] \\ irule EvalM_bind_ignore \\ fs []
\\ conj_tac
>- (qexists_tac ‘c’ \\ fs [EvalM_return])
\\ fs [st_ex_return_def]
QED
(* lift ro refinement invariants *)
Type H = ``:'a -> 'refs -> 'refs # (v list,v) result -> bool``
Definition PURE_def:
PURE a (x:'a) (st1:'refs) (st2,res:(v list,v) result) =
?v:v. (res = Rval [v]) /\ (st1 = st2) /\ a x v
End
Definition EqSt_def:
EqSt abs st = \x st1 (st2, res). st = st1 /\ abs x st1 (st2, res)
End
Theorem state_update_clock_id[simp]:
(s with <|clock := s.clock; refs := refs'|>) =
s with <| refs := refs'|>
Proof
fs [state_component_equality]
QED
Theorem Eval_IMP_PURE:
!H env exp P x. Eval env exp (P x) ==> EvalM ro env st exp (PURE P x) ^H
Proof
rw[Eval_def,EvalM_def,PURE_def,PULL_EXISTS]
\\ first_x_assum(qspec_then`s.refs`strip_assume_tac)
\\ imp_res_tac evaluate_empty_state_IMP
\\ fs[eval_rel_def]
\\ drule evaluate_set_clock \\ fs []
\\ disch_then (qspec_then `s.clock` mp_tac)
\\ strip_tac \\ fs [] \\ asm_exists_tac
\\ fs [REFS_PRED_FRAME_append]
QED
Theorem Eval_IMP_PURE_EvalM_T:
!H env exp P x. Eval env exp (P x) ==> EvalM T env st exp (PURE P x) ^H
Proof
rw[Eval_IMP_PURE]
QED
(* function abstraction and application *)
Definition ArrowP_def:
ArrowP ro H (a:('a, 'refs) H) b f c =
!x st1 s1 st2 (res:(v list,v) result).
a x st1 (st2,res) /\ REFS_PRED H st1 s1 ==>
?v env exp.
(st2 = st1) /\
(res = Rval [v]) /\ do_opapp [c;v] = SOME (env,exp) /\
!junk. ?st3 s3 res3 ck.
evaluate (s1 with <| refs := s1.refs ++ junk ; clock := ck |>)
env [exp] = (s3,res3) /\
s1.fp_state = s3.fp_state /\
b (f x) st1 (st3,res3) /\
REFS_PRED_FRAME ro H (st1, s1) (st3, s3)
End
Definition ArrowM_def:
ArrowM ro H (a:('a, 'refs) H) (b:('b, 'refs) H) =
PURE (ArrowP ro H a b) : ('a -> 'b, 'refs) H
End
val EvalM_Arrow_tac =
rw[EvalM_def,ArrowM_def,ArrowP_def,PURE_def,PULL_EXISTS,evaluate_def,
pair_case_eq,result_case_eq,PULL_EXISTS,EqSt_def,Eq_def,
astTheory.getOpClass_def]
\\ first_x_assum drule \\ strip_tac
\\ drule REFS_PRED_FRAME_imp
\\ disch_then drule \\ strip_tac
\\ first_x_assum drule \\ strip_tac
\\ first_x_assum drule
\\ `REFS_PRED H st s2'` by metis_tac [REFS_PRED_FRAME_imp]
\\ disch_then drule \\ strip_tac
\\ imp_res_tac REFS_PRED_FRAME_trans
\\ qpat_x_assum `!x. _` mp_tac
\\ qpat_x_assum `!x. _` (qspec_then `[]` strip_assume_tac)
\\ disch_then kall_tac
\\ once_rewrite_tac [CONJ_COMM]
\\ asm_exists_tac \\ fs []
\\ imp_res_tac REFS_PRED_FRAME_trans
\\ asm_exists_tac \\ fs []
\\ qpat_x_assum `evaluate _ _ [x1] = _` assume_tac
\\ drule evaluate_set_clock \\ fs []
\\ disch_then (qspec_then `ck''+1` mp_tac) \\ strip_tac
\\ qpat_x_assum `evaluate _ _ [x2] = _` assume_tac
\\ drule evaluate_set_clock \\ fs []
\\ disch_then (qspec_then `ck1` mp_tac) \\ strip_tac
\\ rewrite_tac [GSYM CONJ_ASSOC] \\ fs[PULL_EXISTS]
\\ asm_exists_tac
\\ `(s2' with <|clock := ck''; refs := s2'.refs|>) =
s2' with <|clock := ck''|>` by fs [state_component_equality]
\\ fs [dec_clock_def];
Theorem EvalM_ArrowM:
EvalM ro env st x1 ((ArrowM ro H (PURE a) b) f) H ==>
EvalM ro env st x2 (PURE a x) H ==>
EvalM ro env st (App Opapp [x1;x2]) (b (f x)) ^H
Proof
EvalM_Arrow_tac
QED
Theorem EvalM_ArrowM_EqSt:
EvalM ro env st x1 ((ArrowM ro H (EqSt (PURE a) st) b) f) H ==>
EvalM ro env st x2 (PURE a x) H ==>
EvalM ro env st (App Opapp [x1;x2]) (b (f x)) ^H
Proof
EvalM_Arrow_tac
QED
Theorem EvalM_ArrowM_Eq:
EvalM ro env st x1 ((ArrowM ro H (PURE (Eq a x)) b) f) H ==>
EvalM ro env st x2 (PURE a x) H ==>
EvalM ro env st (App Opapp [x1;x2]) (b (f x)) ^H
Proof
EvalM_Arrow_tac
QED
Theorem EvalM_ArrowM_EqSt_Eq:
EvalM ro env st x1 ((ArrowM ro H (EqSt (PURE (Eq a x)) st) b) f) H ==>
EvalM ro env st x2 (PURE a x) H ==>
EvalM ro env st (App Opapp [x1;x2]) (b (f x)) ^H
Proof
EvalM_Arrow_tac
QED
Theorem EvalM_Fun:
(!v x. a x v ==> EvalM ro (write name v env) n_st body (b (f x)) H) ==>
EvalM ro env st (Fun name body) (ArrowM ro H (EqSt (PURE a) n_st) b f) ^H
Proof
rw[EvalM_def,ArrowM_def,ArrowP_def,PURE_def,Eq_def,evaluate_def,
EqSt_def,PULL_EXISTS] \\ fs [PULL_FORALL]
\\ qexists_tac `s.clock`
\\ fs [REFS_PRED_FRAME_same] \\ rw []
\\ fs [do_opapp_def,GSYM PULL_FORALL] \\ simp [PULL_EXISTS]
\\ strip_tac
\\ first_x_assum drule
\\ `REFS_PRED H n_st (s1 with refs := s1.refs ++ junk)` by
(drule REFS_PRED_append \\ rw[])
\\ disch_then drule
\\ strip_tac \\ fs [write_def]
\\ asm_exists_tac \\ fs []
\\ asm_exists_tac \\ fs []
\\ drule REFS_PRED_FRAME_remove_junk \\ fs[]
QED
Theorem EvalM_Fun_Var_intro:
EvalM ro cl_env st (Fun n exp) (PURE P f) H ==>
∀name. LOOKUP_VAR name env (Closure cl_env n exp) ==>
EvalM ro env st (Var (Short name)) (PURE P f) ^H
Proof
fs[EvalM_def, PURE_def, LOOKUP_VAR_def, evaluate_def,
PULL_EXISTS, lookup_var_def]
QED
Theorem EvalM_Fun_Eq:
(!v. a x v ==> EvalM ro (write name v env) n_st body (b (f x)) H) ==>
EvalM ro env st (Fun name body)
((ArrowM ro H (EqSt (PURE (Eq a x)) n_st) b) f) ^H
Proof
rw[EvalM_def,ArrowM_def,ArrowP_def,PURE_def,Eq_def, evaluate_def,EqSt_def]
\\ qexists_tac `s.clock` \\ fs []
\\ `(s with clock := s.clock) = s` by simp [state_component_equality]
\\ fs [REFS_PRED_FRAME_same,PULL_EXISTS] \\ rw []
\\ fs [do_opapp_def,GSYM PULL_FORALL] \\ simp [PULL_EXISTS] \\ rw []
\\ first_x_assum drule
\\ `REFS_PRED H n_st (s1 with refs := s1.refs ++ junk)` by
(drule REFS_PRED_append \\ rw[])
\\ disch_then drule
\\ strip_tac \\ fs [write_def]
\\ asm_exists_tac \\ fs []
\\ asm_exists_tac \\ fs []
\\ drule REFS_PRED_FRAME_remove_junk \\ fs[]
QED
(* More proofs *)
Theorem LOOKUP_VAR_EvalM_ArrowM_IMP:
(!st env. LOOKUP_VAR n env v ==>
EvalM ro env st (Var (Short n)) (ArrowM ro H a b f) H) ==>
ArrowP ro ^H a b f v
Proof
fs [LOOKUP_VAR_def,lookup_var_def,EvalM_def,ArrowP_def,
ArrowM_def,PURE_def,AND_IMP_INTRO,
evaluate_def, PULL_EXISTS, VALID_REFS_PRED_def]
\\ `nsLookup (<|v := nsBind n v nsEmpty|>).v (Short n) = SOME v` by EVAL_TAC
\\ rw[] \\ first_x_assum drule \\ rw[]
\\ drule REFS_PRED_append \\ rw[]
\\ first_x_assum drule \\ rw[]
\\ first_x_assum drule \\ rw[]
QED
Theorem EvalM_Var_SIMP:
EvalM ro (write n x env) st (Var (Short y)) P ^H =
if n = y then EvalM ro (write n x env) st (Var (Short y)) P H
else EvalM ro env st (Var (Short y)) P H
Proof
SIMP_TAC std_ss [EvalM_def] \\ SRW_TAC [] []
\\ ASM_SIMP_TAC (srw_ss()) [evaluate_def,write_def]
QED
Theorem EvalM_Var_SIMP_ArrowM:
(!st. EvalM ro (write nv v env) st (Var (Short n)) (ArrowM ro H a b x) H) =
if nv = n then ArrowP ro H a b x v
else (!st. EvalM ro env st (Var (Short n)) (ArrowM ro H a b x) ^H)
Proof
SIMP_TAC std_ss [EvalM_def, ArrowM_def, VALID_REFS_PRED_def]
\\ reverse (SRW_TAC [] [])
THEN1 fs [evaluate_def,write_def]
\\ simp [PURE_def, evaluate_def,write_def]
\\ rw[ArrowP_def]
\\ EQ_TAC THEN1 metis_tac []
\\ rw []
\\ qexists_tac `s.clock`
\\ fs [REFS_PRED_FRAME_same]
QED
Theorem EvalM_Recclosure_ALT:
!H funs fname name body.
ALL_DISTINCT (MAP (λ(f,x,e). f) funs) ==>
(∀st v.
a n v ==>
EvalM ro (write name v (write_rec funs env2 env2)) st body (b (f n)) H) ==>
LOOKUP_VAR fname env (Recclosure env2 funs fname) ==>
find_recfun fname funs = SOME (name,body) ==>
EvalM ro env st (Var (Short fname)) ((ArrowM ro H (PURE (Eq a n)) b) f) ^H
Proof
rw[write_rec_thm,write_def]
\\ imp_res_tac LOOKUP_VAR_THM
\\ fs[Eval_def, EvalM_def,ArrowM_def, ArrowP_def, PURE_def] \\ rpt strip_tac
\\ first_x_assum(qspec_then`s.refs` STRIP_ASSUME_TAC)
\\ fs [evaluate_def,eval_rel_def,option_case_eq,PULL_EXISTS,Eq_def]
\\ fs[state_component_equality] \\ rveq \\ fs [PULL_FORALL]
\\ qexists_tac `s.clock` \\ rpt gen_tac \\ strip_tac
\\ fs [REFS_PRED_FRAME_same]
\\ strip_tac
\\ first_x_assum drule \\ strip_tac
\\ fs [do_opapp_def,GSYM PULL_FORALL]
\\ strip_tac
\\ old_drule REFS_PRED_append \\ rw[]
\\ first_x_assum drule \\ strip_tac
\\ asm_exists_tac \\ fs []
\\ asm_exists_tac \\ fs []
\\ metis_tac[REFS_PRED_FRAME_remove_junk]
QED
Theorem EvalM_Recclosure_ALT2:
!H funs fname.
A n_st ==>
!name body.
ALL_DISTINCT (MAP (λ(f,x,e). f) funs) ==>
(∀st v.
A st ==>
a n v ==>
EvalM ro (write name v (write_rec funs env2 env2)) st body (b (f n)) H) ==>
LOOKUP_VAR fname env (Recclosure env2 funs fname) ==>
find_recfun fname funs = SOME (name,body) ==>
EvalM ro env st (Var (Short fname))
((ArrowM ro H (EqSt (PURE (Eq a n)) n_st) b) f) ^H
Proof
rw[write_rec_thm,write_def]
\\ imp_res_tac LOOKUP_VAR_THM
\\ fs[Eval_def, EvalM_def,ArrowM_def, ArrowP_def, PURE_def] \\ rpt strip_tac
\\ first_x_assum(qspec_then`s.refs` STRIP_ASSUME_TAC)
\\ fs [evaluate_def,eval_rel_def,option_case_eq,PULL_EXISTS,Eq_def,
EqSt_def,PURE_def]
\\ fs[state_component_equality] \\ rveq \\ fs [PULL_FORALL]
\\ qexists_tac `s.clock` \\ rpt gen_tac \\ strip_tac
\\ fs [REFS_PRED_FRAME_same]
\\ strip_tac
\\ first_x_assum drule \\ strip_tac
\\ first_x_assum drule \\ strip_tac
\\ fs [do_opapp_def,GSYM PULL_FORALL]
\\ strip_tac
\\ old_drule REFS_PRED_append \\ rw[]
\\ first_x_assum drule \\ strip_tac
\\ asm_exists_tac \\ fs []
\\ asm_exists_tac \\ fs []
\\ metis_tac[REFS_PRED_FRAME_remove_junk]
QED
Theorem EvalM_Recclosure_ALT3:
!H funs fname name body.
(∀st v.
A st ==>
a n v ==>
EvalM ro (write name v (write_rec funs env2 env2)) st body (b (f n)) H) ==>
A n_st ==>
ALL_DISTINCT (MAP (λ(f,x,e). f) funs) ==>
LOOKUP_VAR fname env (Recclosure env2 funs fname) ==>
find_recfun fname funs = SOME (name,body) ==>
EvalM ro env st (Var (Short fname))
((ArrowM ro H (EqSt (PURE (Eq a n)) n_st) b) f) ^H
Proof
rw[write_rec_thm,write_def]
\\ imp_res_tac LOOKUP_VAR_THM
\\ fs[Eval_def, EvalM_def,ArrowM_def, ArrowP_def, PURE_def] \\ rpt strip_tac
\\ first_x_assum(qspec_then`s.refs` STRIP_ASSUME_TAC)
\\ fs [evaluate_def,eval_rel_def,option_case_eq,PULL_EXISTS,Eq_def,
EqSt_def,PURE_def]
\\ fs[state_component_equality] \\ rveq \\ fs [PULL_FORALL]
\\ qexists_tac `s.clock` \\ rpt gen_tac \\ strip_tac
\\ fs [REFS_PRED_FRAME_same]
\\ strip_tac
\\ first_x_assum drule \\ strip_tac
\\ first_x_assum drule \\ strip_tac
\\ fs [do_opapp_def,GSYM PULL_FORALL]
\\ strip_tac
\\ old_drule REFS_PRED_append \\ rw[]
\\ first_x_assum drule \\ strip_tac
\\ asm_exists_tac \\ fs []
\\ asm_exists_tac \\ fs []
\\ metis_tac[REFS_PRED_FRAME_remove_junk]
QED
Theorem EvalM_Recclosure:
!H. (!st v. a n v ==>
EvalM ro (write name v (write_rec [(fname,name,body)] env2 env2))
st body (b (f n)) H) ==>
LOOKUP_VAR fname env (Recclosure env2 [(fname,name,body)] fname) ==>
EvalM ro env st (Var (Short fname)) ((ArrowM ro H (PURE (Eq a n)) b) f) ^H
Proof
gen_tac \\ NTAC 2 strip_tac \\ imp_res_tac LOOKUP_VAR_THM
\\ pop_assum mp_tac \\ pop_assum (K ALL_TAC) \\ pop_assum mp_tac
\\ rw[Eval_def,Arrow_def,EvalM_def,ArrowM_def,PURE_def,
ArrowP_def,Eq_def,PULL_EXISTS]
\\ first_x_assum (qspec_then `s.refs` strip_assume_tac)
\\ fs [evaluate_def,option_case_eq,eval_rel_def,state_component_equality]
\\ rveq \\ fs []
\\ qexists_tac `s` \\ fs []
\\ fs [REFS_PRED_FRAME_same]
\\ rw [do_opapp_def,find_recfun_def]
\\ old_drule REFS_PRED_append \\ rw[]
\\ last_x_assum drule \\ rw[]
\\ first_x_assum drule \\ rw[] \\ fs [write_def,write_rec_def,build_rec_env_def]
\\ asm_exists_tac \\ fs []
\\ asm_exists_tac \\ fs []
\\ metis_tac[REFS_PRED_FRAME_remove_junk]
QED
Theorem EvalM_Eq_Recclosure:
LOOKUP_VAR name env (Recclosure x1 x2 x3) ==>
(ArrowP ro H a b f (Recclosure x1 x2 x3) =
(!st. EvalM ro env st (Var (Short name)) (ArrowM ro H a b f) ^H))
Proof
rw[EvalM_Var_SIMP, EvalM_def, ArrowM_def, LOOKUP_VAR_def, lookup_var_def,
PURE_def, PULL_EXISTS, evaluate_def]
\\ eq_tac
THEN1 (rw[] \\ qexists_tac `s.clock` \\ fs [REFS_PRED_FRAME_same])
\\ rw []
\\ simp[ArrowP_def,PULL_EXISTS]
\\ rpt gen_tac \\ strip_tac
\\ first_x_assum drule \\ strip_tac
\\ fs [ArrowP_def]
QED
Theorem EvalM_Var_ArrowP:
(!st. EvalM ro env st (Var (Short n)) (ArrowM ro H (PURE a) b x) H) ==>
LOOKUP_VAR n env v ==>
ArrowP ro ^H (PURE a) b x v
Proof
rw[EvalM_def,evaluate_def]
\\ fs[ArrowP_def, ArrowM_def,PURE_def,PULL_EXISTS,evaluate_def,option_case_eq]
\\ rw [] \\ fs [LOOKUP_VAR_def,lookup_var_def]
\\ first_x_assum drule \\ strip_tac
\\ first_x_assum drule
\\ disch_then drule
\\ strip_tac \\ fs []
QED
Theorem EvalM_Var_ArrowP_EqSt:
(!st. EvalM ro env st (Var (Short n)) (ArrowM ro H (EqSt (PURE a) n_st) b x) H) ==>
LOOKUP_VAR n env v ==>
ArrowP ro ^H (EqSt (PURE a) n_st) b x v
Proof
rw[EvalM_def,evaluate_def]
\\ fs[ArrowP_def, ArrowM_def,PURE_def,PULL_EXISTS,evaluate_def,
option_case_eq,EqSt_def]
\\ rw [] \\ fs [LOOKUP_VAR_def,lookup_var_def]
\\ first_x_assum drule \\ strip_tac
\\ first_x_assum drule
\\ disch_then drule
\\ strip_tac \\ fs []
QED
(* Eq simps *)
Theorem EvalM_FUN_FORALL:
(!x. EvalM ro env st exp (PURE (P x) f) H) ==>
EvalM ro env st exp (PURE (FUN_FORALL x. P x) f) ^H
Proof
rw[EvalM_def,PURE_def,PULL_EXISTS]
\\ first_x_assum drule
\\ simp[PULL_EXISTS,FUN_FORALL]
\\ strip_tac
\\ first_assum(qspecl_then[`ARB`]strip_assume_tac)
\\ asm_exists_tac \\ simp[]
\\ qx_gen_tac`x`
\\ first_assum(qspecl_then[`x`]strip_assume_tac)
\\ drule evaluate_add_to_clock \\ fs []
\\ qpat_x_assum `evaluate _ _ _ = _` kall_tac
\\ drule evaluate_add_to_clock \\ fs []
\\ disch_then (qspec_then `ck'` strip_assume_tac)
\\ disch_then (qspec_then `ck` strip_assume_tac)
\\ fs []
QED
Theorem EvalM_FUN_FORALL_EQ:
(!x. EvalM ro env st exp (PURE (P x) f) H) =
EvalM ro env st exp (PURE (FUN_FORALL x. P x) f) ^H
Proof
REPEAT strip_tac \\ EQ_TAC \\ FULL_SIMP_TAC std_ss [EvalM_FUN_FORALL]
\\ fs [EvalM_def,PURE_def,PULL_EXISTS,FUN_FORALL] \\ METIS_TAC []
QED
Triviality M_FUN_FORALL_PUSH1:
(FUN_FORALL x. ArrowP ro ^H a (PURE (b x))) =
(ArrowP ro H a (PURE (FUN_FORALL x. b x)))
Proof
rw[FUN_EQ_THM,FUN_FORALL,ArrowP_def,PURE_def,PULL_EXISTS]
\\ reverse EQ_TAC >- METIS_TAC[] \\ rw[]
\\ first_x_assum drule \\ rw[]
\\ first_assum(qspec_then`ARB`strip_assume_tac) \\ fs[]
\\ first_assum drule \\ disch_then strip_assume_tac \\ rw[]
\\ old_drule REFS_PRED_append \\ rw[]
\\ first_x_assum drule \\ disch_then strip_assume_tac
\\ fs[]
\\ first_x_assum(qspecl_then[`[]`]strip_assume_tac)
\\ fs[] \\ rw[]
\\ asm_exists_tac \\ fs []
\\ reverse(rw[])
THEN1 (metis_tac[REFS_PRED_FRAME_remove_junk])
\\ first_x_assum(qspecl_then[`y`]strip_assume_tac)
\\ first_x_assum drule \\ disch_then strip_assume_tac
\\ first_x_assum(qspecl_then[`[]`]strip_assume_tac)
\\ fs[] \\ rw[]
\\ drule evaluate_add_to_clock \\ fs []
\\ qpat_x_assum `evaluate _ _ _ = _` kall_tac
\\ drule evaluate_add_to_clock \\ fs []
\\ disch_then (qspec_then `ck'` strip_assume_tac)
\\ disch_then (qspec_then `ck` strip_assume_tac)
\\ fs []
QED
val M_FUN_FORALL_PUSH2 = Q.prove(
`(FUN_FORALL x. ArrowP ro H ((PURE (a x))) b) =
(ArrowP ro ^H (PURE (FUN_EXISTS x. a x)) b)`,
FULL_SIMP_TAC std_ss [ArrowP_def,FUN_EQ_THM,AppReturns_def,
FUN_FORALL,FUN_EXISTS,PURE_def] \\ METIS_TAC []) |> GEN_ALL;
val M_FUN_FORALL_PUSH3 = Q.prove(
`(FUN_FORALL st. ArrowP ro H (EqSt a st) b) =
(ArrowP ro ^H a b)`,
FULL_SIMP_TAC std_ss [ArrowP_def,FUN_EQ_THM,AppReturns_def,
FUN_FORALL,FUN_EXISTS,EqSt_def] \\ METIS_TAC []) |> GEN_ALL;
val FUN_EXISTS_Eq = Q.prove(
`(FUN_EXISTS x. Eq a x) = a`,
SIMP_TAC std_ss [FUN_EQ_THM,FUN_EXISTS,Eq_def]) |> GEN_ALL;
Theorem M_FUN_QUANT_SIMP =
LIST_CONJ [FUN_EXISTS_Eq,M_FUN_FORALL_PUSH1,M_FUN_FORALL_PUSH2,M_FUN_FORALL_PUSH3]
Theorem EvalM_Eq:
EvalM ro env st exp (PURE a x) H ==> EvalM ro env st exp (PURE (Eq a x) x) ^H
Proof
fs[EvalM_def, PURE_def, Eq_def]
QED
Theorem ArrowM_EqSt_elim:
(!st_v. EvalM ro env st exp (ArrowM ro H (EqSt a st_v) b f) H) ==>
EvalM ro env st exp (ArrowM ro H a b f) ^H
Proof
fs[EvalM_def, ArrowP_def, ArrowM_def]
\\ rw[]
\\ first_x_assum drule \\ rw[]
\\ first_assum (qspec_then `st` strip_assume_tac)
\\ asm_exists_tac \\ fs []
\\ fs[PURE_def] \\ rw[]
\\ fs [PULL_EXISTS]
\\ qpat_x_assum `ArrowP ro H (EqSt a st) b f v` mp_tac
\\ rw [ArrowP_def,EqSt_def]
\\ last_x_assum (qspec_then `st1` strip_assume_tac)
\\ `v' = v` by
(drule evaluate_add_to_clock \\ fs []
\\ qpat_x_assum `evaluate _ _ _ = _` kall_tac
\\ drule evaluate_add_to_clock \\ fs []
\\ disch_then (qspec_then `ck'` strip_assume_tac)
\\ disch_then (qspec_then `ck` strip_assume_tac)
\\ fs [] \\ rveq \\ fs [])
\\ rveq \\ fs []
\\ qpat_x_assum `ArrowP ro H _ b f v` mp_tac
\\ rw [ArrowP_def,EqSt_def]
QED
Theorem ArrowP_EqSt_elim:
(!st_v. ArrowP ro H (EqSt a st_v) b f v) ==> ArrowP ro ^H a b f v
Proof
fs[EqSt_def, ArrowP_def, ArrowM_def] \\ metis_tac[]
QED
(* otherwise *)
Theorem EvalM_otherwise:
!H b n.
((a1 ==> EvalM ro env st exp1 (MONAD a b x1) H) /\
(!st i. a2 st ==> EvalM ro (write n i env) st exp2 (MONAD a b x2) H)) ==>
(a1 /\ !st'. (CONTAINER(SND(x1 st) = st') ==> a2 st')) ==>
EvalM ro env st (Handle exp1 [(Pvar n,exp2)])
(MONAD a b (x1 otherwise x2)) ^H
Proof
simp [EvalM_def, EvalM_def, evaluate_def] \\ rpt strip_tac
\\ fs [pair_case_eq,result_case_eq,PULL_EXISTS,PULL_EXISTS]
\\ last_x_assum drule \\ rw[]
\\ Cases_on `x1 st` \\ fs [CONTAINER_def]
\\ last_x_assum drule
\\ fs[otherwise_def,can_pmatch_all_def,pmatch_def]
\\ rename1 `x1 st = (res1,new_state)`
\\ Cases_on `res` THEN1
(rw [] \\ asm_exists_tac \\ fs [MONAD_def]
\\ imp_res_tac evaluate_sing \\ rveq \\ fs []
\\ CASE_TAC \\ fs [])
\\ Q.PAT_X_ASSUM `MONAD xx yy zz t1 t2` mp_tac
\\ SIMP_TAC std_ss [Once MONAD_def] \\ strip_tac
\\ fs [] \\ rfs []
\\ Cases_on `res1` \\ fs []
\\ Cases_on `e` \\ fs [] \\ rveq \\ fs []
\\ imp_res_tac REFS_PRED_FRAME_imp
\\ disch_then drule
\\ fs [EVAL ``ALL_DISTINCT (pat_bindings (Pvar n) [])``]
\\ rename1 `b b1 b_v`
\\ disch_then (qspec_then `b_v` strip_assume_tac)
\\ qpat_x_assum `evaluate _ _ _ = _` mp_tac
\\ drule evaluate_set_clock \\ fs []
\\ disch_then (qspec_then `ck'` strip_assume_tac) \\ strip_tac
\\ asm_exists_tac \\ fs [pmatch_def]
\\ fs [write_def]
\\ imp_res_tac REFS_PRED_FRAME_trans
\\ fs[MONAD_def]
\\ CASE_TAC \\ fs[]
\\ CASE_TAC \\ fs[]
\\ asm_exists_tac \\ fs[]
QED
(* if *)
Theorem EvalM_If:
!H.
(a1 ==> Eval env x1 (BOOL b1)) /\
(a2 ==> EvalM ro env st x2 (a b2) H) /\
(a3 ==> EvalM ro env st x3 (a b3) H) ==>
(a1 /\ (CONTAINER b1 ==> a2) /\ (~CONTAINER b1 ==> a3) ==>
EvalM ro env st (If x1 x2 x3) (a (if b1 then b2 else b3)) ^H)
Proof
rpt strip_tac \\ fs[]
\\ `∀H. EvalM ro env st x1 (PURE BOOL b1) ^H` by metis_tac[Eval_IMP_PURE]
\\ fs[EvalM_def,PURE_def, BOOL_def,PULL_EXISTS]
\\ rpt strip_tac
\\ first_x_assum drule
\\ disch_then strip_assume_tac
\\ simp[evaluate_def,pair_case_eq,result_case_eq,PULL_EXISTS]
\\ fs [CONTAINER_def,PULL_EXISTS]
\\ Cases_on `b1` \\ fs []
\\ imp_res_tac REFS_PRED_FRAME_imp
\\ first_x_assum drule
\\ strip_tac
\\ rename1 `evaluate (_ with clock := ck0) _ _ = _`
\\ qpat_x_assum `evaluate _ _ _ = _` mp_tac
\\ drule evaluate_set_clock \\ fs []
\\ disch_then (qspec_then `ck0` strip_assume_tac)
\\ rw [] \\ asm_exists_tac \\ fs []
\\ fs [do_if_def]
\\ asm_exists_tac \\ fs []
\\ metis_tac[REFS_PRED_FRAME_trans]
QED
(* Let *)
Theorem EvalM_Let:
!H.
Eval env exp (a res) /\
(!v. a res v ==> EvalM ro (write name v env) st body (b (f res)) H) ==>
EvalM ro env st (Let (SOME name) exp body) (b (LET f res)) ^H
Proof
rw[]
\\ old_drule Eval_IMP_PURE \\ rw[]
\\ fs[EvalM_def]
\\ rpt strip_tac
\\ first_x_assum drule
\\ disch_then strip_assume_tac
\\ fs [evaluate_def,GSYM write_def,namespaceTheory.nsOptBind_def,PURE_def,
pair_case_eq,result_case_eq] \\ rveq \\ fs [PULL_EXISTS]
\\ imp_res_tac REFS_PRED_FRAME_imp
\\ first_x_assum drule
\\ disch_then drule
\\ strip_tac
\\ rename1 `evaluate (_ with clock := ck0) _ _ = _`
\\ qpat_x_assum `evaluate _ _ _ = _` mp_tac
\\ drule evaluate_set_clock \\ fs []
\\ disch_then (qspec_then `ck0` strip_assume_tac)
\\ rw [] \\ asm_exists_tac \\ fs []
\\ rw [] \\ asm_exists_tac \\ fs []
\\ metis_tac[REFS_PRED_FRAME_trans]
QED
(* PMATCH *)
Theorem EvalM_PMATCH_NIL:
!H b x xv a.
Eval env x (a xv) ==>
pmatch_all_no_type_error env.c a ([]:(pat # exp) list) /\
(CONTAINER F ==>
EvalM ro env st (Mat x []) (b (PMATCH xv [])) ^H)
Proof
rw[ml_translatorTheory.CONTAINER_def,pmatch_all_no_type_error_def]
QED
Theorem EvalM_PMATCH:
!H b a x xv.
ALL_DISTINCT (pat_bindings pt []) ⇒
(∀v1 v2. pat v1 = pat v2 ⇒ v1 = v2) ⇒
Eval env x (a xv) ⇒
(pt1 xv ⇒ EvalM ro env st (Mat x ys) (b (PMATCH xv yrs)) H) ⇒
EvalPatRel env a pt pat ⇒
(∀env2 vars.
EvalPatBind env a pt pat vars env2 ∧ pt2 vars ⇒
EvalM ro env2 st e (b (res vars)) H) ⇒
pmatch_all_no_type_error env.c a ys ⇒
pmatch_all_no_type_error env.c a ((pt,e)::ys) /\
((∀vars. PMATCH_ROW_COND pat (K T) xv vars ⇒ pt2 vars) ∧
((∀vars. ¬PMATCH_ROW_COND pat (K T) xv vars) ⇒ pt1 xv) ⇒
EvalM ro env st (Mat x ((pt,e)::ys))
(b (PMATCH xv ((PMATCH_ROW pat (K T) res)::yrs))) ^H)
Proof
rpt gen_tac \\ rewrite_tac [AND_IMP_INTRO] \\ strip_tac
\\ conj_asm1_tac
THEN1
(fs [pmatch_all_no_type_error_def,pmatch_no_type_error_def]
\\ fs[EvalPatRel_def] \\ rw [] \\ res_tac
\\ rename [`pmatch env.c refs2`]
\\ pop_assum (qspec_then `refs2` strip_assume_tac)
\\ fs [CaseEq"bool",evaluate_def,CaseEq"match_result"])
\\ rpt (pop_assum mp_tac)
\\ rw[EvalM_def]
\\ drule_then (qspecl_then[‘st’, ‘ro’] mp_tac) Eval_IMP_PURE \\ rw[]
\\ fs[EvalM_def]
\\ rw[evaluate_def,PULL_EXISTS] \\ fs[]
\\ first_x_assum drule
\\ disch_then strip_assume_tac
\\ fs[PURE_def,pair_case_eq,result_case_eq,PULL_EXISTS] \\ rveq
\\ imp_res_tac REFS_PRED_FRAME_imp
\\ reverse (Cases_on`∃vars. PMATCH_ROW_COND pat (K T) xv vars`) \\ fs[]
THEN1
(drule (GEN_ALL pmatch_PMATCH_ROW_COND_No_match)
\\ disch_then drule \\ disch_then drule
\\ simp[] \\ strip_tac
\\ first_x_assum(qspec_then`s`mp_tac)
\\ disch_then drule \\ rw[]
\\ reverse (fs [evaluate_def,pair_case_eq,result_case_eq])
\\ rveq \\ fs []
THEN1
(rename1 `_ = (s4,Rerr err)`
\\ Cases_on `err = Rabort Rtimeout_error`
THEN1
(rveq \\ fs [] \\ asm_exists_tac \\ fs []
\\ fs[PMATCH_def,PMATCH_ROW_def] \\ asm_exists_tac \\ fs [])
\\ drule evaluate_add_to_clock \\ simp []
\\ qpat_x_assum `evaluate _ _ _ = _` kall_tac
\\ drule evaluate_add_to_clock \\ simp []
\\ disch_then (qspec_then `ck'` assume_tac)
\\ disch_then (qspec_then `ck` assume_tac)
\\ fs [])
\\ drule evaluate_sing \\ rw []
\\ asm_exists_tac \\ fs [CaseEq"bool"]
\\ rename1 `_ = (_,Rval [v2])`
\\ `v2 = v` by
(drule evaluate_add_to_clock \\ simp []
\\ qpat_x_assum `evaluate _ _ _ = _` kall_tac
\\ drule evaluate_add_to_clock \\ simp []
\\ disch_then (qspec_then `ck'` assume_tac)
\\ disch_then (qspec_then `ck` assume_tac)
\\ fs [])
\\ rveq \\ fs []
\\ fs[PMATCH_def,PMATCH_ROW_def]
\\ drule pmatch_all_no_type_error_IMP_can_pmatch_all
\\ disch_then drule \\ fs [] \\ rw [] \\ rfs []
THEN1 (asm_exists_tac \\ fs [])
\\ metis_tac [can_pmatch_all_EVERY,EVERY_DEF])
\\ imp_res_tac pmatch_PMATCH_ROW_COND_Match
\\ fs[EvalPatRel_def]
\\ fs[PMATCH_ROW_COND_def]
\\ first_x_assum(qspec_then`vars`mp_tac)\\simp[] \\ strip_tac
\\ first_x_assum (qspec_then `s2.refs` strip_assume_tac)
\\ fs [] \\ rveq \\ fs []
\\ `EvalPatBind env a pt pat vars (env with v := nsAppend (alist_to_ns env2) env.v)`
by (
simp[EvalPatBind_def,sem_env_component_equality]
\\ qexists_tac `v` \\ fs[]
\\ qspecl_then[`s2.refs`,`pt`,`v`,`[]`,`env`]
mp_tac(CONJUNCT1 pmatch_imp_Pmatch)
\\ simp[]
\\ metis_tac[])
\\ first_x_assum drule
\\ disch_then drule
\\ disch_then drule
\\ strip_tac
\\ simp[PMATCH_def,PMATCH_ROW_def,PMATCH_ROW_COND_def]
\\ `(some x. pat x = pat vars) = SOME vars` by
(simp[optionTheory.some_def] \\ metis_tac[]) \\ fs []
\\ qpat_x_assum `_ = (s2,Rval [v])` assume_tac