Skip to content

Commit 52a6c24

Browse files
v0.18~preview.130.76+222
1 parent 35f6203 commit 52a6c24

File tree

116 files changed

+1439
-1277
lines changed

Some content is hidden

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

116 files changed

+1439
-1277
lines changed

base.opam

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ build: [
1212
depends: [
1313
"ocaml" {>= "5.1.0"}
1414
"basement"
15+
"capsule0"
1516
"ocaml_intrinsics_kernel"
1617
"ppx_array_base"
1718
"ppx_base"

generate/generate_pow_overflow_bounds.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
(* NB: This needs to be pure OCaml (no Base!), since we need this in order to build
2-
Base. *)
1+
(* NB: This needs to be pure OCaml (no Base!), since we need this in order to build Base. *)
32

43
(* This module generates lookup tables to detect integer overflow when calculating integer
5-
exponents. At index [e], [table.[e]^e] will not overflow, but [(table[e] + 1)^e]
6-
will. *)
4+
exponents. At index [e], [table.[e]^e] will not overflow, but [(table[e] + 1)^e] will. *)
75
module Z = Zarith.Z
86

97
type mode =

lint/ppx_base_lint.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ let check current_module =
148148
(remove_loc#attributes new_attrs))
149149
then (
150150
(* Remove attributes written by the user that correspond to attributes in the
151-
expansion *)
151+
expansion *)
152152
List.iter attrs ~f:(fun a ->
153153
if is_part_of_expansion a
154154
then Driver.register_correction ~loc:a.attr_loc ~repl:"");

shadow-stdlib/gen/gen.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
open StdLabels
22

33
let () =
4-
(* -permissive indicates that we should tolerate additions to stdlib.
5-
It's [true] in public-release so that new versions of the stdlib can be compatible
6-
with base, but it should be [false] internally so that we remember to
7-
consider implementing the equivalents in base. *)
4+
(* -permissive indicates that we should tolerate additions to stdlib. It's [true] in
5+
public-release so that new versions of the stdlib can be compatible with base, but it
6+
should be [false] internally so that we remember to consider implementing the
7+
equivalents in base. *)
88
let permissive, cmi_fn, oc =
99
match Sys.argv with
1010
| [| _; "-caml-cmi"; cmi_fn; "-o"; fn |] -> false, cmi_fn, open_out fn

src/array.ml

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ type ('a : any mod separable) t = 'a array
1616
type%template ('a : k) t = 'a array [@@kind k = (base_non_value, immediate, immediate64)]
1717

1818
[%%rederive.portable
19-
type nonrec 'a t = 'a array [@@deriving globalize, sexp ~stackify, sexp_grammar]]
19+
type nonrec ('a : value_or_null mod separable) t = 'a array
20+
[@@deriving globalize, sexp ~stackify, sexp_grammar]]
2021

2122
(* This module implements a new in-place, constant heap sorting algorithm to replace the
22-
one used by the standard libraries. Its only purpose is to be faster (hopefully
23+
one used by the standard libraries. Its only purpose is to be faster (hopefully
2324
strictly faster) than the base sort and stable_sort.
2425
2526
At a high level the algorithm is:
@@ -39,11 +40,10 @@ type%template ('a : k) t = 'a array [@@kind k = (base_non_value, immediate, imme
3940
behavior
4041
4142
See the following for more information:
42-
- "Dual-Pivot Quicksort" by Vladimir Yaroslavskiy.
43-
Available at
43+
- "Dual-Pivot Quicksort" by Vladimir Yaroslavskiy. Available at
4444
http://www.kriche.com.ar/root/programming/spaceTimeComplexity/DualPivotQuicksort.pdf
45-
- "Quicksort is Optimal" by Sedgewick and Bentley.
46-
Slides at http://www.cs.princeton.edu/~rs/talks/QuicksortIsOptimal.pdf
45+
- "Quicksort is Optimal" by Sedgewick and Bentley. Slides at
46+
http://www.cs.princeton.edu/~rs/talks/QuicksortIsOptimal.pdf
4747
- http://www.sorting-algorithms.com/quick-sort-3-way *)
4848

4949
module%template.portable
@@ -75,9 +75,9 @@ struct
7575
(* http://en.wikipedia.org/wiki/Insertion_sort *)
7676
module Insertion_sort : Sort = struct
7777
(* loop invariants:
78-
1. the subarray arr[left .. i-1] is sorted
79-
2. the subarray arr[i+1 .. pos] is sorted and contains only elements > v
80-
3. arr[i] may be thought of as containing v
78+
1. the subarray arr[left .. i-1] is sorted
79+
2. the subarray arr[i+1 .. pos] is sorted and contains only elements > v
80+
3. arr[i] may be thought of as containing v
8181
*)
8282
let rec insert_loop arr ~left ~compare i v =
8383
let i_next = i - 1 in
@@ -89,8 +89,7 @@ struct
8989
;;
9090

9191
let sort arr ~compare ~left ~right =
92-
(* loop invariant:
93-
[arr] is sorted from [left] to [pos - 1], inclusive *)
92+
(* loop invariant: [arr] is sorted from [left] to [pos - 1], inclusive *)
9493
for pos = left + 1 to right do
9594
let v = get arr pos in
9695
let final_pos = insert_loop arr ~left ~compare pos v in
@@ -101,8 +100,7 @@ struct
101100

102101
(* http://en.wikipedia.org/wiki/Heapsort *)
103102
module Heap_sort : Sort = struct
104-
(* loop invariant:
105-
root's children are both either roots of max-heaps or > right *)
103+
(* loop invariant: root's children are both either roots of max-heaps or > right *)
106104
let rec heapify arr ~compare root ~left ~right =
107105
let relative_root = root - left in
108106
let left_child = (2 * relative_root) + left + 1 in
@@ -124,7 +122,7 @@ struct
124122
;;
125123

126124
let build_heap arr ~compare ~left ~right =
127-
(* Elements in the second half of the array are already heaps of size 1. We move
125+
(* Elements in the second half of the array are already heaps of size 1. We move
128126
through the first half of the array from back to front examining the element at
129127
hand, and the left and right children, fixing the heap property as we go. *)
130128
for i = (left + right) / 2 downto left do
@@ -135,9 +133,9 @@ struct
135133
let sort arr ~compare ~left ~right =
136134
build_heap arr ~compare ~left ~right;
137135
(* loop invariants:
138-
1. the subarray arr[left ... i] is a max-heap H
139-
2. the subarray arr[i+1 ... right] is sorted (call it S)
140-
3. every element of H is less than every element of S *)
136+
1. the subarray arr[left ... i] is a max-heap H
137+
2. the subarray arr[i+1 ... right] is sorted (call it S)
138+
3. every element of H is less than every element of S *)
141139
for i = right downto left + 1 do
142140
swap arr left i;
143141
heapify arr ~compare left ~left ~right:(i - 1)
@@ -175,7 +173,7 @@ struct
175173
4-----o--------o--o--|-----o--4
176174
| | |
177175
5-----o--------------o-----o--5
178-
v} *)
176+
v} *)
179177
compare_and_swap m1 m2;
180178
compare_and_swap m4 m5;
181179
compare_and_swap m1 m3;
@@ -188,13 +186,11 @@ struct
188186
;;
189187

190188
(* choose pivots for the array by sorting 5 elements and examining the center three
191-
elements. The goal is to choose two pivots that will either:
192-
- break the range up into 3 even partitions
193-
or
194-
- eliminate a commonly appearing element by sorting it into the center partition
195-
by itself
196-
To this end we look at the center 3 elements of the 5 and return pairs of equal
197-
elements or the widest range *)
189+
elements. The goal is to choose two pivots that will either:
190+
- break the range up into 3 even partitions or
191+
- eliminate a commonly appearing element by sorting it into the center partition by
192+
itself To this end we look at the center 3 elements of the 5 and return pairs of
193+
equal elements or the widest range *)
198194
let choose_pivots arr ~(local_ compare : _ -> _ -> _) ~left ~right =
199195
let sixth = (right - left) / 6 in
200196
let m1 = left + sixth in
@@ -215,7 +211,7 @@ struct
215211

216212
let dual_pivot_partition arr ~(local_ compare : _ -> _ -> _) ~left ~right =
217213
let #(pivot1, pivot2, pivots_equal) = choose_pivots arr ~compare ~left ~right in
218-
(* loop invariants:
214+
(*=loop invariants:
219215
1. left <= l < r <= right
220216
2. l <= p <= r
221217
3. l <= x < p implies arr[x] >= pivot1
@@ -233,7 +229,7 @@ struct
233229
loop (l + 1) (p + 1) r)
234230
else if compare pv pivot2 > 0
235231
then (
236-
(* loop invariants: same as those of the outer loop *)
232+
(* loop invariants: same as those of the outer loop *)
237233
let rec scan_backwards r =
238234
if r > p && compare (get arr r) pivot2 > 0
239235
then scan_backwards (r - 1)
@@ -251,7 +247,7 @@ struct
251247
let rec intro_sort arr ~max_depth ~compare ~left ~right =
252248
let len = right - left + 1 in
253249
(* This takes care of some edge cases, such as left > right or very short arrays,
254-
since Insertion_sort.sort handles these cases properly. Thus we don't need to
250+
since Insertion_sort.sort handles these cases properly. Thus we don't need to
255251
make sure that left and right are valid in recursive calls. *)
256252
if len <= 32
257253
then Insertion_sort.sort arr ~compare ~left ~right
@@ -691,8 +687,7 @@ let check_length2_exn name t1 t2 =
691687
if n1 <> n2 then raise_length_mismatch name n1 n2
692688
;;
693689

694-
(* [of_list_map] and [of_list_rev_map] are based on functions from the OCaml
695-
distribution. *)
690+
(* [of_list_map] and [of_list_rev_map] are based on functions from the OCaml distribution. *)
696691

697692
let of_list_map (xs : (_ List.Constructors.t[@kind k1])) ~f =
698693
match xs with
@@ -1062,8 +1057,8 @@ let sorted_copy t ~compare =
10621057
let last_exn t = t.(length t - 1)
10631058
let last = last_exn
10641059

1065-
(* Convert to a sequence but does not attempt to protect against modification
1066-
in the array. *)
1060+
(* Convert to a sequence but does not attempt to protect against modification in the
1061+
array. *)
10671062
let to_sequence_mutable t =
10681063
Sequence.unfold_step ~init:0 ~f:(fun i ->
10691064
if i >= length t

src/array0.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
(* [Array0] defines array functions that are primitives or can be simply defined in terms
2-
of [Stdlib.Array]. [Array0] is intended to completely express the part of [Stdlib.Array]
3-
that [Base] uses -- no other file in Base other than array0.ml should use [Stdlib.Array].
4-
[Array0] has few dependencies, and so is available early in Base's build order. All
5-
Base files that need to use arrays and come before [Base.Array] in build order should
6-
do [module Array = Array0]. This includes uses of subscript syntax ([x.(i)], [x.(i) <-
7-
e]), which the OCaml parser desugars into calls to [Array.get] and [Array.set].
8-
Defining [module Array = Array0] is also necessary because it prevents ocamldep from
9-
mistakenly causing a file to depend on [Base.Array]. *)
2+
of [Stdlib.Array]. [Array0] is intended to completely express the part of
3+
[Stdlib.Array] that [Base] uses -- no other file in Base other than array0.ml should
4+
use [Stdlib.Array]. [Array0] has few dependencies, and so is available early in Base's
5+
build order. All Base files that need to use arrays and come before [Base.Array] in
6+
build order should do [module Array = Array0]. This includes uses of subscript syntax
7+
([x.(i)], [x.(i) <- e]), which the OCaml parser desugars into calls to [Array.get] and
8+
[Array.set]. Defining [module Array = Array0] is also necessary because it prevents
9+
ocamldep from mistakenly causing a file to depend on [Base.Array]. *)
1010

1111
[@@@warning "-incompatible-with-upstream"]
1212

src/array_intf.ml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ module Definitions = struct
2626
type 'a t := 'a t
2727
type 'a t = 'a t [@@kind base_non_value, immediate, immediate64]]
2828

29-
[@@@kind k = base_with_imm]
29+
[@@@kind k = base_or_null_with_imm]
3030

3131
[%%rederive:
32-
type nonrec ('a : k) t = 'a t
32+
type nonrec ('a : k mod separable) t = 'a t
3333
[@@kind k]
3434
[@@deriving compare ~localize, equal ~localize, sexp ~stackify, globalize]]]
3535

36-
[%%rederive: type nonrec 'a t = 'a t [@@deriving sexp_grammar]]
36+
[%%rederive:
37+
type nonrec ('a : value_or_null mod separable) t = 'a t [@@deriving sexp_grammar]]
3738

3839
include Indexed_container.S1_with_creators with type 'a t := 'a t
3940
include Invariant.S1 with type 'a t := 'a t
@@ -147,7 +148,9 @@ module Definitions = struct
147148
val create_float_uninitialized : len:int -> float t
148149

149150
(** [init n ~f] creates an array of length [n] with index [i] set to [f i]. *)
150-
val%template init : int -> f:(int -> 'a) @ local -> 'a array @ m
151+
val%template init
152+
: ('a : value_or_null mod separable).
153+
int -> f:(int -> 'a) @ local -> 'a array @ m
151154
[@@alloc __ @ m = (heap_global, stack_local)]
152155

153156
(** [Array.make_matrix dimx dimy e] returns a two-dimensional array (an array of
@@ -470,7 +473,7 @@ module type Array = sig @@ portable
470473

471474
(*_ See the Jane Street Style Guide for an explanation of [Private] submodules:
472475
473-
https://opensource.janestreet.com/standards/#private-submodules *)
476+
https://opensource.janestreet.com/standards/#private-submodules *)
474477
module Private : sig
475478
module%template [@kind k = value_with_imm] Sort : sig
476479
module type Sort = sig @@ portable

src/avltree.ml

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,11 @@ let invariant compare =
8383

8484
let invariant t ~compare = (invariant [@kind k v]) compare t
8585

86-
(* In the following comments,
87-
't is balanced' means that 'invariant t' does not
88-
raise an exception. This implies of course that each node's height field is
89-
correct.
90-
't is balanceable' means that height of the left and right subtrees of t
91-
differ by at most 3. *)
92-
93-
(* @pre: left and right subtrees have correct heights
94-
@post: output has the correct height *)
86+
(* In the following comments, 't is balanced' means that 'invariant t' does not raise an
87+
exception. This implies of course that each node's height field is correct. 't is
88+
balanceable' means that height of the left and right subtrees of t differ by at most 3. *)
89+
90+
(* @pre: left and right subtrees have correct heights @post: output has the correct height *)
9591
let update_height = function
9692
| Node ({ left; key = _; value = _; height = old_height; right } as x) ->
9793
let new_height =
@@ -101,26 +97,23 @@ let update_height = function
10197
| Empty | Leaf _ -> assert false
10298
;;
10399

104-
(* @pre: left and right subtrees are balanced
105-
@pre: tree is balanceable
106-
@post: output is balanced (in particular, height is correct) *)
100+
(* @pre: left and right subtrees are balanced @pre: tree is balanceable @post: output is
101+
balanced (in particular, height is correct) *)
107102
let balance tree =
108103
match tree with
109104
| Empty | Leaf _ -> tree
110105
| Node ({ left; key = _; value = _; height = _; right } as root_node) ->
111106
let hl = (height [@kind k v]) left
112107
and hr = (height [@kind k v]) right in
113-
(* + 2 is critically important, lowering it to 1 will break the Leaf
114-
assumptions in the code below, and will force us to promote leaf nodes in
115-
the balance routine. It's also faster, since it will balance less often.
116-
Note that the following code is delicate. The update_height calls must
117-
occur in the correct order, since update_height assumes its children have
118-
the correct heights. *)
108+
(* + 2 is critically important, lowering it to 1 will break the Leaf assumptions in
109+
the code below, and will force us to promote leaf nodes in the balance routine.
110+
It's also faster, since it will balance less often. Note that the following code
111+
is delicate. The update_height calls must occur in the correct order, since
112+
update_height assumes its children have the correct heights. *)
119113
if hl > hr + 2
120114
then (
121115
match left with
122-
(* It cannot be a leaf, because even if right is empty, a leaf
123-
is only height 1 *)
116+
(* It cannot be a leaf, because even if right is empty, a leaf is only height 1 *)
124117
| Empty | Leaf _ -> assert false
125118
| Node
126119
({ left = left_node_left
@@ -137,8 +130,8 @@ let balance tree =
137130
(update_height [@kind k v]) left;
138131
left)
139132
else (
140-
(* if right is a leaf, then left must be empty. That means
141-
height is 2. Even if hr is empty we still can't get here. *)
133+
(* if right is a leaf, then left must be empty. That means height is 2. Even if
134+
hr is empty we still can't get here. *)
142135
match left_node_right with
143136
| Empty | Leaf _ -> assert false
144137
| Node
@@ -191,9 +184,8 @@ let balance tree =
191184
tree)
192185
;;
193186

194-
(* @pre: t is balanced.
195-
@post: result is balanced, with new node inserted
196-
@post: !added = true iff the shape of the input tree changed. *)
187+
(* @pre: t is balanced. @post: result is balanced, with new node inserted @post: !added =
188+
true iff the shape of the input tree changed. *)
197189

198190
let rec add t ~replace ~compare ~added ~key:k ~data:v =
199191
match t with
@@ -202,9 +194,8 @@ let rec add t ~replace ~compare ~added ~key:k ~data:v =
202194
Leaf { key = k; value = v }
203195
| Leaf ({ key = k'; value = _ } as r) ->
204196
let c = compare k' k in
205-
(* This compare is reversed on purpose, we are pretending
206-
that the leaf was just inserted instead of the other way
207-
round, that way we only allocate one node. *)
197+
(* This compare is reversed on purpose, we are pretending that the leaf was just
198+
inserted instead of the other way round, that way we only allocate one node. *)
208199
if c = 0
209200
then (
210201
added := false;

src/base.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
structures (arrays, lists, strings).
2525
- [Result], [Error], and [Or_error], supporting the or-error pattern. *)
2626

27-
(*_ We hide this from the web docs because the line wrapping is bad, making it
28-
pretty much inscrutable. *)
27+
(*_ We hide this from the web docs because the line wrapping is bad, making it pretty much
28+
inscrutable. *)
2929
(**/**)
3030

3131
(* The intent is to shadow all of INRIA's standard library. Modules below would cause
@@ -201,7 +201,7 @@ module Export = struct
201201

202202
(* [deriving hash] is missing for [array] and [ref] since these types are mutable. *)
203203
[%%rederive.portable
204-
type 'a array = 'a Array.t
204+
type ('a : value_or_null mod separable) array = 'a Array.t
205205
[@@deriving
206206
compare ~localize, equal ~localize, globalize, sexp ~stackify, sexp_grammar]]
207207

@@ -406,8 +406,8 @@ module Export = struct
406406

407407
external force : ('a Lazy.t[@local_opt]) -> 'a @@ portable = "%lazy_force"
408408

409-
(* Export ['a or_null] with constructors [Null] and [This] whenever Base is opened,
410-
so uses of those identifiers work in both upstream OCaml and OxCaml. *)
409+
(* Export ['a or_null] with constructors [Null] and [This] whenever Base is opened, so
410+
uses of those identifiers work in both upstream OCaml and OxCaml. *)
411411

412412
type 'a or_null = 'a Or_null.t
413413
[@@or_null_reexport]
@@ -423,8 +423,8 @@ include Modes.Export (** @inline *)
423423
exception Not_found_s = Not_found_s
424424

425425
(* We perform these side effects here because we want them to run for any code that uses
426-
[Base]. If this were in another module in [Base] that was not used in some program,
427-
then the side effects might not be run in that program. This will run as long as the
426+
[Base]. If this were in another module in [Base] that was not used in some program,
427+
then the side effects might not be run in that program. This will run as long as the
428428
program refers to at least one value directly in [Base]; referring to values in
429429
[Base.Bool], for example, is not sufficient. *)
430430
let () = Backtrace.initialize_module ()

0 commit comments

Comments
 (0)