1Pervasives(3) OCamldoc Pervasives(3)
2
3
4
6 Pervasives - The initially opened module.
7
9 Module Pervasives
10
12 Module Pervasives
13 : sig end
14
15
16 The initially opened module.
17
18 This module provides the basic operations over the built-in types (num‐
19 bers, booleans, byte sequences, strings, exceptions, references, lists,
20 arrays, input-output channels, ...).
21
22 This module is automatically opened at the beginning of each compila‐
23 tion. All components of this module can therefore be referred by their
24 short name, without prefixing them by Pervasives .
25
26
27
28
29
30
31
32 === Exceptions ===
33
34
35 val raise : exn -> 'a
36
37 Raise the given exception value
38
39
40
41 val raise_notrace : exn -> 'a
42
43 A faster version raise which does not record the backtrace.
44
45
46 Since 4.02.0
47
48
49
50 val invalid_arg : string -> 'a
51
52 Raise exception Invalid_argument with the given string.
53
54
55
56 val failwith : string -> 'a
57
58 Raise exception Failure with the given string.
59
60
61
62 exception Exit
63
64
65 The Exit exception is not raised by any library function. It is pro‐
66 vided for use in your programs.
67
68
69
70
71 === Comparisons ===
72
73
74 val (=) : 'a -> 'a -> bool
75
76
77 e1 = e2 tests for structural equality of e1 and e2 . Mutable struc‐
78 tures (e.g. references and arrays) are equal if and only if their cur‐
79 rent contents are structurally equal, even if the two mutable objects
80 are not the same physical object. Equality between functional values
81 raises Invalid_argument . Equality between cyclic data structures may
82 not terminate.
83
84
85
86 val (<>) : 'a -> 'a -> bool
87
88 Negation of Pervasives.(=) .
89
90
91
92 val (<) : 'a -> 'a -> bool
93
94 See Pervasives.(>=) .
95
96
97
98 val (>) : 'a -> 'a -> bool
99
100 See Pervasives.(>=) .
101
102
103
104 val (<=) : 'a -> 'a -> bool
105
106 See Pervasives.(>=) .
107
108
109
110 val (>=) : 'a -> 'a -> bool
111
112 Structural ordering functions. These functions coincide with the usual
113 orderings over integers, characters, strings, byte sequences and float‐
114 ing-point numbers, and extend them to a total ordering over all types.
115 The ordering is compatible with ( = ) . As in the case of ( = ) , muta‐
116 ble structures are compared by contents. Comparison between functional
117 values raises Invalid_argument . Comparison between cyclic structures
118 may not terminate.
119
120
121
122 val compare : 'a -> 'a -> int
123
124
125 compare x y returns 0 if x is equal to y , a negative integer if x is
126 less than y , and a positive integer if x is greater than y . The
127 ordering implemented by compare is compatible with the comparison pred‐
128 icates = , < and > defined above, with one difference on the treatment
129 of the float value Pervasives.nan . Namely, the comparison predicates
130 treat nan as different from any other float value, including itself;
131 while compare treats nan as equal to itself and less than any other
132 float value. This treatment of nan ensures that compare defines a
133 total ordering relation.
134
135
136 compare applied to functional values may raise Invalid_argument . com‐
137 pare applied to cyclic structures may not terminate.
138
139 The compare function can be used as the comparison function required by
140 the Set.Make and Map.Make functors, as well as the List.sort and
141 Array.sort functions.
142
143
144
145 val min : 'a -> 'a -> 'a
146
147 Return the smaller of the two arguments. The result is unspecified if
148 one of the arguments contains the float value nan .
149
150
151
152 val max : 'a -> 'a -> 'a
153
154 Return the greater of the two arguments. The result is unspecified if
155 one of the arguments contains the float value nan .
156
157
158
159 val (==) : 'a -> 'a -> bool
160
161
162 e1 == e2 tests for physical equality of e1 and e2 . On mutable types
163 such as references, arrays, byte sequences, records with mutable fields
164 and objects with mutable instance variables, e1 == e2 is true if and
165 only if physical modification of e1 also affects e2 . On non-mutable
166 types, the behavior of ( == ) is implementation-dependent; however, it
167 is guaranteed that e1 == e2 implies compare e1 e2 = 0 .
168
169
170
171 val (!=) : 'a -> 'a -> bool
172
173 Negation of Pervasives.(==) .
174
175
176
177
178 === Boolean operations ===
179
180
181 val not : bool -> bool
182
183 The boolean negation.
184
185
186
187 val (&&) : bool -> bool -> bool
188
189 The boolean 'and'. Evaluation is sequential, left-to-right: in e1 && e2
190 , e1 is evaluated first, and if it returns false , e2 is not evaluated
191 at all.
192
193
194
195 val (&) : bool -> bool -> bool
196
197 Deprecated.
198
199 Pervasives.(&&) should be used instead.
200
201
202
203 val (||) : bool -> bool -> bool
204
205 The boolean 'or'. Evaluation is sequential, left-to-right: in e1 || e2
206 , e1 is evaluated first, and if it returns true , e2 is not evaluated
207 at all.
208
209
210
211 val (or) : bool -> bool -> bool
212
213 Deprecated.
214
215 Pervasives.(||) should be used instead.
216
217
218
219
220 === Debugging ===
221
222
223 val __LOC__ : string
224
225
226 __LOC__ returns the location at which this expression appears in the
227 file currently being parsed by the compiler, with the standard error
228 format of OCaml: "File %S, line %d, characters %d-%d".
229
230
231 Since 4.02.0
232
233
234
235 val __FILE__ : string
236
237
238 __FILE__ returns the name of the file currently being parsed by the
239 compiler.
240
241
242 Since 4.02.0
243
244
245
246 val __LINE__ : int
247
248
249 __LINE__ returns the line number at which this expression appears in
250 the file currently being parsed by the compiler.
251
252
253 Since 4.02.0
254
255
256
257 val __MODULE__ : string
258
259
260 __MODULE__ returns the module name of the file being parsed by the com‐
261 piler.
262
263
264 Since 4.02.0
265
266
267
268 val __POS__ : string * int * int * int
269
270
271 __POS__ returns a tuple (file,lnum,cnum,enum) , corresponding to the
272 location at which this expression appears in the file currently being
273 parsed by the compiler. file is the current filename, lnum the line
274 number, cnum the character position in the line and enum the last char‐
275 acter position in the line.
276
277
278 Since 4.02.0
279
280
281
282 val __LOC_OF__ : 'a -> string * 'a
283
284
285 __LOC_OF__ expr returns a pair (loc, expr) where loc is the location of
286 expr in the file currently being parsed by the compiler, with the stan‐
287 dard error format of OCaml: "File %S, line %d, characters %d-%d".
288
289
290 Since 4.02.0
291
292
293
294 val __LINE_OF__ : 'a -> int * 'a
295
296
297 __LINE__ expr returns a pair (line, expr) , where line is the line num‐
298 ber at which the expression expr appears in the file currently being
299 parsed by the compiler.
300
301
302 Since 4.02.0
303
304
305
306 val __POS_OF__ : 'a -> (string * int * int * int) * 'a
307
308
309 __POS_OF__ expr returns a pair (loc,expr) , where loc is a tuple
310 (file,lnum,cnum,enum) corresponding to the location at which the
311 expression expr appears in the file currently being parsed by the com‐
312 piler. file is the current filename, lnum the line number, cnum the
313 character position in the line and enum the last character position in
314 the line.
315
316
317 Since 4.02.0
318
319
320
321
322 === Composition operators ===
323
324
325 val (|>) : 'a -> ('a -> 'b) -> 'b
326
327 Reverse-application operator: x |> f |> g is exactly equivalent to g (f
328 (x)) .
329
330
331 Since 4.01
332
333
334
335 val (@@) : ('a -> 'b) -> 'a -> 'b
336
337 Application operator: g @@ f @@ x is exactly equivalent to g (f (x)) .
338
339
340 Since 4.01
341
342
343
344
345 === Integer arithmetic ===
346
347
348 === Integers are 31 bits wide (or 63 bits on 64-bit processors). All
349 operations are taken modulo 2^{31} (or 2^{63}). They do not fail on
350 overflow. ===
351
352
353 val (~-) : int -> int
354
355 Unary negation. You can also write - e instead of ~- e .
356
357
358
359 val (~+) : int -> int
360
361 Unary addition. You can also write + e instead of ~+ e .
362
363
364 Since 3.12.0
365
366
367
368 val succ : int -> int
369
370
371 succ x is x + 1 .
372
373
374
375 val pred : int -> int
376
377
378 pred x is x - 1 .
379
380
381
382 val (+) : int -> int -> int
383
384 Integer addition.
385
386
387
388 val (-) : int -> int -> int
389
390 Integer subtraction.
391
392
393
394 val ( * ) : int -> int -> int
395
396 Integer multiplication.
397
398
399
400 val (/) : int -> int -> int
401
402 Integer division. Raise Division_by_zero if the second argument is 0.
403 Integer division rounds the real quotient of its arguments towards
404 zero. More precisely, if x >= 0 and y > 0 , x / y is the greatest
405 integer less than or equal to the real quotient of x by y . Moreover,
406 (- x) / y = x / (- y) = - (x / y) .
407
408
409
410 val (mod) : int -> int -> int
411
412 Integer remainder. If y is not zero, the result of x mod y satisfies
413 the following properties: x = (x / y) * y + x mod y and abs(x mod y) <=
414 abs(y) - 1 . If y = 0 , x mod y raises Division_by_zero . Note that x
415 mod y is negative only if x < 0 . Raise Division_by_zero if y is zero.
416
417
418
419 val abs : int -> int
420
421 Return the absolute value of the argument. Note that this may be nega‐
422 tive if the argument is min_int .
423
424
425
426 val max_int : int
427
428 The greatest representable integer.
429
430
431
432 val min_int : int
433
434 The smallest representable integer.
435
436
437
438
439 === Bitwise operations ===
440
441
442 val (land) : int -> int -> int
443
444 Bitwise logical and.
445
446
447
448 val (lor) : int -> int -> int
449
450 Bitwise logical or.
451
452
453
454 val (lxor) : int -> int -> int
455
456 Bitwise logical exclusive or.
457
458
459
460 val lnot : int -> int
461
462 Bitwise logical negation.
463
464
465
466 val (lsl) : int -> int -> int
467
468
469 n lsl m shifts n to the left by m bits. The result is unspecified if m
470 < 0 or m >= bitsize , where bitsize is 32 on a 32-bit platform and 64
471 on a 64-bit platform.
472
473
474
475 val (lsr) : int -> int -> int
476
477
478 n lsr m shifts n to the right by m bits. This is a logical shift:
479 zeroes are inserted regardless of the sign of n . The result is
480 unspecified if m < 0 or m >= bitsize .
481
482
483
484 val (asr) : int -> int -> int
485
486
487 n asr m shifts n to the right by m bits. This is an arithmetic shift:
488 the sign bit of n is replicated. The result is unspecified if m < 0 or
489 m >= bitsize .
490
491
492
493
494 === Floating-point arithmetic OCaml's floating-point numbers follow the
495 IEEE 754 standard, using double precision (64 bits) numbers. Float‐
496 ing-point operations never raise an exception on overflow, underflow,
497 division by zero, etc. Instead, special IEEE numbers are returned as
498 appropriate, such as infinity for 1.0 /. 0.0, neg_infinity for -1.0 /.
499 0.0, and nan ('not a number') for 0.0 /. 0.0. These special numbers
500 then propagate through floating-point computations as expected: for
501 instance, 1.0 /. infinity is 0.0, and any arithmetic operation with nan
502 as argument returns nan as result. ===
503
504
505 val (~-.) : float -> float
506
507 Unary negation. You can also write -. e instead of ~-. e .
508
509
510
511 val (~+.) : float -> float
512
513 Unary addition. You can also write +. e instead of ~+. e .
514
515
516 Since 3.12.0
517
518
519
520 val (+.) : float -> float -> float
521
522 Floating-point addition
523
524
525
526 val (-.) : float -> float -> float
527
528 Floating-point subtraction
529
530
531
532 val ( *. ) : float -> float -> float
533
534 Floating-point multiplication
535
536
537
538 val (/.) : float -> float -> float
539
540 Floating-point division.
541
542
543
544 val ( ** ) : float -> float -> float
545
546 Exponentiation.
547
548
549
550 val sqrt : float -> float
551
552 Square root.
553
554
555
556 val exp : float -> float
557
558 Exponential.
559
560
561
562 val log : float -> float
563
564 Natural logarithm.
565
566
567
568 val log10 : float -> float
569
570 Base 10 logarithm.
571
572
573
574 val expm1 : float -> float
575
576
577 expm1 x computes exp x -. 1.0 , giving numerically-accurate results
578 even if x is close to 0.0 .
579
580
581 Since 3.12.0
582
583
584
585 val log1p : float -> float
586
587
588 log1p x computes log(1.0 +. x) (natural logarithm), giving numeri‐
589 cally-accurate results even if x is close to 0.0 .
590
591
592 Since 3.12.0
593
594
595
596 val cos : float -> float
597
598 Cosine. Argument is in radians.
599
600
601
602 val sin : float -> float
603
604 Sine. Argument is in radians.
605
606
607
608 val tan : float -> float
609
610 Tangent. Argument is in radians.
611
612
613
614 val acos : float -> float
615
616 Arc cosine. The argument must fall within the range [-1.0, 1.0] .
617 Result is in radians and is between 0.0 and pi .
618
619
620
621 val asin : float -> float
622
623 Arc sine. The argument must fall within the range [-1.0, 1.0] .
624 Result is in radians and is between -pi/2 and pi/2 .
625
626
627
628 val atan : float -> float
629
630 Arc tangent. Result is in radians and is between -pi/2 and pi/2 .
631
632
633
634 val atan2 : float -> float -> float
635
636
637 atan2 y x returns the arc tangent of y /. x . The signs of x and y are
638 used to determine the quadrant of the result. Result is in radians and
639 is between -pi and pi .
640
641
642
643 val hypot : float -> float -> float
644
645
646 hypot x y returns sqrt(x *. x + y *. y) , that is, the length of the
647 hypotenuse of a right-angled triangle with sides of length x and y ,
648 or, equivalently, the distance of the point (x,y) to origin. If one of
649 x or y is infinite, returns infinity even if the other is nan .
650
651
652 Since 4.00.0
653
654
655
656 val cosh : float -> float
657
658 Hyperbolic cosine. Argument is in radians.
659
660
661
662 val sinh : float -> float
663
664 Hyperbolic sine. Argument is in radians.
665
666
667
668 val tanh : float -> float
669
670 Hyperbolic tangent. Argument is in radians.
671
672
673
674 val ceil : float -> float
675
676 Round above to an integer value. ceil f returns the least integer
677 value greater than or equal to f . The result is returned as a float.
678
679
680
681 val floor : float -> float
682
683 Round below to an integer value. floor f returns the greatest integer
684 value less than or equal to f . The result is returned as a float.
685
686
687
688 val abs_float : float -> float
689
690
691 abs_float f returns the absolute value of f .
692
693
694
695 val copysign : float -> float -> float
696
697
698 copysign x y returns a float whose absolute value is that of x and
699 whose sign is that of y . If x is nan , returns nan . If y is nan ,
700 returns either x or -. x , but it is not specified which.
701
702
703 Since 4.00.0
704
705
706
707 val mod_float : float -> float -> float
708
709
710 mod_float a b returns the remainder of a with respect to b . The
711 returned value is a -. n *. b , where n is the quotient a /. b rounded
712 towards zero to an integer.
713
714
715
716 val frexp : float -> float * int
717
718
719 frexp f returns the pair of the significant and the exponent of f .
720 When f is zero, the significant x and the exponent n of f are equal to
721 zero. When f is non-zero, they are defined by f = x *. 2 ** n and 0.5
722 <= x < 1.0 .
723
724
725
726 val ldexp : float -> int -> float
727
728
729 ldexp x n returns x *. 2 ** n .
730
731
732
733 val modf : float -> float * float
734
735
736 modf f returns the pair of the fractional and integral part of f .
737
738
739
740 val float : int -> float
741
742 Same as Pervasives.float_of_int .
743
744
745
746 val float_of_int : int -> float
747
748 Convert an integer to floating-point.
749
750
751
752 val truncate : float -> int
753
754 Same as Pervasives.int_of_float .
755
756
757
758 val int_of_float : float -> int
759
760 Truncate the given floating-point number to an integer. The result is
761 unspecified if the argument is nan or falls outside the range of repre‐
762 sentable integers.
763
764
765
766 val infinity : float
767
768 Positive infinity.
769
770
771
772 val neg_infinity : float
773
774 Negative infinity.
775
776
777
778 val nan : float
779
780 A special floating-point value denoting the result of an undefined
781 operation such as 0.0 /. 0.0 . Stands for 'not a number'. Any float‐
782 ing-point operation with nan as argument returns nan as result. As for
783 floating-point comparisons, = , < , <= , > and >= return false and <>
784 returns true if one or both of their arguments is nan .
785
786
787
788 val max_float : float
789
790 The largest positive finite value of type float .
791
792
793
794 val min_float : float
795
796 The smallest positive, non-zero, non-denormalized value of type float .
797
798
799
800 val epsilon_float : float
801
802 The difference between 1.0 and the smallest exactly representable
803 floating-point number greater than 1.0 .
804
805
806 type fpclass =
807 | FP_normal (* Normal number, none of the below
808 *)
809 | FP_subnormal (* Number very close to 0.0, has reduced precision
810 *)
811 | FP_zero (* Number is 0.0 or -0.0
812 *)
813 | FP_infinite (* Number is positive or negative infinity
814 *)
815 | FP_nan (* Not a number: result of an undefined operation
816 *)
817
818
819 The five classes of floating-point numbers, as determined by the Perva‐
820 sives.classify_float function.
821
822
823
824 val classify_float : float -> fpclass
825
826 Return the class of the given floating-point number: normal, subnormal,
827 zero, infinite, or not a number.
828
829
830
831
832 === String operations More string operations are provided in module
833 String. ===
834
835
836 val (^) : string -> string -> string
837
838 String concatenation.
839
840
841
842
843 === Character operations More character operations are provided in mod‐
844 ule Char. ===
845
846
847 val int_of_char : char -> int
848
849 Return the ASCII code of the argument.
850
851
852
853 val char_of_int : int -> char
854
855 Return the character with the given ASCII code. Raise Invalid_argument
856 char_of_int if the argument is outside the range 0--255.
857
858
859
860
861 === Unit operations ===
862
863
864 val ignore : 'a -> unit
865
866 Discard the value of its argument and return () . For instance,
867 ignore(f x) discards the result of the side-effecting function f . It
868 is equivalent to f x; () , except that the latter may generate a com‐
869 piler warning; writing ignore(f x) instead avoids the warning.
870
871
872
873
874 === String conversion functions ===
875
876
877 val string_of_bool : bool -> string
878
879 Return the string representation of a boolean. As the returned values
880 may be shared, the user should not modify them directly.
881
882
883
884 val bool_of_string : string -> bool
885
886 Convert the given string to a boolean. Raise Invalid_argument
887 bool_of_string if the string is not true or false .
888
889
890
891 val bool_of_string_opt : string -> bool option
892
893 Convert the given string to a boolean. Return None if the string is
894 not true or false .
895
896
897 Since 4.05
898
899
900
901 val string_of_int : int -> string
902
903 Return the string representation of an integer, in decimal.
904
905
906
907 val int_of_string : string -> int
908
909 Convert the given string to an integer. The string is read in decimal
910 (by default), in hexadecimal (if it begins with 0x or 0X ), in octal
911 (if it begins with 0o or 0O ), or in binary (if it begins with 0b or 0B
912 ). The _ (underscore) character can appear anywhere in the string and
913 is ignored. Raise Failure int_of_string if the given string is not a
914 valid representation of an integer, or if the integer represented
915 exceeds the range of integers representable in type int .
916
917
918
919 val int_of_string_opt : string -> int option
920
921 Same as int_of_string , but returs None instead of raising.
922
923
924 Since 4.05
925
926
927
928 val string_of_float : float -> string
929
930 Return the string representation of a floating-point number.
931
932
933
934 val float_of_string : string -> float
935
936 Convert the given string to a float. The string is read in decimal (by
937 default) or in hexadecimal (marked by 0x or 0X ). The format of deci‐
938 mal floating-point numbers is [-] dd.ddd (e|E) [+|-] dd , where d
939 stands for a decimal digit. The format of hexadecimal floating-point
940 numbers is [-] 0(x|X) hh.hhh (p|P) [+|-] dd , where h stands for an
941 hexadecimal digit and d for a decimal digit. In both cases, at least
942 one of the integer and fractional parts must be given; the exponent
943 part is optional. The _ (underscore) character can appear anywhere in
944 the string and is ignored. Depending on the execution platforms, other
945 representations of floating-point numbers can be accepted, but should
946 not be relied upon. Raise Failure float_of_string if the given string
947 is not a valid representation of a float.
948
949
950
951 val float_of_string_opt : string -> float option
952
953 Same as float_of_string , but returns None instead of raising.
954
955
956 Since 4.05
957
958
959
960
961 === Pair operations ===
962
963
964 val fst : 'a * 'b -> 'a
965
966 Return the first component of a pair.
967
968
969
970 val snd : 'a * 'b -> 'b
971
972 Return the second component of a pair.
973
974
975
976
977 === List operations More list operations are provided in module List.
978 ===
979
980
981 val (@) : 'a list -> 'a list -> 'a list
982
983 List concatenation. Not tail-recursive (length of the first argument).
984
985
986
987
988 === Input/output Note: all input/output functions can raise Sys_error
989 when the system calls they invoke fail. ===
990
991
992 type in_channel
993
994
995 The type of input channel.
996
997
998 type out_channel
999
1000
1001 The type of output channel.
1002
1003
1004
1005 val stdin : in_channel
1006
1007 The standard input for the process.
1008
1009
1010
1011 val stdout : out_channel
1012
1013 The standard output for the process.
1014
1015
1016
1017 val stderr : out_channel
1018
1019 The standard error output for the process.
1020
1021
1022
1023
1024 === Output functions on standard output ===
1025
1026
1027 val print_char : char -> unit
1028
1029 Print a character on standard output.
1030
1031
1032
1033 val print_string : string -> unit
1034
1035 Print a string on standard output.
1036
1037
1038
1039 val print_bytes : bytes -> unit
1040
1041 Print a byte sequence on standard output.
1042
1043
1044 Since 4.02.0
1045
1046
1047
1048 val print_int : int -> unit
1049
1050 Print an integer, in decimal, on standard output.
1051
1052
1053
1054 val print_float : float -> unit
1055
1056 Print a floating-point number, in decimal, on standard output.
1057
1058
1059
1060 val print_endline : string -> unit
1061
1062 Print a string, followed by a newline character, on standard output and
1063 flush standard output.
1064
1065
1066
1067 val print_newline : unit -> unit
1068
1069 Print a newline character on standard output, and flush standard out‐
1070 put. This can be used to simulate line buffering of standard output.
1071
1072
1073
1074
1075 === Output functions on standard error ===
1076
1077
1078 val prerr_char : char -> unit
1079
1080 Print a character on standard error.
1081
1082
1083
1084 val prerr_string : string -> unit
1085
1086 Print a string on standard error.
1087
1088
1089
1090 val prerr_bytes : bytes -> unit
1091
1092 Print a byte sequence on standard error.
1093
1094
1095 Since 4.02.0
1096
1097
1098
1099 val prerr_int : int -> unit
1100
1101 Print an integer, in decimal, on standard error.
1102
1103
1104
1105 val prerr_float : float -> unit
1106
1107 Print a floating-point number, in decimal, on standard error.
1108
1109
1110
1111 val prerr_endline : string -> unit
1112
1113 Print a string, followed by a newline character on standard error and
1114 flush standard error.
1115
1116
1117
1118 val prerr_newline : unit -> unit
1119
1120 Print a newline character on standard error, and flush standard error.
1121
1122
1123
1124
1125 === Input functions on standard input ===
1126
1127
1128 val read_line : unit -> string
1129
1130 Flush standard output, then read characters from standard input until a
1131 newline character is encountered. Return the string of all characters
1132 read, without the newline character at the end.
1133
1134
1135
1136 val read_int : unit -> int
1137
1138 Flush standard output, then read one line from standard input and con‐
1139 vert it to an integer. Raise Failure int_of_string if the line read is
1140 not a valid representation of an integer.
1141
1142
1143
1144 val read_int_opt : unit -> int option
1145
1146 Same as read_int_opt , but returs None instead of raising.
1147
1148
1149 Since 4.05
1150
1151
1152
1153 val read_float : unit -> float
1154
1155 Flush standard output, then read one line from standard input and con‐
1156 vert it to a floating-point number. The result is unspecified if the
1157 line read is not a valid representation of a floating-point number.
1158
1159
1160
1161 val read_float_opt : unit -> float option
1162
1163 Flush standard output, then read one line from standard input and con‐
1164 vert it to a floating-point number. Returns None if the line read is
1165 not a valid representation of a floating-point number.
1166
1167
1168 Since 4.05.0
1169
1170
1171
1172
1173 === General output functions ===
1174
1175
1176 type open_flag =
1177 | Open_rdonly (* open for reading.
1178 *)
1179 | Open_wronly (* open for writing.
1180 *)
1181 | Open_append (* open for appending: always write at end of file.
1182 *)
1183 | Open_creat (* create the file if it does not exist.
1184 *)
1185 | Open_trunc (* empty the file if it already exists.
1186 *)
1187 | Open_excl (* fail if Open_creat and the file already exists.
1188 *)
1189 | Open_binary (* open in binary mode (no conversion).
1190 *)
1191 | Open_text (* open in text mode (may perform conversions).
1192 *)
1193 | Open_nonblock (* open in non-blocking mode.
1194 *)
1195
1196
1197 Opening modes for Pervasives.open_out_gen and Pervasives.open_in_gen .
1198
1199
1200
1201 val open_out : string -> out_channel
1202
1203 Open the named file for writing, and return a new output channel on
1204 that file, positioned at the beginning of the file. The file is trun‐
1205 cated to zero length if it already exists. It is created if it does not
1206 already exists.
1207
1208
1209
1210 val open_out_bin : string -> out_channel
1211
1212 Same as Pervasives.open_out , but the file is opened in binary mode, so
1213 that no translation takes place during writes. On operating systems
1214 that do not distinguish between text mode and binary mode, this func‐
1215 tion behaves like Pervasives.open_out .
1216
1217
1218
1219 val open_out_gen : open_flag list -> int -> string -> out_channel
1220
1221
1222 open_out_gen mode perm filename opens the named file for writing, as
1223 described above. The extra argument mode specifies the opening mode.
1224 The extra argument perm specifies the file permissions, in case the
1225 file must be created. Pervasives.open_out and Pervasives.open_out_bin
1226 are special cases of this function.
1227
1228
1229
1230 val flush : out_channel -> unit
1231
1232 Flush the buffer associated with the given output channel, performing
1233 all pending writes on that channel. Interactive programs must be care‐
1234 ful about flushing standard output and standard error at the right
1235 time.
1236
1237
1238
1239 val flush_all : unit -> unit
1240
1241 Flush all open output channels; ignore errors.
1242
1243
1244
1245 val output_char : out_channel -> char -> unit
1246
1247 Write the character on the given output channel.
1248
1249
1250
1251 val output_string : out_channel -> string -> unit
1252
1253 Write the string on the given output channel.
1254
1255
1256
1257 val output_bytes : out_channel -> bytes -> unit
1258
1259 Write the byte sequence on the given output channel.
1260
1261
1262 Since 4.02.0
1263
1264
1265
1266 val output : out_channel -> bytes -> int -> int -> unit
1267
1268
1269 output oc buf pos len writes len characters from byte sequence buf ,
1270 starting at offset pos , to the given output channel oc . Raise
1271 Invalid_argument output if pos and len do not designate a valid range
1272 of buf .
1273
1274
1275
1276 val output_substring : out_channel -> string -> int -> int -> unit
1277
1278 Same as output but take a string as argument instead of a byte
1279 sequence.
1280
1281
1282 Since 4.02.0
1283
1284
1285
1286 val output_byte : out_channel -> int -> unit
1287
1288 Write one 8-bit integer (as the single character with that code) on the
1289 given output channel. The given integer is taken modulo 256.
1290
1291
1292
1293 val output_binary_int : out_channel -> int -> unit
1294
1295 Write one integer in binary format (4 bytes, big-endian) on the given
1296 output channel. The given integer is taken modulo 2^{32. The only
1297 reliable way to read it back is through the Pervasives.input_binary_int
1298 function. The format is compatible across all machines for a given ver‐
1299 sion of OCaml.
1300
1301
1302
1303 val output_value : out_channel -> 'a -> unit
1304
1305 Write the representation of a structured value of any type to a chan‐
1306 nel. Circularities and sharing inside the value are detected and pre‐
1307 served. The object can be read back, by the function Perva‐
1308 sives.input_value . See the description of module Marshal for more
1309 information. Pervasives.output_value is equivalent to Marshal.to_chan‐
1310 nel with an empty list of flags.
1311
1312
1313
1314 val seek_out : out_channel -> int -> unit
1315
1316
1317 seek_out chan pos sets the current writing position to pos for channel
1318 chan . This works only for regular files. On files of other kinds (such
1319 as terminals, pipes and sockets), the behavior is unspecified.
1320
1321
1322
1323 val pos_out : out_channel -> int
1324
1325 Return the current writing position for the given channel. Does not
1326 work on channels opened with the Open_append flag (returns unspecified
1327 results).
1328
1329
1330
1331 val out_channel_length : out_channel -> int
1332
1333 Return the size (number of characters) of the regular file on which the
1334 given channel is opened. If the channel is opened on a file that is
1335 not a regular file, the result is meaningless.
1336
1337
1338
1339 val close_out : out_channel -> unit
1340
1341 Close the given channel, flushing all buffered write operations. Out‐
1342 put functions raise a Sys_error exception when they are applied to a
1343 closed output channel, except close_out and flush , which do nothing
1344 when applied to an already closed channel. Note that close_out may
1345 raise Sys_error if the operating system signals an error when flushing
1346 or closing.
1347
1348
1349
1350 val close_out_noerr : out_channel -> unit
1351
1352 Same as close_out , but ignore all errors.
1353
1354
1355
1356 val set_binary_mode_out : out_channel -> bool -> unit
1357
1358
1359 set_binary_mode_out oc true sets the channel oc to binary mode: no
1360 translations take place during output. set_binary_mode_out oc false
1361 sets the channel oc to text mode: depending on the operating system,
1362 some translations may take place during output. For instance, under
1363 Windows, end-of-lines will be translated from \n to \r\n . This func‐
1364 tion has no effect under operating systems that do not distinguish
1365 between text mode and binary mode.
1366
1367
1368
1369
1370 === General input functions ===
1371
1372
1373 val open_in : string -> in_channel
1374
1375 Open the named file for reading, and return a new input channel on that
1376 file, positioned at the beginning of the file.
1377
1378
1379
1380 val open_in_bin : string -> in_channel
1381
1382 Same as Pervasives.open_in , but the file is opened in binary mode, so
1383 that no translation takes place during reads. On operating systems that
1384 do not distinguish between text mode and binary mode, this function
1385 behaves like Pervasives.open_in .
1386
1387
1388
1389 val open_in_gen : open_flag list -> int -> string -> in_channel
1390
1391
1392 open_in_gen mode perm filename opens the named file for reading, as
1393 described above. The extra arguments mode and perm specify the opening
1394 mode and file permissions. Pervasives.open_in and Perva‐
1395 sives.open_in_bin are special cases of this function.
1396
1397
1398
1399 val input_char : in_channel -> char
1400
1401 Read one character from the given input channel. Raise End_of_file if
1402 there are no more characters to read.
1403
1404
1405
1406 val input_line : in_channel -> string
1407
1408 Read characters from the given input channel, until a newline character
1409 is encountered. Return the string of all characters read, without the
1410 newline character at the end. Raise End_of_file if the end of the file
1411 is reached at the beginning of line.
1412
1413
1414
1415 val input : in_channel -> bytes -> int -> int -> int
1416
1417
1418 input ic buf pos len reads up to len characters from the given channel
1419 ic , storing them in byte sequence buf , starting at character number
1420 pos . It returns the actual number of characters read, between 0 and
1421 len (inclusive). A return value of 0 means that the end of file was
1422 reached. A return value between 0 and len exclusive means that not all
1423 requested len characters were read, either because no more characters
1424 were available at that time, or because the implementation found it
1425 convenient to do a partial read; input must be called again to read the
1426 remaining characters, if desired. (See also Pervasives.really_input
1427 for reading exactly len characters.) Exception Invalid_argument input
1428 is raised if pos and len do not designate a valid range of buf .
1429
1430
1431
1432 val really_input : in_channel -> bytes -> int -> int -> unit
1433
1434
1435 really_input ic buf pos len reads len characters from channel ic ,
1436 storing them in byte sequence buf , starting at character number pos .
1437 Raise End_of_file if the end of file is reached before len characters
1438 have been read. Raise Invalid_argument really_input if pos and len do
1439 not designate a valid range of buf .
1440
1441
1442
1443 val really_input_string : in_channel -> int -> string
1444
1445
1446 really_input_string ic len reads len characters from channel ic and
1447 returns them in a new string. Raise End_of_file if the end of file is
1448 reached before len characters have been read.
1449
1450
1451 Since 4.02.0
1452
1453
1454
1455 val input_byte : in_channel -> int
1456
1457 Same as Pervasives.input_char , but return the 8-bit integer represent‐
1458 ing the character. Raise End_of_file if an end of file was reached.
1459
1460
1461
1462 val input_binary_int : in_channel -> int
1463
1464 Read an integer encoded in binary format (4 bytes, big-endian) from the
1465 given input channel. See Pervasives.output_binary_int . Raise
1466 End_of_file if an end of file was reached while reading the integer.
1467
1468
1469
1470 val input_value : in_channel -> 'a
1471
1472 Read the representation of a structured value, as produced by Perva‐
1473 sives.output_value , and return the corresponding value. This function
1474 is identical to Marshal.from_channel ; see the description of module
1475 Marshal for more information, in particular concerning the lack of type
1476 safety.
1477
1478
1479
1480 val seek_in : in_channel -> int -> unit
1481
1482
1483 seek_in chan pos sets the current reading position to pos for channel
1484 chan . This works only for regular files. On files of other kinds, the
1485 behavior is unspecified.
1486
1487
1488
1489 val pos_in : in_channel -> int
1490
1491 Return the current reading position for the given channel.
1492
1493
1494
1495 val in_channel_length : in_channel -> int
1496
1497 Return the size (number of characters) of the regular file on which the
1498 given channel is opened. If the channel is opened on a file that is
1499 not a regular file, the result is meaningless. The returned size does
1500 not take into account the end-of-line translations that can be per‐
1501 formed when reading from a channel opened in text mode.
1502
1503
1504
1505 val close_in : in_channel -> unit
1506
1507 Close the given channel. Input functions raise a Sys_error exception
1508 when they are applied to a closed input channel, except close_in ,
1509 which does nothing when applied to an already closed channel.
1510
1511
1512
1513 val close_in_noerr : in_channel -> unit
1514
1515 Same as close_in , but ignore all errors.
1516
1517
1518
1519 val set_binary_mode_in : in_channel -> bool -> unit
1520
1521
1522 set_binary_mode_in ic true sets the channel ic to binary mode: no
1523 translations take place during input. set_binary_mode_out ic false
1524 sets the channel ic to text mode: depending on the operating system,
1525 some translations may take place during input. For instance, under
1526 Windows, end-of-lines will be translated from \r\n to \n . This func‐
1527 tion has no effect under operating systems that do not distinguish
1528 between text mode and binary mode.
1529
1530
1531
1532
1533 === Operations on large files ===
1534
1535
1536 module LargeFile : sig end
1537
1538
1539 Operations on large files. This sub-module provides 64-bit variants of
1540 the channel functions that manipulate file positions and file sizes.
1541 By representing positions and sizes by 64-bit integers (type int64 )
1542 instead of regular integers (type int ), these alternate functions
1543 allow operating on files whose sizes are greater than max_int .
1544
1545
1546
1547
1548 === References ===
1549
1550
1551 type 'a ref = {
1552
1553 mutable contents : 'a ;
1554 }
1555
1556
1557 The type of references (mutable indirection cells) containing a value
1558 of type 'a .
1559
1560
1561
1562 val ref : 'a -> 'a ref
1563
1564 Return a fresh reference containing the given value.
1565
1566
1567
1568 val (!) : 'a ref -> 'a
1569
1570
1571 !r returns the current contents of reference r . Equivalent to fun r
1572 -> r.contents .
1573
1574
1575
1576 val (:=) : 'a ref -> 'a -> unit
1577
1578
1579 r := a stores the value of a in reference r . Equivalent to fun r v ->
1580 r.contents <- v .
1581
1582
1583
1584 val incr : int ref -> unit
1585
1586 Increment the integer contained in the given reference. Equivalent to
1587 fun r -> r := succ !r .
1588
1589
1590
1591 val decr : int ref -> unit
1592
1593 Decrement the integer contained in the given reference. Equivalent to
1594 fun r -> r := pred !r .
1595
1596
1597
1598
1599 === Result type ===
1600
1601
1602 type ('a, 'b) result =
1603 | Ok of 'a
1604 | Error of 'b
1605
1606
1607 Since 4.03.0
1608
1609
1610
1611
1612 === Operations on format strings ===
1613
1614
1615 === Format strings are character strings with special lexical conven‐
1616 tions that defines the functionality of formatted input/output func‐
1617 tions. Format strings are used to read data with formatted input func‐
1618 tions from module Scanf and to print data with formatted output func‐
1619 tions from modules Printf and Format. Format strings are made of three
1620 kinds of entities: - conversions specifications, introduced by the spe‐
1621 cial character '%' followed by one or more characters specifying what
1622 kind of argument to read or print, - formatting indications, introduced
1623 by the special character '@' followed by one or more characters speci‐
1624 fying how to read or print the argument, - plain characters that are
1625 regular characters with usual lexical conventions. Plain characters
1626 specify string literals to be read in the input or printed in the out‐
1627 put. There is an additional lexical rule to escape the special charac‐
1628 ters '%' and '@' in format strings: if a special character follows a
1629 '%' character, it is treated as a plain character. In other words, %%
1630 is considered as a plain '%' and %@ as a plain '@'. For more informa‐
1631 tion about conversion specifications and formatting indications avail‐
1632 able, read the documentation of modules Scanf, Printf and Format. ===
1633
1634
1635 === Format strings have a general and highly polymorphic type ('a, 'b,
1636 'c, 'd, 'e, 'f) format6. The two simplified types, format and format4
1637 below are included for backward compatibility with earlier releases of
1638 OCaml. The meaning of format string type parameters is as follows: -
1639 'a is the type of the parameters of the format for formatted output
1640 functions (printf-style functions); 'a is the type of the values read
1641 by the format for formatted input functions (scanf-style functions). -
1642 'b is the type of input source for formatted input functions and the
1643 type of output target for formatted output functions. For printf-style
1644 functions from module Printf, 'b is typically out_channel; for
1645 printf-style functions from module Format, 'b is typically Format.for‐
1646 matter; for scanf-style functions from module Scanf, 'b is typically
1647 Scanf.Scanning.in_channel. Type argument 'b is also the type of the
1648 first argument given to user's defined printing functions for %a and %t
1649 conversions, and user's defined reading functions for %r conversion. -
1650 'c is the type of the result of the %a and %t printing functions, and
1651 also the type of the argument transmitted to the first argument of
1652 kprintf-style functions or to the kscanf-style functions. - 'd is the
1653 type of parameters for the scanf-style functions. - 'e is the type of
1654 the receiver function for the scanf-style functions. - 'f is the final
1655 result type of a formatted input/output function invocation: for the
1656 printf-style functions, it is typically unit; for the scanf-style func‐
1657 tions, it is typically the result type of the receiver function. ===
1658
1659
1660 type ('a, 'b, 'c, 'd, 'e, 'f) format6 = ('a, 'b, 'c, 'd, 'e, 'f) Cam‐
1661 linternalFormatBasics.format6
1662
1663
1664
1665
1666 type ('a, 'b, 'c, 'd) format4 = ('a, 'b, 'c, 'c, 'c, 'd) format6
1667
1668
1669
1670
1671 type ('a, 'b, 'c) format = ('a, 'b, 'c, 'c) format4
1672
1673
1674
1675
1676
1677 val string_of_format : ('a, 'b, 'c, 'd, 'e, 'f) format6 -> string
1678
1679 Converts a format string into a string.
1680
1681
1682
1683 val format_of_string : ('a, 'b, 'c, 'd, 'e, 'f) format6 -> ('a, 'b, 'c,
1684 'd, 'e, 'f) format6
1685
1686
1687 format_of_string s returns a format string read from the string literal
1688 s . Note: format_of_string can not convert a string argument that is
1689 not a literal. If you need this functionality, use the more general
1690 Scanf.format_from_string function.
1691
1692
1693
1694 val (^^) : ('a, 'b, 'c, 'd, 'e, 'f) format6 -> ('f, 'b, 'c, 'e, 'g, 'h)
1695 format6 -> ('a, 'b, 'c, 'd, 'g, 'h) format6
1696
1697
1698 f1 ^^ f2 catenates format strings f1 and f2 . The result is a format
1699 string that behaves as the concatenation of format strings f1 and f2 :
1700 in case of formatted output, it accepts arguments from f1 , then argu‐
1701 ments from f2 ; in case of formatted input, it returns results from f1
1702 , then results from f2 .
1703
1704
1705
1706
1707 === Program termination ===
1708
1709
1710 val exit : int -> 'a
1711
1712 Terminate the process, returning the given status code to the operating
1713 system: usually 0 to indicate no errors, and a small positive integer
1714 to indicate failure. All open output channels are flushed with
1715 flush_all . An implicit exit 0 is performed each time a program termi‐
1716 nates normally. An implicit exit 2 is performed if the program termi‐
1717 nates early because of an uncaught exception.
1718
1719
1720
1721 val at_exit : (unit -> unit) -> unit
1722
1723 Register the given function to be called at program termination time.
1724 The functions registered with at_exit will be called when the program
1725 executes Pervasives.exit , or terminates, either normally or because of
1726 an uncaught exception. The functions are called in 'last in, first
1727 out' order: the function most recently added with at_exit is called
1728 first.
1729
1730
1731
1732
1733
17342018-04-14 source: Pervasives(3)