ligo/src/environment/v1/pervasives.mli

602 lines
22 KiB
OCaml
Raw Normal View History

2016-09-08 21:13:10 +04:00
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(* TEZOS CHANGES
2017-03-17 17:39:31 +04:00
* Import version 4.04.0
* Remove [channel], [exit], ...
* Remove polymorphic comparisons
* Remove non IEEE754-standardized functions on floats
* Remove deprecated functions
2016-09-08 21:13:10 +04:00
*)
(** The initially opened module.
This module provides the basic operations over the built-in types
(numbers, booleans, byte sequences, strings, exceptions, references,
lists, arrays, input-output channels, ...).
2016-09-08 21:13:10 +04:00
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 [Pervasives].
2016-09-08 21:13:10 +04:00
*)
(** {6 Exceptions} *)
external raise : exn -> 'a = "%raise"
(** Raise the given exception value *)
2017-03-17 17:39:31 +04:00
external raise_notrace : exn -> 'a = "%raise_notrace"
2016-09-08 21:13:10 +04:00
(** 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. *)
2017-03-17 17:39:31 +04:00
val failwith : string -> 'a
2016-09-08 21:13:10 +04:00
(** 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. *)
(** {6 Boolean operations} *)
external not : bool -> bool = "%boolnot"
(** The boolean negation. *)
external ( && ) : bool -> bool -> bool = "%sequand"
(** 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. *)
2016-09-08 21:13:10 +04:00
external ( || ) : bool -> bool -> bool = "%sequor"
(** 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. *)
2016-09-08 21:13:10 +04:00
(** {6 Debugging} *)
external __LOC__ : string = "%loc_LOC"
(** [__LOC__] returns the location at which this expression appears in
the file currently being parsed by the compiler, with the standard
2017-03-17 17:39:31 +04:00
error format of OCaml: "File %S, line %d, characters %d-%d".
@since 4.02.0
*)
2016-09-08 21:13:10 +04:00
external __FILE__ : string = "%loc_FILE"
(** [__FILE__] returns the name of the file currently being
2017-03-17 17:39:31 +04:00
parsed by the compiler.
@since 4.02.0
*)
2016-09-08 21:13:10 +04:00
external __LINE__ : int = "%loc_LINE"
(** [__LINE__] returns the line number at which this expression
2017-03-17 17:39:31 +04:00
appears in the file currently being parsed by the compiler.
@since 4.02.0
*)
2016-09-08 21:13:10 +04:00
external __MODULE__ : string = "%loc_MODULE"
(** [__MODULE__] returns the module name of the file being
2017-03-17 17:39:31 +04:00
parsed by the compiler.
@since 4.02.0
*)
2016-09-08 21:13:10 +04:00
external __POS__ : string * int * int * int = "%loc_POS"
(** [__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
2017-03-17 17:39:31 +04:00
the line and [enum] the last character position in the line.
@since 4.02.0
*)
2016-09-08 21:13:10 +04:00
external __LOC_OF__ : 'a -> string * 'a = "%loc_LOC"
(** [__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
2017-03-17 17:39:31 +04:00
%d, characters %d-%d".
@since 4.02.0
*)
2016-09-08 21:13:10 +04:00
external __LINE_OF__ : 'a -> int * 'a = "%loc_LINE"
(** [__LINE__ expr] returns a pair [(line, expr)], where [line] is the
line number at which the expression [expr] appears in the file
2017-03-17 17:39:31 +04:00
currently being parsed by the compiler.
@since 4.02.0
*)
2017-03-17 17:39:31 +04:00
2016-09-08 21:13:10 +04:00
external __POS_OF__ : 'a -> (string * int * int * int) * 'a = "%loc_POS"
2017-03-17 17:39:31 +04:00
(** [__POS_OF__ expr] returns a pair [(loc,expr)], where [loc] is a
2016-09-08 21:13:10 +04:00
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]
2017-03-17 17:39:31 +04:00
the last character position in the line.
@since 4.02.0
*)
2016-09-08 21:13:10 +04:00
(** {6 Composition operators} *)
external ( |> ) : 'a -> ('a -> 'b) -> 'b = "%revapply"
(** Reverse-application operator: [x |> f |> g] is exactly equivalent
to [g (f (x))].
@since 4.01
2016-09-08 21:13:10 +04:00
*)
external ( @@ ) : ('a -> 'b) -> 'a -> 'b = "%apply"
(** Application operator: [g @@ f @@ x] is exactly equivalent to
[g (f (x))].
@since 4.01
2016-09-08 21:13:10 +04:00
*)
(** {6 Integer arithmetic} *)
(** Integers are 31 bits wide (or 63 bits on 64-bit processors).
All operations are taken modulo 2{^31} (or 2{^63}).
They do not fail on overflow. *)
2016-09-08 21:13:10 +04:00
external ( ~- ) : int -> int = "%negint"
(** Unary negation. You can also write [- e] instead of [~- e]. *)
external ( ~+ ) : int -> int = "%identity"
(** Unary addition. You can also write [+ e] instead of [~+ e].
@since 3.12.0
*)
external succ : int -> int = "%succint"
(** [succ x] is [x + 1]. *)
external pred : int -> int = "%predint"
(** [pred x] is [x - 1]. *)
external ( + ) : int -> int -> int = "%addint"
(** Integer addition. *)
external ( - ) : int -> int -> int = "%subint"
(** Integer subtraction. *)
external ( * ) : int -> int -> int = "%mulint"
(** Integer multiplication. *)
external ( / ) : int -> int -> int = "%divint"
(** 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)]. *)
2016-09-08 21:13:10 +04:00
external ( mod ) : int -> int -> int = "%modint"
(** 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. *)
2016-09-08 21:13:10 +04:00
val abs : int -> int
(** Return the absolute value of the argument. Note that this may be
negative if the argument is [min_int]. *)
2016-09-08 21:13:10 +04:00
val max_int : int
(** The greatest representable integer. *)
val min_int : int
(** The smallest representable integer. *)
(** {7 Bitwise operations} *)
external ( land ) : int -> int -> int = "%andint"
(** Bitwise logical and. *)
external ( lor ) : int -> int -> int = "%orint"
(** Bitwise logical or. *)
external ( lxor ) : int -> int -> int = "%xorint"
(** Bitwise logical exclusive or. *)
val lnot : int -> int
(** Bitwise logical negation. *)
external ( lsl ) : int -> int -> int = "%lslint"
(** [n lsl m] shifts [n] to the left by [m] bits.
The result is unspecified if [m < 0] or [m >= bitsize],
where [bitsize] is [32] on a 32-bit platform and
[64] on a 64-bit platform. *)
2016-09-08 21:13:10 +04:00
external ( lsr ) : int -> int -> int = "%lsrint"
(** [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 >= bitsize]. *)
2016-09-08 21:13:10 +04:00
external ( asr ) : int -> int -> int = "%asrint"
(** [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 >= bitsize]. *)
2016-09-08 21:13:10 +04:00
(** {6 Floating-point arithmetic}
OCaml's floating-point numbers follow the
IEEE 754 standard, using double precision (64 bits) numbers.
Floating-point operations never raise an exception on overflow,
underflow, division by zero, etc. Instead, special IEEE numbers
are returned as appropriate, such as [infinity] for [1.0 /. 0.0],
[neg_infinity] for [-1.0 /. 0.0], and [nan] ('not a number')
for [0.0 /. 0.0]. These special numbers then propagate through
floating-point computations as expected: for instance,
[1.0 /. infinity] is [0.0], and any arithmetic operation with [nan]
as argument returns [nan] as result.
2016-09-08 21:13:10 +04:00
*)
external ( ~-. ) : float -> float = "%negfloat"
(** Unary negation. You can also write [-. e] instead of [~-. e]. *)
external ( ~+. ) : float -> float = "%identity"
(** Unary addition. You can also write [+. e] instead of [~+. e].
@since 3.12.0
*)
external ( +. ) : float -> float -> float = "%addfloat"
(** Floating-point addition *)
external ( -. ) : float -> float -> float = "%subfloat"
(** Floating-point subtraction *)
external ( *. ) : float -> float -> float = "%mulfloat"
(** Floating-point multiplication *)
external ( /. ) : float -> float -> float = "%divfloat"
(** Floating-point division. *)
external ceil : float -> float = "caml_ceil_float" "ceil"
[@@unboxed] [@@noalloc]
2016-09-08 21:13:10 +04:00
(** Round above to an integer value.
[ceil f] returns the least integer value greater than or equal to [f].
The result is returned as a float. *)
external floor : float -> float = "caml_floor_float" "floor"
[@@unboxed] [@@noalloc]
2016-09-08 21:13:10 +04:00
(** Round below to an integer value.
[floor f] returns the greatest integer value less than or
equal to [f].
The result is returned as a float. *)
external abs_float : float -> float = "%absfloat"
(** [abs_float f] returns the absolute value of [f]. *)
external copysign : float -> float -> float
= "caml_copysign_float" "caml_copysign"
[@@unboxed] [@@noalloc]
2016-09-08 21:13:10 +04:00
(** [copysign x y] returns a float whose absolute value is that of [x]
and whose sign is that of [y]. If [x] is [nan], returns [nan].
If [y] is [nan], returns either [x] or [-. x], but it is not
specified which.
@since 4.00.0 *)
2016-09-08 21:13:10 +04:00
external mod_float : float -> float -> float = "caml_fmod_float" "fmod"
[@@unboxed] [@@noalloc]
2016-09-08 21:13:10 +04:00
(** [mod_float a b] returns the remainder of [a] with respect to
[b]. The returned value is [a -. n *. b], where [n]
is the quotient [a /. b] rounded towards zero to an integer. *)
2016-09-08 21:13:10 +04:00
external frexp : float -> float * int = "caml_frexp_float"
(** [frexp f] returns the pair of the significant
and the exponent of [f]. When [f] is zero, the
significant [x] and the exponent [n] of [f] are equal to
zero. When [f] is non-zero, they are defined by
[f = x *. 2 ** n] and [0.5 <= x < 1.0]. *)
2016-09-08 21:13:10 +04:00
external ldexp : (float [@unboxed]) -> (int [@untagged]) -> (float [@unboxed]) =
"caml_ldexp_float" "caml_ldexp_float_unboxed" [@@noalloc]
(** [ldexp x n] returns [x *. 2 ** n]. *)
external modf : float -> float * float = "caml_modf_float"
(** [modf f] returns the pair of the fractional and integral
part of [f]. *)
2016-09-08 21:13:10 +04:00
external float : int -> float = "%floatofint"
(** Same as {!Pervasives.float_of_int}. *)
external float_of_int : int -> float = "%floatofint"
(** Convert an integer to floating-point. *)
external truncate : float -> int = "%intoffloat"
(** Same as {!Pervasives.int_of_float}. *)
external int_of_float : float -> int = "%intoffloat"
(** Truncate the given floating-point number to an integer.
The result is unspecified if the argument is [nan] or falls outside the
range of representable integers. *)
2016-09-08 21:13:10 +04:00
val infinity : float
(** Positive infinity. *)
val neg_infinity : float
(** Negative infinity. *)
val nan : float
(** A special floating-point value denoting the result of an
undefined operation such as [0.0 /. 0.0]. Stands for
'not a number'. Any floating-point operation with [nan] as
argument returns [nan] as result. As for floating-point comparisons,
[=], [<], [<=], [>] and [>=] return [false] and [<>] returns [true]
if one or both of their arguments is [nan]. *)
2016-09-08 21:13:10 +04:00
val max_float : float
(** The largest positive finite value of type [float]. *)
val min_float : float
(** The smallest positive, non-zero, non-denormalized value of type [float]. *)
val epsilon_float : float
(** The difference between [1.0] and the smallest exactly representable
floating-point number greater than [1.0]. *)
type fpclass =
FP_normal (** Normal number, none of the below *)
| FP_subnormal (** Number very close to 0.0, has reduced precision *)
| FP_zero (** Number is 0.0 or -0.0 *)
| FP_infinite (** Number is positive or negative infinity *)
| FP_nan (** Not a number: result of an undefined operation *)
(** The five classes of floating-point numbers, as determined by
the {!Pervasives.classify_float} function. *)
2016-09-08 21:13:10 +04:00
external classify_float : (float [@unboxed]) -> fpclass =
"caml_classify_float" "caml_classify_float_unboxed" [@@noalloc]
(** Return the class of the given floating-point number:
normal, subnormal, zero, infinite, or not a number. *)
2016-09-08 21:13:10 +04:00
(** {6 String operations}
More string operations are provided in module {!String}.
2016-09-08 21:13:10 +04:00
*)
val ( ^ ) : string -> string -> string
(** String concatenation. *)
(** {6 Character operations}
More character operations are provided in module {!Char}.
2016-09-08 21:13:10 +04:00
*)
external int_of_char : char -> int = "%identity"
(** 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. *)
2016-09-08 21:13:10 +04:00
(** {6 Unit operations} *)
external ignore : 'a -> unit = "%ignore"
(** 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. *)
2016-09-08 21:13:10 +04:00
(** {6 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.
2016-09-08 21:13:10 +04:00
*)
val bool_of_string : string -> bool
(** Convert the given string to a boolean.
Raise [Invalid_argument "bool_of_string"] if the string is not
["true"] or ["false"]. *)
2016-09-08 21:13:10 +04:00
val string_of_int : int -> string
(** Return the string representation of an integer, in decimal. *)
external int_of_string : string -> int = "caml_int_of_string"
(** Convert the given string to an integer.
The string is read in decimal (by default), 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 [_] (underscore) character can appear anywhere in the string
and is ignored.
Raise [Failure "int_of_string"] 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]. *)
2016-09-08 21:13:10 +04:00
val string_of_float : float -> string
(** Return the string representation of a floating-point number. *)
external float_of_string : string -> float = "caml_float_of_string"
(** Convert the given string to a float. The string is read in decimal
(by default) or in hexadecimal (marked by [0x] or [0X]).
The format of decimal floating-point numbers is
[ [-] dd.ddd (e|E) [+|-] dd ], where [d] stands for a decimal digit.
The format of hexadecimal floating-point numbers is
[ [-] 0(x|X) hh.hhh (p|P) [+|-] dd ], where [h] stands for an
hexadecimal digit and [d] for a decimal digit.
In both cases, at least one of the integer and fractional parts must be
given; the exponent part is optional.
The [_] (underscore) character can appear anywhere in the string
and is ignored.
Depending on the execution platforms, other representations of
floating-point numbers can be accepted, but should not be relied upon.
Raise [Failure "float_of_string"] if the given string is not a valid
representation of a float. *)
2016-09-08 21:13:10 +04:00
(** {6 Pair operations} *)
external fst : 'a * 'b -> 'a = "%field0"
(** Return the first component of a pair. *)
external snd : 'a * 'b -> 'b = "%field1"
(** Return the second component of a pair. *)
(** {6 List operations}
More list operations are provided in module {!List}.
2016-09-08 21:13:10 +04:00
*)
val ( @ ) : 'a list -> 'a list -> 'a list
(** List concatenation. Not tail-recursive (length of the first argument). *)
(** {6 References} *)
type 'a ref = { mutable contents : 'a }
(** The type of references (mutable indirection cells) containing
a value of type ['a]. *)
2016-09-08 21:13:10 +04:00
external ref : 'a -> 'a ref = "%makemutable"
(** Return a fresh reference containing the given value. *)
external ( ! ) : 'a ref -> 'a = "%field0"
(** [!r] returns the current contents of reference [r].
Equivalent to [fun r -> r.contents]. *)
2016-09-08 21:13:10 +04:00
external ( := ) : 'a ref -> 'a -> unit = "%setfield0"
(** [r := a] stores the value of [a] in reference [r].
Equivalent to [fun r v -> r.contents <- v]. *)
2016-09-08 21:13:10 +04:00
external incr : int ref -> unit = "%incr"
(** Increment the integer contained in the given reference.
Equivalent to [fun r -> r := succ !r]. *)
2016-09-08 21:13:10 +04:00
external decr : int ref -> unit = "%decr"
(** Decrement the integer contained in the given reference.
Equivalent to [fun r -> r := pred !r]. *)
2016-09-08 21:13:10 +04:00
(** {6 Result type} *)
type ('a,'b) result = Ok of 'a | Error of 'b
(** {6 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}.
2016-09-08 21:13:10 +04:00
Format strings are made of three kinds of entities:
- {e conversions specifications}, introduced by the special character ['%']
2016-09-08 21:13:10 +04:00
followed by one or more characters specifying what kind of argument to
read or print,
- {e formatting indications}, introduced by the special character ['@']
2016-09-08 21:13:10 +04:00
followed by one or more characters specifying how to read or print the
argument,
- {e plain characters} that are regular characters with usual lexical
2016-09-08 21:13:10 +04:00
conventions. Plain characters specify string literals to be read in the
input or printed in the output.
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 ['@'].
2016-09-08 21:13:10 +04:00
For more information about conversion specifications and formatting
indications available, read the documentation of modules {!Scanf},
{!Printf} and {!Format}.
2016-09-08 21:13:10 +04:00
*)
(** 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:
- ['a] is the type of the parameters of the format for formatted output
functions ([printf]-style functions);
['a] is the type of the values read by the format for formatted input
functions ([scanf]-style functions).
- ['b] is the type of input source for formatted input functions and the
type of output target for formatted output functions.
For [printf]-style functions from module [Printf], ['b] is typically
[out_channel];
for [printf]-style functions from module [Format], ['b] is typically
[Format.formatter];
for [scanf]-style functions from module [Scanf], ['b] is typically
[Scanf.Scanning.in_channel].
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.
- ['c] is the type of the result of the [%a] and [%t] printing
functions, and also the type of the argument transmitted to the
first argument of [kprintf]-style functions or to the
[kscanf]-style functions.
- ['d] is the type of parameters for the [scanf]-style functions.
- ['e] is the type of the receiver function for the [scanf]-style functions.
- ['f] is the final result type of a formatted input/output function
invocation: for the [printf]-style functions, it is typically [unit];
for the [scanf]-style functions, it is typically the result type of the
receiver function.
*)
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
2017-03-17 17:39:31 +04:00
val string_of_format : ('a, 'b, 'c, 'd, 'e, 'f) format6 -> string
(** Converts a format string into a string. *)
external format_of_string :
('a, 'b, 'c, 'd, 'e, 'f) format6 ->
('a, 'b, 'c, 'd, 'e, 'f) format6 = "%identity"
(** [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.
*)
2016-09-08 21:13:10 +04:00
2017-03-17 17:39:31 +04:00
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].
2017-03-17 17:39:31 +04:00
*)
2016-09-08 21:13:10 +04:00