From 6c1991824015e8e00e23c1b8252b1f72f4406c14 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 26 Jun 2014 15:01:34 +0200 Subject: [PATCH] monad instance for CCFun --- core/CCFun.ml | 7 +++++++ core/CCFun.mli | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/core/CCFun.ml b/core/CCFun.ml index fa4eadb6..55f1a337 100644 --- a/core/CCFun.ml +++ b/core/CCFun.ml @@ -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 diff --git a/core/CCFun.mli b/core/CCFun.mli index 81048ae0..40aed09e 100644 --- a/core/CCFun.mli +++ b/core/CCFun.mli @@ -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