Module Make.Pervasives

The OCaml Standard library.

This module is automatically opened at the beginning of each compilation. All components of this module can therefore be referred by their short name, without prefixing them by Stdlib.

It particular, it provides the basic operations over the built-in types (numbers, booleans, byte sequences, strings, exceptions, references, lists, arrays, input-output channels, ...) and the standard library modules.

Exceptions

val raise : exn -> 'a

Raise the given exception value

val raise_notrace : exn -> 'a

A faster version raise which does not record the backtrace.

  • since 4.02.0
val invalid_arg : string -> 'a

Raise exception Invalid_argument with the given string.

val failwith : string -> 'a

Raise exception Failure with the given string.

exception Exit

The Exit exception is not raised by any library function. It is provided for use in your programs.

Boolean operations

val not : bool -> bool

The boolean negation.

val (&&) : bool -> bool -> bool

The boolean 'and'. Evaluation is sequential, left-to-right: in e1 && e2, e1 is evaluated first, and if it returns false, e2 is not evaluated at all. Right-associative operator, see Ocaml_operators for more information.

val (||) : bool -> bool -> bool

The boolean 'or'. Evaluation is sequential, left-to-right: in e1 || e2, e1 is evaluated first, and if it returns true, e2 is not evaluated at all. Right-associative operator, see Ocaml_operators for more information.

Debugging

val __LOC__ : string

__LOC__ returns the location at which this expression appears in the file currently being parsed by the compiler, with the standard error format of OCaml: "File %S, line %d, characters %d-%d".

  • since 4.02.0
val __FILE__ : string

__FILE__ returns the name of the file currently being parsed by the compiler.

  • since 4.02.0
val __LINE__ : int

__LINE__ returns the line number at which this expression appears in the file currently being parsed by the compiler.

  • since 4.02.0
val __MODULE__ : string

__MODULE__ returns the module name of the file being parsed by the compiler.

  • since 4.02.0
val __POS__ : string * int * int * int

__POS__ returns a tuple (file,lnum,cnum,enum), corresponding to the location at which this expression appears in the file currently being parsed by the compiler. file is the current filename, lnum the line number, cnum the character position in the line and enum the last character position in the line.

  • since 4.02.0
val __LOC_OF__ : 'a -> string * 'a

__LOC_OF__ expr returns a pair (loc, expr) where loc is the location of expr in the file currently being parsed by the compiler, with the standard error format of OCaml: "File %S, line %d, characters %d-%d".

  • since 4.02.0
val __LINE_OF__ : 'a -> int * 'a

__LINE_OF__ expr returns a pair (line, expr), where line is the line number at which the expression expr appears in the file currently being parsed by the compiler.

  • since 4.02.0
val __POS_OF__ : 'a -> (string * int * int * int) * 'a

__POS_OF__ expr returns a pair (loc,expr), where loc is a tuple (file,lnum,cnum,enum) corresponding to the location at which the expression expr appears in the file currently being parsed by the compiler. file is the current filename, lnum the line number, cnum the character position in the line and enum the last character position in the line.

  • since 4.02.0

Composition operators

val (|>) : 'a -> ('a -> 'b) -> 'b

Reverse-application operator: x |> f |> g is exactly equivalent to g (f (x)). Left-associative operator, see Ocaml_operators for more information.

  • since 4.01
val (@@) : ('a -> 'b) -> 'a -> 'b

Application operator: g @@ f @@ x is exactly equivalent to g (f (x)). Right-associative operator, see Ocaml_operators for more information.

  • since 4.01

Integer arithmetic

Integers are Sys.int_size bits wide. All operations are taken modulo 2Sys.int_size. They do not fail on overflow.

val (~-) : int -> int

Unary negation. You can also write - e instead of ~- e. Unary operator, see Ocaml_operators for more information.

val (~+) : int -> int

Unary addition. You can also write + e instead of ~+ e. Unary operator, see Ocaml_operators for more information.

  • since 3.12.0
val succ : int -> int

succ x is x + 1.

val pred : int -> int

pred x is x - 1.

val (+) : int -> int -> int

Integer addition. Left-associative operator, see Ocaml_operators for more information.

val (-) : int -> int -> int

Integer subtraction. Left-associative operator, , see Ocaml_operators for more information.

val (*) : int -> int -> int

Integer multiplication. Left-associative operator, see Ocaml_operators for more information.

val (/) : int -> int -> int

Integer division. Raise Division_by_zero if the second argument is 0. Integer division rounds the real quotient of its arguments towards zero. More precisely, if x >= 0 and y > 0, x / y is the greatest integer less than or equal to the real quotient of x by y. Moreover, (- x) / y = x / (- y) = - (x / y). Left-associative operator, see Ocaml_operators for more information.

val (mod) : int -> int -> int

Integer remainder. If y is not zero, the result of x mod y satisfies the following properties: x = (x / y) * y + x mod y and abs(x mod y) <= abs(y) - 1. If y = 0, x mod y raises Division_by_zero. Note that x mod y is negative only if x < 0. Raise Division_by_zero if y is zero. Left-associative operator, see Ocaml_operators for more information.

val abs : int -> int

Return the absolute value of the argument. Note that this may be negative if the argument is min_int.

val max_int : int

The greatest representable integer.

val min_int : int

The smallest representable integer.

Bitwise operations

val (land) : int -> int -> int

Bitwise logical and. Left-associative operator, see Ocaml_operators for more information.

val (lor) : int -> int -> int

Bitwise logical or. Left-associative operator, see Ocaml_operators for more information.

val (lxor) : int -> int -> int

Bitwise logical exclusive or. Left-associative operator, see Ocaml_operators for more information.

val lnot : int -> int

Bitwise logical negation.

val (lsl) : int -> int -> int

n lsl m shifts n to the left by m bits. The result is unspecified if m < 0 or m > Sys.int_size. Right-associative operator, see Ocaml_operators for more information.

val (lsr) : int -> int -> int

n lsr m shifts n to the right by m bits. This is a logical shift: zeroes are inserted regardless of the sign of n. The result is unspecified if m < 0 or m > Sys.int_size. Right-associative operator, see Ocaml_operators for more information.

val (asr) : int -> int -> int

n asr m shifts n to the right by m bits. This is an arithmetic shift: the sign bit of n is replicated. The result is unspecified if m < 0 or m > Sys.int_size. Right-associative operator, see Ocaml_operators for more information.

String operations

More string operations are provided in module String.

val (^) : string -> string -> string

String concatenation. Right-associative operator, see Ocaml_operators for more information.

Character operations

More character operations are provided in module Char.

val int_of_char : char -> int

Return the ASCII code of the argument.

val char_of_int : int -> char

Return the character with the given ASCII code. Raise Invalid_argument "char_of_int" if the argument is outside the range 0--255.

Unit operations

val ignore : 'a -> unit

Discard the value of its argument and return (). For instance, ignore(f x) discards the result of the side-effecting function f. It is equivalent to f x; (), except that the latter may generate a compiler warning; writing ignore(f x) instead avoids the warning.

String conversion functions

val string_of_bool : bool -> string

Return the string representation of a boolean. As the returned values may be shared, the user should not modify them directly.

val bool_of_string_opt : string -> bool option

Convert the given string to a boolean.

Return None if the string is not "true" or "false".

  • since 4.05
val string_of_int : int -> string

Return the string representation of an integer, in decimal.

val int_of_string_opt : string -> int option

Convert the given string to an integer. The string is read in decimal (by default, or if the string begins with 0u), in hexadecimal (if it begins with 0x or 0X), in octal (if it begins with 0o or 0O), or in binary (if it begins with 0b or 0B).

The 0u prefix reads the input as an unsigned integer in the range [0, 2*max_int+1]. If the input exceeds max_int it is converted to the signed integer min_int + input - max_int - 1.

The _ (underscore) character can appear anywhere in the string and is ignored.

Return None if the given string is not a valid representation of an integer, or if the integer represented exceeds the range of integers representable in type int.

  • since 4.05

Pair operations

val fst : ('a * 'b) -> 'a

Return the first component of a pair.

val snd : ('a * 'b) -> 'b

Return the second component of a pair.

List operations

More list operations are provided in module List.

val (@) : 'a list -> 'a list -> 'a list

List concatenation. Not tail-recursive (length of the first argument). Right-associative operator, see Ocaml_operators for more information.

References

type 'a ref = {
  1. mutable contents : 'a;
}

The type of references (mutable indirection cells) containing a value of type 'a.

val ref : 'a -> 'a ref

Return a fresh reference containing the given value.

val (!) : 'a ref -> 'a

!r returns the current contents of reference r. Equivalent to fun r -> r.contents. Unary operator, see Ocaml_operators for more information.

val (:=) : 'a ref -> 'a -> unit

r := a stores the value of a in reference r. Equivalent to fun r v -> r.contents <- v. Right-associative operator, see Ocaml_operators for more information.

val incr : int ref -> unit

Increment the integer contained in the given reference. Equivalent to fun r -> r := succ !r.

val decr : int ref -> unit

Decrement the integer contained in the given reference. Equivalent to fun r -> r := pred !r.

Result type

type ('a, 'b) result = ('a, 'b) Stdlib.result =
  1. | Ok of 'a
  2. | Error of 'b
  • since 4.03.0

Operations on format strings

Format strings are character strings with special lexical conventions that defines the functionality of formatted input/output functions. Format strings are used to read data with formatted input functions from module Scanf and to print data with formatted output functions from modules Printf and Format.

Format strings are made of three kinds of entities:

There is an additional lexical rule to escape the special characters '%' and '@' in format strings: if a special character follows a '%' character, it is treated as a plain character. In other words, "%%" is considered as a plain '%' and "%@" as a plain '@'.

For more information about conversion specifications and formatting indications available, read the documentation of modules Scanf, Printf and Format.

Format strings have a general and highly polymorphic type ('a, 'b, 'c, 'd, 'e, 'f) format6. The two simplified types, format and format4 below are included for backward compatibility with earlier releases of OCaml.

The meaning of format string type parameters is as follows:

Type argument 'b is also the type of the first argument given to user's defined printing functions for %a and %t conversions, and user's defined reading functions for %r conversion.

type ('a, 'b, 'c, 'd, 'e, 'f) format6 = ('a, 'b, 'c, 'd, 'e, 'f) CamlinternalFormatBasics.format6
type ('a, 'b, 'c, 'd) format4 = ('a, 'b, 'c, 'c, 'c, 'd) format6
type ('a, 'b, 'c) format = ('a, 'b, 'c, 'c) format4
val string_of_format : ('a, 'b, 'c, 'd, 'e, 'f) format6 -> string

Converts a format string into a string.

val format_of_string : ('a, 'b, 'c, 'd, 'e, 'f) format6 -> ('a, 'b, 'c, 'd, 'e, 'f) format6

format_of_string s returns a format string read from the string literal s. Note: format_of_string can not convert a string argument that is not a literal. If you need this functionality, use the more general Scanf.format_from_string function.

val (^^) : ('a, 'b, 'c, 'd, 'e, 'f) format6 -> ('f, 'b, 'c, 'e, 'g, 'h) format6 -> ('a, 'b, 'c, 'd, 'g, 'h) format6

f1 ^^ f2 catenates format strings f1 and f2. The result is a format string that behaves as the concatenation of format strings f1 and f2: in case of formatted output, it accepts arguments from f1, then arguments from f2; in case of formatted input, it returns results from f1, then results from f2. Right-associative operator, see Ocaml_operators for more information.