Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* [Caml-list] The "dummy" monad
@ 2012-08-03 14:20 Dario Teixeira
  2012-08-03 14:30 ` Jacques Carette
  0 siblings, 1 reply; 2+ messages in thread
From: Dario Teixeira @ 2012-08-03 14:20 UTC (permalink / raw)
  To: O Caml Mailing List

Hi,

I have a question about terminology.  Suppose I have a functor whose
implementation relies on a "get" function defined in the functor's parameter,
which is a module of type GETTER.  I want this functor to handle GETTER
implementations which may rely on the lightweight threads of Lwt.  The keyword
is "may", because I also want to allow GETTER implementations using another
monad other than Lwt's, or implementations not using any monad altogether.

To implement this I'm using a technique inspired by Jérôme Vouillon's
refactorisation of PG'OCaml to work in a monadic style.  Essentially,
I require any module implementing GETTER to define the monad it uses:

module type GETTER =
sig
    module Monad:
    sig
        type 'a t

        val return: 'a -> 'a t
        val fail: exn -> 'a t
        val bind: 'a t -> ('a -> 'b t) -> 'b t
        val catch: (unit -> 'a t) -> (exn -> 'a t) -> 'a t
    end

    val get: string -> string Monad.t
end


An implementation "Mygetter1" using Lwt can be defined as follows:

module Mygetter1: GETTER with type 'a Monad.t = 'a Lwt.t =
struct
    module Monad = Lwt

    let get x = ...
end


Whereas an implementation not requiring any special monad would be defined
as listed below, in essence defining a "dummy" monad.

module Mygetter2: GETTER with type 'a Monad.t = 'a =
struct
    module Monad =
    struct
        type 'a t = 'a

        let return x = x
        let fail x = raise x
        let bind t f = f t
        let catch t f = try t () with exc -> f exc
    end

    let get x = ...
end


Now, I suspect this is a well-known technique in monad-breathing circles.
But what is it called?  In particular, what should I be calling the "dummy"
monad?

Thanks in advance!
Best regards,
Dario Teixeira


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-08-03 14:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-03 14:20 [Caml-list] The "dummy" monad Dario Teixeira
2012-08-03 14:30 ` Jacques Carette

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox