-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathml_translatorScript.sml
3140 lines (2809 loc) · 95.9 KB
/
ml_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
(*
This script defines Eval and other core definitions used by the
translator. The theorems about Eval serve as an interface between
the source semantics and the translator's automation.
*)
open integerTheory ml_progTheory
astTheory semanticPrimitivesTheory
semanticPrimitivesPropsTheory evaluatePropsTheory
fpOptTheory fpValTreeTheory fpSemTheory fpSemPropsTheory;
open mlvectorTheory mlstringTheory packLib;
open integer_wordSyntax
open evaluateTheory
local open integer_wordSyntax in end;
open preamble;
val _ = new_theory "ml_translator";
infix \\ val op \\ = op THEN;
Type state = ``:'ffi semanticPrimitives$state``
(* Definitions *)
Definition empty_state_def:
empty_state = <|
clock := 0;
refs := empty_store;
(* force the ffi state to unit
the monadic translator must be used for FFI calls *)
ffi := initial_ffi_state ARB ();
next_type_stamp := 0;
next_exn_stamp := 0;
fp_state := <|
rws := []; canOpt := FPScope Opt; choices := 0;
opts := \x.[];
real_sem := F |>;
eval_state := NONE|>
End
Definition Eval_def:
Eval env exp P =
!refs. ?res refs'.
eval_rel (empty_state with refs := refs) env exp
(empty_state with refs := refs ++ refs') res /\
P (res:v)
End
Definition AppReturns_def: (* think of this as a Hoare triple {P} cl {Q} *)
AppReturns P cl Q =
!v. P v ==>
!refs. ?env exp refs' u.
do_opapp [cl;v] = SOME (env,exp) /\
eval_rel (empty_state with refs := refs) env exp
(empty_state with refs := refs++refs') u /\
Q u
End
Definition Arrow_def:
Arrow a b =
\f v. !x. AppReturns (a x) v (b (f x))
End
Overload "-->" = ``Arrow``
Definition Eq_def:
Eq (abs:'a->v->bool) x =
(\y v. (x = y) /\ abs y v)
End
Definition And_def:
And a P x v = (P x /\ a (x:'a) (v:v))
End
Definition UNIT_TYPE_def:
UNIT_TYPE (u:unit) (v:v) = (v = Conv NONE [])
End
Definition INT_def:
INT i = \v:v. (v = Litv (IntLit i))
End
Definition NUM_def:
NUM n = INT (& n)
End
Definition BOOL_def:
BOOL b = \v:v. (v = Boolv b)
End
Definition WORD_def:
WORD (w:'a word) =
\v:v. dimindex (:'a) <= 64 /\
(v = Litv (if dimindex (:'a) <= 8
then Word8 (w2w w << (8 - dimindex (:'a)))
else Word64 (w2w w << (64 - dimindex (:'a)))))
End
Definition CHAR_def:
CHAR (c:char) = \v:v. (v = Litv (Char c))
End
Definition STRING_TYPE_def:
STRING_TYPE (strlit s) = \v:v. (v = Litv (StrLit s))
End
Theorem STRING_TYPE_explode:
STRING_TYPE s = \v. (v = Litv (StrLit (explode s)))
Proof
Cases_on`s` \\ rw[STRING_TYPE_def]
QED
Theorem explode_eq:
explode s = l <=> s = strlit l
Proof
rw[EQ_IMP_THM]
\\ rw[GSYM mlstringTheory.implode_def]
QED
Theorem eq_explode:
l = explode s <=> s = strlit l
Proof
rw[EQ_IMP_THM]
\\ rw[GSYM mlstringTheory.implode_def]
QED
Definition HOL_STRING_TYPE_def:
HOL_STRING_TYPE cs = STRING_TYPE (implode cs)
End
Definition CONTAINER_def:
CONTAINER x = x
End
Definition TAG_def:
TAG n x = x
End
Definition PRECONDITION_def:
PRECONDITION b = (b:bool)
End
Definition PreImp_def:
PreImp b1 b2 = (PRECONDITION b1 ==> b2)
End
Definition PreImpEval_def:
PreImpEval b env code P = PreImp b (Eval env code P)
End
(* Theorems *)
Theorem AppReturns_thm:
AppReturns P cl Q ⇔
∀v. P v ⇒
∃env exp.
do_opapp [cl;v] = SOME (env,exp) ∧
∀refs.
∃refs' u.
eval_rel (empty_state with refs := refs) env exp
(empty_state with refs := refs++refs') u ∧
Q u
Proof
fs [AppReturns_def] \\ eq_tac \\ rw []
\\ first_x_assum drule
\\ Cases_on ‘cl’ \\ fs [do_opapp_def,AllCaseEqs()]
\\ rename [‘find_recfun x1 x2’]
\\ Cases_on ‘find_recfun x1 x2’ \\ fs []
\\ PairCases_on ‘x’ \\ fs []
\\ rename [‘ALL_DISTINCT xx’]
\\ Cases_on ‘ALL_DISTINCT xx’ \\ fs []
QED
local
val Eval_lemma = prove(
``∀env exp P.
Eval env exp P ⇔
∀refs.
∃ck1 res refs' ck2.
evaluate (empty_state with <|clock := ck1; refs := refs|>)
env [exp] =
(empty_state with <|clock := ck2; refs := refs ⧺ refs'|>,
Rval [res]) ∧ P res``,
metis_tac [Eval_def |> SIMP_RULE (srw_ss()) [eval_rel_def,PULL_EXISTS]]);
in
val Eval_rw = CONJ evaluate_def Eval_lemma
end;
Theorem fp_state_eq_thm[local]:
s with <| clock := ck1; refs := refsN; fp_state := s.fp_state |> =
s with <| clock := ck1; refs := refsN |>
Proof
fs[state_component_equality]
QED
Theorem evaluate_empty_state_IMP:
eval_rel (empty_state with refs := s.refs) env exp (empty_state with refs := s.refs ++ refs') x ⇒
eval_rel (s:'ffi state) env exp (s with refs := s.refs ++ refs') x
Proof
rw [eval_rel_def]
\\ drule (CONJUNCT1 evaluatePropsTheory.evaluate_fp_intro_canOpt_true)
\\ disch_then (qspec_then `s.fp_state` mp_tac) \\ impl_tac
>- (fs[fpState_component_equality, state_component_equality, empty_state_def])
\\ strip_tac
\\ dxrule_then (qspec_then `s` mp_tac) evaluatePropsTheory.evaluate_ffi_etc_intro
\\ simp [empty_state_def]
QED
Theorem Eval_Arrow:
Eval env x1 ((a --> b) f) ==>
Eval env x2 (a x) ==>
Eval env (App Opapp [x1;x2]) (b (f x))
Proof
rw[Eval_rw,Arrow_def,AppReturns_def]
\\ pop_assum (qspec_then `refs` strip_assume_tac) \\ fs []
\\ drule evaluate_add_to_clock
\\ first_x_assum (qspec_then `refs ++ refs'` strip_assume_tac) \\ fs []
\\ drule evaluate_add_to_clock
\\ first_x_assum drule
\\ disch_then (qspec_then `refs ++ refs' ++ refs''` strip_assume_tac)
\\ fs [eval_rel_def]
\\ disch_then (qspec_then `ck2+1+ck1''` strip_assume_tac)
\\ disch_then (qspec_then `ck1'+1+ck1''` strip_assume_tac) \\ fs []
\\ qexists_tac `ck1+ck1'+1+ck1''` \\ fs []
\\ fs [evaluateTheory.dec_clock_def,eval_rel_def]
\\ ntac 2 (pop_assum kall_tac)
\\ drule evaluate_add_to_clock \\ fs []
\\ fs [empty_state_def,state_component_equality]
QED
Theorem Eval_Fun:
(!v x. a x v ==> Eval (write name v env) body (b ((f:'a->'b) x))) ==>
Eval env (Fun name body) ((a --> b) f)
Proof
rw[Eval_rw,Arrow_def,AppReturns_def]
\\ fs [empty_state_def,state_component_equality]
\\ rw [] \\ first_x_assum drule
\\ disch_then (qspec_then `refs` strip_assume_tac)
\\ fs [do_opapp_def,eval_rel_def,PULL_EXISTS]
\\ metis_tac [write_def]
QED
Theorem Eval_Fun_Eq:
(!v. a x v ==> Eval (write name v env) body (b (f x))) ==>
Eval env (Fun name body) ((Eq a x --> b) f)
Proof
rw[Eval_rw,Arrow_def,AppReturns_def]
\\ fs [empty_state_def,state_component_equality,Eq_def]
\\ rw [] \\ first_x_assum drule
\\ disch_then (qspec_then `refs` strip_assume_tac)
\\ fs [do_opapp_def,eval_rel_def,PULL_EXISTS]
\\ metis_tac [write_def]
QED
Theorem And_IMP_Eq:
Eval env exp ((And a P --> b) f) ==>
(P x ==> Eval env exp ((Eq a x --> b) f))
Proof
fs [Eval_rw,Arrow_def,AppReturns_def,And_def,Eq_def] \\ metis_tac []
QED
Theorem Eq_IMP_And:
(!x. P x ==> Eval env (Fun name exp) ((Eq a x --> b) f)) ==>
Eval env (Fun name exp) ((And a P --> b) f)
Proof
simp[Eval_rw,Arrow_def,AppReturns_def,And_def,Eq_def]
\\ fs[state_component_equality]
QED
Theorem Eval_Fun_And:
(!v x. P x ==> a x v ==> Eval (write name v env) body (b (f x))) ==>
Eval env (Fun name body) ((And a P --> b) f)
Proof
fs [GSYM And_def,AND_IMP_INTRO]
\\ rw [] \\ match_mp_tac Eval_Fun \\ simp []
QED
Theorem Eval_Let:
Eval env exp (a res) /\
(!v. a res v ==> Eval (write name v env) body (b (f res))) ==>
Eval env (Let (SOME name) exp body) (b (LET f res))
Proof
rw[Eval_rw,write_def]
\\ last_x_assum (qspec_then `refs` strip_assume_tac)
\\ drule evaluate_add_to_clock
\\ first_x_assum drule
\\ disch_then (qspec_then `refs++refs'` strip_assume_tac)
\\ drule evaluate_add_to_clock
\\ disch_then (qspec_then `ck2` strip_assume_tac)
\\ disch_then (qspec_then `ck1'` strip_assume_tac)
\\ fs [] \\ qexists_tac `ck1+ck1'`
\\ fs [namespaceTheory.nsOptBind_def]
\\ fs [empty_state_def,state_component_equality]
QED
Theorem Eval_Var_general:
P v ==> !iden. nsLookup env.v iden = SOME v ==> Eval env (Var iden) P
Proof
fs [Eval_rw,state_component_equality]
QED
Theorem Eval_Var_Short:
P v ==> !name env.
(nsLookup env.v (Short name) = SOME v) ==>
Eval env (Var (Short name)) P
Proof
fs [Eval_Var_general]
QED
Theorem Eval_Var_Long:
P v ==> !m name env.
(nsLookup env.v (Long m (Short name)) = SOME v) ==>
Eval env (Var (Long m (Short name))) P
Proof
fs [Eval_Var_general]
QED
Theorem Eval_Var_SWAP_ENV:
!env1.
Eval env1 (Var (Short name)) P /\
(lookup_var name env = lookup_var name env1) ==>
Eval env (Var (Short name)) P
Proof
fs [FORALL_PROD,lookup_var_def,Eval_rw]
QED
Definition LOOKUP_VAR_def:
LOOKUP_VAR name env x = (lookup_var name env = SOME x)
End
Theorem LOOKUP_VAR_THM:
LOOKUP_VAR name env x ==> Eval env (Var (Short name)) ($= x)
Proof
fs [FORALL_PROD,lookup_var_def,Eval_rw,LOOKUP_VAR_def]
\\ fs [state_component_equality]
QED
Theorem LOOKUP_VAR_SIMP:
LOOKUP_VAR name (write x v env) y =
if x = name then (v = y) else LOOKUP_VAR name env y
Proof
simp [LOOKUP_VAR_def,write_def,lookup_var_def] \\ rw []
QED
Theorem Eval_Val_INT:
!n. Eval env (Lit (IntLit n)) (INT n)
Proof
simp [Eval_rw,state_component_equality,INT_def]
QED
Theorem Eval_Val_NUM:
!n. Eval env (Lit (IntLit (&n))) (NUM n)
Proof
simp [Eval_rw,state_component_equality,INT_def,NUM_def]
QED
Theorem Eval_Val_UNIT:
Eval env (Con NONE []) (UNIT_TYPE ())
Proof
simp [Eval_rw,state_component_equality,UNIT_TYPE_def]
\\ fs [EVAL ``do_con_check env.c NONE 0``,state_component_equality,
EVAL ``build_conv env.c NONE []``]
QED
Theorem Eval_Val_BOOL_T:
Eval env (App (Opb Leq) [Lit (IntLit 0); Lit (IntLit 0)]) (BOOL T)
Proof
fs [Eval_rw,do_app_def,empty_state_def,state_component_equality]
\\ EVAL_TAC
QED
Theorem Eval_Val_BOOL_F:
Eval env (App (Opb Lt) [Lit (IntLit 0); Lit (IntLit 0)]) (BOOL F)
Proof
fs [Eval_rw,do_app_def,empty_state_def,state_component_equality]
\\ EVAL_TAC
QED
Theorem Eval_Val_CHAR:
!c. Eval env (Lit (Char c)) (CHAR c)
Proof
fs [Eval_rw,empty_state_def,state_component_equality,CHAR_def]
QED
Theorem Eval_Val_STRING:
!s. Eval env (Lit (StrLit s)) (STRING_TYPE (strlit s))
Proof
fs [Eval_rw,empty_state_def,state_component_equality,STRING_TYPE_def]
QED
Theorem Eval_Val_WORD:
!w:'a word.
dimindex(:'a) ≤ 64 ⇒
Eval env (Lit (if dimindex(:'a) ≤ 8
then Word8 (w2w w << (8-dimindex(:'a)))
else Word64 (w2w w << (64-dimindex(:'a)))))
(WORD w)
Proof
simp [WORD_def,Eval_rw,state_component_equality]
QED
(* Equality *)
Definition no_closures_def:
(no_closures (Conv _ vs) = EVERY no_closures vs) /\
(no_closures (Vectorv vs) = EVERY no_closures vs) /\
(no_closures (Closure _ _ _) = F) /\
(no_closures (Recclosure _ _ _) = F) /\
(no_closures (Env _ _) = F) /\
(no_closures _ = T)
Termination
WF_REL_TAC `measure v_size` \\ REPEAT STRIP_TAC
\\ Induct_on `vs` \\ FULL_SIMP_TAC (srw_ss()) [MEM]
\\ REPEAT STRIP_TAC \\ FULL_SIMP_TAC (srw_ss()) [MEM,v_size_def]
\\ DECIDE_TAC
End
Definition types_match_def:
(types_match (Litv l1) (Litv l2) = lit_same_type l1 l2) /\
(types_match (Loc b1 l1) (Loc b2 l2) = (b1 ∧ b2)) /\
(types_match (Conv cn1 vs1) (Conv cn2 vs2) =
(ctor_same_type cn1 cn2 /\ ((cn1 = cn2) ⇒ types_match_list vs1 vs2))) /\
(types_match (FP_WordTree (Fp_const w1)) (FP_WordTree (Fp_const w2)) = T) /\
(types_match _ _ = F) /\
(types_match_list [] [] = T) /\
(types_match_list (v1::vs1) (v2::vs2) =
(types_match v1 v2 /\ types_match_list vs1 vs2)) /\
(* We could change this case to T, or change the semantics to have a type error
* when equality reaches unequal-length lists *)
(types_match_list _ _ = F)
Termination
WF_REL_TAC `measure (\x. case x of INL (v1,v2) => v_size v1 |
INR (vs1,vs2) => v1_size vs1)`
End
Definition EqualityType_def:
EqualityType (abs:'a->v->bool) <=>
(!x1 v1. abs x1 v1 ==> no_closures v1) /\
(!x1 v1 x2 v2. abs x1 v1 /\ abs x2 v2 ==> ((v1 = v2) = (x1 = x2))) /\
(!x1 v1 x2 v2. abs x1 v1 /\ abs x2 v2 ==> types_match v1 v2)
End
Triviality LSL_n2w_eq:
a < 2 ** (dimindex (:'a) - n) /\ b < 2 ** (dimindex (:'a) - n) /\
n <= dimindex (:'a) ==>
((n2w a ≪ n) = ((n2w b : 'a word) ≪ n) <=> a = b)
Proof
rw [WORD_MUL_LSL, word_mul_n2w]
\\ qsuff_tac `(a * 2 ** n) < dimword (:'a) /\ (b * 2 ** n) < dimword (:'a)`
\\ simp []
\\ qsuff_tac `(2 : num) ** dimindex(:'a) = (2 ** (dimindex (:α) − n)) * (2 ** n)`
\\ simp_tac bool_ss [dimword_def]
\\ simp []
\\ simp [GSYM EXP_ADD]
QED
Theorem EqualityType_NUM_BOOL:
EqualityType NUM /\ EqualityType INT /\
EqualityType BOOL /\ EqualityType WORD /\
EqualityType CHAR /\ EqualityType STRING_TYPE /\
EqualityType UNIT_TYPE /\ EqualityType HOL_STRING_TYPE /\
EqualityType WORD
Proof
EVAL_TAC \\ fs [no_closures_def,
types_match_def, lit_same_type_def,
stringTheory.ORD_11,mlstringTheory.explode_11]
\\ SRW_TAC [] [] \\ EVAL_TAC
\\ fs [w2w_def] \\ Cases_on `x1`
\\ fs[STRING_TYPE_def] \\ EVAL_TAC
\\ Cases_on `x2` \\ fs[STRING_TYPE_def] \\ EVAL_TAC
\\ DEP_REWRITE_TAC [LSL_n2w_eq]
\\ simp [GSYM dimword_def]
QED
Definition EqualityType_at_def:
EqualityType_at (abs:'a->v->bool) x <=>
(!v. abs x v ==> no_closures v) /\
(!v x2 v2. abs x v /\ abs x2 v2 ==> ((v = v2) = (x = x2))) /\
(!v x2 v2. abs x v /\ abs x2 v2 ==> types_match v v2)
End
Theorem EqualityType_eq_at:
EqualityType TY = (!x. EqualityType_at TY x)
Proof
simp [EqualityType_def, EqualityType_at_def]
\\ metis_tac []
QED
Theorem EqualityType_at_eq_Case_rearranged:
EqualityType_at TY x <=>
!y vx vy. Case (x, y, vx, vy) ==>
TY x vx /\ TY y vy ==>
(x = y ==> vx = vy ==> no_closures vx)
/\ (vx = vy <=> x = y) /\ types_match vx vy
Proof
fs [EqualityType_at_def, markerTheory.Case_def] \\ metis_tac []
QED
Theorem EqualityType_def_rearranged:
EqualityType abs = (!x y vx vy. abs x vx /\ abs y vy
==> (x = y ==> vx = vy ==> no_closures vx)
/\ (vx = vy <=> x = y) /\ types_match vx vy)
Proof
fs [EqualityType_def] \\ metis_tac []
QED
Theorem EqualityType_from_ONTO:
(!a. ?r. (a = num2a r) ∧ r < (N : num))
==> (!TY stamps stn. (GENLIST (\n v. TY (num2a n) v) N
= MAP (\st v. v = Conv (SOME (TypeStamp st stn)) []) stamps)
==> ALL_DISTINCT stamps
==> EqualityType TY)
Proof
rpt strip_tac
\\ fs [EqualityType_def_rearranged]
\\ rpt GEN_TAC
\\ FIRST_X_ASSUM (fn a => ((dest_exists o snd o dest_forall o concl) a;
ASSUME_TAC (CONJ (Q.SPEC `x` a) (Q.SPEC `y` a))))
\\ fs []
\\ FIRST_X_ASSUM (fn a => MP_TAC (LIST_CONJ [Q.AP_TERM `LENGTH` a,
Q.AP_TERM `EL r` a, Q.AP_TERM `EL r'` a]))
\\ fs [EL_MAP, satTheory.AND_IMP, FUN_EQ_THM, no_closures_def,
types_match_def, ctor_same_type_def, listTheory.EL_ALL_DISTINCT_EL_EQ,
same_type_def]
\\ metis_tac (map TypeBase.one_one_of [``:stamp``, ``:'a option``, ``: v``])
QED
Theorem EqualityType_from_ONTO_Exn:
(!a. ?r. (a = num2a r) ∧ r < (N : num))
==> (!TY stamps. (GENLIST (\n v. TY (num2a n) v) N
= MAP (\st v. v = Conv (SOME (ExnStamp st)) []) stamps)
==> ALL_DISTINCT stamps
==> EqualityType TY)
Proof
rpt strip_tac
\\ fs [EqualityType_def_rearranged]
\\ rpt GEN_TAC
\\ FIRST_X_ASSUM (fn a => ((dest_exists o snd o dest_forall o concl) a;
ASSUME_TAC (CONJ (Q.SPEC `x` a) (Q.SPEC `y` a))))
\\ fs []
\\ FIRST_X_ASSUM (fn a => MP_TAC (LIST_CONJ [Q.AP_TERM `LENGTH` a,
Q.AP_TERM `EL r` a, Q.AP_TERM `EL r'` a]))
\\ fs [EL_MAP, satTheory.AND_IMP, FUN_EQ_THM, no_closures_def,
types_match_def, ctor_same_type_def, listTheory.EL_ALL_DISTINCT_EL_EQ,
same_type_def]
\\ metis_tac (map TypeBase.one_one_of [``:stamp``, ``:'a option``, ``: v``])
QED
Definition UNIT_v_def:
UNIT_v (u : unit) = (Conv NONE [])
End
Definition INT_v_def:
INT_v i = Litv (IntLit i)
End
Definition NUM_v_def:
NUM_v n = INT_v (& n)
End
Definition BOOL_v_def:
BOOL_v b = Boolv b
End
Definition WORD_v_def:
WORD_v (w:'a word) =
if dimindex (:'a) <= 8
then Litv (Word8 (w2w w << (8 - dimindex (:'a))))
else if dimindex (:'a) <= 64
then Litv (Word64 (w2w w << (64 - dimindex (:'a))))
else Conv NONE []
End
Definition CHAR_v_def:
CHAR_v (c:char) = Litv (Char c)
End
Definition STRING_v_def:
STRING_v (strlit s) = Litv (StrLit s)
End
Definition HOL_STRING_v_def:
HOL_STRING_v cs = STRING_v (implode cs)
End
Triviality types_match_list_REPLICATE:
!n m. types_match_list (REPLICATE n x) (REPLICATE m y) =
(n = m /\ (0 < n ==> types_match x y))
Proof
Induct \\ simp [] \\ Cases \\ simp [types_match_def]
\\ metis_tac []
QED
(* nearly all types with equality will be fully representable within v *)
Definition IsTypeRep_def:
IsTypeRep f R <=> (!x. R x (f x : v))
End
Theorem IsTypeRep_EqualityType_Unique:
EqualityType R ==> IsTypeRep f R ==> IsTypeRep g R ==>
g = f
Proof
rw [EqualityType_def, FUN_EQ_THM]
\\ rpt (first_x_assum (qspecl_then [`x`, `f x`, `x`, `g x`] mp_tac))
\\ fs [IsTypeRep_def]
QED
Theorem IsTypeRep_EqualityType_INJ:
EqualityType R ==> IsTypeRep f R ==> INJ f UNIV UNIV
Proof
rw [EqualityType_def, pred_setTheory.INJ_DEF]
\\ rpt (first_x_assum (qspecl_then [`x`, `f x`, `y`, `f y`] mp_tac))
\\ fs [IsTypeRep_def]
QED
Definition DUMMY_TYPE_REP_v:
DUMMY_TYPE_REP_v x = UNIT_v ()
End
Theorem IsTypeRep_NUM_BOOL:
IsTypeRep NUM_v NUM /\ IsTypeRep INT_v INT /\
IsTypeRep BOOL_v BOOL /\
IsTypeRep CHAR_v CHAR /\
IsTypeRep UNIT_v UNIT_TYPE /\
IsTypeRep STRING_v STRING_TYPE /\
IsTypeRep HOL_STRING_v HOL_STRING_TYPE /\
(dimindex (:'a) <= 64 ==> IsTypeRep WORD_v (WORD : 'a word -> v -> bool))
Proof
simp [] \\ EVAL_TAC \\ simp []
\\ rpt (conj_tac ORELSE disch_tac)
\\ Cases
\\ EVAL_TAC
\\ rw []
QED
Theorem types_match_list_length:
!vs1 vs2. types_match_list vs1 vs2 ==> LENGTH vs1 = LENGTH vs2
Proof
Induct \\ Cases_on`vs2` \\ rw[types_match_def]
QED
Theorem type_match_implies_do_eq_succeeds:
(!v1 v2. types_match v1 v2 ==> (do_eq v1 v2 = Eq_val (v1 = v2))) /\
(!vs1 vs2.
types_match_list vs1 vs2 ==> (do_eq_list vs1 vs2 = Eq_val (vs1 = vs2)))
Proof
ho_match_mp_tac do_eq_ind
\\ rw [do_eq_def, types_match_def]
\\ imp_res_tac types_match_list_length
\\ fs[] \\ Cases_on`cn1=cn2`\\fs[]
\\ imp_res_tac types_match_list_length
\\ rename1 `types_match (FP_WordTree fp1) (FP_WordTree fp2)`
\\ Cases_on `fp1` \\ Cases_on `fp2` \\ fs[types_match_def, compress_word_def]
QED
Triviality do_eq_succeeds:
(!a x1 v1 x2 v2. EqualityType a /\ a x1 v1 /\ a x2 v2 ==>
(do_eq v1 v2 = Eq_val (x1 = x2)))
Proof
rw [EqualityType_def]
\\ res_tac
\\ imp_res_tac type_match_implies_do_eq_succeeds
\\ Cases_on `v1 = v2`
\\ fs []
QED
Triviality empty_state_with_refs_eq:
empty_state with refs := r =
s2 with <| refs := r'; ffi := f |> ⇔
∃refs ffi.
s2 = empty_state with <| refs := refs; ffi := ffi |> ∧
r' = r ∧ f = empty_state.ffi
Proof
rw[state_component_equality,EQ_IMP_THM]
QED
Triviality empty_state_with_ffi_elim:
empty_state with <| refs := r; ffi := empty_state.ffi |> =
empty_state with refs := r
Proof
rw[state_component_equality]
QED
val Eval2_tac =
first_x_assum (qspec_then `refs` strip_assume_tac)
\\ drule evaluate_add_to_clock
\\ first_x_assum (qspec_then `refs++refs'` strip_assume_tac)
\\ drule evaluate_add_to_clock
\\ disch_then (qspec_then `ck2` strip_assume_tac)
\\ disch_then (qspec_then `ck1'` strip_assume_tac)
\\ fs [] \\ qexists_tac `ck1+ck1'` \\ fs [];
Theorem Eval_Equality:
Eval env x1 (a y1) /\ Eval env x2 (a y2) ==>
EqualityType a ==>
Eval env (App Equality [x1;x2]) (BOOL (y1 = y2))
Proof
simp [Eval_rw,BOOL_def] \\ rw []
\\ Eval2_tac
\\ fs [do_app_def] \\ imp_res_tac do_eq_succeeds \\ fs []
\\ rw[state_component_equality]
QED
(* booleans *)
Theorem Eval_Or:
(a1 ==> Eval env x1 (BOOL b1)) /\
(a2 ==> Eval env x2 (BOOL b2))
==>
(a1 /\ (~CONTAINER b1 ==> a2) ==>
Eval env (Log Or x1 x2) (BOOL (b1 \/ b2)))
Proof
Cases_on `b1`
\\ rw[Eval_rw,BOOL_def,CONTAINER_def] \\ fs []
THEN1
(pop_assum kall_tac
\\ pop_assum (qspec_then `refs` strip_assume_tac)
\\ qexists_tac `ck1`
\\ fs [EVAL``do_log Or (Boolv T) x``]
\\ fs [EVAL``Boolv T``,state_component_equality])
\\ last_x_assum assume_tac
\\ Eval2_tac
\\ fs [EVAL``do_log Or (Boolv F) x``]
\\ fs [EVAL``Boolv F``,state_component_equality]
QED
Theorem Eval_And:
(a1 ==> Eval env x1 (BOOL b1)) /\
(a2 ==> Eval env x2 (BOOL b2))
==>
(a1 /\ (CONTAINER b1 ==> a2) ==>
Eval env (Log And x1 x2) (BOOL (b1 /\ b2)))
Proof
reverse (Cases_on `b1`)
\\ rw[Eval_rw,BOOL_def,CONTAINER_def] \\ fs []
THEN1
(pop_assum kall_tac
\\ pop_assum (qspec_then `refs` strip_assume_tac)
\\ qexists_tac `ck1`
\\ fs [EVAL``do_log And (Boolv F) x``]
\\ fs [EVAL``Boolv F``,state_component_equality])
\\ last_x_assum assume_tac
\\ Eval2_tac
\\ fs [EVAL``do_log And (Boolv T) x``]
\\ fs [EVAL``Boolv F``,state_component_equality]
QED
Theorem Eval_If:
(a1 ==> Eval env x1 (BOOL b1)) /\
(a2 ==> Eval env x2 (a b2)) /\
(a3 ==> Eval env x3 (a b3))
==>
(a1 /\ (CONTAINER b1 ==> a2) /\ (~CONTAINER b1 ==> a3) ==>
Eval env (If x1 x2 x3) (a (if b1 then b2 else b3)))
Proof
rw[Eval_rw,BOOL_def,CONTAINER_def] \\ fs []
\\ qpat_x_assum `_ ==> _` kall_tac
\\ last_x_assum assume_tac
\\ Eval2_tac
\\ fs [EVAL``do_if (Boolv T) x y``,EVAL``do_if (Boolv F) x y``,
state_component_equality]
QED
Theorem Eval_Bool_Not:
Eval env x1 (BOOL b1) ==>
Eval env (App Equality
[x1; App (Opb Lt) [Lit (IntLit 0); Lit (IntLit 0)]]) (BOOL (~b1))
Proof
rw[Eval_rw,BOOL_def,do_app_def,opb_lookup_def]
\\ pop_assum (qspec_then `refs` strip_assume_tac)
\\ qexists_tac `ck1` \\ fs [empty_state_def]
\\ Cases_on `b1` \\ fs []
\\ fs [EVAL``do_eq (Boolv T) (Boolv F)``,EVAL``do_eq (Boolv F) (Boolv F)``]
QED
Theorem Eval_Implies:
Eval env x1 (BOOL b1) ==>
Eval env x2 (BOOL b2) ==>
Eval env (If x1 x2 (App (Opb Leq) [Lit (IntLit 0); Lit (IntLit 0)]))
(BOOL (b1 ==> b2))
Proof
reverse (Cases_on `b1`)
\\ rw[Eval_rw,BOOL_def,CONTAINER_def] \\ fs []
THEN1
(last_assum (qspec_then `refs` strip_assume_tac)
\\ qexists_tac `ck1` \\ fs [EVAL ``do_if (Boolv F) x2 x1``]
\\ fs [Eval_rw,do_app_def,state_component_equality] \\ EVAL_TAC)
\\ last_x_assum assume_tac \\ Eval2_tac
\\ fs [EVAL ``do_if (Boolv T) x2 x1``,state_component_equality]
QED
(* misc *)
Theorem Eval_Var_SIMP:
Eval (write x v env) (Var (Short y)) p =
if x = y then p v else Eval env (Var (Short y)) p
Proof
simp [LOOKUP_VAR_def,write_def,lookup_var_def,Eval_rw]
\\ rw [] \\ fs [state_component_equality]
QED
Theorem Eval_Eq:
Eval env exp (a x) ==> Eval env exp ((Eq a x) x)
Proof
simp [Eval_def,Eq_def]
QED
val FUN_FORALL = new_binder_definition("FUN_FORALL",
``($FUN_FORALL) = \ (abs:'a->'b->v->bool) a v. !y. abs y a v``);
val FUN_EXISTS = new_binder_definition("FUN_EXISTS",
``($FUN_EXISTS) = \ (abs:'a->'b->v->bool) a v. ?y. abs y a v``);
Theorem FUN_FORALL_INTRO:
(!x. p x f v) ==> (FUN_FORALL x. p x) f v
Proof
fs [FUN_FORALL]
QED
Theorem eval_rel_11:
eval_rel s1 env e s2 x2 /\ eval_rel s1 env e s3 x3 ==>
s2 = s3 /\ x2 = x3
Proof
rw [eval_rel_def]
\\ drule evaluate_add_to_clock
\\ qpat_x_assum `evaluate _ _ _ = _` mp_tac
\\ drule evaluate_add_to_clock
\\ disch_then (qspec_then `ck1'` strip_assume_tac)
\\ strip_tac
\\ disch_then (qspec_then `ck1` strip_assume_tac)
\\ fs [state_component_equality]
QED
Theorem Eval_FUN_FORALL:
(!x. Eval env exp ((p x) f)) ==>
Eval env exp ((FUN_FORALL x. p x) f)
Proof
rw[Eval_def,FUN_FORALL]
\\ first_assum (qspecl_then [`ARB`,`refs`] strip_assume_tac)
\\ asm_exists_tac \\ fs [] \\ rw []
\\ first_assum (qspecl_then [`y`,`refs`] strip_assume_tac)
\\ imp_res_tac eval_rel_11 \\ fs []
QED
Theorem Eval_FUN_FORALL_EQ:
(!x. Eval env exp ((p x) f)) =
Eval env exp ((FUN_FORALL x. p x) f)
Proof
REPEAT STRIP_TAC \\ EQ_TAC \\ FULL_SIMP_TAC std_ss [Eval_FUN_FORALL]
\\ EVAL_TAC \\ FULL_SIMP_TAC std_ss [FUN_FORALL] \\ METIS_TAC []
QED
val FUN_FORALL_PUSH1 = Q.prove(
`(FUN_FORALL x. a --> (b x)) = (a --> FUN_FORALL x. b x)`,
rw [Arrow_def,FUN_EQ_THM,AppReturns_def,FUN_FORALL]
\\ METIS_TAC[eval_rel_11,PAIR_EQ,result_11,SOME_11]) |> GEN_ALL;
val FUN_FORALL_PUSH2 = Q.prove(
`(FUN_FORALL x. (a x) --> b) = ((FUN_EXISTS x. a x) --> b)`,
FULL_SIMP_TAC std_ss [Arrow_def,FUN_EQ_THM,AppReturns_def,FUN_FORALL,FUN_EXISTS,
Eval_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 FUN_QUANT_SIMP =
LIST_CONJ [FUN_EXISTS_Eq,FUN_FORALL_PUSH1,FUN_FORALL_PUSH2]
Theorem Eval_Recclosure_ALT:
!funs fname name body.
(ALL_DISTINCT (MAP (\ (f,x,e). f) funs)) ==>
(!v. a n v ==>
Eval (write name v (write_rec funs env2 env2)) body (b (f n))) ==>
LOOKUP_VAR fname env (Recclosure env2 funs fname) ==>
(find_recfun fname funs = SOME (name,body)) ==>
Eval env (Var (Short fname)) ((Eq a n --> b) f)
Proof
rw[write_rec_thm,write_def]
\\ IMP_RES_TAC LOOKUP_VAR_THM
\\ fs[Eval_rw,Arrow_def] \\ REPEAT STRIP_TAC
\\ Cases_on `nsLookup env.v (Short fname)` \\ fs [state_component_equality]
\\ rveq
\\ rw[AppReturns_def,Eq_def,do_opapp_def,PULL_EXISTS]
\\ fs[build_rec_env_def,FOLDR,eval_rel_def]
\\ METIS_TAC[APPEND_ASSOC]
QED
Theorem Eval_Recclosure:
(!v. a n v ==>
Eval (write name v (write_rec [(fname,name,body)] env2 env2))
body (b (f n))) ==>
LOOKUP_VAR fname env (Recclosure env2 [(fname,name,body)] fname) ==>
Eval env (Var (Short fname)) ((Eq a n --> b) f)
Proof
(Eval_Recclosure_ALT |> Q.SPECL [`[(fname,name,body)]`,`fname`]
|> SIMP_RULE (srw_ss()) [Once find_recfun_def] |> ASSUME_TAC)
\\ FULL_SIMP_TAC std_ss []
QED
Definition SafeVar_def:
SafeVar = Var
End
Theorem Eval_Eq_Recclosure:
LOOKUP_VAR name env (Recclosure x1 x2 x3) ==>
(P f (Recclosure x1 x2 x3) =
Eval env (Var (Short name)) (P f))
Proof
simp [Eval_Var_SIMP,Eval_rw,LOOKUP_VAR_def,lookup_var_def]
\\ simp [state_component_equality]
QED
Theorem Eval_Eq_Fun:
Eval env (Fun v x) p ==>
!env2. Eval env2 (Var name) ($= (Closure env v x)) ==>
Eval env2 (Var name) p
Proof
simp [Eval_Var_SIMP,Eval_rw] \\ rw []
\\ Cases_on `nsLookup env2.v name` \\ fs []
\\ fs [state_component_equality]
QED
Theorem Eval_WEAKEN:
Eval env exp P ==> (!v. P v ==> Q v) ==> Eval env exp Q
Proof
simp [Eval_def] \\ metis_tac []
QED
Theorem Eval_CONST:
(!v. P v = (v = x)) ==>
Eval env (Var name) ($= x) ==> Eval env (Var name) P
Proof
simp [Eval_def]
QED
(* arithmetic for integers *)
Triviality Eval_Opn:
!f n1 n2.
Eval env x1 (INT n1) ==>
Eval env x2 (INT n2) ==>
PRECONDITION (((f = Divide) \/ (f = Modulo)) ==> ~(n2 = 0)) ==>
Eval env (App (Opn f) [x1;x2]) (INT (opn_lookup f n1 n2))
Proof
rw[Eval_rw,INT_def,PRECONDITION_def]
\\ Eval2_tac \\ fs [do_app_def] \\ rw []
\\ fs [state_component_equality]
QED
local
fun f name q =
save_thm("Eval_" ^ name,SIMP_RULE (srw_ss()) [opn_lookup_def,EVAL ``PRECONDITION T``]
(Q.SPEC q Eval_Opn))
in
val Eval_INT_ADD = f "INT_ADD" `Plus`
val Eval_INT_SUB = f "INT_SUB" `Minus`
val Eval_INT_MULT = f "INT_MULT" `Times`
val Eval_INT_DIV = f "INT_DIV" `Divide`
val Eval_INT_MOD = f "INT_MOD" `Modulo`
end;
Triviality Eval_Opb:
!f n1 n2.
Eval env x1 (INT n1) ==>
Eval env x2 (INT n2) ==>
Eval env (App (Opb f) [x1;x2]) (BOOL (opb_lookup f n1 n2))
Proof
rw[Eval_rw,INT_def,PRECONDITION_def,BOOL_def]
\\ Eval2_tac \\ fs [do_app_def] \\ rw []
\\ fs [state_component_equality]
QED
local
fun f name q = let
val th = SIMP_RULE (srw_ss()) [opb_lookup_def] (Q.SPEC q Eval_Opb)
val _ = save_thm("Eval_" ^ name,SPEC_ALL th)
in th end
in
val Eval_INT_LESS = f "INT_LESS" `Lt`
val Eval_INT_LESS_EQ = f "INT_LESS_EQ" `Leq`
val Eval_INT_GREATER = f "INT_GREATER" `Gt`
val Eval_INT_GREATER_EQ = f "INT_GREATER_EQ" `Geq`
end;
Theorem Eval_Num:
Eval env x1 (INT i) ==> PRECONDITION (0 <= i) ==>
Eval env x1 (NUM (Num i))
Proof
SIMP_TAC std_ss [NUM_def,PRECONDITION_def] \\ rw[]
\\ `&Num i = i` by intLib.COOPER_TAC \\ simp[]
QED
local
val th0 = Q.SPEC `0` Eval_Val_INT
val th_sub = MATCH_MP (MATCH_MP Eval_INT_SUB (Q.SPEC `0` Eval_Val_INT))
(ASSUME ``Eval env (Var (Short "k")) (INT k)``)
val th1 = ASSUME ``Eval env (Var (Short "k")) (INT k)``
val th2 = Eval_INT_LESS |> Q.SPECL [`k`,`0`]
|> (fn th => MATCH_MP th th1) |> (fn th => MATCH_MP th th0)
val th = MATCH_MP Eval_If (LIST_CONJ (map (DISCH T) [th2,th_sub,th1]))