1Misc(3) OCaml library Misc(3)
2
3
4
6 Misc - Miscellaneous useful types and functions
7
9 Module Misc
10
12 Module Misc
13 : sig end
14
15
16 Miscellaneous useful types and functions
17
18 Warning: this module is unstable and part of Compiler_libs .
19
20
21
22
23
24
25 val fatal_error : string -> 'a
26
27
28
29
30 val fatal_errorf : ('a, Format.formatter, unit, 'b) format4 -> 'a
31
32
33
34
35 exception Fatal_error
36
37
38
39
40
41 val try_finally : ?always:(unit -> unit) -> ?exceptionally:(unit ->
42 unit) -> (unit -> 'a) -> 'a
43
44
45 try_finally work ~always ~exceptionally is designed to run code in work
46 that may fail with an exception, and has two kind of cleanup routines:
47 always , that must be run after any execution of the function (typi‐
48 cally, freeing system resources), and exceptionally , that should be
49 run only if work or always failed with an exception (typically, undoing
50 user-visible state changes that would only make sense if the function
51 completes correctly). For example:
52
53
54 let objfile = outputprefix ^ .cmo in let oc = open_out_bin objfile in
55 Misc.try_finally (fun () -> bytecode ++ Timings.(accumulate_time (Gen‐
56 erate sourcefile)) (Emitcode.to_file oc modulename objfile); Warn‐
57 ings.check_fatal ()) ~always:(fun () -> close_out oc) ~exception‐
58 ally:(fun _exn -> remove_file objfile);
59
60 If exceptionally fail with an exception, it is propagated as usual.
61
62 If always or exceptionally use exceptions internally for control-flow
63 but do not raise, then try_finally is careful to preserve any exception
64 backtrace coming from work or always for easier debugging.
65
66
67
68 val map_end : ('a -> 'b) -> 'a list -> 'b list -> 'b list
69
70
71
72
73 val map_left_right : ('a -> 'b) -> 'a list -> 'b list
74
75
76
77
78 val for_all2 : ('a -> 'b -> bool) -> 'a list -> 'b list -> bool
79
80
81
82
83 val replicate_list : 'a -> int -> 'a list
84
85
86
87
88 val list_remove : 'a -> 'a list -> 'a list
89
90
91
92
93 val split_last : 'a list -> 'a list * 'a
94
95
96
97
98 val may : ('a -> unit) -> 'a option -> unit
99
100
101
102
103 val may_map : ('a -> 'b) -> 'a option -> 'b option
104
105
106
107 type ref_and_value =
108 | R : 'a ref * 'a -> ref_and_value
109
110
111
112
113
114 val protect_refs : ref_and_value list -> (unit -> 'a) -> 'a
115
116
117 protect_refs l f temporarily sets r to v for each R (r, v) in l while
118 executing f . The previous contents of the references is restored even
119 if f raises an exception.
120
121
122 module Stdlib : sig end
123
124
125
126
127
128 val find_in_path : string list -> string -> string
129
130
131
132
133 val find_in_path_rel : string list -> string -> string
134
135
136
137
138 val find_in_path_uncap : string list -> string -> string
139
140
141
142
143 val remove_file : string -> unit
144
145
146
147
148 val expand_directory : string -> string -> string
149
150
151
152
153 val split_path_contents : ?sep:char -> string -> string list
154
155
156
157
158 val create_hashtable : int -> ('a * 'b) list -> ('a, 'b) Hashtbl.t
159
160
161
162
163 val copy_file : in_channel -> out_channel -> unit
164
165
166
167
168 val copy_file_chunk : in_channel -> out_channel -> int -> unit
169
170
171
172
173 val string_of_file : in_channel -> string
174
175
176
177
178 val output_to_file_via_temporary : ?mode:open_flag list -> string ->
179 (string -> out_channel -> 'a) -> 'a
180
181
182
183
184 val log2 : int -> int
185
186
187
188
189 val align : int -> int -> int
190
191
192
193
194 val no_overflow_add : int -> int -> bool
195
196
197
198
199 val no_overflow_sub : int -> int -> bool
200
201
202
203
204 val no_overflow_mul : int -> int -> bool
205
206
207
208
209 val no_overflow_lsl : int -> int -> bool
210
211
212
213 module Int_literal_converter : sig end
214
215
216
217
218
219 val chop_extensions : string -> string
220
221
222
223
224 val search_substring : string -> string -> int -> int
225
226
227
228
229 val replace_substring : before:string -> after:string -> string ->
230 string
231
232
233
234
235 val rev_split_words : string -> string list
236
237
238
239
240 val get_ref : 'a list ref -> 'a list
241
242
243
244
245 val set_or_ignore : ('a -> 'b option) -> 'b option ref -> 'a -> unit
246
247
248
249
250 val fst3 : 'a * 'b * 'c -> 'a
251
252
253
254
255 val snd3 : 'a * 'b * 'c -> 'b
256
257
258
259
260 val thd3 : 'a * 'b * 'c -> 'c
261
262
263
264
265 val fst4 : 'a * 'b * 'c * 'd -> 'a
266
267
268
269
270 val snd4 : 'a * 'b * 'c * 'd -> 'b
271
272
273
274
275 val thd4 : 'a * 'b * 'c * 'd -> 'c
276
277
278
279
280 val for4 : 'a * 'b * 'c * 'd -> 'd
281
282
283
284 module LongString : sig end
285
286
287
288
289
290 val edit_distance : string -> string -> int -> int option
291
292
293 edit_distance a b cutoff computes the edit distance between strings a
294 and b . To help efficiency, it uses a cutoff: if the distance d is
295 smaller than cutoff , it returns Some d , else None .
296
297 The distance algorithm currently used is Damerau-Levenshtein: it com‐
298 putes the number of insertion, deletion, substitution of letters, or
299 swapping of adjacent letters to go from one word to the other. The par‐
300 ticular algorithm may change in the future.
301
302
303
304 val spellcheck : string list -> string -> string list
305
306
307 spellcheck env name takes a list of names env that exist in the current
308 environment and an erroneous name , and returns a list of suggestions
309 taken from env , that are close enough to name that it may be a typo
310 for one of them.
311
312
313
314 val did_you_mean : Format.formatter -> (unit -> string list) -> unit
315
316
317 did_you_mean ppf get_choices hints that the user may have meant one of
318 the option returned by calling get_choices . It does nothing if the
319 returned list is empty.
320
321 The unit -> ... thunking is meant to delay any potentially-slow compu‐
322 tation (typically computing edit-distance with many things from the
323 current environment) to when the hint message is to be printed. You
324 should print an understandable error message before calling
325 did_you_mean , so that users get a clear notification of the failure
326 even if producing the hint is slow.
327
328
329
330 val cut_at : string -> char -> string * string
331
332
333 String.cut_at s c returns a pair containing the sub-string before the
334 first occurrence of c in s , and the sub-string after the first occur‐
335 rence of c in s . let (before, after) = String.cut_at s c in before ^
336 String.make 1 c ^ after is the identity if s contains c .
337
338 Raise Not_found if the character does not appear in the string
339
340
341 Since 4.01
342
343
344 module Color : sig end
345
346
347
348
349 module Error_style : sig end
350
351
352
353
354
355 val normalise_eol : string -> string
356
357
358 normalise_eol s returns a fresh copy of s with any '\r' characters
359 removed. Intended for pre-processing text which will subsequently be
360 printed on a channel which performs EOL transformations (i.e. Windows)
361
362
363
364 val delete_eol_spaces : string -> string
365
366
367 delete_eol_spaces s returns a fresh copy of s with any end of line spa‐
368 ces removed. Intended to normalize the output of the toplevel for
369 tests.
370
371
372
373 val pp_two_columns : ?sep:string -> ?max_lines:int -> Format.formatter
374 -> (string * string) list -> unit
375
376
377 pp_two_columns ?sep ?max_lines ppf l prints the lines in l as two col‐
378 umns separated by sep ("|" by default). max_lines can be used to indi‐
379 cate a maximum number of lines to print -- an ellipsis gets inserted at
380 the middle if the input has too many lines.
381
382 Example:
383
384 pp_two_columns ~max_lines:3 Format.std_formatter [ "abc", "hello";
385 "def", "zzz"; "a" , "bllbl"; "bb" , "dddddd"; ]
386
387 prints
388
389 abc | hello ... bb | dddddd
390
391
392
393
394
395 Hook machinery
396 Hooks machinery: add_hook name f will register a function that will be
397 called on the argument of a later call to apply_hooks . Hooks are
398 applied in the lexicographical order of their names.
399
400 type hook_info = {
401 sourcefile : string ;
402 }
403
404
405
406
407
408 exception HookExnWrapper of {
409 error : exn ;
410 hook_name : string ;
411 hook_info : hook_info ;
412 }
413
414
415 An exception raised by a hook will be wrapped into a HookExnWrapper
416 constructor by the hook machinery.
417
418
419
420 val raise_direct_hook_exn : exn -> 'a
421
422 A hook can use raise_unwrapped_hook_exn to raise an exception that will
423 not be wrapped into a Misc.HookExnWrapper .
424
425
426 module type HookSig = sig end
427
428
429
430
431 module MakeHooks : functor (M : sig end) -> sig end
432
433
434
435
436
437 val show_config_and_exit : unit -> unit
438
439 configuration variables
440
441
442
443 val show_config_variable_and_exit : string -> unit
444
445
446
447
448 val get_build_path_prefix_map : unit -> Build_path_prefix_map.map
449 option
450
451 Returns the map encoded in the BUILD_PATH_PREFIX_MAP environment vari‐
452 able.
453
454
455
456 val debug_prefix_map_flags : unit -> string list
457
458 Returns the list of --debug-prefix-map flags to be passed to the assem‐
459 bler, built from the BUILD_PATH_PREFIX_MAP environment variable.
460
461
462
463 val print_if : Format.formatter -> bool ref -> (Format.formatter -> 'a
464 -> unit) -> 'a -> 'a
465
466
467 print_if ppf flag fmt x prints x with fmt on ppf if b is true.
468
469
470
471
472
473OCamldoc 2019-07-30 Misc(3)