caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Anonymous sum types in functors
Date: Sun, 23 Jun 2013 17:14:50 +0200	[thread overview]
Message-ID: <51C710EA.5090100@inria.fr> (raw)
In-Reply-To: <001601ce6fe1$a2ea9390$e8bfbab0$@metastack.com>

On 23/06/13 09:16, David Allsopp wrote:

> a) Is there a way to do it where you can end up with type elt = A | B (I
> think the answer is no?)

No, because that wouldn't quite respect the generativity of sum type
definitions, but you can get close, see below.

> b) Is there a syntactically lighter way to write the module definition?

I would recommend naming your anonymous "struct":

module Flag = struct
  type t = A | B
  let compare = compare
  (* Other useful operations over flags, e.g. *)
  let to_string = function A -> "A" | B -> "B"
end

module FlagSet = Set.Make(Flag)

Then you get

FlagSet: sig
  type elt = Flag.t
  type t
  ...
end

The good thing about this solution is that it gives you a natural
place to put additional operations over flags, like "to_string" above,
shall you ever need them.

> the idea is that I'd
> be able to write Flag1Set.add Flag1Set.CommonFlag Flag1Set.empty and
> Flag2Set.add Flag2Set.CommonFlag Flag2Set.empty, etc.

Why not "Flag1Set.add Flag1.CommonFlag Flag1Set.empty"?  Flags morally
come from Flag1, not from Flag1Set.

As an aside, some ML-like module systems require functor arguments to
be module names or paths, e.g. Coq's.  OCaml accepts anonymous structs
as arguments, and does the best it can with them, but if the struct
contains generative type definitions, some information is necessarily
lost.

Hope this helps,

- Xavier Leroy

  reply	other threads:[~2013-06-23 15:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-23  7:16 David Allsopp
2013-06-23 15:14 ` Xavier Leroy [this message]
2013-06-23 23:15 ` Jacques Garrigue
2013-06-24  9:39   ` David Allsopp
2013-06-24 14:37     ` Kristopher Micinski
2013-06-24 14:41       ` Raphaël Proust
2013-06-24 19:13         ` Kristopher Micinski
2013-06-25  8:39       ` David Allsopp
     [not found] <563607038852382852@orange.fr>
2013-06-23  9:48 ` David Allsopp

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=51C710EA.5090100@inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    /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).