Module Io

module Io: sig .. end
Rudimentary I/O-monad


IO-Monad



Types


type error = 
| EndOfFile (*Tried to read beyond end of file*)
| IntOfString (*Could not convert string to int*)
| SysError of string (*Operating system signaled an error. The string takes the precise reason.*)
Possible errors during an IO-operation. They are named after their imperative counterparts.
type world 
The imperative, "outside" world the functional IO-monad deals with.
type ('left, 'right) either 
This type takes care of the control-flow similarly to an exception monad. The 'left type is for errors, the 'right type for correct values.
type 'a t = world -> (error, 'a) either * world 
An IO-monad of type 'a takes the current world and produces either a correct value (of type 'a or an error and a new world.

Run-Time Support


val __conjure_up : unit -> world
__conjure_up ()

Conjure up a new "world".

This function should only be used once per program. It is best place in some pre-main initialization code like, for example,

        let () =
          let world = Io.__conjure_up () in
            ignore ((Io.catch (main ()) (fun _error -> ...)) world)
    
where we call main with the initial world.

Fundamental Functions


val bind : 'a t -> ('a -> 'b t) -> 'b t
bind an_iomonad a_function

Apply a_function to an_iomonad producing another IO-monad. a_function takes an 'a value as argument and returns a 'b IO-monad.

val return : 'a -> 'a t
return a_value

List a_value into the IO-monad.

val throw : error -> 'a t
throw an_error

Throw an_error inside the IO-monad.

val catch : 'a t -> (error -> 'a t) -> 'a t
catch an_iomonad a_handler_function

Catch IO-exceptions from an_iomonad and feed them into a_handler_function, which takes an error value as argument and returns an 'a IO-monad.


Output Functions

All of these functions have exactly the same names as their imperative counterparts. Moreover, they take the same arguments.

val print_char : char -> unit t
print_char a_character
val print_string : string -> unit t
print_string a_string
val print_int : int -> unit t
print_int an_integer
val print_float : float -> unit t
print_float a_float
val print_endline : string -> unit t
print_endline a_string
val print_newline : unit -> unit t
print_newline ()
val prerr_char : char -> unit t
prerr_char a_character
val prerr_string : string -> unit t
prerr_string a_string
val prerr_int : int -> unit t
prerr_int an_integer
val prerr_float : float -> unit t
prerr_float a_float
val prerr_endline : string -> unit t
prerr_endline a_string
val prerr_newline : unit -> unit t
prerr_newline ()

Input Functions


val read_line : unit -> string t
read_line ()
val read_int : unit -> int t
read_int ()
val read_float : unit -> float t
read_float ()

General Output Functions


val open_out : string -> Pervasives.out_channel t
open_out a_filename
val output_char : Pervasives.out_channel -> char -> unit t
output_char a_channel a_char
val output_string : Pervasives.out_channel -> string -> unit t
output_string a_channel a_string
val close_out : Pervasives.out_channel -> unit t
close_out a_channel