caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Signature monomorphism in functors
@ 2011-01-18  2:00 Lauri Alanko
  2011-01-18 10:02 ` rossberg
  0 siblings, 1 reply; 6+ messages in thread
From: Lauri Alanko @ 2011-01-18  2:00 UTC (permalink / raw)
  To: caml-list

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

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

end of thread, other threads:[~2011-01-18 16:41 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-18  2:00 [Caml-list] Signature monomorphism in functors Lauri Alanko
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

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