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