Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds an example on how to create a monad transformer stack (#1354) #1510

Merged
merged 1 commit into from
Jun 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/monads/monads.mli
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,27 @@ open Core_kernel[@@warning "-D"]
Each monad transformer creates a module that has functions [lift]
and [run]. The [lift] function lifts original computation into the
transformed one, and [run] will run the computation.

In order to use the monad transformers to build up a monad
transformer stack, the inner module needs both the monad types and
monad functions, so that it satisfies the [Monad.S] signature.
Therefore, it needs to include both [T] and [Make] (or [T2] and
[Make2]), in order to use the [Make] functor of the outer monad.
For example, to create a state monad nested inside a writer monad,
you can do the following.

{[
module St = struct
module Env = struct
type t = int
end

include Monad.State.T1(Env)(Monad.Ident) (* adds types *)
include Monad.State.Make(Env)(Monad.Ident) (* adds functions and modules *)
end

module W = Monad.Writer.Make(Monoid.String)(St)
]}
*)
module Std : sig

Expand Down