monad instance for CCFun

This commit is contained in:
Simon Cruanes 2014-06-26 15:01:34 +02:00
parent 64fedce1b0
commit 6c19918240
2 changed files with 18 additions and 0 deletions

View file

@ -58,3 +58,10 @@ let finally ~h ~f =
with e ->
h ();
raise e
module Monad(X : sig type t end) = struct
type 'a t = X.t -> 'a
let return x _ = x
let (>|=) f g x = g (f x)
let (>>=) f g x = g (f x) x
end

View file

@ -67,3 +67,14 @@ val finally : h:(unit -> unit) -> f:(unit -> 'a) -> 'a
(** [finally h f] calls [f ()] and returns its result. If it raises, the
same exception is raised; in {b any} case, [h ()] is called after
[f ()] terminates. *)
(** {2 Monad}
functions with a fixed domain are monads in their codomain *)
module Monad(X : sig type t end) : sig
type 'a t = X.t -> 'a
val return : 'a -> 'a t
val (>|=) : 'a t -> ('a -> 'b) -> 'b t
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
end