Skip to content

Commit 26c2f4d

Browse files
v0.18~preview.130.33+516
1 parent 21649eb commit 26c2f4d

File tree

117 files changed

+2886
-1729
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+2886
-1729
lines changed

composition_infix/src/composition_infix.mli

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@
33
- [ a |> (f >> g) = a |> f |> g ]
44
- [ (f << g) a = f (g a) ] *)
55

6-
val ( >> ) : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c
7-
val ( << ) : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c
6+
val ( >> )
7+
: ('a : value_or_null) ('b : value_or_null) ('c : value_or_null).
8+
('a -> 'b) -> ('b -> 'c) -> 'a -> 'c
9+
10+
val ( << )
11+
: ('a : value_or_null) ('b : value_or_null) ('c : value_or_null).
12+
('b -> 'c) -> ('a -> 'b) -> 'a -> 'c

md5/src/md5_lib.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ type t = string
44
let empty = Digest.string ""
55
let make s = if s = empty then empty else s
66
let make_local s = if s = empty then empty else s
7+
let compare__local = compare
78
let compare = compare
89
let length = 16
910
let to_binary s = s
@@ -16,6 +17,7 @@ let of_binary_exn s =
1617

1718
let unsafe_of_binary = make
1819
let unsafe_of_binary_local = make_local
20+
let unsafe_of_binary__local = make_local
1921

2022
external globalize : local_ t -> t @@ portable = "%obj_dup"
2123

md5/src/md5_lib.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
@@ portable
22

3-
type t
3+
type t : immutable_data
44

55
val compare : t -> t -> int
6+
val compare__local : t @ local -> t @ local -> int
67

78
(** [length = 16] is the size of the digest in bytes. *)
89
val length : int
@@ -15,6 +16,7 @@ val of_binary_exn : string -> t
1516
val unsafe_of_binary : string -> t
1617

1718
val unsafe_of_binary_local : local_ string -> local_ t
19+
val unsafe_of_binary__local : local_ string -> local_ t
1820
val globalize : local_ t -> t
1921
val to_hex : t -> string
2022
val of_hex_exn : string -> t

src/array.ml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ open! Import
22
include Array_intf.Definitions
33
include Array0
44

5-
type 'a t = 'a array
6-
[@@deriving compare ~localize, globalize, sexp ~localize, sexp_grammar]
5+
type ('a : any_non_null) t = 'a array
6+
7+
[%%rederive.portable
8+
type nonrec 'a t = 'a array
9+
[@@deriving compare ~localize, globalize, sexp ~localize, sexp_grammar]]
710

811
[@@@warning "-incompatible-with-upstream"]
912

@@ -415,6 +418,21 @@ let foldi t ~init ~f =
415418
!acc
416419
;;
417420

421+
let%template foldi_right (t @ local) ~(init @ m) ~(f @ local) =
422+
(let rec (aux @ local) (t @ local) ~idx ~(acc @ m) ~(f @ local) =
423+
(if idx < 0
424+
then acc
425+
else (
426+
(* [unsafe_get] is safe, since [idx >= 0 && idx < Array.length t] *)
427+
let acc = f idx (unsafe_get t idx) acc in
428+
aux t ~idx:(idx - 1) ~acc ~f))
429+
[@exclave_if_stack a]
430+
in
431+
aux t ~idx:(length t - 1) ~acc:init ~f [@nontail])
432+
[@exclave_if_stack a]
433+
[@@alloc a @ m = (stack_local, heap_global)]
434+
;;
435+
418436
let folding_mapi t ~init ~f =
419437
let acc = ref init in
420438
mapi t ~f:(fun i x ->

src/array0.ml

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,68 @@ module Sys = Sys0
1414
let invalid_argf = Printf.invalid_argf
1515

1616
module Array = struct
17-
external create : int -> 'a -> 'a array @@ portable = "caml_make_vect"
17+
[%%template
18+
external create
19+
: ('a : any_non_null).
20+
len:int -> 'a -> 'a array @ m
21+
@@ portable
22+
= "%makearray_dynamic"
23+
[@@alloc __ @ m = (heap_global, stack_local)] [@@layout_poly]]
1824

1925
external create_local
20-
: int
21-
-> 'a
22-
-> local_ 'a array
26+
: ('a : any_non_null).
27+
len:int -> 'a -> local_ 'a array
28+
@@ portable
29+
= "%makearray_dynamic"
30+
[@@layout_poly]
31+
32+
external magic_create_uninitialized
33+
: ('a : any_non_null).
34+
len:int -> ('a array[@local_opt])
2335
@@ portable
24-
= "caml_make_local_vect"
36+
= "%makearray_dynamic_uninit"
37+
[@@layout_poly]
2538

2639
external create_float_uninitialized
2740
: int
2841
-> float array
2942
@@ portable
3043
= "caml_make_float_vect"
3144

32-
external get
33-
: ('a array[@local_opt])
34-
-> (int[@local_opt])
35-
-> 'a
45+
external%template get
46+
: ('a : any_non_null).
47+
('a array[@local_opt]) @ m -> (int[@local_opt]) -> 'a @ m
3648
@@ portable
3749
= "%array_safe_get"
50+
[@@layout_poly] [@@mode m = (uncontended, shared)]
3851

39-
external length : ('a array[@local_opt]) -> int @@ portable = "%array_length"
52+
external length
53+
: ('a : any_non_null).
54+
('a array[@local_opt]) @ contended -> int
55+
@@ portable
56+
= "%array_length"
57+
[@@layout_poly]
4058

4159
external set
42-
: ('a array[@local_opt])
43-
-> (int[@local_opt])
44-
-> 'a
45-
-> unit
60+
: ('a : any_non_null).
61+
('a array[@local_opt]) -> (int[@local_opt]) -> 'a -> unit
4662
@@ portable
4763
= "%array_safe_set"
64+
[@@layout_poly]
4865

49-
external unsafe_get
50-
: ('a array[@local_opt])
51-
-> (int[@local_opt])
52-
-> 'a
66+
external%template unsafe_get
67+
: ('a : any_non_null).
68+
('a array[@local_opt]) @ m -> (int[@local_opt]) -> 'a @ m
5369
@@ portable
5470
= "%array_unsafe_get"
71+
[@@mode m = (uncontended, shared)] [@@layout_poly]
5572

5673
external unsafe_set
57-
: ('a array[@local_opt])
58-
-> (int[@local_opt])
59-
-> 'a
60-
-> unit
74+
: ('a : any_non_null).
75+
('a array[@local_opt]) -> (int[@local_opt]) -> 'a -> unit
6176
@@ portable
6277
= "%array_unsafe_set"
78+
[@@layout_poly]
6379

6480
external unsafe_blit
6581
: src:('a array[@local_opt])
@@ -95,16 +111,6 @@ include Array
95111

96112
let max_length = Sys.max_array_length
97113

98-
let create ~len x =
99-
try create len x with
100-
| Invalid_argument _ -> invalid_argf "Array.create ~len:%d: invalid length" len ()
101-
;;
102-
103-
let create_local ~len x = exclave_
104-
try create_local len x with
105-
| Invalid_argument _ -> invalid_argf "Array.create_local ~len:%d: invalid length" len ()
106-
;;
107-
108114
let create_float_uninitialized ~len =
109115
try create_float_uninitialized len with
110116
| Invalid_argument _ ->
@@ -163,12 +169,13 @@ let fold t ~init ~(local_ f : _ -> _ -> _) =
163169
!r
164170
;;
165171

166-
let fold_right t ~(local_ f : _ -> _ -> _) ~init =
172+
let%template fold_right (t @ m) ~(local_ f : _ @ m -> _ -> _) ~init =
167173
let r = ref init in
168174
for i = length t - 1 downto 0 do
169-
r := f (unsafe_get t i) !r
175+
r := f ((unsafe_get [@mode m]) t i) !r
170176
done;
171177
!r
178+
[@@mode m = (uncontended, shared)]
172179
;;
173180

174181
let iter t ~(local_ f : _ -> _) =

src/array_intf.ml

Lines changed: 84 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ open! Import
66

77
module Definitions = struct
88
module type Public = sig
9-
type 'a t
10-
[@@deriving
11-
compare ~localize, equal ~localize, globalize, sexp ~localize, sexp_grammar]
9+
type ('a : any_non_null) t
10+
11+
[%%rederive:
12+
type nonrec 'a t = 'a t
13+
[@@deriving
14+
compare ~localize, equal ~localize, globalize, sexp ~localize, sexp_grammar]]
1215

1316
include Binary_searchable.S1 with type 'a t := 'a t
1417
include Indexed_container.S1_with_creators with type 'a t := 'a t
@@ -18,55 +21,95 @@ module Definitions = struct
1821
[max_length/2] on 32-bit machines and [max_length] on 64-bit machines. *)
1922
val max_length : int
2023

21-
(*_ Declared as externals so that the compiler skips the caml_apply_X wrapping even when
22-
compiling without cross library inlining. *)
24+
(*_ Declared as externals so that the compiler skips the caml_apply_X wrapping even
25+
when compiling without cross library inlining. *)
2326

24-
external length : ('a t[@local_opt]) -> int = "%array_length"
27+
external length
28+
: ('a : any_non_null).
29+
('a array[@local_opt]) @ contended -> int
30+
= "%array_length"
31+
[@@layout_poly]
2532

2633
(** [Array.get a n] returns the element number [n] of array [a]. The first element has
2734
number 0. The last element has number [Array.length a - 1]. You can also write
2835
[a.(n)] instead of [Array.get a n].
2936
3037
Raise [Invalid_argument "index out of bounds"] if [n] is outside the range 0 to
3138
[(Array.length a - 1)]. *)
32-
external get : ('a t[@local_opt]) -> (int[@local_opt]) -> 'a = "%array_safe_get"
39+
external%template get
40+
: ('a : any_non_null).
41+
('a array[@local_opt]) @ m -> (int[@local_opt]) -> 'a @ m
42+
= "%array_safe_get"
43+
[@@layout_poly] [@@mode m = (uncontended, shared)]
3344

3445
(** [Array.set a n x] modifies array [a] in place, replacing element number [n] with
3546
[x]. You can also write [a.(n) <- x] instead of [Array.set a n x].
3647
3748
Raise [Invalid_argument "index out of bounds"] if [n] is outside the range 0 to
3849
[Array.length a - 1]. *)
3950
external set
40-
: ('a t[@local_opt])
41-
-> (int[@local_opt])
42-
-> 'a
43-
-> unit
51+
: ('a : any_non_null).
52+
('a array[@local_opt]) -> (int[@local_opt]) -> 'a -> unit
4453
= "%array_safe_set"
54+
[@@layout_poly]
4555

4656
(** Unsafe version of [get]. Can cause arbitrary behavior when used for an
4757
out-of-bounds array access. *)
48-
external unsafe_get
49-
: ('a t[@local_opt])
50-
-> (int[@local_opt])
51-
-> 'a
58+
external%template unsafe_get
59+
: ('a : any_non_null).
60+
('a array[@local_opt]) @ m -> (int[@local_opt]) -> 'a @ m
5261
= "%array_unsafe_get"
62+
[@@layout_poly] [@@mode m = (uncontended, shared)]
5363

5464
(** Unsafe version of [set]. Can cause arbitrary behavior when used for an
5565
out-of-bounds array access. *)
5666
external unsafe_set
57-
: ('a t[@local_opt])
58-
-> (int[@local_opt])
59-
-> 'a
60-
-> unit
67+
: ('a : any_non_null).
68+
('a array[@local_opt]) -> (int[@local_opt]) -> 'a -> unit
6169
= "%array_unsafe_set"
62-
63-
(** [create ~len x] creates an array of length [len] with the value [x] populated in
64-
each element. *)
65-
val create : len:int -> 'a -> 'a t
66-
67-
(** [create_local ~len x] is like [create]. It allocates the array on the local stack.
68-
The array's elements are still global. *)
69-
val create_local : len:int -> 'a -> local_ 'a t
70+
[@@layout_poly]
71+
72+
[%%template:
73+
external create
74+
: ('a : any_non_null).
75+
len:int -> 'a -> 'a array @ m
76+
= "%makearray_dynamic"
77+
[@@ocaml.doc
78+
" [create ~len x] creates an array of length [len] with the value [x] populated in\n\
79+
\ each element. "]
80+
[@@layout_poly]
81+
[@@alloc __ @ m = (heap_global, stack_local)]]
82+
83+
external create_local
84+
: ('a : any_non_null).
85+
len:int -> 'a -> local_ 'a array
86+
= "%makearray_dynamic"
87+
[@@ocaml.doc
88+
" [create_local ~len x] is like [create]. It allocates the array on the local\n\
89+
\ stack. The array's elements are still global. "]
90+
[@@layout_poly]
91+
92+
external magic_create_uninitialized
93+
: ('a : any_non_null).
94+
len:int -> ('a array[@local_opt])
95+
= "%makearray_dynamic_uninit"
96+
[@@ocaml.doc
97+
" [magic_create_uninitialized ~len] creates an array of length [len] with\n\
98+
\ uninitialized elements -- that is, they may contain arbitrary, \
99+
nondeterministic\n\
100+
\ 'a values. This can be significantly faster than using [create].\n\n\
101+
\ [magic_create_uninitialized] can only be used for GC-ignorable arrays not\n\
102+
\ involving tagged immediates and arrays of elements with unboxed number \
103+
layout.\n\
104+
\ The compiler rejects attempts to use [magic_create_uninitialized] to \
105+
produce\n\
106+
\ e.g. an [('a : value) array].\n\n\
107+
\ [magic_create_uninitialized] can break abstraction boundaries and type \
108+
safety\n\
109+
\ (e.g. by creating phony witnesses to type equality) and so should be \
110+
used with\n\
111+
\ caution. "]
112+
[@@layout_poly]
70113

71114
(** [create_float_uninitialized ~len] creates a float array of length [len] with
72115
uninitialized elements -- that is, they may contain arbitrary, nondeterministic
@@ -116,6 +159,13 @@ module Definitions = struct
116159
types. The unsafe versions do not bound-check the arguments. *)
117160
include Blit.S1 with type 'a t := 'a t
118161

162+
val%template foldi_right
163+
: 'a t @ local
164+
-> init:'acc @ m
165+
-> f:(int -> 'a -> 'acc @ m -> 'acc @ m)
166+
-> 'acc @ m
167+
[@@alloc a @ m = (stack_local, heap_global)]
168+
119169
(** [folding_map] is a version of [map] that threads an accumulator through calls to
120170
[f]. *)
121171
val folding_map : 'a t -> init:'acc -> f:local_ ('acc -> 'a -> 'acc * 'b) -> 'b t
@@ -139,7 +189,12 @@ module Definitions = struct
139189
(** [Array.fold_right f a ~init] computes
140190
[f a.(0) (f a.(1) ( ... (f a.(n-1) init) ...))], where [n] is the length of the
141191
array [a]. *)
142-
val fold_right : 'a t -> f:local_ ('a -> 'acc -> 'acc) -> init:'acc -> 'acc
192+
val%template fold_right
193+
: 'a t @ m
194+
-> f:local_ ('a @ m -> 'acc -> 'acc)
195+
-> init:'acc
196+
-> 'acc
197+
[@@mode m = (uncontended, shared)]
143198

144199
(** All sort functions in this module sort in increasing order by default. *)
145200

@@ -309,7 +364,7 @@ module type Array = sig @@ portable
309364
include Definitions
310365
end
311366

312-
include Public with type 'a t = 'a array (** @inline *)
367+
include Public with type ('a : any_non_null) t = 'a array (** @inline *)
313368

314369
(**/**)
315370

0 commit comments

Comments
 (0)