caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Lauri Alanko <la@iki.fi>
To: caml-list@inria.fr
Subject: [Caml-list] Signature monomorphism in functors
Date: Tue, 18 Jan 2011 04:00:19 +0200	[thread overview]
Message-ID: <20110118020018.GI323@melkinpaasi.cs.helsinki.fi> (raw)

I stumbled into a limitation in functors:


module type S1 = sig
  type t
end

module type S2 = sig
  include S1
  val v : t
end

module type S3 = sig
  module M : S1
end

module M2 : S2 = struct
  type t = int
  let v = 42
end

module M3A : S3 with module M = M2 = struct
  module M = M2
end

module F(M_ : S1) : S3 with module M = M_ = struct
  module M = M_
end

module M3B : S3 with module M = M2 = F(M2)


Here, M3A compiles ok, but when I try to do the same thing through a
functor, I get an error upon the definition of M3B:

Error: Signature mismatch:
       Modules do not match:
         sig module M : sig type t = M2.t end end
       is not included in
         sig module M : sig type t = M2.t val v : t end end
       Modules do not match:
         sig type t = M2.t end
       is not included in
         sig type t = M2.t val v : t end
       The field `v' is required but not provided

I can kind of understand the problem: modules are implemented as
records, and signature coercions are translated to record
relayouts. The functor code expects the layout for S1, so the argument
M2 : S2 gets coerced into S1 and thus the extra fields in S2 are
hidden and cannot be exposed in the result of the functor.

In my particular situation, this isn't a horrible problem, and I can
live with F(M2) : S3 with type M.t = M2.t, since the module M2 with
its extra fields is visible directly and doesn't need to be accessed
through F(M2). But still, this seems like a very unintuitive
limitation: abstracting a module member into a functor suddenly
changed the expressivity of the program. It also seems to stem
directly from implementation limitations instead of any theoretical
problems.

I'd be interested in hearing if anyone has stumbled into this problem
earlier, and if there are any known workarounds. Also, are there any
plans to remove this limitation from the language?


Lauri

             reply	other threads:[~2011-01-18  2:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-18  2:00 Lauri Alanko [this message]
2011-01-18 10:02 ` rossberg
2011-01-18 10:22   ` rossberg
2011-01-18 12:10   ` Lauri Alanko
2011-01-18 14:03     ` Jacques Garrigue
2011-01-18 16:41     ` rossberg

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=20110118020018.GI323@melkinpaasi.cs.helsinki.fi \
    --to=la@iki.fi \
    --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).