Source

spotlib / lib / monad_intf.ml

(** Monad interface *)

(** Minimum monad interface *)
module type S = sig
  type +'a t
  val return : 'a -> 'a t
  val bind : 'a t -> ('a -> 'b t) -> 'b t
end

(** Opened signature with very common names and infixes of Monads *)
module type Open = sig
  type +'a t
  val bind : 'a t -> ('a -> 'b t) -> 'b t
  val ( >>= ) : 'a t -> ('a -> 'b t) -> 'b t
  val fmap : ('a -> 'b) -> 'a t -> 'b t
  val ( >>| ) : 'a t -> ('a -> 'b) -> 'b t
  val return : 'a -> 'a t
    
    (** Applicative style binops *)
    
  val (^<$>) : ('a -> 'b) -> 'a t -> 'b t
    (** same as map, <$> in Haskell *)
    
  val (/<*>) : ('a -> 'b) t -> 'a t -> 'b t
  (** <*> in Haskell *)
end

(** Monad module signature *)
module type T = sig
  include Open
  module Open : Open with type 'a t = 'a t
  (** for [M : T], [open M.Open] may provide some common functions of [M] in the name space. *)

  val fmap : ('a -> 'b) -> 'a t -> 'b t
  val liftM : ('a -> 'b) -> 'a t -> 'b t
  val map : f:('a -> 'b) -> 'a t -> 'b t
  (** fmap in Haskell *)

  val fmap2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
  val liftM2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t


  val void : 'a t -> unit t

  val seq : 'a t list -> 'a list t
  (** sequence in Haskell. Not tail recursive. *)

  val seq_ : unit t list -> unit t
  (** sequence_ in Haskell. Tail recursive. *)

  val mapM : ('a -> 'b t) -> 'a list -> 'b list t
  (** Not tail recursive *)

  val iteri : (int -> 'a -> unit t) -> 'a list -> unit t
  (** Iteration with index starting from [0] *)

  val for_ : int -> int -> (int -> unit t) -> unit t
  (** for like iteration *)

  val join : 'a t t -> 'a t
end

(** Minimum monad signature with an additional type parameter ['z]  *)
module type S2 = sig
  type ('a, 'z) t
  val return : 'a -> ('a, 'z) t
  val bind : ('a, 'z) t -> ('a -> ('b, 'z) t) -> ('b, 'z) t
end

(** Open name space for [S2] *)
module type Open2 = sig
  type ('a, 'z) t
  val bind : ('a, 'z) t -> ('a -> ('b, 'z) t) -> ('b, 'z) t
  val ( >>= ) : ('a, 'z) t -> ('a -> ('b, 'z) t) -> ('b, 'z) t
  val fmap : ('a -> 'b) -> ('a, 'z) t -> ('b, 'z) t
  val ( >>| ) : ('a, 'z) t -> ('a -> 'b) -> ('b, 'z) t
  val return : 'a -> ('a, 'z) t
    
    (** Applicative style binops *)
    
  val (^<$>) : ('a -> 'b) -> ('a, 'z) t -> ('b, 'z) t
    (** same as map, <$> in Haskell *)
    
  val (/<*>) : ('a -> 'b, 'z) t -> ('a, 'z) t -> ('b, 'z) t
  (** <*> in Haskell *)
end

(** Monad module signature for [S2] *)
module type T2 = sig
  include Open2
  module Open : Open2 with type ('a, 'z) t = ('a, 'z) t

  val fmap : ('a -> 'b) -> ('a, 'z) t -> ('b, 'z) t
  val map : f:('a -> 'b) -> ('a, 'z) t -> ('b, 'z) t
    (** fmap in Haskell *)

  val void : ('a, 'z) t -> (unit, 'z) t

  val seq : ('a, 'z) t list -> ('a list, 'z) t
  (** sequence in Haskell. Not tail recursive. *)

  val seq_unit : (unit, 'z) t list -> (unit, 'z) t
  (** sequence_ in Haskell. Tail recursive. *)

  val mapM : ('a -> ('b, 'z) t) -> 'a list -> ('b list, 'z) t
  (** Not tail recursive *)

  val iteri : (int -> 'a -> (unit, 'z) t) -> 'a list -> (unit, 'z) t
  (** Iteration with index starting from [0] *)

  val for_ : int -> int -> (int -> (unit, 'z) t) -> (unit, 'z) t
  (** for like iteration *)

  val join : (('a, 'z) t, 'z) t -> ('a, 'z) t
end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.