mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-08 12:15:32 -05:00
monad instance for CCFun
This commit is contained in:
parent
64fedce1b0
commit
6c19918240
2 changed files with 18 additions and 0 deletions
|
|
@ -58,3 +58,10 @@ let finally ~h ~f =
|
||||||
with e ->
|
with e ->
|
||||||
h ();
|
h ();
|
||||||
raise e
|
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
|
||||||
|
|
|
||||||
|
|
@ -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
|
(** [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
|
same exception is raised; in {b any} case, [h ()] is called after
|
||||||
[f ()] terminates. *)
|
[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
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue