forked from rems-project/sail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast_util.ml
2284 lines (2004 loc) · 96.9 KB
/
ast_util.ml
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
(**************************************************************************)
(* Sail *)
(* *)
(* Copyright (c) 2013-2017 *)
(* Kathyrn Gray *)
(* Shaked Flur *)
(* Stephen Kell *)
(* Gabriel Kerneis *)
(* Robert Norton-Wright *)
(* Christopher Pulte *)
(* Peter Sewell *)
(* Alasdair Armstrong *)
(* Brian Campbell *)
(* Thomas Bauereiss *)
(* Anthony Fox *)
(* Jon French *)
(* Dominic Mulligan *)
(* Stephen Kell *)
(* Mark Wassell *)
(* *)
(* All rights reserved. *)
(* *)
(* This software was developed by the University of Cambridge Computer *)
(* Laboratory as part of the Rigorous Engineering of Mainstream Systems *)
(* (REMS) project, funded by EPSRC grant EP/K008528/1. *)
(* *)
(* Redistribution and use in source and binary forms, with or without *)
(* modification, are permitted provided that the following conditions *)
(* are met: *)
(* 1. Redistributions of source code must retain the above copyright *)
(* notice, this list of conditions and the following disclaimer. *)
(* 2. Redistributions in binary form must reproduce the above copyright *)
(* notice, this list of conditions and the following disclaimer in *)
(* the documentation and/or other materials provided with the *)
(* distribution. *)
(* *)
(* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' *)
(* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *)
(* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *)
(* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR *)
(* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *)
(* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *)
(* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF *)
(* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *)
(* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, *)
(* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT *)
(* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF *)
(* SUCH DAMAGE. *)
(**************************************************************************)
open Ast
open Util
module Big_int = Nat_big_num
type mut = Immutable | Mutable
type 'a lvar = Register of effect * effect * 'a | Enum of 'a | Local of mut * 'a | Unbound
let lvar_typ = function
| Local (_, typ) -> typ
| Register (_, _, typ) -> typ
| Enum typ -> typ
| Unbound -> Reporting.unreachable Parse_ast.Unknown __POS__ "No type for unbound variable"
let no_annot = (Parse_ast.Unknown, ())
let gen_loc l = Parse_ast.Generated l
let inc_ord = Ord_aux (Ord_inc, Parse_ast.Unknown)
let dec_ord = Ord_aux (Ord_dec, Parse_ast.Unknown)
let mk_id str = Id_aux (Id str, Parse_ast.Unknown)
let mk_nc nc_aux = NC_aux (nc_aux, Parse_ast.Unknown)
let mk_nexp nexp_aux = Nexp_aux (nexp_aux, Parse_ast.Unknown)
let mk_exp ?loc:(l=Parse_ast.Unknown) exp_aux = E_aux (exp_aux, (l, ()))
let unaux_exp (E_aux (exp_aux, _)) = exp_aux
let uncast_exp = function
| E_aux (E_internal_return (E_aux (E_cast (typ, exp), _)), a) ->
E_aux (E_internal_return exp, a), Some typ
| E_aux (E_cast (typ, exp), _) -> exp, Some typ
| exp -> exp, None
let mk_pat pat_aux = P_aux (pat_aux, no_annot)
let unaux_pat (P_aux (pat_aux, _)) = pat_aux
let untyp_pat = function
| P_aux (P_typ (typ, pat), _) -> pat, Some typ
| pat -> pat, None
let mk_pexp ?loc:(l=Parse_ast.Unknown) pexp_aux = Pat_aux (pexp_aux, (l, ()))
let mk_mpat mpat_aux = MP_aux (mpat_aux, no_annot)
let mk_mpexp mpexp_aux = MPat_aux (mpexp_aux, no_annot)
let mk_lexp lexp_aux = LEXP_aux (lexp_aux, no_annot)
let mk_typ_pat tpat_aux = TP_aux (tpat_aux, Parse_ast.Unknown)
let mk_lit lit_aux = L_aux (lit_aux, Parse_ast.Unknown)
let mk_lit_exp lit_aux = mk_exp (E_lit (mk_lit lit_aux))
let mk_funcl id pat body = FCL_aux (FCL_Funcl (id, Pat_aux (Pat_exp (pat, body),no_annot)), no_annot)
let mk_qi_nc nc = QI_aux (QI_constraint nc, Parse_ast.Unknown)
let mk_qi_id k kid =
let kopt =
KOpt_aux (KOpt_kind (K_aux (k, Parse_ast.Unknown), kid), Parse_ast.Unknown)
in
QI_aux (QI_id kopt, Parse_ast.Unknown)
let mk_qi_kopt kopt = QI_aux (QI_id kopt, Parse_ast.Unknown)
let mk_fundef funcls =
let tannot_opt = Typ_annot_opt_aux (Typ_annot_opt_none, Parse_ast.Unknown) in
let effect_opt = Effect_opt_aux (Effect_opt_none, Parse_ast.Unknown) in
let rec_opt = Rec_aux (Rec_nonrec, Parse_ast.Unknown) in
DEF_fundef
(FD_aux (FD_function (rec_opt, tannot_opt, effect_opt, funcls), no_annot))
let mk_letbind pat exp = LB_aux (LB_val (pat, exp), no_annot)
let mk_val_spec vs_aux =
DEF_spec (VS_aux (vs_aux, no_annot))
let kopt_kid (KOpt_aux (KOpt_kind (_, kid), _)) = kid
let kopt_kind (KOpt_aux (KOpt_kind (k, _), _)) = k
let is_int_kopt = function
| KOpt_aux (KOpt_kind (K_aux (K_int, _), _), _) -> true
| _ -> false
let is_order_kopt = function
| KOpt_aux (KOpt_kind (K_aux (K_order, _), _), _) -> true
| _ -> false
let is_typ_kopt = function
| KOpt_aux (KOpt_kind (K_aux (K_type, _), _), _) -> true
| _ -> false
let is_bool_kopt = function
| KOpt_aux (KOpt_kind (K_aux (K_bool, _), _), _) -> true
| _ -> false
let string_of_kid = function
| Kid_aux (Var v, _) -> v
module Kid = struct
type t = kid
let compare kid1 kid2 = String.compare (string_of_kid kid1) (string_of_kid kid2)
end
module Kind = struct
type t = kind
let compare (K_aux (aux1, _)) (K_aux (aux2, _)) =
match aux1, aux2 with
| K_int, K_int -> 0
| K_type, K_type -> 0
| K_order, K_order -> 0
| K_bool, K_bool -> 0
| K_int, _ -> 1 | _, K_int -> -1
| K_type, _ -> 1 | _, K_type -> -1
| K_order, _ -> 1 | _, K_order -> -1
end
module KOpt = struct
type t = kinded_id
let compare kopt1 kopt2 =
let lex_ord c1 c2 = if c1 = 0 then c2 else c1 in
lex_ord (Kid.compare (kopt_kid kopt1) (kopt_kid kopt2))
(Kind.compare (kopt_kind kopt1) (kopt_kind kopt2))
end
module Id = struct
type t = id
let compare id1 id2 =
match (id1, id2) with
| Id_aux (Id x, _), Id_aux (Id y, _) -> String.compare x y
| Id_aux (Operator x, _), Id_aux (Operator y, _) -> String.compare x y
| Id_aux (Id _, _), Id_aux (Operator _, _) -> -1
| Id_aux (Operator _, _), Id_aux (Id _, _) -> 1
end
module Nexp = struct
type t = nexp
let rec compare (Nexp_aux (nexp1, _)) (Nexp_aux (nexp2, _)) =
let lex_ord (c1, c2) = if c1 = 0 then c2 else c1 in
match nexp1, nexp2 with
| Nexp_id v1, Nexp_id v2 -> Id.compare v1 v2
| Nexp_var kid1, Nexp_var kid2 -> Kid.compare kid1 kid2
| Nexp_constant c1, Nexp_constant c2 -> Big_int.compare c1 c2
| Nexp_app (op1, args1), Nexp_app (op2, args2) ->
let lex1 = Id.compare op1 op2 in
let lex2 = List.length args1 - List.length args2 in
let lex3 =
if lex2 = 0 then
List.fold_left2 (fun l n1 n2 -> lex_ord (l, compare n1 n2)) 0 args1 args2
else 0
in
lex_ord (lex1, lex_ord (lex2, lex3))
| Nexp_times (n1a, n1b), Nexp_times (n2a, n2b)
| Nexp_sum (n1a, n1b), Nexp_sum (n2a, n2b)
| Nexp_minus (n1a, n1b), Nexp_minus (n2a, n2b) ->
lex_ord (compare n1a n2a, compare n1b n2b)
| Nexp_exp n1, Nexp_exp n2 -> compare n1 n2
| Nexp_neg n1, Nexp_neg n2 -> compare n1 n2
| Nexp_constant _, _ -> -1 | _, Nexp_constant _ -> 1
| Nexp_id _, _ -> -1 | _, Nexp_id _ -> 1
| Nexp_var _, _ -> -1 | _, Nexp_var _ -> 1
| Nexp_neg _, _ -> -1 | _, Nexp_neg _ -> 1
| Nexp_exp _, _ -> -1 | _, Nexp_exp _ -> 1
| Nexp_minus _, _ -> -1 | _, Nexp_minus _ -> 1
| Nexp_sum _, _ -> -1 | _, Nexp_sum _ -> 1
| Nexp_times _, _ -> -1 | _, Nexp_times _ -> 1
end
module Bindings = Map.Make(Id)
module IdSet = Set.Make(Id)
module KBindings = Map.Make(Kid)
module KidSet = Set.Make(Kid)
module KOptSet = Set.Make(KOpt)
module KOptMap = Map.Make(KOpt)
module NexpSet = Set.Make(Nexp)
module NexpMap = Map.Make(Nexp)
let rec nexp_identical nexp1 nexp2 = (Nexp.compare nexp1 nexp2 = 0)
let rec is_nexp_constant (Nexp_aux (nexp, _)) = match nexp with
| Nexp_id _ | Nexp_var _ -> false
| Nexp_constant _ -> true
| Nexp_times (n1, n2) | Nexp_sum (n1, n2) | Nexp_minus (n1, n2) ->
is_nexp_constant n1 && is_nexp_constant n2
| Nexp_exp n | Nexp_neg n -> is_nexp_constant n
| Nexp_app (_, nexps) -> List.for_all is_nexp_constant nexps
let int_of_nexp_opt nexp =
match nexp with
| Nexp_aux(Nexp_constant i,_) -> Some i
| _ -> None
let rec nexp_simp (Nexp_aux (nexp, l)) = Nexp_aux (nexp_simp_aux nexp, l)
and nexp_simp_aux = function
(* (n - (n - m)) often appears in foreach loops *)
| Nexp_minus (nexp1, Nexp_aux (Nexp_minus (nexp2, Nexp_aux (n3,_)),_))
when nexp_identical nexp1 nexp2 ->
nexp_simp_aux n3
| Nexp_minus (Nexp_aux (Nexp_sum (Nexp_aux (n1, _), nexp2), _), nexp3)
when nexp_identical nexp2 nexp3 ->
nexp_simp_aux n1
| Nexp_sum (Nexp_aux (Nexp_minus (Nexp_aux (n1, _), nexp2), _), nexp3)
when nexp_identical nexp2 nexp3 ->
nexp_simp_aux n1
| Nexp_sum (n1, n2) ->
begin
let (Nexp_aux (n1_simp, _) as n1) = nexp_simp n1 in
let (Nexp_aux (n2_simp, _) as n2) = nexp_simp n2 in
match n1_simp, n2_simp with
| Nexp_constant c1, Nexp_constant c2 -> Nexp_constant (Big_int.add c1 c2)
| _, Nexp_neg n2 -> Nexp_minus (n1, n2)
| _, _ -> Nexp_sum (n1, n2)
end
| Nexp_times (n1, n2) ->
begin
let (Nexp_aux (n1_simp, _) as n1) = nexp_simp n1 in
let (Nexp_aux (n2_simp, _) as n2) = nexp_simp n2 in
match n1_simp, n2_simp with
| Nexp_constant c, _ when Big_int.equal c (Big_int.of_int 1) -> n2_simp
| _, Nexp_constant c when Big_int.equal c (Big_int.of_int 1) -> n1_simp
| Nexp_constant c1, Nexp_constant c2 -> Nexp_constant (Big_int.mul c1 c2)
| _, _ -> Nexp_times (n1, n2)
end
| Nexp_minus (n1, n2) ->
begin
let (Nexp_aux (n1_simp, _) as n1) = nexp_simp n1 in
let (Nexp_aux (n2_simp, _) as n2) = nexp_simp n2 in
match n1_simp, n2_simp with
| Nexp_constant c1, Nexp_constant c2 -> Nexp_constant (Big_int.sub c1 c2)
(* A vector range x['n-1 .. 0] can result in the size "('n-1) - -1" *)
| Nexp_minus (Nexp_aux (n,_), Nexp_aux (Nexp_constant c1,_)), Nexp_constant c2
when Big_int.equal c1 (Big_int.negate c2) -> n
| _, _ -> Nexp_minus (n1, n2)
end
| Nexp_neg n ->
begin
let (Nexp_aux (n_simp, _) as n) = nexp_simp n in
match n_simp with
| Nexp_constant c -> Nexp_constant (Big_int.negate c)
| _ -> Nexp_neg n
end
| Nexp_app (Id_aux (Id "div",_) as id,[n1;n2]) ->
begin
let (Nexp_aux (n1_simp, _) as n1) = nexp_simp n1 in
let (Nexp_aux (n2_simp, _) as n2) = nexp_simp n2 in
match n1_simp, n2_simp with
| Nexp_constant c1, Nexp_constant c2 -> Nexp_constant (Big_int.div c1 c2)
| _, _ -> Nexp_app (id,[n1;n2])
end
| nexp -> nexp
let rec constraint_simp (NC_aux (nc_aux, l)) =
let nc_aux = match nc_aux with
| NC_equal (nexp1, nexp2) ->
let nexp1, nexp2 = nexp_simp nexp1, nexp_simp nexp2 in
if nexp_identical nexp1 nexp2 then
NC_true
else
NC_equal (nexp1, nexp2)
| NC_and (nc1, nc2) ->
let nc1, nc2 = constraint_simp nc1, constraint_simp nc2 in
begin match nc1, nc2 with
| NC_aux (NC_true, _), NC_aux (nc, _) -> nc
| NC_aux (nc, _), NC_aux (NC_true, _) -> nc
| _, _ -> NC_and (nc1, nc2)
end
| NC_or (nc1, nc2) ->
let nc1, nc2 = constraint_simp nc1, constraint_simp nc2 in
begin match nc1, nc2 with
| NC_aux (NC_false, _), NC_aux (nc, _) -> nc
| NC_aux (nc, _), NC_aux (NC_false, _) -> nc
| NC_aux (NC_true, _), NC_aux (nc, _) -> NC_true
| NC_aux (nc, _), NC_aux (NC_true, _) -> NC_true
| _, _ -> NC_or (nc1, nc2)
end
| NC_bounded_ge (nexp1, nexp2) ->
let nexp1, nexp2 = nexp_simp nexp1, nexp_simp nexp2 in
begin match nexp1, nexp2 with
| Nexp_aux (Nexp_constant c1, _), Nexp_aux (Nexp_constant c2, _) ->
if Big_int.greater_equal c1 c2 then NC_true else NC_false
| _, _ -> NC_bounded_ge (nexp1, nexp2)
end
| NC_bounded_gt (nexp1, nexp2) ->
let nexp1, nexp2 = nexp_simp nexp1, nexp_simp nexp2 in
begin match nexp1, nexp2 with
| Nexp_aux (Nexp_constant c1, _), Nexp_aux (Nexp_constant c2, _) ->
if Big_int.greater c1 c2 then NC_true else NC_false
| _, _ -> NC_bounded_gt (nexp1, nexp2)
end
| NC_bounded_le (nexp1, nexp2) ->
let nexp1, nexp2 = nexp_simp nexp1, nexp_simp nexp2 in
begin match nexp1, nexp2 with
| Nexp_aux (Nexp_constant c1, _), Nexp_aux (Nexp_constant c2, _) ->
if Big_int.less_equal c1 c2 then NC_true else NC_false
| _, _ -> NC_bounded_le (nexp1, nexp2)
end
| NC_bounded_lt (nexp1, nexp2) ->
let nexp1, nexp2 = nexp_simp nexp1, nexp_simp nexp2 in
begin match nexp1, nexp2 with
| Nexp_aux (Nexp_constant c1, _), Nexp_aux (Nexp_constant c2, _) ->
if Big_int.less c1 c2 then NC_true else NC_false
| _, _ -> NC_bounded_lt (nexp1, nexp2)
end
| _ -> nc_aux
in
NC_aux (nc_aux, l)
let rec constraint_conj (NC_aux (nc_aux, l) as nc) =
match nc_aux with
| NC_and (nc1, nc2) -> constraint_conj nc1 @ constraint_conj nc2
| _ -> [nc]
let rec constraint_disj (NC_aux (nc_aux, l) as nc) =
match nc_aux with
| NC_or (nc1, nc2) -> constraint_disj nc1 @ constraint_disj nc2
| _ -> [nc]
let mk_typ typ = Typ_aux (typ, Parse_ast.Unknown)
let mk_typ_arg arg = A_aux (arg, Parse_ast.Unknown)
let mk_kid str = Kid_aux (Var ("'" ^ str), Parse_ast.Unknown)
let mk_infix_id str = Id_aux (Operator str, Parse_ast.Unknown)
let mk_id_typ id = Typ_aux (Typ_id id, Parse_ast.Unknown)
let mk_kopt kind_aux id =
KOpt_aux (KOpt_kind (K_aux (kind_aux, Parse_ast.Unknown), id), Parse_ast.Unknown)
let mk_ord ord_aux = Ord_aux (ord_aux, Parse_ast.Unknown)
let unknown_typ = mk_typ Typ_internal_unknown
let int_typ = mk_id_typ (mk_id "int")
let nat_typ = mk_id_typ (mk_id "nat")
let unit_typ = mk_id_typ (mk_id "unit")
let bit_typ = mk_id_typ (mk_id "bit")
let real_typ = mk_id_typ (mk_id "real")
let app_typ id args = mk_typ (Typ_app (id, args))
let register_typ typ = mk_typ (Typ_app (mk_id "register", [mk_typ_arg (A_typ typ)]))
let atom_typ nexp =
mk_typ (Typ_app (mk_id "atom", [mk_typ_arg (A_nexp (nexp_simp nexp))]))
let implicit_typ nexp =
mk_typ (Typ_app (mk_id "implicit", [mk_typ_arg (A_nexp (nexp_simp nexp))]))
let range_typ nexp1 nexp2 =
mk_typ (Typ_app (mk_id "range", [mk_typ_arg (A_nexp (nexp_simp nexp1));
mk_typ_arg (A_nexp (nexp_simp nexp2))]))
let bool_typ = mk_id_typ (mk_id "bool")
let atom_bool_typ nc = mk_typ (Typ_app (mk_id "atom_bool", [mk_typ_arg (A_bool nc)]))
let string_typ = mk_id_typ (mk_id "string")
let list_typ typ = mk_typ (Typ_app (mk_id "list", [mk_typ_arg (A_typ typ)]))
let tuple_typ typs = mk_typ (Typ_tup typs)
let function_typ arg_typs ret_typ eff = mk_typ (Typ_fn (arg_typs, ret_typ, eff))
let vector_typ n ord typ =
mk_typ (Typ_app (mk_id "vector",
[mk_typ_arg (A_nexp (nexp_simp n));
mk_typ_arg (A_order ord);
mk_typ_arg (A_typ typ)]))
let bitvector_typ n ord =
mk_typ (Typ_app (mk_id "bitvector",
[mk_typ_arg (A_nexp (nexp_simp n));
mk_typ_arg (A_order ord)]))
let exc_typ = mk_id_typ (mk_id "exception")
let nconstant c = Nexp_aux (Nexp_constant c, Parse_ast.Unknown)
let nint i = nconstant (Big_int.of_int i)
let nminus n1 n2 = Nexp_aux (Nexp_minus (n1, n2), Parse_ast.Unknown)
let nsum n1 n2 = Nexp_aux (Nexp_sum (n1, n2), Parse_ast.Unknown)
let ntimes n1 n2 = Nexp_aux (Nexp_times (n1, n2), Parse_ast.Unknown)
let npow2 n = Nexp_aux (Nexp_exp n, Parse_ast.Unknown)
let nvar kid = Nexp_aux (Nexp_var kid, Parse_ast.Unknown)
let nid id = Nexp_aux (Nexp_id id, Parse_ast.Unknown)
let napp id args = Nexp_aux (Nexp_app (id, args), Parse_ast.Unknown)
let nc_set kid nums = mk_nc (NC_set (kid, nums))
let nc_int_set kid ints = mk_nc (NC_set (kid, List.map Big_int.of_int ints))
let nc_eq n1 n2 = mk_nc (NC_equal (n1, n2))
let nc_neq n1 n2 = mk_nc (NC_not_equal (n1, n2))
let nc_lteq n1 n2 = NC_aux (NC_bounded_le (n1, n2), Parse_ast.Unknown)
let nc_lt n1 n2 = NC_aux (NC_bounded_lt (n1, n2), Parse_ast.Unknown)
let nc_gteq n1 n2 = NC_aux (NC_bounded_ge (n1, n2), Parse_ast.Unknown)
let nc_gt n1 n2 = NC_aux (NC_bounded_gt (n1, n2), Parse_ast.Unknown)
let nc_var kid = mk_nc (NC_var kid)
let nc_true = mk_nc NC_true
let nc_false = mk_nc NC_false
let nc_or nc1 nc2 =
match nc1, nc2 with
| _, NC_aux (NC_false, _) -> nc1
| NC_aux (NC_false, _), _ -> nc2
| _, _ -> mk_nc (NC_or (nc1, nc2))
let nc_and nc1 nc2 =
match nc1, nc2 with
| _, NC_aux (NC_true, _) -> nc1
| NC_aux (NC_true, _), _ -> nc2
| _, _ -> mk_nc (NC_and (nc1, nc2))
let arg_nexp ?loc:(l=Parse_ast.Unknown) n = A_aux (A_nexp n, l)
let arg_order ?loc:(l=Parse_ast.Unknown) ord = A_aux (A_order ord, l)
let arg_typ ?loc:(l=Parse_ast.Unknown) typ = A_aux (A_typ typ, l)
let arg_bool ?loc:(l=Parse_ast.Unknown) nc = A_aux (A_bool nc, l)
let arg_kopt (KOpt_aux (KOpt_kind (K_aux (k, _), v), l)) =
match k with
| K_int -> arg_nexp (nvar v)
| K_order -> arg_order (Ord_aux (Ord_var v, l))
| K_bool -> arg_bool (nc_var v)
| K_type -> arg_typ (mk_typ (Typ_var v))
let nc_not nc = mk_nc (NC_app (mk_id "not", [arg_bool nc]))
let mk_typschm typq typ = TypSchm_aux (TypSchm_ts (typq, typ), Parse_ast.Unknown)
let mk_typquant qis = TypQ_aux (TypQ_tq qis, Parse_ast.Unknown)
let mk_fexp id exp = FE_aux (FE_Fexp (id, exp), no_annot)
let mk_effect effs =
Effect_aux (Effect_set (List.map (fun be_aux -> BE_aux (be_aux, Parse_ast.Unknown)) effs), Parse_ast.Unknown)
let no_effect = mk_effect []
let quant_add qi typq =
match qi, typq with
| QI_aux (QI_constraint (NC_aux (NC_true, _)), _), _ -> typq
| QI_aux (QI_id _, _), TypQ_aux (TypQ_tq qis, l) -> TypQ_aux (TypQ_tq (qi :: qis), l)
| QI_aux (QI_constant _, _), TypQ_aux (TypQ_tq qis, l) -> TypQ_aux (TypQ_tq (qis @ [qi]), l)
| QI_aux (QI_constraint nc, _), TypQ_aux (TypQ_tq qis, l) -> TypQ_aux (TypQ_tq (qis @ [qi]), l)
| _, TypQ_aux (TypQ_no_forall, l) -> TypQ_aux (TypQ_tq [qi], l)
let quant_items : typquant -> quant_item list = function
| TypQ_aux (TypQ_tq qis, _) -> qis
| TypQ_aux (TypQ_no_forall, _) -> []
let quant_kopts typq =
let qi_kopt = function
| QI_aux (QI_id kopt, _) -> [kopt]
| QI_aux _ -> []
in
quant_items typq |> List.map qi_kopt |> List.concat
let quant_split typq =
let qi_kopt = function
| QI_aux (QI_id kopt, _) -> [kopt]
| _ -> []
in
let qi_nc = function
| QI_aux (QI_constraint nc, _) -> [nc]
| _ -> []
in
let qis = quant_items typq in
List.concat (List.map qi_kopt qis), List.concat (List.map qi_nc qis)
let quant_map_items f = function
| TypQ_aux (TypQ_no_forall, l) -> TypQ_aux (TypQ_no_forall, l)
| TypQ_aux (TypQ_tq qis, l) -> TypQ_aux (TypQ_tq (List.map f qis), l)
let is_quant_kopt = function
| QI_aux (QI_id _, _) -> true
| _ -> false
let is_quant_constraint = function
| QI_aux (QI_constraint _, _) -> true
| _ -> false
let unaux_nexp (Nexp_aux (nexp, _)) = nexp
let unaux_order (Ord_aux (ord, _)) = ord
let unaux_typ (Typ_aux (typ, _)) = typ
let unaux_kind (K_aux (k, _)) = k
let unaux_constraint (NC_aux (nc, _)) = nc
let rec map_exp_annot f (E_aux (exp, annot)) = E_aux (map_exp_annot_aux f exp, f annot)
and map_exp_annot_aux f = function
| E_block xs -> E_block (List.map (map_exp_annot f) xs)
| E_id id -> E_id id
| E_ref id -> E_ref id
| E_lit lit -> E_lit lit
| E_cast (typ, exp) -> E_cast (typ, map_exp_annot f exp)
| E_app (id, xs) -> E_app (id, List.map (map_exp_annot f) xs)
| E_app_infix (x, op, y) -> E_app_infix (map_exp_annot f x, op, map_exp_annot f y)
| E_tuple xs -> E_tuple (List.map (map_exp_annot f) xs)
| E_if (cond, t, e) -> E_if (map_exp_annot f cond, map_exp_annot f t, map_exp_annot f e)
| E_for (v, e1, e2, e3, o, e4) -> E_for (v, map_exp_annot f e1, map_exp_annot f e2, map_exp_annot f e3, o, map_exp_annot f e4)
| E_loop (loop_type, measure, e1, e2) -> E_loop (loop_type, map_measure_annot f measure, map_exp_annot f e1, map_exp_annot f e2)
| E_vector exps -> E_vector (List.map (map_exp_annot f) exps)
| E_vector_access (exp1, exp2) -> E_vector_access (map_exp_annot f exp1, map_exp_annot f exp2)
| E_vector_subrange (exp1, exp2, exp3) -> E_vector_subrange (map_exp_annot f exp1, map_exp_annot f exp2, map_exp_annot f exp3)
| E_vector_update (exp1, exp2, exp3) -> E_vector_update (map_exp_annot f exp1, map_exp_annot f exp2, map_exp_annot f exp3)
| E_vector_update_subrange (exp1, exp2, exp3, exp4) ->
E_vector_update_subrange (map_exp_annot f exp1, map_exp_annot f exp2, map_exp_annot f exp3, map_exp_annot f exp4)
| E_vector_append (exp1, exp2) -> E_vector_append (map_exp_annot f exp1, map_exp_annot f exp2)
| E_list xs -> E_list (List.map (map_exp_annot f) xs)
| E_cons (exp1, exp2) -> E_cons (map_exp_annot f exp1, map_exp_annot f exp2)
| E_record fexps -> E_record (List.map (map_fexp_annot f) fexps)
| E_record_update (exp, fexps) -> E_record_update (map_exp_annot f exp, List.map (map_fexp_annot f) fexps)
| E_field (exp, id) -> E_field (map_exp_annot f exp, id)
| E_case (exp, cases) -> E_case (map_exp_annot f exp, List.map (map_pexp_annot f) cases)
| E_try (exp, cases) -> E_try (map_exp_annot f exp, List.map (map_pexp_annot f) cases)
| E_let (letbind, exp) -> E_let (map_letbind_annot f letbind, map_exp_annot f exp)
| E_assign (lexp, exp) -> E_assign (map_lexp_annot f lexp, map_exp_annot f exp)
| E_sizeof nexp -> E_sizeof nexp
| E_constraint nc -> E_constraint nc
| E_exit exp -> E_exit (map_exp_annot f exp)
| E_throw exp -> E_throw (map_exp_annot f exp)
| E_return exp -> E_return (map_exp_annot f exp)
| E_assert (test, msg) -> E_assert (map_exp_annot f test, map_exp_annot f msg)
| E_internal_value v -> E_internal_value v
| E_var (lexp, exp1, exp2) -> E_var (map_lexp_annot f lexp, map_exp_annot f exp1, map_exp_annot f exp2)
| E_internal_plet (pat, exp1, exp2) -> E_internal_plet (map_pat_annot f pat, map_exp_annot f exp1, map_exp_annot f exp2)
| E_internal_return exp -> E_internal_return (map_exp_annot f exp)
and map_measure_annot f (Measure_aux (m, l)) = Measure_aux (map_measure_annot_aux f m, l)
and map_measure_annot_aux f = function
| Measure_none -> Measure_none
| Measure_some exp -> Measure_some (map_exp_annot f exp)
and map_opt_default_annot f (Def_val_aux (df, annot)) = Def_val_aux (map_opt_default_annot_aux f df, f annot)
and map_opt_default_annot_aux f = function
| Def_val_empty -> Def_val_empty
| Def_val_dec exp -> Def_val_dec (map_exp_annot f exp)
and map_fexp_annot f (FE_aux (FE_Fexp (id, exp), annot)) = FE_aux (FE_Fexp (id, map_exp_annot f exp), f annot)
and map_pexp_annot f (Pat_aux (pexp, annot)) = Pat_aux (map_pexp_annot_aux f pexp, f annot)
and map_pexp_annot_aux f = function
| Pat_exp (pat, exp) -> Pat_exp (map_pat_annot f pat, map_exp_annot f exp)
| Pat_when (pat, guard, exp) -> Pat_when (map_pat_annot f pat, map_exp_annot f guard, map_exp_annot f exp)
and map_pat_annot f (P_aux (pat, annot)) = P_aux (map_pat_annot_aux f pat, f annot)
and map_pat_annot_aux f = function
| P_lit lit -> P_lit lit
| P_wild -> P_wild
| P_or (pat1, pat2) -> P_or (map_pat_annot f pat1, map_pat_annot f pat2)
| P_not pat -> P_not (map_pat_annot f pat)
| P_as (pat, id) -> P_as (map_pat_annot f pat, id)
| P_typ (typ, pat) -> P_typ (typ, map_pat_annot f pat)
| P_id id -> P_id id
| P_var (pat, tpat) -> P_var (map_pat_annot f pat, tpat)
| P_app (id, pats) -> P_app (id, List.map (map_pat_annot f) pats)
| P_tup pats -> P_tup (List.map (map_pat_annot f) pats)
| P_list pats -> P_list (List.map (map_pat_annot f) pats)
| P_vector_concat pats -> P_vector_concat (List.map (map_pat_annot f) pats)
| P_vector pats -> P_vector (List.map (map_pat_annot f) pats)
| P_cons (pat1, pat2) -> P_cons (map_pat_annot f pat1, map_pat_annot f pat2)
| P_string_append pats -> P_string_append (List.map (map_pat_annot f) pats)
and map_mpexp_annot f (MPat_aux (mpexp, annot)) = MPat_aux (map_mpexp_annot_aux f mpexp, f annot)
and map_mpexp_annot_aux f = function
| MPat_pat mpat -> MPat_pat (map_mpat_annot f mpat)
| MPat_when (mpat, guard) -> MPat_when (map_mpat_annot f mpat, map_exp_annot f guard)
and map_mapcl_annot f = function
| (MCL_aux (MCL_bidir (mpexp1, mpexp2), annot)) ->
MCL_aux (MCL_bidir (map_mpexp_annot f mpexp1, map_mpexp_annot f mpexp2), f annot)
| (MCL_aux (MCL_forwards (mpexp, exp), annot)) ->
MCL_aux (MCL_forwards (map_mpexp_annot f mpexp, map_exp_annot f exp), f annot)
| (MCL_aux (MCL_backwards (mpexp, exp), annot)) ->
MCL_aux (MCL_backwards (map_mpexp_annot f mpexp, map_exp_annot f exp), f annot)
and map_mpat_annot f (MP_aux (mpat, annot)) = MP_aux (map_mpat_annot_aux f mpat, f annot)
and map_mpat_annot_aux f = function
| MP_lit lit -> MP_lit lit
| MP_id id -> MP_id id
| MP_app (id, mpats) -> MP_app (id, List.map (map_mpat_annot f) mpats)
| MP_tup mpats -> MP_tup (List.map (map_mpat_annot f) mpats)
| MP_list mpats -> MP_list (List.map (map_mpat_annot f) mpats)
| MP_vector_concat mpats -> MP_vector_concat (List.map (map_mpat_annot f) mpats)
| MP_vector mpats -> MP_vector (List.map (map_mpat_annot f) mpats)
| MP_cons (mpat1, mpat2) -> MP_cons (map_mpat_annot f mpat1, map_mpat_annot f mpat2)
| MP_string_append mpats -> MP_string_append (List.map (map_mpat_annot f) mpats)
| MP_typ (mpat, typ) -> MP_typ (map_mpat_annot f mpat, typ)
| MP_as (mpat, id) -> MP_as (map_mpat_annot f mpat, id)
and map_letbind_annot f (LB_aux (lb, annot)) = LB_aux (map_letbind_annot_aux f lb, f annot)
and map_letbind_annot_aux f = function
| LB_val (pat, exp) -> LB_val (map_pat_annot f pat, map_exp_annot f exp)
and map_lexp_annot f (LEXP_aux (lexp, annot)) = LEXP_aux (map_lexp_annot_aux f lexp, f annot)
and map_lexp_annot_aux f = function
| LEXP_id id -> LEXP_id id
| LEXP_deref exp -> LEXP_deref (map_exp_annot f exp)
| LEXP_memory (id, exps) -> LEXP_memory (id, List.map (map_exp_annot f) exps)
| LEXP_cast (typ, id) -> LEXP_cast (typ, id)
| LEXP_tup lexps -> LEXP_tup (List.map (map_lexp_annot f) lexps)
| LEXP_vector_concat lexps -> LEXP_vector_concat (List.map (map_lexp_annot f) lexps)
| LEXP_vector (lexp, exp) -> LEXP_vector (map_lexp_annot f lexp, map_exp_annot f exp)
| LEXP_vector_range (lexp, exp1, exp2) -> LEXP_vector_range (map_lexp_annot f lexp, map_exp_annot f exp1, map_exp_annot f exp2)
| LEXP_field (lexp, id) -> LEXP_field (map_lexp_annot f lexp, id)
and map_typedef_annot f = function
| TD_aux (td_aux, annot) -> TD_aux (td_aux, f annot)
and map_fundef_annot f = function
| FD_aux (fd_aux, annot) -> FD_aux (map_fundef_annot_aux f fd_aux, f annot)
and map_fundef_annot_aux f = function
| FD_function (rec_opt, tannot_opt, eff_opt, funcls) -> FD_function (map_recopt_annot f rec_opt, tannot_opt, eff_opt,
List.map (map_funcl_annot f) funcls)
and map_funcl_annot f = function
| FCL_aux (fcl, annot) -> FCL_aux (map_funcl_annot_aux f fcl, f annot)
and map_funcl_annot_aux f = function
| FCL_Funcl (id, pexp) -> FCL_Funcl (id, map_pexp_annot f pexp)
and map_recopt_annot f = function
| Rec_aux (rec_aux, l) -> Rec_aux (map_recopt_annot_aux f rec_aux, l)
and map_recopt_annot_aux f = function
| Rec_nonrec -> Rec_nonrec
| Rec_rec -> Rec_rec
| Rec_measure (pat, exp) -> Rec_measure (map_pat_annot f pat, map_exp_annot f exp)
and map_mapdef_annot f = function
| MD_aux (md_aux, annot) -> MD_aux (map_mapdef_annot_aux f md_aux, f annot)
and map_mapdef_annot_aux f = function
| MD_mapping (id, tannot_opt, mapcls) -> MD_mapping (id, tannot_opt, List.map (map_mapcl_annot f) mapcls)
and map_valspec_annot f = function
| VS_aux (vs_aux, annot) -> VS_aux (vs_aux, f annot)
and map_scattered_annot f = function
| SD_aux (sd_aux, annot) -> SD_aux (map_scattered_annot_aux f sd_aux, f annot)
and map_scattered_annot_aux f = function
| SD_function (rec_opt, tannot_opt, eff_opt, name) -> SD_function (map_recopt_annot f rec_opt, tannot_opt, eff_opt, name)
| SD_funcl fcl -> SD_funcl (map_funcl_annot f fcl)
| SD_variant (id, typq) -> SD_variant (id, typq)
| SD_unioncl (id, tu) -> SD_unioncl (id, tu)
| SD_mapping (id, tannot_opt) -> SD_mapping (id, tannot_opt)
| SD_mapcl (id, mcl) -> SD_mapcl (id, map_mapcl_annot f mcl)
| SD_end id -> SD_end id
and map_decspec_annot f = function
| DEC_aux (dec_aux, annot) -> DEC_aux (map_decspec_annot_aux f dec_aux, f annot)
and map_decspec_annot_aux f = function
| DEC_reg (eff1, eff2, typ, id) -> DEC_reg (eff1, eff2, typ, id)
| DEC_config (id, typ, exp) -> DEC_config (id, typ, map_exp_annot f exp)
| DEC_alias (id, als) -> DEC_alias (id, map_aliasspec_annot f als)
| DEC_typ_alias (typ, id, als) -> DEC_typ_alias (typ, id, map_aliasspec_annot f als)
and map_aliasspec_annot f = function
| AL_aux (al_aux, annot) -> AL_aux (map_aliasspec_annot_aux f al_aux, f annot)
and map_aliasspec_annot_aux f = function
| AL_subreg (reg_id, id) -> AL_subreg (map_regid_annot f reg_id, id)
| AL_bit (reg_id, exp) -> AL_bit (map_regid_annot f reg_id, map_exp_annot f exp)
| AL_slice (reg_id, exp1, exp2) -> AL_slice (map_regid_annot f reg_id, map_exp_annot f exp1, map_exp_annot f exp2)
| AL_concat (reg_id1, reg_id2) -> AL_concat (map_regid_annot f reg_id1, map_regid_annot f reg_id2)
and map_regid_annot f = function
| RI_aux (ri_aux, annot) -> RI_aux (map_regid_annot_aux f ri_aux, f annot)
and map_regid_annot_aux f = function
| RI_id id -> RI_id id
and map_def_annot f = function
| DEF_type td -> DEF_type (map_typedef_annot f td)
| DEF_fundef fd -> DEF_fundef (map_fundef_annot f fd)
| DEF_mapdef md -> DEF_mapdef (map_mapdef_annot f md)
| DEF_val lb -> DEF_val (map_letbind_annot f lb)
| DEF_spec vs -> DEF_spec (map_valspec_annot f vs)
| DEF_fixity (prec, n, id) -> DEF_fixity (prec, n, id)
| DEF_overload (name, overloads) -> DEF_overload (name, overloads)
| DEF_default ds -> DEF_default ds
| DEF_scattered sd -> DEF_scattered (map_scattered_annot f sd)
| DEF_measure (id, pat, exp) -> DEF_measure (id, map_pat_annot f pat, map_exp_annot f exp)
| DEF_loop_measures (id, measures) -> DEF_loop_measures (id, List.map (map_loop_measure_annot f) measures)
| DEF_reg_dec ds -> DEF_reg_dec (map_decspec_annot f ds)
| DEF_internal_mutrec fds -> DEF_internal_mutrec (List.map (map_fundef_annot f) fds)
| DEF_pragma (name, arg, l) -> DEF_pragma (name, arg, l)
and map_defs_annot f = function
| Defs defs -> Defs (List.map (map_def_annot f) defs)
and map_loop_measure_annot f = function
| Loop (loop, exp) -> Loop (loop, map_exp_annot f exp)
let id_loc = function
| Id_aux (_, l) -> l
let kid_loc = function
| Kid_aux (_, l) -> l
let typ_loc = function
| Typ_aux (_, l) -> l
let pat_loc = function
| P_aux (_, (l, _)) -> l
let exp_loc = function
| E_aux (_, (l, _)) -> l
let def_loc = function
| DEF_type (TD_aux (_, (l, _)))
| DEF_fundef (FD_aux (_, (l, _)))
| DEF_mapdef (MD_aux (_, (l, _)))
| DEF_val (LB_aux (_, (l, _)))
| DEF_spec (VS_aux (_, (l, _)))
| DEF_default (DT_aux (_, l))
| DEF_scattered (SD_aux (_, (l, _)))
| DEF_reg_dec (DEC_aux (_, (l, _)))
| DEF_fixity (_, _, Id_aux (_, l))
| DEF_overload (Id_aux (_, l), _) -> l
| DEF_internal_mutrec _ -> Parse_ast.Unknown
| DEF_pragma (_, _, l) -> l
| DEF_measure (id, _, _) -> id_loc id
| DEF_loop_measures (id, _) -> id_loc id
let string_of_id = function
| Id_aux (Id v, _) -> v
| Id_aux (Operator v, _) -> "(operator " ^ v ^ ")"
let id_of_kid = function
| Kid_aux (Var v, l) -> Id_aux (Id (String.sub v 1 (String.length v - 1)), l)
let kid_of_id = function
| Id_aux (Id v, l) -> Kid_aux (Var ("'" ^ v), l)
| Id_aux (Operator v, _) -> assert false
let prepend_id str = function
| Id_aux (Id v, l) -> Id_aux (Id (str ^ v), l)
| Id_aux (Operator v, l) -> Id_aux (Operator (str ^ v), l)
let append_id id str =
match id with
| Id_aux (Id v, l) -> Id_aux (Id (v ^ str), l)
| Id_aux (Operator v, l) -> Id_aux (Operator (v ^ str), l)
let prepend_kid str = function
| Kid_aux (Var v, l) -> Kid_aux (Var ("'" ^ str ^ String.sub v 1 (String.length v - 1)), l)
let string_of_base_effect_aux = function
| BE_rreg -> "rreg"
| BE_wreg -> "wreg"
| BE_rmem -> "rmem"
| BE_rmemt -> "rmemt"
| BE_wmem -> "wmem"
| BE_eamem -> "eamem"
| BE_exmem -> "exmem"
| BE_wmv -> "wmv"
| BE_wmvt -> "wmvt"
| BE_barr -> "barr"
| BE_depend -> "depend"
| BE_undef -> "undef"
| BE_unspec -> "unspec"
| BE_nondet -> "nondet"
| BE_escape -> "escape"
| BE_config -> "configuration"
(*| BE_lset -> "lset"
| BE_lret -> "lret"*)
let string_of_kind_aux = function
| K_type -> "Type"
| K_int -> "Int"
| K_order -> "Order"
| K_bool -> "Bool"
let string_of_kind (K_aux (k, _)) = string_of_kind_aux k
let string_of_kinded_id (KOpt_aux (KOpt_kind (k, kid), _)) =
"(" ^ string_of_kid kid ^ " : " ^ string_of_kind k ^ ")"
let string_of_base_effect = function
| BE_aux (beff, _) -> string_of_base_effect_aux beff
let string_of_effect = function
| Effect_aux (Effect_set [], _) -> "pure"
| Effect_aux (Effect_set beffs, _) ->
let beffs = List.map string_of_base_effect beffs |> List.sort String.compare in
"{" ^ string_of_list ", " (fun x -> x) beffs ^ "}"
let string_of_order = function
| Ord_aux (Ord_var kid, _) -> string_of_kid kid
| Ord_aux (Ord_inc, _) -> "inc"
| Ord_aux (Ord_dec, _) -> "dec"
let rec string_of_nexp = function
| Nexp_aux (nexp, _) -> string_of_nexp_aux nexp
and string_of_nexp_aux = function
| Nexp_id id -> string_of_id id
| Nexp_var kid -> string_of_kid kid
| Nexp_constant c -> Big_int.to_string c
| Nexp_times (n1, n2) -> "(" ^ string_of_nexp n1 ^ " * " ^ string_of_nexp n2 ^ ")"
| Nexp_sum (n1, n2) -> "(" ^ string_of_nexp n1 ^ " + " ^ string_of_nexp n2 ^ ")"
| Nexp_minus (n1, n2) -> "(" ^ string_of_nexp n1 ^ " - " ^ string_of_nexp n2 ^ ")"
| Nexp_app (id, nexps) -> string_of_id id ^ "(" ^ string_of_list ", " string_of_nexp nexps ^ ")"
| Nexp_exp n -> "2 ^ " ^ string_of_nexp n
| Nexp_neg n -> "- " ^ string_of_nexp n
let rec string_of_typ = function
| Typ_aux (typ, l) -> string_of_typ_aux typ
and string_of_typ_aux = function
| Typ_internal_unknown -> "<UNKNOWN TYPE>"
| Typ_id id -> string_of_id id
| Typ_var kid -> string_of_kid kid
| Typ_tup typs -> "(" ^ string_of_list ", " string_of_typ typs ^ ")"
| Typ_app (id, args) when Id.compare id (mk_id "atom") = 0 -> "int(" ^ string_of_list ", " string_of_typ_arg args ^ ")"
| Typ_app (id, args) when Id.compare id (mk_id "atom_bool") = 0 -> "bool(" ^ string_of_list ", " string_of_typ_arg args ^ ")"
| Typ_app (id, args) -> string_of_id id ^ "(" ^ string_of_list ", " string_of_typ_arg args ^ ")"
| Typ_fn ([typ_arg], typ_ret, eff) ->
string_of_typ typ_arg ^ " -> " ^ string_of_typ typ_ret ^ " effect " ^ string_of_effect eff
| Typ_fn (typ_args, typ_ret, eff) ->
"(" ^ string_of_list ", " string_of_typ typ_args ^ ") -> "
^ string_of_typ typ_ret ^ " effect " ^ string_of_effect eff
| Typ_bidir (typ1, typ2, eff) -> string_of_typ typ1 ^ " <-> " ^ string_of_typ typ2 ^ " effect " ^ string_of_effect eff
| Typ_exist (kids, nc, typ) ->
"{" ^ string_of_list " " string_of_kinded_id kids ^ ", " ^ string_of_n_constraint nc ^ ". " ^ string_of_typ typ ^ "}"
and string_of_typ_arg = function
| A_aux (typ_arg, l) -> string_of_typ_arg_aux typ_arg
and string_of_typ_arg_aux = function
| A_nexp n -> string_of_nexp n
| A_typ typ -> string_of_typ typ
| A_order o -> string_of_order o
| A_bool nc -> string_of_n_constraint nc
and string_of_n_constraint = function
| NC_aux (NC_equal (n1, n2), _) -> string_of_nexp n1 ^ " == " ^ string_of_nexp n2
| NC_aux (NC_not_equal (n1, n2), _) -> string_of_nexp n1 ^ " != " ^ string_of_nexp n2
| NC_aux (NC_bounded_ge (n1, n2), _) -> string_of_nexp n1 ^ " >= " ^ string_of_nexp n2
| NC_aux (NC_bounded_gt (n1, n2), _) -> string_of_nexp n1 ^ " > " ^ string_of_nexp n2
| NC_aux (NC_bounded_le (n1, n2), _) -> string_of_nexp n1 ^ " <= " ^ string_of_nexp n2
| NC_aux (NC_bounded_lt (n1, n2), _) -> string_of_nexp n1 ^ " < " ^ string_of_nexp n2
| NC_aux (NC_or (nc1, nc2), _) ->
"(" ^ string_of_n_constraint nc1 ^ " | " ^ string_of_n_constraint nc2 ^ ")"
| NC_aux (NC_and (nc1, nc2), _) ->
"(" ^ string_of_n_constraint nc1 ^ " & " ^ string_of_n_constraint nc2 ^ ")"
| NC_aux (NC_set (kid, ns), _) ->
string_of_kid kid ^ " in {" ^ string_of_list ", " Big_int.to_string ns ^ "}"
| NC_aux (NC_app (Id_aux (Operator op, _), [arg1; arg2]), _) ->
"(" ^ string_of_typ_arg arg1 ^ " " ^ op ^ " " ^ string_of_typ_arg arg2 ^ ")"
| NC_aux (NC_app (id, args), _) -> string_of_id id ^ "(" ^ string_of_list ", " string_of_typ_arg args ^ ")"
| NC_aux (NC_var v, _) -> string_of_kid v
| NC_aux (NC_true, _) -> "true"
| NC_aux (NC_false, _) -> "false"
let string_of_kinded_id (KOpt_aux (KOpt_kind (k, kid), _)) = "(" ^ string_of_kid kid ^ " : " ^ string_of_kind k ^ ")"
let string_of_quant_item_aux = function
| QI_id kopt -> string_of_kinded_id kopt
| QI_constant kopts -> "is_constant(" ^ Util.string_of_list ", " string_of_kinded_id kopts ^ ")"
| QI_constraint constr -> string_of_n_constraint constr
let string_of_quant_item = function
| QI_aux (qi, _) -> string_of_quant_item_aux qi
let string_of_typquant_aux = function
| TypQ_tq quants -> "forall " ^ string_of_list ", " string_of_quant_item quants
| TypQ_no_forall -> ""
let string_of_typquant = function
| TypQ_aux (quant, _) -> string_of_typquant_aux quant
let string_of_typschm (TypSchm_aux (TypSchm_ts (quant, typ), _)) =
string_of_typquant quant ^ ". " ^ string_of_typ typ
let string_of_lit (L_aux (lit, _)) =
match lit with
| L_unit -> "()"
| L_zero -> "bitzero"
| L_one -> "bitone"
| L_true -> "true"
| L_false -> "false"
| L_num n -> Big_int.to_string n
| L_hex n -> "0x" ^ n
| L_bin n -> "0b" ^ n
| L_undef -> "undefined"
| L_real r -> r
| L_string str -> "\"" ^ str ^ "\""
let rec string_of_exp (E_aux (exp, _)) =
match exp with
| E_block exps -> "{ " ^ string_of_list "; " string_of_exp exps ^ " }"
| E_id v -> string_of_id v
| E_ref id -> "ref " ^ string_of_id id
| E_sizeof nexp -> "sizeof " ^ string_of_nexp nexp
| E_constraint nc -> "constraint(" ^ string_of_n_constraint nc ^ ")"
| E_lit lit -> string_of_lit lit
| E_return exp -> "return " ^ string_of_exp exp
| E_app (f, args) -> string_of_id f ^ "(" ^ string_of_list ", " string_of_exp args ^ ")"
| E_app_infix (x, op, y) -> "(" ^ string_of_exp x ^ " " ^ string_of_id op ^ " " ^ string_of_exp y ^ ")"
| E_tuple exps -> "(" ^ string_of_list ", " string_of_exp exps ^ ")"
| E_case (exp, cases) ->
"match " ^ string_of_exp exp ^ " { " ^ string_of_list ", " string_of_pexp cases ^ " }"
| E_try (exp, cases) ->
"try " ^ string_of_exp exp ^ " catch { case " ^ string_of_list " case " string_of_pexp cases ^ "}"
| E_let (letbind, exp) -> "let " ^ string_of_letbind letbind ^ " in " ^ string_of_exp exp
| E_assign (lexp, bind) -> string_of_lexp lexp ^ " = " ^ string_of_exp bind
| E_cast (typ, exp) -> string_of_exp exp ^ " : " ^ string_of_typ typ
| E_vector vec -> "[" ^ string_of_list ", " string_of_exp vec ^ "]"
| E_vector_access (v, n) -> string_of_exp v ^ "[" ^ string_of_exp n ^ "]"
| E_vector_update (v, n, exp) -> "[" ^ string_of_exp v ^ " with " ^ string_of_exp n ^ " = " ^ string_of_exp exp ^ "]"
| E_vector_update_subrange (v, n, m, exp) -> "[" ^ string_of_exp v ^ " with " ^ string_of_exp n ^ " .. " ^ string_of_exp m ^ " = " ^ string_of_exp exp ^ "]"
| E_vector_subrange (v, n1, n2) -> string_of_exp v ^ "[" ^ string_of_exp n1 ^ " .. " ^ string_of_exp n2 ^ "]"
| E_vector_append (v1, v2) -> string_of_exp v1 ^ " @ " ^ string_of_exp v2
| E_if (cond, then_branch, else_branch) ->
"if " ^ string_of_exp cond ^ " then " ^ string_of_exp then_branch ^ " else " ^ string_of_exp else_branch
| E_field (exp, id) -> string_of_exp exp ^ "." ^ string_of_id id
| E_for (id, f, t, u, ord, body) ->
"foreach ("
^ string_of_id id ^ " from " ^ string_of_exp f ^ " to " ^ string_of_exp t
^ " by " ^ string_of_exp u ^ " order " ^ string_of_order ord
^ ") { "
^ string_of_exp body
| E_loop (While, measure, cond, body) -> "while " ^ string_of_measure measure ^ string_of_exp cond ^ " do " ^ string_of_exp body
| E_loop (Until, measure, cond, body) -> "repeat " ^ string_of_measure measure ^ string_of_exp body ^ " until " ^ string_of_exp cond
| E_assert (test, msg) -> "assert(" ^ string_of_exp test ^ ", " ^ string_of_exp msg ^ ")"
| E_exit exp -> "exit " ^ string_of_exp exp
| E_throw exp -> "throw " ^ string_of_exp exp
| E_cons (x, xs) -> string_of_exp x ^ " :: " ^ string_of_exp xs
| E_list xs -> "[|" ^ string_of_list ", " string_of_exp xs ^ "|]"
| E_record_update (exp, fexps) ->
"{ " ^ string_of_exp exp ^ " with " ^ string_of_list "; " string_of_fexp fexps ^ " }"
| E_record fexps ->
"{ " ^ string_of_list "; " string_of_fexp fexps ^ " }"
| E_var (lexp, binding, exp) -> "var " ^ string_of_lexp lexp ^ " = " ^ string_of_exp binding ^ " in " ^ string_of_exp exp
| E_internal_return exp -> "internal_return (" ^ string_of_exp exp ^ ")"
| E_internal_plet (pat, exp, body) -> "internal_plet " ^ string_of_pat pat ^ " = " ^ string_of_exp exp ^ " in " ^ string_of_exp body
| E_internal_value v -> "INTERNAL_VALUE(" ^ Value.string_of_value v ^ ")"
and string_of_measure (Measure_aux (m,_)) =
match m with
| Measure_none -> ""
| Measure_some exp -> "termination_measure { " ^ string_of_exp exp ^ "}"
and string_of_fexp (FE_aux (FE_Fexp (field, exp), _)) =
string_of_id field ^ " = " ^ string_of_exp exp
and string_of_pexp (Pat_aux (pexp, _)) =
match pexp with
| Pat_exp (pat, exp) -> string_of_pat pat ^ " -> " ^ string_of_exp exp
| Pat_when (pat, guard, exp) -> string_of_pat pat ^ " when " ^ string_of_exp guard ^ " -> " ^ string_of_exp exp
and string_of_typ_pat (TP_aux (tpat_aux, _)) =
match tpat_aux with
| TP_wild -> "_"
| TP_var kid -> string_of_kid kid
| TP_app (f, tpats) -> string_of_id f ^ "(" ^ string_of_list ", " string_of_typ_pat tpats ^ ")"
and string_of_pat (P_aux (pat, l)) =
match pat with
| P_lit lit -> string_of_lit lit
| P_wild -> "_"
| P_or (pat1, pat2) -> "(" ^ string_of_pat pat1 ^ " | " ^ string_of_pat pat2
^ ")"
| P_not pat -> "(!" ^ string_of_pat pat ^ ")"
| P_id v -> string_of_id v
| P_var (pat, tpat) -> string_of_pat pat ^ " as " ^ string_of_typ_pat tpat
| P_typ (typ, pat) -> string_of_pat pat ^ " : " ^ string_of_typ typ
| P_tup pats -> "(" ^ string_of_list ", " string_of_pat pats ^ ")"
| P_app (f, pats) -> string_of_id f ^ "(" ^ string_of_list ", " string_of_pat pats ^ ")"
| P_cons (pat1, pat2) -> string_of_pat pat1 ^ " :: " ^ string_of_pat pat2
| P_list pats -> "[||" ^ string_of_list "," string_of_pat pats ^ "||]"
| P_vector_concat pats -> string_of_list " @ " string_of_pat pats
| P_vector pats -> "[" ^ string_of_list ", " string_of_pat pats ^ "]"
| P_as (pat, id) -> "(" ^ string_of_pat pat ^ " as " ^ string_of_id id ^ ")"
| P_string_append [] -> "\"\""
| P_string_append pats -> string_of_list " ^ " string_of_pat pats
and string_of_mpat (MP_aux (pat, l)) =
match pat with
| MP_lit lit -> string_of_lit lit
| MP_id v -> string_of_id v