-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathsemanticPrimitives.lem
1663 lines (1547 loc) · 53.4 KB
/
semanticPrimitives.lem
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.
*)
open import Pervasives
open import Lib
import List_extra
import List
import String
import String_extra
open import Ast
open import Namespace
open import Ffi
open import FpSem FpOpt
open import FpValTree
open import RealOps
(* Constructors and exceptions need unique identities, which we represent by stamps. *)
type stamp =
(* Each type gets a unique number, and the constructor name must be unique
inside of the type *)
| TypeStamp of conN * nat
| ExnStamp of nat
(*
val type_defs_to_new_tdecs : list modN -> type_def -> set tid_or_exn
let type_defs_to_new_tdecs mn tdefs =
Set.fromList (List.map (fun (tvs,tn,ctors) -> TypeId (mk_id mn tn)) tdefs)
*)
type sem_env 'v =
<| v : namespace modN varN 'v
(* Lexical mapping of constructor idents to arity, stamp pairs *)
; c : namespace modN conN (nat * stamp)
|>
(* Value forms *)
type v =
| Litv of lit
(* Constructor application. Can be a tuple or a given constructor of a given type *)
| Conv of maybe stamp * list v
(* Function closures
The environment is used for the free variables in the function *)
| Closure of sem_env v * 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 of sem_env v * list (varN * varN * exp) * varN
| Loc of nat
| Vectorv of list v
| FP_WordTree of fp_word_val
| FP_BoolTree of fp_bool_val
| Real of real
(* Environment value for Eval, and its numeric identifier *)
| Env of (sem_env v) * (nat * nat)
(* The runtime semantics should be able to deal with constants too, thus we add
a translation function from word constants to value trees *)
val fp_translate: v -> maybe v
let rec fp_translate (Litv (Word64 w)) = Just (FP_WordTree (Fp_const w))
and fp_translate (FP_WordTree v) = Just (FP_WordTree v)
and fp_translate (FP_BoolTree v) = Just (FP_BoolTree v)
and fp_translate _ = Nothing
type env_ctor = namespace modN conN (nat * stamp)
type env_val = namespace modN varN v
let bind_stamp = ExnStamp 0
let chr_stamp = ExnStamp 1
let div_stamp = ExnStamp 2
let subscript_stamp = ExnStamp 3
let bind_exn_v = Conv (Just bind_stamp) []
let chr_exn_v = Conv (Just chr_stamp) []
let div_exn_v = Conv (Just div_stamp) []
let sub_exn_v = Conv (Just subscript_stamp) []
let bool_type_num : nat = 0
let list_type_num : nat = 1
let option_type_num : nat = 2
let lit_type_num : nat = 3
let id_type_num : nat = 4
let ast_t_type_num : nat = 5
let pat_type_num : nat = 6
let lop_type_num : nat = 7
let opn_type_num : nat = 8
let opb_type_num : nat = 9
let opw_type_num : nat = 10
let shift_type_num : nat = 11
let word_size_type_num : nat = 12
let fp_uop_type_num : nat = 13
let fp_bop_type_num : nat = 14
let fp_top_type_num : nat = 15
let fp_cmp_type_num : nat = 16
let real_uop_type_num : nat = 17
let real_bop_type_num : nat = 18
let real_cmp_type_num : nat = 19
let op_type_num : nat = 20
let locn_type_num : nat = 21
let locs_type_num : nat = 22
let fp_opt_num : nat = 23
let exp_type_num : nat = 24
let dec_type_num : nat = 25
(* The result of evaluation *)
type abort =
| Rtype_error
| Rtimeout_error
| Rffi_error of final_event
type error_result 'a =
| Rraise of 'a (* Should only be a value of type exn *)
| Rabort of abort
type result 'a 'b =
| Rval of 'a
| Rerr of error_result 'b
(* Stores *)
type store_v 'a =
(* A ref cell *)
Refv of 'a
(* A byte array *)
| W8array of list word8
(* An array of values *)
| Varray of list 'a
val store_v_same_type : forall 'a. store_v 'a -> store_v 'a -> bool
let store_v_same_type v1 v2 =
match (v1,v2) with
| (Refv _, Refv _) -> true
| (W8array _,W8array _) -> true
| (Varray _,Varray _) -> true
| _ -> false
end
(* The nth item in the list is the value at location n *)
type store 'a = list (store_v 'a)
val empty_store : forall 'a. store 'a
let empty_store = []
val store_lookup : forall 'a. nat -> store 'a -> maybe (store_v 'a)
let store_lookup l st =
if l < List.length st then
Just (List_extra.nth st l)
else
Nothing
val store_alloc : forall 'a. store_v 'a -> store 'a -> store 'a * nat
let store_alloc v st =
((st ++ [v]), List.length st)
val store_assign : forall 'a. nat -> store_v 'a -> store 'a -> maybe (store 'a)
let store_assign n v st =
if n < List.length st &&
store_v_same_type (List_extra.nth st n) v
then
Just (List.update st n v)
else
Nothing
(*
State component encapsulating Icing optimizations and rewriter oracle.
rws := list of rewrites allowed to be applied by the semantics
opts := oracle that decides which rewrites are applied next
choices := global counter how many rewriting steps have been done
canOpt := flag indicating whether evaluation has stepped below an "opt" scope
*)
type optChoice =
| Strict (* strict 64-bit floating-point semantics, ignores annotations *)
| FPScope of fp_opt (* currently under scope *)
(* TODO: do we need to `list exp` to assert? *)
type fpState =
<| rws: list fp_rw
; opts: nat -> list rewrite_app
; choices: nat
; canOpt : optChoice
; assertions : nat -> store v -> sem_env v -> bool
; real_sem : bool
|>
(* Compiler checker for Eval. The current design requires the compiler
to be run (as regular code within the semantics) before Eval is called,
and these config parameters specify how to interpret the declarations to
be evaluated and check that the compiler was run correctly. *)
type compiler_args = ((nat * nat) * v * list dec)
type compiler_fun = compiler_args -> maybe (v * list word8 * list word64)
type eval_decs_state =
<|
compiler : compiler_fun ;
(* state must be encoded as v somehow *)
compiler_state : v ;
env_id_counter : (nat * nat * nat) ;
(* decs must also be encoded as v *)
decode_decs : v -> maybe (list dec)
|>
(* Alternative mode for Eval including an oracle.
This is only for use in the compiler proof. *)
type eval_oracle_fun = nat -> compiler_args
type eval_oracle_state =
<|
oracle : eval_oracle_fun ;
custom_do_eval : list v -> eval_oracle_fun ->
maybe ((nat * nat) * eval_oracle_fun * list dec) ;
envs : list (list (sem_env v)) ;
generation : nat
|>
type eval_state =
EvalDecs of eval_decs_state
| EvalOracle of eval_oracle_state
type state 'ffi =
<| clock : nat
; refs : store v
; ffi : ffi_state 'ffi
; next_type_stamp : nat
; next_exn_stamp : nat
; fp_state : fpState
; eval_state : maybe eval_state
|>
(* Other primitives *)
(* Check that a constructor is properly applied *)
val do_con_check : env_ctor -> maybe (id modN conN) -> nat -> bool
let do_con_check cenv n_opt l =
match n_opt with
| Nothing -> true
| Just n ->
match nsLookup cenv n with
| Nothing -> false
| Just (l',_) -> l = l'
end
end
val build_conv : env_ctor -> maybe (id modN conN) -> list v -> maybe v
let build_conv envC cn vs =
match cn with
| Nothing ->
Just (Conv Nothing vs)
| Just id ->
match nsLookup envC id with
| Nothing -> Nothing
| Just (len,stamp) -> Just (Conv (Just stamp) vs)
end
end
val lit_same_type : lit -> lit -> bool
let lit_same_type l1 l2 =
match (l1,l2) with
| (IntLit _, IntLit _) -> true
| (Char _, Char _) -> true
| (StrLit _, StrLit _) -> true
| (Word8 _, Word8 _) -> true
| (Word64 _, Word64 _) -> true
| _ -> false
end
type match_result 'a =
| No_match
| Match_type_error
| Match of 'a
val same_type : stamp -> stamp -> bool
let rec same_type (TypeStamp _ n1) (TypeStamp _ n2) = n1 = n2
and same_type (ExnStamp _) (ExnStamp _) = true
and same_type _ _ = false
val same_ctor : stamp -> stamp -> bool
let same_ctor stamp1 stamp2 = stamp1 = stamp2
val ctor_same_type : maybe stamp -> maybe stamp -> bool
let ctor_same_type c1 c2 =
match (c1,c2) with
| (Nothing, Nothing) -> true
| (Just stamp1, Just stamp2) -> same_type stamp1 stamp2
| _ -> false
end
val Boolv : bool -> v
let Boolv b = if b
then Conv (Just (TypeStamp "True" bool_type_num)) []
else Conv (Just (TypeStamp "False" bool_type_num)) []
val compress: v -> v
val compress_list : list v -> list v
let rec compress (FP_WordTree fp) = (Litv (Word64 (compress_word fp)))
and compress (FP_BoolTree fp) = (Boolv (compress_bool fp))
and compress (Real r) = Real r
and compress (Litv l) = (Litv l)
and compress (Conv ts vs) = Conv ts (compress_list vs)
and compress (Closure env x e) = Closure env x e
and compress (Env env n) = Env env n
and compress (Recclosure env es x) = Recclosure env es x
and compress (Loc n) = Loc n
and compress (Vectorv vs) = Vectorv (compress_list vs)
and compress_list ([]) = []
and compress_list (v::vs) = (compress v)::(compress_list vs)
(* 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 *)
val pmatch : env_ctor -> store v -> pat -> v -> alist varN v -> match_result (alist varN v)
let rec
pmatch envC s Pany v' env = Match env
and
pmatch envC s (Pvar x) v' env = Match ((x,v')::env)
and
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
and
pmatch envC s (Pcon (Just n) ps) (Conv (Just stamp') vs) env =
match nsLookup envC n with
| Just (l,stamp) ->
if same_type stamp stamp' && List.length ps = l then
if same_ctor stamp stamp' then
if List.length vs = l then
pmatch_list envC s ps vs env
else
Match_type_error
else
No_match
else
Match_type_error
| _ -> Match_type_error
end
and
pmatch envC s (Pcon Nothing ps) (Conv Nothing vs) env =
if List.length ps = List.length vs then
pmatch_list envC s ps vs env
else
Match_type_error
and
pmatch envC s (Pref p) (Loc lnum) env =
match store_lookup lnum s with
| Just (Refv v) -> pmatch envC s p v env
| Just _ -> Match_type_error
| Nothing -> Match_type_error
end
and
pmatch envC s (Pas p i) v env =
pmatch envC s p v ((i,v)::env)
and
pmatch envC s (Ptannot p t) v env =
pmatch envC s p v env
and
pmatch envC _ _ _ env = Match_type_error
and
pmatch_list envC s [] [] env = Match env
and
pmatch_list envC s (p::ps) (v::vs) env =
match pmatch envC s p v env with
| Match_type_error -> Match_type_error
| Match env' -> pmatch_list envC s ps vs env'
| No_match ->
match pmatch_list envC s ps vs env with
| Match_type_error -> Match_type_error
| _ -> No_match
end
end
and
pmatch_list envC s _ _ env = Match_type_error
val can_pmatch_all : env_ctor -> store v -> list pat -> v -> bool
let rec
can_pmatch_all envC refs [] v = true
and
can_pmatch_all envC refs (p::ps) v =
if pmatch envC refs p v [] = Match_type_error
then false else can_pmatch_all envC refs ps v
(* Bind each function of a mutually recursive set of functions to its closure *)
val build_rec_env : list (varN * varN * exp) -> sem_env v -> env_val -> env_val
let build_rec_env funs cl_env add_to_env =
foldr
(fun (f,x,e) env' -> nsBind f (Recclosure cl_env funs f) env')
add_to_env
funs
(* Lookup in the list of mutually recursive functions *)
val find_recfun : forall 'a 'b. varN -> list (varN * 'a * 'b) -> maybe ('a * 'b)
let rec find_recfun n funs =
match funs with
| [] -> Nothing
| (f,x,e) :: funs ->
if f = n then
Just (x,e)
else
find_recfun n funs
end
declare termination_argument find_recfun = automatic
type eq_result =
| Eq_val of bool
| Eq_type_error
val do_eq : v -> v -> eq_result
let rec
do_eq (Litv l1) (Litv l2) =
if lit_same_type l1 l2 then Eq_val (l1 = l2)
else Eq_type_error
and
do_eq (Loc l1) (Loc l2) = Eq_val (l1 = l2)
and
do_eq (Conv cn1 vs1) (Conv cn2 vs2) =
if cn1 = cn2 && (List.length vs1 = List.length vs2) then
do_eq_list vs1 vs2
else if ctor_same_type cn1 cn2 then
Eq_val false
else
Eq_type_error
and
do_eq (Vectorv vs1) (Vectorv vs2) =
if List.length vs1 = List.length vs2 then
do_eq_list vs1 vs2
else
Eq_val false
and
do_eq (Closure _ _ _) (Closure _ _ _) = Eq_val true
and
do_eq (Closure _ _ _) (Recclosure _ _ _) = Eq_val true
and
do_eq (Recclosure _ _ _) (Closure _ _ _) = Eq_val true
and
do_eq (Recclosure _ _ _) (Recclosure _ _ _) = Eq_val true
and
do_eq (FP_BoolTree v1) (FP_BoolTree v2) = Eq_val (compress_bool v1 = compress_bool v2)
and
do_eq (FP_BoolTree v1) (Conv cn2 vs2) =
match Boolv (compress_bool v1) with
| Conv cn1 vs1 ->
if cn1 = cn2 && (List.length vs1 = List.length vs2) then
do_eq_list vs1 vs2
else if ctor_same_type cn1 cn2 then
Eq_val false
else
Eq_type_error
| _ -> Eq_type_error
end
and
do_eq (Conv cn1 vs1) (FP_BoolTree v2) =
match Boolv (compress_bool v2) with
| Conv cn2 vs2 ->
if cn1 = cn2 && (List.length vs1 = List.length vs2) then
do_eq_list vs1 vs2
else if ctor_same_type cn1 cn2 then
Eq_val false
else
Eq_type_error
| _ -> Eq_type_error
end
and
do_eq (FP_WordTree v1) (FP_WordTree v2) = Eq_val (compress_word v1 = compress_word v2)
and
do_eq (Litv (Word64 w1)) (FP_WordTree v2) = Eq_val (w1 = compress_word v2)
and
do_eq (FP_WordTree v1) (Litv (Word64 w2)) = Eq_val (compress_word v1 = w2)
and
do_eq (Real r1) (Real r2) = Eq_val (r1 = r2)
and
do_eq (Env _ (gen1, id1)) (Env _ (gen2, id2)) = Eq_val (gen1 = gen2 && id1 = id2)
and
do_eq _ _ = Eq_type_error
and
do_eq_list [] [] = Eq_val true
and
do_eq_list (v1::vs1) (v2::vs2) =
match do_eq v1 v2 with
| Eq_type_error -> Eq_type_error
| Eq_val r ->
if not r then
Eq_val false
else
do_eq_list vs1 vs2
end
and
do_eq_list _ _ = Eq_val false
(* Do an application *)
val do_opapp : list v -> maybe (sem_env v * exp)
let do_opapp vs =
match vs with
| [Closure env n e; v] ->
Just (<| env with v = nsBind n v env.v |>, e)
| [Recclosure env funs n; v] ->
if allDistinct (List.map (fun (f,x,e) -> f) funs) then
match find_recfun n funs with
| Just (n,e) -> Just (<| env with v = nsBind n v (build_rec_env funs env env.v) |>, e)
| Nothing -> Nothing
end
else
Nothing
| _ -> Nothing
end
(* If a value represents a list, get that list. Otherwise return Nothing *)
val v_to_list : v -> maybe (list v)
let rec v_to_list (Conv (Just stamp) []) =
if stamp = TypeStamp "[]" list_type_num then
Just []
else
Nothing
and v_to_list (Conv (Just stamp) [v1;v2]) =
if stamp = TypeStamp "::" list_type_num then
match v_to_list v2 with
| Just vs -> Just (v1::vs)
| Nothing -> Nothing
end
else
Nothing
and v_to_list _ = Nothing
val list_to_v : list v -> v
let rec list_to_v [] = Conv (Just (TypeStamp "[]" list_type_num)) []
and list_to_v (x::xs) = Conv (Just (TypeStamp "::" list_type_num)) [x; list_to_v xs]
val v_to_char_list : v -> maybe (list char)
let rec v_to_char_list (Conv (Just stamp) []) =
if stamp = TypeStamp "[]" list_type_num then
Just []
else
Nothing
and v_to_char_list (Conv (Just stamp) [Litv (Char c);v]) =
if stamp = TypeStamp "::" list_type_num then
match v_to_char_list v with
| Just cs -> Just (c::cs)
| Nothing -> Nothing
end
else
Nothing
and v_to_char_list _ = Nothing
val vs_to_string : list v -> maybe string
let rec vs_to_string [] = Just ""
and vs_to_string (Litv(StrLit s1)::vs) =
match vs_to_string vs with
| Just s2 -> Just (s1 ^ s2)
| _ -> Nothing
end
and vs_to_string _ = Nothing
val maybe_to_v : maybe v -> v
let rec maybe_to_v Nothing =
Conv (Just (TypeStamp "None" option_type_num)) []
and maybe_to_v (Just v) =
Conv (Just (TypeStamp "Some" option_type_num)) [v]
val v_to_id : v -> maybe (id modN varN)
let rec v_to_id (Conv (Just stamp) [Litv (StrLit s)]) =
if stamp = TypeStamp "Short" id_type_num then
Just (Short s)
else
Nothing
and v_to_id (Conv (Just stamp) [Litv (StrLit s); v]) =
if stamp = TypeStamp "Long" id_type_num then
match v_to_id v with
| Just id -> Just (Long s id)
| Nothing -> Nothing
end
else
Nothing
and v_to_id _ = Nothing
declare termination_argument v_to_id = automatic
val enc_pair : v -> v -> v
let enc_pair v1 v2 = Conv Nothing [v1; v2]
val enc_list : list v -> v
let rec
enc_list [] =
Conv (Just (TypeStamp "[]" list_type_num)) []
and
enc_list (x::xs) =
Conv (Just (TypeStamp "::" list_type_num)) [x; enc_list xs]
val enc_option : maybe v -> v
let rec
enc_option Nothing =
Conv (Just (TypeStamp "None" option_type_num)) []
and
enc_option (Just x) =
Conv (Just (TypeStamp "Some" option_type_num)) [x]
val enc_lit : lit -> v
let rec
enc_lit (Word64 w) =
Conv (Just (TypeStamp "Word64" lit_type_num)) [Litv (Word64 w)]
and
enc_lit (Word8 b) =
Conv (Just (TypeStamp "Word8" lit_type_num)) [Litv (Word8 b)]
and
enc_lit (StrLit s) =
Conv (Just (TypeStamp "Strlit" lit_type_num)) [Litv (StrLit s)]
and
enc_lit (Char c) =
Conv (Just (TypeStamp "Char" lit_type_num)) [Litv (Char c)]
and
enc_lit (IntLit i) =
Conv (Just (TypeStamp "Intlit" lit_type_num)) [Litv (IntLit i)]
val enc_id : id modN typeN -> v
let rec
enc_id (Short s) =
Conv (Just (TypeStamp "Short" id_type_num)) [Litv (StrLit s)]
and
enc_id (Long s i) =
Conv (Just (TypeStamp "Long" id_type_num)) [Litv (StrLit s); enc_id i]
val enc_ast_t : ast_t -> v
let rec
enc_ast_t (Atapp x y) =
Conv (Just (TypeStamp "Atapp" ast_t_type_num))
[enc_list (List.map enc_ast_t x); enc_id y]
and
enc_ast_t (Attup x) =
Conv (Just (TypeStamp "Attup" ast_t_type_num))
[enc_list (List.map enc_ast_t x)]
and
enc_ast_t (Atfun x_3 x_2) =
Conv (Just (TypeStamp "Atfun" ast_t_type_num))
[enc_ast_t x_3; enc_ast_t x_2]
and
enc_ast_t (Atvar x_1) =
Conv (Just (TypeStamp "Atvar" ast_t_type_num)) [Litv (StrLit x_1)]
val enc_pat : pat -> v
let rec
enc_pat (Ptannot x_9 x_8) =
Conv (Just (TypeStamp "Ptannot" pat_type_num))
[enc_pat x_9; enc_ast_t x_8]
and
enc_pat (Pref x_7) =
Conv (Just (TypeStamp "Pref" pat_type_num))
[enc_pat x_7]
and
enc_pat (Pas x_6 x_5) =
Conv (Just (TypeStamp "Pas" pat_type_num))
[enc_pat x_6; Litv (StrLit x_5)]
and
enc_pat (Pcon x_4 x_3) =
Conv (Just (TypeStamp "Pcon" pat_type_num))
[enc_option (option_map enc_id x_4); enc_list (List.map enc_pat x_3)]
and
enc_pat (Plit x_2) =
Conv (Just (TypeStamp "Plit" pat_type_num))
[enc_lit x_2]
and
enc_pat (Pvar x_1) =
Conv (Just (TypeStamp "Pvar" pat_type_num))
[Litv (StrLit x_1)]
and
enc_pat Pany =
Conv (Just (TypeStamp "Pany" pat_type_num)) []
val enc_lop : lop -> v
let rec
enc_lop Or = Conv (Just (TypeStamp "Or" lop_type_num)) []
and
enc_lop And = Conv (Just (TypeStamp "And" lop_type_num)) []
val enc_opn : opn -> v
let rec
enc_opn Modulo = Conv (Just (TypeStamp "Modulo" opn_type_num)) []
and
enc_opn Divide = Conv (Just (TypeStamp "Divide" opn_type_num)) []
and
enc_opn Times = Conv (Just (TypeStamp "Times" opn_type_num)) []
and
enc_opn Minus = Conv (Just (TypeStamp "Minus" opn_type_num)) []
and
enc_opn Plus = Conv (Just (TypeStamp "Plus" opn_type_num)) []
val enc_opb : opb -> v
let rec
enc_opb Geq = Conv (Just (TypeStamp "Geq" opb_type_num)) []
and
enc_opb Leq = Conv (Just (TypeStamp "Leq" opb_type_num)) []
and
enc_opb Gt = Conv (Just (TypeStamp "Gt" opb_type_num)) []
and
enc_opb Lt = Conv (Just (TypeStamp "Lt" opb_type_num)) []
val enc_opw : opw -> v
let rec
enc_opw Sub = Conv (Just (TypeStamp "Sub" opw_type_num)) []
and
enc_opw Add = Conv (Just (TypeStamp "Add" opw_type_num)) []
and
enc_opw Xor = Conv (Just (TypeStamp "Xor" opw_type_num)) []
and
enc_opw Orw = Conv (Just (TypeStamp "Orw" opw_type_num)) []
and
enc_opw Andw = Conv (Just (TypeStamp "Andw" opw_type_num)) []
val enc_shift : shift -> v
let rec
enc_shift Ror = Conv (Just (TypeStamp "Ror" shift_type_num)) []
and
enc_shift Asr = Conv (Just (TypeStamp "Asr" shift_type_num)) []
and
enc_shift Lsr = Conv (Just (TypeStamp "Lsr" shift_type_num)) []
and
enc_shift Lsl = Conv (Just (TypeStamp "Lsl" shift_type_num)) []
val enc_word_size : word_size -> v
let rec
enc_word_size W64 = Conv (Just (TypeStamp "W64" word_size_type_num)) []
and
enc_word_size W8 = Conv (Just (TypeStamp "W8" word_size_type_num)) []
val enc_fp_uop : fp_uop -> v
let rec
enc_fp_uop FP_Sqrt = Conv (Just (TypeStamp "Fp_sqrt" fp_uop_type_num)) []
and
enc_fp_uop FP_Neg = Conv (Just (TypeStamp "Fp_neg" fp_uop_type_num)) []
and
enc_fp_uop FP_Abs = Conv (Just (TypeStamp "Fp_abs" fp_uop_type_num)) []
val enc_fp_bop : fp_bop -> v
let rec
enc_fp_bop FP_Div = Conv (Just (TypeStamp "Fp_div" fp_bop_type_num)) []
and
enc_fp_bop FP_Mul = Conv (Just (TypeStamp "Fp_mul" fp_bop_type_num)) []
and
enc_fp_bop FP_Sub = Conv (Just (TypeStamp "Fp_sub" fp_bop_type_num)) []
and
enc_fp_bop FP_Add = Conv (Just (TypeStamp "Fp_add" fp_bop_type_num)) []
val enc_fp_top : fp_top -> v
let rec
enc_fp_top FP_Fma = Conv (Just (TypeStamp "Fp_fma" fp_top_type_num)) []
val enc_fp_cmp : fp_cmp -> v
let rec
enc_fp_cmp FP_Equal =
Conv (Just (TypeStamp "Fp_equal" fp_cmp_type_num)) []
and
enc_fp_cmp FP_GreaterEqual =
Conv (Just (TypeStamp "Fp_greaterequal" fp_cmp_type_num)) []
and
enc_fp_cmp FP_Greater =
Conv (Just (TypeStamp "Fp_greater" fp_cmp_type_num)) []
and
enc_fp_cmp FP_LessEqual =
Conv (Just (TypeStamp "Fp_lessequal" fp_cmp_type_num)) []
and
enc_fp_cmp FP_Less =
Conv (Just (TypeStamp "Fp_less" fp_cmp_type_num)) []
val enc_real_uop : real_uop -> v
let rec
enc_real_uop Real_Sqrt = Conv (Just (TypeStamp "Real_sqrt" real_uop_type_num)) []
and
enc_real_uop Real_Neg = Conv (Just (TypeStamp "Real_neg" real_uop_type_num)) []
and
enc_real_uop Real_Abs = Conv (Just (TypeStamp "Real_abs" real_uop_type_num)) []
val enc_real_bop : real_bop -> v
let rec
enc_real_bop Real_Div = Conv (Just (TypeStamp "Real_div" real_bop_type_num)) []
and
enc_real_bop Real_Mul = Conv (Just (TypeStamp "Real_mul" real_bop_type_num)) []
and
enc_real_bop Real_Sub = Conv (Just (TypeStamp "Real_sub" real_bop_type_num)) []
and
enc_real_bop Real_Add = Conv (Just (TypeStamp "Real_add" real_bop_type_num)) []
val enc_real_cmp : real_cmp -> v
let rec
enc_real_cmp Real_Equal =
Conv (Just (TypeStamp "Real_equal" real_cmp_type_num)) []
and
enc_real_cmp Real_GreaterEqual =
Conv (Just (TypeStamp "Real_greaterequal" real_cmp_type_num)) []
and
enc_real_cmp Real_Greater =
Conv (Just (TypeStamp "Real_greater" real_cmp_type_num)) []
and
enc_real_cmp Real_LessEqual =
Conv (Just (TypeStamp "Real_lessequal" real_cmp_type_num)) []
and
enc_real_cmp Real_Less =
Conv (Just (TypeStamp "Real_less" real_cmp_type_num)) []
val nat_to_v : nat -> v
let nat_to_v n = Litv (IntLit (integerFromNat n))
val enc_op : op -> v
let rec
enc_op Eval = Conv (Just (TypeStamp "Eval" op_type_num)) []
and
enc_op Env_id = Conv (Just (TypeStamp "Env_id" op_type_num)) []
and
enc_op (FFI x_15) =
Conv (Just (TypeStamp "Ffi" op_type_num)) [Litv (StrLit x_15)]
and
enc_op ConfigGC = Conv (Just (TypeStamp "Configgc" op_type_num)) []
and
enc_op ListAppend = Conv (Just (TypeStamp "Listappend" op_type_num)) []
and
enc_op Aupdate = Conv (Just (TypeStamp "Aupdate" op_type_num)) []
and
enc_op Alength = Conv (Just (TypeStamp "Alength" op_type_num)) []
and
enc_op Asub = Conv (Just (TypeStamp "Asub" op_type_num)) []
and
enc_op AallocEmpty = Conv (Just (TypeStamp "Aallocempty" op_type_num)) []
and
enc_op Aalloc = Conv (Just (TypeStamp "Aalloc" op_type_num)) []
and
enc_op Aupdate_unsafe =
Conv (Just (TypeStamp "Aupdate_unsafe" op_type_num)) []
and
enc_op Asub_unsafe = Conv (Just (TypeStamp "Asub_unsafe" op_type_num)) []
and
enc_op Vlength = Conv (Just (TypeStamp "Vlength" op_type_num)) []
and
enc_op Vsub = Conv (Just (TypeStamp "Vsub" op_type_num)) []
and
enc_op VfromList = Conv (Just (TypeStamp "Vfromlist" op_type_num)) []
and
enc_op Strcat = Conv (Just (TypeStamp "Strcat" op_type_num)) []
and
enc_op Strlen = Conv (Just (TypeStamp "Strlen" op_type_num)) []
and
enc_op Strsub = Conv (Just (TypeStamp "Strsub" op_type_num)) []
and
enc_op Explode = Conv (Just (TypeStamp "Explode" op_type_num)) []
and
enc_op Implode = Conv (Just (TypeStamp "Implode" op_type_num)) []
and
enc_op (Chopb x_14) =
Conv (Just (TypeStamp "Chopb" op_type_num)) [enc_opb x_14]
and
enc_op Chr = Conv (Just (TypeStamp "Chr_1" op_type_num)) []
and
enc_op Ord = Conv (Just (TypeStamp "Ord" op_type_num)) []
and
enc_op CopyAw8Aw8 = Conv (Just (TypeStamp "Copyaw8aw8" op_type_num)) []
and
enc_op CopyAw8Str = Conv (Just (TypeStamp "Copyaw8str" op_type_num)) []
and
enc_op CopyStrAw8 = Conv (Just (TypeStamp "Copystraw8" op_type_num)) []
and
enc_op CopyStrStr = Conv (Just (TypeStamp "Copystrstr" op_type_num)) []
and
enc_op (WordToInt x_13) =
Conv (Just (TypeStamp "Wordtoint" op_type_num)) [enc_word_size x_13]
and
enc_op (WordFromInt x_12) =
Conv (Just (TypeStamp "Wordfromint" op_type_num)) [enc_word_size x_12]
and
enc_op Aw8update = Conv (Just (TypeStamp "Aw8update" op_type_num)) []
and
enc_op Aw8length = Conv (Just (TypeStamp "Aw8length" op_type_num)) []
and
enc_op Aw8sub = Conv (Just (TypeStamp "Aw8sub" op_type_num)) []
and
enc_op Aw8alloc = Conv (Just (TypeStamp "Aw8alloc" op_type_num)) []
and
enc_op Aw8sub_unsafe = Conv (Just (TypeStamp "Aw8sub_unsafe" op_type_num)) []
and
enc_op Aw8update_unsafe =
Conv (Just (TypeStamp "Aw8update_unsafe" op_type_num)) []
and
enc_op Opderef = Conv (Just (TypeStamp "Opderef" op_type_num)) []
and
enc_op Opref = Conv (Just (TypeStamp "Opref" op_type_num)) []
and
enc_op Opassign = Conv (Just (TypeStamp "Opassign" op_type_num)) []
and
enc_op Opapp = Conv (Just (TypeStamp "Opapp" op_type_num)) []
and
enc_op (FP_top x_11) =
Conv (Just (TypeStamp "Fp_top" op_type_num)) [enc_fp_top x_11]
and
enc_op (FP_bop x_10) =
Conv (Just (TypeStamp "Fp_bop" op_type_num)) [enc_fp_bop x_10]
and
enc_op (FP_uop x_9) =
Conv (Just (TypeStamp "Fp_uop" op_type_num)) [enc_fp_uop x_9]
and
enc_op (FP_cmp x_8) =
Conv (Just (TypeStamp "Fp_cmp" op_type_num)) [enc_fp_cmp x_8]
and
enc_op (FpFromWord) =
Conv (Just (TypeStamp "Fpfromword" op_type_num)) []
and
enc_op (FpToWord) =
Conv (Just (TypeStamp "Fptoword" op_type_num)) []
and
enc_op (Real_bop x_10) =
Conv (Just (TypeStamp "Real_bop" op_type_num)) [enc_real_bop x_10]
and
enc_op (Real_uop x_9) =
Conv (Just (TypeStamp "Real_uop" op_type_num)) [enc_real_uop x_9]
and
enc_op (Real_cmp x_8) =
Conv (Just (TypeStamp "Real_cmp" op_type_num)) [enc_real_cmp x_8]
and
enc_op (RealFromFP) =
Conv (Just (TypeStamp "Realfromfp" op_type_num)) []
and
enc_op Equality = Conv (Just (TypeStamp "Equality" op_type_num)) []
and
enc_op (Shift x_7 x_6 x_5) =
Conv (Just (TypeStamp "Shift" op_type_num))
[enc_word_size x_7; enc_shift x_6; nat_to_v x_5]
and
enc_op (Opw x_4 x_3) =
Conv (Just (TypeStamp "Opw" op_type_num)) [enc_word_size x_4; enc_opw x_3]
and
enc_op (Opb x_2) =
Conv (Just (TypeStamp "Opb" op_type_num)) [enc_opb x_2]
and
enc_op (Opn x_1) =
Conv (Just (TypeStamp "Opn" op_type_num)) [enc_opn x_1]
val maybe_all_list : forall 'a. list (maybe 'a) -> maybe (list 'a)
let rec maybe_all_list v =
match v with
| [] -> Just []
| (Nothing :: _) -> Nothing
| (Just x :: vs) -> match maybe_all_list vs with
| Just xs -> Just (x :: xs)
| Nothing -> Nothing
end
end
val v_to_word8 : v -> maybe word8
let v_to_word8 v = match v with
| Litv (Word8 w) -> Just w
| _ -> Nothing
end
val v_to_word8_list : v -> maybe (list word8)
let v_to_word8_list v = match v_to_list v with
| Just xs -> maybe_all_list (List.map v_to_word8 xs)
| Nothing -> Nothing
end
val v_to_word64 : v -> maybe word64
let v_to_word64 v = match v with
| Litv (Word64 w) -> Just w
| _ -> Nothing
end
val v_to_word64_list : v -> maybe (list word64)
let v_to_word64_list v = match v_to_list v with
| Just xs -> maybe_all_list (List.map v_to_word64 xs)
| Nothing -> Nothing
end
val lookup_env : eval_oracle_state -> (nat * nat) -> maybe (sem_env v)
let lookup_env s (i, j) = match List.index s.envs i with
| Nothing -> Nothing
| Just gen_envs -> List.index gen_envs j
end
val add_decs_generation : eval_decs_state -> eval_decs_state
let add_decs_generation s = match s.env_id_counter with
| (cur_gen, next_id, next_gen) -> <| s with env_id_counter =
(next_gen, 0, next_gen + 1) |>
end
val add_env_generation : eval_oracle_state -> eval_oracle_state
let add_env_generation s = <| s with
generation = List.length s.envs ;
envs = List.append s.envs [[]] |>
val declare_env : maybe eval_state -> sem_env v -> maybe (v * maybe eval_state)
let declare_env es env = match es with
| Nothing -> Nothing
| Just (EvalDecs s) -> match s.env_id_counter with
| (cur_gen, next_id, next_gen) -> Just (Env env (cur_gen, next_id),
Just (EvalDecs <| s with env_id_counter =
(cur_gen, next_id + 1, next_gen) |>))
end
| Just (EvalOracle s) -> match (List.index s.envs s.generation) with
| Just gen_envs -> Just (Conv Nothing
[nat_to_v s.generation; nat_to_v (List.length gen_envs)],
Just (EvalOracle <| s with envs = List.update s.envs s.generation