Skip to content

Commit feefcaa

Browse files
authored
flambda-backend: Remove immutable arrays from stdlib.ml and stdlib.mli (#1154)
1 parent ba101be commit feefcaa

File tree

18 files changed

+299
-210
lines changed

18 files changed

+299
-210
lines changed

stdlib/iarray.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ open! Stdlib
44

55
[@@@ocaml.flambda_o3]
66

7+
(* If you update this file please also update iarrayLabels.ml *)
8+
79
(* An alias for the type of immutable arrays. *)
810
type +'a t = 'a iarray
911

stdlib/iarrayLabels.ml

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,98 @@ open! Stdlib
88

99
[@@@ocaml.nolabels]
1010

11-
include Iarray
11+
(* CR mshinwell: Change to "include Iarray"; we can't do this at present
12+
as it requires referencing Stdlib__Iarray which will work under the make
13+
build but not under dune. *)
14+
15+
(* CR aspectorzabusky: This needs a copyright header; should I copy [array.ml]? *)
16+
17+
(* An alias for the type of immutable arrays. *)
18+
type +'a t = 'a iarray
19+
20+
(* Array operations *)
21+
22+
external length : 'a iarray -> int = "%array_length"
23+
external get : 'a iarray -> int -> 'a = "%array_safe_get"
24+
external ( .:() ) : 'a iarray -> int -> 'a = "%array_safe_get"
25+
external unsafe_get : 'a iarray -> int -> 'a = "%array_unsafe_get"
26+
27+
(* Immutable and mutable arrays have the same runtime representation; we
28+
construct immutable arrays by constructing mutable arrays and then blindly
29+
casting them to become immutable. This is safe here because:
30+
1. None of these functions mutate their array inputs;
31+
2. None of these functions hold on to their array inputs; and
32+
3. All of these functions return fresh arrays if they return an array. *)
33+
let init =
34+
Obj.magic (Array.init : int -> (int -> 'a) -> 'a array)
35+
let append =
36+
Obj.magic (Array.append : 'a array -> 'a array -> 'a array)
37+
let concat =
38+
Obj.magic (Array.concat : 'a array list -> 'a array)
39+
let sub =
40+
Obj.magic (Array.sub : 'a array -> int -> int -> 'a array)
41+
let to_list =
42+
Obj.magic (Array.to_list : 'a array -> 'a list)
43+
let of_list =
44+
Obj.magic (Array.of_list : 'a list -> 'a array)
45+
let iter =
46+
Obj.magic (Array.iter : ('a -> unit) -> 'a array -> unit)
47+
let iteri =
48+
Obj.magic (Array.iteri : (int -> 'a -> unit) -> 'a array -> unit)
49+
let map =
50+
Obj.magic (Array.map : ('a -> 'b) -> 'a array -> 'b array)
51+
let mapi =
52+
Obj.magic (Array.mapi : (int -> 'a -> 'b) -> 'a array -> 'b array)
53+
let fold_left =
54+
Obj.magic (Array.fold_left : ('a -> 'b -> 'a) -> 'a -> 'b array -> 'a)
55+
let fold_left_map =
56+
Obj.magic (Array.fold_left_map :
57+
('a -> 'b -> 'a * 'c) -> 'a -> 'b array -> 'a * 'c array)
58+
let fold_right =
59+
Obj.magic (Array.fold_right : ('b -> 'a -> 'a) -> 'b array -> 'a -> 'a)
60+
let iter2 =
61+
Obj.magic (Array.iter2 : ('a -> 'b -> unit) -> 'a array -> 'b array -> unit)
62+
let map2 =
63+
Obj.magic (Array.map2 : ('a -> 'b -> 'c) -> 'a array -> 'b array -> 'c array)
64+
let for_all =
65+
Obj.magic (Array.for_all : ('a -> bool) -> 'a array -> bool)
66+
let exists =
67+
Obj.magic (Array.exists : ('a -> bool) -> 'a array -> bool)
68+
let for_all2 =
69+
Obj.magic (Array.for_all2 : ('a -> 'b -> bool) -> 'a array -> 'b array -> bool)
70+
let exists2 =
71+
Obj.magic (Array.exists2 : ('a -> 'b -> bool) -> 'a array -> 'b array -> bool)
72+
let mem =
73+
Obj.magic (Array.mem : 'a -> 'a array -> bool)
74+
let memq =
75+
Obj.magic (Array.memq : 'a -> 'a array -> bool)
76+
let find_opt =
77+
Obj.magic (Array.find_opt : ('a -> bool) -> 'a array -> 'a option)
78+
let find_map =
79+
Obj.magic (Array.find_map : ('a -> 'b option) -> 'a array -> 'b option)
80+
let split =
81+
Obj.magic (Array.split : ('a * 'b) array -> 'a array * 'b array)
82+
let combine =
83+
Obj.magic (Array.combine : 'a array -> 'b array -> ('a * 'b) array)
84+
let to_seq =
85+
Obj.magic (Array.to_seq : 'a array -> 'a Seq.t)
86+
let to_seqi =
87+
Obj.magic (Array.to_seqi : 'a array -> (int * 'a) Seq.t)
88+
let of_seq =
89+
Obj.magic (Array.of_seq : 'a Seq.t -> 'a array)
90+
91+
(* Only safe if the array isn't used after this call *)
92+
let unsafe_of_array : 'a array -> 'a iarray = Obj.magic
93+
94+
(* Must be fully applied due to the value restriction *)
95+
let lift_sort sorter cmp iarr =
96+
let arr = Array.of_iarray iarr in
97+
sorter cmp arr;
98+
unsafe_of_array arr
99+
100+
let sort cmp iarr = lift_sort Array.sort cmp iarr
101+
let stable_sort cmp iarr = lift_sort Array.stable_sort cmp iarr
102+
let fast_sort cmp iarr = lift_sort Array.fast_sort cmp iarr
103+
104+
let to_array = Array.of_iarray
105+
let of_array = Array.to_iarray

stdlib/stdlib.ml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,6 @@ external unsafe_char_of_int : int -> char = "%identity"
229229
let char_of_int n =
230230
if n < 0 || n > 255 then invalid_arg "char_of_int" else unsafe_char_of_int n
231231

232-
(* Array operations -- more in modules Array and Iarray *)
233-
234-
external ( .:() ) : 'a iarray -> int -> 'a = "%array_safe_get"
235-
236232
(* Unit operations *)
237233

238234
external ignore : 'a -> unit = "%ignore"
@@ -608,8 +604,6 @@ module Gc = Gc
608604
module Genlex = Genlex
609605
module Hashtbl = Hashtbl
610606
module In_channel = In_channel
611-
module Iarray = Iarray
612-
module IarrayLabels = IarrayLabels
613607
module Int = Int
614608
module Int32 = Int32
615609
module Int64 = Int64

stdlib/stdlib.mli

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -724,21 +724,6 @@ val char_of_int : int -> char
724724
outside the range 0--255. *)
725725

726726

727-
(** {1 Array operations}
728-
729-
More array operations are provided in modules {!Array} and {!Iarray}.
730-
*)
731-
732-
external ( .:() ) : 'a iarray -> int -> 'a = "%array_safe_get"
733-
(** [a.:(n)] returns the element number [n] of immutable array [a].
734-
The first element has number 0.
735-
The last element has number [length a - 1].
736-
You can also write [a.:(n)] instead of [get a n].
737-
738-
@raise Invalid_argument
739-
if [n] is outside the range 0 to [(length a - 1)]. *)
740-
741-
742727
(** {1 Unit operations} *)
743728

744729
external ignore : 'a -> unit = "%ignore"
@@ -1436,8 +1421,6 @@ module Genlex = Genlex
14361421
[@@deprecated "Use the camlp-streams library instead."]
14371422
module Hashtbl = Hashtbl
14381423
module In_channel = In_channel
1439-
module Iarray = Iarray
1440-
module IarrayLabels = IarrayLabels
14411424
module Int = Int
14421425
module Int32 = Int32
14431426
module Int64 = Int64

testsuite/tests/array-functions/test_iarray.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
flags = "-extension immutable_arrays_experimental"
33
*)
44

5+
module Iarray = Stdlib__Iarray
6+
external ( .:() ) : 'a iarray -> int -> 'a = "%array_safe_get"
7+
58
(* Copied from [test.ml], but with all the [Array.fill] tests deleted *)
69

710
(* [iarray]s don't have the [make*] functions, so we redefine them here *)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Fatal error: exception Stdlib.Exit
2-
Raised by primitive operation at Stdlib.open_in_gen in file "stdlib.ml", line 412, characters 28-54
2+
Raised by primitive operation at Stdlib.open_in_gen in file "stdlib.ml", line 408, characters 28-54
33
Called from Pr2195 in file "pr2195.ml", line 24, characters 6-19
44
Re-raised at Pr2195 in file "pr2195.ml", line 29, characters 4-41
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Fatal error: exception Stdlib.Exit
2-
Raised by primitive operation at Stdlib.open_in_gen in file "stdlib.ml", line 412, characters 28-54
3-
Called from Stdlib.open_in in file "stdlib.ml" (inlined), line 417, characters 2-45
2+
Raised by primitive operation at Stdlib.open_in_gen in file "stdlib.ml", line 408, characters 28-54
3+
Called from Stdlib.open_in in file "stdlib.ml" (inlined), line 413, characters 2-45
44
Called from Pr2195 in file "pr2195.ml", line 24, characters 6-19
55
Re-raised at Pr2195 in file "pr2195.ml", line 29, characters 4-41

0 commit comments

Comments
 (0)