caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Martin Jambon <martin.jambon@ens-lyon.org>
To: Yotam Barnoy <yotambarnoy@gmail.com>
Cc: Ocaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Default methods for module signatures
Date: Wed, 05 Feb 2014 12:10:42 -0800	[thread overview]
Message-ID: <52F29AC2.1070700@ens-lyon.org> (raw)
In-Reply-To: <52F2944A.3000908@ens-lyon.org>

On Wed 05 Feb 2014 11:43:06 AM PST, Martin Jambon wrote:
> On Wed 05 Feb 2014 10:49:29 AM PST, Yotam Barnoy wrote:
>> Hello List
>>
>> I would like the following feature, and I'm not enough of an expert in
>> module-fu to know if something like this is doable.
>>
>> Suppose I have a module signature of
>>
>> module type Monad = sig
>>   type 'a m
>>   val return : 'a -> 'a m
>>   val (>>=) : 'a m -> ('a -> 'b m) -> 'b m
>>   val (>>) : 'a m -> 'b m -> 'b m
>> end
>>
>> I would like to have a default implementation for (>>), since a simple
>> default implementation is
>>
>> let (>>) m f = m >>= fun _ -> f
>>
>> Alternatively, I would like to include this from some DefaultMonad
>> module, but have the (>>=) referred to in the function be my newly
>> defined (>>=) implementation (ie. late binding). Is there currently
>> any way to do this? If not, would there be a way to implement a
>> partial default implementation built into or associated with a module
>> signature? Something like
>
> OCaml has functors but they don't support optional fields in their
> arguments.
>
> You can create a functor Monad.Make that takes a module without (>>)
> and creates a module with an extra (>>) field. However, if the input
> module contains (>>) already, the new implementation will override it
> as in this minimal example:
>
> # module A = struct end;;
> module A : sig  end
>
> # module M(A: module type of A) = struct include A let x = 0 end;;
> module M : functor (A : sig  end) -> sig val x : int end
>
> # module B = M(struct end);;
> module B : sig val x : int end
>
> # module C = M(struct let x = 1 end);;
> module C : sig val x : int end
>
> # C.x;;
> - : int = 0
>
> C.x is 0, not 1.
>
>
> There may be clever tricks to support some kind of optional module
> field pattern, though. I'll let others scratch their heads. :-)

The following works, but each optional field must be explicitely set to 
None:

module type Partial = sig
  val x : int option
end

module type Full = sig
  val x : int
end

module Complete (X : Partial) : Full = struct
  include X
  let x =
    match X.x with
    | None -> 0
    | Some v -> v
end

module A = Complete (struct
  let x = None
end)

module B = Complete (struct
  let x = Some 1
end)

let () =
  assert (A.x = 0);
  assert (B.x = 1)



>
>> module type Monad = sig... default struct... end
>>
>> Haskell has this available as part of the semantics of their typeclass
>> system, and I think it would be really handy to have (if there isn't
>> already a way to do it currently).
>>
>> -Yotam
>>
>>
>



  parent reply	other threads:[~2014-02-05 20:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-05 18:49 Yotam Barnoy
2014-02-05 19:43 ` Martin Jambon
2014-02-05 20:03   ` Yotam Barnoy
2014-02-05 20:10   ` Martin Jambon [this message]
2014-02-05 19:53 ` Pippijn van Steenhoven
2014-02-05 20:29 ` Alain Frisch
2014-02-05 21:17   ` Yotam Barnoy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=52F29AC2.1070700@ens-lyon.org \
    --to=martin.jambon@ens-lyon.org \
    --cc=caml-list@inria.fr \
    --cc=yotambarnoy@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).