jon.recoil.org

Source file iarrayLabels.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
(**************************************************************************)
(*                                                                        *)
(*                                 OCaml                                  *)
(*                                                                        *)
(*                Jacques Garrigue, Kyoto University RIMS                 *)
(*              Antal Spector-Zabusky, Jane Street, New York              *)
(*                                                                        *)
(*   Copyright 2001 Institut National de Recherche en Informatique et     *)
(*     en Automatique.                                                    *)
(*   Copyright 2023 Jane Street Group LLC                                 *)
(*                                                                        *)
(*   All rights reserved.  This file is distributed under the terms of    *)
(*   the GNU Lesser General Public License version 2.1, with the          *)
(*   special exception on linking described in the file LICENSE.          *)
(*                                                                        *)
(**************************************************************************)

open! Stdlib

[@@@ocaml.flambda_o3]

(* Module [IarrayLabels]: labelled Iarray module *)

[@@@ocaml.nolabels]

(* NOTE: Please work in iarray.ml and then copy the results here; for more
   information, see the top of that file.  This is a temporary state of affairs,
   but for now, please copy things! *)

(* CR mshinwell: Change to "include Iarray"; we can't do this at present
   as it requires referencing Stdlib__Iarray which will work under the make
   build but not under dune. *)

(* An alias for the type of immutable arrays. *)
type (+'a : any mod separable) t = 'a iarray

(* Array operations *)

external length : ('a : any mod separable). local_ 'a iarray -> int @@ portable
  = "%array_length"
[@@layout_poly]
external get :
  ('a : any mod separable). ('a iarray[@local_opt]) -> int -> ('a[@local_opt])
  @@ portable = "%array_safe_get"
[@@layout_poly]
external ( .:() ) :
  ('a : any mod separable). ('a iarray[@local_opt]) -> int -> ('a[@local_opt])
  @@ portable = "%array_safe_get"
[@@layout_poly]
external unsafe_get :
  ('a : any mod separable). ('a iarray[@local_opt]) -> int -> ('a[@local_opt])
  @@ portable = "%array_unsafe_get"
[@@layout_poly]
external concat : ('a : any mod separable). 'a iarray list -> 'a iarray
  @@ portable = "caml_array_concat"
external concat_local :
  ('a : any mod separable). local_ 'a iarray list -> local_ 'a iarray
  @@ portable = "caml_array_concat_local"

external append_prim :
  ('a : any mod separable). 'a iarray -> 'a iarray -> 'a iarray @@ portable
  = "caml_array_append"
external append_prim_local :
  ('a : any mod separable).
  local_ 'a iarray -> local_ 'a iarray -> local_ 'a iarray @@ portable
  = "caml_array_append_local"
external unsafe_sub :
  ('a : any mod separable). 'a iarray -> int -> int -> 'a iarray @@ portable
  = "caml_array_sub"
external unsafe_sub_local :
  ('a : any mod separable). local_ 'a iarray -> int -> int -> local_ 'a iarray
  @@ portable = "caml_array_sub_local"
external unsafe_of_array : ('a : any mod separable). 'a array -> 'a iarray
  @@ portable = "%array_to_iarray"
external unsafe_to_array : ('a : any mod separable). 'a iarray -> 'a array
  @@ portable = "%array_of_iarray"

(* Used only to reimplement [init] *)
external unsafe_set_mutable :
  ('a : any mod separable). 'a array -> int -> 'a -> unit @@ portable
  = "%array_unsafe_set"
[@@layout_poly]

(* VERY UNSAFE: Any of these functions can be used to violate the "no forward
   pointers" restriction for the local stack if not used carefully.  Each of
   these can either make a local mutable array or mutate its contents, and if
   not careful, this can lead to an array's contents pointing forwards. *)
external make_mutable_local :
  ('a : value_or_null mod separable). int -> local_ 'a -> local_ 'a array
  @@ portable = "caml_array_make_local"
external unsafe_of_local_array :
  ('a : any mod separable). local_ 'a array -> local_ 'a iarray @@ portable
  = "%array_to_iarray"
external unsafe_set_local :
  ('a : any mod separable). local_ 'a array -> int -> local_ 'a -> unit
  @@ portable = "%array_unsafe_set"
[@@layout_poly]

(* We can't use immutable array literals in this file, since we don't want to
   require the stdlib to be compiled with extensions, so instead of [[::]] we
   use [unsafe_of_array [||]] below.  Thankfully, we never need it in the
   [local] case so we don't have to think about the details. *)

(* Really trusting the inliner here; to get maximum performance, it has to
   inline both [unsafe_init_local] *and* [f]. *)
(** Precondition: [l >= 0]. *)
let[@inline always] unsafe_init_local l (local_ f : int -> local_ 'a) = exclave_
  if l = 0 then
    unsafe_of_local_array [||]
  else
    (* The design of this function is exceedingly delicate, and is the only way
       we can correctly allocate a local array on the stack via mutation.  We
       are subject to the "no forward pointers" constraint on the local stack;
       we're not allowed to make pointers to later-allocated objects even within
       the same stack frame.  Thus, in order to get this right, we consume O(n)
       call-stack space: we allocate the values to put in the array, and only
       *then* recurse, creating the array as the very last thing of all and
       *returning* it.  This is why the [f i] call is the first thing in the
       function, and why it's not tail-recursive; if it were tail-recursive,
       then we wouldn't have anywhere to put the array elements during the whole
       process. *)
    let rec go ~l ~f i = exclave_ begin
      let x = f i in
      if i = l - 1 then
        make_mutable_local l x
      else begin
        let res = go ~l ~f (i+1) in
        unsafe_set_local res i x;
        res
      end
    end in
    unsafe_of_local_array (go ~l ~f 0)

(* The implementation is copied from [Array] so that [f] can be [local_] *)
let init l (local_ f) =
  if l = 0 then unsafe_of_array [||] else
  if l < 0 then invalid_arg "Iarray.init"
  (* See #6575. We could also check for maximum array size, but this depends
     on whether we create a float array or a regular one... *)
  else
   let res = Array.make l (f 0) in
   for i = 1 to pred l do
     unsafe_set_mutable res i (f i)
   done;
   unsafe_of_array res

let init_local l f = exclave_
  if l < 0 then invalid_arg "Iarray.init_local"
  (* See #6575. We could also check for maximum array size, but this depends
     on whether we create a float array or a regular one... *)
  else unsafe_init_local l f

let append a1 a2 =
  if length a1 = 0 then a2 (* Safe because they're immutable *)
  else if length a2 = 0 then a1
  else append_prim a1 a2

let append_local a1 a2 = exclave_
  if length a1 = 0 then a2 (* Safe because they're immutable *)
  else if length a2 = 0 then a1
  else append_prim_local a1 a2

let sub a ofs len =
  if ofs < 0 || len < 0 || ofs > length a - len
  then invalid_arg "Iarray.sub"
  else unsafe_sub a ofs len

let sub_local a ofs len = exclave_
  if ofs < 0 || len < 0 || ofs > length a - len
  then invalid_arg "Iarray.sub"
  else unsafe_sub_local a ofs len

let iter f a =
  for i = 0 to length a - 1 do f(unsafe_get a i) done

let iter_local f a =
  for i = 0 to length a - 1 do f(unsafe_get a i) done

let iter2 f a b =
  if length a <> length b then
    invalid_arg "Iarray.iter2: arrays must have the same length"
  else
    for i = 0 to length a - 1 do f (unsafe_get a i) (unsafe_get b i) done

let iter2_local f a b =
  if length a <> length b then
    invalid_arg "Iarray.iter2_local: arrays must have the same length"
  else
    for i = 0 to length a - 1 do f (unsafe_get a i) (unsafe_get b i) done

let iter2_local_first f a b =
  if length a <> length b then
    invalid_arg "Iarray.iter2_local_first: arrays must have the same length"
  else
    for i = 0 to length a - 1 do f (unsafe_get a i) (unsafe_get b i) done

let iter2_local_second f a b =
  if length a <> length b then
    invalid_arg "Iarray.iter2_local_second: arrays must have the same length"
  else
    for i = 0 to length a - 1 do f (unsafe_get a i) (unsafe_get b i) done

let map f a =
  let l = length a in
  if l = 0 then unsafe_of_array [||] else begin
    let r = Array.make l (f(unsafe_get a 0)) in
    for i = 1 to l - 1 do
      Array.unsafe_set r i (f(unsafe_get a i))
    done;
    unsafe_of_array r
  end

let map_local f a = exclave_
  unsafe_init_local (length a) (fun i -> exclave_ f (unsafe_get a i))

let map_local_input f a =
  let l = length a in
  if l = 0 then unsafe_of_array [||] else begin
    let r = Array.make l (f(unsafe_get a 0)) in
    for i = 1 to l - 1 do
      Array.unsafe_set r i (f(unsafe_get a i))
    done;
    unsafe_of_array r
  end

let map_local_output f a = exclave_
  unsafe_init_local (length a) (fun i -> exclave_ f (unsafe_get a i))

let map2 f a b =
  let la = length a in
  let lb = length b in
  if la <> lb then
    invalid_arg "Iarray.map2: arrays must have the same length"
  else begin
    if la = 0 then unsafe_of_array [||] else begin
      let r = Array.make la (f (unsafe_get a 0) (unsafe_get b 0)) in
      for i = 1 to la - 1 do
        Array.unsafe_set r i (f (unsafe_get a i) (unsafe_get b i))
      done;
      unsafe_of_array r
    end
  end

let map2_local f a b = exclave_
  let la = length a in
  let lb = length b in
  if la <> lb then
    invalid_arg "Iarray.map2_local: arrays must have the same length"
  else
    unsafe_init_local la (fun i -> exclave_ f (unsafe_get a i) (unsafe_get b i))

let map2_local_inputs f a b =
  let la = length a in
  let lb = length b in
  if la <> lb then
    invalid_arg "Iarray.map2: arrays must have the same length"
  else begin
    if la = 0 then unsafe_of_array [||] else begin
      let r = Array.make la (f (unsafe_get a 0) (unsafe_get b 0)) in
      for i = 1 to la - 1 do
        Array.unsafe_set r i (f (unsafe_get a i) (unsafe_get b i))
      done;
      unsafe_of_array r
    end
  end

let map2_local_output f a b = exclave_
  let la = length a in
  let lb = length b in
  if la <> lb then
    invalid_arg "Iarray.map2_local: arrays must have the same length"
  else
    unsafe_init_local la (fun i -> exclave_ f (unsafe_get a i) (unsafe_get b i))

let map2_local_first_input f a b =
  let la = length a in
  let lb = length b in
  if la <> lb then
    invalid_arg "Iarray.map2: arrays must have the same length"
  else begin
    if la = 0 then unsafe_of_array [||] else begin
      let r = Array.make la (f (unsafe_get a 0) (unsafe_get b 0)) in
      for i = 1 to la - 1 do
        Array.unsafe_set r i (f (unsafe_get a i) (unsafe_get b i))
      done;
      unsafe_of_array r
    end
  end

let map2_local_second_input f a b =
  let la = length a in
  let lb = length b in
  if la <> lb then
    invalid_arg "Iarray.map2: arrays must have the same length"
  else begin
    if la = 0 then unsafe_of_array [||] else begin
      let r = Array.make la (f (unsafe_get a 0) (unsafe_get b 0)) in
      for i = 1 to la - 1 do
        Array.unsafe_set r i (f (unsafe_get a i) (unsafe_get b i))
      done;
      unsafe_of_array r
    end
  end

let map2_local_first_input_and_output f a b = exclave_
  let la = length a in
  let lb = length b in
  if la <> lb then
    invalid_arg "Iarray.map2_local: arrays must have the same length"
  else
    unsafe_init_local la (fun i -> exclave_ f (unsafe_get a i) (unsafe_get b i))

let map2_local_second_input_and_output f a b = exclave_
  let la = length a in
  let lb = length b in
  if la <> lb then
    invalid_arg "Iarray.map2_local: arrays must have the same length"
  else
    unsafe_init_local la (fun i -> exclave_ f (unsafe_get a i) (unsafe_get b i))

let iteri f a =
  for i = 0 to length a - 1 do f i (unsafe_get a i) done

let iteri_local f a =
  for i = 0 to length a - 1 do f i (unsafe_get a i) done

let mapi f a =
  let l = length a in
  if l = 0 then unsafe_of_array [||] else begin
    let r = Array.make l (f 0 (unsafe_get a 0)) in
    for i = 1 to l - 1 do
      Array.unsafe_set r i (f i (unsafe_get a i))
    done;
    unsafe_of_array r
  end

let mapi_local f a = exclave_
  unsafe_init_local (length a) (fun i -> exclave_ f i (unsafe_get a i))

let mapi_local_input f a =
  let l = length a in
  if l = 0 then unsafe_of_array [||] else begin
    let r = Array.make l (f 0 (unsafe_get a 0)) in
    for i = 1 to l - 1 do
      Array.unsafe_set r i (f i (unsafe_get a i))
    done;
    unsafe_of_array r
  end

let mapi_local_output f a = exclave_
  unsafe_init_local (length a) (fun i -> exclave_ f i (unsafe_get a i))

let to_list a =
  let rec tolist i res =
    if i < 0 then res else tolist (i - 1) (unsafe_get a i :: res) in
  tolist (length a - 1) []

let to_list_local a = exclave_
  let rec tolist i res = exclave_
    if i < 0 then res else tolist (i - 1) (unsafe_get a i :: res) in
  tolist (length a - 1) []

let of_list l = unsafe_of_array (Array.of_list l)

(* Cannot use List.length here because the List module depends on Array. *)
let rec list_length accu = function
  | [] -> accu
  | _::t -> list_length (succ accu) t

(* This shouldn't violate the forward-pointers restriction because the list
   elements already exist *)
let of_list_local = function
  | [] -> exclave_ unsafe_of_array [||]
  | hd::tl as l -> exclave_
      let a = make_mutable_local (list_length 0 l) hd in
      let rec fill i = function
        | [] -> exclave_ a
        | hd::tl -> exclave_ unsafe_set_local a i hd; fill (i+1) tl in
      unsafe_of_local_array (fill 1 tl)

let to_array ia = Array.copy (unsafe_to_array ia)

let of_array ma = unsafe_of_array (Array.copy ma)

let fold_left f x a =
  let r = ref x in
  for i = 0 to length a - 1 do
    r := f !r (unsafe_get a i)
  done;
  !r

let fold_left_local f x a = exclave_
  let len = length a in
  let rec go r i = exclave_
    if i = len
    then r
    else go (f r (unsafe_get a i)) (i+1)
  in
  go x 0

let fold_left_local_input f x a =
  let r = ref x in
  for i = 0 to length a - 1 do
    r := f !r (unsafe_get a i)
  done;
  !r

let fold_left_local_output f x a = exclave_
  let len = length a in
  let rec go r i = exclave_
    if i = len
    then r
    else go (f r (unsafe_get a i)) (i+1)
  in
  go x 0

let fold_left_map f acc input_array =
  let len = length input_array in
  if len = 0 then (acc, unsafe_of_array [||]) else begin
    let acc, elt = f acc (unsafe_get input_array 0) in
    let output_array = Array.make len elt in
    let acc = ref acc in
    for i = 1 to len - 1 do
      let acc', elt = f !acc (unsafe_get input_array i) in
      acc := acc';
      Array.unsafe_set output_array i elt;
    done;
    !acc, unsafe_of_array output_array
  end

let fold_left_map_local f acc input_array = exclave_
  let len = length input_array in
  if len = 0 then (acc, unsafe_of_local_array [||]) else begin
    let rec go acc i = exclave_
      let acc, elt = f acc (unsafe_get input_array i) in
      if i = len - 1 then
        acc, make_mutable_local len elt
      else begin
        let (_, output_array) as res = go acc (i+1) in
        unsafe_set_local output_array i elt;
        res
      end
    in
    let acc, output_array = go acc 0 in
    acc, unsafe_of_local_array output_array
  end

let fold_left_map_local_input f acc input_array =
  let len = length input_array in
  if len = 0 then (acc, unsafe_of_array [||]) else begin
    let acc, elt = f acc (unsafe_get input_array 0) in
    let output_array = Array.make len elt in
    let acc = ref acc in
    for i = 1 to len - 1 do
      let acc', elt = f !acc (unsafe_get input_array i) in
      acc := acc';
      Array.unsafe_set output_array i elt;
    done;
    !acc, unsafe_of_array output_array
  end

let fold_left_map_local_output f acc input_array = exclave_
  let len = length input_array in
  if len = 0 then (acc, unsafe_of_local_array [||]) else begin
    let rec go acc i = exclave_
      let acc, elt = f acc (unsafe_get input_array i) in
      if i = len - 1 then
        acc, make_mutable_local len elt
      else begin
        let (_, output_array) as res = go acc (i+1) in
        unsafe_set_local output_array i elt;
        res
      end
    in
    let acc, output_array = go acc 0 in
    acc, unsafe_of_local_array output_array
  end

let fold_right f a x =
  let r = ref x in
  for i = length a - 1 downto 0 do
    r := f (unsafe_get a i) !r
  done;
  !r

let fold_right_local f a x = exclave_
  let rec go r i = exclave_
    if i = -1
    then r
    else go (f (unsafe_get a i) r) (i-1)
  in
  go x (length a - 1)

let fold_right_local_input f a x =
  let r = ref x in
  for i = length a - 1 downto 0 do
    r := f (unsafe_get a i) !r
  done;
  !r

let fold_right_local_output f a x = exclave_
  let rec go r i = exclave_
    if i = -1
    then r
    else go (f (unsafe_get a i) r) (i-1)
  in
  go x (length a - 1)

(* CR aspectorzabusky: Why do I need this?  Shouldn't mode-crossing handle doing
   this? *)
let[@inline always] globalize_bool : local_ bool -> bool = fun b -> b

let exists p a =
  let n = length a in
  let rec loop i = exclave_
    if i = n then false
    else if p (unsafe_get a i) then true
    else loop (succ i) in
  globalize_bool (loop 0)

let exists_local p a =
  let n = length a in
  let rec loop i = exclave_
    if i = n then false
    else if p (unsafe_get a i) then true
    else loop (succ i) in
  globalize_bool (loop 0)

let for_all p a =
  let n = length a in
  let rec loop i = exclave_
    if i = n then true
    else if p (unsafe_get a i) then loop (succ i)
    else false in
  globalize_bool (loop 0)

let for_all_local p a =
  let n = length a in
  let rec loop i = exclave_
    if i = n then true
    else if p (unsafe_get a i) then loop (succ i)
    else false in
  globalize_bool (loop 0)

let for_all2 p l1 l2 =
  let n1 = length l1
  and n2 = length l2 in
  if n1 <> n2 then invalid_arg "Iarray.for_all2"
  else let rec loop i = exclave_
    if i = n1 then true
    else if p (unsafe_get l1 i) (unsafe_get l2 i) then loop (succ i)
    else false in
  globalize_bool (loop 0)

let for_all2_local p l1 l2 =
  let n1 = length l1
  and n2 = length l2 in
  if n1 <> n2 then invalid_arg "Iarray.for_all2_local"
  else let rec loop i = exclave_
    if i = n1 then true
    else if p (unsafe_get l1 i) (unsafe_get l2 i) then loop (succ i)
    else false in
  globalize_bool (loop 0)

let for_all2_local_first p l1 l2 =
  let n1 = length l1
  and n2 = length l2 in
  if n1 <> n2 then invalid_arg "Iarray.for_all2_local_first"
  else let rec loop i = exclave_
    if i = n1 then true
    else if p (unsafe_get l1 i) (unsafe_get l2 i) then loop (succ i)
    else false in
  globalize_bool (loop 0)

let for_all2_local_second p l1 l2 =
  let n1 = length l1
  and n2 = length l2 in
  if n1 <> n2 then invalid_arg "Iarray.for_all2_local_second"
  else let rec loop i = exclave_
    if i = n1 then true
    else if p (unsafe_get l1 i) (unsafe_get l2 i) then loop (succ i)
    else false in
  globalize_bool (loop 0)

let exists2 p l1 l2 =
  let n1 = length l1
  and n2 = length l2 in
  if n1 <> n2 then invalid_arg "Iarray.exists2"
  else let rec loop i = exclave_
    if i = n1 then false
    else if p (unsafe_get l1 i) (unsafe_get l2 i) then true
    else loop (succ i) in
  globalize_bool (loop 0)

let exists2_local p l1 l2 =
  let n1 = length l1
  and n2 = length l2 in
  if n1 <> n2 then invalid_arg "Iarray.exists2_local"
  else let rec loop i = exclave_
    if i = n1 then false
    else if p (unsafe_get l1 i) (unsafe_get l2 i) then true
    else loop (succ i) in
  globalize_bool (loop 0)

let exists2_local_first p l1 l2 =
  let n1 = length l1
  and n2 = length l2 in
  if n1 <> n2 then invalid_arg "Iarray.exists2_local_first"
  else let rec loop i = exclave_
    if i = n1 then false
    else if p (unsafe_get l1 i) (unsafe_get l2 i) then true
    else loop (succ i) in
  globalize_bool (loop 0)

let exists2_local_second p l1 l2 =
  let n1 = length l1
  and n2 = length l2 in
  if n1 <> n2 then invalid_arg "Iarray.exists2_local_second"
  else let rec loop i = exclave_
    if i = n1 then false
    else if p (unsafe_get l1 i) (unsafe_get l2 i) then true
    else loop (succ i) in
  globalize_bool (loop 0)

let mem x a =
  let n = length a in
  let rec loop i = exclave_
    if i = n then false
    else if compare (unsafe_get a i) x = 0 then true
    else loop (succ i) in
  globalize_bool (loop 0)

let memq x a =
  let n = length a in
  let rec loop i = exclave_
    if i = n then false
    else if x == (unsafe_get a i) then true
    else loop (succ i) in
  globalize_bool (loop 0)

let find_opt p a =
  let n = length a in
  let rec loop i =
    if i = n then None
    else
      let x = unsafe_get a i in
      if p x then Some x
      else loop (succ i)
  in
  loop 0 [@nontail]

let find_opt_local p a = exclave_
  let n = length a in
  let rec loop i = exclave_
    if i = n then None
    else
      let x = unsafe_get a i in
      if p x then Some x
      else loop (succ i)
  in
  loop 0

let find_map f a =
  let n = length a in
  let rec loop i =
    if i = n then None
    else
      match f (unsafe_get a i) with
      | None -> loop (succ i)
      | Some _ as r -> r
  in
  loop 0 [@nontail]

let find_map_local f a = exclave_
  let n = length a in
  let rec loop i = exclave_
    if i = n then None
    else
      match f (unsafe_get a i) with
      | None -> loop (succ i)
      | Some _ as r -> r
  in
  loop 0

let find_map_local_input f a =
  let n = length a in
  let rec loop i =
    if i = n then None
    else
      match f (unsafe_get a i) with
      | None -> loop (succ i)
      | Some _ as r -> r
  in
  loop 0 [@nontail]

let find_map_local_output f a = exclave_
  let n = length a in
  let rec loop i = exclave_
    if i = n then None
    else
      match f (unsafe_get a i) with
      | None -> loop (succ i)
      | Some _ as r -> r
  in
  loop 0

let split x =
  if x = unsafe_of_array [||]
  then unsafe_of_array [||], unsafe_of_array [||]
  else begin
    let a0, b0 = unsafe_get x 0 in
    let n = length x in
    let a = Array.make n a0 in
    let b = Array.make n b0 in
    for i = 1 to n - 1 do
      let ai, bi = unsafe_get x i in
      Array.unsafe_set a i ai;
      Array.unsafe_set b i bi
    done;
    unsafe_of_array a, unsafe_of_array b
  end

(* This shouldn't violate the forward-pointers restriction because the array
   elements already exist.  (This doesn't work for [combine], where we need to
   create the tuples.) *)
let split_local x = exclave_
  if x = unsafe_of_array [||]
  then unsafe_of_array [||], unsafe_of_array [||]
  else begin
    let a0, b0 = unsafe_get x 0 in
    let n = length x in
    let a = make_mutable_local n a0 in
    let b = make_mutable_local n b0 in
    for i = 1 to n - 1 do
      let ai, bi = unsafe_get x i in
      unsafe_set_local a i ai;
      unsafe_set_local b i bi
    done;
    unsafe_of_local_array a, unsafe_of_local_array b
  end

let combine a b =
  let na = length a in
  let nb = length b in
  if na <> nb then invalid_arg "Iarray.combine";
  let r = if na = 0 then [||]
  else begin
    let x = Array.make na (unsafe_get a 0, unsafe_get b 0) in
    for i = 1 to na - 1 do
      Array.unsafe_set x i (unsafe_get a i, unsafe_get b i)
    done;
    x
  end in
  unsafe_of_array r

let combine_local a b = exclave_
  let na = length a in
  let nb = length b in
  if na <> nb then invalid_arg "Iarray.combine_local";
  unsafe_init_local na (fun i -> exclave_ unsafe_get a i, unsafe_get b i)

(* Must be fully applied due to the value restriction *)
let lift_sort sorter cmp iarr =
  let arr = to_array iarr in
  sorter cmp arr;
  unsafe_of_array arr

let sort cmp iarr = lift_sort Array.sort cmp iarr
let stable_sort cmp iarr = lift_sort Array.stable_sort cmp iarr
let fast_sort cmp iarr = lift_sort Array.fast_sort cmp iarr

let to_seq a =
  let rec aux i () =
    if i < length a
    then
      let x = unsafe_get a i in
      Seq.Cons (x, aux (i+1))
    else Seq.Nil
  in
  aux 0

let to_seqi a =
  let rec aux i () =
    if i < length a
    then
      let x = unsafe_get a i in
      Seq.Cons ((i,x), aux (i+1))
    else Seq.Nil
  in
  aux 0

let of_seq i = unsafe_of_array (Array.of_seq i)