Module Duppy.Monad

module Monad: sig .. end

Monadic interface to Duppy.Io.

This module can be used to write code that runs in various Duppy's tasks and raise values in a completely transparent way.

You can see examples of its use in the examples/ directory of the source code and in the files src/tools/{harbor.camlp4,server.camlp4} in liquidsoap's code.

When a server communicates with a client, it performs several computations and, eventually, terminates. A computation can either return a new value or terminate. For instance:

The purpose of the monad is to embed computations which can either return a new value or raise a value that is used to terminate.


type ('a, 'b) t 

Type representing a computation which returns a value of type 'a or raises a value of type 'b

val return : 'a -> ('a, 'b) t

return x create a computation that returns value x.

val raise : 'b -> ('a, 'b) t

raise x create a computation that raises value x.

val bind : ('a, 'b) t ->
('a -> ('c, 'b) t) -> ('c, 'b) t

Compose two computations. bind f g is equivalent to: let x = f in g x where x has f's return type.

val (>>=) : ('a, 'b) t ->
('a -> ('c, 'b) t) -> ('c, 'b) t

>>= is an alternative notation for bind

val run : return:('a -> unit) -> raise:('b -> unit) -> ('a, 'b) t -> unit

run f ~return ~raise () executes f and process returned values with return or raised values with raise.

val catch : ('a, 'b) t ->
('b -> ('a, 'c) t) -> ('a, 'c) t

catch f g redirects values x raised during f's execution to g. The name suggests the usual try .. with .. exception catching.

val (=<<) : ('b -> ('a, 'c) t) ->
('a, 'b) t -> ('a, 'c) t

=<< is an alternative notation for catch.

val fold_left : ('a -> 'b -> ('a, 'c) t) ->
'a -> 'b list -> ('a, 'c) t

fold_left f a [b1; b2; ..] returns computation (f a b1) >>= (fun a -> f a b2) >>= ...

val iter : ('a -> (unit, 'b) t) -> 'a list -> (unit, 'b) t

iter f [x1; x2; ..] returns computation f x1 >>= (fun () -> f x2) >>= ...

module Mutex: sig .. end

This module implements monadic mutex computations.

module Condition: sig .. end

This module implements monadic condition computations.

module type Monad_io_t = sig .. end

This module implements monadic computations using Duppy.Io.

module MakeIo: 
functor (Io : Duppy.Io_t) -> Monad_io_t with type socket = Io.socket and module Io = Io
module Io: Monad_io_t  with type socket = Unix.file_descr and module Io = Io