Repository navigation
Expand file tree
/
Copy pathsemanticPrimitivesScript.sml
More file actions
1755 lines (1637 loc) · 59.9 KB
/
Copy pathsemanticPrimitivesScript.sml
File metadata and controls
1755 lines (1637 loc) · 59.9 KB
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
(*
Definitions of semantic primitives (e.g., values, and functions for doing
primitive operations) used in the semantics.
*)
Theory semanticPrimitives
Ancestors
misc ast machine_ieee namespace ffi fpSem mlstring
val _ = numLib.temp_prefer_num();
(* Constructors and exceptions need unique identities, which we represent by stamps. *)
Datatype:
stamp =
(* Arguments: constructor name, unique type number, and zero-based constructor
position in the datatype definition (needed for polymorphic compare).
Constructor names must be unique within the type. *)
TypeStamp conN num num
| ExnStamp num
End
Datatype:
sem_env =
<| v : (modN, varN, 'v) namespace
(* Lexical mapping of constructor idents to arity, stamp pairs *)
; c : (modN, conN, (num # stamp)) namespace
|>
End
(* Value forms *)
Datatype:
v =
Litv lit
(* Constructor application. Can be a tuple or a given constructor of a given type *)
| Conv (stamp option) (v list)
(* Function closures
The environment is used for the free variables in the function *)
| Closure (v sem_env) varN exp
(* Function closure for recursive functions
* See Closure and Letrec above
* The last variable name indicates which function from the mutually
* recursive bundle this closure value represents *)
| Recclosure (v sem_env) ((varN # varN # exp) list) varN
| Loc bool (* = is allowed *) num (* address *)
| Vectorv (v list)
(* Environment value for Eval, and its numeric identifier *)
| Env (v sem_env) (num # num)
End
Type env_ctor = ``: (modN, conN, (num # stamp)) namespace``
Type env_val = ``: (modN, varN, v) namespace``
Definition bind_stamp_def:
bind_stamp = ExnStamp 0
End
Definition chr_stamp_def:
chr_stamp = ExnStamp 1
End
Definition div_stamp_def:
div_stamp = ExnStamp 2
End
Definition subscript_stamp_def:
subscript_stamp = ExnStamp 3
End
Definition bind_exn_v_def:
bind_exn_v = Conv (SOME bind_stamp) []
End
Definition chr_exn_v_def:
chr_exn_v = Conv (SOME chr_stamp) []
End
Definition div_exn_v_def:
div_exn_v = Conv (SOME div_stamp) []
End
Definition sub_exn_v_def:
sub_exn_v = Conv (SOME subscript_stamp) []
End
Definition bool_type_num_def:
bool_type_num = 0
End
Definition list_type_num_def:
list_type_num = 1
End
Definition option_type_num_def:
option_type_num = 2
End
Definition lit_type_num_def:
lit_type_num = 3
End
Definition id_type_num_def:
id_type_num = 4
End
Definition ast_t_type_num_def:
ast_t_type_num = 5
End
Definition pat_type_num_def:
pat_type_num = 6
End
Definition lop_type_num_def:
lop_type_num = 7
End
Definition opn_type_num_def:
opn_type_num = 8
End
Definition opb_type_num_def:
opb_type_num = 9
End
Definition opw_type_num_def:
opw_type_num = 10
End
Definition shift_type_num_def:
shift_type_num = 11
End
Definition word_size_type_num_def:
word_size_type_num = 12
End
Definition fp_uop_type_num_def:
fp_uop_type_num = 13
End
Definition fp_bop_type_num_def:
fp_bop_type_num = 14
End
Definition fp_top_type_num_def:
fp_top_type_num = 15
End
Definition fp_cmp_type_num_def:
fp_cmp_type_num = 16
End
Definition op_type_num_def:
op_type_num = 20
End
Definition locn_type_num_def:
locn_type_num = 21
End
Definition locs_type_num_def:
locs_type_num = 22
End
Definition exp_type_num_def:
exp_type_num = 24
End
Definition dec_type_num_def:
dec_type_num = 25
End
(* The result of evaluation *)
Datatype:
abort =
Rtype_error
| Rtimeout_error
| Rffi_error final_event
End
Datatype:
error_result =
Rraise 'a (* Should only be a value of type exn *)
| Rabort abort
End
Datatype:
result =
Rval 'a
| Rerr ('b error_result)
End
(* Stores *)
Datatype:
store_v =
(* A ref cell *)
Refv 'a
(* A byte array *)
| W8array (word8 list)
(* An array of values *)
| Varray ('a list)
(* Thunk *)
| Thunk thunk_mode 'a
End
Definition store_v_same_type_def:
store_v_same_type v1 v2 =
case (v1:'a store_v, v2:'a store_v) of
| (Refv _, Refv _ ) => T
| (W8array _, W8array _) => T
| (Varray _, Varray _ ) => T
| (Thunk NotEvaluated _, Thunk _ _) => T
| _ => F
End
(* The nth item in the list is the value at location n *)
Type store = “:('a store_v) list”
Definition empty_store_def:
empty_store = []:α store_v list
End
Definition store_lookup_def:
store_lookup l (st:('a store_v) list) =
if l < LENGTH st then SOME (EL l st) else NONE
End
Definition store_alloc_def:
store_alloc (v:'a store_v) st = (st ++ [v],LENGTH st)
End
Definition store_assign_def:
store_assign n (v:'a store_v) st =
if n < LENGTH st ∧ store_v_same_type (EL n st) v then
SOME (LUPDATE v n st)
else NONE
End
(* The eval state of the source semantics. A one-argument call
Eval (st,cl) initialises Eval: it is accepted once, when init_ok st cl
holds, and cl is kept as closure. The compiler proofs instantiate
init_ok so that cl must implement the compiler and st must be its
initial state. A two-argument call Eval (env,decs) succeeds or fails as
decided by the eval oracle of the state (see below), where failure
covers both a compiler that rejects decs and a missing initialisation.
On success, it evaluates decs in env and returns the pair
(Env env' id', ""), where env' is the resulting environment, and the
call is added to history. On failure, it returns
(Env env id, "Compiler error") without evaluating anything, but it
may consume entries of the pointer equality oracle (the compiled code
runs the compiler closure, which may use pointer equality). *)
Datatype:
source_eval_state =
<|
decode_decs : v -> (dec list) option ;
env_id_counter : (num # num # num) ;
init_ok : v -> v -> bool ;
initialised : bool ;
(* the compiler closure given at initialisation *)
closure : v ;
(* the env id and declarations of every successful two-argument
call to Eval, oldest first *)
history : ((num # num) # v) list
|>
End
Datatype:
state =
<| clock : num
; refs : v store
; ffi : 'ffi ffi_state
; next_type_stamp : num
; next_exn_stamp : num
; eval_state : source_eval_state option
(* oracle deciding whether each successive PtrEq returns the result of
structural equality (T) or is attenuated to false (F); indexed first by
install epoch, which advances at every successful Eval, and then by
position within that epoch. *)
; ptr_eq_oracle : num -> num -> bool
(* oracle deciding whether a two-argument call to Eval succeeds (T) or
fails (F), given whether Eval has been initialised, the history of
successful calls and the env id and declarations of this call. Like
a compiler, it gives the same answer to the same call. It also gives
the number of pointer equality oracle entries that a failing call
consumes, as a function of the compiler closure, the store and the
pointer equality oracle. *)
; eval_oracle : bool # ((num # num) # v) list # ((num # num) # v) ->
bool # (v -> v store -> (num -> num -> bool) -> num)
|>
End
(* Other primitives *)
(* Check that a constructor is properly applied *)
Definition do_con_check_def:
do_con_check (cenv:((mlstring),(mlstring),(num#stamp))namespace) n_opt l ⇔
case n_opt of
NONE => T
| SOME n => case nsLookup cenv n of NONE => F | SOME (l',v2) => l = l'
End
Definition one_con_check_def[simp]:
one_con_check envc (Con cn es) = do_con_check envc cn (LENGTH es) ∧
one_con_check envc _ = T
End
(* Constructor prechecks also inspect unevaluated function bodies, but must
follow the lexical constructor environment through each local open. *)
Definition check_exp_constructors_def:
check_exp_constructors envc (Raise e) = check_exp_constructors envc e ∧
check_exp_constructors envc (Handle e pes) =
(check_exp_constructors envc e ∧
EVERY (λ(p,e). check_exp_constructors envc e) pes) ∧
check_exp_constructors envc (Lit l) = T ∧
check_exp_constructors envc (Con cn es) =
(do_con_check envc cn (LENGTH es) ∧
EVERY (check_exp_constructors envc) es) ∧
check_exp_constructors envc (Var n) = T ∧
check_exp_constructors envc (Fun n e) = check_exp_constructors envc e ∧
check_exp_constructors envc (App op es) =
EVERY (check_exp_constructors envc) es ∧
check_exp_constructors envc (Log op e1 e2) =
(check_exp_constructors envc e1 ∧ check_exp_constructors envc e2) ∧
check_exp_constructors envc (If e1 e2 e3) =
(check_exp_constructors envc e1 ∧ check_exp_constructors envc e2 ∧
check_exp_constructors envc e3) ∧
check_exp_constructors envc (Mat e pes) =
(check_exp_constructors envc e ∧
EVERY (λ(p,e). check_exp_constructors envc e) pes) ∧
check_exp_constructors envc (Let n e1 e2) =
(check_exp_constructors envc e1 ∧ check_exp_constructors envc e2) ∧
check_exp_constructors envc (Letrec funs e) =
(EVERY (λ(f,n,e). check_exp_constructors envc e) funs ∧
check_exp_constructors envc e) ∧
check_exp_constructors envc (Tannot e t) = check_exp_constructors envc e ∧
check_exp_constructors envc (Lannot e l) = check_exp_constructors envc e ∧
check_exp_constructors envc (Open path e) =
(case nsOpen path envc of
NONE => F
| SOME opened => check_exp_constructors (nsAppend opened envc) e) ∧
check_exp_constructors envc (Assert e sa) = check_exp_constructors envc e
End
Theorem check_exp_constructors_open:
check_exp_constructors envc (Open path e) ⇔
∃opened. nsOpen path envc = SOME opened ∧
check_exp_constructors (nsAppend opened envc) e
Proof
Cases_on `nsOpen path envc` >> simp [check_exp_constructors_def]
QED
Definition build_conv_def:
build_conv (envC:((mlstring),(mlstring),(num#stamp))namespace) cn vs =
case cn of
NONE => SOME (Conv NONE vs)
| SOME id =>
case nsLookup envC id of
NONE => NONE
| SOME (len,stamp) => SOME (Conv (SOME stamp) vs)
End
Definition lit_same_type_def:
lit_same_type l1 l2 ⇔
case (l1,l2) of
| (IntLit _, IntLit _) => T
| (Char _, Char _) => T
| (StrLit _, StrLit _) => T
| (Word8 _, Word8 _) => T
| (Word64 _, Word64 _) => T
| (Float64 _, Float64 _) => T
| _ => F
End
Datatype:
match_result =
No_match
| Match_type_error
| Match 'a
End
Definition same_type_def:
(same_type (TypeStamp v0 n1 pos1) (TypeStamp v1 n2 pos2) ⇔ n1 = n2) ∧
(same_type (ExnStamp v2) (ExnStamp v3) ⇔ T) ∧
(same_type _ _ ⇔ F)
End
Definition same_ctor_def:
same_ctor stamp1 stamp2 ⇔ stamp1 = (stamp2:stamp)
End
Definition ctor_same_type_def:
ctor_same_type c1 c2 ⇔
case (c1,c2) of
(NONE,NONE) => T
| (SOME stamp1,SOME stamp2) => same_type stamp1 stamp2
| _ => F
End
Definition Boolv_def:
Boolv b =
if b then Conv (SOME (TypeStamp «True» bool_type_num 1)) []
else Conv (SOME (TypeStamp «False» bool_type_num 0)) []
End
(* A big-step pattern matcher. If the value matches the pattern, return an
* environment with the pattern variables bound to the corresponding sub-terms
* of the value; this environment extends the environment given as an argument.
* No_match is returned when there is no match, but any constructors
* encountered in determining the match failure are applied to the correct
* number of arguments, and constructors in corresponding positions in the
* pattern and value come from the same type. Match_type_error is returned
* when one of these conditions is violated *)
Definition pmatch_def:
pmatch envC s Pany v' env = Match env ∧
pmatch envC s (Pvar x) v' env = Match ((x,v')::env) ∧
pmatch envC s (Plit l) (Litv l') env =
(if l = l' then Match env
else if lit_same_type l l' then No_match
else Match_type_error) ∧
pmatch envC s (Pcon (SOME n) ps) (Conv (SOME stamp') vs) env =
(case nsLookup envC n of
NONE => Match_type_error
| SOME (l,stamp) =>
if same_type stamp stamp' ∧ LENGTH ps = l then
if same_ctor stamp stamp' then
if LENGTH vs = l then pmatch_list envC s ps vs env
else Match_type_error
else No_match
else Match_type_error) ∧
pmatch envC s (Pcon NONE ps) (Conv NONE vs) env =
(if LENGTH ps = LENGTH vs then pmatch_list envC s ps vs env
else Match_type_error) ∧
pmatch envC s (Pref p) (Loc _ lnum) env =
(case store_lookup lnum s of
NONE => Match_type_error
| SOME (Refv v) => pmatch envC s p v env
| SOME _ => Match_type_error) ∧
pmatch envC s (Pas p i) v env = pmatch envC s p v ((i,v)::env) ∧
pmatch envC s (Ptannot p t) v env = pmatch envC s p v env ∧
pmatch envC s _ _ env = Match_type_error ∧
pmatch_list envC s [] [] env = Match env ∧
pmatch_list envC s (p::ps) (v::vs) env =
(case pmatch envC s p v env of
No_match =>
(case pmatch_list envC s ps vs env of
No_match => No_match
| Match_type_error => Match_type_error
| Match v1 => No_match)
| Match_type_error => Match_type_error
| Match env' => pmatch_list envC s ps vs env') ∧
pmatch_list envC s _ _ env = Match_type_error
Termination
WF_REL_TAC
`inv_image $< (λx. case x of INL (s,a,p,b,c) => pat_size p
| INR (s,a,ps,b,c) => list_size pat_size ps)`
End
Definition can_pmatch_all_def:
(can_pmatch_all envC refs [] v ⇔ T) ∧
(can_pmatch_all envC refs (p::ps) v ⇔
if pmatch envC refs p v [] = Match_type_error then F
else can_pmatch_all envC refs ps v)
End
(* Bind each function of a mutually recursive set of functions to its closure *)
Definition build_rec_env_def:
(build_rec_env:(varN#varN#exp)list ->(v)sem_env ->((mlstring),(mlstring),(v))namespace
->((mlstring),(mlstring),(v))namespace) funs cl_env add_to_env =
FOLDR (λ(f,x,e) env'. nsBind f (Recclosure cl_env funs f) env')
add_to_env funs
End
(* Lookup in the list of mutually recursive functions *)
Definition find_recfun_def:
(find_recfun:mlstring ->(mlstring#'a#'b)list ->('a#'b)option) n funs =
case funs of
[] => NONE
| (f,x,e)::funs' => if f = n then SOME (x,e) else find_recfun n funs'
End
Datatype:
eq_result =
Eq_val bool
| Eq_type_error
End
Definition do_eq_def:
do_eq (Litv l1) (Litv l2) =
(if lit_same_type l1 l2 then Eq_val (l1 = l2) else Eq_type_error) ∧
do_eq (Loc b1 l1) (Loc b2 l2) =
(if b1 ∧ b2 then Eq_val (l1 = l2) else Eq_type_error) ∧
do_eq (Conv cn1 vs1) (Conv cn2 vs2) =
(if cn1 = cn2 ∧ LENGTH vs1 = LENGTH vs2 then do_eq_list vs1 vs2
else if ctor_same_type cn1 cn2 then Eq_val F
else Eq_type_error) ∧
do_eq (Vectorv vs1) (Vectorv vs2) =
(if LENGTH vs1 = LENGTH vs2 then do_eq_list vs1 vs2 else Eq_val F) ∧
do_eq (Closure v0 v3 v4) (Closure v5 v6 v7) = Eq_val T ∧
do_eq (Closure v8 v9 v10) (Recclosure v11 v12 v13) = Eq_val T ∧
do_eq (Recclosure v14 v15 v16) (Closure v17 v18 v19) = Eq_val T ∧
do_eq (Recclosure v20 v21 v22) (Recclosure v23 v24 v25) = Eq_val T ∧
do_eq (Env v26 (gen1,id1)) (Env v27 (gen2,id2)) =
Eq_val (gen1 = gen2 ∧ id1 = id2) ∧
do_eq _ _ = Eq_type_error ∧
do_eq_list [] [] = Eq_val T ∧
do_eq_list (v1::vs1) (v2::vs2) =
(case do_eq v1 v2 of
Eq_val r => if ¬r then Eq_val F else do_eq_list vs1 vs2
| Eq_type_error => Eq_type_error) ∧
do_eq_list _ _ = Eq_val F
Termination
WF_REL_TAC `inv_image $< (λx. case x of INL (v1,v2) => v_size v1
| INR (vs1,vs2) => list_size v_size vs1)`
>> Cases_on ‘compress_bool v1’ >> gs[Boolv_def, listTheory.list_size_def]
End
Datatype:
cmp_result =
Cmp_val int
| Cmp_type_error
End
Definition compare_int_def:
compare_int (i:int) j = if i < j then -1 else if j < i then 1 else 0
End
Definition compare_lit_def:
compare_lit (IntLit i) (IntLit j) = Cmp_val (compare_int i j) ∧
compare_lit (Char c) (Char d) = Cmp_val (compare_int (&ORD c) (&ORD d)) ∧
compare_lit (StrLit s) (StrLit t) =
Cmp_val (if mlstring_lt s t then -1 else if mlstring_lt t s then 1 else 0) ∧
compare_lit (Word8 w) (Word8 v) = Cmp_val (compare_int (&w2n w) (&w2n v)) ∧
compare_lit (Word64 w) (Word64 v) = Cmp_val (compare_int (&w2n w) (&w2n v)) ∧
compare_lit (Float64 w) (Float64 v) = Cmp_val (compare_int (&w2n w) (&w2n v)) ∧
compare_lit _ _ = Cmp_type_error
End
(* Called only after checking that the constructors belong to the same type. *)
Definition compare_stamp_def:
compare_stamp (SOME (TypeStamp cn1 ty1 pos1)) (SOME (TypeStamp cn2 ty2 pos2)) =
(if ty1 ≠ ty2 then Cmp_type_error
else Cmp_val (if pos1 = pos2 ∧ cn1 ≠ cn2 then 1
else compare_int (&pos1) (&pos2))) ∧
compare_stamp (SOME (ExnStamp n1)) (SOME (ExnStamp n2)) =
Cmp_val (if n1 = n2 then 0 else 1) ∧
compare_stamp NONE NONE = Cmp_val 0 ∧
compare_stamp _ _ = Cmp_type_error
End
(* The checks and short-circuiting follow do_eq. Locations return 0 when equal
and 1 otherwise, without exposing their allocation order. All closures
compare equal. Environments compare their identifiers lexicographically. *)
Definition do_compare_def:
do_compare (Litv l1) (Litv l2) = compare_lit l1 l2 ∧
do_compare (Loc b1 l1) (Loc b2 l2) =
(if b1 ∧ b2 then Cmp_val (if l1 = l2 then 0 else 1) else Cmp_type_error) ∧
do_compare (Conv cn1 vs1) (Conv cn2 vs2) =
(if ¬ctor_same_type cn1 cn2 then Cmp_type_error
else if cn1 = cn2 then
if LENGTH vs1 = LENGTH vs2 then do_compare_list vs1 vs2
else Cmp_val (compare_int (&LENGTH vs1) (&LENGTH vs2))
else if ctor_same_type cn1 (SOME (ExnStamp 0)) then Cmp_val 1
else if NULL vs1 ∧ ¬NULL vs2 then Cmp_val (-1)
else if ¬NULL vs1 ∧ NULL vs2 then Cmp_val 1
else compare_stamp cn1 cn2) ∧
do_compare (Vectorv vs1) (Vectorv vs2) =
(if LENGTH vs1 = LENGTH vs2 then do_compare_list vs1 vs2
else Cmp_val (compare_int (&LENGTH vs1) (&LENGTH vs2))) ∧
do_compare (Closure env1 n1 e1) (Closure env2 n2 e2) = Cmp_val 0 ∧
do_compare (Closure env1 n1 e1) (Recclosure env2 funs2 n2) = Cmp_val 0 ∧
do_compare (Recclosure env1 funs1 n1) (Closure env2 n2 e2) = Cmp_val 0 ∧
do_compare (Recclosure env1 funs1 n1) (Recclosure env2 funs2 n2) = Cmp_val 0 ∧
do_compare (Env env1 (gen1,id1)) (Env env2 (gen2,id2)) =
Cmp_val (if gen1 = gen2 then compare_int (&id1) (&id2)
else compare_int (&gen1) (&gen2)) ∧
do_compare _ _ = Cmp_type_error ∧
do_compare_list [] [] = Cmp_val 0 ∧
do_compare_list [] (_::ys) = Cmp_val (-1) ∧
do_compare_list (_::xs) [] = Cmp_val 1 ∧
do_compare_list (x::xs) (y::ys) =
(case do_compare x y of
Cmp_val n => if n = 0 then do_compare_list xs ys else Cmp_val n
| Cmp_type_error => Cmp_type_error)
Termination
WF_REL_TAC `inv_image $< (λx. case x of INL (v1,v2) => v_size v1
| INR (vs1,vs2) => list_size v_size vs1)`
End
Theorem compare_int_eq_zero:
compare_int i j = 0 ⇔ i = j
Proof
rw [compare_int_def] >> intLib.ARITH_TAC
QED
Theorem compare_lit_eq:
∀l1 l2.
(case compare_lit l1 l2 of
Cmp_val n => Eq_val (n = 0)
| Cmp_type_error => Eq_type_error) =
if lit_same_type l1 l2 then Eq_val (l1 = l2) else Eq_type_error
Proof
Cases >> Cases >>
simp [compare_lit_def, lit_same_type_def, compare_int_eq_zero, stringTheory.ORD_11] >>
rw [] >> metis_tac [mlstring_lt_nonrefl, mlstring_lt_cases]
QED
Theorem compare_stamp_eq[local]:
∀cn1 cn2.
(case compare_stamp cn1 cn2 of
Cmp_val n => Eq_val (n = 0)
| Cmp_type_error => Eq_type_error) =
if ctor_same_type cn1 cn2 then Eq_val (cn1 = cn2) else Eq_type_error
Proof
Cases >> Cases >> simp [ctor_same_type_def, compare_stamp_def] >>
rename1 `compare_stamp (SOME stamp1) (SOME stamp2)` >>
Cases_on `stamp1` >> Cases_on `stamp2` >>
simp [compare_stamp_def, same_type_def, compare_int_eq_zero] >>
rw [] >> simp [compare_int_eq_zero] >> metis_tac []
QED
Theorem do_compare_eq:
(∀v1 v2.
(case do_compare v1 v2 of
Cmp_val n => Eq_val (n = 0)
| Cmp_type_error => Eq_type_error) = do_eq v1 v2) ∧
(∀vs1 vs2.
(case do_compare_list vs1 vs2 of
Cmp_val n => Eq_val (n = 0)
| Cmp_type_error => Eq_type_error) = do_eq_list vs1 vs2)
Proof
`∀cn. ctor_same_type cn cn` by (
qx_gen_tac `cn` >> Cases_on `cn` >> simp [ctor_same_type_def] >>
rename1 `same_type stamp stamp` >> Cases_on `stamp` >> simp [same_type_def]) >>
ho_match_mp_tac do_compare_ind >>
simp [do_compare_def, do_eq_def, compare_lit_eq, compare_stamp_eq,
compare_int_eq_zero] >>
rw [] >>
fs [compare_int_eq_zero, compare_stamp_eq]
>~ [`¬ctor_same_type cn cn`] >- metis_tac [] >>
Cases_on `do_compare v1 v2` >> gvs [] >>
IF_CASES_TAC >> gvs []
QED
Theorem do_compare_eq_iff:
∀v1 v2.
(do_eq v1 v2 = Eq_type_error ⇔ do_compare v1 v2 = Cmp_type_error) ∧
(do_eq v1 v2 = Eq_val T ⇔ do_compare v1 v2 = Cmp_val 0)
Proof
qx_genl_tac [`v1`,`v2`] >>
mp_tac (GSYM (Q.SPECL [`v1`,`v2`] (CONJUNCT1 do_compare_eq))) >>
Cases_on `do_compare v1 v2` >> rw []
QED
(* Do an application *)
Definition do_opapp_def:
do_opapp vs =
case vs of
| [Closure env n e; v] => SOME (env with v := nsBind n v env.v,e)
| [Recclosure env' funs n'; v] =>
if ALL_DISTINCT (MAP (λ(f,x,e). f) funs) then
case find_recfun n' funs of
NONE => NONE
| SOME (n,e) =>
SOME
(env' with v := nsBind n v (build_rec_env funs env' env'.v),e)
else NONE
| _ => NONE
End
(* If a value represents a list, get that list. Otherwise return Nothing *)
Definition v_to_list_def:
v_to_list (Conv (SOME stamp) []) =
(if stamp = TypeStamp «[]» list_type_num 0 then SOME [] else NONE) ∧
v_to_list (Conv (SOME stamp) [v1; v2]) =
(if stamp = TypeStamp «::» list_type_num 1 then
case v_to_list v2 of NONE => NONE | SOME vs => SOME (v1::vs)
else NONE) ∧
v_to_list _ = NONE
End
Definition list_to_v_def:
list_to_v [] = Conv (SOME (TypeStamp «[]» list_type_num 0)) [] ∧
list_to_v (x::xs) = Conv (SOME (TypeStamp «::» list_type_num 1)) [x; list_to_v xs]
End
Definition v_to_char_list_def:
v_to_char_list (Conv (SOME stamp) []) =
(if stamp = TypeStamp «[]» list_type_num 0 then SOME "" else NONE) ∧
v_to_char_list (Conv (SOME stamp) [Litv (Char c); v]) =
(if stamp = TypeStamp «::» list_type_num 1 then
case v_to_char_list v of NONE => NONE | SOME cs => SOME (STRING c cs)
else NONE) ∧
v_to_char_list _ = NONE
End
Definition vs_to_string_def:
vs_to_string [] = SOME «» ∧
vs_to_string (Litv (StrLit s1)::vs) =
(case vs_to_string vs of NONE => NONE | SOME s2 => SOME (strcat s1 s2)) ∧
vs_to_string (_::v1) = NONE
End
Definition maybe_to_v_def:
maybe_to_v NONE = Conv (SOME (TypeStamp «None» option_type_num 0)) [] ∧
maybe_to_v (SOME v) = Conv (SOME (TypeStamp «Some» option_type_num 1)) [v]
End
Definition v_to_id_def:
v_to_id (Conv (SOME stamp) [Litv (StrLit s)]) =
(if stamp = TypeStamp «Short» id_type_num 0 then
SOME (Short s)
else NONE) ∧
v_to_id (Conv (SOME stamp) [Litv (StrLit s); v]) =
(if stamp = TypeStamp «Long» id_type_num 1 then
case v_to_id v of NONE => NONE | SOME id => SOME (Long s id)
else NONE) ∧
v_to_id _ = NONE
End
Definition nat_to_v_def:
nat_to_v n = Litv (IntLit (&n))
End
Definition maybe_all_list_def:
maybe_all_list v =
case v of
[] => SOME []
| NONE::vs => NONE
| SOME x::vs =>
case maybe_all_list vs of NONE => NONE | SOME xs => SOME (x::xs)
End
Definition v_to_word64_def:
v_to_word64 v =
case v of
| Litv (Word64 w) => SOME w
| _ => NONE
End
Definition v_to_word64_list_def:
v_to_word64_list v =
case v_to_list v of
| NONE => NONE
| SOME xs => maybe_all_list (MAP v_to_word64 xs)
End
Definition v_to_mlstring_def:
v_to_mlstring v =
case v of
| Litv (StrLit s) => SOME s
| _ => NONE
End
Definition declare_env_def:
declare_env (es:source_eval_state option) env =
case es of
| NONE => NONE
| SOME s =>
(case s.env_id_counter of
(cur_gen,next_id,next_gen) =>
SOME
(Env env (cur_gen,next_id),
SOME
(s with
env_id_counter := (cur_gen,next_id + 1,next_gen))))
End
(* values that contain neither code nor references *)
Definition data_v_def:
(data_v v ⇔
case v of
| Litv _ => T
| Conv v_ vs => data_v_list vs
| Vectorv vs => data_v_list vs
| _ => F) ∧
(data_v_list [] ⇔ T) ∧
(data_v_list (v::vs) ⇔ data_v v ∧ data_v_list vs)
Termination
WF_REL_TAC `measure (λx. case x of INL v => v_size v
| INR vs => list_size v_size vs)`
End
(* get the declarations to be evaluated by a two-argument call to Eval *)
Definition do_eval_def:
do_eval vs (es:source_eval_state option) =
case es of
| NONE => NONE
| SOME s =>
case vs of
| [Env env id; decs_v] =>
(case s.decode_decs decs_v of
NONE => NONE
| SOME decs =>
if data_v decs_v then
(case s.env_id_counter of
(cur_gen,next_id,next_gen) =>
SOME (env,decs,
SOME (s with <| env_id_counter :=
(next_gen,0,next_gen + 1);
history :=
s.history ++ [(id,decs_v)] |>)))
else NONE)
| _ => NONE
End
(* a one-argument call to Eval initialises Eval *)
Definition do_eval_init_def:
do_eval_init vs (es:source_eval_state option) =
case es of
| NONE => NONE
| SOME s =>
case vs of
| [Conv NONE [st_v; cl]] =>
if ¬s.initialised ∧ s.init_ok st_v cl
then SOME (s with <| initialised := T; closure := cl |>) else NONE
| _ => NONE
End
(* conclude an Eval generation *)
Definition reset_env_generation_def:
reset_env_generation (prior_es:source_eval_state option) es =
case (prior_es,es) of
| (SOME prior_s',SOME s') =>
(case (prior_s'.env_id_counter,s'.env_id_counter) of
((cur_gen,next_id,v5),v6,v8,next_gen) =>
SOME (s' with env_id_counter := (cur_gen,next_id,next_gen)))
| _ => es
End
Definition copy_array_def:
copy_array (src,srcoff) len d =
if
srcoff < 0 ∨ len < 0 ∨ LENGTH src < Num (ABS (I (srcoff + len)))
then
NONE
else
(let
copied =
TAKE (Num (ABS (I len))) (DROP (Num (ABS (I srcoff))) src)
in
case d of
NONE => SOME copied
| SOME (dst,dstoff) =>
if dstoff < 0 ∨ LENGTH dst < Num (ABS (I (dstoff + len))) then
NONE
else
SOME
(TAKE (Num (ABS (I dstoff))) dst ⧺ copied ⧺
DROP (Num (ABS (I (dstoff + len)))) dst))
End
Definition ws_to_chars_def:
ws_to_chars (ws:word8 list) = MAP (λw. CHR (w2n w)) ws
End
Definition chars_to_ws_def:
chars_to_ws cs = MAP (λc. i2w (&ORD c)) cs :word8 list
End
(*
Definition opn_lookup_def:
opn_lookup n =
case n of
| Plus => $+
| Minus => $-
| Times => $*
| Divide => $/
| Modulo => $%
End
Definition opb_lookup_def:
opb_lookup n =
case n of Lt => $< | Gt => $> | Leq => $<= | Geq => ($>= : int -> int -> bool)
End
Definition opw8_lookup_def:
opw8_lookup op =
case op of
| Andw => word_and
| Orw => word_or
| Xor => word_xor
| Add => word_add
| Sub => word_sub : word8 -> word8 -> word8
End
Definition opw64_lookup_def:
opw64_lookup op =
case op of
| Andw => word_and
| Orw => word_or
| Xor => word_xor
| Add => word_add
| Sub => word_sub : word64 -> word64 -> word64
End
*)
Definition shift8_lookup_def:
shift8_lookup sh =
case sh of
| Lsl => word_lsl
| Lsr => word_lsr
| Asr => word_asr
| Ror => word_ror : word8 -> num -> word8
End
Definition shift64_lookup_def:
shift64_lookup sh =
case sh of
| Lsl => word_lsl
| Lsr => word_lsr
| Asr => word_asr
| Ror => word_ror : word64 -> num -> word64
End
Datatype:
exp_or_val = Exp exp | Val v
End
Type store_ffi = “: 'v store # 'ffi ffi_state”
Overload w64lit[local] = “λw. Litv (Word64 w)”
Overload f64lit[local] = “λw. Litv (Float64 w)”
Definition xor_bytes_def:
xor_bytes [] bs2 = SOME (bs2:word8 list) ∧
xor_bytes bs1 [] = NONE ∧
xor_bytes (b1::bs1) (b2::bs2) =
case xor_bytes bs1 bs2 of
| NONE => NONE
| SOME rest => SOME (word_xor b1 b2 :: rest)
End
(* With `dest_thunk` we check 3 things:
- The values contain exactly one reference
- The reference is valid
- The reference points to a thunk
We distinguish between `BadRef` and `NotThunk` instead of returning an option
with `NONE` for both, because we want `update_thunk` to succeed when
`dest_thunk` fails but only when the reference actually exists and points to
something other than a thunk. *)
Datatype:
dest_thunk_ret
= BadRef
| NotThunk
| IsThunk thunk_mode v
End
Definition dest_thunk_def:
dest_thunk [Loc b n] st =
(case store_lookup n st of
| NONE => BadRef
| SOME (Thunk Evaluated v) =>
if b then BadRef else IsThunk Evaluated v
| SOME (Thunk NotEvaluated v) =>
if b then BadRef else IsThunk NotEvaluated v
| SOME _ => NotThunk) ∧
dest_thunk vs st = NotThunk
End
Definition update_thunk_def:
update_thunk [Loc F n] st [v] =
(case dest_thunk [v] st of
| NotThunk => store_assign n (Thunk Evaluated v) st
| _ => NONE) ∧
update_thunk _ st _ = NONE
End
Definition bad_thunk_update_def:
bad_thunk_update m v st ⇔
m = Evaluated ∧ dest_thunk [v] st ≠ NotThunk
End
Definition thunk_op_def:
thunk_op (s: v store_v list, t: 'ffi ffi_state) th_op vs =
case (th_op,vs) of
| (AllocThunk m, [v]) =>
(if bad_thunk_update m v s then NONE else
let (s',n) = store_alloc (Thunk m v) s in
SOME ((s',t), Rval (Loc F n)))
| (UpdateThunk m, [Loc F lnum; v]) =>
(if bad_thunk_update m v s then NONE else
case store_assign lnum (Thunk m v) s of
| SOME s' => SOME ((s',t), Rval (Conv NONE []))
| NONE => NONE)
| _ => NONE
End
Definition check_type_def:
check_type BoolT v = (v = Boolv T ∨ v = Boolv F) ∧
check_type IntT v = (∃i. v = Litv (IntLit i)) ∧
check_type CharT v = (∃c. v = Litv (Char c)) ∧
check_type StrT v = (∃s. v = Litv (StrLit s)) ∧
check_type (WordT W8) v = (∃w. v = Litv (Word8 w)) ∧
check_type (WordT W64) v = (∃w. v = Litv (Word64 w)) ∧
check_type Float64T v = (∃w. v = Litv (Float64 w))
End
(* The arity of a constructor; unknown constructors get 0, which does not
matter since a pattern with an unknown constructor is a type error. *)
Definition con_arity_def:
con_arity envC cn =
case nsLookup envC cn of
| NONE => 0
| SOME (n:num,stamp:stamp) => n
End
(* Checks a shape assertion; a false result means a type error. ConsA behaves
like matching against the constructor pattern applied to wildcards,
followed by a catch-all wildcard; such patterns never inspect the store. *)
Definition check_shape_def:
check_shape envC AnyA v = T ∧
check_shape envC (PrimA ty) v = check_type ty v ∧
check_shape envC (ConsA cn) v =
(pmatch envC [] (Pcon (SOME cn) (REPLICATE (con_arity envC cn) Pany)) v [] ≠
Match_type_error) ∧
check_shape envC (TupleA sas) v =
(case v of
| Conv NONE vs => check_shapes envC sas vs
| _ => F) ∧
check_shape envC (ListA sa) v =
(case v_to_list v of
| NONE => F
| SOME vs => EVERY (check_shape envC sa) vs) ∧
check_shapes envC [] [] = T ∧
check_shapes envC (sa::sas) (v::vs) =