caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: rossberg@mpi-sws.org
To: "Lauri Alanko" <la@iki.fi>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Signature monomorphism in functors
Date: Tue, 18 Jan 2011 11:02:31 +0100 (CET)	[thread overview]
Message-ID: <7f9a42a3cac8ea539f9f48432cd8a5af.squirrel@mail.mpi-sws.org> (raw)
In-Reply-To: <20110118020018.GI323@melkinpaasi.cs.helsinki.fi>

> 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.

I disagree with this assessment: you can abstract the module just fine, as
long as you do it at the signature you were using it at, namely S2, not S1.
I don't think it should be surprising that you narrow the type of an
expression when you narrow the types of its free variables.

> It also seems to stem
> directly from implementation limitations instead of any theoretical
> problems.

I wouldn't say so. Coercive subtyping is not just an implementation detail,
it is an essential part of the language design and semantics, and visible in
other places. It might not be the only possible design, but it's not clear
what the alternatives would be, either. Consider for example:

  module F (X : sig val x : int end) =
  struct
    val y = 1
    include X
    val z = x + y
  end

Without coercive subtyping semantics, what would be the meaning of:

  module A = F (struct val x = 2 val y = 3 end)

Would it even be legal? If not, why not? And then consider the case where y
isn't a value but a type.

/Andreas


  reply	other threads:[~2011-01-18 10:02 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-18  2:00 Lauri Alanko
2011-01-18 10:02 ` rossberg [this message]
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=7f9a42a3cac8ea539f9f48432cd8a5af.squirrel@mail.mpi-sws.org \
    --to=rossberg@mpi-sws.org \
    --cc=caml-list@inria.fr \
    --cc=la@iki.fi \
    /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).