Misc(3) OCaml library Misc(3)

# [NAME](#NAME)

Misc - Miscellaneous useful types and functions

# [Module](#Module)

Module Misc

# [Documentation](#Documentation)

Module **Misc**  
: **sig end**

Miscellaneous useful types and functions

Warning: this module is unstable and part of **Compiler\_libs** .

*val fatal\_error* : **string -&gt; 'a**

*val fatal\_errorf* : **('a, Format.formatter, unit, 'b) format4 -&gt; 'a**

*exception Fatal\_error*

*val try\_finally* : **?always:(unit -&gt; unit) -&gt; ?exceptionally:(unit -&gt; unit) -&gt; (unit -&gt; 'a) -&gt; '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.

*val reraise\_preserving\_backtrace* : **exn -&gt; (unit -&gt; unit) -&gt; 'a**

**reraise\_preserving\_backtrace e f** is (f (); raise e) except that the current backtrace is preserved, even if **f** uses exceptions internally.

*val map\_end* : **('a -&gt; 'b) -&gt; 'a list -&gt; 'b list -&gt; 'b list**

*val map\_left\_right* : **('a -&gt; 'b) -&gt; 'a list -&gt; 'b list**

*val for\_all2* : **('a -&gt; 'b -&gt; bool) -&gt; 'a list -&gt; 'b list -&gt; bool**

*val replicate\_list* : **'a -&gt; int -&gt; 'a list**

*val list\_remove* : **'a -&gt; 'a list -&gt; 'a list**

*val split\_last* : **'a list -&gt; 'a list * 'a**

*type ref\_and\_value* =  
| R **:** **'a ref * 'a** **-&gt;** **ref\_and\_value**

*val protect\_refs* : **ref\_and\_value list -&gt; (unit -&gt; 'a) -&gt; '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**

*val find\_in\_path* : **string list -&gt; string -&gt; string**

*val find\_in\_path\_rel* : **string list -&gt; string -&gt; string**

*val find\_in\_path\_uncap* : **string list -&gt; string -&gt; string**

*val remove\_file* : **string -&gt; unit**

*val expand\_directory* : **string -&gt; string -&gt; string**

*val split\_path\_contents* : **?sep:char -&gt; string -&gt; string list**

*val create\_hashtable* : **int -&gt; ('a * 'b) list -&gt; ('a, 'b) Hashtbl.t**

*val copy\_file* : **in\_channel -&gt; out\_channel -&gt; unit**

*val copy\_file\_chunk* : **in\_channel -&gt; out\_channel -&gt; int -&gt; unit**

*val string\_of\_file* : **in\_channel -&gt; string**

*val output\_to\_file\_via\_temporary* : **?mode:open\_flag list -&gt;** **string -&gt; (string -&gt; out\_channel -&gt; 'a) -&gt; 'a**

*val protect\_writing\_to\_file* : **filename:string -&gt; f:(out\_channel -&gt; 'a) -&gt; '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.

*val log2* : **int -&gt; int**

*val align* : **int -&gt; int -&gt; int**

*val no\_overflow\_add* : **int -&gt; int -&gt; bool**

*val no\_overflow\_sub* : **int -&gt; int -&gt; bool**

*val no\_overflow\_mul* : **int -&gt; int -&gt; bool**

*val no\_overflow\_lsl* : **int -&gt; int -&gt; bool**

*module Int\_literal\_converter :* **sig end**

*val chop\_extensions* : **string -&gt; string**

*val search\_substring* : **string -&gt; string -&gt; int -&gt; int**

*val replace\_substring* : **before:string -&gt; after:string -&gt; string -&gt; string**

*val rev\_split\_words* : **string -&gt; string list**

*val get\_ref* : **'a list ref -&gt; 'a list**

*val set\_or\_ignore* : **('a -&gt; 'b option) -&gt; 'b option ref -&gt; 'a -&gt; unit**

*val fst3* : **'a * 'b * 'c -&gt; 'a**

*val snd3* : **'a * 'b * 'c -&gt; 'b**

*val thd3* : **'a * 'b * 'c -&gt; 'c**

*val fst4* : **'a * 'b * 'c * 'd -&gt; 'a**

*val snd4* : **'a * 'b * 'c * 'd -&gt; 'b**

*val thd4* : **'a * 'b * 'c * 'd -&gt; 'c**

*val for4* : **'a * 'b * 'c * 'd -&gt; 'd**

*module LongString :* **sig end**

*val edit\_distance* : **string -&gt; string -&gt; int -&gt; int option**

**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.

*val spellcheck* : **string list -&gt; string -&gt; string list**

**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.

*val did\_you\_mean* : **Format.formatter -&gt; (unit -&gt; string list) -&gt; 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 -&gt; ...** 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.

*val cut\_at* : **string -&gt; char -&gt; 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

**Since** 4.01

*val ordinal\_suffix* : **int -&gt; string**

**ordinal\_suffix n** is the appropriate suffix to append to the numeral **n** as an ordinal number: **1** -&gt; **"st"** , **2** -&gt; **"nd"** , **3** -&gt; **"rd"** , **4** -&gt; **"th"** , and so on. Handles larger numbers (e.g., **42** -&gt; **"nd"** ) and the numbers 11--13 (which all get **"th"** ) correctly.

*module Color :* **sig end**

*module Error\_style :* **sig end**

*val normalise\_eol* : **string -&gt; 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)

*val delete\_eol\_spaces* : **string -&gt; 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.

*val pp\_two\_columns* : **?sep:string -&gt;** **?max\_lines:int -&gt; Format.formatter -&gt; (string * string) list -&gt; 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

*val show\_config\_and\_exit* : **unit -&gt; unit**

configuration variables

*val show\_config\_variable\_and\_exit* : **string -&gt; unit**

*val get\_build\_path\_prefix\_map* : **unit -&gt; Build\_path\_prefix\_map.map option**

Returns the map encoded in the **BUILD\_PATH\_PREFIX\_MAP** environment variable.

*val debug\_prefix\_map\_flags* : **unit -&gt; string list**

Returns the list of **--debug-prefix-map** flags to be passed to the assembler, built from the **BUILD\_PATH\_PREFIX\_MAP** environment variable.

*val print\_if* : **Format.formatter -&gt;** **bool ref -&gt; (Format.formatter -&gt; 'a -&gt; unit) -&gt; 'a -&gt; '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* = **(modname * Digest.t option) list**

*type alerts* = **string Stdlib.String.Map.t**

*module Magic\_number :* **sig end**

2026-10-27 OCamldoc

---

Document: Misc(3o) from ocaml-4 4.14.4-r4 (Chainguard OS)
Source: https://github.com/ocaml/ocaml
