Module Misc

module Misc: sig .. end

Miscellaneous useful types and functions

Warning: this module is unstable and part of compiler-libs.


let fatal_error: string => 'a;
let fatal_errorf: format4('a, Format.formatter, unit, 'b) => 'a;
exception Fatal_error;
let try_finally:
  (~always: unit => unit=?, ~exceptionally: unit => unit=?, unit => 'a) => 'a;

try_finally work ~always ~exceptionally is designed to run code in work that may fail with an exception, and has two kind of cleanup routines: always, that must be run after any execution of the function (typically, freeing system resources), and exceptionally, that should be run only if work or always failed with an exception (typically, undoing user-visible state changes that would only make sense if the function completes correctly). For example:

      let objfile = outputprefix ^ ".cmo" in
      let oc = open_out_bin objfile in
      Misc.try_finally
        (fun () ->
           bytecode
           ++ Timings.(accumulate_time (Generate sourcefile))
               (Emitcode.to_file oc modulename objfile);
           Warnings.check_fatal ())
        ~always:(fun () -> close_out oc)
        ~exceptionally:(fun _exn -> remove_file objfile);
    

If exceptionally fail with an exception, it is propagated as usual.

If always or exceptionally use exceptions internally for control-flow but do not raise, then try_finally is careful to preserve any exception backtrace coming from work or always for easier debugging.

let reraise_preserving_backtrace: (exn, unit => unit) => 'a;

reraise_preserving_backtrace e f is (f (); raise e) except that the current backtrace is preserved, even if f uses exceptions internally.

let map_end: ('a => 'b, list('a), list('b)) => list('b);
let map_left_right: ('a => 'b, list('a)) => list('b);
let for_all2: (('a, 'b) => bool, list('a), list('b)) => bool;
let replicate_list: ('a, int) => list('a);
let list_remove: ('a, list('a)) => list('a);
let split_last: list('a) => (list('a), 'a);
type ref_and_value = 
| R : 'a ref * 'a -> ref_and_value
let protect_refs: (list(ref_and_value), unit => 'a) => 'a;

protect_refs l f temporarily sets r to v for each R (r, v) in l while executing f. The previous contents of the references is restored even if f raises an exception, without altering the exception backtrace.

module Stdlib: sig .. end
let find_in_path: (list(string), string) => string;
let find_in_path_rel: (list(string), string) => string;
let find_in_path_uncap: (list(string), string) => string;
let remove_file: string => unit;
let expand_directory: (string, string) => string;
let split_path_contents: (~sep: char=?, string) => list(string);
let create_hashtable: (int, list(('a, 'b))) => Hashtbl.t('a, 'b);
let copy_file: (in_channel, out_channel) => unit;
let copy_file_chunk: (in_channel, out_channel, int) => unit;
let string_of_file: in_channel => string;
let output_to_file_via_temporary:
  (~mode: list(open_flag)=?, string, (string, out_channel) => 'a) => 'a;
let protect_writing_to_file: (~filename: string, ~f: out_channel => 'a) => 'a;

Open the given filename for writing (in binary mode), pass the out_channel to the given function, then close the channel. If the function raises an exception then filename will be removed.

let log2: int => int;
let align: (int, int) => int;
let no_overflow_add: (int, int) => bool;
let no_overflow_sub: (int, int) => bool;
let no_overflow_mul: (int, int) => bool;
let no_overflow_lsl: (int, int) => bool;
module Int_literal_converter: sig .. end
let chop_extensions: string => string;
let search_substring: (string, string, int) => int;
let replace_substring: (~before: string, ~after: string, string) => string;
let rev_split_words: string => list(string);
let get_ref: ref(list('a)) => list('a);
let set_or_ignore: ('a => option('b), ref(option('b)), 'a) => unit;
let fst3: (('a, 'b, 'c)) => 'a;
let snd3: (('a, 'b, 'c)) => 'b;
let thd3: (('a, 'b, 'c)) => 'c;
let fst4: (('a, 'b, 'c, 'd)) => 'a;
let snd4: (('a, 'b, 'c, 'd)) => 'b;
let thd4: (('a, 'b, 'c, 'd)) => 'c;
let for4: (('a, 'b, 'c, 'd)) => 'd;
module LongString: sig .. end
let edit_distance: (string, string, int) => option(int);

edit_distance a b cutoff computes the edit distance between strings a and b. To help efficiency, it uses a cutoff: if the distance d is smaller than cutoff, it returns Some d, else None.

The distance algorithm currently used is Damerau-Levenshtein: it computes the number of insertion, deletion, substitution of letters, or swapping of adjacent letters to go from one word to the other. The particular algorithm may change in the future.

let spellcheck: (list(string), string) => list(string);

spellcheck env name takes a list of names env that exist in the current environment and an erroneous name, and returns a list of suggestions taken from env, that are close enough to name that it may be a typo for one of them.

let did_you_mean: (Format.formatter, unit => list(string)) => unit;

did_you_mean ppf get_choices hints that the user may have meant one of the option returned by calling get_choices. It does nothing if the returned list is empty.

The unit -> ... thunking is meant to delay any potentially-slow computation (typically computing edit-distance with many things from the current environment) to when the hint message is to be printed. You should print an understandable error message before calling did_you_mean, so that users get a clear notification of the failure even if producing the hint is slow.

let cut_at: (string, char) => (string, string);

String.cut_at s c returns a pair containing the sub-string before the first occurrence of c in s, and the sub-string after the first occurrence of c in s. let (before, after) = String.cut_at s c in before ^ String.make 1 c ^ after is the identity if s contains c.

Raise Not_found if the character does not appear in the string

module Color: sig .. end
module Error_style: sig .. end
let normalise_eol: string => string;

normalise_eol s returns a fresh copy of s with any '\r' characters removed. Intended for pre-processing text which will subsequently be printed on a channel which performs EOL transformations (i.e. Windows)

let delete_eol_spaces: string => string;

delete_eol_spaces s returns a fresh copy of s with any end of line spaces removed. Intended to normalize the output of the toplevel for tests.

let pp_two_columns:
  (
    ~sep: string=?,
    ~max_lines: int=?,
    Format.formatter,
    list((string, string))
  ) =>
  unit;

pp_two_columns ?sep ?max_lines ppf l prints the lines in l as two columns separated by sep ("|" by default). max_lines can be used to indicate a maximum number of lines to print -- an ellipsis gets inserted at the middle if the input has too many lines.

Example:

pp_two_columns ~max_lines:3 Format.std_formatter [
      "abc", "hello";
      "def", "zzz";
      "a"  , "bllbl";
      "bb" , "dddddd";
    ]

prints

    abc | hello
    ...
    bb  | dddddd
   
let show_config_and_exit: unit => unit;

configuration variables

let show_config_variable_and_exit: string => unit;
let get_build_path_prefix_map: unit => option(Build_path_prefix_map.map);

Returns the map encoded in the BUILD_PATH_PREFIX_MAP environment variable.

let debug_prefix_map_flags: unit => list(string);

Returns the list of --debug-prefix-map flags to be passed to the assembler, built from the BUILD_PATH_PREFIX_MAP environment variable.

let print_if:
  (Format.formatter, ref(bool), (Format.formatter, 'a) => unit, 'a) => 'a;

print_if ppf flag fmt x prints x with fmt on ppf if b is true.

type filepath = string;
type modname = string;
type crcs = list((modname, option(Digest.t)));
type alerts = Stdlib.String.Map.t(string);
module EnvLazy: sig .. end
module Magic_number: sig .. end